Search in sources :

Example 56 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

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 LongVersion(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) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Aggregations

LongVersion (org.apache.bookkeeper.versioning.LongVersion)56 Test (org.junit.Test)37 Versioned (org.apache.bookkeeper.versioning.Versioned)29 Stat (org.apache.zookeeper.data.Stat)25 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Version (org.apache.bookkeeper.versioning.Version)18 GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)16 BKException (org.apache.bookkeeper.client.BKException)12 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)12 Set (java.util.Set)11 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)10 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)9 Before (org.junit.Before)9 URI (java.net.URI)8 ZKException (org.apache.distributedlog.exceptions.ZKException)8 KeeperException (org.apache.zookeeper.KeeperException)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.fail (org.junit.Assert.fail)8