Search in sources :

Example 6 with ZKTransaction

use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.

the class TestLedgerAllocatorPool method testConcurrentAllocation.

@Test(timeout = 60000)
public void testConcurrentAllocation() throws Exception {
    final int numAllocators = 5;
    String allocationPath = "/concurrentAllocation";
    final LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
    final ConcurrentMap<Long, LedgerHandle> allocatedLedgers = new ConcurrentHashMap<Long, LedgerHandle>();
    final AtomicInteger numFailures = new AtomicInteger(0);
    Thread[] allocationThreads = new Thread[numAllocators];
    for (int i = 0; i < numAllocators; i++) {
        final int tid = i;
        allocationThreads[i] = new Thread() {

            int numLedgers = 50;

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numLedgers; i++) {
                        pool.allocate();
                        ZKTransaction txn = newTxn();
                        LedgerHandle lh = Utils.ioResult(pool.tryObtain(txn, NULL_LISTENER));
                        Utils.ioResult(txn.execute());
                        lh.close();
                        allocatedLedgers.putIfAbsent(lh.getId(), lh);
                        logger.info("[thread {}] allocate {}th ledger {}", new Object[] { tid, i, lh.getId() });
                    }
                } catch (Exception ioe) {
                    numFailures.incrementAndGet();
                }
            }
        };
    }
    for (Thread t : allocationThreads) {
        t.start();
    }
    for (Thread t : allocationThreads) {
        t.join();
    }
    assertEquals(0, numFailures.get());
    assertEquals(50 * numAllocators, allocatedLedgers.size());
    Utils.close(pool);
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 7 with ZKTransaction

use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.

the class TestLedgerAllocatorPool method testObtainWhenNoAllocator.

@Test(timeout = 60000)
public void testObtainWhenNoAllocator() throws Exception {
    String allocationPath = "/obtainWhenNoAllocator";
    LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, 0, dlConf, zkc, bkc, allocationExecutor);
    ZKTransaction txn = newTxn();
    try {
        Utils.ioResult(pool.tryObtain(txn, NULL_LISTENER));
        fail("Should fail obtain ledger handle if there is no allocator.");
    } catch (SimpleLedgerAllocator.AllocationException ae) {
        fail("Should fail obtain ledger handle if there is no allocator.");
    } catch (IOException ioe) {
    // expected.
    }
    Utils.close(pool);
}
Also used : ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with ZKTransaction

use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.

the class TestLedgerAllocator method testCloseAllocatorDuringObtaining.

@Test(timeout = 60000)
public void testCloseAllocatorDuringObtaining() throws Exception {
    String allocationPath = "/allocation2";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8));
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) Test(org.junit.Test)

Example 9 with ZKTransaction

use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.

the class TestLedgerAllocator method testAllocatorWithoutEnoughBookies.

@Test(timeout = 60000)
public void testAllocatorWithoutEnoughBookies() throws Exception {
    String allocationPath = "/allocator-without-enough-bookies";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setEnsembleSize(numBookies * 2);
    confLocal.setWriteQuorumSize(numBookies * 2);
    SimpleLedgerAllocator allocator1 = createAllocator(allocationPath, confLocal);
    allocator1.allocate();
    ZKTransaction txn1 = newTxn();
    try {
        Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
        fail("Should fail allocating ledger if there aren't enough bookies");
    } catch (AllocationException ioe) {
        // expected
        assertEquals(Phase.ERROR, ioe.getPhase());
    }
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals(0, data.length);
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) AllocationException(org.apache.distributedlog.bk.SimpleLedgerAllocator.AllocationException) Test(org.junit.Test)

Example 10 with ZKTransaction

use of org.apache.distributedlog.zk.ZKTransaction in project bookkeeper by apache.

the class TestLedgerAllocator method testConcurrentAllocation.

@Test(timeout = 60000)
public void testConcurrentAllocation() throws Exception {
    String allcationPath = "/" + runtime.getMethodName();
    SimpleLedgerAllocator allocator = createAllocator(allcationPath);
    allocator.allocate();
    ZKTransaction txn1 = newTxn();
    CompletableFuture<LedgerHandle> obtainFuture1 = allocator.tryObtain(txn1, NULL_LISTENER);
    ZKTransaction txn2 = newTxn();
    CompletableFuture<LedgerHandle> obtainFuture2 = allocator.tryObtain(txn2, NULL_LISTENER);
    assertTrue(obtainFuture2.isDone());
    assertTrue(obtainFuture2.isCompletedExceptionally());
    try {
        Utils.ioResult(obtainFuture2);
        fail("Should fail the concurrent obtain since there is already a transaction obtaining the ledger handle");
    } catch (SimpleLedgerAllocator.ConcurrentObtainException cbe) {
    // expected
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) Test(org.junit.Test)

Aggregations

ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)14 Test (org.junit.Test)13 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)12 ZKException (org.apache.distributedlog.exceptions.ZKException)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Stat (org.apache.zookeeper.data.Stat)3 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 LongVersion (org.apache.bookkeeper.versioning.LongVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 Ignore (org.junit.Ignore)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BKException (org.apache.bookkeeper.client.BKException)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 AllocationException (org.apache.distributedlog.bk.SimpleLedgerAllocator.AllocationException)1 OpListener (org.apache.distributedlog.util.Transaction.OpListener)1