Example usage for org.apache.zookeeper KeeperException code

List of usage examples for org.apache.zookeeper KeeperException code

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException code.

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

From source file:com.twitter.distributedlog.bk.LedgerAllocatorPool.java

License:Apache License

/**
 * Initialize simple allocators with given list of allocator names <i>allocators</i>.
 * It initializes a simple allocator with its simple allocator path.
 *//*w w  w. j a  va2s.  c o m*/
private void initializeAllocators(List<String> allocators) throws IOException, InterruptedException {
    final AtomicInteger numPendings = new AtomicInteger(allocators.size());
    final AtomicInteger numFailures = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(numPendings.get() > 0 ? 1 : 0);
    AsyncCallback.DataCallback dataCallback = new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (KeeperException.Code.OK.intValue() != rc) {
                numFailures.incrementAndGet();
                latch.countDown();
                return;
            }
            Versioned<byte[]> allocatorData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
            SimpleLedgerAllocator allocator = new SimpleLedgerAllocator(path, allocatorData,
                    quorumConfigProvider, zkc, bkc);
            allocator.start();
            pendingList.add(allocator);
            if (numPendings.decrementAndGet() == 0 && numFailures.get() == 0) {
                latch.countDown();
            }
        }
    };
    for (String name : allocators) {
        String path = poolPath + "/" + name;
        zkc.get().getData(path, false, dataCallback, null);
    }
    latch.await();
    if (numFailures.get() > 0) {
        throw new IOException("Failed to initialize allocators : " + allocators);
    }
}

From source file:com.twitter.distributedlog.bk.LedgerAllocatorPool.java

License:Apache License

/**
 * Rescue a ledger allocator from an ERROR state
 * @param ledgerAllocator//from  w  w w  .  j  a  va  2s .  co  m
 *          ledger allocator to rescue
 */
private void rescueAllocator(final SimpleLedgerAllocator ledgerAllocator) throws DLInterruptedException {
    SimpleLedgerAllocator oldAllocator;
    synchronized (this) {
        oldAllocator = rescueMap.put(ledgerAllocator.allocatePath, ledgerAllocator);
    }
    if (oldAllocator != null) {
        logger.info("ledger allocator {} is being rescued.", ledgerAllocator.allocatePath);
        return;
    }
    try {
        zkc.get().getData(ledgerAllocator.allocatePath, false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                boolean retry = false;
                SimpleLedgerAllocator newAllocator = null;
                if (KeeperException.Code.OK.intValue() == rc) {
                    Versioned<byte[]> allocatorData = new Versioned<byte[]>(data,
                            new ZkVersion(stat.getVersion()));
                    logger.info("Rescuing ledger allocator {}.", path);
                    newAllocator = new SimpleLedgerAllocator(path, allocatorData, quorumConfigProvider, zkc,
                            bkc);
                    newAllocator.start();
                    logger.info("Rescued ledger allocator {}.", path);
                } else if (KeeperException.Code.NONODE.intValue() == rc) {
                    logger.info("Ledger allocator {} doesn't exist, skip rescuing it.", path);
                } else {
                    retry = true;
                }
                synchronized (LedgerAllocatorPool.this) {
                    rescueMap.remove(ledgerAllocator.allocatePath);
                    if (null != newAllocator) {
                        pendingList.addLast(newAllocator);
                    }
                }
                if (retry) {
                    scheduleAllocatorRescue(ledgerAllocator);
                }
            }
        }, null);
    } catch (InterruptedException ie) {
        logger.warn("Interrupted on rescuing ledger allocator {} : ", ledgerAllocator.allocatePath, ie);
        synchronized (LedgerAllocatorPool.this) {
            rescueMap.remove(ledgerAllocator.allocatePath);
        }
        throw new DLInterruptedException(
                "Interrupted on rescuing ledger allocator " + ledgerAllocator.allocatePath, ie);
    } catch (IOException ioe) {
        logger.warn("Failed to rescue ledger allocator {}, retry rescuing it later : ",
                ledgerAllocator.allocatePath, ioe);
        synchronized (LedgerAllocatorPool.this) {
            rescueMap.remove(ledgerAllocator.allocatePath);
        }
        scheduleAllocatorRescue(ledgerAllocator);
    }
}

From source file:com.twitter.distributedlog.bk.SimpleLedgerAllocator.java

License:Apache License

private static Future<Versioned<byte[]>> createAllocationData(final String allocatePath,
        final ZooKeeperClient zkc) {
    try {/*from   w  ww. ja va2s. c  o  m*/
        final Promise<Versioned<byte[]>> promise = new Promise<Versioned<byte[]>>();
        zkc.get().create(allocatePath, DistributedLogConstants.EMPTY_BYTES, zkc.getDefaultACL(),
                CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.Create2Callback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            promise.setValue(new Versioned<byte[]>(DistributedLogConstants.EMPTY_BYTES,
                                    new ZkVersion(stat.getVersion())));
                        } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                            Utils.zkGetData(zkc, allocatePath, false).proxyTo(promise);
                        } else {
                            promise.setException(FutureUtils.zkException(
                                    KeeperException.create(KeeperException.Code.get(rc)), allocatePath));
                        }
                    }
                }, null);
        return promise;
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return Future.exception(FutureUtils.zkException(e, allocatePath));
    } catch (InterruptedException e) {
        return Future.exception(FutureUtils.zkException(e, allocatePath));
    }
}

From source file:com.twitter.distributedlog.bk.SimpleLedgerAllocator.java

License:Apache License

@Override
public void onAbort(Throwable t) {
    OpListener<LedgerHandle> listenerToNotify;
    synchronized (this) {
        listenerToNotify = tryObtainListener;
        if (t instanceof KeeperException && ((KeeperException) t).code() == KeeperException.Code.BADVERSION) {
            LOG.info("Set ledger allocator {} to ERROR state after hit bad version : version = {}",
                    allocatePath, getVersion());
            setPhase(Phase.ERROR);//w w  w .  j av  a  2s .  com
        } else {
            if (Phase.HANDING_OVER == phase) {
                setPhase(Phase.ALLOCATED);
                tryObtainTxn = null;
                tryObtainListener = null;
            }
        }
    }
    if (null != listenerToNotify) {
        listenerToNotify.onAbort(t);
    }
}

From source file:com.twitter.distributedlog.bk.TestLedgerAllocator.java

License:Apache License

@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));

    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData,
            newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData,
            newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();/*from  w w  w . j a v a  2 s .  c o m*/
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    FutureUtils.result(txn1.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);

    long eid = lh.addEntry("hello world".getBytes());
    lh.close();
    LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}

From source file:com.twitter.distributedlog.BKDistributedLogNamespace.java

License:Apache License

private static boolean checkIfLogExists(DistributedLogConfiguration conf, URI uri, String name)
        throws IOException, IllegalArgumentException {
    validateInput(conf, uri, name);/*from   w w  w  .jav a 2s .c om*/
    final String logRootPath = uri.getPath() + String.format("/%s", name);
    return withZooKeeperClient(new ZooKeeperClientHandler<Boolean>() {
        @Override
        public Boolean handle(ZooKeeperClient zkc) throws IOException {
            // check existence after syncing
            try {
                return null != Utils.sync(zkc, logRootPath).exists(logRootPath, false);
            } catch (KeeperException e) {
                throw new ZKException("Error on checking if log " + logRootPath + " exists", e.code());
            } catch (InterruptedException e) {
                throw new DLInterruptedException("Interrupted on checking if log " + logRootPath + " exists",
                        e);
            }
        }
    }, conf, uri);
}

From source file:com.twitter.distributedlog.BKLogHandler.java

License:Apache License

Future<Void> checkLogStreamExistsAsync() {
    final Promise<Void> promise = new Promise<Void>();
    try {/*from   w  w  w .  jav a 2  s.c om*/
        final ZooKeeper zk = zooKeeperClient.get();
        zk.sync(logMetadata.getLogSegmentsPath(), new AsyncCallback.VoidCallback() {
            @Override
            public void processResult(int syncRc, String path, Object syncCtx) {
                if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    promise.setException(new LogNotFoundException(String
                            .format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    return;
                } else if (KeeperException.Code.OK.intValue() != syncRc) {
                    promise.setException(
                            new ZKException("Error on checking log existence for " + getFullyQualifiedName(),
                                    KeeperException.create(KeeperException.Code.get(syncRc))));
                    return;
                }
                zk.exists(logMetadata.getLogSegmentsPath(), false, new AsyncCallback.StatCallback() {
                    @Override
                    public void processResult(int rc, String path, Object ctx, Stat stat) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            promise.setValue(null);
                        } else if (KeeperException.Code.NONODE.intValue() == rc) {
                            promise.setException(new LogNotFoundException(String.format(
                                    "Log %s does not exist or has been deleted", getFullyQualifiedName())));
                        } else {
                            promise.setException(new ZKException(
                                    "Error on checking log existence for " + getFullyQualifiedName(),
                                    KeeperException.create(KeeperException.Code.get(rc))));
                        }
                    }
                }, null);
            }
        }, null);

    } catch (InterruptedException ie) {
        LOG.error("Interrupted while reading {}", logMetadata.getLogSegmentsPath(), ie);
        promise.setException(new DLInterruptedException(
                "Interrupted while checking " + logMetadata.getLogSegmentsPath(), ie));
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    }
    return promise;
}

From source file:com.twitter.distributedlog.BKLogHandler.java

License:Apache License

/**
 * Get a list of all segments in the journal.
 *//*  w w w  .j  a  v a 2 s .  co  m*/
protected List<LogSegmentMetadata> forceGetLedgerList(final Comparator<LogSegmentMetadata> comparator,
        final LogSegmentFilter segmentFilter, boolean throwOnEmpty) throws IOException {
    final List<LogSegmentMetadata> ledgers = new ArrayList<LogSegmentMetadata>();
    final AtomicInteger result = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Stopwatch stopwatch = Stopwatch.createStarted();
    asyncGetLedgerListInternal(comparator, segmentFilter, null,
            new GenericCallback<List<LogSegmentMetadata>>() {
                @Override
                public void operationComplete(int rc, List<LogSegmentMetadata> logSegmentMetadatas) {
                    result.set(rc);
                    if (KeeperException.Code.OK.intValue() == rc) {
                        ledgers.addAll(logSegmentMetadatas);
                    } else {
                        LOG.error("Failed to get ledger list for {} : with error {}", getFullyQualifiedName(),
                                rc);
                    }
                    latch.countDown();
                }
            }, new AtomicInteger(conf.getZKNumRetries()), new AtomicLong(conf.getZKRetryBackoffStartMillis()));
    try {
        latch.await();
    } catch (InterruptedException e) {
        forceGetListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        throw new DLInterruptedException(
                "Interrupted on reading ledger list from zkfor " + getFullyQualifiedName(), e);
    }
    long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);

    KeeperException.Code rc = KeeperException.Code.get(result.get());
    if (rc == KeeperException.Code.OK) {
        forceGetListStat.registerSuccessfulEvent(elapsedMicros);
    } else {
        forceGetListStat.registerFailedEvent(elapsedMicros);
        if (KeeperException.Code.NONODE == rc) {
            throw new LogNotFoundException("Log " + getFullyQualifiedName() + " is not found");
        } else {
            throw new IOException("ZK Exception " + rc + " reading ledger list for " + getFullyQualifiedName());
        }
    }

    if (throwOnEmpty && ledgers.isEmpty()) {
        throw new LogEmptyException("Log " + getFullyQualifiedName() + " is empty");
    }
    return ledgers;
}

From source file:com.twitter.distributedlog.BKLogHandler.java

License:Apache License

protected Future<List<LogSegmentMetadata>> asyncGetLedgerListWithRetries(
        Comparator<LogSegmentMetadata> comparator, LogSegmentFilter segmentFilter, Watcher watcher) {
    final Promise<List<LogSegmentMetadata>> promise = new Promise<List<LogSegmentMetadata>>();
    asyncGetLedgerListWithRetries(comparator, segmentFilter, watcher,
            new GenericCallback<List<LogSegmentMetadata>>() {
                @Override//from  w ww. j a va  2  s.  c o m
                public void operationComplete(int rc, List<LogSegmentMetadata> segments) {
                    if (KeeperException.Code.OK.intValue() == rc) {
                        promise.setValue(segments);
                    } else if (KeeperException.Code.NONODE.intValue() == rc) {
                        promise.setException(
                                new LogNotFoundException("Log " + getFullyQualifiedName() + " not found"));
                    } else {
                        String errMsg = "ZK Exception " + rc + " reading ledger list for "
                                + getFullyQualifiedName();
                        promise.setException(new ZKException(errMsg, KeeperException.Code.get(rc)));
                    }
                }
            });
    return promise;
}

From source file:com.twitter.distributedlog.BKLogHandler.java

License:Apache License

private void asyncGetLedgerListInternal(final Comparator<LogSegmentMetadata> comparator,
        final LogSegmentFilter segmentFilter, final Watcher watcher,
        final GenericCallback<List<LogSegmentMetadata>> finalCallback, final AtomicInteger numAttemptsLeft,
        final AtomicLong backoffMillis) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {//from w w w  .jav a  2s . c om
        if (LOG.isTraceEnabled()) {
            LOG.trace("Async getting ledger list for {}.", getFullyQualifiedName());
        }
        final GenericCallback<List<LogSegmentMetadata>> callback = new GenericCallback<List<LogSegmentMetadata>>() {
            @Override
            public void operationComplete(int rc, List<LogSegmentMetadata> result) {
                long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
                if (KeeperException.Code.OK.intValue() != rc) {
                    getListStat.registerFailedEvent(elapsedMicros);
                } else {
                    if (LogSegmentFilter.DEFAULT_FILTER == segmentFilter) {
                        isFullListFetched.set(true);
                    }
                    getListStat.registerSuccessfulEvent(elapsedMicros);
                }
                finalCallback.operationComplete(rc, result);
            }
        };
        zooKeeperClient.get().getChildren(logMetadata.getLogSegmentsPath(), watcher,
                new AsyncCallback.Children2Callback() {
                    @Override
                    public void processResult(final int rc, final String path, final Object ctx,
                            final List<String> children, final Stat stat) {
                        if (KeeperException.Code.OK.intValue() != rc) {

                            if ((KeeperException.Code.CONNECTIONLOSS.intValue() == rc
                                    || KeeperException.Code.SESSIONEXPIRED.intValue() == rc
                                    || KeeperException.Code.SESSIONMOVED.intValue() == rc)
                                    && numAttemptsLeft.decrementAndGet() > 0) {
                                long backoffMs = backoffMillis.get();
                                backoffMillis.set(Math.min(conf.getZKRetryBackoffMaxMillis(), 2 * backoffMs));
                                scheduler.schedule(new Runnable() {
                                    @Override
                                    public void run() {
                                        asyncGetLedgerListInternal(comparator, segmentFilter, watcher,
                                                finalCallback, numAttemptsLeft, backoffMillis);
                                    }
                                }, backoffMs, TimeUnit.MILLISECONDS);
                                return;
                            }
                            callback.operationComplete(rc, null);
                            return;
                        }

                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Got ledger list from {} : {}", logMetadata.getLogSegmentsPath(),
                                    children);
                        }

                        ledgerListWatchSet.set(true);
                        Set<String> segmentsReceived = new HashSet<String>();
                        segmentsReceived.addAll(segmentFilter.filter(children));
                        Set<String> segmentsAdded;
                        final Set<String> removedSegments = Collections.synchronizedSet(new HashSet<String>());
                        final Map<String, LogSegmentMetadata> addedSegments = Collections
                                .synchronizedMap(new HashMap<String, LogSegmentMetadata>());
                        Pair<Set<String>, Set<String>> segmentChanges = logSegmentCache.diff(segmentsReceived);
                        segmentsAdded = segmentChanges.getLeft();
                        removedSegments.addAll(segmentChanges.getRight());

                        if (segmentsAdded.isEmpty()) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("No segments added for {}.", getFullyQualifiedName());
                            }

                            // update the cache before fetch
                            logSegmentCache.update(removedSegments, addedSegments);

                            List<LogSegmentMetadata> segmentList;
                            try {
                                segmentList = getCachedLogSegments(comparator);
                            } catch (UnexpectedException e) {
                                callback.operationComplete(KeeperException.Code.DATAINCONSISTENCY.intValue(),
                                        null);
                                return;
                            }
                            callback.operationComplete(KeeperException.Code.OK.intValue(), segmentList);
                            notifyUpdatedLogSegments(segmentList);
                            if (!removedSegments.isEmpty()) {
                                notifyOnOperationComplete();
                            }
                            return;
                        }

                        final AtomicInteger numChildren = new AtomicInteger(segmentsAdded.size());
                        final AtomicInteger numFailures = new AtomicInteger(0);
                        for (final String segment : segmentsAdded) {
                            metadataStore.getLogSegment(logMetadata.getLogSegmentPath(segment))
                                    .addEventListener(new FutureEventListener<LogSegmentMetadata>() {

                                        @Override
                                        public void onSuccess(LogSegmentMetadata result) {
                                            addedSegments.put(segment, result);
                                            complete();
                                        }

                                        @Override
                                        public void onFailure(Throwable cause) {
                                            // NONODE exception is possible in two cases
                                            // 1. A log segment was deleted by truncation between the call to getChildren and read
                                            // attempt on the znode corresponding to the segment
                                            // 2. In progress segment has been completed => inprogress ZNode does not exist
                                            if (cause instanceof KeeperException
                                                    && KeeperException.Code.NONODE == ((KeeperException) cause)
                                                            .code()) {
                                                removedSegments.add(segment);
                                                complete();
                                            } else {
                                                // fail fast
                                                if (1 == numFailures.incrementAndGet()) {
                                                    int rcToReturn = KeeperException.Code.SYSTEMERROR
                                                            .intValue();
                                                    if (cause instanceof KeeperException) {
                                                        rcToReturn = ((KeeperException) cause).code()
                                                                .intValue();
                                                    } else if (cause instanceof ZKException) {
                                                        rcToReturn = ((ZKException) cause)
                                                                .getKeeperExceptionCode().intValue();
                                                    }
                                                    // :( properly we need dlog related response code.
                                                    callback.operationComplete(rcToReturn, null);
                                                    return;
                                                }
                                            }
                                        }

                                        private void complete() {
                                            if (0 == numChildren.decrementAndGet() && numFailures.get() == 0) {
                                                // update the cache only when fetch completed
                                                logSegmentCache.update(removedSegments, addedSegments);
                                                List<LogSegmentMetadata> segmentList;
                                                try {
                                                    segmentList = getCachedLogSegments(comparator);
                                                } catch (UnexpectedException e) {
                                                    callback.operationComplete(
                                                            KeeperException.Code.DATAINCONSISTENCY.intValue(),
                                                            null);
                                                    return;
                                                }
                                                callback.operationComplete(KeeperException.Code.OK.intValue(),
                                                        segmentList);
                                                notifyUpdatedLogSegments(segmentList);
                                                notifyOnOperationComplete();
                                            }
                                        }
                                    });
                        }
                    }
                }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    } catch (InterruptedException e) {
        getListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
        finalCallback.operationComplete(KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    }
}