Search in sources :

Example 11 with AddCallback

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

the class TestMaxSizeWorkersQueue method testAddRejected.

@Test
public void testAddRejected() throws Exception {
    LedgerHandle lh = bkc.createLedger(1, 1, digestType, new byte[0]);
    byte[] content = new byte[100];
    final int n = 1000;
    // Write asynchronously, and expect at least few writes to have failed with NotEnoughBookies,
    // because when we get the TooManyRequestException, the client will try to form a new ensemble and that
    // operation will fail since we only have 1 bookie available
    final CountDownLatch counter = new CountDownLatch(n);
    final AtomicBoolean receivedTooManyRequestsException = new AtomicBoolean();
    // Write few entries
    for (int i = 0; i < n; i++) {
        lh.asyncAddEntry(content, new AddCallback() {

            @Override
            public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
                if (rc == BKException.Code.NotEnoughBookiesException) {
                    receivedTooManyRequestsException.set(true);
                }
                counter.countDown();
            }
        }, null);
    }
    counter.await();
    assertTrue(receivedTooManyRequestsException.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with AddCallback

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

the class TestReadTimeout method testReadTimeout.

@SuppressWarnings("deprecation")
@Test
public void testReadTimeout() throws Exception {
    final AtomicBoolean completed = new AtomicBoolean(false);
    LedgerHandle writelh = bkc.createLedger(3, 3, digestType, "testPasswd".getBytes());
    String tmp = "Foobar";
    final int numEntries = 10;
    for (int i = 0; i < numEntries; i++) {
        writelh.addEntry(tmp.getBytes());
    }
    Set<BookieSocketAddress> beforeSet = new HashSet<BookieSocketAddress>();
    beforeSet.addAll(writelh.getLedgerMetadata().getEnsemble(numEntries));
    final BookieSocketAddress bookieToSleep = writelh.getLedgerMetadata().getEnsemble(numEntries).get(0);
    int sleeptime = baseClientConf.getReadTimeout() * 3;
    CountDownLatch latch = sleepBookie(bookieToSleep, sleeptime);
    latch.await();
    writelh.asyncAddEntry(tmp.getBytes(), new AddCallback() {

        public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
            completed.set(true);
        }
    }, null);
    Thread.sleep((baseClientConf.getReadTimeout() * 3) * 1000);
    Assert.assertTrue("Write request did not finish", completed.get());
    Set<BookieSocketAddress> afterSet = new HashSet<BookieSocketAddress>();
    afterSet.addAll(writelh.getLedgerMetadata().getEnsemble(numEntries + 1));
    beforeSet.removeAll(afterSet);
    Assert.assertTrue("Bookie set should not match", beforeSet.size() != 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with AddCallback

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

the class UpdateLedgerOpTest 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)

Example 14 with AddCallback

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

the class BookieReadWriteTest method writeNEntriesLastWriteSync.

private long writeNEntriesLastWriteSync(LedgerHandle lh, int numToWrite) throws Exception {
    final CountDownLatch completeLatch = new CountDownLatch(numToWrite - 1);
    final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
    ByteBuffer entry = ByteBuffer.allocate(4);
    for (int i = 0; i < numToWrite - 1; i++) {
        entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        entriesSize.add(entry.array().length);
        lh.asyncAddEntry(entry.array(), new AddCallback() {

            public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
                rc.compareAndSet(BKException.Code.OK, rccb);
                completeLatch.countDown();
            }
        }, null);
    }
    completeLatch.await();
    if (rc.get() != BKException.Code.OK) {
        throw BKException.create(rc.get());
    }
    entry = ByteBuffer.allocate(4);
    entry.putInt(rng.nextInt(maxInt));
    entry.position(0);
    entries.add(entry.array());
    entriesSize.add(entry.array().length);
    lh.addEntry(entry.array());
    return lh.getLastAddConfirmed();
}
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 15 with AddCallback

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

the class BookieReadWriteTest method testReadWriteZero.

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

                public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rccb);
                    completeLatch.countDown();
                }
            }, null);
        }
        completeLatch.await();
        if (rc.get() != BKException.Code.OK) {
            throw BKException.create(rc.get());
        }
        /*
             * Write a non-zero entry
             */
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        lh.addEntry(entry.array());
        lh.close();
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        LOG.debug("Number of entries written: " + lh.getLastAddConfirmed());
        assertTrue("Verifying number of entries written", lh.getLastAddConfirmed() == numEntriesToWrite);
        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();
    } 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)

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