Search in sources :

Example 21 with Versioned

use of org.apache.bookkeeper.versioning.Versioned in project distributedlog by twitter.

the class LedgerAllocatorPool method initializeAllocators.

/**
     * Initialize simple allocators with given list of allocator names <i>allocators</i>.
     * It initializes a simple allocator with its simple allocator path.
     */
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);
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Stat(org.apache.zookeeper.data.Stat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 22 with Versioned

use of org.apache.bookkeeper.versioning.Versioned 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

Versioned (org.apache.bookkeeper.versioning.Versioned)22 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)21 Test (org.junit.Test)17 Stat (org.apache.zookeeper.data.Stat)10 URI (java.net.URI)9 Promise (com.twitter.util.Promise)8 ZKException (com.twitter.distributedlog.exceptions.ZKException)7 Transaction (com.twitter.distributedlog.util.Transaction)6 Version (org.apache.bookkeeper.versioning.Version)6 KeeperException (org.apache.zookeeper.KeeperException)4 AsyncCallback (org.apache.zookeeper.AsyncCallback)3 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)2 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 ZooKeeperClient (com.twitter.distributedlog.ZooKeeperClient)1 LogExistsException (com.twitter.distributedlog.exceptions.LogExistsException)1 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)1 File (java.io.File)1 IOException (java.io.IOException)1 List (java.util.List)1