Search in sources :

Example 1 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKDistributedLogManager method delete.

/**
     * Delete all the partitions of the specified log
     *
     * @throws IOException if the deletion fails
     */
@Override
public void delete() throws IOException {
    BKLogWriteHandler ledgerHandler = createWriteHandler(true);
    try {
        ledgerHandler.deleteLog();
    } finally {
        Utils.closeQuietly(ledgerHandler);
    }
    // Delete the ZK path associated with the log stream
    String zkPath = getZKPath();
    // Safety check when we are using the shared zookeeper
    if (zkPath.toLowerCase().contains("distributedlog")) {
        try {
            LOG.info("Delete the path associated with the log {}, ZK Path {}", name, zkPath);
            ZKUtil.deleteRecursive(writerZKC.get(), zkPath);
        } catch (InterruptedException ie) {
            LOG.error("Interrupted while accessing ZK", ie);
            throw new DLInterruptedException("Error initializing zk", ie);
        } catch (KeeperException ke) {
            LOG.error("Error accessing entry in zookeeper", ke);
            throw new IOException("Error initializing zk", ke);
        }
    } else {
        LOG.warn("Skip deletion of unrecognized ZK Path {}", zkPath);
    }
}
Also used : DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) IOException(java.io.IOException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKLogWriteHandler method deleteLog.

public void deleteLog() throws IOException {
    lock.checkOwnershipAndReacquire();
    FutureUtils.result(purgeLogSegmentsOlderThanTxnId(-1));
    try {
        Utils.closeQuietly(lock);
        zooKeeperClient.get().exists(logMetadata.getLogSegmentsPath(), false);
        zooKeeperClient.get().exists(logMetadata.getMaxTxIdPath(), false);
        if (logMetadata.getLogRootPath().toLowerCase().contains("distributedlog")) {
            ZKUtil.deleteRecursive(zooKeeperClient.get(), logMetadata.getLogRootPath());
        } else {
            LOG.warn("Skip deletion of unrecognized ZK Path {}", logMetadata.getLogRootPath());
        }
    } catch (InterruptedException ie) {
        LOG.error("Interrupted while deleting log znodes", ie);
        throw new DLInterruptedException("Interrupted while deleting " + logMetadata.getLogRootPath(), ie);
    } catch (KeeperException ke) {
        LOG.error("Error deleting" + logMetadata.getLogRootPath() + " in zookeeper", ke);
    }
}
Also used : DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKSyncLogReaderDLSN method readNext.

@Override
public synchronized LogRecordWithDLSN readNext(boolean nonBlocking) throws IOException {
    if (null != readerException.get()) {
        throw readerException.get();
    }
    LogRecordWithDLSN record = null;
    if (nonBlocking) {
        record = readAheadRecords.poll();
    } else {
        try {
            // reader is still catching up, waiting for next record
            while (!reader.bkLedgerManager.isReadAheadCaughtUp() && null == readerException.get() && null == record) {
                record = readAheadRecords.poll(maxReadAheadWaitTime, TimeUnit.MILLISECONDS);
            }
            // reader caught up
            boolean shallWait = true;
            while (shallWait && reader.bkLedgerManager.isReadAheadCaughtUp() && null == record && null == readerException.get()) {
                record = readAheadRecords.poll(maxReadAheadWaitTime, TimeUnit.MILLISECONDS);
                if (null != record) {
                    break;
                }
                DLSN lastDLSNSeenByReadAhead = reader.bkLedgerManager.readAheadCache.getLastReadAheadUserDLSN();
                // if last seen DLSN by reader is same as the one seen by ReadAhead
                // that means that reader is caught up with ReadAhead and ReadAhead
                // is caught up with stream
                shallWait = DLSN.InitialDLSN != lastDLSNSeenByReadAhead && lastSeenDLSN.compareTo(lastDLSNSeenByReadAhead) < 0 && startDLSN.compareTo(lastDLSNSeenByReadAhead) <= 0;
            }
        } catch (InterruptedException e) {
            throw new DLInterruptedException("Interrupted on waiting next available log record for stream " + reader.getStreamName(), e);
        }
    }
    if (null != readerException.get()) {
        throw readerException.get();
    }
    if (null != record) {
        if (record.isEndOfStream()) {
            EndOfStreamException eos = new EndOfStreamException("End of Stream Reached for " + reader.bkLedgerManager.getFullyQualifiedName());
            readerException.compareAndSet(null, eos);
            throw eos;
        }
        invokeReadAheadCallback();
    }
    return record;
}
Also used : EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Example 4 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKLogHandler method checkLogStreamExistsAsync.

Future<Void> checkLogStreamExistsAsync() {
    final Promise<Void> promise = new Promise<Void>();
    try {
        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;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Example 5 with DLInterruptedException

use of com.twitter.distributedlog.exceptions.DLInterruptedException in project distributedlog by twitter.

the class BKLogReadHandler method ensureReadLockPathExist.

private Future<Void> ensureReadLockPathExist() {
    final Promise<Void> promise = new Promise<Void>();
    promise.setInterruptHandler(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            FutureUtils.setException(promise, new LockCancelledException(readLockPath, "Could not ensure read lock path", t));
            return null;
        }
    });
    Optional<String> parentPathShouldNotCreate = Optional.of(logMetadata.getLogRootPath());
    Utils.zkAsyncCreateFullPathOptimisticRecursive(zooKeeperClient, readLockPath, parentPathShouldNotCreate, new byte[0], zooKeeperClient.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.StringCallback() {

        @Override
        public void processResult(final int rc, final String path, Object ctx, String name) {
            scheduler.submit(new Runnable() {

                @Override
                public void run() {
                    if (KeeperException.Code.NONODE.intValue() == rc) {
                        FutureUtils.setException(promise, new LogNotFoundException(String.format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    } else if (KeeperException.Code.OK.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Created path {}.", path);
                    } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Path {} is already existed.", path);
                    } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new ZooKeeperClient.ZooKeeperConnectionException(path));
                    } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new DLInterruptedException(path));
                    } else {
                        FutureUtils.setException(promise, KeeperException.create(KeeperException.Code.get(rc)));
                    }
                }
            });
        }
    }, null);
    return promise;
}
Also used : LockCancelledException(com.twitter.distributedlog.exceptions.LockCancelledException) Promise(com.twitter.util.Promise) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) BoxedUnit(scala.runtime.BoxedUnit) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Aggregations

DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)22 IOException (java.io.IOException)15 KeeperException (org.apache.zookeeper.KeeperException)8 Stat (org.apache.zookeeper.data.Stat)6 ZKException (com.twitter.distributedlog.exceptions.ZKException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AsyncCallback (org.apache.zookeeper.AsyncCallback)5 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)4 Promise (com.twitter.util.Promise)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ArrayList (java.util.ArrayList)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Stopwatch (com.google.common.base.Stopwatch)2 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)2 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2