java.lang.Object
net.i2p.router.networkdb.kademlia.SearchState
Manages state for network database search operations.
Tracks peer interaction status, timing information, and search progress for iterative Kademlia searches. Maintains separate sets for pending, attempted, failed, successful, and replied peers to coordinate concurrent search operations and prevent duplicate queries.
Thread safety: the collections are plain and guarded by synchronized blocks on each collection, with volatile timing fields; a few read-only size checks in toString() run unlocked. Safe for concurrent access from multiple search jobs.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanTrue if the search was aborted.Peers asked at any point in the search.private longWhen the search completed, 0 until then.private final RouterContextThe router context.Peers that failed to reply.private longWhen the first lease set arrived.private final AtomicIntegerInitial responses received so far.private longWhen waiting for the initial responses began.Lease sets found, by hash.private final LogLogger for the lookup search.Peers asked, waiting for a reply.When each pending peer was asked.Peers that replied at all.private final HashThe hash being searched for.private longWhen the search started.private longPublish date of the lease set stored.Peers that replied with results.private static final intInitial responses waited for.private static final longTime waited for the initial responses.Constructor Summary
ConstructorsConstructorDescriptionSearchState(RouterContext context, Hash key) Opens the search's log, records the start time and initializes the peer-tracking sets.Method Summary
Modifier and TypeMethodDescriptionvoidabort()Aborts this search state.voidaddInitialLeaseSetResponse(Hash peer, LeaseSet ls) Record the peer's LeaseSet response from the initial phase.voidaddLeaseSetResponse(Hash peer, LeaseSet ls) Record the peer's LeaseSet response.voidaddPending(Collection<Hash> pending) Add the peers to the pending and attempted sets.voidaddPending(Hash peer) Add the peer to the pending and attempted sets.voidClear the initial response tracking state.voidcomplete()Mark the search as completed at the current time.booleanWhether the search has completed.longHow long it took to get the reply, or -1 if we don't know.Copy of the attempted peers.intNumber of attempted peers.LeaseSet with the newest lease date among the initial responses.getClosestAttempted(int max) Up to max attempted peers closest to the target.Copy of the failed peers.intNumber of failed peers.LeaseSet with the newest lease date among the responses.Copy of the pending peers.intNumber of pending peers.Copy of the peers that replied.longStored lease date of the last accepted LeaseSet.Copy of the successful peers.intNumber of successful peers.The search target.longTime the search completed, or -1 if not yet completed.longTime the search started.booleanChecks if this search state has been aborted.locked_getClosest(Set<Hash> peers, int max, Hash target) Picks the peers closest to the target.voidremovePending(Hash peer) We didn't actually want to add this peer as part of the pending list...longreplyFound(Hash peer) How long it took to get the reply, or -1 if we don't know.voidreplyTimeout(Hash peer) Move the peer from pending to failed on timeout.voidsetStoredLeaseDate(long date) Record the stored lease date.booleanWhether enough initial responses have been received to store.booleanWhether the given LeaseSet is newer than the stored one.toString()Debug string for this search state.booleanwasAttempted(Hash peer) Whether the peer was attempted.
Field Details
_context
The router context._pendingPeers
_pendingPeerTimes
_attemptedPeers
_failedPeers
_successfulPeers
_repliedPeers
_searchKey
The hash being searched for._completed
private volatile long _completedWhen the search completed, 0 until then._started
private volatile long _startedWhen the search started._aborted
private volatile boolean _abortedTrue if the search was aborted._log
Logger for the lookup search._leaseSetResponses
_firstLeaseSetTime
private long _firstLeaseSetTimeWhen the first lease set arrived._initialResponseCount
Initial responses received so far._initialResponseStart
private volatile long _initialResponseStartWhen waiting for the initial responses began.INITIAL_RESPONSE_TARGET
private static final int INITIAL_RESPONSE_TARGETInitial responses waited for.- See Also:
INITIAL_RESPONSE_TIMEOUT
private static final long INITIAL_RESPONSE_TIMEOUTTime waited for the initial responses.- See Also:
_storedLeaseDate
private volatile long _storedLeaseDatePublish date of the lease set stored.
Constructor Details
SearchState
Opens the search's log, records the start time and initializes the peer-tracking sets.- Parameters:
context- the context supplying the log and the clock this search is timed againstkey- the search target that peers are ranked against by XOR distance
Method Details
getTarget
getPendingSize
public int getPendingSize()Number of pending peers.- Returns:
- the pending size
getPending
getAttempted
getAttemptedSize
public int getAttemptedSize()Number of attempted peers.- Returns:
- the attempted size
getClosestAttempted
locked_getClosest
wasAttempted
Whether the peer was attempted.- Parameters:
peer- the peer to look for in the attempted set- Returns:
- true if this peer has already been queried for this search
getSuccessful
getFailed
getFailedSize
public int getFailedSize()Number of failed peers.- Returns:
- the failed size
getSuccessfulSize
public int getSuccessfulSize()Number of successful peers.- Returns:
- the successful size
completed
public boolean completed()Whether the search has completed.- Returns:
- true once a completion time has been recorded
complete
public void complete()Mark the search as completed at the current time.isAborted
public boolean isAborted()Checks if this search state has been aborted.- Returns:
- true once
abort()has been called
abort
public void abort()Aborts this search state.getWhenStarted
public long getWhenStarted()Time the search started.- Returns:
- the when started
getWhenCompleted
public long getWhenCompleted()Time the search completed, or -1 if not yet completed.- Returns:
- the when completed
addPending
Add the peers to the pending and attempted sets.- Parameters:
pending- the peers to add
addPending
Add the peer to the pending and attempted sets.- Parameters:
peer- the peer to add
removePending
We didn't actually want to add this peer as part of the pending list...- Parameters:
peer- the peer to take back out of the pending and attempted sets
dataFound
How long it took to get the reply, or -1 if we don't know.- Parameters:
peer- the peer that answered with data for this search- Returns:
- the milliseconds between request and reply, or -1 if the request was never recorded
replyFound
How long it took to get the reply, or -1 if we don't know.- Parameters:
peer- the peer that answered this search without data- Returns:
- the milliseconds between request and reply, or -1 if the request was never recorded
getRepliedPeers
replyTimeout
Move the peer from pending to failed on timeout.- Parameters:
peer- the peer that timed out
addLeaseSetResponse
getNewestLeaseSet
LeaseSet with the newest lease date among the responses.- Returns:
- the newest lease set
addInitialLeaseSetResponse
shouldStoreInitial
public boolean shouldStoreInitial()Whether enough initial responses have been received to store.- Returns:
- whether store initial
getBestInitialLeaseSet
LeaseSet with the newest lease date among the initial responses.- Returns:
- the best initial lease set
clearInitialTracking
public void clearInitialTracking()Clear the initial response tracking state.getStoredLeaseDate
public long getStoredLeaseDate()Stored lease date of the last accepted LeaseSet.- Returns:
- the stored lease date
setStoredLeaseDate
public void setStoredLeaseDate(long date) Record the stored lease date.- Parameters:
date- the new stored lease date
shouldUpdateStored
Whether the given LeaseSet is newer than the stored one.- Parameters:
ls- the LeaseSet to compare- Returns:
- whether update stored
toString