use of org.apache.bookkeeper.client.api.WriteAdvHandle in project bookkeeper by apache.
the class BookieWriteLedgerTest method testLedgerCreateAdvWithLedgerIdInLoop.
/**
* In a loop create/write/delete the ledger with same ledgerId through
* the functionality of Advanced Ledger which accepts ledgerId as input.
*
* @throws Exception
*/
@Test
public void testLedgerCreateAdvWithLedgerIdInLoop() throws Exception {
int ledgerCount = 40;
long maxId = 9999999999L;
if (baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
// since LongHierarchicalLedgerManager supports ledgerIds of decimal length upto 19 digits but other
// LedgerManagers only upto 10 decimals
maxId = Long.MAX_VALUE;
}
// generate a stream of ledger ids
rng.longs(ledgerCount, 0, maxId).mapToObj(ledgerId -> {
// create a ledger for each ledger id
LOG.info("Creating adv ledger with id {}", ledgerId);
return bkc.newCreateLedgerOp().withEnsembleSize(1).withWriteQuorumSize(1).withAckQuorumSize(1).withDigestType(org.apache.bookkeeper.client.api.DigestType.CRC32).withPassword(ledgerPassword).makeAdv().withLedgerId(ledgerId).execute().thenApply(writer -> {
// Add entries to ledger when created
LOG.info("Writing stream of {} entries to {}", numEntriesToWrite, ledgerId);
List<ByteBuf> entries = rng.ints(numEntriesToWrite, 0, maxInt).mapToObj(i -> {
ByteBuf entry = Unpooled.buffer(4);
entry.retain();
entry.writeInt(i);
return entry;
}).collect(Collectors.toList());
CompletableFuture<?> lastRequest = null;
int i = 0;
for (ByteBuf entry : entries) {
long entryId = i++;
LOG.info("Writing {}:{} as {}", ledgerId, entryId, entry.slice().readInt());
lastRequest = writer.writeAsync(entryId, entry);
}
lastRequest.join();
return Pair.of(writer, entries);
});
}).parallel().map(// wait for all creations and adds in parallel
CompletableFuture::join).forEach(e -> {
// check that each set of adds succeeded
try {
WriteAdvHandle handle = e.getLeft();
List<ByteBuf> entries = e.getRight();
// Read and verify
LOG.info("Read entries for ledger: {}", handle.getId());
readEntries(handle, entries);
entries.forEach(ByteBuf::release);
handle.close();
bkc.deleteLedger(handle.getId());
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
Assert.fail("Test interrupted");
} catch (Exception ex) {
LOG.info("Readback failed with exception", ex);
Assert.fail("Readback failed " + ex.getMessage());
}
});
}
Aggregations