Search in sources :

Example 11 with ZKTransaction

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

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

Example 12 with ZKTransaction

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

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();
    Future<LedgerHandle> obtainFuture1 = allocator.tryObtain(txn1, NULL_LISTENER);
    ZKTransaction txn2 = newTxn();
    Future<LedgerHandle> obtainFuture2 = allocator.tryObtain(txn2, NULL_LISTENER);
    assertTrue(obtainFuture2.isDefined());
    assertTrue(obtainFuture2.isThrow());
    try {
        FutureUtils.result(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(com.twitter.distributedlog.zk.ZKTransaction) Test(org.junit.Test)

Example 13 with ZKTransaction

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

the class TestLedgerAllocator method testAllocation.

/**
 * {@link https://issues.apache.org/jira/browse/DL-43}
 */
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
public void testAllocation() throws Exception {
    String allocationPath = "/allocation1";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    logger.info("Try obtaining ledger handle {}", lh.getId());
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1)));
    try {
        FutureUtils.result(txn.execute());
        fail("Should fail the transaction when setting unexisted path");
    } catch (ZKException ke) {
        // expected
        logger.info("Should fail on executing transaction when setting unexisted path", ke);
    }
    data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // Create new transaction to obtain the ledger again.
    txn = newTxn();
    // we could obtain the ledger if it was obtained
    LedgerHandle newLh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    assertEquals(lh.getId(), newLh.getId());
    FutureUtils.result(txn.execute());
    data = zkc.get().getData(allocationPath, false, null);
    assertEquals(0, data.length);
    Utils.close(allocator);
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with ZKTransaction

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

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 ZkVersion(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 = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    FutureUtils.result(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(com.twitter.distributedlog.zk.ZKTransaction) ZKException(com.twitter.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Test(org.junit.Test)

Example 15 with ZKTransaction

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

the class TestLedgerAllocator method testCloseAllocatorAfterAbort.

@Test(timeout = 60000)
public void testCloseAllocatorAfterAbort() throws Exception {
    String allocationPath = "/allocation3";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1)));
    try {
        FutureUtils.result(txn.execute());
        fail("Should fail the transaction when setting unexisted path");
    } catch (ZKException ke) {
    // expected
    }
    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 : ZKException(com.twitter.distributedlog.exceptions.ZKException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) 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