Search in sources :

Example 1 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

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 {
        FutureUtils.result(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(com.twitter.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

the class TestLedgerAllocatorPool method testRescueAllocators.

@Test(timeout = 60000)
public void testRescueAllocators() throws Exception {
    String allocationPath = "/rescueAllocators";
    int numAllocators = 3;
    LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
    List<ZKTransaction> pendingTxns = Lists.newArrayListWithExpectedSize(numAllocators);
    List<String> allocatePaths = Lists.newArrayListWithExpectedSize(numAllocators);
    for (int i = 0; i < numAllocators; i++) {
        ZKTransaction txn = newTxn();
        pool.allocate();
        LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
        // get the corresponding ledger allocator
        SimpleLedgerAllocator sla = pool.getLedgerAllocator(lh);
        String slaPath = sla.allocatePath;
        logger.info("Allocated ledger {} from path {}", lh.getId(), slaPath);
        pendingTxns.add(txn);
        allocatePaths.add(slaPath);
    }
    for (int i = 0; i < numAllocators; i++) {
        ZKTransaction txn = pendingTxns.get(i);
        String slaPath = allocatePaths.get(i);
        // execute the transaction to confirm/abort obtain
        FutureUtils.result(txn.execute());
        // introduce error to individual ledger allocator
        byte[] data = zkc.get().getData(slaPath, false, new Stat());
        zkc.get().setData(slaPath, data, -1);
    }
    int numSuccess = 0;
    Set<String> allocatedPathSet = new HashSet<String>();
    while (numSuccess < 2 * numAllocators) {
        try {
            pool.allocate();
            ZKTransaction txn = newTxn();
            LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
            // get the corresponding ledger allocator
            SimpleLedgerAllocator sla = pool.getLedgerAllocator(lh);
            String slaPath = sla.allocatePath;
            logger.info("Allocated ledger {} from path {}", lh.getId(), slaPath);
            allocatedPathSet.add(slaPath);
            FutureUtils.result(txn.execute());
            ++numSuccess;
        } catch (IOException ioe) {
        // continue
        }
    }
    assertEquals(2 * numAllocators, numSuccess);
    assertEquals(numAllocators, allocatedPathSet.size());
    Utils.close(pool);
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) Stat(org.apache.zookeeper.data.Stat) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

the class TestLedgerAllocatorPool method testAllocateMultipleLedgers.

@Test(timeout = 60000)
public void testAllocateMultipleLedgers() throws Exception {
    String allocationPath = "/" + runtime.getMethodName();
    int numAllocators = 5;
    final LedgerAllocatorPool pool = new LedgerAllocatorPool(allocationPath, numAllocators, dlConf, zkc, bkc, allocationExecutor);
    int numLedgers = 20;
    Set<LedgerHandle> allocatedLedgers = new HashSet<LedgerHandle>();
    for (int i = 0; i < numLedgers; i++) {
        pool.allocate();
        ZKTransaction txn = newTxn();
        LedgerHandle lh = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
        FutureUtils.result(txn.execute());
        allocatedLedgers.add(lh);
    }
    assertEquals(numLedgers, allocatedLedgers.size());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

the class TestLedgerAllocatorPool method testConcurrentAllocation.

@Test
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 = FutureUtils.result(pool.tryObtain(txn, NULL_LISTENER));
                        FutureUtils.result(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(com.twitter.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 5 with ZKTransaction

use of com.twitter.distributedlog.zk.ZKTransaction in project distributedlog by twitter.

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 {
        FutureUtils.result(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(com.twitter.distributedlog.DistributedLogConfiguration) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) AllocationException(com.twitter.distributedlog.bk.SimpleLedgerAllocator.AllocationException) Test(org.junit.Test)

Aggregations

ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)16 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)13 Test (org.junit.Test)13 IOException (java.io.IOException)5 ZKException (com.twitter.distributedlog.exceptions.ZKException)4 HashSet (java.util.HashSet)3 Stat (org.apache.zookeeper.data.Stat)3 FutureEventListener (com.twitter.util.FutureEventListener)2 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 Ignore (org.junit.Ignore)2 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 AllocationException (com.twitter.distributedlog.bk.SimpleLedgerAllocator.AllocationException)1 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)1 EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)1 TransactionIdOutOfOrderException (com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 OpListener (com.twitter.distributedlog.util.Transaction.OpListener)1 LinkedList (java.util.LinkedList)1