Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.apache.hadoop.hbase.client;
- import java.io.Closeable;
- import java.io.IOException;
- import java.io.InterruptedIOException;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HConstants;
- import org.apache.hadoop.hbase.classification.InterfaceAudience;
- import org.apache.hadoop.hbase.classification.InterfaceStability;
- import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
- import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneRequest;
- import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneResponse;
- import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
- import org.apache.hadoop.hbase.snapshot.SnapshotCreationException;
- import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
- import com.google.protobuf.ServiceException;
- /**
- * The administrative API for HBase. Obtain an instance from an {@link Connection#getAdmin()} and
- */
- @InterfaceAudience.Public
- @InterfaceStability.Evolving
- public class AdminUtil {
- private static final Log LOG = LogFactory.getLog(AdminUtil.class);
- private volatile Configuration conf;
- private RpcRetryingCallerFactory rpcCallerFactory;
- private int operationTimeout;
- private long pause;
- private final int numRetries;
- public AdminUtil(Configuration conf) {
- this.conf = conf;
- this.rpcCallerFactory = RpcRetryingCallerFactory.instantiate(this.conf);
- this.operationTimeout = this.conf.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
- HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
- this.pause = conf.getLong(HConstants.HBASE_CLIENT_PAUSE,
- HConstants.DEFAULT_HBASE_CLIENT_PAUSE);
- this.numRetries = this.conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
- HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
- }
- private <C extends RetryingCallable<V> & Closeable, V> V executeCallable(C callable)
- throws IOException {
- return executeCallable(callable, rpcCallerFactory, operationTimeout);
- }
- private static <C extends RetryingCallable<V> & Closeable, V> V executeCallable(C callable,
- RpcRetryingCallerFactory rpcCallerFactory, int operationTimeout) throws IOException {
- RpcRetryingCaller<V> caller = rpcCallerFactory.newCaller();
- try {
- return caller.callWithRetries(callable, operationTimeout);
- } finally {
- callable.close();
- }
- }
- public static long getPauseTime(int tries, long pause) {
- int triesCount = tries;
- if (triesCount >= HConstants.RETRY_BACKOFF.length) {
- triesCount = HConstants.RETRY_BACKOFF.length - 1;
- }
- return pause * HConstants.RETRY_BACKOFF[triesCount];
- }
- public void waitForSnapshot(SnapshotDescription snapshot, long max,
- HConnection conn) throws IOException {
- final IsSnapshotDoneRequest request = IsSnapshotDoneRequest.newBuilder().setSnapshot(snapshot)
- .build();
- IsSnapshotDoneResponse done = null;
- long start = EnvironmentEdgeManager.currentTime();
- long maxPauseTime = max / numRetries;
- int tries = 0;
- LOG.debug("Waiting a max of " + max + " ms for snapshot '" +
- ClientSnapshotDescriptionUtils.toString(snapshot) + "'' to complete. (max " +
- maxPauseTime + " ms per retry)");
- while (tries == 0
- || ((EnvironmentEdgeManager.currentTime() - start) < max && !done.getDone())) {
- try {
- // sleep a backoff <= pauseTime amount
- long sleep = getPauseTime(tries++, pause);
- sleep = sleep > maxPauseTime ? maxPauseTime : sleep;
- LOG.debug("(#" + tries + ") Sleeping: " + sleep +
- "ms while waiting for snapshot completion.");
- Thread.sleep(sleep);
- } catch (InterruptedException e) {
- throw (InterruptedIOException)new InterruptedIOException("Interrupted").initCause(e);
- }
- LOG.debug("Getting current status of snapshot from master...");
- done = executeCallable(new MasterCallable<IsSnapshotDoneResponse>(conn) {
- @Override
- public IsSnapshotDoneResponse call(int callTimeout) throws ServiceException {
- return master.isSnapshotDone(null, request);
- }
- });
- }
- if (!done.getDone()) {
- throw new SnapshotCreationException("Snapshot '" + snapshot.getName()
- + "' wasn't completed in expectedTime:" + max + " ms", snapshot);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment