Search in sources :

Example 1 with UpdateLedgerNotifier

use of org.apache.bookkeeper.bookie.BookieShell.UpdateLedgerNotifier in project bookkeeper by apache.

the class UpdateLedgerOp method updateBookieIdInLedgers.

/**
 * Update the bookie id present in the ledger metadata.
 *
 * @param oldBookieId
 *            current bookie id
 * @param newBookieId
 *            new bookie id
 * @param rate
 *            number of ledgers updating per second (default 5 per sec)
 * @param limit
 *            maximum number of ledgers to update (default: no limit). Stop
 *            update if reaching limit
 * @param progressable
 *            report progress of the ledger updates
 * @throws IOException
 *             if there is an error when updating bookie id in ledger
 *             metadata
 * @throws InterruptedException
 *             interrupted exception when update ledger meta
 */
public void updateBookieIdInLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) throws IOException {
    final ExecutorService executor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("UpdateLedgerThread", true));
    final AtomicInteger issuedLedgerCnt = new AtomicInteger();
    final AtomicInteger updatedLedgerCnt = new AtomicInteger();
    final Future<?> updateBookieCb = executor.submit(new Runnable() {

        @Override
        public void run() {
            updateLedgers(oldBookieId, newBookieId, rate, limit, progressable);
        }

        private void updateLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) {
            try {
                final AtomicBoolean stop = new AtomicBoolean(false);
                final Set<Long> outstandings = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
                final RateLimiter throttler = RateLimiter.create(rate);
                final Iterator<Long> ledgerItr = admin.listLedgers().iterator();
                final CountDownLatch syncObj = new CountDownLatch(1);
                // iterate through all the ledgers
                while (ledgerItr.hasNext() && !stop.get()) {
                    // throttler to control updates per second
                    throttler.acquire();
                    final Long lId = ledgerItr.next();
                    final ReadLedgerMetadataCb readCb = new ReadLedgerMetadataCb(bkc, lId, oldBookieId, newBookieId);
                    outstandings.add(lId);
                    FutureCallback<Void> updateLedgerCb = new UpdateLedgerCb(lId, stop, issuedLedgerCnt, updatedLedgerCnt, outstandings, syncObj, progressable);
                    Futures.addCallback(readCb.getFutureListener(), updateLedgerCb);
                    issuedLedgerCnt.incrementAndGet();
                    if (limit != Integer.MIN_VALUE && issuedLedgerCnt.get() >= limit || !ledgerItr.hasNext()) {
                        stop.set(true);
                    }
                    bkc.getLedgerManager().readLedgerMetadata(lId, readCb);
                }
                // waiting till all the issued ledgers are finished
                syncObj.await();
            } catch (IOException ioe) {
                LOG.error("Exception while updating ledger", ioe);
                throw new RuntimeException("Exception while updating ledger", ioe.getCause());
            } catch (InterruptedException ie) {
                LOG.error("Exception while updating ledger metadata", ie);
                Thread.currentThread().interrupt();
                throw new RuntimeException("Exception while updating ledger", ie.getCause());
            }
        }
    });
    try {
        // Wait to finish the issued ledgers.
        updateBookieCb.get();
    } catch (ExecutionException ee) {
        throw new IOException("Exception while updating ledger", ee);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new IOException("Exception while updating ledger", ie);
    } finally {
        executor.shutdown();
    }
}
Also used : Set(java.util.Set) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) RateLimiter(com.google.common.util.concurrent.RateLimiter) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.google.common.util.concurrent.FutureCallback) UpdateLedgerNotifier(org.apache.bookkeeper.bookie.BookieShell.UpdateLedgerNotifier)

Aggregations

FutureCallback (com.google.common.util.concurrent.FutureCallback)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 UpdateLedgerNotifier (org.apache.bookkeeper.bookie.BookieShell.UpdateLedgerNotifier)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1