Search in sources :

Example 71 with BookKeeper

use of org.apache.bookkeeper.client.BookKeeper in project incubator-pulsar by apache.

the class ManagedLedgerOfflineBacklog method readLedgerMeta.

private void readLedgerMeta(final ManagedLedgerFactoryImpl factory, final TopicName topicName, final NavigableMap<Long, MLDataFormats.ManagedLedgerInfo.LedgerInfo> ledgers) throws Exception {
    String managedLedgerName = topicName.getPersistenceNamingEncoding();
    MetaStore store = factory.getMetaStore();
    BookKeeper bk = factory.getBookKeeper();
    final CountDownLatch mlMetaCounter = new CountDownLatch(1);
    store.getManagedLedgerInfo(managedLedgerName, new MetaStore.MetaStoreCallback<MLDataFormats.ManagedLedgerInfo>() {

        @Override
        public void operationComplete(MLDataFormats.ManagedLedgerInfo mlInfo, MetaStore.Stat version) {
            for (MLDataFormats.ManagedLedgerInfo.LedgerInfo ls : mlInfo.getLedgerInfoList()) {
                ledgers.put(ls.getLedgerId(), ls);
            }
            // find no of entries in last ledger
            if (ledgers.size() > 0) {
                final long id = ledgers.lastKey();
                AsyncCallback.OpenCallback opencb = (rc, lh, ctx1) -> {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] Opened ledger {}: ", managedLedgerName, id, BKException.getMessage(rc));
                    }
                    if (rc == BKException.Code.OK) {
                        MLDataFormats.ManagedLedgerInfo.LedgerInfo info = MLDataFormats.ManagedLedgerInfo.LedgerInfo.newBuilder().setLedgerId(id).setEntries(lh.getLastAddConfirmed() + 1).setSize(lh.getLength()).setTimestamp(System.currentTimeMillis()).build();
                        ledgers.put(id, info);
                        mlMetaCounter.countDown();
                    } else if (rc == BKException.Code.NoSuchLedgerExistsException) {
                        log.warn("[{}] Ledger not found: {}", managedLedgerName, ledgers.lastKey());
                        ledgers.remove(ledgers.lastKey());
                        mlMetaCounter.countDown();
                    } else {
                        log.error("[{}] Failed to open ledger {}: {}", managedLedgerName, id, BKException.getMessage(rc));
                        mlMetaCounter.countDown();
                    }
                };
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Opening ledger {}", managedLedgerName, id);
                }
                try {
                    bk.asyncOpenLedgerNoRecovery(id, digestType, password, opencb, null);
                } catch (Exception e) {
                    log.warn("[{}] Failed to open ledger {}: {}", managedLedgerName, id, e);
                    mlMetaCounter.countDown();
                }
            } else {
                log.warn("[{}] Ledger list empty", managedLedgerName);
                mlMetaCounter.countDown();
            }
        }

        @Override
        public void operationFailed(ManagedLedgerException.MetaStoreException e) {
            log.warn("[{}] Unable to obtain managed ledger metadata - {}", e);
            mlMetaCounter.countDown();
        }
    });
    if (accurate) {
        // block until however long it takes for operation to complete
        mlMetaCounter.await();
    } else {
        mlMetaCounter.await(META_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : BookKeeper(org.apache.bookkeeper.client.BookKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MLDataFormats(org.apache.bookkeeper.mledger.proto.MLDataFormats)

Example 72 with BookKeeper

use of org.apache.bookkeeper.client.BookKeeper in project incubator-pulsar by apache.

the class CompactedTopicTest method testCleanupOldCompactedTopicLedger.

@Test
public void testCleanupOldCompactedTopicLedger() throws Exception {
    BookKeeper bk = pulsar.getBookKeeperClientFactory().create(this.conf, null);
    LedgerHandle oldCompactedLedger = bk.createLedger(1, 1, Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    oldCompactedLedger.close();
    LedgerHandle newCompactedLedger = bk.createLedger(1, 1, Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    newCompactedLedger.close();
    // set the compacted topic ledger
    CompactedTopicImpl compactedTopic = new CompactedTopicImpl(bk);
    compactedTopic.newCompactedLedger(new PositionImpl(1, 2), oldCompactedLedger.getId()).get();
    // ensure both ledgers still exist, can be opened
    bk.openLedger(oldCompactedLedger.getId(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD).close();
    bk.openLedger(newCompactedLedger.getId(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD).close();
    // update the compacted topic ledger
    compactedTopic.newCompactedLedger(new PositionImpl(1, 2), newCompactedLedger.getId()).get();
    // old ledger should be deleted, new still there
    try {
        bk.openLedger(oldCompactedLedger.getId(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD).close();
        Assert.fail("Should have failed to open old ledger");
    } catch (BKException.BKNoSuchLedgerExistsException e) {
    // correct, expected behaviour
    }
    bk.openLedger(newCompactedLedger.getId(), Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD).close();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 73 with BookKeeper

use of org.apache.bookkeeper.client.BookKeeper in project incubator-pulsar by apache.

the class CompactorTest method compactAndVerify.

private List<String> compactAndVerify(String topic, Map<String, byte[]> expected) throws Exception {
    BookKeeper bk = pulsar.getBookKeeperClientFactory().create(this.conf, null);
    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    long compactedLedgerId = compactor.compact(topic).get();
    LedgerHandle ledger = bk.openLedger(compactedLedgerId, Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE, Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD);
    // 0..lac
    Assert.assertEquals(// 0..lac
    ledger.getLastAddConfirmed() + 1, expected.size(), "Should have as many entries as there is keys");
    List<String> keys = new ArrayList<>();
    Enumeration<LedgerEntry> entries = ledger.readEntries(0, ledger.getLastAddConfirmed());
    while (entries.hasMoreElements()) {
        ByteBuf buf = entries.nextElement().getEntryBuffer();
        RawMessage m = RawMessageImpl.deserializeFrom(buf);
        String key = extractKey(m);
        keys.add(key);
        ByteBuf payload = extractPayload(m);
        byte[] bytes = new byte[payload.readableBytes()];
        payload.readBytes(bytes);
        Assert.assertEquals(bytes, expected.remove(key), "Compacted version should match expected version");
        m.close();
    }
    Assert.assertTrue(expected.isEmpty(), "All expected keys should have been found");
    return keys;
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ArrayList(java.util.ArrayList) BookKeeper(org.apache.bookkeeper.client.BookKeeper) ByteBuf(io.netty.buffer.ByteBuf) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) RawMessage(org.apache.pulsar.client.api.RawMessage)

Example 74 with BookKeeper

use of org.apache.bookkeeper.client.BookKeeper in project incubator-pulsar by apache.

the class TwoPhaseCompactor method phaseTwoSeekThenLoop.

private CompletableFuture<Long> phaseTwoSeekThenLoop(RawReader reader, MessageId from, MessageId to, Map<String, MessageId> latestForKey, BookKeeper bk, LedgerHandle ledger) {
    CompletableFuture<Long> promise = new CompletableFuture<>();
    reader.seekAsync(from).thenCompose((v) -> {
        Semaphore outstanding = new Semaphore(MAX_OUTSTANDING);
        CompletableFuture<Void> loopPromise = new CompletableFuture<Void>();
        phaseTwoLoop(reader, to, latestForKey, ledger, outstanding, loopPromise);
        return loopPromise;
    }).thenCompose((v) -> closeLedger(ledger)).thenCompose((v) -> reader.acknowledgeCumulativeAsync(to, ImmutableMap.of(COMPACTED_TOPIC_LEDGER_PROPERTY, ledger.getId()))).whenComplete((res, exception) -> {
        if (exception != null) {
            deleteLedger(bk, ledger).whenComplete((res2, exception2) -> {
                if (exception2 != null) {
                    log.warn("Cleanup of ledger {} for failed", ledger, exception2);
                }
                // complete with original exception
                promise.completeExceptionally(exception);
            });
        } else {
            promise.complete(ledger.getId());
        }
    });
    return promise;
}
Also used : RawBatchConverter(org.apache.pulsar.client.impl.RawBatchConverter) RawMessage(org.apache.pulsar.client.api.RawMessage) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RawReader(org.apache.pulsar.client.api.RawReader) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) PulsarClient(org.apache.pulsar.client.api.PulsarClient) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Commands(org.apache.pulsar.common.api.Commands) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Semaphore(java.util.concurrent.Semaphore) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) IOException(java.io.IOException) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MessageId(org.apache.pulsar.client.api.MessageId) Optional(java.util.Optional) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) Semaphore(java.util.concurrent.Semaphore)

Example 75 with BookKeeper

use of org.apache.bookkeeper.client.BookKeeper in project herddb by diennea.

the class LedgerClosedTest method uselessLedgerDroppedError.

@Test
public void uselessLedgerDroppedError() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    try (Server server = new Server(serverconfig_1)) {
        server.start();
        server.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        server.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        BookkeeperCommitLog log = (BookkeeperCommitLog) tableSpaceManager.getLog();
        long ledgerId = log.getLastSequenceNumber().ledgerId;
        assertTrue(ledgerId >= 0);
    }
    // restart
    try (Server server = new Server(serverconfig_1)) {
        server.start();
        server.waitForStandaloneBoot();
    }
    // restart
    LedgersInfo actualLedgersList;
    try (Server server = new Server(serverconfig_1)) {
        server.start();
        server.waitForStandaloneBoot();
        TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        BookkeeperCommitLog log = (BookkeeperCommitLog) tableSpaceManager.getLog();
        actualLedgersList = log.getActualLedgersList();
        assertEquals(3, actualLedgersList.getActiveLedgers().size());
        System.out.println("actualLedgersList: " + actualLedgersList);
        long lastLedgerId = log.getLastLedgerId();
        assertEquals(lastLedgerId, actualLedgersList.getActiveLedgers().get(actualLedgersList.getActiveLedgers().size() - 1).longValue());
        try (BookKeeper bk = createBookKeeper()) {
            long ledgerIdToDrop = actualLedgersList.getActiveLedgers().get(0);
            System.out.println("dropping " + ledgerIdToDrop);
            bk.newDeleteLedgerOp().withLedgerId(ledgerIdToDrop).execute().get();
        }
    }
    // the server should boot even if the ledger does not exist anymore
    try (Server server = new Server(serverconfig_1)) {
        server.start();
        server.waitForStandaloneBoot();
    }
}
Also used : LedgersInfo(herddb.cluster.LedgersInfo) Table(herddb.model.Table) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) TableSpaceManager(herddb.core.TableSpaceManager) BookKeeper(org.apache.bookkeeper.client.BookKeeper) InsertStatement(herddb.model.commands.InsertStatement) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Aggregations

BookKeeper (org.apache.bookkeeper.client.BookKeeper)76 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)48 Test (org.junit.Test)25 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)24 BKException (org.apache.bookkeeper.client.BKException)18 IOException (java.io.IOException)17 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)12 List (java.util.List)11 CompletableFuture (java.util.concurrent.CompletableFuture)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)10 ArrayList (java.util.ArrayList)9 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 AsyncCallback (org.apache.bookkeeper.client.AsyncCallback)7 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)7 BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)6 TableSpaceManager (herddb.core.TableSpaceManager)6 DataScanner (herddb.model.DataScanner)6 StatementExecutionException (herddb.model.StatementExecutionException)6