Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
*
* <p>Each upstream identity holds one immutable statistics record that is replaced
* atomically on every feedback. The record carries a smoothed latency in the same
* EWMA family as the p2c trigger. Feedback classification follows
* EWMA family as the p2c trigger. The smoothing keeps double precision internally
* (in milliseconds): truncating every update to whole milliseconds would discard
* sub-millisecond increments at short feedback intervals and could pin the estimate
* at its old value forever. Rounding happens only where a long leaves the record,
* namely {@link Stats#ewmaLatencyMillis()} and the ceiling of {@link #score}. Feedback
* classification follows
* {@link AdaptiveOutcome}: a 4xx is neutral (the observation says nothing about the
* upstream), while a 5xx or a transport failure enters the smoothing as a synthetic
* latency sample of at least {@link #FAILURE_PENALTY_MILLIS} — this is what keeps a
Expand All @@ -56,6 +61,13 @@ public class AdaptiveUpstreamStats {
*/
public static final long EWMA_WINDOW_MILLIS = 600L;

/**
* Minimum decay interval in millis applied to same-timestamp observations: a
* zero gap would give the new sample zero weight and discard it permanently,
* so concurrent completions are blended with at least this much evidence.
*/
public static final long MIN_DECAY_INTERVAL_MILLIS = 1L;

/**
* Synthetic latency floor for a failed attempt: a 5xx or transport failure costs
* the caller at least a retry, so it is smoothed in with this latency even when
Expand Down Expand Up @@ -110,7 +122,7 @@ public OptionalLong score(final UpstreamIdentity identity, final long inflight,
if (Objects.isNull(current) || current.samples() < MIN_SAMPLES) {
return OptionalLong.empty();
}
double score = (current.ewmaLatencyMillis() + 1) * (inflight + 1)
double score = (current.ewmaLatency() + 1) * (inflight + 1)
/ (double) Math.max(effectiveWeight, 1);
return OptionalLong.of((long) Math.ceil(score));
}
Expand Down Expand Up @@ -168,29 +180,42 @@ Stats statsOf(final UpstreamIdentity identity) {
/**
* One immutable statistics snapshot.
*
* @param ewmaLatencyMillis smoothed latency of latency-bearing attempts
* @param ewmaLatency smoothed latency of latency-bearing attempts, kept as a
* double in millis so sub-millisecond increments survive
* short feedback intervals; always finite and non-negative
* @param samples number of latency-bearing observations
* @param lastFeedbackMillis receipt time of the last feedback of any class
*/
record Stats(long ewmaLatencyMillis, long samples, long lastFeedbackMillis) {
record Stats(double ewmaLatency, long samples, long lastFeedbackMillis) {

static Stats first(final long elapsedMillis, final AdaptiveOutcome outcome, final long nowMillis) {
return new Stats(latencyOf(elapsedMillis, outcome), samplesOf(outcome), nowMillis);
}

/**
* The smoothed latency rounded to whole millis; the internal double keeps
* the unrounded estimate for the next update and for scoring.
*
* @return the smoothed latency in millis, rounded to the nearest long
*/
long ewmaLatencyMillis() {
return Math.round(ewmaLatency);
}

Stats next(final long elapsedMillis, final AdaptiveOutcome outcome, final long nowMillis) {
if (outcome == AdaptiveOutcome.CLIENT_ERROR) {
// neutral: the observation says nothing about the upstream
return new Stats(ewmaLatencyMillis, samples, nowMillis);
return new Stats(ewmaLatency, samples, nowMillis);
}
long latency = latencyOf(elapsedMillis, outcome);
long updated = samples <= 0L ? latency : smooth(latency, nowMillis - lastFeedbackMillis);
double updated = samples <= 0L ? latency : smooth(latency, nowMillis - lastFeedbackMillis);
return new Stats(updated, samples + 1L, nowMillis);
}

private long smooth(final long latency, final long sinceLastMillis) {
double w = Math.exp((double) -Math.max(sinceLastMillis, 0L) / (double) EWMA_WINDOW_MILLIS);
return (long) ((double) ewmaLatencyMillis * w + (double) latency * (1.0 - w));
private double smooth(final long latency, final long sinceLastMillis) {
long interval = Math.max(sinceLastMillis, MIN_DECAY_INTERVAL_MILLIS);
double w = Math.exp((double) -interval / (double) EWMA_WINDOW_MILLIS);
return ewmaLatency * w + (double) latency * (1.0 - w);
}

private static long latencyOf(final long elapsedMillis, final AdaptiveOutcome outcome) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,59 @@ void cleanSamplesPullTheLatencyBackBelowTheFailureFloor() {
"with a 1s gap the p=0.19 weighting recovers within a few samples");
}

@Test
void sustainedSlowerSamplesConvergeDespiteShortFeedbackIntervals() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("latency:8080", 50).identity();
stats.record(identity, 1L, AdaptiveOutcome.SUCCESS, 0L);
for (int time = 1; time <= 10_000; time++) {
stats.record(identity, 100L, AdaptiveOutcome.SUCCESS, time);
}

long smoothed = stats.statsOf(identity).ewmaLatencyMillis();
assertTrue(smoothed >= 99L && smoothed <= 100L,
"10,000 sustained 100 ms samples spaced 1 ms apart must converge toward 100 ms, got " + smoothed);
}

@Test
void sameTimestampObservationsStillMoveTheEstimate() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("node:8080", 50).identity();
stats.record(identity, 1L, AdaptiveOutcome.SUCCESS, 0L);
for (int i = 0; i < 2_000; i++) {
stats.record(identity, 100L, AdaptiveOutcome.SUCCESS, 0L);
}

assertTrue(stats.statsOf(identity).ewmaLatencyMillis() > 1L,
"same-timestamp completions must blend with the minimum decay interval, not be discarded");
}

@Test
void decreasingLatencyPullsTheEstimateDownAtShortIntervals() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("node:8080", 50).identity();
stats.record(identity, 100L, AdaptiveOutcome.SUCCESS, 0L);
for (int time = 1; time <= 10_000; time++) {
stats.record(identity, 1L, AdaptiveOutcome.SUCCESS, time);
}

assertTrue(stats.statsOf(identity).ewmaLatencyMillis() <= 2L,
"sustained faster samples must pull the smoothing down even at 1 ms feedback intervals");
}

@Test
void failurePenaltySurvivesShortIntervalSmoothing() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("node:8080", 50).identity();
stats.record(identity, 1L, AdaptiveOutcome.SERVER_ERROR, 0L);
for (int time = 1; time <= 20; time++) {
stats.record(identity, 1L, AdaptiveOutcome.SERVER_ERROR, time);
}

assertEquals(AdaptiveUpstreamStats.FAILURE_PENALTY_MILLIS, stats.statsOf(identity).ewmaLatencyMillis(),
"the synthetic failure floor must hold while every sample is a fast failure");
}

@Test
void idleNodeKeepsTheForcedExploration() {
Upstream preferred = upstream("preferred:8080", 50);
Expand Down
Loading