Search in sources :

Example 16 with AddCallback

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

the class LedgerDeleteTest method writeLedgerEntries.

/**
 * Common method to create ledgers and write entries to them.
 */
private LedgerHandle[] writeLedgerEntries(int numLedgers, int msgSize, int numMsgs) throws Exception {
    // Create the ledgers
    LedgerHandle[] lhs = new LedgerHandle[numLedgers];
    for (int i = 0; i < numLedgers; i++) {
        lhs[i] = bkc.createLedger(1, 1, digestType, "".getBytes());
    }
    // 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 * numLedgers);
    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 < numLedgers; j++) {
            lhs[j].asyncAddEntry(msg.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());
    }
    // Return the ledger handles to the inserted ledgers and entries
    return lhs;
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 17 with AddCallback

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

the class BookKeeperCloseTest method createLedgerWithEntries.

private LedgerHandle createLedgerWithEntries(BookKeeper bk, int numOfEntries) throws Exception {
    LedgerHandle lh = bk.createLedger(3, 3, digestType, PASSWORD.getBytes());
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    final CountDownLatch latch = new CountDownLatch(numOfEntries);
    final AddCallback cb = new AddCallback() {

        public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
            rc.compareAndSet(BKException.Code.OK, rccb);
            latch.countDown();
        }
    };
    for (int i = 0; i < numOfEntries; i++) {
        lh.asyncAddEntry("foobar".getBytes(), cb, null);
    }
    if (!latch.await(30, TimeUnit.SECONDS)) {
        throw new Exception("Entries took too long to add");
    }
    if (rc.get() != BKException.Code.OK) {
        throw BKException.create(rc.get());
    }
    return lh;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) BookieException(org.apache.bookkeeper.bookie.BookieException) IOException(java.io.IOException) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException)

Example 18 with AddCallback

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

the class LedgerCloseTest method verifyMetadataConsistency.

private void verifyMetadataConsistency(int numEntries, LedgerHandle lh) throws Exception {
    final CountDownLatch addDoneLatch = new CountDownLatch(1);
    final CountDownLatch deadIOLatch = new CountDownLatch(1);
    final CountDownLatch recoverDoneLatch = new CountDownLatch(1);
    final CountDownLatch failedLatch = new CountDownLatch(1);
    // kill first bookie to replace with a unauthorize bookie
    BookieSocketAddress bookie = lh.getLedgerMetadata().currentEnsemble.get(0);
    ServerConfiguration conf = killBookie(bookie);
    // replace a unauthorize bookie
    startUnauthorizedBookie(conf, addDoneLatch);
    // kill second bookie to replace with a dead bookie
    bookie = lh.getLedgerMetadata().currentEnsemble.get(1);
    conf = killBookie(bookie);
    // replace a slow dead bookie
    startDeadBookie(conf, deadIOLatch);
    // tried to add entries
    for (int i = 0; i < numEntries; i++) {
        lh.asyncAddEntry("data".getBytes(), new AddCallback() {

            @Override
            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
                if (BKException.Code.OK != rc) {
                    failedLatch.countDown();
                    deadIOLatch.countDown();
                }
                if (0 == entryId) {
                    try {
                        recoverDoneLatch.await();
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }, null);
    }
    // add finished
    addDoneLatch.countDown();
    // wait until entries failed due to UnauthorizedAccessException
    failedLatch.await();
    // simulate the ownership of this ledger is transfer to another host
    LOG.info("Recover ledger {}.", lh.getId());
    ClientConfiguration newConf = new ClientConfiguration();
    newConf.addConfiguration(baseClientConf);
    BookKeeper newBkc = new BookKeeperTestClient(newConf.setReadTimeout(1));
    LedgerHandle recoveredLh = newBkc.openLedger(lh.getId(), digestType, "".getBytes());
    LOG.info("Recover ledger {} done.", lh.getId());
    recoverDoneLatch.countDown();
    // wait a bit until add operations failed from second bookie due to IOException
    TimeUnit.SECONDS.sleep(5);
    // open the ledger again to make sure we ge the right last confirmed.
    LedgerHandle newLh = newBkc.openLedger(lh.getId(), digestType, "".getBytes());
    assertEquals("Metadata should be consistent across different opened ledgers", recoveredLh.getLastAddConfirmed(), newLh.getLastAddConfirmed());
}
Also used : ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Example 19 with AddCallback

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

the class LedgerRecoveryTest method testEnsembleChangeDuringRecovery.

/**
 * Verify that it doesn't break the recovery when changing ensemble in
 * recovery add.
 */
@Test
public void testEnsembleChangeDuringRecovery() throws Exception {
    LedgerHandle lh = bkc.createLedger(numBookies, 2, 2, digestType, "".getBytes());
    int numEntries = (numBookies * 3) + 1;
    final AtomicInteger numPendingAdds = new AtomicInteger(numEntries);
    final CountDownLatch addDone = new CountDownLatch(1);
    for (int i = 0; i < numEntries; i++) {
        lh.asyncAddEntry("data".getBytes(), new AddCallback() {

            @Override
            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
                if (BKException.Code.OK != rc) {
                    addDone.countDown();
                    return;
                }
                if (numPendingAdds.decrementAndGet() == 0) {
                    addDone.countDown();
                }
            }
        }, null);
    }
    addDone.await(10, TimeUnit.SECONDS);
    if (numPendingAdds.get() > 0) {
        fail("Failed to add " + numEntries + " to ledger handle " + lh.getId());
    }
    // kill first 2 bookies to replace bookies
    BookieSocketAddress bookie1 = lh.getLedgerMetadata().currentEnsemble.get(0);
    ServerConfiguration conf1 = killBookie(bookie1);
    BookieSocketAddress bookie2 = lh.getLedgerMetadata().currentEnsemble.get(1);
    ServerConfiguration conf2 = killBookie(bookie2);
    // replace these two bookies
    startDeadBookie(conf1);
    startDeadBookie(conf2);
    // kick in two brand new bookies
    startNewBookie();
    startNewBookie();
    // two dead bookies are put in the ensemble which would cause ensemble
    // change
    LedgerHandle recoveredLh = bkc.openLedger(lh.getId(), digestType, "".getBytes());
    assertEquals("Fenced ledger should have correct lastAddConfirmed", lh.getLastAddConfirmed(), recoveredLh.getLastAddConfirmed());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with AddCallback

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

the class BookKeeperTest method testCloseDuringOp.

/**
 * Test that bookkeeper will close cleanly if close is issued
 * while another operation is in progress.
 */
@Test
public void testCloseDuringOp() throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    for (int i = 0; i < 10; i++) {
        final BookKeeper client = new BookKeeper(conf);
        final CountDownLatch l = new CountDownLatch(1);
        final AtomicBoolean success = new AtomicBoolean(false);
        Thread t = new Thread() {

            public void run() {
                try {
                    LedgerHandle lh = client.createLedger(3, 3, digestType, "testPasswd".getBytes());
                    startNewBookie();
                    killBookie(0);
                    lh.asyncAddEntry("test".getBytes(), new AddCallback() {

                        @Override
                        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
                        // noop, we don't care if this completes
                        }
                    }, null);
                    client.close();
                    success.set(true);
                    l.countDown();
                } catch (Exception e) {
                    LOG.error("Error running test", e);
                    success.set(false);
                    l.countDown();
                }
            }
        };
        t.start();
        assertTrue("Close never completed", l.await(10, TimeUnit.SECONDS));
        assertTrue("Close was not successful", success.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) BKBookieHandleNotAvailableException(org.apache.bookkeeper.client.BKException.BKBookieHandleNotAvailableException) IOException(java.io.IOException) 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