Search in sources :

Example 1 with WriteHandle

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]));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) WriteHandle(org.apache.bookkeeper.client.api.WriteHandle) CreateBuilder(org.apache.bookkeeper.client.api.CreateBuilder) DigestType(org.apache.bookkeeper.client.api.DigestType)

Example 2 with WriteHandle

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());
}
Also used : lombok.val(lombok.val) WriteHandle(org.apache.bookkeeper.client.api.WriteHandle) HashMap(java.util.HashMap) BookKeeper(org.apache.bookkeeper.client.api.BookKeeper) Test(org.junit.Test)

Example 3 with WriteHandle

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);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) WriteHandle(org.apache.bookkeeper.client.api.WriteHandle) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) BookKeeperLogFactory(io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperLogFactory)

Example 4 with WriteHandle

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()));
}
Also used : WriteHandle(org.apache.bookkeeper.client.api.WriteHandle) ArrayView(io.pravega.common.util.ArrayView) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)

Example 5 with WriteHandle

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);
        }
    }
}
Also used : WriteHandle(org.apache.bookkeeper.client.api.WriteHandle) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Aggregations

WriteHandle (org.apache.bookkeeper.client.api.WriteHandle)9 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)3 lombok.val (lombok.val)3 IOException (java.io.IOException)2 CompletionException (java.util.concurrent.CompletionException)2 Test (org.junit.Test)2 ArrayView (io.pravega.common.util.ArrayView)1 DataLogDisabledException (io.pravega.segmentstore.storage.DataLogDisabledException)1 DurableDataLogException (io.pravega.segmentstore.storage.DurableDataLogException)1 BookKeeperLogFactory (io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperLogFactory)1 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 SneakyThrows (lombok.SneakyThrows)1 BookKeeper (org.apache.bookkeeper.client.api.BookKeeper)1 CreateBuilder (org.apache.bookkeeper.client.api.CreateBuilder)1 DigestType (org.apache.bookkeeper.client.api.DigestType)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1