Class AdaptiveBackoff

java.lang.Object
com.cockroachlabs.crdb.retry.AdaptiveBackoff

public final class AdaptiveBackoff extends Object
Computes backoff delays that adapt to observed contention.

The engine keeps a short rolling window of how many retries recent operations actually needed. When contention is high the base delay is lengthened (up to RetryPolicy.maxDelayMillis()); when the cluster is calm it shrinks back toward RetryPolicy.baseDelayMillis(). Jitter is drawn from an injectable DoubleSupplier, so the class is fully deterministic under test.

Not thread-safe on its own; the retry engine synchronizes access.

  • Constructor Details

    • AdaptiveBackoff

      public AdaptiveBackoff(RetryPolicy policy)
      Uses ThreadLocalRandom for jitter.
    • AdaptiveBackoff

      public AdaptiveBackoff(RetryPolicy policy, DoubleSupplier jitterSource)
      Parameters:
      policy - the retry policy driving base/max/multiplier/jitter
      jitterSource - supplies values in [0, 1); inject for deterministic tests
  • Method Details

    • record

      public void record(int retriesUsed)
      Feed the observed number of retries an operation needed. Drives the adaptive contention estimate.
    • contentionRatio

      public double contentionRatio()
      Contention estimate in [0, 1]: recent average retries relative to the policy's maxRetries.
    • nextDelayMillis

      public long nextDelayMillis(int attempt)
      The delay to wait before the given zero-based attempt. Guaranteed to lie within [0, maxDelayMillis], and (with zero jitter) to be non-decreasing in attempt.