Search in sources :

Example 21 with AddCallback

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

the class AuditorLedgerCheckerTest method addEntry.

private void addEntry(int numEntriesToWrite, LedgerHandle lh) throws InterruptedException, BKException {
    final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    for (int i = 0; i < numEntriesToWrite; i++) {
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(Integer.MAX_VALUE));
        entry.position(0);
        lh.asyncAddEntry(entry.array(), 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) ByteBuffer(java.nio.ByteBuffer)

Example 22 with AddCallback

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

the class TestBookieHealthCheck method testBkQuarantine.

@Test
public void testBkQuarantine() throws Exception {
    LedgerHandle lh = bkc.createLedger(2, 2, 2, BookKeeper.DigestType.CRC32, new byte[] {});
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        byte[] msg = ("msg-" + i).getBytes();
        lh.addEntry(msg);
    }
    BookieSocketAddress bookieToQuarantine = lh.getLedgerMetadata().getEnsemble(numEntries).get(0);
    sleepBookie(bookieToQuarantine, baseClientConf.getAddEntryTimeout() * 2).await();
    byte[] tempMsg = "temp-msg".getBytes();
    lh.asyncAddEntry(tempMsg, new AddCallback() {

        @Override
        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
        // no-op
        }
    }, null);
    // make sure the add entry timeouts
    Thread.sleep(baseClientConf.getAddEntryTimeout() * 2 * 1000);
    // make sure the health check runs once after the timeout
    Thread.sleep(baseClientConf.getBookieHealthCheckIntervalSeconds() * 2 * 1000);
    // the bookie watcher should contain the bookieToQuarantine in the quarantine set
    Assert.assertTrue(bkc.bookieWatcher.quarantinedBookies.asMap().containsKey(bookieToQuarantine));
    // the bookie to be left out of the ensemble should always be the quarantined bookie
    LedgerHandle lh1 = bkc.createLedger(2, 2, 2, BookKeeper.DigestType.CRC32, new byte[] {});
    LedgerHandle lh2 = bkc.createLedger(3, 3, 3, BookKeeper.DigestType.CRC32, new byte[] {});
    Assert.assertFalse(lh1.getLedgerMetadata().getEnsemble(0).contains(bookieToQuarantine));
    Assert.assertFalse(lh2.getLedgerMetadata().getEnsemble(0).contains(bookieToQuarantine));
    // the quarantined bookie can still be in the ensemble if we do not have enough healthy bookies
    LedgerHandle lh3 = bkc.createLedger(4, 4, 4, BookKeeper.DigestType.CRC32, new byte[] {});
    Assert.assertTrue(lh3.getLedgerMetadata().getEnsemble(0).contains(bookieToQuarantine));
    // make sure faulty bookie is out of quarantine
    Thread.sleep(baseClientConf.getBookieQuarantineTimeSeconds() * 1000);
    // the bookie should not be quarantined anymore
    Assert.assertFalse(bkc.bookieWatcher.quarantinedBookies.asMap().containsKey(bookieToQuarantine));
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) Test(org.junit.Test)

Example 23 with AddCallback

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

the class UpdateLedgerOpTest method testChangeEnsembleAfterRenaming.

public void testChangeEnsembleAfterRenaming(boolean useShortHostName) throws Exception {
    BookKeeper bk = new BookKeeper(baseClientConf, zkc);
    BookKeeperAdmin bkadmin = new BookKeeperAdmin(bk);
    LOG.info("Create ledger and add entries to it");
    LedgerHandle lh = createLedgerWithEntries(bk, 100);
    BookieServer bookieServer = bs.get(0);
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(0);
    BookieSocketAddress curBookieAddr = null;
    for (BookieSocketAddress bookieSocketAddress : ensemble) {
        if (bookieServer.getLocalAddress().equals(bookieSocketAddress)) {
            curBookieAddr = bookieSocketAddress;
        }
    }
    assertNotNull("Couldn't find the bookie in ledger metadata!", curBookieAddr);
    baseConf.setUseHostNameAsBookieID(true);
    baseConf.setUseShortHostName(useShortHostName);
    BookieSocketAddress toBookieId = Bookie.getBookieAddress(baseConf);
    BookieSocketAddress toBookieAddr = new BookieSocketAddress(toBookieId.getHostName() + ":" + curBookieAddr.getPort());
    UpdateLedgerOp updateLedgerOp = new UpdateLedgerOp(bk, bkadmin);
    updateLedgerOp.updateBookieIdInLedgers(curBookieAddr, toBookieAddr, 5, 100, progressable);
    bookieServer.shutdown();
    ServerConfiguration serverConf1 = newServerConfiguration();
    bsConfs.add(serverConf1);
    bs.add(startBookie(serverConf1));
    // ledger#asyncAddEntry() would hit BadVersion exception as rename incr
    // cversion. But LedgerMetadata#isConflictWith() gracefully handles
    // this conflicts.
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    lh.asyncAddEntry("foobar".getBytes(), new AddCallback() {

        @Override
        public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
            rc.compareAndSet(BKException.Code.OK, rccb);
            latch.countDown();
        }
    }, 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());
    }
    lh.close();
    LedgerHandle openLedger = bk.openLedger(lh.getId(), digestType, PASSWORD.getBytes());
    final LedgerMetadata ledgerMetadata = openLedger.getLedgerMetadata();
    assertEquals("Failed to reform ensemble!", 2, ledgerMetadata.getEnsembles().size());
    ensemble = ledgerMetadata.getEnsemble(0);
    assertTrue("Failed to update the ledger metadata to use bookie host name", ensemble.contains(toBookieAddr));
}
Also used : ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) BookieServer(org.apache.bookkeeper.proto.BookieServer) CountDownLatch(java.util.concurrent.CountDownLatch) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback)

Example 24 with AddCallback

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

the class UpdateLedgerOpTest method testRenameWhenAddEntryInProgress.

/**
 * Tests verifies simultaneous flow between adding entries and rename of
 * bookie id.
 */
@Test
public void testRenameWhenAddEntryInProgress() throws Exception {
    final BookKeeper bk = new BookKeeper(baseClientConf, zkc);
    BookKeeperAdmin bkadmin = new BookKeeperAdmin(bk);
    LOG.info("Create ledger and add entries to it");
    final int numOfEntries = 5000;
    final CountDownLatch latch = new CountDownLatch(numOfEntries);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    final LedgerHandle lh = createLedgerWithEntries(bk, 1);
    latch.countDown();
    Thread th = new Thread() {

        public void run() {
            final AddCallback cb = new AddCallback() {

                public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rccb);
                    if (entryId % 100 == 0) {
                        LOG.info("Added entries till entryId:{}", entryId);
                    }
                    latch.countDown();
                }
            };
            for (int i = 1; i < numOfEntries; i++) {
                lh.asyncAddEntry(("foobar" + i).getBytes(), cb, null);
            }
        }
    };
    th.start();
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(0);
    BookieSocketAddress curBookieAddr = ensemble.get(0);
    BookieSocketAddress toBookieAddr = new BookieSocketAddress("localhost:" + curBookieAddr.getPort());
    UpdateLedgerOp updateLedgerOp = new UpdateLedgerOp(bk, bkadmin);
    updateLedgerOp.updateBookieIdInLedgers(curBookieAddr, toBookieAddr, 5, 100, progressable);
    if (!latch.await(120, TimeUnit.SECONDS)) {
        throw new Exception("Entries took too long to add");
    }
    if (rc.get() != BKException.Code.OK) {
        throw BKException.create(rc.get());
    }
    lh.close();
    LedgerHandle openLedger = bk.openLedger(lh.getId(), digestType, PASSWORD.getBytes());
    ensemble = openLedger.getLedgerMetadata().getEnsemble(0);
    assertTrue("Failed to update the ledger metadata to use bookie host name", ensemble.contains(toBookieAddr));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Test(org.junit.Test)

Example 25 with AddCallback

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

the class UpdateLedgerCmdTest 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" + i).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) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

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