Search in sources :

Example 21 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class RecoverableZooKeeper method exists.

/**
   * exists is an idempotent operation. Retry before throwing exception
   * @return A Stat instance
   */
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.exists");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return checkZk().exists(path, watcher);
            } catch (KeeperException e) {
                switch(e.code()) {
                    case CONNECTIONLOSS:
                    case OPERATIONTIMEOUT:
                        retryOrThrow(retryCounter, e, "exists");
                        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 22 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class ZKUtil method watchAndCheckExists.

//
// Existence checks and watches
//
/**
   * Watch the specified znode for delete/create/change events.  The watcher is
   * set whether or not the node exists.  If the node already exists, the method
   * returns true.  If the node does not exist, the method returns false.
   *
   * @param zkw zk reference
   * @param znode path of node to watch
   * @return true if znode exists, false if does not exist or error
   * @throws KeeperException if unexpected zookeeper exception
   */
public static boolean watchAndCheckExists(ZooKeeperWatcher zkw, String znode) throws KeeperException {
    try {
        Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw);
        boolean exists = s != null ? true : false;
        if (exists) {
            LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode));
        } else {
            LOG.debug(zkw.prefix("Set watcher on znode that does not yet exist, " + znode));
        }
        return exists;
    } catch (KeeperException e) {
        LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
        zkw.keeperException(e);
        return false;
    } catch (InterruptedException e) {
        LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
        zkw.interruptedException(e);
        return false;
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException)

Example 23 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class ZKUtil method convert.

/**
   * Convert a {@link DeserializationException} to a more palatable {@link KeeperException}.
   * Used when can't let a {@link DeserializationException} out w/o changing public API.
   * @param e Exception to convert
   * @return Converted exception
   */
public static KeeperException convert(final DeserializationException e) {
    KeeperException ke = new KeeperException.DataInconsistencyException();
    ke.initCause(e);
    return ke;
}
Also used : KeeperException(org.apache.zookeeper.KeeperException)

Example 24 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class DisabledTableSnapshotHandler method snapshotRegions.

// TODO consider parallelizing these operations since they are independent. Right now its just
// easier to keep them serial though
@Override
public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations) throws IOException, KeeperException {
    try {
        // 1. get all the regions hosting this table.
        // extract each pair to separate lists
        Set<HRegionInfo> regions = new HashSet<>();
        for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
            // Don't include non-default regions
            HRegionInfo hri = p.getFirst();
            if (RegionReplicaUtil.isDefaultReplica(hri)) {
                regions.add(hri);
            }
        }
        // handle the mob files if any.
        boolean mobEnabled = MobUtils.hasMobColumns(htd);
        if (mobEnabled) {
            // snapshot the mob files as a offline region.
            HRegionInfo mobRegionInfo = MobUtils.getMobRegionInfo(htd.getTableName());
            regions.add(mobRegionInfo);
        }
        // 2. for each region, write all the info to disk
        String msg = "Starting to write region info and WALs for regions for offline snapshot:" + ClientSnapshotDescriptionUtils.toString(snapshot);
        LOG.info(msg);
        status.setStatus(msg);
        ThreadPoolExecutor exec = SnapshotManifest.createExecutor(conf, "DisabledTableSnapshot");
        try {
            ModifyRegionUtils.editRegions(exec, regions, new ModifyRegionUtils.RegionEditTask() {

                @Override
                public void editRegion(final HRegionInfo regionInfo) throws IOException {
                    snapshotManifest.addRegion(FSUtils.getTableDir(rootDir, snapshotTable), regionInfo);
                }
            });
        } finally {
            exec.shutdown();
        }
    } catch (Exception e) {
        // make sure we capture the exception to propagate back to the client later
        String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot) + " due to exception:" + e.getMessage();
        ForeignException ee = new ForeignException(reason, e);
        monitor.receive(ee);
        status.abort("Snapshot of table: " + snapshotTable + " failed because " + e.getMessage());
    } finally {
        LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot) + " as finished.");
    }
}
Also used : IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ServerName(org.apache.hadoop.hbase.ServerName) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) ModifyRegionUtils(org.apache.hadoop.hbase.util.ModifyRegionUtils) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) HashSet(java.util.HashSet)

Example 25 with KeeperException

use of org.apache.zookeeper.KeeperException in project hbase by apache.

the class ZKProcedureCoordinator method start.

/**
   * Start monitoring znodes in ZK - subclass hook to start monitoring znodes they are about.
   * @return true if succeed, false if encountered initialization errors.
   */
@Override
public final boolean start(final ProcedureCoordinator coordinator) {
    if (this.coordinator != null) {
        throw new IllegalStateException("ZKProcedureCoordinator already started and already has listener installed");
    }
    this.coordinator = coordinator;
    try {
        this.zkProc = new ZKProcedureUtil(watcher, procedureType) {

            @Override
            public void nodeCreated(String path) {
                if (!isInProcedurePath(path))
                    return;
                LOG.debug("Node created: " + path);
                logZKTree(this.baseZNode);
                if (isAcquiredPathNode(path)) {
                    // node wasn't present when we created the watch so zk event triggers acquire
                    coordinator.memberAcquiredBarrier(ZKUtil.getNodeName(ZKUtil.getParent(path)), ZKUtil.getNodeName(path));
                } else if (isReachedPathNode(path)) {
                    // node was absent when we created the watch so zk event triggers the finished barrier.
                    // TODO Nothing enforces that acquire and reached znodes from showing up in wrong order.
                    String procName = ZKUtil.getNodeName(ZKUtil.getParent(path));
                    String member = ZKUtil.getNodeName(path);
                    // get the data from the procedure member
                    try {
                        byte[] dataFromMember = ZKUtil.getData(watcher, path);
                        // ProtobufUtil.isPBMagicPrefix will check null
                        if (dataFromMember != null && dataFromMember.length > 0) {
                            if (!ProtobufUtil.isPBMagicPrefix(dataFromMember)) {
                                ForeignException ee = new ForeignException(coordName, "Failed to get data from finished node or data is illegally formatted:" + path);
                                coordinator.abortProcedure(procName, ee);
                            } else {
                                dataFromMember = Arrays.copyOfRange(dataFromMember, ProtobufUtil.lengthOfPBMagic(), dataFromMember.length);
                                LOG.debug("Finished data from procedure '" + procName + "' member '" + member + "': " + new String(dataFromMember));
                                coordinator.memberFinishedBarrier(procName, member, dataFromMember);
                            }
                        } else {
                            coordinator.memberFinishedBarrier(procName, member, dataFromMember);
                        }
                    } catch (KeeperException e) {
                        ForeignException ee = new ForeignException(coordName, e);
                        coordinator.abortProcedure(procName, ee);
                    } catch (InterruptedException e) {
                        ForeignException ee = new ForeignException(coordName, e);
                        coordinator.abortProcedure(procName, ee);
                    }
                } else if (isAbortPathNode(path)) {
                    abort(path);
                } else {
                    LOG.debug("Ignoring created notification for node:" + path);
                }
            }
        };
        zkProc.clearChildZNodes();
    } catch (KeeperException e) {
        LOG.error("Unable to start the ZK-based Procedure Coordinator rpcs.", e);
        return false;
    }
    LOG.debug("Starting the controller for procedure member:" + coordName);
    return true;
}
Also used : ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

KeeperException (org.apache.zookeeper.KeeperException)345 IOException (java.io.IOException)114 Stat (org.apache.zookeeper.data.Stat)79 ZooKeeper (org.apache.zookeeper.ZooKeeper)54 Test (org.junit.Test)37 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)36 ArrayList (java.util.ArrayList)30 SolrException (org.apache.solr.common.SolrException)30 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)24 HashMap (java.util.HashMap)21 WatchedEvent (org.apache.zookeeper.WatchedEvent)20 Watcher (org.apache.zookeeper.Watcher)20 InterruptedIOException (java.io.InterruptedIOException)19 Map (java.util.Map)19 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)17 ServerName (org.apache.hadoop.hbase.ServerName)15 ACL (org.apache.zookeeper.data.ACL)15 List (java.util.List)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 RetryCounter (org.apache.hadoop.hbase.util.RetryCounter)13