org.apache.bookkeeper.client.BookKeeper Java Examples

The following examples show how to use org.apache.bookkeeper.client.BookKeeper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ManagedLedgerTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagedLedgerWithCreateLedgerTimeOut() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMetadataOperationsTimeoutSeconds(3);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("timeout_ledger_test", config);

    BookKeeper bk = mock(BookKeeper.class);
    doNothing().when(bk).asyncCreateLedger(anyInt(), anyInt(), anyInt(), any(), any(), any(), any(), any());
    AtomicInteger response = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Object> ctxHolder = new AtomicReference<>();
    ledger.asyncCreateLedger(bk, config, null, (rc, lh, ctx) -> {
        response.set(rc);
        latch.countDown();
        ctxHolder.set(ctx);
    }, Collections.emptyMap());

    latch.await(config.getMetadataOperationsTimeoutSeconds() + 2, TimeUnit.SECONDS);
    assertEquals(response.get(), BKException.Code.TimeoutException);
    assertTrue(ctxHolder.get() instanceof AtomicBoolean);
    AtomicBoolean ledgerCreated = (AtomicBoolean) ctxHolder.get();
    assertFalse(ledgerCreated.get());

    ledger.close();
}
 
Example #2
Source File: BookkeeperSchemaStorage.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@NotNull
private CompletableFuture<LedgerHandle> createLedger(String schemaId) {
    Map<String, byte[]> metadata = LedgerMetadataUtils.buildMetadataForSchema(schemaId);
    final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
    try {
        bookKeeper.asyncCreateLedger(
                config.getManagedLedgerDefaultEnsembleSize(),
                config.getManagedLedgerDefaultWriteQuorum(),
                config.getManagedLedgerDefaultAckQuorum(),
                BookKeeper.DigestType.fromApiDigestType(config.getManagedLedgerDigestType()),
                LedgerPassword,
                (rc, handle, ctx) -> {
                    if (rc != BKException.Code.OK) {
                        future.completeExceptionally(bkException("Failed to create ledger", rc, -1, -1));
                    } else {
                        future.complete(handle);
                    }
                }, null, metadata);
    } catch (Throwable t) {
        log.error("[{}] Encountered unexpected error when creating schema ledger", schemaId, t);
        return FutureUtil.failedFuture(t);
    }
    return future;
}
 
Example #3
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testCloseAllocatorAfterAbort() throws Exception {
    String allocationPath = "/allocation3";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
    txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1), null));
    try {
        Utils.ioResult(txn.execute());
        fail("Should fail the transaction when setting unexisted path");
    } catch (ZKException ke) {
        // expected
    }
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted.
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes(UTF_8));
}
 
Example #4
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public Future<LedgerHandle> createLedger(int ensembleSize,
                                         int writeQuorumSize,
                                         int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<LedgerHandle> promise = new Promise<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize,
            BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {
                @Override
                public void createComplete(int rc, LedgerHandle lh, Object ctx) {
                    if (BKException.Code.OK == rc) {
                        promise.updateIfEmpty(new Return<LedgerHandle>(lh));
                    } else {
                        promise.updateIfEmpty(new Throw<LedgerHandle>(BKException.create(rc)));
                    }
                }
            }, null);
    return promise;
}
 
Example #5
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<LedgerHandle> createLedger(int ensembleSize,
                                                    int writeQuorumSize,
                                                    int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return FutureUtils.exception(ioe);
    }
    final CompletableFuture<LedgerHandle> promise = new CompletableFuture<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize,
            BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {
                @Override
                public void createComplete(int rc, LedgerHandle lh, Object ctx) {
                    if (BKException.Code.OK == rc) {
                        promise.complete(lh);
                    } else {
                        promise.completeExceptionally(BKException.create(rc));
                    }
                }
            }, null, Collections.emptyMap());
    return promise;
}
 
Example #6
Source File: TestBKLogSegmentWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private BKLogSegmentWriter createLogSegmentWriter(DistributedLogConfiguration conf,
                                                  long logSegmentSequenceNumber,
                                                  long startTxId,
                                                  ZKDistributedLock lock) throws Exception {
    LedgerHandle lh = bkc.get().createLedger(3, 2, 2,
            BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    return new BKLogSegmentWriter(
            runtime.getMethodName(),
            runtime.getMethodName(),
            conf,
            LogSegmentMetadata.LEDGER_METADATA_CURRENT_LAYOUT_VERSION,
            new BKLogSegmentEntryWriter(lh),
            lock,
            startTxId,
            logSegmentSequenceNumber,
            scheduler,
            NullStatsLogger.INSTANCE,
            NullStatsLogger.INSTANCE,
            new AlertStatsLogger(NullStatsLogger.INSTANCE, "test"),
            PermitLimiter.NULL_PERMIT_LIMITER,
            new SettableFeatureProvider("", 0),
            ConfUtils.getConstDynConf(conf));
}
 
Example #7
Source File: TwoPhaseCompactor.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<LedgerHandle> createLedger(BookKeeper bk, Map<String,byte[]> metadata) {
    CompletableFuture<LedgerHandle> bkf = new CompletableFuture<>();

    try {
        bk.asyncCreateLedger(conf.getManagedLedgerDefaultEnsembleSize(),
                conf.getManagedLedgerDefaultWriteQuorum(),
                conf.getManagedLedgerDefaultAckQuorum(),
                Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE,
                Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD,
                (rc, ledger, ctx) -> {
                    if (rc != BKException.Code.OK) {
                        bkf.completeExceptionally(BKException.create(rc));
                    } else {
                        bkf.complete(ledger);
                    }
                }, null, metadata);
    } catch (Throwable t) {
        log.error("Encountered unexpected error when creating compaction ledger", t);
        return FutureUtil.failedFuture(t);
    }
    return bkf;
}
 
Example #8
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testCloseAllocatorAfterAbort() throws Exception {
    String allocationPath = "/allocation3";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    txn.addOp(DefaultZKOp.of(Op.setData("/unexistedpath", "data".getBytes(UTF_8), -1)));
    try {
        FutureUtils.result(txn.execute());
        fail("Should fail the transaction when setting unexisted path");
    } catch (ZKException ke) {
        // expected
    }
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted.
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes(UTF_8));
}
 
Example #9
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * {@link https://issues.apache.org/jira/browse/DL-26}
 */
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
public void testCloseAllocatorAfterConfirm() throws Exception {
    String allocationPath = "/allocation2";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    FutureUtils.result(txn.execute());
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals(0, data.length);
    // the ledger is not deleted.
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes(UTF_8));
}
 
Example #10
Source File: TestBKLogSegmentWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private BKLogSegmentWriter createLogSegmentWriter(DistributedLogConfiguration conf,
                                                  long logSegmentSequenceNumber,
                                                  long startTxId,
                                                  ZKDistributedLock lock) throws Exception {
    LedgerHandle lh = bkc.get().createLedger(3, 2, 2,
            BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    return new BKLogSegmentWriter(
            runtime.getMethodName(),
            runtime.getMethodName(),
            conf,
            LogSegmentMetadata.LEDGER_METADATA_CURRENT_LAYOUT_VERSION,
            new BKLogSegmentEntryWriter(lh),
            lock,
            startTxId,
            logSegmentSequenceNumber,
            scheduler,
            NullStatsLogger.INSTANCE,
            NullStatsLogger.INSTANCE,
            new AlertStatsLogger(NullStatsLogger.INSTANCE, "test"),
            PermitLimiter.NULL_PERMIT_LIMITER,
            new SettableFeatureProvider("", 0),
            ConfUtils.getConstDynConf(conf));
}
 
Example #11
Source File: BookKeeperClientFactoryImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient,
                         Optional<Class<? extends EnsemblePlacementPolicy>> ensemblePlacementPolicyClass,
                         Map<String, Object> properties, StatsLogger statsLogger) throws IOException {
    ClientConfiguration bkConf = createBkClientConfiguration(conf);
    if (properties != null) {
        properties.forEach((key, value) -> bkConf.setProperty(key, value));
    }
    if (ensemblePlacementPolicyClass.isPresent()) {
        setEnsemblePlacementPolicy(bkConf, conf, zkClient, ensemblePlacementPolicyClass.get());
    } else {
        setDefaultEnsemblePlacementPolicy(rackawarePolicyZkCache, clientIsolationZkCache, bkConf, conf, zkClient);
    }
    try {
        return BookKeeper.forConfig(bkConf)
                .allocator(PulsarByteBufAllocator.DEFAULT)
                .statsLogger(statsLogger)
                .build();
    } catch (InterruptedException | BKException e) {
        throw new IOException(e);
    }
}
 
Example #12
Source File: TestLedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000, expected = BKException.ZKException.class)
public void testOpenLedgerWhenZkClosed() throws Exception {
    ZooKeeperClient newZkc = TestZooKeeperClientBuilder.newBuilder()
            .name("zkc-openledger-when-zk-closed")
            .zkServers(zkServers)
            .build();
    BookKeeperClient newBkc = BookKeeperClientBuilder.newBuilder()
            .name("bkc-openledger-when-zk-closed")
            .zkc(newZkc)
            .ledgersPath(ledgersPath)
            .dlConfig(conf)
            .build();
    try {
        LedgerHandle lh = newBkc.get().createLedger(BookKeeper.DigestType.CRC32, "zkcClosed".getBytes(UTF_8));
        lh.close();
        newZkc.close();
        LedgerHandleCache cache =
                LedgerHandleCache.newBuilder().bkc(newBkc).conf(conf).build();
        // open ledger after zkc closed
        cache.openLedger(new LogSegmentMetadata.LogSegmentMetadataBuilder("",
                2, lh.getId(), 1).setLogSegmentSequenceNo(lh.getId()).build(), false);
    } finally {
        newBkc.close();
    }
}
 
Example #13
Source File: BookkeeperSchemaStorage.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@NotNull
private CompletableFuture<LedgerHandle> openLedger(Long ledgerId) {
    final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
    bookKeeper.asyncOpenLedger(
        ledgerId,
        BookKeeper.DigestType.fromApiDigestType(config.getManagedLedgerDigestType()),
        LedgerPassword,
        (rc, handle, ctx) -> {
            if (rc != BKException.Code.OK) {
                future.completeExceptionally(bkException("Failed to open ledger", rc, ledgerId, -1));
            } else {
                future.complete(handle);
            }
        }, null
    );
    return future;
}
 
Example #14
Source File: TestLedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testOpenAndCloseLedger() throws Exception {
    LedgerHandle lh = bkc.get().createLedger(1, 1, 1,
            BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    LedgerHandleCache cache =
            LedgerHandleCache.newBuilder().bkc(bkc).conf(conf).build();
    LogSegmentMetadata segment = new LogSegmentMetadata.LogSegmentMetadataBuilder(
            "/data", LogSegmentMetadata.LogSegmentMetadataVersion.VERSION_V5_SEQUENCE_ID, lh.getId(), 0L)
            .build();
    LedgerDescriptor desc1 = cache.openLedger(segment, false);
    assertTrue(cache.handlesMap.containsKey(desc1));
    LedgerHandleCache.RefCountedLedgerHandle refLh = cache.handlesMap.get(desc1);
    assertEquals(1, refLh.getRefCount());
    cache.openLedger(segment, false);
    assertTrue(cache.handlesMap.containsKey(desc1));
    assertEquals(2, refLh.getRefCount());
    // close the ledger
    cache.closeLedger(desc1);
    assertTrue(cache.handlesMap.containsKey(desc1));
    assertEquals(1, refLh.getRefCount());
    cache.closeLedger(desc1);
    assertFalse(cache.handlesMap.containsKey(desc1));
    assertEquals(0, refLh.getRefCount());
}
 
Example #15
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private Map<BookieSocketAddress, Integer> getBookieStats(LogSegmentMetadata segment) throws Exception {
    Map<BookieSocketAddress, Integer> stats = new HashMap<BookieSocketAddress, Integer>();
    LedgerHandle lh = bkc.client().get().openLedgerNoRecovery(segment.getLogSegmentId(),
            BookKeeper.DigestType.CRC32, getConf().getBKDigestPW().getBytes(UTF_8));
    long eidFirst = 0;
    for (SortedMap.Entry<Long, ArrayList<BookieSocketAddress>>
            entry : LedgerReader.bookiesForLedger(lh).entrySet()) {
        long eidLast = entry.getKey().longValue();
        long count = eidLast - eidFirst + 1;
        for (BookieSocketAddress bookie : entry.getValue()) {
            merge(stats, bookie, (int) count);
        }
        eidFirst = eidLast;
    }
    return stats;
}
 
Example #16
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testCloseAllocatorDuringObtaining() throws Exception {
    String allocationPath = "/allocation2";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = Utils.ioResult(allocator.tryObtain(txn, NULL_LISTENER));
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes(UTF_8));
}
 
Example #17
Source File: Compactor.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public Compactor(ServiceConfiguration conf,
                 PulsarClient pulsar,
                 BookKeeper bk,
                 ScheduledExecutorService scheduler) {
    this.conf = conf;
    this.scheduler = scheduler;
    this.pulsar = pulsar;
    this.bk = bk;
}
 
Example #18
Source File: KopProtocolHandlerTestBase.java    From kop with Apache License 2.0 5 votes vote down vote up
@Override
public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient,
                         Optional<Class<? extends EnsemblePlacementPolicy>> ensemblePlacementPolicyClass,
                         Map<String, Object> properties) {
    // Always return the same instance (so that we don't loose the mock BK content on broker restart
    return mockBookKeeper;
}
 
Example #19
Source File: RackAwareTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testPlacement() throws Exception {
    for (int i = 0; i < NUM_BOOKIES; i++) {
        String bookie = bookies.get(i).getLocalAddress().toString();

        // Place bookie-1 in "rack-1" and the rest in "rack-2"
        int rackId = i == 0 ? 1 : 2;
        BookieInfo bi = new BookieInfo("rack-" + rackId, "bookie-" + (i + 1));
        log.info("setting rack for bookie at {} -- {}", bookie, bi);
        admin.bookies().updateBookieRackInfo(bookie, "default", bi);
    }

    // Make sure the racks cache gets updated through the ZK watch
    Thread.sleep(1000);

    BookKeeper bkc = this.pulsar.getBookKeeperClient();

    // Create few ledgers and verify all of them should have a copy in the first bookie
    BookieSocketAddress fistBookie = bookies.get(0).getLocalAddress();
    for (int i = 0; i < 100; i++) {
        LedgerHandle lh = bkc.createLedger(2, 2, DigestType.DUMMY, new byte[0]);
        log.info("Ledger: {} -- Ensemble: {}", i, lh.getLedgerMetadata().getEnsembleAt(0));
        assertTrue(lh.getLedgerMetadata().getEnsembleAt(0).contains(fistBookie),
                "first bookie in rack 0 not included in ensemble");
        lh.close();
    }
}
 
Example #20
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper, MetaStore store,
        ManagedLedgerConfig config, OrderedScheduler scheduledExecutor, OrderedExecutor orderedExecutor,
        final String name, final Supplier<Boolean> mlOwnershipChecker) {
    this.factory = factory;
    this.bookKeeper = bookKeeper;
    this.config = config;
    this.store = store;
    this.name = name;
    this.ledgerMetadata = LedgerMetadataUtils.buildBaseManagedLedgerMetadata(name);
    this.digestType = BookKeeper.DigestType.fromApiDigestType(config.getDigestType());
    this.scheduledExecutor = scheduledExecutor;
    this.executor = orderedExecutor;
    TOTAL_SIZE_UPDATER.set(this, 0);
    NUMBER_OF_ENTRIES_UPDATER.set(this, 0);
    ENTRIES_ADDED_COUNTER_UPDATER.set(this, 0);
    STATE_UPDATER.set(this, State.None);
    this.ledgersStat = null;
    this.mbean = new ManagedLedgerMBeanImpl(this);
    this.entryCache = factory.getEntryCacheManager().getEntryCache(this);
    this.waitingCursors = Queues.newConcurrentLinkedQueue();
    this.uninitializedCursors = Maps.newHashMap();
    this.clock = config.getClock();

    // Get the next rollover time. Add a random value upto 5% to avoid rollover multiple ledgers at the same time
    this.maximumRolloverTimeMs = (long) (config.getMaximumRolloverTimeMs() * (1 + random.nextDouble() * 5 / 100.0));
    this.mlOwnershipChecker = mlOwnershipChecker;
    this.propertiesMap = Maps.newHashMap();
}
 
Example #21
Source File: BlobStoreManagedLedgerOffloaderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private ReadHandle buildReadHandle(int maxBlockSize, int blockCount) throws Exception {
    Assert.assertTrue(maxBlockSize > DataBlockHeaderImpl.getDataStartOffset());

    LedgerHandle lh = bk.createLedger(1,1,1, BookKeeper.DigestType.CRC32, "foobar".getBytes());

    int i = 0;
    int bytesWrittenCurrentBlock = DataBlockHeaderImpl.getDataStartOffset();
    int blocksWritten = 1;
    int entries = 0;

    while (blocksWritten < blockCount
           || bytesWrittenCurrentBlock < maxBlockSize/2) {
        byte[] entry = ("foobar"+i).getBytes();
        int sizeInBlock = entry.length + 12 /* ENTRY_HEADER_SIZE */;

        if (bytesWrittenCurrentBlock + sizeInBlock > maxBlockSize) {
            bytesWrittenCurrentBlock = DataBlockHeaderImpl.getDataStartOffset();
            blocksWritten++;
            entries = 0;
        }
        entries++;

        lh.addEntry(entry);
        bytesWrittenCurrentBlock += sizeInBlock;
        i++;
    }

    lh.close();

    return bk.newOpenLedgerOp().withLedgerId(lh.getId())
        .withPassword("foobar".getBytes()).withDigestType(DigestType.CRC32).execute().get();
}
 
Example #22
Source File: DistributedLogInputFormat.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public List<InputSplit> getSplits(JobContext jobContext)
        throws IOException, InterruptedException {
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    List<InputSplit> inputSplits = Lists.newArrayListWithCapacity(segments.size());
    BookKeeper bk = namespace.getReaderBKC().get();
    LedgerManager lm = BookKeeperAccessor.getLedgerManager(bk);
    final AtomicInteger rcHolder = new AtomicInteger(0);
    final AtomicReference<LedgerMetadata> metadataHolder = new AtomicReference<LedgerMetadata>(null);
    for (LogSegmentMetadata segment : segments) {
        final CountDownLatch latch = new CountDownLatch(1);
        lm.readLedgerMetadata(segment.getLedgerId(),
                new BookkeeperInternalCallbacks.GenericCallback<LedgerMetadata>() {
            @Override
            public void operationComplete(int rc, LedgerMetadata ledgerMetadata) {
                metadataHolder.set(ledgerMetadata);
                rcHolder.set(rc);
                latch.countDown();
            }
        });
        latch.await();
        if (BKException.Code.OK != rcHolder.get()) {
            throw new IOException("Faild to get log segment metadata for " + segment + " : "
                    + BKException.getMessage(rcHolder.get()));
        }
        inputSplits.add(new LogSegmentSplit(segment, metadataHolder.get()));
    }
    return inputSplits;
}
 
Example #23
Source File: CompactorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompactEmptyTopic() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

    // trigger creation of topic on server side
    pulsarClient.newConsumer().topic(topic).subscriptionName("sub1").subscribe().close();

    BookKeeper bk = pulsar.getBookKeeperClientFactory().create(
            this.conf, null, Optional.empty(), null);
    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    compactor.compact(topic).get();
}
 
Example #24
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public synchronized BookKeeper get() throws IOException {
    checkClosedOrInError();
    if (null == bkc) {
        initialize();
    }
    return bkc;
}
 
Example #25
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Open the given ledger <i>ledgerDesc</i>.
 *
 * @param ledgerDesc
 *          ledger description
 * @param callback
 *          open callback.
 * @param ctx
 *          callback context
 */
private void asyncOpenLedger(LedgerDescriptor ledgerDesc, AsyncCallback.OpenCallback callback, Object ctx) {
    try {
        if (!ledgerDesc.isFenced()) {
            bkc.get().asyncOpenLedgerNoRecovery(ledgerDesc.getLedgerId(),
                    BookKeeper.DigestType.CRC32, digestpw.getBytes(UTF_8), callback, ctx);
        } else {
            bkc.get().asyncOpenLedger(ledgerDesc.getLedgerId(),
                    BookKeeper.DigestType.CRC32, digestpw.getBytes(UTF_8), callback, ctx);
        }
    } catch (IOException ace) {
        // :) when we can't get bkc, it means bookie handle not available
        callback.openComplete(BKException.Code.BookieHandleNotAvailableException, null, ctx);
    }
}
 
Example #26
Source File: DLMTestUtil.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public static void fenceStream(DistributedLogConfiguration conf, URI uri, String name) throws Exception {
    BKDistributedLogManager dlm = (BKDistributedLogManager) createNewDLM(name, conf, uri);
    try {
        BKLogReadHandler readHandler = dlm.createReadHandler();
        List<LogSegmentMetadata> ledgerList = readHandler.getFullLedgerList(true, true);
        LogSegmentMetadata lastSegment = ledgerList.get(ledgerList.size() - 1);
        BookKeeperClient bkc = dlm.getWriterBKC();
        LedgerHandle lh = bkc.get().openLedger(lastSegment.getLedgerId(),
                BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
        lh.close();
    } finally {
        dlm.close();
    }
}
 
Example #27
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected int runCmd() throws Exception {
    LedgerHandle lh = getBookKeeperClient().get().openLedgerNoRecovery(
            getLedgerID(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes(UTF_8));
    try {
        long lac = lh.readLastConfirmed();
        System.out.println("LastAddConfirmed: " + lac);
    } finally {
        lh.close();
    }
    return 0;
}
 
Example #28
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testCloseAllocatorDuringObtaining() throws Exception {
    String allocationPath = "/allocation2";
    SimpleLedgerAllocator allocator = createAllocator(allocationPath);
    allocator.allocate();
    ZKTransaction txn = newTxn();
    // close during obtaining ledger.
    LedgerHandle lh = FutureUtils.result(allocator.tryObtain(txn, NULL_LISTENER));
    Utils.close(allocator);
    byte[] data = zkc.get().getData(allocationPath, false, null);
    assertEquals((Long) lh.getId(), Long.valueOf(new String(data, UTF_8)));
    // the ledger is not deleted
    bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32,
            dlConf.getBKDigestPW().getBytes(UTF_8));
}
 
Example #29
Source File: TestLedgerAllocator.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@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 ZkVersion(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 = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    FutureUtils.result(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);
}
 
Example #30
Source File: MockedPulsarServiceBaseTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient,
        Optional<Class<? extends EnsemblePlacementPolicy>> ensemblePlacementPolicyClass,
        Map<String, Object> properties) {
    // Always return the same instance (so that we don't loose the mock BK content on broker restart
    return mockBookKeeper;
}