use of org.apache.bookkeeper.client.api.LastConfirmedAndEntry in project herddb by diennea.
the class BookkeeperCommitLog method followTheLeader.
@Override
public void followTheLeader(LogSequenceNumber lastPosition, EntryAcceptor consumer, FollowerContext context) throws LogNotAvailableException {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(tableSpaceDescription() + " followTheLeader lastPosition:" + lastPosition);
}
BKFollowerContext fContext = (BKFollowerContext) context;
try {
fContext.ensureOpenReader(lastPosition);
if (fContext.currentLedger == null) {
// no data to read
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(tableSpaceDescription() + " no more data to read for now");
}
return;
}
long nextEntry = fContext.nextEntryToRead;
long lastAddConfirmed = fContext.currentLedger.getLastAddConfirmed();
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(tableSpaceDescription() + " next entry to read " + nextEntry + " from ledger " + fContext.currentLedger.getId() + " lastAddConfiremd " + lastAddConfirmed);
}
if (lastAddConfirmed < nextEntry) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(tableSpaceDescription() + " ledger not closed but there is nothing to read by now");
}
return;
}
ReadHandle lh = fContext.currentLedger;
try (LastConfirmedAndEntry entryAndLac = lh.readLastAddConfirmedAndEntry(nextEntry, LONG_POLL_TIMEOUT, false)) {
if (entryAndLac.hasEntry()) {
org.apache.bookkeeper.client.api.LedgerEntry e = entryAndLac.getEntry();
boolean canContinue = acceptEntryForFollower(e, consumer);
if (!canContinue) {
LOGGER.log(Level.INFO, "exit follower {0}", tableSpaceDescription());
return;
}
long startEntry = nextEntry + 1;
long endEntry = entryAndLac.getLastAddConfirmed();
if (startEntry > endEntry) {
return;
}
// to be read then read them
if (endEntry - startEntry > MAX_ENTRY_TO_TAIL) {
// put a bound on the max entries to read per round
endEntry = startEntry + MAX_ENTRY_TO_TAIL;
}
try (LedgerEntries entries = lh.read(startEntry, endEntry)) {
for (org.apache.bookkeeper.client.api.LedgerEntry ee : entries) {
acceptEntryForFollower(ee, consumer);
}
}
}
}
} catch (BKClientClosedException err) {
LOGGER.log(Level.FINE, "stop following " + tableSpaceDescription(), err);
} catch (org.apache.bookkeeper.client.api.BKException err) {
LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal BK error", err);
throw new LogNotAvailableException(err);
} catch (InterruptedException err) {
LOGGER.log(Level.SEVERE, tableSpaceDescription() + " interrupted", err);
Thread.currentThread().interrupt();
throw new LogNotAvailableException(err);
} catch (LogNotAvailableException err) {
LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal error", err);
throw err;
} catch (Exception err) {
LOGGER.log(Level.SEVERE, tableSpaceDescription() + " internal error", err);
throw new LogNotAvailableException(err);
}
}
Aggregations