Search in sources :

Example 16 with ZKException

use of com.twitter.distributedlog.exceptions.ZKException 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 17 with ZKException

use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testCreateLogSegment.

@Test(timeout = 60000)
public void testCreateLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L);
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment);
    FutureUtils.result(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment + " should be created", zkc.get().exists(segment.getZkPath(), false));
    LogSegmentMetadata segment2 = createLogSegment(1L);
    Transaction<Object> createTxn2 = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn2, segment2);
    try {
        FutureUtils.result(createTxn2.execute());
        fail("Should fail if log segment exists");
    } catch (Throwable t) {
        // expected
        assertTrue("Should throw NodeExistsException if log segment exists", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Should throw NodeExistsException if log segment exists", KeeperException.Code.NODEEXISTS, zke.getKeeperExceptionCode());
    }
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 18 with ZKException

use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.

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);
    FutureUtils.result(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);
    lsmStore.deleteLogSegment(createDeleteTxn, segment2);
    lsmStore.createLogSegment(createDeleteTxn, segment3);
    try {
        FutureUtils.result(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(com.twitter.distributedlog.exceptions.ZKException) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 19 with ZKException

use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.

the class TestDistributedLogTool method testToolCreateZkAclId.

@Test(timeout = 60000)
public void testToolCreateZkAclId() throws Exception {
    createStream(defaultUri, "0", "CreateAclStream", defaultPrivilegedZkAclId);
    try {
        DistributedLogManager dlm = DLMTestUtil.createNewDLM("0CreateAclStream", conf, defaultUri);
        DLMTestUtil.generateCompletedLogSegments(dlm, conf, 3, 1000);
        dlm.close();
    } catch (ZKException ex) {
        assertEquals(KeeperException.Code.NOAUTH, ex.getKeeperExceptionCode());
    }
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) Test(org.junit.Test)

Example 20 with ZKException

use of com.twitter.distributedlog.exceptions.ZKException in project distributedlog by twitter.

the class Utils method sync.

/**
     * Sync zookeeper client on given <i>path</i>.
     *
     * @param zkc
     *          zookeeper client
     * @param path
     *          path to sync
     * @return zookeeper client after sync
     * @throws IOException
     */
public static ZooKeeper sync(ZooKeeperClient zkc, String path) throws IOException {
    ZooKeeper zk;
    try {
        zk = zkc.get();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on checking if log " + path + " exists", e);
    }
    final CountDownLatch syncLatch = new CountDownLatch(1);
    final AtomicInteger syncResult = new AtomicInteger(0);
    zk.sync(path, new AsyncCallback.VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            syncResult.set(rc);
            syncLatch.countDown();
        }
    }, null);
    try {
        syncLatch.await();
    } catch (InterruptedException e) {
        throw new DLInterruptedException("Interrupted on syncing zookeeper connection", e);
    }
    if (KeeperException.Code.OK.intValue() != syncResult.get()) {
        throw new ZKException("Error syncing zookeeper connection ", KeeperException.Code.get(syncResult.get()));
    }
    return zk;
}
Also used : ZKException(com.twitter.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Aggregations

ZKException (com.twitter.distributedlog.exceptions.ZKException)23 Test (org.junit.Test)13 KeeperException (org.apache.zookeeper.KeeperException)9 Stat (org.apache.zookeeper.data.Stat)9 Promise (com.twitter.util.Promise)7 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)7 Versioned (org.apache.bookkeeper.versioning.Versioned)7 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)6 List (java.util.List)6 AsyncCallback (org.apache.zookeeper.AsyncCallback)5 LogSegmentMetadata (com.twitter.distributedlog.LogSegmentMetadata)4 Transaction (com.twitter.distributedlog.util.Transaction)4 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)4 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)4 Version (org.apache.bookkeeper.versioning.Version)4 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)3 ArrayList (java.util.ArrayList)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 LogExistsException (com.twitter.distributedlog.exceptions.LogExistsException)2 IOException (java.io.IOException)2