use of org.apache.bookkeeper.client.AsyncCallback.AddCallback in project bookkeeper by apache.
the class LedgerRecoveryTest method batchRecovery.
private void batchRecovery(int batchSize) throws Exception {
ClientConfiguration newConf = new ClientConfiguration().setReadEntryTimeout(60000).setAddEntryTimeout(60000).setRecoveryReadBatchSize(batchSize);
newConf.setZkServers(zkUtil.getZooKeeperConnectString());
BookKeeper newBk = new BookKeeper(newConf);
LedgerHandle lh = newBk.createLedger(numBookies, 2, 2, digestType, "".getBytes());
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
sleepBookie(lh.getLedgerMetadata().currentEnsemble.get(0), latch1);
sleepBookie(lh.getLedgerMetadata().currentEnsemble.get(1), latch2);
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(("" + i).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);
}
latch1.countDown();
latch2.countDown();
addDone.await(10, TimeUnit.SECONDS);
assertEquals(0, numPendingAdds.get());
LedgerHandle recoverLh = newBk.openLedgerNoRecovery(lh.getId(), digestType, "".getBytes());
assertEquals(BookieProtocol.INVALID_ENTRY_ID, recoverLh.getLastAddConfirmed());
final CountDownLatch recoverLatch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
LedgerRecoveryOp recoveryOp = new LedgerRecoveryOp(recoverLh, new BookkeeperInternalCallbacks.GenericCallback<Void>() {
@Override
public void operationComplete(int rc, Void result) {
success.set(BKException.Code.OK == rc);
recoverLatch.countDown();
}
}).parallelRead(true).readBatchSize(newConf.getRecoveryReadBatchSize());
recoveryOp.initiate();
recoverLatch.await(10, TimeUnit.SECONDS);
assertTrue(success.get());
assertEquals(numEntries, recoveryOp.readCount.get());
assertEquals(numEntries, recoveryOp.writeCount.get());
Enumeration<LedgerEntry> enumeration = recoverLh.readEntries(0, numEntries - 1);
int numReads = 0;
while (enumeration.hasMoreElements()) {
LedgerEntry entry = enumeration.nextElement();
assertEquals((long) numReads, entry.getEntryId());
assertEquals(numReads, Integer.parseInt(new String(entry.getEntry())));
++numReads;
}
assertEquals(numEntries, numReads);
newBk.close();
}
use of org.apache.bookkeeper.client.AsyncCallback.AddCallback in project bookkeeper by apache.
the class BookKeeperTest method testAsyncReadWithError.
/**
* Tests that when trying to use a closed BK client object we get
* a callback error and not an InterruptedException.
* @throws Exception
*/
@Test
public void testAsyncReadWithError() throws Exception {
LedgerHandle lh = bkc.createLedger(3, 3, DigestType.CRC32, "testPasswd".getBytes());
bkc.close();
final AtomicInteger result = new AtomicInteger(0);
final CountDownLatch counter = new CountDownLatch(1);
// Try to write, we shoud get and error callback but not an exception
lh.asyncAddEntry("test".getBytes(), new AddCallback() {
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
result.set(rc);
counter.countDown();
}
}, null);
counter.await();
assertTrue(result.get() != 0);
}
use of org.apache.bookkeeper.client.AsyncCallback.AddCallback in project bookkeeper by apache.
the class BookieWriteLedgerTest method testLedgerHandleAdvFunctionality.
/*
* Verify the functionality of Advanced Ledger which accepts ledgerId as
* input and returns LedgerHandleAdv. LedgerHandleAdv takes entryId for
* addEntry, and let user manage entryId allocation.
* This testcase is mainly added for covering missing code coverage branches
* in LedgerHandleAdv
*
* @throws Exception
*/
@Test
public void testLedgerHandleAdvFunctionality() throws Exception {
// Create a ledger
long ledgerId = 0xABCDEF;
lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);
numEntriesToWrite = 3;
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(0, entry.array());
// here asyncAddEntry(final long entryId, final byte[] data, final
// AddCallback cb, final Object ctx) method is
// called which is not covered in any other testcase
entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
CountDownLatch latch = new CountDownLatch(1);
final int[] returnedRC = new int[1];
lh.asyncAddEntry(1, entry.array(), new AddCallback() {
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
returnedRC[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
assertTrue("Returned code is expected to be OK", returnedRC[0] == BKException.Code.OK);
// here addEntry is called with incorrect offset and length
entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
try {
lh.addEntry(2, entry.array(), -3, 9);
fail("AddEntry is called with negative offset and incorrect length," + "so it is expected to throw RuntimeException/IndexOutOfBoundsException");
} catch (RuntimeException exception) {
// expected RuntimeException/IndexOutOfBoundsException
}
// here addEntry is called with corrected offset and length and it is
// supposed to succeed
entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(2, entry.array());
// LedgerHandle is closed for write
lh.close();
// here addEntry is called even after the close of the LedgerHandle, so
// it is expected to throw exception
entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
try {
lh.addEntry(3, entry.array());
fail("AddEntry is called after the close of LedgerHandle," + "so it is expected to throw BKLedgerClosedException");
} catch (BKLedgerClosedException exception) {
}
readEntries(lh, entries1);
bkc.deleteLedger(ledgerId);
}
use of org.apache.bookkeeper.client.AsyncCallback.AddCallback in project bookkeeper by apache.
the class LedgerCmdTest method createLedgerWithEntries.
private LedgerHandle createLedgerWithEntries(BookKeeper bk, int numOfEntries) throws Exception {
LedgerHandle lh = bk.createLedger(1, 1, 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;
}
use of org.apache.bookkeeper.client.AsyncCallback.AddCallback in project bookkeeper by apache.
the class BookKeeperCloseTest method testAddLedgerEntry.
/**
* Test that adding entry to a ledger using bookkeeper client which is
* closed should throw ClientClosedException.
*/
@Test
public void testAddLedgerEntry() throws Exception {
BookKeeper bk = new BookKeeper(baseClientConf, zkc);
LOG.info("Create ledger and add entries to it");
LedgerHandle lh = createLedgerWithEntries(bk, 1);
LOG.info("Closing bookkeeper client");
restartBookieSlow();
bk.close();
try {
lh.addEntry("foobar".getBytes());
fail("should have failed, client is closed");
} catch (BKClientClosedException e) {
// correct
}
final CountDownLatch completeLatch = new CountDownLatch(1);
final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
lh.asyncAddEntry("foobar".getBytes(), new AddCallback() {
public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
rc.set(rccb);
completeLatch.countDown();
}
}, null);
LOG.info("Waiting to finish adding another entry asynchronously");
assertTrue("Add entry to ledger call should have completed", completeLatch.await(20, TimeUnit.SECONDS));
assertEquals("Add entry to ledger should not have succeeded through closed bkclient!", BKException.Code.ClientClosedException, rc.get());
}
Aggregations