use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class BookieReadWriteTest method testReadWriteAsyncSingleClient.
private void testReadWriteAsyncSingleClient(int numEntries) throws IOException {
SyncObj sync = new SyncObj();
try {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
for (int i = 0; i < numEntriesToWrite; i++) {
ByteBuffer 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(), this, sync);
}
// wait for all entries to be acknowledged
synchronized (sync) {
while (sync.counter < numEntriesToWrite) {
LOG.debug("Entries counter = " + sync.counter);
sync.wait();
}
assertEquals("Error adding", BKException.Code.OK, sync.getReturnCode());
}
LOG.debug("*** WRITE COMPLETE ***");
// close ledger
lh.close();
// *** WRITING PART COMPLETE // READ PART BEGINS ***
// open ledger
lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
LOG.debug("Number of entries written: " + (lh.getLastAddConfirmed() + 1));
assertTrue("Verifying number of entries written", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
// read entries
lh.asyncReadEntries(0, numEntriesToWrite - 1, this, sync);
synchronized (sync) {
while (!sync.value) {
sync.wait();
}
assertEquals("Error reading", BKException.Code.OK, sync.getReturnCode());
}
LOG.debug("*** READ COMPLETE ***");
// at this point, Enumeration<LedgerEntry> ls is filled with the returned
// values
int i = 0;
Enumeration<LedgerEntry> ls = sync.getLedgerEntries();
while (ls.hasMoreElements()) {
ByteBuffer origbb = ByteBuffer.wrap(entries.get(i));
Integer origEntry = origbb.getInt();
byte[] entry = ls.nextElement().getEntry();
ByteBuffer result = ByteBuffer.wrap(entry);
LOG.debug("Length of result: " + result.capacity());
LOG.debug("Original entry: " + origEntry);
Integer retrEntry = result.getInt();
LOG.debug("Retrieved entry: " + retrEntry);
assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
assertTrue("Checking entry " + i + " for size", entry.length == entriesSize.get(i).intValue());
i++;
}
assertTrue("Checking number of read entries", i == numEntriesToWrite);
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");
}
}
use of org.apache.bookkeeper.client.LedgerEntry 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");
}
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class BookieReadWriteTest method testReadFromOpenLedger.
@Test
public void testReadFromOpenLedger() throws Exception {
try {
// Create a ledger
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
LOG.info("Ledger ID: " + lh.getId());
long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
// no recovery opened ledger 's last confirmed entry id is less than written
// and it just can read until (i-1)
long toRead = lac - 1;
Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead, toRead);
assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements());
LedgerEntry e = readEntry.nextElement();
assertEquals(toRead, e.getEntryId());
assertArrayEquals(entries.get((int) toRead), e.getEntry());
// should not written to a read only ledger
try {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
lhOpen.addEntry(entry.array());
fail("Should have thrown an exception here");
} catch (BKException.BKIllegalOpException bkioe) {
// this is the correct response
} catch (Exception ex) {
LOG.error("Unexpected exception", ex);
fail("Unexpected exception");
}
// close read only ledger should not change metadata
lhOpen.close();
lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
assertEquals("Last confirmed add: ", lac, (numEntriesToWrite * 2) - 1);
LOG.debug("*** WRITE COMPLETE ***");
// close ledger
lh.close();
/*
* Asynchronous call to read last confirmed entry
*/
lh = bkc.createLedger(digestType, ledgerPassword);
// bkc.initMessageDigest("SHA1");
ledgerId = lh.getId();
writeNEntriesLastWriteSync(lh, numEntriesToWrite);
SyncObj sync = new SyncObj();
lh.asyncReadLastConfirmed(this, sync);
// Wait for for last confirmed
synchronized (sync) {
while (sync.lastConfirmed == -1) {
LOG.debug("Counter = " + sync.lastConfirmed);
sync.wait();
}
assertEquals("Error reading", BKException.Code.OK, sync.getReturnCode());
}
assertEquals("Last confirmed add", sync.lastConfirmed, (numEntriesToWrite - 2));
LOG.debug("*** WRITE COMPLETE ***");
// close ledger
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");
}
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class TestLedgerAllocator method testSuccessAllocatorShouldDeleteUnusedledger.
@Test(timeout = 60000)
public void testSuccessAllocatorShouldDeleteUnusedledger() throws Exception {
String allocationPath = "/allocation-delete-unused-ledger";
zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Stat stat = new Stat();
byte[] data = zkc.get().getData(allocationPath, false, stat);
Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator1.allocate();
// wait until allocated
ZKTransaction txn1 = newTxn();
LedgerHandle lh1 = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
// Second allocator kicks in
stat = new Stat();
data = zkc.get().getData(allocationPath, false, stat);
allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator2.allocate();
// wait until allocated
ZKTransaction txn2 = newTxn();
LedgerHandle lh2 = Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
// should fail to commit txn1 as version is changed by second allocator
try {
Utils.ioResult(txn1.execute());
fail("Should fail commit obtaining ledger handle from first allocator" + " as allocator is modified by second allocator.");
} catch (ZKException ke) {
// as expected
}
Utils.ioResult(txn2.execute());
Utils.close(allocator1);
Utils.close(allocator2);
// ledger handle should be deleted
try {
lh1.close();
fail("LedgerHandle allocated by allocator1 should be deleted.");
} catch (BKException bke) {
// as expected
}
try {
bkc.get().openLedger(lh1.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
fail("LedgerHandle allocated by allocator1 should be deleted.");
} catch (BKException.BKNoSuchLedgerExistsException nslee) {
// as expected
}
long eid = lh2.addEntry("hello world".getBytes());
lh2.close();
LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
int i = 0;
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("hello world", new String(entry.getEntry(), UTF_8));
++i;
}
assertEquals(1, i);
}
use of org.apache.bookkeeper.client.LedgerEntry in project bookkeeper by apache.
the class TestLedgerAllocator method testBadVersionOnTwoAllocators.
@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
String allocationPath = "/allocation-bad-version";
zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Stat stat = new Stat();
byte[] data = zkc.get().getData(allocationPath, false, stat);
Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
allocator1.allocate();
// wait until allocated
ZKTransaction txn1 = newTxn();
LedgerHandle lh = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
allocator2.allocate();
ZKTransaction txn2 = newTxn();
try {
Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
} catch (ZKException ke) {
assertEquals(KeeperException.Code.BADVERSION, ke.getKeeperExceptionCode());
}
Utils.ioResult(txn1.execute());
Utils.close(allocator1);
Utils.close(allocator2);
long eid = lh.addEntry("hello world".getBytes());
lh.close();
LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
int i = 0;
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
assertEquals("hello world", new String(entry.getEntry(), UTF_8));
++i;
}
assertEquals(1, i);
}
Aggregations