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());
}
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);
}
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;
}
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();
}
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");
}
}
Aggregations