Search in sources :

Example 11 with ZKTransaction

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

the class TestLedgerAllocator method testSuccessAllocatorShouldDeleteUnusedledger.

@Test(timeout = 60000)
public void testSuccessAllocatorShouldDeleteUnusedledger() throws Exception {
    String allocationPath = "/allocation-delete-unused-ledger";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh1 = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
    // Second allocator kicks in
    stat = new Stat();
    data = zkc.get().getData(allocationPath, false, stat);
    allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator2.allocate();
    // wait until allocated
    ZKTransaction txn2 = newTxn();
    LedgerHandle lh2 = Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
    // should fail to commit txn1 as version is changed by second allocator
    try {
        Utils.ioResult(txn1.execute());
        fail("Should fail commit obtaining ledger handle from first allocator" + " as allocator is modified by second allocator.");
    } catch (ZKException ke) {
    // as expected
    }
    Utils.ioResult(txn2.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);
    // ledger handle should be deleted
    try {
        lh1.close();
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException bke) {
    // as expected
    }
    try {
        bkc.get().openLedger(lh1.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException.BKNoSuchLedgerExistsException nslee) {
    // as expected
    }
    long eid = lh2.addEntry("hello world".getBytes());
    lh2.close();
    LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Example 12 with ZKTransaction

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

the class TestLedgerAllocator method testBadVersionOnTwoAllocators.

@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException ke) {
        assertEquals(KeeperException.Code.BADVERSION, ke.getKeeperExceptionCode());
    }
    Utils.ioResult(txn1.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);
    long eid = lh.addEntry("hello world".getBytes());
    lh.close();
    LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.junit.Test)

Example 13 with ZKTransaction

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

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 = Utils.ioResult(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
        Utils.ioResult(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 = Utils.ioResult(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);
            Utils.ioResult(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(org.apache.distributedlog.zk.ZKTransaction) IOException(java.io.IOException) Stat(org.apache.zookeeper.data.Stat) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with ZKTransaction

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

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 = Utils.ioResult(pool.tryObtain(txn, NULL_LISTENER));
        Utils.ioResult(txn.execute());
        allocatedLedgers.add(lh);
    }
    assertEquals(numLedgers, allocatedLedgers.size());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) HashSet(java.util.HashSet) 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