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