Responsibilities: - Maintains `_peersByCapability` index (character → Set of Hash) for quick capability lookups - Runs Reorg on SimpleTimer2 which calls ProfileOrganizer.reorganize() at adaptive intervals (30s early uptime → 90s medium → 250s steady state) - Periodically stores profiles to disk via ProfilePersistenceHelper (every 15 min) - Loads stored profiles from disk on startup in a background thread - Selects peers for PeerTestJob via selectPeers(criteria) — PURPOSE_TEST only
Profile persistence: - Only Tier 1 (Active) and Tier 2 (Passive) profiles are written to disk - Gossip-only (Tier 3) profiles are kept in memory but never persisted - Profiles older than EXPIRE_AGE (7 days) are deleted after store.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classRequests RouterInfos for stored profiles whose peer the netdb cannot resolve.private classLoad the profiles and instantiate Reorg, waiting for 60s uptime to allow the routerinfos to load first.private classTimer reorganizing the peer profiles.private classReorganize the profiles.Field Summary
Modifier and TypeFieldDescriptionValue strings are lower case.private final RouterContextThe router context.private booleanAlternates the half of the peers evicted.private longWhen the profiles were last stored.private final LogLogger for the peer manager.private final ProfileOrganizerThe organizer holding the peer profiles.Peer hashes, by capability flag.private final ProfilePersistenceHelperHelper reading and writing the profiles.private final AtomicBooleanTrue while the profiles are being stored.private static final longTimeout for a RouterInfo lookup issued on behalf of a stored profile whose peer the netdb cannot resolve, in ms.private static final longAge after which a profile is dropped.private static final longInterval between profile reorganizations.(package private) static final longWe don't want this much longer than the average connect time, as the CapacityCalculator now includes connection as a factor.private static final longReorganization interval on slow routers.(package private) static final intAfter first two hours of uptime ~= 246.private static final longInterval between profile stores.static final StringCapability letters the manager indexes its per-capability peer sets by.Constructor Summary
ConstructorsConstructorDescriptionPeerManager(RouterContext context) Profiles are now loaded in a separate thread, so this should return quickly.Method Summary
Modifier and TypeMethodDescription(package private) voidDrops every tracked peer profile and the capability index built from them, for a router that is not going to reuse this state.intcountPeersByCapability(char capability) How many tracked peers advertise a capability.getPeersByCapability(char capability) Every tracked peer advertising a capability.private booleanisLowBandwidthPeer(Hash peer) Is this peer in a bandwidth tier too low to be worth remembering across a restart?(package private) voidThis may take a long time - 30 seconds or more.private voidLoad the profiles in a separate thread, so we don't spend forever in the constructor (slowing down the Router constructor viaRouterContext.initAll()).locked_getPeers(char c) Locking no longer required.voidremoveCapabilities(Hash peer) Remove all capabilities for the peer from the index.All tracked peers from the organizer.selectPeers(PeerSelectionCriteria criteria) Find some peers that meet the criteria and we have the netDb info for locally.selectTestPeersNonBlocking(int needed) Non-blocking test peer selection.voidsetCapabilities(Hash peer, String caps) Records the capability string a peer advertised and reindexes it in the per-capability lookup, dropping any capability it no longer holds.private booleanstoreProfile(Hash peer, long cutoff) Save a peer's profile to disk if it is still usable.(package private) voidWrite active profiles to disk.
Field Details
_log
Logger for the peer manager._context
The router context._organizer
The organizer holding the peer profiles._persistenceHelper
Helper reading and writing the profiles._peersByCapability
_capabilitiesByPeer
_storeLock
True while the profiles are being stored._lastStore
private volatile long _lastStoreWhen the profiles were last stored._evictToggle
private boolean _evictToggleAlternates the half of the peers evicted.REORGANIZE_TIME
private static final long REORGANIZE_TIMEInterval between profile reorganizations.- See Also:
REORGANIZE_TIME_MEDIUM
private static final long REORGANIZE_TIME_MEDIUMReorganization interval on slow routers.- See Also:
REORGANIZE_TIME_LONG
static final long REORGANIZE_TIME_LONGWe don't want this much longer than the average connect time, as the CapacityCalculator now includes connection as a factor. This must also be less than 10 minutes, which is the shortest Rate contained in the profile, as the Rates must be coalesced.- See Also:
REORGANIZES_PER_DAY
static final int REORGANIZES_PER_DAYAfter first two hours of uptime ~= 246.- See Also:
STORE_TIME
EXPIRE_AGE
private static final long EXPIRE_AGEAge after which a profile is dropped.- See Also:
ADDRESS_REFRESH_TIMEOUT_MS
private static final long ADDRESS_REFRESH_TIMEOUT_MSTimeout for a RouterInfo lookup issued on behalf of a stored profile whose peer the netdb cannot resolve, in ms.- See Also:
TRACKED_CAPS
Capability letters the manager indexes its per-capability peer sets by.- See Also:
Constructor Details
PeerManager
Profiles are now loaded in a separate thread, so this should return quickly.- Parameters:
context- the router context, whose profile organizer and log manager seed the peer indexes
Method Details
storeProfiles
void storeProfiles()Write active profiles to disk.clearProfiles
void clearProfiles()Drops every tracked peer profile and the capability index built from them, for a router that is not going to reuse this state.selectPeers
storeProfile
Save a peer's profile to disk if it is still usable.- Parameters:
peer- the peer whose profile may be written to diskcutoff- only store if most recent activity is newer than this (absolute time)- Returns:
- true if the profile was written to disk, false if it was skipped
isLowBandwidthPeer
Is this peer in a bandwidth tier too low to be worth remembering across a restart?K, L, M and Unknown are the tiers a peer advertises when it cannot or will not commit bandwidth. Their profiles are still tracked in memory, where they matter to selection and to the fast/high-cap tiers; what is not worth the disk is the persisted history of a peer whose RouterInfo already tells us it will not host a tunnel usefully.
A missing or unreadable RouterInfo is not treated as low bandwidth: absence of evidence is not evidence, and refusing to persist a peer we simply have not looked up yet would discard the very profiles a floodfill lookup would then have to rebuild.
- Parameters:
peer- hash of the peer to classify- Returns:
- true if the peer advertises K, L, M or Unknown bandwidth
loadProfilesInBackground
private void loadProfilesInBackground()Load the profiles in a separate thread, so we don't spend forever in the constructor (slowing down the Router constructor viaRouterContext.initAll()). This also instantiates Reorg, so only call this once.loadProfiles
void loadProfiles()This may take a long time - 30 seconds or more.selectPeers
Find some peers that meet the criteria and we have the netDb info for locally. Returned list will not include ourselves.Only supports PURPOSE_TEST — used exclusively by
PeerTestJob. Other purpose constants were removed; add new purposes as needed.- Parameters:
criteria- supplies the selection purpose and the minimum number of peers to gather before falling back- Returns:
- up to the requested number of peer hashes, excluding ourselves; the list may be shorter or empty if too few peers are known
selectTestPeersNonBlocking
Non-blocking test peer selection. Returns null if the read lock cannot be acquired (reorganize in progress). Used exclusively byPeerTestJobto avoid blocking the job queue thread.- Parameters:
needed- how many peers to gather at most- Returns:
- selected peers, or null if lock not acquired
setCapabilities
locked_getPeers
removeCapabilities
Remove all capabilities for the peer from the index.- Parameters:
peer- the router to drop from every capability bucket
getPeersByCapability
countPeersByCapability
public int countPeersByCapability(char capability) How many tracked peers advertise a capability.- Parameters:
capability- the advertised capability letter, matched case-insensitively- Returns:
- the number of tracked peers advertising it, 0 if none