Search in sources :

Example 11 with RetryCounter

use of org.apache.hadoop.hbase.util.RetryCounter in project hbase by apache.

the class RecoverableZooKeeper method getData.

/**
   * getData is an idemnpotent operation. Retry before throwing exception
   * @return Data
   */
public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.getData");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                byte[] revData = checkZk().getData(path, watch, stat);
                return removeMetaData(revData);
            } catch (KeeperException e) {
                switch(e.code()) {
                    case CONNECTIONLOSS:
                    case OPERATIONTIMEOUT:
                        retryOrThrow(retryCounter, e, "getData");
                        break;
                    default:
                        throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}
Also used : RetryCounter(org.apache.hadoop.hbase.util.RetryCounter) TraceScope(org.apache.htrace.TraceScope) KeeperException(org.apache.zookeeper.KeeperException)

Example 12 with RetryCounter

use of org.apache.hadoop.hbase.util.RetryCounter in project hbase by apache.

the class RecoverableZooKeeper method createNonSequential.

private String createNonSequential(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    // False for first attempt, true for all retries.
    boolean isRetry = false;
    while (true) {
        try {
            return checkZk().create(path, data, acl, createMode);
        } catch (KeeperException e) {
            switch(e.code()) {
                case NODEEXISTS:
                    if (isRetry) {
                        // If the connection was lost, there is still a possibility that
                        // we have successfully created the node at our previous attempt,
                        // so we read the node and compare.
                        byte[] currentData = checkZk().getData(path, false, null);
                        if (currentData != null && Bytes.compareTo(currentData, data) == 0) {
                            // We successfully created a non-sequential node
                            return path;
                        }
                        LOG.error("Node " + path + " already exists with " + Bytes.toStringBinary(currentData) + ", could not write " + Bytes.toStringBinary(data));
                        throw e;
                    }
                    LOG.debug("Node " + path + " already exists");
                    throw e;
                case CONNECTIONLOSS:
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "create");
                    break;
                default:
                    throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        isRetry = true;
    }
}
Also used : RetryCounter(org.apache.hadoop.hbase.util.RetryCounter) KeeperException(org.apache.zookeeper.KeeperException)

Example 13 with RetryCounter

use of org.apache.hadoop.hbase.util.RetryCounter in project hbase by apache.

the class RecoverableZooKeeper method delete.

/**
   * delete is an idempotent operation. Retry before throwing exception.
   * This function will not throw NoNodeException if the path does not
   * exist.
   */
public void delete(String path, int version) throws InterruptedException, KeeperException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.delete");
        RetryCounter retryCounter = retryCounterFactory.create();
        // False for first attempt, true for all retries.
        boolean isRetry = false;
        while (true) {
            try {
                checkZk().delete(path, version);
                return;
            } catch (KeeperException e) {
                switch(e.code()) {
                    case NONODE:
                        if (isRetry) {
                            LOG.debug("Node " + path + " already deleted. Assuming a " + "previous attempt succeeded.");
                            return;
                        }
                        LOG.debug("Node " + path + " already deleted, retry=" + isRetry);
                        throw e;
                    case CONNECTIONLOSS:
                    case OPERATIONTIMEOUT:
                        retryOrThrow(retryCounter, e, "delete");
                        break;
                    default:
                        throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            isRetry = true;
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}
Also used : RetryCounter(org.apache.hadoop.hbase.util.RetryCounter) TraceScope(org.apache.htrace.TraceScope) KeeperException(org.apache.zookeeper.KeeperException)

Example 14 with RetryCounter

use of org.apache.hadoop.hbase.util.RetryCounter in project hbase by apache.

the class RecoverableZooKeeper method setAcl.

/**
   * setAcl is an idempotent operation. Retry before throwing exception
   * @return list of ACLs
   */
public Stat setAcl(String path, List<ACL> acls, int version) throws KeeperException, InterruptedException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.setAcl");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return checkZk().setACL(path, acls, version);
            } catch (KeeperException e) {
                switch(e.code()) {
                    case CONNECTIONLOSS:
                    case OPERATIONTIMEOUT:
                        retryOrThrow(retryCounter, e, "setAcl");
                        break;
                    default:
                        throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}
Also used : RetryCounter(org.apache.hadoop.hbase.util.RetryCounter) TraceScope(org.apache.htrace.TraceScope) KeeperException(org.apache.zookeeper.KeeperException)

Example 15 with RetryCounter

use of org.apache.hadoop.hbase.util.RetryCounter in project hbase by apache.

the class RecoverableZooKeeper method multi.

/**
   * Run multiple operations in a transactional manner. Retry before throwing exception
   */
public List<OpResult> multi(Iterable<Op> ops) throws KeeperException, InterruptedException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.multi");
        RetryCounter retryCounter = retryCounterFactory.create();
        Iterable<Op> multiOps = prepareZKMulti(ops);
        while (true) {
            try {
                return checkZk().multi(multiOps);
            } catch (KeeperException e) {
                switch(e.code()) {
                    case CONNECTIONLOSS:
                    case OPERATIONTIMEOUT:
                        retryOrThrow(retryCounter, e, "multi");
                        break;
                    default:
                        throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}
Also used : Op(org.apache.zookeeper.Op) RetryCounter(org.apache.hadoop.hbase.util.RetryCounter) TraceScope(org.apache.htrace.TraceScope) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

RetryCounter (org.apache.hadoop.hbase.util.RetryCounter)16 KeeperException (org.apache.zookeeper.KeeperException)13 TraceScope (org.apache.htrace.TraceScope)11 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)1 TableNotFoundException (org.apache.hadoop.hbase.TableNotFoundException)1 FlushRegionCallable (org.apache.hadoop.hbase.client.FlushRegionCallable)1 HBaseRpcController (org.apache.hadoop.hbase.ipc.HBaseRpcController)1 AdminService (org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService)1 FlushRegionResponse (org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.FlushRegionResponse)1 ServerInfo (org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ServerInfo)1 RetryCounterFactory (org.apache.hadoop.hbase.util.RetryCounterFactory)1 Op (org.apache.zookeeper.Op)1 Stat (org.apache.zookeeper.data.Stat)1