Class SloSampleStore

java.lang.Object
org.mockserver.slo.SloSampleStore

public class SloSampleStore extends Object
Process-wide store of recent request samples used to evaluate SloCriteria into an SloVerdict. Mirrors the singleton + reset() pattern of ServiceChaosRegistry / ChaosAutoHaltMonitor.

Each sample is one upstream round-trip (v1 records only Scope.FORWARD traffic): its wall-clock end time, latency, whether it was an error (status null or >= 500), the Scope, and the upstream host. Samples are bounded two ways:

  • By count — at most sloWindowMaxSamples (default 50 000); the oldest is evicted when full.
  • By age — samples older than sloWindowRetentionMillis (default 600 000 ms) relative to the newest recorded sample are evicted lazily on record and on query.

When sloTrackingEnabled is false (the default), record(long, long, boolean, org.mockserver.slo.Scope, java.lang.String) is a no-op so the forward hot path pays nothing. Queries always work against whatever samples exist.

Latency percentiles use the same algorithm as org.mockserver.mock.drift.PercentileTracker: sort the in-window latencies ascending and take the ceil(p/100 * n) - 1 index (clamped).

  • Constructor Details

    • SloSampleStore

      public SloSampleStore()
  • Method Details

    • getInstance

      public static SloSampleStore getInstance()
    • record

      public void record(long epochMillis, long latencyMillis, boolean isError, Scope scope, String host)
      Record one sample. No-op when sloTrackingEnabled is false so the forward hot path pays nothing when the feature is off.
      Parameters:
      epochMillis - the sample's wall-clock end time (epoch millis)
      latencyMillis - upstream round-trip latency in milliseconds
      isError - true when the response was an error (status null or >= 500)
      scope - which traffic the sample belongs to (v1 records Scope.FORWARD)
      host - the upstream host label, may be null
    • latenciesInWindow

      public long[] latenciesInWindow(long fromEpochMillis, long toEpochMillis, Scope scope, Set<String> hosts)
      Returns:
      the in-window, in-scope, in-host latencies (ascending order ready for percentile selection), or an empty array when none match.
    • errorCountsInWindow

      public SloSampleStore.ErrorCounts errorCountsInWindow(long fromEpochMillis, long toEpochMillis, Scope scope, Set<String> hosts)
      Returns:
      error and total sample counts for the in-window, in-scope, in-host samples.
    • percentile

      public static Double percentile(long[] ascendingLatencies, int pct)
      The latency percentile (0-100) over the supplied ascending latencies, using the same algorithm as PercentileTracker.percentile. Returns null when there are no samples (so the caller can mark the objective INCONCLUSIVE rather than reporting a misleading zero).
    • size

      public int size()
      The total number of samples currently held (across all scopes/hosts).
    • reset

      public void reset()
      Clears all samples. Called on server reset and for test isolation.