java.lang.Object
net.i2p.router.peermanager.TunnelHistory

public class TunnelHistoryextends Object
Tunnel related history information.
  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the tunnel history.
    • _lifetimeAgreedTo

      private final AtomicLong _lifetimeAgreedTo
      Build requests we agreed to, lifetime.
    • _lifetimeRejected

      private final AtomicLong _lifetimeRejected
      Build requests we rejected, lifetime.
    • _lifetimeTimedOut

      private final AtomicLong _lifetimeTimedOut
      Builds that timed out, lifetime.
    • _lastAgreedTo

      private volatile long _lastAgreedTo
      When we last agreed to a build request.
    • _lastRejectedCritical

      private volatile long _lastRejectedCritical
      When we last rejected for a critical reason.
    • _lastRejectedBandwidth

      private volatile long _lastRejectedBandwidth
      When we last rejected for bandwidth.
    • _lastRejectedTransient

      private volatile long _lastRejectedTransient
      When we last rejected for a transient reason.
    • _lastRejectedProbabalistic

      private volatile long _lastRejectedProbabalistic
      When we last rejected probabilistically.
    • _lastTimedOut

      private volatile long _lastTimedOut
      When a build last timed out.
    • _lifetimeFailed

      private final AtomicLong _lifetimeFailed
      Tunnels that failed, lifetime.
    • _lastFailed

      private volatile long _lastFailed
      When a tunnel last failed.
    • _lastTestedSuccessfully

      private volatile long _lastTestedSuccessfully
      When a tunnel last passed a test.
    • _rejectRate

      private final RateStat _rejectRate
      Rate stat for the rejections.
    • _failRate

      private final RateStat _failRate
      Rate stat for the failures.
    • _lastCoalesce

      private volatile long _lastCoalesce
      When the history was last written.
    • RATES

      static final long[] RATES
      Rate periods for history tracking.
    • TUNNEL_REJECT_PROBABALISTIC_REJECT

      public static final int TUNNEL_REJECT_PROBABALISTIC_REJECT
      Probabalistic tunnel rejection due to a flood of requests - infrequent.
      See Also:
    • TUNNEL_REJECT_TRANSIENT_OVERLOAD

      public static final int TUNNEL_REJECT_TRANSIENT_OVERLOAD
      Tunnel rejection due to temporary cpu/job/tunnel overload - rare.
      See Also:
    • TUNNEL_REJECT_BANDWIDTH

      public static final int TUNNEL_REJECT_BANDWIDTH
      Tunnel rejection due to excess bandwidth usage - used for most rejections even if not really for bandwidth.
      See Also:
    • TUNNEL_REJECT_CRIT

      public static final int TUNNEL_REJECT_CRIT
      Tunnel rejection due to system failure - not currently used.
      See Also:
    • TUNNEL_REJECT_STREAMING_NACK

      public static final int TUNNEL_REJECT_STREAMING_NACK
      Streaming NACK - peer accepted tunnel but can't deliver data.
      See Also:
    • DECAY_INTERVAL_MS

      static final long DECAY_INTERVAL_MS
      How often the lifetime counters decay, as a whole interval.

      Eight hours, not fifteen minutes. At 3/4 per interval the shorter period made a peer forget its own record faster than it could accumulate one: 50 accepted builds fell to 16 within an hour and under 1 within four, so a *good* history decayed exactly as fast as a bad one. Eight hours gives a half-life near 19 hours, so an established record takes about 128 hours to fall to 1% and a peer's last few days of behaviour dominate its ratio.

      See Also:
    • DECAY_NUMERATOR

      private static final long DECAY_NUMERATOR
      Numerator of the rejection rate decay.
      See Also:
    • DECAY_DENOMINATOR

      private static final long DECAY_DENOMINATOR
      Denominator of the rejection rate decay.
      See Also:
    • NL

      private static final String NL
      The platform line separator.
    • HR

      private static final String HR
      Comment rule line of the history file.
      See Also:
  • Constructor Details

    • TunnelHistory

      public TunnelHistory(RouterContext context, String statGroup)
      Creates the per-peer reject and fail rate stats.
      Parameters:
      context - the router context this history is attached to
      statGroup - the statistics group the rate stats are filed under
  • Method Details

    • getLifetimeAgreedTo

      public long getLifetimeAgreedTo()
      Total tunnels the peer has agreed to participate in.
      Returns:
      the count since the last decay, applied every 8 hours by decayLifetimeCounters(long)
    • getLifetimeRejected

      public long getLifetimeRejected()
      Total tunnels the peer has refused to participate in (does not include timeouts).
      Returns:
      the count since the last decay, applied every 8 hours by decayLifetimeCounters(long)
    • getLifetimeTimedOut

      public long getLifetimeTimedOut()
      Total tunnel builds to this peer that timed out without a response.
      Returns:
      the count since the last decay, applied every 8 hours by decayLifetimeCounters(long)
    • getLifetimeFailed

      public long getLifetimeFailed()
      Total tunnels the peer has agreed to participate in that were later marked as failed prematurely.
      Returns:
      the count since the last decay, applied every 8 hours by decayLifetimeCounters(long)
    • getLastAgreedTo

      public long getLastAgreedTo()
      When the peer last agreed to participate in a tunnel.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never agreed
    • getLastRejectedCritical

      public long getLastRejectedCritical()
      When the peer last refused to participate in a tunnel with level of critical.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never rejected at this severity
    • getLastRejectedBandwidth

      public long getLastRejectedBandwidth()
      When the peer last refused to participate in a tunnel complaining of bandwidth overload.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never rejected at this severity
    • getLastRejectedTransient

      public long getLastRejectedTransient()
      When the peer last refused to participate in a tunnel complaining of transient overload.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never rejected at this severity
    • getLastRejectedProbabalistic

      public long getLastRejectedProbabalistic()
      When the peer last refused to participate in a tunnel probabalistically.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never rejected at this severity
    • getLastTimedOut

      public long getLastTimedOut()
      When the peer last timed out without responding to a tunnel request.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never timed out
    • getLastFailed

      public long getLastFailed()
      When the last tunnel the peer participated in failed.
      Returns:
      milliseconds since the epoch, or 0 if no participating tunnel has failed
    • getLastTestedSuccessfully

      public long getLastTestedSuccessfully()
      When the peer last passed a tunnel test.
      Returns:
      milliseconds since the epoch, or 0 if the peer has never passed a tunnel test
    • incrementTestSucceeded

      public void incrementTestSucceeded()
      Record that a tunnel test through this peer succeeded. This indicates the peer is actively participating in working tunnels.
    • getAcceptanceRatio

      public double getAcceptanceRatio()
      Calculate the ratio of accepted to total tunnel requests. Only explicit rejections are counted; timeouts are tracked separately via getLifetimeTimedOut() and the time-windowed getRejectionRate() RateStat.
      Returns:
      ratio (0.0 to 1.0), or 1.0 if no data available
    • incrementProcessed

      public void incrementProcessed(int processedSuccessfully, int failedProcessing)
      Processed message counter; legacy method, no longer tracked.
      Parameters:
      processedSuccessfully - the number of successfully processed messages, ignored
      failedProcessing - the number of messages that failed to process, ignored
    • incrementAgreedTo

      public void incrementAgreedTo()
      Count a tunnel participation agreement and record its time.
    • incrementRejected

      public void incrementRejected(int severity)
      Count a tunnel rejection at the given severity and record its time.
      Parameters:
      severity - how much the peer doesnt want to participate in the tunnel (large == more severe)
    • incrementTimedOut

      public void incrementTimedOut()
      Count a tunnel build timeout (peer did not respond in time). Timeouts are tracked separately from rejections because a peer that is slow to respond is different from a peer that actively refuses. Timeouts do NOT count toward getLifetimeRejected() and therefore do not affect getAcceptanceRatio().
    • incrementFailed

      public void incrementFailed(int pct)
      Define this rate as the probability it really failed.
      Parameters:
      pct - = probability * 100
    • getRejectionRate

      public RateStat getRejectionRate()
      Rejection rate statistics for this peer.
      Returns:
      the rejection rate
    • getFailedRate

      public RateStat getFailedRate()
      Failure rate statistics for this peer.
      Returns:
      the failed rate
    • decayLifetimeCounters

      public void decayLifetimeCounters(long now)
      Apply the lifetime-counter decay if its interval has elapsed.

      Deliberately does not coalesce the RateStats. Those are folded by coalesceStats() on the caller's own cadence, and folding them again here would advance their windows twice as often as the statistic's consumers expect. A caller that only wants the decay — the tier reorganise walking every candidate — can call this cheaply: one volatile read and one comparison, with real work only on the pass that actually crosses the interval.

      Must be called with the write lock held when called from the reorganise, so the tier membership being read and the counters being decayed are consistent.

      Parameters:
      now - current time in ms
    • coalesceStats

      public void coalesceStats()
      Coalesce the rate statistics and periodically decay lifetime counters.

      Lifetime counters (_lifetimeAgreedTo, _lifetimeRejected, _lifetimeTimedOut, _lifetimeFailed) are decayed at 75% per 8 hours (matching DBHistory). This prevents unbounded growth across restarts and ensures stale history does not permanently block peers from tiers. Recency is handled by the RateStats (10-minute and 1-hour windows) and by selection gates.

    • decayCounter

      private void decayCounter(AtomicLong counter, String name)
      Apply decay to a counter. 3/4 per 8 h = ~1% after 128 hours.
      Parameters:
      counter - the lifetime counter to scale down, left alone when already zero
      name - the counter's name, for the debug log
    • store

      public void store(OutputStream out) throws IOException
      Write the tunnel history to the stream, including comments.
      Parameters:
      out - the stream to write the properties-formatted history to
      Throws:
      IOException - if the stream rejects a write
    • store

      public void store(OutputStream out, boolean addComments) throws IOException
      Write the data from the profile to the stream.
      Parameters:
      out - the stream to write the properties-formatted history to
      addComments - add comment lines to the output
      Throws:
      IOException - if the stream rejects a write
    • addDate

      private static void addDate(StringBuilder buf, boolean addComments, String name, long val, String description)
      Appends the date line to the history file.
      Parameters:
      buf - where the line is appended
      addComments - true to write the line as a comment
      name - the statistic name
      val - the statistic value, used as the date
      description - the text shown before the date
    • add

      private static void add(StringBuilder buf, boolean addComments, String name, long val, String description)
      Appends one statistic to the history file.
      Parameters:
      buf - where the line is appended
      addComments - true to write the line as a comment
      name - the statistic name
      val - the statistic value
      description - the text shown with the value
    • load

      public void load(Properties props)
      Restore the tunnel history fields from the given properties.
      Parameters:
      props - the persisted properties previously written by store(OutputStream, boolean)
    • getLong

      private static final long getLong(Properties props, String key)
      Reads a long property with a default of zero.
      Parameters:
      props - the properties read
      key - the property name
      Returns:
      the value, zero if unparseable