Search in sources :

Example 1 with AddCallback

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

the class AuditorPeriodicCheckTest method testPeriodicCheckWhenDisabled.

/**
 * Test that the period checker will not run when auto replication has been disabled.
 */
@Test
public void testPeriodicCheckWhenDisabled() throws Exception {
    LedgerManagerFactory mFactory = driver.getLedgerManagerFactory();
    final LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
    final int numLedgers = 10;
    final int numMsgs = 2;
    final CountDownLatch completeLatch = new CountDownLatch(numMsgs * numLedgers);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    List<LedgerHandle> lhs = new ArrayList<LedgerHandle>();
    for (int i = 0; i < numLedgers; i++) {
        LedgerHandle lh = bkc.createLedger(3, 3, DigestType.CRC32, "passwd".getBytes());
        lhs.add(lh);
        for (int j = 0; j < 2; j++) {
            lh.asyncAddEntry("testdata".getBytes(), new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    if (rc.compareAndSet(BKException.Code.OK, rc2)) {
                        LOG.info("Failed to add entry : {}", BKException.getMessage(rc2));
                    }
                    completeLatch.countDown();
                }
            }, null);
        }
    }
    completeLatch.await();
    if (rc.get() != BKException.Code.OK) {
        throw BKException.create(rc.get());
    }
    for (LedgerHandle lh : lhs) {
        lh.close();
    }
    underReplicationManager.disableLedgerReplication();
    final AtomicInteger numReads = new AtomicInteger(0);
    ServerConfiguration conf = killBookie(0);
    Bookie deadBookie = new Bookie(conf) {

        @Override
        public ByteBuf readEntry(long ledgerId, long entryId) throws IOException, NoLedgerException {
            // we want to disable during checking
            numReads.incrementAndGet();
            throw new IOException("Fake I/O exception");
        }
    };
    bsConfs.add(conf);
    bs.add(startBookie(conf, deadBookie));
    Thread.sleep(CHECK_INTERVAL * 2000);
    assertEquals("Nothing should have tried to read", 0, numReads.get());
    underReplicationManager.enableLedgerReplication();
    // give it time to run
    Thread.sleep(CHECK_INTERVAL * 2000);
    underReplicationManager.disableLedgerReplication();
    // give it time to stop, from this point nothing new should be marked
    Thread.sleep(CHECK_INTERVAL * 2000);
    int numUnderreplicated = 0;
    long underReplicatedLedger = -1;
    do {
        underReplicatedLedger = underReplicationManager.pollLedgerToRereplicate();
        if (underReplicatedLedger == -1) {
            break;
        }
        numUnderreplicated++;
        underReplicationManager.markLedgerReplicated(underReplicatedLedger);
    } while (underReplicatedLedger != -1);
    // give a chance to run again (it shouldn't, it's disabled)
    Thread.sleep(CHECK_INTERVAL * 2000);
    // ensure that nothing is marked as underreplicated
    underReplicatedLedger = underReplicationManager.pollLedgerToRereplicate();
    assertEquals("There should be no underreplicated ledgers", -1, underReplicatedLedger);
    LOG.info("{} of {} ledgers underreplicated", numUnderreplicated, numUnderreplicated);
    assertTrue("All should be underreplicated", numUnderreplicated <= numLedgers && numUnderreplicated > 0);
}
Also used : LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) Bookie(org.apache.bookkeeper.bookie.Bookie) Test(org.junit.Test)

Example 2 with AddCallback

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

the class BookieJournalRollingTest method writeLedgerEntries.

private void writeLedgerEntries(LedgerHandle[] lhs, int msgSize, int numMsgs) throws Exception {
    // Create a dummy message string to write as ledger entries
    StringBuilder msgSB = new StringBuilder();
    for (int i = 0; i < msgSize; i++) {
        msgSB.append("a");
    }
    String msg = msgSB.toString();
    final CountDownLatch completeLatch = new CountDownLatch(numMsgs * lhs.length);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    // Write all of the entries for all of the ledgers
    for (int i = 0; i < numMsgs; i++) {
        for (int j = 0; j < lhs.length; j++) {
            StringBuilder sb = new StringBuilder();
            sb.append(lhs[j].getId()).append('-').append(i).append('-').append(msg);
            lhs[j].asyncAddEntry(sb.toString().getBytes(), new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rc2);
                    completeLatch.countDown();
                }
            }, null);
        }
    }
    completeLatch.await();
    if (rc.get() != BKException.Code.OK) {
        throw BKException.create(rc.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with AddCallback

use of org.apache.bookkeeper.client.AsyncCallback.AddCallback 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 4 with AddCallback

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

the class BookieReadWriteTest method testMultiLedger.

@Test
public void testMultiLedger() throws IOException {
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        lh2 = bkc.createLedger(digestType, ledgerPassword);
        long ledgerId = lh.getId();
        long ledgerId2 = lh2.getId();
        final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite * 2);
        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
        // bkc.initMessageDigest("SHA1");
        LOG.info("Ledger ID 1: " + lh.getId() + ", Ledger ID 2: " + lh2.getId());
        for (int i = 0; i < numEntriesToWrite; i++) {
            lh.asyncAddEntry(new byte[0], new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rc2);
                    completeLatch.countDown();
                }
            }, null);
            lh2.asyncAddEntry(new byte[0], new AddCallback() {

                public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rc2);
                    completeLatch.countDown();
                }
            }, null);
        }
        completeLatch.await();
        if (rc.get() != BKException.Code.OK) {
            throw BKException.create(rc.get());
        }
        lh.close();
        lh2.close();
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        lh2 = bkc.openLedger(ledgerId2, digestType, ledgerPassword);
        LOG.debug("Number of entries written: " + lh.getLastAddConfirmed() + ", " + lh2.getLastAddConfirmed());
        assertTrue("Verifying number of entries written lh (" + lh.getLastAddConfirmed() + ")", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
        assertTrue("Verifying number of entries written lh2 (" + lh2.getLastAddConfirmed() + ")", lh2.getLastAddConfirmed() == (numEntriesToWrite - 1));
        Enumeration<LedgerEntry> ls = lh.readEntries(0, numEntriesToWrite - 1);
        int i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            LOG.debug("Length of result: " + result.capacity());
            assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
        }
        lh.close();
        ls = lh2.readEntries(0, numEntriesToWrite - 1);
        i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            LOG.debug("Length of result: " + result.capacity());
            assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
        }
        lh2.close();
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with AddCallback

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

the class BookieReadWriteTest method testLedgerHandle.

@Test
public void testLedgerHandle() throws Exception {
    // Create a ledger
    lh = bkc.createLedger(digestType, ledgerPassword);
    ledgerId = lh.getId();
    LOG.info("Ledger ID: " + lh.getId());
    long lac = writeNEntriesLastWriteSync(lh, 5);
    // doing addEntry with entryid using regular Ledgerhandle should fail
    CountDownLatch latch = new CountDownLatch(1);
    final int[] rcArray = { 0 };
    lh.asyncAddEntry(lac + 1, "".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 - addEntry with EntryID is expected to fail with IllegalOpException, " + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
    }
    // doing addEntry with entryid using regular Ledgerhandle should fail
    latch = new CountDownLatch(1);
    rcArray[0] = 0;
    lh.asyncAddEntry(lac + 1, "".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 - addEntry with EntryID is expected to fail with IllegalOpException," + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
    }
    // doing addEntry with entryid using regular Ledgerhandle should fail
    try {
        lh.addEntry(lac + 1, "".getBytes());
        Assert.fail("Test3 - addEntry with EntryID is expected to fail");
    } catch (BKIllegalOpException E) {
    }
    // doing addEntry with entryid using regular Ledgerhandle should fail
    try {
        lh.addEntry(lac + 1, "".getBytes(), 0, 0);
        Assert.fail("Test4 - addEntry with EntryID is expected to fail");
    } catch (BKIllegalOpException E) {
    }
    lh.close();
}
Also used : AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BKIllegalOpException(org.apache.bookkeeper.client.BKException.BKIllegalOpException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AddCallback (org.apache.bookkeeper.client.AsyncCallback.AddCallback)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 Test (org.junit.Test)14 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)9 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)6 IOException (java.io.IOException)5 ByteBuffer (java.nio.ByteBuffer)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)4 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)3 BKException (org.apache.bookkeeper.client.BKException)2 BKClientClosedException (org.apache.bookkeeper.client.BKException.BKClientClosedException)2 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 BookieException (org.apache.bookkeeper.bookie.BookieException)1