Class CrawlerEntryLedger
Tracks document state and any other meta information required for document processing. Acts as a facade over a few processing state-specific data stores. Includes persisting of information necessary for incremental crawls and resumes.
The processing of a document has the following stages:
- Queued: References extracted from documents are first queued for processing.
- Processing: A reference is currently being processed.
- Processed: A reference has been processed. If the same reference is encountered again during the same run, it will be ignored.
Once a crawl completes, processed references become "cached" on the next run and are used to establish deltas and save processing.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidArchives the current ledger by making it the baseline and resetting the current ledger to empty.voidvoidClears QUEUED entries from the active ledger without issuing a physical distributed queue clear.longcountByStatus(ProcessingStatus status) Counts entries with the given processing status.voiddeleteByStatus(ProcessingStatus status) Deletes all entries with the given processing status.voidEnsures the current ledger alias exists in session cache.booleanWhether a reference exists in the current ledger.booleanfinishClaimedEntry(@NonNull CrawlerEntry entry) Records the outcome of processing an entry, likeupdateEntry(CrawlerEntry), but only if the claim it carries still holds: the entry was not given to another node in the meantime.voidvoidvoidvoidgetBaselineChildren(String parentReference) Gets the entries of the previous crawl that were split off from or embedded in the given document, and that have not been processed yet in this crawl.longgetEntriesByStatus(ProcessingStatus status) Gets all entries matching the given processing status.Gets the current ledger entry for the given reference, if it exists.longlongGets a reference processing status.longlongReturns the number of entries tracked asProcessingStatus.QUEUEDin the current ledger.voidinit(CrawlerSession session) booleanbooleanbooleanbooleanReturns whether the current ledger has any entries still tracked inProcessingStatus.QUEUEDstate.booleanbooleanmarkSending(@NonNull CrawlerEntry claimedEntry) Marks an entry taken for processing as being sent to committers, if its claim still holds: the entry was not given to another node in the meantime.nextQueuedBatch(int batchSize) voidqueue(@NonNull CrawlerEntry crawlEntry) voidWorks out how many documents may be processed in total by this run: the configured maximum, plus those already processed when the run resumes an earlier one of the same session.removeEntry(String reference) Removes an entry from the ledger, updating the status counters.booleanrequeueEntry(@NonNull CrawlerEntry crawlEntry) booleanrequeueEntry(@NonNull String reference) intRe-queues entries that were in PROCESSING state from a previous run.intRe-queues entries that were in QUEUED state from a previous run.intrequeueStrandedEntries(@NonNull Set<String> workingNodeNames) Puts back in the queue the entries taken for processing by a node no longer working on the crawl, so the nodes still working on it process them in this run.voidsetQueuedListener(Consumer<CrawlerEntry> listener) Registers a listener that is called each time a new reference is successfully added to the queue.updateEntry(CrawlerEntry entry) Updates an entry in the ledger, maintaining the status counters.
-
Constructor Details
-
CrawlerEntryLedger
public CrawlerEntryLedger()
-
-
Method Details
-
setQueuedListener
Registers a listener that is called each time a new reference is successfully added to the queue. Use this to decouple application-level event publishing from the ledger.- Parameters:
listener- callback invoked with the queuedCrawlerEntry
-
ensureCurrentLedgerAliasExists
public void ensureCurrentLedgerAliasExists()Ensures the current ledger alias exists in session cache. Sets default (LEDGER_A) if not present. This should be called by the coordinator before any ledger rotation to establish the initial state for all cluster nodes. -
init
-
refreshMaxDocs
public void refreshMaxDocs()Works out how many documents may be processed in total by this run: the configured maximum, plus those already processed when the run resumes an earlier one of the same session. Called when the ledger is initialized, and again if what it was based on changed since (the stored history being cleared within the same session). -
requeueQueuedEntries
public int requeueQueuedEntries()Re-queues entries that were in QUEUED state from a previous run. This is needed when the persistent queue fails to restore items (e.g., due to partition ownership changes across restarts). Called by CrawlerEntryLedgerBootstrapper during RUN LEVEL initialization.- Returns:
- the number of entries re-queued
-
updateEntry
Updates an entry in the ledger, maintaining the status counters.- Parameters:
entry- the entry to update- Returns:
- the previous entry if it existed
-
markSending
Marks an entry taken for processing as being sent to committers, if its claim still holds: the entry was not given to another node in the meantime.- Parameters:
claimedEntry- the entry as taken for processing (with its claim)- Returns:
trueif the claim still holds and the entry is marked
-
finishClaimedEntry
Records the outcome of processing an entry, likeupdateEntry(CrawlerEntry), but only if the claim it carries still holds: the entry was not given to another node in the meantime. An entry carrying no claim is recorded as is.- Parameters:
entry- the processed entry, with the claim it was taken with- Returns:
trueif recorded
-
removeEntry
Removes an entry from the ledger, updating the status counters.- Parameters:
reference- the reference to remove- Returns:
- the removed entry if it existed
-
exists
Whether a reference exists in the current ledger.- Parameters:
ref- document reference- Returns:
trueif existing
-
getProcessingStatus
Gets a reference processing status. If a document does not exist, the processing status will beProcessingStatus.UNTRACKED.- Parameters:
ref- document reference- Returns:
- the processing status
-
getEntry
Gets the current ledger entry for the given reference, if it exists.- Parameters:
ref- document reference- Returns:
- the current crawl entry, or empty if not found
-
isQueueEmpty
public boolean isQueueEmpty() -
getQueueCount
public long getQueueCount() -
getQueuedEntryCount
public long getQueuedEntryCount()Returns the number of entries tracked asProcessingStatus.QUEUEDin the current ledger.Unlike the physical queue size, this value comes from the authoritative ledger state and avoids expensive queue-store size operations.
- Returns:
- queued entry count from the ledger
-
isQueuedEntryEmpty
public boolean isQueuedEntryEmpty()Returns whether the current ledger has any entries still tracked inProcessingStatus.QUEUEDstate.- Returns:
trueif no queued entries remain in the ledger
-
clearQueue
public void clearQueue() -
clearQueuedEntriesInLedger
public void clearQueuedEntriesInLedger()Clears QUEUED entries from the active ledger without issuing a physical distributed queue clear. -
forEachQueued
-
queue
-
requeueEntry
-
requeueEntry
-
nextQueuedBatch
-
requeueProcessingEntries
public int requeueProcessingEntries()Re-queues entries that were in PROCESSING state from a previous run. stopped. These entries were pulled from the queue but not completed, so they need to be queued again on resume to avoid losing them. Updates their status back to QUEUED.- Returns:
- the number of entries re-queued
-
requeueStrandedEntries
Puts back in the queue the entries taken for processing by a node no longer working on the crawl, so the nodes still working on it process them in this run. An entry whose node is still working, and one that does not say which node took it, is left alone. Each entry is put back only if it is still held by the same node, so nodes doing this at the same time put it back once, and one finished in the meantime stays finished.- Parameters:
workingNodeNames- the nodes still working on the crawl; when empty (the cluster could not tell), nothing is put back- Returns:
- the number of entries put back in the queue
-
getProcessingCount
public long getProcessingCount() -
isProcessingEmpty
public boolean isProcessingEmpty() -
forEachProcessing
-
getProcessedCount
public long getProcessedCount() -
isProcessedEmpty
public boolean isProcessedEmpty() -
forEachProcessed
-
getBaselineCount
public long getBaselineCount() -
forEachBaseline
-
getBaselineEntry
-
getBaselineChildren
Gets the entries of the previous crawl that were split off from or embedded in the given document, and that have not been processed yet in this crawl. A child that was processed in this crawl is no longer part of the baseline, so what is returned is what the parent did not produce again, plus what has not been dealt with yet.Costs one pass over the baseline the first time it is called in a crawl, to index the children by their parent. It is not meant to be called for every document: only for a parent known to have children (
CrawlerEntry.getChildCount()above zero).- Parameters:
parentReference- the reference of the parent- Returns:
- the children still in the baseline, never
null
-
getEntriesByStatus
Gets all entries matching the given processing status.- Parameters:
status- the processing status to match- Returns:
- matching entries
-
countByStatus
Counts entries with the given processing status. Uses an efficient O(1) counter instead of executing a query.- Parameters:
status- the processing status to count- Returns:
- count of entries with the given status
-
deleteByStatus
Deletes all entries with the given processing status. Also updates the status counter accordingly.- Parameters:
status- the processing status of entries to delete
-
archiveCurrentLedger
public void archiveCurrentLedger()Archives the current ledger by making it the baseline and resetting the current ledger to empty. Must only be called by the coordinator node. In a multi-node cluster, calling this on a non-coordinator node is a no-op (logged as a warning). -
isMaxDocsProcessedReached
public boolean isMaxDocsProcessedReached()
-