Search in sources :

Example 1 with Processor

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor in project bookkeeper by apache.

the class BookieLedgerIndexer method getBookieToLedgerIndex.

/**
 * Generating bookie vs its ledgers map by reading all the ledgers in each
 * bookie and parsing its metadata.
 *
 * @return bookie2ledgersMap map of bookie vs ledgers
 * @throws BKAuditException
 *             exception while getting bookie-ledgers
 */
public Map<String, Set<Long>> getBookieToLedgerIndex() throws BKAuditException {
    // bookie vs ledgers map
    final ConcurrentHashMap<String, Set<Long>> bookie2ledgersMap = new ConcurrentHashMap<String, Set<Long>>();
    final CountDownLatch ledgerCollectorLatch = new CountDownLatch(1);
    Processor<Long> ledgerProcessor = new Processor<Long>() {

        @Override
        public void process(final Long ledgerId, final AsyncCallback.VoidCallback iterCallback) {
            GenericCallback<LedgerMetadata> genericCallback = new GenericCallback<LedgerMetadata>() {

                @Override
                public void operationComplete(int rc, LedgerMetadata ledgerMetadata) {
                    if (rc == BKException.Code.OK) {
                        for (Map.Entry<Long, ArrayList<BookieSocketAddress>> ensemble : ledgerMetadata.getEnsembles().entrySet()) {
                            for (BookieSocketAddress bookie : ensemble.getValue()) {
                                putLedger(bookie2ledgersMap, bookie.toString(), ledgerId);
                            }
                        }
                    } else if (rc == BKException.Code.NoSuchLedgerExistsException) {
                        LOG.info("Ignoring replication of already deleted ledger {}", ledgerId);
                        rc = BKException.Code.OK;
                    } else {
                        LOG.warn("Unable to read the ledger:" + ledgerId + " information");
                    }
                    iterCallback.processResult(rc, null, null);
                }
            };
            ledgerManager.readLedgerMetadata(ledgerId, genericCallback);
        }
    };
    // Reading the result after processing all the ledgers
    final List<Integer> resultCode = new ArrayList<Integer>(1);
    ledgerManager.asyncProcessLedgers(ledgerProcessor, new AsyncCallback.VoidCallback() {

        @Override
        public void processResult(int rc, String s, Object obj) {
            resultCode.add(rc);
            ledgerCollectorLatch.countDown();
        }
    }, null, BKException.Code.OK, BKException.Code.ReadException);
    try {
        ledgerCollectorLatch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new BKAuditException("Exception while getting the bookie-ledgers", e);
    }
    if (!resultCode.contains(BKException.Code.OK)) {
        throw new BKAuditException("Exception while getting the bookie-ledgers", BKException.create(resultCode.get(0)));
    }
    return bookie2ledgersMap;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Processor(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor) AsyncCallback(org.apache.zookeeper.AsyncCallback) ArrayList(java.util.ArrayList) BKAuditException(org.apache.bookkeeper.replication.ReplicationException.BKAuditException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) CountDownLatch(java.util.concurrent.CountDownLatch) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 2 with Processor

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor in project bookkeeper by apache.

the class Auditor method checkAllLedgers.

/**
 * List all the ledgers and check them individually. This should not
 * be run very often.
 */
void checkAllLedgers() throws BKAuditException, BKException, IOException, InterruptedException, KeeperException {
    ZooKeeper newzk = ZooKeeperClient.newBuilder().connectString(conf.getZkServers()).sessionTimeoutMs(conf.getZkTimeout()).build();
    final BookKeeper client = new BookKeeper(new ClientConfiguration(conf), newzk);
    final BookKeeperAdmin admin = new BookKeeperAdmin(client, statsLogger);
    try {
        final LedgerChecker checker = new LedgerChecker(client);
        final AtomicInteger returnCode = new AtomicInteger(BKException.Code.OK);
        final CountDownLatch processDone = new CountDownLatch(1);
        Processor<Long> checkLedgersProcessor = new Processor<Long>() {

            @Override
            public void process(final Long ledgerId, final AsyncCallback.VoidCallback callback) {
                try {
                    if (!ledgerUnderreplicationManager.isLedgerReplicationEnabled()) {
                        LOG.info("Ledger rereplication has been disabled, aborting periodic check");
                        processDone.countDown();
                        return;
                    }
                } catch (ReplicationException.UnavailableException ue) {
                    LOG.error("Underreplication manager unavailable running periodic check", ue);
                    processDone.countDown();
                    return;
                }
                LedgerHandle lh = null;
                try {
                    lh = admin.openLedgerNoRecovery(ledgerId);
                    checker.checkLedger(lh, new ProcessLostFragmentsCb(lh, callback), conf.getAuditorLedgerVerificationPercentage());
                    // we collect the following stats to get a measure of the
                    // distribution of a single ledger within the bk cluster
                    // the higher the number of fragments/bookies, the more distributed it is
                    numFragmentsPerLedger.registerSuccessfulValue(lh.getNumFragments());
                    numBookiesPerLedger.registerSuccessfulValue(lh.getNumBookies());
                    numLedgersChecked.inc();
                } catch (BKException.BKNoSuchLedgerExistsException bknsle) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Ledger was deleted before we could check it", bknsle);
                    }
                    callback.processResult(BKException.Code.OK, null, null);
                    return;
                } catch (BKException bke) {
                    LOG.error("Couldn't open ledger " + ledgerId, bke);
                    callback.processResult(BKException.Code.BookieHandleNotAvailableException, null, null);
                    return;
                } catch (InterruptedException ie) {
                    LOG.error("Interrupted opening ledger", ie);
                    Thread.currentThread().interrupt();
                    callback.processResult(BKException.Code.InterruptedException, null, null);
                    return;
                } finally {
                    if (lh != null) {
                        try {
                            lh.close();
                        } catch (BKException bke) {
                            LOG.warn("Couldn't close ledger " + ledgerId, bke);
                        } catch (InterruptedException ie) {
                            LOG.warn("Interrupted closing ledger " + ledgerId, ie);
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            }
        };
        ledgerManager.asyncProcessLedgers(checkLedgersProcessor, new AsyncCallback.VoidCallback() {

            @Override
            public void processResult(int rc, String s, Object obj) {
                returnCode.set(rc);
                processDone.countDown();
            }
        }, null, BKException.Code.OK, BKException.Code.ReadException);
        try {
            processDone.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new BKAuditException("Exception while checking ledgers", e);
        }
        if (returnCode.get() != BKException.Code.OK) {
            throw BKException.create(returnCode.get());
        }
    } finally {
        admin.close();
        client.close();
        newzk.close();
    }
}
Also used : Processor(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor) AsyncCallback(org.apache.zookeeper.AsyncCallback) BKAuditException(org.apache.bookkeeper.replication.ReplicationException.BKAuditException) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerChecker(org.apache.bookkeeper.client.LedgerChecker) BKException(org.apache.bookkeeper.client.BKException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) UnavailableException(org.apache.bookkeeper.replication.ReplicationException.UnavailableException)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 Processor (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor)2 BKAuditException (org.apache.bookkeeper.replication.ReplicationException.BKAuditException)2 AsyncCallback (org.apache.zookeeper.AsyncCallback)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BKException (org.apache.bookkeeper.client.BKException)1 BookKeeper (org.apache.bookkeeper.client.BookKeeper)1 BookKeeperAdmin (org.apache.bookkeeper.client.BookKeeperAdmin)1 LedgerChecker (org.apache.bookkeeper.client.LedgerChecker)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1 GenericCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback)1 UnavailableException (org.apache.bookkeeper.replication.ReplicationException.UnavailableException)1