Search in sources :

Example 1 with CloseCallback

use of org.apache.bookkeeper.client.AsyncCallback.CloseCallback in project bookkeeper by apache.

the class BookieReadWriteTest method testWriteUsingReadOnlyHandle.

@Test
public void testWriteUsingReadOnlyHandle() throws Exception {
    // Create a ledger
    lh = bkc.createLedger(digestType, ledgerPassword);
    ledgerId = lh.getId();
    LOG.info("Ledger ID: " + lh.getId());
    long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
    LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
    // addEntry on ReadOnlyHandle should fail
    CountDownLatch latch = new CountDownLatch(1);
    final int[] rcArray = { 0 };
    lhOpen.asyncAddEntry("".getBytes(), new AddCallback() {

        @Override
        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            latch.countDown();
        }
    }, latch);
    latch.await();
    if (rcArray[0] != BKException.Code.IllegalOpException) {
        Assert.fail("Test1 - asyncAddOperation is supposed to be failed, but it got following rc - " + KeeperException.Code.get(rcArray[0]));
    }
    // addEntry on ReadOnlyHandle should fail
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    lhOpen.asyncAddEntry("".getBytes(), 0, 0, new AddCallback() {

        @Override
        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            latch.countDown();
        }
    }, latch);
    latch.await();
    if (rcArray[0] != BKException.Code.IllegalOpException) {
        Assert.fail("Test2 - asyncAddOperation is supposed to fail with IllegalOpException, but it got following rc - " + KeeperException.Code.get(rcArray[0]));
    }
    // close readonlyhandle
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    lhOpen.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            latch.countDown();
        }
    }, latch);
    latch.await();
    if (rcArray[0] != KeeperException.Code.OK.intValue()) {
        Assert.fail("Test3 - asyncClose failed because of exception - " + KeeperException.Code.get(rcArray[0]));
    }
    // close of readonlyhandle should not affect the writehandle
    writeNEntriesLastWriteSync(lh, 5);
    lh.close();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CloseCallback(org.apache.bookkeeper.client.AsyncCallback.CloseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with CloseCallback

use of org.apache.bookkeeper.client.AsyncCallback.CloseCallback in project bookkeeper by apache.

the class BookKeeperCloseTest method testCloseLedger.

/**
 * Test that closing a ledger using bookkeeper client which is closed should
 * throw ClientClosedException.
 */
@Test
public void testCloseLedger() throws Exception {
    BookKeeper bk = new BookKeeper(baseClientConf, zkc);
    LOG.info("Create ledger and add entries to it");
    LedgerHandle lh = createLedgerWithEntries(bk, 100);
    LedgerHandle lh2 = createLedgerWithEntries(bk, 100);
    LOG.info("Closing bookkeeper client");
    bk.close();
    try {
        lh.close();
        fail("should have failed, client is closed");
    } catch (BKClientClosedException e) {
    // correct
    }
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    lh2.asyncClose(new CloseCallback() {

        public void closeComplete(int rccb, LedgerHandle lh, Object ctx) {
            rc.set(rccb);
            completeLatch.countDown();
        }
    }, null);
    LOG.info("Waiting to finish adding another entry asynchronously");
    assertTrue("Close ledger call should have completed", completeLatch.await(20, TimeUnit.SECONDS));
    assertEquals("Close ledger should have succeeded through closed bkclient!", BKException.Code.ClientClosedException, rc.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CloseCallback(org.apache.bookkeeper.client.AsyncCallback.CloseCallback) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with CloseCallback

use of org.apache.bookkeeper.client.AsyncCallback.CloseCallback in project incubator-pulsar by apache.

the class ManagedCursorImpl method asyncCloseCursorLedger.

void asyncCloseCursorLedger(final AsyncCallbacks.CloseCallback callback, final Object ctx) {
    LedgerHandle lh = cursorLedger;
    ledger.mbean.startCursorLedgerCloseOp();
    log.info("[{}] [{}] Closing metadata ledger {}", ledger.getName(), name, lh.getId());
    lh.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
            ledger.mbean.endCursorLedgerCloseOp();
            if (rc == BKException.Code.OK) {
                callback.closeComplete(ctx);
            } else {
                callback.closeFailed(createManagedLedgerException(rc), ctx);
            }
        }
    }, ctx);
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CloseCallback(org.apache.bookkeeper.client.AsyncCallback.CloseCallback)

Example 4 with CloseCallback

use of org.apache.bookkeeper.client.AsyncCallback.CloseCallback in project pulsar by yahoo.

the class ManagedCursorImpl method asyncCloseCursorLedger.

void asyncCloseCursorLedger(final AsyncCallbacks.CloseCallback callback, final Object ctx) {
    LedgerHandle lh = cursorLedger;
    ledger.mbean.startCursorLedgerCloseOp();
    log.info("[{}] [{}] Closing metadata ledger {}", ledger.getName(), name, lh.getId());
    lh.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
            ledger.mbean.endCursorLedgerCloseOp();
            if (rc == BKException.Code.OK) {
                callback.closeComplete(ctx);
            } else {
                callback.closeFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
            }
        }
    }, ctx);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CloseCallback(org.apache.bookkeeper.client.AsyncCallback.CloseCallback)

Aggregations

CloseCallback (org.apache.bookkeeper.client.AsyncCallback.CloseCallback)4 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AddCallback (org.apache.bookkeeper.client.AsyncCallback.AddCallback)1 BKClientClosedException (org.apache.bookkeeper.client.BKException.BKClientClosedException)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1