use of org.apache.bookkeeper.client.api.WriteHandle in project bookkeeper by apache.
the class SimpleTestCommandTest method testCommand.
public void testCommand(String... args) throws Exception {
WriteHandle wh = mock(WriteHandle.class);
AtomicLong counter = new AtomicLong(0L);
when(wh.append(any(byte[].class))).thenReturn(counter.get());
CreateBuilder createBuilder = mock(CreateBuilder.class);
when(createBuilder.execute()).thenReturn(FutureUtils.value(wh));
when(createBuilder.withEnsembleSize(anyInt())).thenReturn(createBuilder);
when(createBuilder.withWriteQuorumSize(anyInt())).thenReturn(createBuilder);
when(createBuilder.withAckQuorumSize(anyInt())).thenReturn(createBuilder);
when(createBuilder.withDigestType(any(DigestType.class))).thenReturn(createBuilder);
when(createBuilder.withPassword(any(byte[].class))).thenReturn(createBuilder);
when(mockBk.newCreateLedgerOp()).thenReturn(createBuilder);
CommandRunner runner = createCommandRunner(new SimpleTestCommand());
assertTrue(runner.runArgs(args));
// verify create builder
verify(createBuilder, times(1)).withEnsembleSize(eq(5));
verify(createBuilder, times(1)).withWriteQuorumSize(eq(3));
verify(createBuilder, times(1)).withAckQuorumSize(eq(3));
verify(createBuilder, times(1)).withDigestType(eq(DigestType.CRC32C));
verify(createBuilder, times(1)).withPassword(eq(new byte[0]));
verify(createBuilder, times(1)).execute();
// verify appends
verify(wh, times(10)).append(eq(new byte[100]));
}
use of org.apache.bookkeeper.client.api.WriteHandle in project pravega by pravega.
the class BookKeeperLogTests method testReconcileLedgersEmptyLog.
/**
* Tests {@link DebugBookKeeperLogWrapper#reconcileLedgers} with an empty log metadata and various types of candidate ledgers
* that may or may not belong to it.
*/
@Test
public void testReconcileLedgersEmptyLog() throws Exception {
final BookKeeper bk = this.factory.get().getBookKeeperClient();
BookKeeperLog log = (BookKeeperLog) createDurableDataLog();
val wrapper = this.factory.get().createDebugLogWrapper(log.getLogId());
wrapper.disable();
// Empty out the log's metadata.
val emptyMetadata = LogMetadata.builder().enabled(false).epoch(1).truncationAddress(LogMetadata.INITIAL_TRUNCATION_ADDRESS).updateVersion(wrapper.fetchMetadata().getUpdateVersion()).ledgers(Collections.emptyList()).build();
log.overWriteMetadata(emptyMetadata);
// Create a few ledgers. One without an Id, one with a bad id, and one with
WriteHandle ledgerNoLogId = createCustomLedger(null);
val corruptedId = new HashMap<>(Ledgers.createLedgerCustomMetadata(log.getLogId()));
corruptedId.put(Ledgers.PROPERTY_LOG_ID, "abc".getBytes());
WriteHandle ledgerBadLogId = createCustomLedger(corruptedId);
WriteHandle ledgerOtherLogId = createCustomLedger(Ledgers.createLedgerCustomMetadata(log.getLogId() + 1));
WriteHandle ledgerGoodLogId = Ledgers.create(bk, this.config.get(), log.getLogId());
List<WriteHandle> candidateLedgers = Arrays.asList(ledgerGoodLogId, ledgerBadLogId, ledgerNoLogId, ledgerOtherLogId);
for (WriteHandle lh : candidateLedgers) {
lh.append(new byte[100]);
}
// Perform reconciliation.
boolean isChanged = wrapper.reconcileLedgers(candidateLedgers);
Assert.assertTrue("Expected something to change.", isChanged);
// Validate new metadata.
val newMetadata = wrapper.fetchMetadata();
val expectedLedgers = Collections.singletonList(ledgerGoodLogId.getId());
val newLedgers = newMetadata.getLedgers().stream().map(LedgerMetadata::getLedgerId).collect(Collectors.toList());
Assert.assertFalse("Expected metadata to still be disabled.", newMetadata.isEnabled());
Assert.assertEquals("Unexpected epoch.", emptyMetadata.getUpdateVersion(), newMetadata.getEpoch());
Assert.assertEquals("Unexpected update version.", emptyMetadata.getUpdateVersion() + 1, newMetadata.getUpdateVersion());
AssertExtensions.assertListEquals("Unexpected ledger list.", expectedLedgers, newLedgers, Long::equals);
checkLogReadAfterReconciliation(expectedLedgers.size());
}
use of org.apache.bookkeeper.client.api.WriteHandle in project pravega by pravega.
the class BookKeeperAdapter method shutDown.
@Override
protected void shutDown() {
for (WriteHandle lh : this.ledgers.values()) {
try {
lh.close();
} catch (Exception ex) {
System.err.println(ex);
}
}
this.ledgers.clear();
BookKeeperLogFactory lf = this.logFactory;
if (lf != null) {
lf.close();
this.logFactory = null;
}
stopBookKeeper();
CuratorFramework zkClient = this.zkClient;
if (zkClient != null) {
zkClient.close();
this.zkClient = null;
}
Runtime.getRuntime().removeShutdownHook(this.stopBookKeeperProcess);
}
use of org.apache.bookkeeper.client.api.WriteHandle in project pravega by pravega.
the class BookKeeperAdapter method append.
@Override
public CompletableFuture<Void> append(String logName, Event event, Duration timeout) {
ensureRunning();
WriteHandle lh = this.ledgers.getOrDefault(logName, null);
if (lh == null) {
return Futures.failedFuture(new StreamSegmentNotExistsException(logName));
}
ArrayView s = event.getSerialization();
return Futures.toVoid(lh.appendAsync(s.array(), s.arrayOffset(), s.getLength()));
}
use of org.apache.bookkeeper.client.api.WriteHandle in project bookkeeper by apache.
the class TestMaxEnsembleChangeNum method testChangeEnsembleMaxNumWithWriter.
@Test
public void testChangeEnsembleMaxNumWithWriter() throws Exception {
long lId;
int numEntries = 5;
int changeNum = 5;
setBookkeeperConfig(new ClientConfiguration().setDelayEnsembleChange(false).setMaxAllowedEnsembleChanges(5));
try (WriteHandle writer = result(newCreateLedgerOp().withAckQuorumSize(3).withWriteQuorumSize(3).withEnsembleSize(3).withPassword(password).execute())) {
lId = writer.getId();
// first fragment
for (int i = 0; i < numEntries; i++) {
writer.append(ByteBuffer.wrap(data));
}
assertEquals("There should be zero ensemble change", 1, getLedgerMetadata(lId).getEnsembles().size());
simulateEnsembleChangeWithWriter(changeNum, numEntries, writer);
// one more ensemble change
startNewBookie();
killBookie(writer.getLedgerMetadata().getEnsembleAt(writer.getLastAddConfirmed()).get(0));
// add failure
try {
writer.append(ByteBuffer.wrap(data));
fail("should not come to here");
} catch (BKException exception) {
assertEquals(exception.getCode(), WriteException);
}
}
}
Aggregations