net.i2p.router.networkdb.kademlia
Class KademliaNetworkDatabaseFacade

java.lang.Object
  extended by net.i2p.router.NetworkDatabaseFacade
      extended by net.i2p.router.networkdb.kademlia.KademliaNetworkDatabaseFacade
All Implemented Interfaces:
Service
Direct Known Subclasses:
FloodfillNetworkDatabaseFacade

public class KademliaNetworkDatabaseFacade
extends NetworkDatabaseFacade

Kademlia based version of the network database. Never instantiated directly; see FloodfillNetworkDatabaseFacade.


Field Summary
protected  RouterContext _context
           
protected  Log _log
           
protected  PeerSelector _peerSelector
           
static String DEFAULT_DB_DIR
           
protected static long DONT_FAIL_PERIOD
          for the 10 minutes after startup, don't fail db entries so that if we were offline for a while, we'll have a chance of finding some live peers with the previous references
(package private) static int MAX_EXPLORE_QUEUE
           
(package private) static long MAX_LEASE_FUTURE
          Don't let leaseSets go 20 minutes into the future
protected static int MIN_REMAINING_ROUTERS
          if we have less than this many routers left, don't drop any more, even if they're failing or doing bad stuff.
(package private) static int MIN_RESEED
          Reseed if below this.
static String PROP_DB_DIR
           
protected static long PUBLISH_JOB_DELAY
          this needs to be long enough to give us time to start up, but less than 20m (when we start accepting tunnels and could be a IBGW) Actually no, we need this soon if we are a new router or other routers have forgotten about us, else we can't build IB exploratory tunnels.
 
Constructor Summary
KademliaNetworkDatabaseFacade(RouterContext context)
           
 
Method Summary
protected  void createHandlers()
          unused, see override
protected  PeerSelector createPeerSelector()
           
(package private)  void dropAfterLookupFailed(Hash peer)
          Final remove for a router info.
 void fail(Hash dbEntry)
          Final remove for a leaseset.
(package private)  void failPermanently(Destination dest)
          Negative cache until restart
 Set<Hash> findNearestRouters(Hash key, int maxNumRouters, Set<Hash> peersToIgnore)
          Get the routers closest to that key in response to a remote lookup Only used by ../HDLMJ Set MAY INCLUDE our own router - add to peersToIgnore if you don't want
 Set<Hash> getAllRouters()
          get the hashes for all known routers
(package private)  DataStore getDataStore()
           
(package private)  String getDbDir()
           
 Set<Hash> getExploreKeys()
           
(package private)  KBucketSet<Hash> getKBuckets()
           
protected  int getKBucketSetSize()
          This is fast and doesn't use synchronization, but it includes both routerinfos and leasesets.
 int getKnownLeaseSets()
          This is only used by StatisticsManager to publish the count if we are floodfill.
 int getKnownRouters()
          This used to return the number of routers that were in both the kbuckets AND the data store, which was fine when the kbuckets held everything.
(package private)  long getLastExploreNewDate()
           
 long getLastRouterInfoPublishTime()
          The last time we successfully published our RI.
 Set<LeaseSet> getLeases()
          public for NetDbRenderer in routerconsole
 PeerSelector getPeerSelector()
           
 int getPeerTimeout(Hash peer)
          todo: does this need more tuning?
 Set<RouterInfo> getRouters()
          public for NetDbRenderer in routerconsole
 boolean isInitialized()
           
(package private)  boolean isNegativeCached(Hash key)
          Is the key in the negative lookup cache? &
 boolean isNegativeCachedForever(Hash key)
          Is it permanently negative cached?
protected  void lookupBeforeDropping(Hash peer, RouterInfo info)
          don't use directly - see F.N.D.F.
 void lookupDestination(Hash key, Job onFinishedJob, long timeoutMs, Hash fromLocalDest)
          Lookup using the client's tunnels Succeeds even if LS validation and store fails due to unsupported sig type, expired, etc.
 Destination lookupDestinationLocally(Hash key)
          Lookup locally in netDB and in badDest cache Succeeds even if LS validation fails due to unsupported sig type, expired, etc.
(package private)  void lookupFailed(Hash key)
          Increment in the negative lookup cache
 void lookupLeaseSet(Hash key, Job onFindJob, Job onFailedLookupJob, long timeoutMs)
          Lookup using exploratory tunnels.
 void lookupLeaseSet(Hash key, Job onFindJob, Job onFailedLookupJob, long timeoutMs, Hash fromLocalDest)
          Lookup using the client's tunnels Use lookupDestination() if you don't need the LS or need it validated.
 LeaseSet lookupLeaseSetLocally(Hash key)
          Use lookupDestination() if you don't need the LS or need it validated.
 DatabaseEntry lookupLocally(Hash key)
           
(package private)  DatabaseEntry lookupLocallyWithoutValidation(Hash key)
          Not for use without validation
 void lookupRouterInfo(Hash key, Job onFindJob, Job onFailedLookupJob, long timeoutMs)
           
 RouterInfo lookupRouterInfoLocally(Hash key)
           
 void publish(LeaseSet localLeaseSet)
           
 void publish(RouterInfo localRouterInfo)
          Stores to local db only.
 void queueForExploration(Collection<Hash> keys)
           
 void removeFromExploreKeys(Collection<Hash> toRemove)
           
 void renderStatusHTML(Writer out)
          Debug info, HTML formatted
 void rescan()
           
 ReseedChecker reseedChecker()
           
 void restart()
          Perform a soft restart.
(package private)  void routerInfoPublishSuccessful()
          Set the last time we successfully published our RI.
(package private)  SearchJob search(Hash key, Job onFindJob, Job onFailedLookupJob, long timeoutMs, boolean isLease)
          Begin a kademlia style search for the key specified, which can take up to timeoutMs and will fire the appropriate jobs on success or timeout (or if the kademlia search completes without any match) Unused - called only by FNDF.searchFull() from FloodSearchJob which is overridden - don't use this.
(package private)  SearchJob search(Hash key, Job onFindJob, Job onFailedLookupJob, long timeoutMs, boolean isLease, Hash fromLocalDest)
          Unused - see FNDF
(package private)  void searchComplete(Hash key)
          The search for the given key is no longer active
 void sendStore(Hash key, DatabaseEntry ds, Job onSuccess, Job onFailure, long sendTimeout, Set<Hash> toIgnore)
          unused (overridden in FNDF)
(package private)  void setLastExploreNewDate(long when)
           
 void shutdown()
          Instruct the service that the router is shutting down and that it should do whatever is necessary to go down gracefully.
 void startup()
          Instruct the service that it should start normal operation.
(package private)  void stopPublishing(Hash target)
           
 LeaseSet store(Hash key, LeaseSet leaseSet)
          Store the leaseSet.
 RouterInfo store(Hash key, RouterInfo routerInfo)
          Store the routerInfo.
(package private)  RouterInfo store(Hash key, RouterInfo routerInfo, boolean persist)
          Store the routerInfo.
 void unpublish(LeaseSet localLeaseSet)
           
(package private)  String validate(RouterInfo routerInfo)
          Determine whether this routerInfo will be accepted as valid and current given what we know now.
 
Methods inherited from class net.i2p.router.NetworkDatabaseFacade
floodfillEnabled, store
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

protected final Log _log

_peerSelector

protected final PeerSelector _peerSelector

_context

protected final RouterContext _context

DONT_FAIL_PERIOD

protected static final long DONT_FAIL_PERIOD
for the 10 minutes after startup, don't fail db entries so that if we were offline for a while, we'll have a chance of finding some live peers with the previous references

See Also:
Constant Field Values

PROP_DB_DIR

public static final String PROP_DB_DIR
See Also:
Constant Field Values

DEFAULT_DB_DIR

public static final String DEFAULT_DB_DIR
See Also:
Constant Field Values

MIN_RESEED

static final int MIN_RESEED
Reseed if below this.

Since:
0.9.4
See Also:
Constant Field Values

MIN_REMAINING_ROUTERS

protected static final int MIN_REMAINING_ROUTERS
if we have less than this many routers left, don't drop any more, even if they're failing or doing bad stuff. As of 0.9.4, we make this LOWER than the min for reseeding, so a reseed will be forced if necessary.

See Also:
Constant Field Values

PUBLISH_JOB_DELAY

protected static final long PUBLISH_JOB_DELAY
this needs to be long enough to give us time to start up, but less than 20m (when we start accepting tunnels and could be a IBGW) Actually no, we need this soon if we are a new router or other routers have forgotten about us, else we can't build IB exploratory tunnels. Unused.

See Also:
Constant Field Values

MAX_EXPLORE_QUEUE

static final int MAX_EXPLORE_QUEUE
See Also:
Constant Field Values

MAX_LEASE_FUTURE

static final long MAX_LEASE_FUTURE
Don't let leaseSets go 20 minutes into the future

See Also:
Constant Field Values
Constructor Detail

KademliaNetworkDatabaseFacade

public KademliaNetworkDatabaseFacade(RouterContext context)
Method Detail

searchComplete

void searchComplete(Hash key)
The search for the given key is no longer active


isInitialized

public boolean isInitialized()
Overrides:
isInitialized in class NetworkDatabaseFacade

createPeerSelector

protected PeerSelector createPeerSelector()

getPeerSelector

public PeerSelector getPeerSelector()

reseedChecker

public ReseedChecker reseedChecker()
Overrides:
reseedChecker in class NetworkDatabaseFacade
Since:
0.9

getKBuckets

KBucketSet<Hash> getKBuckets()

getDataStore

DataStore getDataStore()

getLastExploreNewDate

long getLastExploreNewDate()

setLastExploreNewDate

void setLastExploreNewDate(long when)

getExploreKeys

public Set<Hash> getExploreKeys()
Returns:
unmodifiable set

removeFromExploreKeys

public void removeFromExploreKeys(Collection<Hash> toRemove)

queueForExploration

public void queueForExploration(Collection<Hash> keys)

shutdown

public void shutdown()
Description copied from interface: Service
Instruct the service that the router is shutting down and that it should do whatever is necessary to go down gracefully. It should not depend on other components at this point. This call DOES block.


restart

public void restart()
Description copied from interface: Service
Perform a soft restart.


rescan

public void rescan()
Overrides:
rescan in class NetworkDatabaseFacade

getDbDir

String getDbDir()

startup

public void startup()
Description copied from interface: Service
Instruct the service that it should start normal operation. This call DOES block until the service is ready.


createHandlers

protected void createHandlers()
unused, see override


findNearestRouters

public Set<Hash> findNearestRouters(Hash key,
                                    int maxNumRouters,
                                    Set<Hash> peersToIgnore)
Get the routers closest to that key in response to a remote lookup Only used by ../HDLMJ Set MAY INCLUDE our own router - add to peersToIgnore if you don't want

Specified by:
findNearestRouters in class NetworkDatabaseFacade
Parameters:
key - the real key, NOT the routing key
peersToIgnore - can be null
maxNumRouters - The maximum number of routers to return

getAllRouters

public Set<Hash> getAllRouters()
get the hashes for all known routers

Specified by:
getAllRouters in class NetworkDatabaseFacade

getKnownRouters

public int getKnownRouters()
This used to return the number of routers that were in both the kbuckets AND the data store, which was fine when the kbuckets held everything. But now that is probably not what you want. Just return the count in the data store.

Overrides:
getKnownRouters in class NetworkDatabaseFacade

getKnownLeaseSets

public int getKnownLeaseSets()
This is only used by StatisticsManager to publish the count if we are floodfill. So to hide a clue that a popular eepsite is hosted on a floodfill router, only count leasesets that are "received as published", as of 0.7.14

Overrides:
getKnownLeaseSets in class NetworkDatabaseFacade

getKBucketSetSize

protected int getKBucketSetSize()
This is fast and doesn't use synchronization, but it includes both routerinfos and leasesets. Use it to avoid deadlocks. No - not true - the KBS contains RIs only.


lookupLocally

public DatabaseEntry lookupLocally(Hash key)
Specified by:
lookupLocally in class NetworkDatabaseFacade
Returns:
RouterInfo, LeaseSet, or null, validated
Since:
0.8.3

lookupLocallyWithoutValidation

DatabaseEntry lookupLocallyWithoutValidation(Hash key)
Not for use without validation

Returns:
RouterInfo, LeaseSet, or null, NOT validated
Since:
0.9.9

lookupLeaseSet

public void lookupLeaseSet(Hash key,
                           Job onFindJob,
                           Job onFailedLookupJob,
                           long timeoutMs)
Lookup using exploratory tunnels. Use lookupDestination() if you don't need the LS or need it validated.

Specified by:
lookupLeaseSet in class NetworkDatabaseFacade

lookupLeaseSet

public void lookupLeaseSet(Hash key,
                           Job onFindJob,
                           Job onFailedLookupJob,
                           long timeoutMs,
                           Hash fromLocalDest)
Lookup using the client's tunnels Use lookupDestination() if you don't need the LS or need it validated.

Specified by:
lookupLeaseSet in class NetworkDatabaseFacade
Parameters:
fromLocalDest - use these tunnels for the lookup, or null for exploratory
Since:
0.9.10

lookupLeaseSetLocally

public LeaseSet lookupLeaseSetLocally(Hash key)
Use lookupDestination() if you don't need the LS or need it validated.

Specified by:
lookupLeaseSetLocally in class NetworkDatabaseFacade

lookupDestination

public void lookupDestination(Hash key,
                              Job onFinishedJob,
                              long timeoutMs,
                              Hash fromLocalDest)
Lookup using the client's tunnels Succeeds even if LS validation and store fails due to unsupported sig type, expired, etc. Note that there are not separate success and fail jobs. Caller must call lookupDestinationLocally() in the job to determine success.

Specified by:
lookupDestination in class NetworkDatabaseFacade
Parameters:
onFinishedJob - non-null
fromLocalDest - use these tunnels for the lookup, or null for exploratory
Since:
0.9.16

lookupDestinationLocally

public Destination lookupDestinationLocally(Hash key)
Lookup locally in netDB and in badDest cache Succeeds even if LS validation fails due to unsupported sig type, expired, etc.

Specified by:
lookupDestinationLocally in class NetworkDatabaseFacade
Since:
0.9.16

lookupRouterInfo

public void lookupRouterInfo(Hash key,
                             Job onFindJob,
                             Job onFailedLookupJob,
                             long timeoutMs)
Specified by:
lookupRouterInfo in class NetworkDatabaseFacade

lookupRouterInfoLocally

public RouterInfo lookupRouterInfoLocally(Hash key)
Specified by:
lookupRouterInfoLocally in class NetworkDatabaseFacade

publish

public void publish(LeaseSet localLeaseSet)
             throws IllegalArgumentException
Specified by:
publish in class NetworkDatabaseFacade
Throws:
IllegalArgumentException - if the leaseSet is not valid

stopPublishing

void stopPublishing(Hash target)

publish

public void publish(RouterInfo localRouterInfo)
             throws IllegalArgumentException
Stores to local db only. Overridden in FNDF to actually send to the floodfills.

Specified by:
publish in class NetworkDatabaseFacade
Throws:
IllegalArgumentException - if the local router info is invalid

routerInfoPublishSuccessful

void routerInfoPublishSuccessful()
Set the last time we successfully published our RI.

Since:
0.9.9

getLastRouterInfoPublishTime

public long getLastRouterInfoPublishTime()
The last time we successfully published our RI.

Overrides:
getLastRouterInfoPublishTime in class NetworkDatabaseFacade
Since:
0.9.9

store

public LeaseSet store(Hash key,
                      LeaseSet leaseSet)
               throws IllegalArgumentException
Store the leaseSet. If the store fails due to unsupported crypto, it will negative cache the hash until restart.

Specified by:
store in class NetworkDatabaseFacade
Returns:
previous entry or null
Throws:
IllegalArgumentException - if the leaseSet is not valid
UnsupportedCryptoException - if that's why it failed.

validate

String validate(RouterInfo routerInfo)
          throws IllegalArgumentException
Determine whether this routerInfo will be accepted as valid and current given what we know now. Call this before each use, to check expiration

Returns:
reason why the entry is not valid, or null if it is valid
Throws:
IllegalArgumentException
Since:
0.9.7

store

public RouterInfo store(Hash key,
                        RouterInfo routerInfo)
                 throws IllegalArgumentException
Store the routerInfo. If the store fails due to unsupported crypto, it will banlist the router hash until restart and then throw UnsupportedCrytpoException.

Specified by:
store in class NetworkDatabaseFacade
Returns:
previous entry or null
Throws:
IllegalArgumentException - if the routerInfo is not valid
UnsupportedCryptoException - if that's why it failed.

store

RouterInfo store(Hash key,
                 RouterInfo routerInfo,
                 boolean persist)
           throws IllegalArgumentException
Store the routerInfo. If the store fails due to unsupported crypto, it will banlist the router hash until restart and then throw UnsupportedCrytpoException.

Returns:
previous entry or null
Throws:
IllegalArgumentException - if the routerInfo is not valid
UnsupportedCryptoException - if that's why it failed.

fail

public void fail(Hash dbEntry)
Final remove for a leaseset. For a router info, will look up in the network before dropping.

Specified by:
fail in class NetworkDatabaseFacade

lookupBeforeDropping

protected void lookupBeforeDropping(Hash peer,
                                    RouterInfo info)
don't use directly - see F.N.D.F. override


dropAfterLookupFailed

void dropAfterLookupFailed(Hash peer)
Final remove for a router info. Do NOT use for leasesets.


unpublish

public void unpublish(LeaseSet localLeaseSet)
Specified by:
unpublish in class NetworkDatabaseFacade

search

SearchJob search(Hash key,
                 Job onFindJob,
                 Job onFailedLookupJob,
                 long timeoutMs,
                 boolean isLease)
Begin a kademlia style search for the key specified, which can take up to timeoutMs and will fire the appropriate jobs on success or timeout (or if the kademlia search completes without any match) Unused - called only by FNDF.searchFull() from FloodSearchJob which is overridden - don't use this.

Throws:
UnsupportedOperationException - always

search

SearchJob search(Hash key,
                 Job onFindJob,
                 Job onFailedLookupJob,
                 long timeoutMs,
                 boolean isLease,
                 Hash fromLocalDest)
Unused - see FNDF

Throws:
UnsupportedOperationException - always
Since:
0.9.10

getLeases

public Set<LeaseSet> getLeases()
public for NetDbRenderer in routerconsole

Overrides:
getLeases in class NetworkDatabaseFacade

getRouters

public Set<RouterInfo> getRouters()
public for NetDbRenderer in routerconsole

Overrides:
getRouters in class NetworkDatabaseFacade

getPeerTimeout

public int getPeerTimeout(Hash peer)
todo: does this need more tuning?


sendStore

public void sendStore(Hash key,
                      DatabaseEntry ds,
                      Job onSuccess,
                      Job onFailure,
                      long sendTimeout,
                      Set<Hash> toIgnore)
unused (overridden in FNDF)


lookupFailed

void lookupFailed(Hash key)
Increment in the negative lookup cache

Parameters:
key - for Destinations or RouterIdentities
Since:
0.9.4 moved from FNDF to KNDF in 0.9.16

isNegativeCached

boolean isNegativeCached(Hash key)
Is the key in the negative lookup cache? &

Parameters:
key - for Destinations or RouterIdentities
Since:
0.9.4 moved from FNDF to KNDF in 0.9.16

failPermanently

void failPermanently(Destination dest)
Negative cache until restart

Since:
0.9.16

isNegativeCachedForever

public boolean isNegativeCachedForever(Hash key)
Is it permanently negative cached?

Overrides:
isNegativeCachedForever in class NetworkDatabaseFacade
Parameters:
key - only for Destinations; for RouterIdentities, see Banlist
Since:
0.9.16

renderStatusHTML

public void renderStatusHTML(Writer out)
                      throws IOException
Debug info, HTML formatted

Specified by:
renderStatusHTML in interface Service
Overrides:
renderStatusHTML in class NetworkDatabaseFacade
Throws:
IOException
Since:
0.9.10