Search in sources :

Example 21 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testDeleteNonExistentLogSegment.

@Test(timeout = 60000)
public void testDeleteNonExistentLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> deleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(deleteTxn, segment, null);
    try {
        Utils.ioResult(deleteTxn.execute());
        fail("Should fail deletion if log segment doesn't exist");
    } catch (Throwable t) {
        assertTrue("Should throw NoNodeException if log segment doesn't exist", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NoNodeException if log segment doesn't exist", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 22 with ZKException

use of org.apache.distributedlog.exceptions.ZKException 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 23 with ZKException

use of org.apache.distributedlog.exceptions.ZKException 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 24 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdOnNonExistentPath.

@Test(timeout = 60000)
public void testStoreMaxTxnIdOnNonExistentPath() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    String nonExistentPath = rootZkPath + "/non-existent";
    LogMetadataForWriter metadata = mock(LogMetadataForWriter.class);
    when(metadata.getMaxTxIdPath()).thenReturn(nonExistentPath);
    lsmStore.storeMaxTxnId(updateTxn, metadata, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.complete(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.completeExceptionally(t);
        }
    });
    try {
        Utils.ioResult(updateTxn.execute());
        fail("Should fail on storing log record transaction id if path doesn't exist");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log record transaction id if path doesn't exist");
    } catch (ZKException ze) {
        assertEquals(KeeperException.Code.NONODE, ze.getKeeperExceptionCode());
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.distributedlog.exceptions.ZKException) Transaction(org.apache.distributedlog.util.Transaction) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter) Test(org.junit.Test)

Example 25 with ZKException

use of org.apache.distributedlog.exceptions.ZKException in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testCreateDeleteLogSegmentFailure.

@Test(timeout = 60000)
public void testCreateDeleteLogSegmentFailure() throws Exception {
    LogSegmentMetadata segment1 = createLogSegment(1L);
    LogSegmentMetadata segment2 = createLogSegment(2L);
    LogSegmentMetadata segment3 = createLogSegment(3L);
    // create log segment 1
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment1, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment1 + " should be created", zkc.get().exists(segment1.getZkPath(), false));
    // delete log segment 1 and delete log segment 2
    Transaction<Object> createDeleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(createDeleteTxn, segment1, null);
    lsmStore.deleteLogSegment(createDeleteTxn, segment2, null);
    lsmStore.createLogSegment(createDeleteTxn, segment3, null);
    try {
        Utils.ioResult(createDeleteTxn.execute());
        fail("Should fail transaction if one operation failed");
    } catch (Throwable t) {
        assertTrue("Transaction is aborted", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Transaction is aborted", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    // segment 1 should not be deleted
    assertNotNull("LogSegment " + segment1 + " should not be deleted", zkc.get().exists(segment1.getZkPath(), false));
    // segment 3 should not be created
    assertNull("LogSegment " + segment3 + " should be created", zkc.get().exists(segment3.getZkPath(), false));
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Aggregations

ZKException (org.apache.distributedlog.exceptions.ZKException)31 Test (org.junit.Test)15 CompletableFuture (java.util.concurrent.CompletableFuture)8 LongVersion (org.apache.bookkeeper.versioning.LongVersion)8 Versioned (org.apache.bookkeeper.versioning.Versioned)8 Stat (org.apache.zookeeper.data.Stat)7 List (java.util.List)6 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)5 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)4 Version (org.apache.bookkeeper.versioning.Version)4 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)4 ZKTransaction (org.apache.distributedlog.zk.ZKTransaction)4 KeeperException (org.apache.zookeeper.KeeperException)4 ZooKeeperConnectionException (org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException)3 LogExistsException (org.apache.distributedlog.exceptions.LogExistsException)3 LogNotFoundException (org.apache.distributedlog.exceptions.LogNotFoundException)3 Transaction (org.apache.distributedlog.util.Transaction)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3