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();
}
}
Aggregations