use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class BookieReadWriteTest method testWriteUsingReadOnlyHandle.
@Test
public void testWriteUsingReadOnlyHandle() throws Exception {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
// addEntry on ReadOnlyHandle should fail
CountDownLatch latch = new CountDownLatch(1);
final int[] rcArray = { 0 };
lhOpen.asyncAddEntry("".getBytes(), new AddCallback() {
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
if (rcArray[0] != BKException.Code.IllegalOpException) {
Assert.fail("Test1 - asyncAddOperation is supposed to be failed, but it got following rc - " + KeeperException.Code.get(rcArray[0]));
}
// addEntry on ReadOnlyHandle should fail
latch = new CountDownLatch(1);
rcArray[0] = 0;
lhOpen.asyncAddEntry("".getBytes(), 0, 0, new AddCallback() {
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
if (rcArray[0] != BKException.Code.IllegalOpException) {
Assert.fail("Test2 - asyncAddOperation is supposed to fail with IllegalOpException, but it got following rc - " + KeeperException.Code.get(rcArray[0]));
}
// close readonlyhandle
latch = new CountDownLatch(1);
rcArray[0] = 0;
lhOpen.asyncClose(new CloseCallback() {
@Override
public void closeComplete(int rc, LedgerHandle lh, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
if (rcArray[0] != KeeperException.Code.OK.intValue()) {
Assert.fail("Test3 - asyncClose failed because of exception - " + KeeperException.Code.get(rcArray[0]));
}
// close of readonlyhandle should not affect the writehandle
writeNEntriesLastWriteSync(lh, 5);
lh.close();
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class BookieReadWriteTest method testReadLastConfirmed.
@Test
public void testReadLastConfirmed() throws Exception {
// Create a ledger and add entries
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
long previousLAC = writeNEntriesLastWriteSync(lh, 5);
// add more entries after opening ReadonlyLedgerHandle
LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
long currentLAC = writeNEntriesLastWriteSync(lh, 5);
// get LAC instance variable of ReadHandle and verify if it is equal to (previousLAC - 1)
long readLAC = lhOpen.getLastAddConfirmed();
Assert.assertEquals("Test1 - For ReadHandle LAC", (previousLAC - 1), readLAC);
// close the write LedgerHandle and sleep for 500 msec to make sure all close watchers are called
lh.close();
Thread.sleep(500);
// now call asyncReadLastConfirmed and verify if it is equal to currentLAC
CountDownLatch latch = new CountDownLatch(1);
final int[] rcArray = { 0 };
final long[] lastConfirmedArray = { 0 };
lhOpen.asyncReadLastConfirmed(new ReadLastConfirmedCallback() {
@Override
public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
lastConfirmedArray[0] = lastConfirmed;
latch.countDown();
}
}, latch);
latch.await();
Assert.assertEquals("Test3 - asyncReadLastConfirmed response", KeeperException.Code.OK.intValue(), rcArray[0]);
Assert.assertEquals("Test3 - ReadLAC", currentLAC, lastConfirmedArray[0]);
// similarly try calling asyncTryReadLastConfirmed and verify if it is equal to currentLAC
latch = new CountDownLatch(1);
rcArray[0] = 0;
lastConfirmedArray[0] = 0;
lhOpen.asyncTryReadLastConfirmed(new ReadLastConfirmedCallback() {
@Override
public void readLastConfirmedComplete(int rc, long lastConfirmed, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
lastConfirmedArray[0] = lastConfirmed;
latch.countDown();
}
}, latch);
latch.await();
Assert.assertEquals("Test4 - asyncTryReadLastConfirmed response", KeeperException.Code.OK.intValue(), rcArray[0]);
Assert.assertEquals("Test4 - ReadLAC", currentLAC, lastConfirmedArray[0]);
// similarly try calling tryReadLastConfirmed and verify if it is equal to currentLAC
long tryReadLAC = lhOpen.tryReadLastConfirmed();
Assert.assertEquals("Test5 - ReadLAC", currentLAC, tryReadLAC);
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class BookieReadWriteTest method testMultiLedger.
@Test
public void testMultiLedger() throws IOException {
try {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
lh2 = bkc.createLedger(digestType, ledgerPassword);
long ledgerId = lh.getId();
long ledgerId2 = lh2.getId();
final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite * 2);
final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
// bkc.initMessageDigest("SHA1");
LOG.info("Ledger ID 1: " + lh.getId() + ", Ledger ID 2: " + lh2.getId());
for (int i = 0; i < numEntriesToWrite; i++) {
lh.asyncAddEntry(new byte[0], new AddCallback() {
public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) {
rc.compareAndSet(BKException.Code.OK, rc2);
completeLatch.countDown();
}
}, null);
lh2.asyncAddEntry(new byte[0], 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());
}
lh.close();
lh2.close();
lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
lh2 = bkc.openLedger(ledgerId2, digestType, ledgerPassword);
LOG.debug("Number of entries written: " + lh.getLastAddConfirmed() + ", " + lh2.getLastAddConfirmed());
assertTrue("Verifying number of entries written lh (" + lh.getLastAddConfirmed() + ")", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
assertTrue("Verifying number of entries written lh2 (" + lh2.getLastAddConfirmed() + ")", lh2.getLastAddConfirmed() == (numEntriesToWrite - 1));
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();
ls = lh2.readEntries(0, numEntriesToWrite - 1);
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);
}
lh2.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");
}
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class BookieReadWriteTest method testLedgerHandle.
@Test
public void testLedgerHandle() throws Exception {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
long lac = writeNEntriesLastWriteSync(lh, 5);
// doing addEntry with entryid using regular Ledgerhandle should fail
CountDownLatch latch = new CountDownLatch(1);
final int[] rcArray = { 0 };
lh.asyncAddEntry(lac + 1, "".getBytes(), new AddCallback() {
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
if (rcArray[0] != BKException.Code.IllegalOpException) {
Assert.fail("Test1 - addEntry with EntryID is expected to fail with IllegalOpException, " + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
}
// doing addEntry with entryid using regular Ledgerhandle should fail
latch = new CountDownLatch(1);
rcArray[0] = 0;
lh.asyncAddEntry(lac + 1, "".getBytes(), 0, 0, new AddCallback() {
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
CountDownLatch latch = (CountDownLatch) ctx;
rcArray[0] = rc;
latch.countDown();
}
}, latch);
latch.await();
if (rcArray[0] != BKException.Code.IllegalOpException) {
Assert.fail("Test2 - addEntry with EntryID is expected to fail with IllegalOpException," + "but it got following rc - " + KeeperException.Code.get(rcArray[0]));
}
// doing addEntry with entryid using regular Ledgerhandle should fail
try {
lh.addEntry(lac + 1, "".getBytes());
Assert.fail("Test3 - addEntry with EntryID is expected to fail");
} catch (BKIllegalOpException E) {
}
// doing addEntry with entryid using regular Ledgerhandle should fail
try {
lh.addEntry(lac + 1, "".getBytes(), 0, 0);
Assert.fail("Test4 - addEntry with EntryID is expected to fail");
} catch (BKIllegalOpException E) {
}
lh.close();
}
use of org.apache.bookkeeper.client.LedgerHandle in project bookkeeper by apache.
the class CloseTest method testCloseByOthers.
@Test
public void testCloseByOthers() throws Exception {
int numLedgers = 1;
int numMsgs = 10;
LedgerHandle lh = bkc.createLedger(digestType, "".getBytes());
String tmp = "BookKeeper is cool!";
/*
* Write 10 entries to lh.
*/
for (int i = 0; i < numMsgs; i++) {
lh.addEntry(tmp.getBytes());
}
// other one close the entries
LedgerHandle lh2 = bkc.openLedger(lh.getId(), digestType, "".getBytes());
// so the ledger would be closed, the metadata is changed
// the original ledger handle should be able to close it successfully
lh2.close();
}
Aggregations