org.apache.bookkeeper.client.BookKeeperAdmin Java Examples

The following examples show how to use org.apache.bookkeeper.client.BookKeeperAdmin. 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: ZKTestEnv.java    From herddb with Apache License 2.0 6 votes vote down vote up
private String startBookie(boolean format) throws Exception {
    if (format && !bookies.isEmpty()) {
        throw new Exception("cannot format, you aleady have bookies");
    }
    ServerConfiguration conf = createBookieConf(nextBookiePort++);

    if (format) {
        BookKeeperAdmin.initNewCluster(conf);
        BookKeeperAdmin.format(conf, false, true);
    }

    BookieServer bookie = new BookieServer(conf);
    bookies.add(bookie);
    bookie.start();
    return bookie.getLocalAddress().getSocketAddress().toString();
}
 
Example #2
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private int bkQuery(BookKeeperAdmin bkAdmin, Set<BookieSocketAddress> bookieAddrs)
        throws InterruptedException, BKException {
    SortedMap<Long, LedgerMetadata> ledgersContainBookies =
            bkAdmin.getLedgersContainBookies(bookieAddrs);
    System.err.println("NOTE: Bookies in inspection list are marked with '*'.");
    for (Map.Entry<Long, LedgerMetadata> ledger : ledgersContainBookies.entrySet()) {
        System.out.println("ledger " + ledger.getKey() + " : " + ledger.getValue().getState());
        Map<Long, Integer> numBookiesToReplacePerEnsemble =
                inspectLedger(ledger.getValue(), bookieAddrs);
        System.out.print("summary: [");
        for (Map.Entry<Long, Integer> entry : numBookiesToReplacePerEnsemble.entrySet()) {
            System.out.print(entry.getKey() + "=" + entry.getValue() + ", ");
        }
        System.out.println("]");
        System.out.println();
    }
    System.out.println("Done");
    return 0;
}
 
Example #3
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private int bkRecovery(final LinkedBlockingQueue<Long> ledgers, final Set<BookieSocketAddress> bookieAddrs,
                       final boolean dryrun, final boolean skipOpenLedgers)
        throws Exception {
    return runBKCommand(new BKCommandRunner() {
        @Override
        public int run(ZooKeeperClient zkc, BookKeeperClient bkc) throws Exception {
            BookKeeperAdmin bkAdmin = new BookKeeperAdmin(bkc.get());
            try {
                bkRecovery(bkAdmin, ledgers, bookieAddrs, dryrun, skipOpenLedgers);
                return 0;
            } finally {
                bkAdmin.close();
            }
        }
    });
}
 
Example #4
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegments(LogSegmentMetadataStore metadataStore,
                                 BookKeeperClient bkc,
                                 List<LogSegmentMetadata> segments) throws Exception {
    BookKeeperAdmin bkAdmin = new BookKeeperAdmin(bkc.get());
    try {
        MetadataUpdater metadataUpdater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(
                getConf(), metadataStore);
        for (LogSegmentMetadata segment : segments) {
            repairLogSegment(bkAdmin, metadataUpdater, segment);
        }
    } finally {
        bkAdmin.close();
    }
}
 
Example #5
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegment(BookKeeperAdmin bkAdmin,
                                MetadataUpdater metadataUpdater,
                                LogSegmentMetadata segment) throws Exception {
    if (segment.isInProgress()) {
        System.out.println("Skip inprogress log segment " + segment);
        return;
    }
    LedgerHandle lh = bkAdmin.openLedger(segment.getLogSegmentId());
    long lac = lh.getLastAddConfirmed();
    Enumeration<LedgerEntry> entries = lh.readEntries(lac, lac);
    if (!entries.hasMoreElements()) {
        throw new IOException("Entry " + lac + " isn't found for " + segment);
    }
    LedgerEntry lastEntry = entries.nextElement();
    Entry.Reader reader = Entry.newBuilder()
            .setLogSegmentInfo(segment.getLogSegmentSequenceNumber(), segment.getStartSequenceId())
            .setEntryId(lastEntry.getEntryId())
            .setEnvelopeEntry(LogSegmentMetadata.supportsEnvelopedEntries(segment.getVersion()))
            .setEntry(lastEntry.getEntryBuffer())
            .buildReader();
    lastEntry.getEntryBuffer().release();
    LogRecordWithDLSN record = reader.nextRecord();
    LogRecordWithDLSN lastRecord = null;
    while (null != record) {
        lastRecord = record;
        record = reader.nextRecord();
    }
    if (null == lastRecord) {
        throw new IOException("No record found in entry " + lac + " for " + segment);
    }
    System.out.println("Updating last record for " + segment + " to " + lastRecord);
    if (!IOUtils.confirmPrompt("Do you want to make this change (Y/N): ")) {
        return;
    }
    metadataUpdater.updateLastRecord(segment, lastRecord);
}
 
Example #6
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegments(LogSegmentMetadataStore metadataStore,
                                 BookKeeperClient bkc,
                                 List<LogSegmentMetadata> segments) throws Exception {
    BookKeeperAdmin bkAdmin = new BookKeeperAdmin(bkc.get());
    try {
        MetadataUpdater metadataUpdater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(
                getConf(), metadataStore);
        for (LogSegmentMetadata segment : segments) {
            repairLogSegment(bkAdmin, metadataUpdater, segment);
        }
    } finally {
        bkAdmin.close();
    }
}
 
Example #7
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegment(BookKeeperAdmin bkAdmin,
                                MetadataUpdater metadataUpdater,
                                LogSegmentMetadata segment) throws Exception {
    if (segment.isInProgress()) {
        System.out.println("Skip inprogress log segment " + segment);
        return;
    }
    LedgerHandle lh = bkAdmin.openLedger(segment.getLedgerId(), true);
    long lac = lh.getLastAddConfirmed();
    Enumeration<LedgerEntry> entries = lh.readEntries(lac, lac);
    if (!entries.hasMoreElements()) {
        throw new IOException("Entry " + lac + " isn't found for " + segment);
    }
    LedgerEntry lastEntry = entries.nextElement();
    Entry.Reader reader = Entry.newBuilder()
            .setLogSegmentInfo(segment.getLogSegmentSequenceNumber(), segment.getStartSequenceId())
            .setEntryId(lastEntry.getEntryId())
            .setEnvelopeEntry(LogSegmentMetadata.supportsEnvelopedEntries(segment.getVersion()))
            .setInputStream(lastEntry.getEntryInputStream())
            .buildReader();
    LogRecordWithDLSN record = reader.nextRecord();
    LogRecordWithDLSN lastRecord = null;
    while (null != record) {
        lastRecord = record;
        record = reader.nextRecord();
    }
    if (null == lastRecord) {
        throw new IOException("No record found in entry " + lac + " for " + segment);
    }
    System.out.println("Updating last record for " + segment + " to " + lastRecord);
    if (!IOUtils.confirmPrompt("Do you want to make this change (Y/N): ")) {
        return;
    }
    metadataUpdater.updateLastRecord(segment, lastRecord);
}
 
Example #8
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private int bkRecovery(final BookKeeperAdmin bkAdmin, final LinkedBlockingQueue<Long> ledgers,
                       final Set<BookieSocketAddress> bookieAddrs,
                       final boolean dryrun, final boolean skipOpenLedgers)
        throws InterruptedException, BKException {
    final AtomicInteger numPendings = new AtomicInteger(ledgers.size());
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final CountDownLatch doneLatch = new CountDownLatch(concurrency);
    Runnable r = new Runnable() {
        @Override
        public void run() {
            while (!ledgers.isEmpty()) {
                long lid = -1L;
                try {
                    lid = ledgers.take();
                    System.out.println("Recovering ledger " + lid);
                    bkAdmin.recoverBookieData(lid, bookieAddrs, dryrun, skipOpenLedgers);
                    System.out.println("Recovered ledger completed : " + lid + ", " + numPendings.decrementAndGet() + " left");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    doneLatch.countDown();
                    break;
                } catch (BKException ke) {
                    System.out.println("Recovered ledger failed : " + lid + ", rc = " + BKException.getMessage(ke.getCode()));
                }
            }
            doneLatch.countDown();
        }
    };
    for (int i = 0; i < concurrency; i++) {
        executorService.submit(r);
    }
    doneLatch.await();
    SchedulerUtils.shutdownScheduler(executorService, 2, TimeUnit.MINUTES);
    return 0;
}
 
Example #9
Source File: TestBaseOffload.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public boolean ledgerExistsInBookKeeper(long ledgerId) throws Exception {
    ClientConfiguration bkConf = new ClientConfiguration();
    bkConf.setZkServers(pulsarCluster.getZKConnString());
    try (BookKeeperAdmin bk = new BookKeeperAdmin(bkConf)) {
        try {
            bk.openLedger(ledgerId).close();
            return true;
        } catch (BKException.BKNoSuchLedgerExistsException
            | BKException.BKNoSuchLedgerExistsOnMetadataServerException e) {
            return false;
        }
    }
}
 
Example #10
Source File: EmbeddedBookie.java    From herddb with Apache License 2.0 4 votes vote down vote up
public void start() throws Exception {
    org.apache.bookkeeper.conf.ServerConfiguration conf = new org.apache.bookkeeper.conf.ServerConfiguration();
    conf.setZkTimeout(metadataManager.getZkSessionTimeout());
    conf.setZkServers(metadataManager.getZkAddress());
    conf.setZkLedgersRootPath(configuration.getString(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH, ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH_DEFAULT));
    conf.setStatisticsEnabled(true);
    int port = configuration.getInt(ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_PORT, ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_PORT_DEFAULT);

    conf.setUseHostNameAsBookieID(true);
    Path bookie_dir = baseDirectory.resolve("bookie");
    if (port <= 0) {
        Integer _port = readLocalBookiePort(bookie_dir);
        if (_port == null) {
            _port = NetworkUtils.assignFirstFreePort();
            LOG.log(Level.SEVERE, "As configuration parameter "
                    + ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_PORT + " is {0},I have choosen to listen on port {1}."
                    + " Set to a positive number in order to use a fixed port", new Object[]{Integer.toString(port), Integer.toString(_port)});
            persistLocalBookiePort(bookie_dir, _port);
        }
        port = _port;
    }
    conf.setBookiePort(port);
    Files.createDirectories(bookie_dir);
    Path bookie_data_dir = bookie_dir.resolve("bookie_data").toAbsolutePath();
    Path bookie_journal_dir = bookie_dir.resolve("bookie_journal").toAbsolutePath();
    Files.createDirectories(bookie_data_dir);
    Files.createDirectories(bookie_journal_dir);
    conf.setLedgerDirNames(new String[]{bookie_data_dir.toString()});
    conf.setJournalDirName(bookie_journal_dir.toString());
    conf.setFlushInterval(1000);
    conf.setMaxBackupJournals(5);
    conf.setMaxJournalSizeMB(1048);
    conf.setNumAddWorkerThreads(8);
    conf.setMaxPendingReadRequestPerThread(200000); // new in 4.6
    conf.setMaxPendingAddRequestPerThread(200000); // new in 4.6
    conf.setEnableLocalTransport(true);
    conf.setProperty("journalMaxGroupWaitMSec", 10L); // default 200ms
    conf.setJournalFlushWhenQueueEmpty(true);
    conf.setAutoRecoveryDaemonEnabled(false);
    conf.setLedgerManagerFactoryClass(HierarchicalLedgerManagerFactory.class);

    for (String key : configuration.keys()) {
        if (key.startsWith("bookie.")) {
            String bookieConf = key.substring("bookie.".length());
            String value = configuration.getString(key, null);
            conf.addProperty(bookieConf, value);
            LOG.log(Level.CONFIG, "config {0} remapped to {1}={2}", new Object[]{key, bookieConf, value});
        }
    }
    long _start = System.currentTimeMillis();
    LOG.severe("Booting Apache Bookkeeper on port " + port + ",  base directory: " + bookie_dir);

    Files.createDirectories(bookie_dir);
    dumpBookieConfiguration(bookie_dir, conf);

    boolean forcemetaformat = configuration.getBoolean("bookie.forcemetaformat", false);
    LOG.log(Level.CONFIG, "bookie.forcemetaformat={0}", forcemetaformat);

    boolean result = BookKeeperAdmin.format(conf, false, forcemetaformat);
    if (result) {
        LOG.info("BookKeeperAdmin.format: created a new workspace on ZK");
    } else {
        LOG.info("BookKeeperAdmin.format: ZK space does not need an format operation");
    }

    boolean forceformat = configuration.getBoolean("bookie.forceformat", false);
    LOG.log(Level.CONFIG, "bookie.forceformat={0}", forceformat);
    if (forceformat) {
        result = Bookie.format(conf, false, forceformat);
        if (result) {
            LOG.info("Bookie.format: formatter applied to local bookie");
        } else {
            LOG.info("Bookie.format: local boookie did not need formatting");
        }
    }

    bookieServer = new BookieServer(conf, statsLogger);
    bookieServer.start();
    for (int i = 0; i < 100; i++) {
        if (bookieServer.getBookie().isRunning()) {
            LOG.info("Apache Bookkeeper started");
            break;
        }
        Thread.sleep(500);
    }
    long _stop = System.currentTimeMillis();
    LOG.severe("Booting Apache Bookkeeper finished. Time " + (_stop - _start) + " ms");

}
 
Example #11
Source File: ZKTestEnv.java    From herddb with Apache License 2.0 4 votes vote down vote up
public void startBookie(boolean format) throws Exception {
        if (bookie != null) {
            throw new Exception("bookie already started");
        }
        ServerConfiguration conf = new ServerConfiguration();
        conf.setBookiePort(0);
        conf.setUseHostNameAsBookieID(true);

        Path targetDir = path.resolve("bookie_data");
        conf.setZkServers("localhost:1282");
        conf.setZkLedgersRootPath("/ledgers");
        conf.setLedgerDirNames(new String[]{targetDir.toAbsolutePath().toString()});
        conf.setJournalDirName(targetDir.toAbsolutePath().toString());
        conf.setFlushInterval(10000);
        conf.setGcWaitTime(5);
        conf.setJournalFlushWhenQueueEmpty(true);
//        conf.setJournalBufferedEntriesThreshold(1);
        conf.setAutoRecoveryDaemonEnabled(false);
        conf.setEnableLocalTransport(true);
        conf.setJournalSyncData(false);

        conf.setAllowLoopback(true);
        conf.setProperty("journalMaxGroupWaitMSec", 10); // default 200ms

        try (ZooKeeperClient zkc = ZooKeeperClient
                .newBuilder()
                .connectString("localhost:1282")
                .sessionTimeoutMs(10000)
                .build()) {

            boolean rootExists = zkc.exists(getPath(), false) != null;

            if (!rootExists) {
                zkc.create(getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }

        }

        if (format) {
            BookKeeperAdmin.initNewCluster(conf);
            BookKeeperAdmin.format(conf, false, true);
        }

        this.bookie = new BookieServer(conf);
        this.bookie.start();
    }
 
Example #12
Source File: BookKeeperLogTests.java    From pravega with Apache License 2.0 4 votes vote down vote up
@Test
public void testReconcileMetadata() throws Exception {
    @Cleanup
    BookKeeperAdmin a = new BookKeeperAdmin((org.apache.bookkeeper.client.BookKeeper) this.factory.get().getBookKeeperClient());
    val initialLedgers = Sets.newHashSet(a.listLedgers());

    // Test initialization (node creation).
    try (val log = new TestBookKeeperLog()) {
        // Data not persisted and we throw an error - this is a real fencing event.
        log.setThrowZkException(true);
        log.setPersistData(false);
        AssertExtensions.assertThrows(
                "Create(Persist=False, Throw=True)",
                () -> log.initialize(TIMEOUT),
                ex -> ex instanceof DataLogWriterNotPrimaryException);
        Assert.assertEquals(1, log.getCreateExceptionCount());

        // Data persisted correctly and we throw an error - reconciliation needed.
        log.setPersistData(true);
        log.initialize(TIMEOUT);
        Assert.assertEquals("Create(Persist=True, Throw=True)", 2, log.getCreateExceptionCount());
    }

    val expectedLedgerIds = new HashSet<Long>();
    // Test updates (subsequent recoveries).
    try (val log = new TestBookKeeperLog()) {
        // Data not persisted and we throw an error - this is a real fencing event.
        log.setThrowZkException(true);
        log.setPersistData(false);
        AssertExtensions.assertThrows(
                "Update(Persist=False, Throw=True)",
                () -> log.initialize(TIMEOUT),
                ex -> ex instanceof DataLogWriterNotPrimaryException);
        Assert.assertEquals(1, log.getUpdateExceptionCount());

        // Data persisted correctly and we throw an error - reconciliation needed.
        log.setPersistData(true);
        log.initialize(TIMEOUT);
        Assert.assertEquals("Update(Persist=True, Throw=True)", 2, log.getUpdateExceptionCount());
        log.loadMetadata().getLedgers().stream().map(LedgerMetadata::getLedgerId).forEach(expectedLedgerIds::add);
    }

    // Verify ledger cleanup.
    val allLedgers = Sets.newHashSet(a.listLedgers());
    allLedgers.removeAll(initialLedgers);
    Assert.assertEquals("Unexpected ledgers in BK.", expectedLedgerIds, allLedgers);
}
 
Example #13
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected int runBKCmd(ZooKeeperClient zkc, BookKeeperClient bkc) throws Exception {
    BookKeeperAdmin bkAdmin = new BookKeeperAdmin(bkc.get());
    try {
        if (query) {
            return bkQuery(bkAdmin, bookiesSrc);
        }
        if (fenceOnly) {
            return bkFence(bkc, ledgers, fenceRate);
        }
        if (!force) {
            System.out.println("Bookies : " + bookiesSrc);
            if (!IOUtils.confirmPrompt("Do you want to recover them: (Y/N)")) {
                return -1;
            }
        }
        if (!ledgers.isEmpty()) {
            System.out.println("Ledgers : " + ledgers);
            long numProcessed = 0;
            Iterator<Long> ledgersIter = ledgers.iterator();
            LinkedBlockingQueue<Long> ledgersToProcess = new LinkedBlockingQueue<Long>();
            while (ledgersIter.hasNext()) {
                long lid = ledgersIter.next();
                if (numPartitions <=0 || (numPartitions > 0 && lid % numPartitions == partition)) {
                    ledgersToProcess.add(lid);
                    ++numProcessed;
                }
                if (ledgersToProcess.size() == 10000) {
                    System.out.println("Processing " + numProcessed + " ledgers");
                    bkRecovery(ledgersToProcess, bookiesSrc, dryrun, skipOpenLedgers);
                    ledgersToProcess.clear();
                    System.out.println("Processed " + numProcessed + " ledgers");
                }
            }
            if (!ledgersToProcess.isEmpty()) {
                System.out.println("Processing " + numProcessed + " ledgers");
                bkRecovery(ledgersToProcess, bookiesSrc, dryrun, skipOpenLedgers);
                System.out.println("Processed " + numProcessed + " ledgers");
            }
            System.out.println("Done.");
            CountDownLatch latch = new CountDownLatch(1);
            latch.await();
            return 0;
        }
        return bkRecovery(bkAdmin, bookiesSrc, dryrun, skipOpenLedgers);
    } finally {
        bkAdmin.close();
    }
}
 
Example #14
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private int bkRecovery(BookKeeperAdmin bkAdmin, Set<BookieSocketAddress> bookieAddrs,
                       boolean dryrun, boolean skipOpenLedgers)
        throws InterruptedException, BKException {
    bkAdmin.recoverBookieData(bookieAddrs, dryrun, skipOpenLedgers);
    return 0;
}