Search in sources :

Example 11 with DLInterruptedException

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

the class ZKMetadataAccessor method getMetadata.

/**
     * Retrieve the metadata stored at the node
     * @return byte array containing the metadata
     * @throws IOException
     */
@Override
public byte[] getMetadata() throws IOException {
    checkClosedOrInError("createOrUpdateMetadata");
    String zkPath = getZKPath();
    LOG.debug("Getting application specific metadata from {}", zkPath);
    try {
        Stat currentStat = readerZKC.get().exists(zkPath, false);
        if (currentStat == null) {
            return null;
        } else {
            return readerZKC.get().getData(zkPath, false, currentStat);
        }
    } catch (InterruptedException ie) {
        throw new DLInterruptedException("Error reading the max tx id from zk", ie);
    } catch (Exception e) {
        throw new IOException("Error reading the max tx id from zk", e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) IOException(java.io.IOException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) AlreadyClosedException(com.twitter.distributedlog.exceptions.AlreadyClosedException) IOException(java.io.IOException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Example 12 with DLInterruptedException

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

the class BookKeeperClient method initialize.

private synchronized void initialize() throws IOException {
    if (null != this.bkc) {
        return;
    }
    boolean registerExpirationHandler;
    if (null == this.zkc) {
        int zkSessionTimeout = conf.getBKClientZKSessionTimeoutMilliSeconds();
        RetryPolicy retryPolicy = null;
        if (conf.getBKClientZKNumRetries() > 0) {
            retryPolicy = new BoundExponentialBackoffRetryPolicy(conf.getBKClientZKRetryBackoffStartMillis(), conf.getBKClientZKRetryBackoffMaxMillis(), conf.getBKClientZKNumRetries());
        }
        Credentials credentials = Credentials.NONE;
        if (conf.getZkAclId() != null) {
            credentials = new DigestCredentials(conf.getZkAclId(), conf.getZkAclId());
        }
        this.zkc = new ZooKeeperClient(name + ":zk", zkSessionTimeout, 2 * zkSessionTimeout, zkServers, retryPolicy, statsLogger.scope("bkc_zkc"), conf.getZKClientNumberRetryThreads(), conf.getBKClientZKRequestRateLimit(), credentials);
    }
    registerExpirationHandler = conf.getBKClientZKNumRetries() <= 0;
    try {
        commonInitialization(conf, ledgersPath, channelFactory, statsLogger, requestTimer, registerExpirationHandler);
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on creating bookkeeper client " + name + " : ", e);
    } catch (KeeperException e) {
        throw new ZKException("Error on creating bookkeeper client " + name + " : ", e);
    }
    if (ownZK) {
        LOG.info("BookKeeper Client created {} with its own ZK Client : ledgersPath = {}, numRetries = {}, " + "sessionTimeout = {}, backoff = {}, maxBackoff = {}, dnsResolver = {}, registerExpirationHandler = {}", new Object[] { name, ledgersPath, conf.getBKClientZKNumRetries(), conf.getBKClientZKSessionTimeoutMilliSeconds(), conf.getBKClientZKRetryBackoffStartMillis(), conf.getBKClientZKRetryBackoffMaxMillis(), conf.getBkDNSResolverOverrides(), registerExpirationHandler });
    } else {
        LOG.info("BookKeeper Client created {} with shared zookeeper client : ledgersPath = {}, numRetries = {}, " + "sessionTimeout = {}, backoff = {}, maxBackoff = {}, dnsResolver = {}, registerExpirationHandler = {}", new Object[] { name, ledgersPath, conf.getZKNumRetries(), conf.getZKSessionTimeoutMilliseconds(), conf.getZKRetryBackoffStartMillis(), conf.getZKRetryBackoffMaxMillis(), conf.getBkDNSResolverOverrides(), registerExpirationHandler });
    }
}
Also used : DigestCredentials(com.twitter.distributedlog.ZooKeeperClient.DigestCredentials) ZKException(com.twitter.distributedlog.exceptions.ZKException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) RetryPolicy(org.apache.bookkeeper.zookeeper.RetryPolicy) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) DigestCredentials(com.twitter.distributedlog.ZooKeeperClient.DigestCredentials) Credentials(com.twitter.distributedlog.ZooKeeperClient.Credentials) KeeperException(org.apache.zookeeper.KeeperException)

Example 13 with DLInterruptedException

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

the class ZKSubscriptionStateStore method getLastCommitPositionFromZK.

Future<DLSN> getLastCommitPositionFromZK() {
    final Promise<DLSN> result = new Promise<DLSN>();
    try {
        logger.debug("Reading last commit position from path {}", zkPath);
        zooKeeperClient.get().getData(zkPath, false, new AsyncCallback.DataCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                logger.debug("Read last commit position from path {}: rc = {}", zkPath, rc);
                if (KeeperException.Code.NONODE.intValue() == rc) {
                    result.setValue(DLSN.NonInclusiveLowerBound);
                } else if (KeeperException.Code.OK.intValue() != rc) {
                    result.setException(KeeperException.create(KeeperException.Code.get(rc), path));
                } else {
                    try {
                        DLSN dlsn = DLSN.deserialize(new String(data, Charsets.UTF_8));
                        result.setValue(dlsn);
                    } catch (Exception t) {
                        logger.warn("Invalid last commit position found from path {}", zkPath, t);
                        // invalid dlsn recorded in subscription state store
                        result.setValue(DLSN.NonInclusiveLowerBound);
                    }
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
        result.setException(zkce);
    } catch (InterruptedException ie) {
        result.setException(new DLInterruptedException("getLastCommitPosition was interrupted", ie));
    }
    return result;
}
Also used : DLSN(com.twitter.distributedlog.DLSN) AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Example 14 with DLInterruptedException

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

the class Utils method sync.

/**
     * Sync zookeeper client on given <i>path</i>.
     *
     * @param zkc
     *          zookeeper client
     * @param path
     *          path to sync
     * @return zookeeper client after sync
     * @throws IOException
     */
public static ZooKeeper sync(ZooKeeperClient zkc, String path) throws IOException {
    ZooKeeper zk;
    try {
        zk = zkc.get();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on checking if log " + path + " exists", e);
    }
    final CountDownLatch syncLatch = new CountDownLatch(1);
    final AtomicInteger syncResult = new AtomicInteger(0);
    zk.sync(path, new AsyncCallback.VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            syncResult.set(rc);
            syncLatch.countDown();
        }
    }, null);
    try {
        syncLatch.await();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on syncing zookeeper connection", e);
    }
    if (KeeperException.Code.OK.intValue() != syncResult.get()) {
        throw new ZKException("Error syncing zookeeper connection ", KeeperException.Code.get(syncResult.get()));
    }
    return zk;
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Example 15 with DLInterruptedException

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

the class BKLogHandler method forceGetLedgerList.

/**
     * Get a list of all segments in the journal.
     */
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;
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) KeeperException(org.apache.zookeeper.KeeperException)

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