Search in sources :

Example 1 with ReadLastConfirmedCallback

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

the class BookieReadWriteTest method testReadLastConfirmed.

@Test
public void testReadLastConfirmed() throws Exception {
    // Create a ledger and add entries
    lh = bkc.createLedger(digestType, ledgerPassword);
    // bkc.initMessageDigest("SHA1");
    ledgerId = lh.getId();
    LOG.info("Ledger ID: " + lh.getId());
    long previousLAC = writeNEntriesLastWriteSync(lh, 5);
    // add more entries after opening ReadonlyLedgerHandle
    LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
    long currentLAC = writeNEntriesLastWriteSync(lh, 5);
    // get LAC instance variable of ReadHandle and verify if it is equal to (previousLAC - 1)
    long readLAC = lhOpen.getLastAddConfirmed();
    Assert.assertEquals("Test1 - For ReadHandle LAC", (previousLAC - 1), readLAC);
    // close the write LedgerHandle and sleep for 500 msec to make sure all close watchers are called
    lh.close();
    Thread.sleep(500);
    // now call asyncReadLastConfirmed and verify if it is equal to currentLAC
    CountDownLatch latch = new CountDownLatch(1);
    final int[] rcArray = { 0 };
    final long[] lastConfirmedArray = { 0 };
    lhOpen.asyncReadLastConfirmed(new ReadLastConfirmedCallback() {

        @Override
        public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            lastConfirmedArray[0] = lastConfirmed;
            latch.countDown();
        }
    }, latch);
    latch.await();
    Assert.assertEquals("Test3 - asyncReadLastConfirmed response", KeeperException.Code.OK.intValue(), rcArray[0]);
    Assert.assertEquals("Test3 - ReadLAC", currentLAC, lastConfirmedArray[0]);
    // similarly try calling asyncTryReadLastConfirmed and verify if it is equal to currentLAC
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    lastConfirmedArray[0] = 0;
    lhOpen.asyncTryReadLastConfirmed(new ReadLastConfirmedCallback() {

        @Override
        public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
            CountDownLatch latch = (CountDownLatch) ctx;
            rcArray[0] = rc;
            lastConfirmedArray[0] = lastConfirmed;
            latch.countDown();
        }
    }, latch);
    latch.await();
    Assert.assertEquals("Test4 - asyncTryReadLastConfirmed response", KeeperException.Code.OK.intValue(), rcArray[0]);
    Assert.assertEquals("Test4 - ReadLAC", currentLAC, lastConfirmedArray[0]);
    // similarly try calling tryReadLastConfirmed and verify if it is equal to currentLAC
    long tryReadLAC = lhOpen.tryReadLastConfirmed();
    Assert.assertEquals("Test5 - ReadLAC", currentLAC, tryReadLAC);
}
Also used : ReadLastConfirmedCallback(org.apache.bookkeeper.client.AsyncCallback.ReadLastConfirmedCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ReadLastConfirmedCallback

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

the class LedgerOpenOp method operationComplete.

/**
 * Implements Open Ledger Callback.
 */
@Override
public void operationComplete(int rc, LedgerMetadata metadata) {
    if (BKException.Code.OK != rc) {
        // open ledger failed.
        openComplete(rc, null);
        return;
    }
    final byte[] passwd;
    // we should use digest type from metadata *ONLY* when:
    // 1) digest type is stored in metadata
    // 2) `autodetection` is enabled
    DigestType digestType = enableDigestAutodetection && metadata.hasPassword() ? fromApiDigestType(metadata.getDigestType()) : suggestedDigestType;
    /* For an administrative open, the default passwords
         * are read from the configuration, but if the metadata
         * already contains passwords, use these instead. */
    if (administrativeOpen && metadata.hasPassword()) {
        passwd = metadata.getPassword();
        digestType = fromApiDigestType(metadata.getDigestType());
    } else {
        passwd = this.passwd;
        if (metadata.hasPassword()) {
            if (!Arrays.equals(passwd, metadata.getPassword())) {
                LOG.error("Provided passwd does not match that in metadata");
                openComplete(BKException.Code.UnauthorizedAccessException, null);
                return;
            }
            // changes. e.g. moving from `crc32` to `crc32c`.
            if (suggestedDigestType != fromApiDigestType(metadata.getDigestType()) && !enableDigestAutodetection) {
                LOG.error("Provided digest does not match that in metadata");
                openComplete(BKException.Code.DigestMatchException, null);
                return;
            }
        }
    }
    // get the ledger metadata back
    try {
        lh = new ReadOnlyLedgerHandle(bk, ledgerId, metadata, digestType, passwd, !doRecovery);
    } catch (GeneralSecurityException e) {
        LOG.error("Security exception while opening ledger: " + ledgerId, e);
        openComplete(BKException.Code.DigestNotInitializedException, null);
        return;
    } catch (NumberFormatException e) {
        LOG.error("Incorrectly entered parameter throttle: " + bk.getConf().getThrottleValue(), e);
        openComplete(BKException.Code.IncorrectParameterException, null);
        return;
    }
    if (metadata.isClosed()) {
        // Ledger was closed properly
        openComplete(BKException.Code.OK, lh);
        return;
    }
    if (doRecovery) {
        lh.recover(new OrderedGenericCallback<Void>(bk.getMainWorkerPool(), ledgerId) {

            @Override
            public void safeOperationComplete(int rc, Void result) {
                if (rc == BKException.Code.OK) {
                    openComplete(BKException.Code.OK, lh);
                } else if (rc == BKException.Code.UnauthorizedAccessException) {
                    openComplete(BKException.Code.UnauthorizedAccessException, null);
                } else {
                    openComplete(bk.getReturnRc(BKException.Code.LedgerRecoveryException), null);
                }
            }

            @Override
            public String toString() {
                return String.format("Recover(%d)", ledgerId);
            }
        });
    } else {
        lh.asyncReadLastConfirmed(new ReadLastConfirmedCallback() {

            @Override
            public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
                if (rc != BKException.Code.OK) {
                    openComplete(bk.getReturnRc(BKException.Code.ReadException), null);
                } else {
                    lh.lastAddConfirmed = lh.lastAddPushed = lastConfirmed;
                    openComplete(BKException.Code.OK, lh);
                }
            }
        }, null);
    }
}
Also used : ReadLastConfirmedCallback(org.apache.bookkeeper.client.AsyncCallback.ReadLastConfirmedCallback) GeneralSecurityException(java.security.GeneralSecurityException) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) DigestType.fromApiDigestType(org.apache.bookkeeper.client.BookKeeper.DigestType.fromApiDigestType)

Aggregations

ReadLastConfirmedCallback (org.apache.bookkeeper.client.AsyncCallback.ReadLastConfirmedCallback)2 GeneralSecurityException (java.security.GeneralSecurityException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DigestType (org.apache.bookkeeper.client.BookKeeper.DigestType)1 DigestType.fromApiDigestType (org.apache.bookkeeper.client.BookKeeper.DigestType.fromApiDigestType)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 Test (org.junit.Test)1