Search in sources :

Example 1 with LastConfirmedAndEntry

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);
    }
}
Also used : LastConfirmedAndEntry(org.apache.bookkeeper.client.api.LastConfirmedAndEntry) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) LogNotAvailableException(herddb.log.LogNotAvailableException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) BKException(org.apache.bookkeeper.client.BKException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) BKClientClosedException(org.apache.bookkeeper.client.BKException.BKClientClosedException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Aggregations

FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)1 LogNotAvailableException (herddb.log.LogNotAvailableException)1 IOException (java.io.IOException)1 CompletionException (java.util.concurrent.CompletionException)1 BKException (org.apache.bookkeeper.client.BKException)1 BKClientClosedException (org.apache.bookkeeper.client.BKException.BKClientClosedException)1 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)1 LastConfirmedAndEntry (org.apache.bookkeeper.client.api.LastConfirmedAndEntry)1 LedgerEntries (org.apache.bookkeeper.client.api.LedgerEntries)1 ReadHandle (org.apache.bookkeeper.client.api.ReadHandle)1