Skip to content
Merged
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 @@ -40,7 +40,9 @@
* latency sample of at least {@link #FAILURE_PENALTY_MILLIS} — this is what keeps a
* fast failing node unattractive: finishing quickly can never make its score smaller
* than the failure floor, and a clean node recovers because subsequent successful
* samples dominate the smoothing.</p>
* samples dominate the smoothing. Neutrality also covers timing: a 4xx refreshes the
* activity timestamp used for retention, but never the effective-sample timestamp
* that drives the decay of the next latency-bearing observation.</p>
*
* <p>The map is bounded: once it holds more than {@link #PRUNE_THRESHOLD} entries a
* selection prunes the identities whose last feedback is older than
Expand Down Expand Up @@ -184,12 +186,16 @@ Stats statsOf(final UpstreamIdentity identity) {
* 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
* @param lastFeedbackMillis receipt time of the last feedback of any class;
* activity bookkeeping for retention and pruning only
* @param lastSampleMillis receipt time of the last latency-bearing observation;
* the decay of the next sample is measured from here, so
* a neutral 4xx cannot reshape the next latency update
*/
record Stats(double ewmaLatency, long samples, long lastFeedbackMillis) {
record Stats(double ewmaLatency, long samples, long lastFeedbackMillis, long lastSampleMillis) {

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

/**
Expand All @@ -204,12 +210,13 @@ long ewmaLatencyMillis() {

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(ewmaLatency, samples, nowMillis);
// neutral: the observation says nothing about the upstream; only the
// activity timestamp moves, the effective-sample timing stays intact
return new Stats(ewmaLatency, samples, nowMillis, lastSampleMillis);
}
long latency = latencyOf(elapsedMillis, outcome);
double updated = samples <= 0L ? latency : smooth(latency, nowMillis - lastFeedbackMillis);
return new Stats(updated, samples + 1L, nowMillis);
double updated = samples <= 0L ? latency : smooth(latency, nowMillis - lastSampleMillis);
return new Stats(updated, samples + 1L, nowMillis, nowMillis);
}

private double smooth(final long latency, final long sinceLastMillis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -117,6 +121,116 @@ void clientErrorObservationsAreNeutral() {
assertTrue(after.lastFeedbackMillis() >= before.lastFeedbackMillis());
}

@Test
void clientErrorDoesNotReshapeTheNextLatencyUpdate() {
Upstream node = upstream("node:8080", 50);
AdaptiveUpstreamStats normal = new AdaptiveUpstreamStats();
AdaptiveUpstreamStats with4xx = new AdaptiveUpstreamStats();
normal.record(node.identity(), 1000L, AdaptiveOutcome.SERVER_ERROR, 0L);
with4xx.record(node.identity(), 1000L, AdaptiveOutcome.SERVER_ERROR, 0L);
with4xx.record(node.identity(), 1L, AdaptiveOutcome.CLIENT_ERROR, 999L);
normal.record(node.identity(), 50L, AdaptiveOutcome.SUCCESS, 1000L);
with4xx.record(node.identity(), 50L, AdaptiveOutcome.SUCCESS, 1000L);

assertEquals(normal.statsOf(node.identity()).ewmaLatencyMillis(),
with4xx.statsOf(node.identity()).ewmaLatencyMillis(),
"an unrelated 4xx must not change the decay of the next latency-bearing sample");
assertEquals(normal.statsOf(node.identity()).samples(), with4xx.statsOf(node.identity()).samples());
}

@Test
void manyClientErrorsLeaveSubsequentUpdatesEquivalent() {
Upstream node = upstream("node:8080", 50);
AdaptiveUpstreamStats normal = new AdaptiveUpstreamStats();
AdaptiveUpstreamStats with4xx = new AdaptiveUpstreamStats();
for (int time = 0; time <= 5_000; time += 100) {
normal.record(node.identity(), 50L, AdaptiveOutcome.SUCCESS, time);
with4xx.record(node.identity(), 50L, AdaptiveOutcome.SUCCESS, time);
for (int i = 1; i <= 5; i++) {
with4xx.record(node.identity(), 1L, AdaptiveOutcome.CLIENT_ERROR, time + i);
}
}

assertEquals(normal.statsOf(node.identity()).ewmaLatencyMillis(),
with4xx.statsOf(node.identity()).ewmaLatencyMillis(),
"any number of interleaved 4xx observations must leave the latency path equivalent");
assertEquals(normal.statsOf(node.identity()).samples(), with4xx.statsOf(node.identity()).samples());
}

@Test
void clientErrorAloneNeverWarmsAColdTarget() {
Upstream node = upstream("cold:8080", 50);
Upstream warm = upstream("warm:8080", 50);
feed(balancer, warm, AdaptiveOutcome.SUCCESS, 10L, 20);
for (int i = 0; i < 20; i++) {
balancer.onFeedback(node, 1L, AdaptiveOutcome.CLIENT_ERROR);
}

AdaptiveUpstreamStats.Stats stats = balancer.stats().statsOf(node.identity());
assertEquals(0L, stats.samples(), "a 4xx must never count as a latency sample");
assertTrue(balancer.stats().score(node.identity(), 1L, 50).isEmpty(),
"a target seen only through 4xx must stay cold");
assertFalse(balancer.stats().bothAdaptive(node.identity(), warm.identity()));
}

@Test
void identityFirstSeenThroughClientErrorStartsFromTheFirstRealSample() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("node:8080", 50).identity();
stats.record(identity, 1L, AdaptiveOutcome.CLIENT_ERROR, 0L);
stats.record(identity, 50L, AdaptiveOutcome.SUCCESS, 1000L);

AdaptiveUpstreamStats.Stats record = stats.statsOf(identity);
assertEquals(1L, record.samples());
assertEquals(50L, record.ewmaLatencyMillis(),
"the first real sample must be taken as-is, not decayed from the 4xx timestamp");
}

@Test
void clientErrorActivityStillCountsForRetention() {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity retained = upstream("retained:8080", 50).identity();
stats.record(retained, 10L, AdaptiveOutcome.SUCCESS, 0L);
stats.record(retained, 1L, AdaptiveOutcome.CLIENT_ERROR, AdaptiveUpstreamStats.IDLE_MILLIS);
for (int i = 0; i < AdaptiveUpstreamStats.PRUNE_THRESHOLD + 10; i++) {
stats.record(upstream("filler-" + i + ":8080", 50).identity(), 10L, AdaptiveOutcome.SUCCESS, 0L);
}

stats.pruneIfNeeded(AdaptiveUpstreamStats.IDLE_MILLIS + 1L);
assertNotNull(stats.statsOf(retained),
"a recent 4xx keeps the identity alive for retention even without a new latency sample");
}

@Test
void concurrentFeedbackKeepsSampleAccountingConsistent() throws Exception {
AdaptiveUpstreamStats stats = new AdaptiveUpstreamStats();
UpstreamIdentity identity = upstream("node:8080", 50).identity();
int workers = 4;
int perWorker = 250;
ExecutorService pool = Executors.newFixedThreadPool(workers);
try {
for (int w = 0; w < workers; w++) {
final int worker = w;
pool.submit(() -> {
for (int i = 0; i < perWorker; i++) {
long now = (long) worker * perWorker + i;
stats.record(identity, 50L, AdaptiveOutcome.SUCCESS, now);
stats.record(identity, 1L, AdaptiveOutcome.CLIENT_ERROR, now);
}
});
}
pool.shutdown();
assertTrue(pool.awaitTermination(30, TimeUnit.SECONDS));
} finally {
pool.shutdownNow();
}

AdaptiveUpstreamStats.Stats record = stats.statsOf(identity);
assertEquals((long) workers * perWorker, record.samples(),
"only latency-bearing observations may count, regardless of interleaving");
assertTrue(Double.isFinite(record.ewmaLatency()) && record.ewmaLatency() >= 0.0);
}

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