Guest User

Untitled

a guest
Mar 27th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. /**
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package org.apache.hadoop.hbase.client;
  20.  
  21. import java.io.Closeable;
  22. import java.io.IOException;
  23. import java.io.InterruptedIOException;
  24.  
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.hadoop.conf.Configuration;
  28. import org.apache.hadoop.hbase.HConstants;
  29. import org.apache.hadoop.hbase.classification.InterfaceAudience;
  30. import org.apache.hadoop.hbase.classification.InterfaceStability;
  31. import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
  32. import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneRequest;
  33. import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneResponse;
  34. import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
  35. import org.apache.hadoop.hbase.snapshot.SnapshotCreationException;
  36. import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
  37.  
  38. import com.google.protobuf.ServiceException;
  39.  
  40. /**
  41. * The administrative API for HBase. Obtain an instance from an {@link Connection#getAdmin()} and
  42. */
  43. @InterfaceAudience.Public
  44. @InterfaceStability.Evolving
  45. public class AdminUtil {
  46. private static final Log LOG = LogFactory.getLog(AdminUtil.class);
  47. private volatile Configuration conf;
  48. private RpcRetryingCallerFactory rpcCallerFactory;
  49. private int operationTimeout;
  50. private long pause;
  51. private final int numRetries;
  52.  
  53. public AdminUtil(Configuration conf) {
  54. this.conf = conf;
  55. this.rpcCallerFactory = RpcRetryingCallerFactory.instantiate(this.conf);
  56. this.operationTimeout = this.conf.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
  57. HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
  58. this.pause = conf.getLong(HConstants.HBASE_CLIENT_PAUSE,
  59. HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
  60. this.numRetries = this.conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
  61. HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
  62. }
  63.  
  64. private <C extends RetryingCallable<V> & Closeable, V> V executeCallable(C callable)
  65. throws IOException {
  66. return executeCallable(callable, rpcCallerFactory, operationTimeout);
  67. }
  68.  
  69. private static <C extends RetryingCallable<V> & Closeable, V> V executeCallable(C callable,
  70. RpcRetryingCallerFactory rpcCallerFactory, int operationTimeout) throws IOException {
  71. RpcRetryingCaller<V> caller = rpcCallerFactory.newCaller();
  72. try {
  73. return caller.callWithRetries(callable, operationTimeout);
  74. } finally {
  75. callable.close();
  76. }
  77. }
  78.  
  79. public static long getPauseTime(int tries, long pause) {
  80. int triesCount = tries;
  81. if (triesCount >= HConstants.RETRY_BACKOFF.length) {
  82. triesCount = HConstants.RETRY_BACKOFF.length - 1;
  83. }
  84. return pause * HConstants.RETRY_BACKOFF[triesCount];
  85. }
  86.  
  87. public void waitForSnapshot(SnapshotDescription snapshot, long max,
  88. HConnection conn) throws IOException {
  89. final IsSnapshotDoneRequest request = IsSnapshotDoneRequest.newBuilder().setSnapshot(snapshot)
  90. .build();
  91. IsSnapshotDoneResponse done = null;
  92. long start = EnvironmentEdgeManager.currentTime();
  93. long maxPauseTime = max / numRetries;
  94. int tries = 0;
  95. LOG.debug("Waiting a max of " + max + " ms for snapshot '" +
  96. ClientSnapshotDescriptionUtils.toString(snapshot) + "'' to complete. (max " +
  97. maxPauseTime + " ms per retry)");
  98. while (tries == 0
  99. || ((EnvironmentEdgeManager.currentTime() - start) < max && !done.getDone())) {
  100. try {
  101. // sleep a backoff <= pauseTime amount
  102. long sleep = getPauseTime(tries++, pause);
  103. sleep = sleep > maxPauseTime ? maxPauseTime : sleep;
  104. LOG.debug("(#" + tries + ") Sleeping: " + sleep +
  105. "ms while waiting for snapshot completion.");
  106. Thread.sleep(sleep);
  107. } catch (InterruptedException e) {
  108. throw (InterruptedIOException)new InterruptedIOException("Interrupted").initCause(e);
  109. }
  110. LOG.debug("Getting current status of snapshot from master...");
  111. done = executeCallable(new MasterCallable<IsSnapshotDoneResponse>(conn) {
  112. @Override
  113. public IsSnapshotDoneResponse call(int callTimeout) throws ServiceException {
  114. return master.isSnapshotDone(null, request);
  115. }
  116. });
  117. }
  118. if (!done.getDone()) {
  119. throw new SnapshotCreationException("Snapshot '" + snapshot.getName()
  120. + "' wasn't completed in expectedTime:" + max + " ms", snapshot);
  121. }
  122.  
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment