Search in sources :

Example 6 with ZooKeeperClient

use of com.twitter.distributedlog.ZooKeeperClient in project distributedlog by twitter.

the class DLAuditor method calculateLedgerSpaceUsage.

public long calculateLedgerSpaceUsage(URI uri) throws IOException {
    List<URI> uris = Lists.newArrayList(uri);
    String zkServers = validateAndGetZKServers(uris);
    RetryPolicy retryPolicy = new BoundExponentialBackoffRetryPolicy(conf.getZKRetryBackoffStartMillis(), conf.getZKRetryBackoffMaxMillis(), Integer.MAX_VALUE);
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().name("DLAuditor-ZK").zkServers(zkServers).sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryPolicy(retryPolicy).zkAclId(conf.getZkAclId()).build();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        BKDLConfig bkdlConfig = resolveBKDLConfig(zkc, uris);
        logger.info("Resolved bookkeeper config : {}", bkdlConfig);
        BookKeeperClient bkc = BookKeeperClientBuilder.newBuilder().name("DLAuditor-BK").dlConfig(conf).zkServers(bkdlConfig.getBkZkServersForWriter()).ledgersPath(bkdlConfig.getBkLedgersPath()).build();
        try {
            return calculateLedgerSpaceUsage(bkc, executorService);
        } finally {
            bkc.close();
        }
    } finally {
        zkc.close();
        executorService.shutdown();
    }
}
Also used : ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) BookKeeperClient(com.twitter.distributedlog.BookKeeperClient) ExecutorService(java.util.concurrent.ExecutorService) BKDLConfig(com.twitter.distributedlog.metadata.BKDLConfig) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) URI(java.net.URI) RetryPolicy(org.apache.bookkeeper.zookeeper.RetryPolicy) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy)

Example 7 with ZooKeeperClient

use of com.twitter.distributedlog.ZooKeeperClient in project distributedlog by twitter.

the class DLAuditor method collectLedgers.

public Pair<Set<Long>, Set<Long>> collectLedgers(List<URI> uris, List<List<String>> allocationPaths) throws IOException {
    Preconditions.checkArgument(uris.size() > 0, "No uri provided to audit");
    String zkServers = validateAndGetZKServers(uris);
    RetryPolicy retryPolicy = new BoundExponentialBackoffRetryPolicy(conf.getZKRetryBackoffStartMillis(), conf.getZKRetryBackoffMaxMillis(), Integer.MAX_VALUE);
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().name("DLAuditor-ZK").zkServers(zkServers).sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryPolicy(retryPolicy).zkAclId(conf.getZkAclId()).build();
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        BKDLConfig bkdlConfig = resolveBKDLConfig(zkc, uris);
        logger.info("Resolved bookkeeper config : {}", bkdlConfig);
        BookKeeperClient bkc = BookKeeperClientBuilder.newBuilder().name("DLAuditor-BK").dlConfig(conf).zkServers(bkdlConfig.getBkZkServersForWriter()).ledgersPath(bkdlConfig.getBkLedgersPath()).build();
        try {
            Set<Long> bkLedgers = collectLedgersFromBK(bkc, executorService);
            Set<Long> dlLedgers = collectLedgersFromDL(uris, allocationPaths);
            return Pair.of(bkLedgers, dlLedgers);
        } finally {
            bkc.close();
        }
    } finally {
        zkc.close();
        executorService.shutdown();
    }
}
Also used : ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) BookKeeperClient(com.twitter.distributedlog.BookKeeperClient) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) BKDLConfig(com.twitter.distributedlog.metadata.BKDLConfig) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy) RetryPolicy(org.apache.bookkeeper.zookeeper.RetryPolicy) BoundExponentialBackoffRetryPolicy(org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy)

Example 8 with ZooKeeperClient

use of com.twitter.distributedlog.ZooKeeperClient in project distributedlog by twitter.

the class DLMetadata method update.

public void update(URI uri) throws IOException {
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    ZooKeeperClient zkc = ZooKeeperClientBuilder.newBuilder().sessionTimeoutMs(conf.getZKSessionTimeoutMilliseconds()).retryThreadCount(conf.getZKClientNumberRetryThreads()).requestRateLimit(conf.getZKRequestRateLimit()).zkAclId(conf.getZkAclId()).uri(uri).build();
    byte[] data = serialize();
    try {
        zkc.get().setData(uri.getPath(), data, -1);
    } catch (KeeperException e) {
        throw new IOException("Fail to update dl metadata " + new String(data, UTF_8) + " to uri " + uri, e);
    } catch (InterruptedException e) {
        throw new IOException("Interrupted when updating dl metadata " + new String(data, UTF_8) + " to uri " + uri, e);
    } finally {
        zkc.close();
    }
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException)

Example 9 with ZooKeeperClient

use of com.twitter.distributedlog.ZooKeeperClient in project distributedlog by twitter.

the class TestZKTransaction method testAbortTransaction.

@Test(timeout = 60000)
public void testAbortTransaction() throws Exception {
    ZooKeeperClient zkc = mock(ZooKeeperClient.class);
    ZKTransaction transaction = new ZKTransaction(zkc);
    int numOps = 3;
    final CountDownLatch commitLatch = new CountDownLatch(numOps);
    final CountDownLatch abortLatch = new CountDownLatch(numOps);
    for (int i = 0; i < numOps; i++) {
        transaction.addOp(new CountDownZKOp(commitLatch, abortLatch));
    }
    transaction.abort(new DLIllegalStateException("Illegal State"));
    abortLatch.await();
    assertEquals(0, abortLatch.getCount());
    assertEquals(numOps, commitLatch.getCount());
}
Also used : ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) DLIllegalStateException(com.twitter.distributedlog.exceptions.DLIllegalStateException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with ZooKeeperClient

use of com.twitter.distributedlog.ZooKeeperClient in project distributedlog by twitter.

the class SimpleLedgerAllocator method createAllocationData.

private static Future<Versioned<byte[]>> createAllocationData(final String allocatePath, final ZooKeeperClient zkc) {
    try {
        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));
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Aggregations

ZooKeeperClient (com.twitter.distributedlog.ZooKeeperClient)12 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)5 Test (org.junit.Test)5 BKDLConfig (com.twitter.distributedlog.metadata.BKDLConfig)4 IOException (java.io.IOException)4 KeeperException (org.apache.zookeeper.KeeperException)3 BookKeeperClient (com.twitter.distributedlog.BookKeeperClient)2 URI (java.net.URI)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 BoundExponentialBackoffRetryPolicy (org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy)2 RetryPolicy (org.apache.bookkeeper.zookeeper.RetryPolicy)2 Stopwatch (com.google.common.base.Stopwatch)1 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)1 LogSegmentMetadata (com.twitter.distributedlog.LogSegmentMetadata)1 AccessControlManager (com.twitter.distributedlog.acl.AccessControlManager)1 ZKAccessControl (com.twitter.distributedlog.acl.ZKAccessControl)1 DLException (com.twitter.distributedlog.exceptions.DLException)1 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)1 LogMetadataStore (com.twitter.distributedlog.metadata.LogMetadataStore)1