From b96a3553e3592d0990deea33838e2ec578beabda Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Sat, 21 Nov 2026 00:58:26 -0500 Subject: [PATCH 1/6] Bound ConcurrentHashtable's bucket array by the heap createBounded allocated one bucket per entry of its cap, up front and for good, since the table never resizes. A table capped at 131,072 entries retained a 512 KiB array even while nearly empty, which on a small heap is out of proportion and, under G1, a humongous allocation. Size bounded tables by bucketCountFor(maxCapacity) instead: one bucket per entry for small tables, but at most 1/1024 of the maximum heap and below G1's humongous threshold, with a floor of 1,024 buckets. The cap is unchanged; chains carry any excess. A new overload takes an explicit bucket count for callers that know their expected population. Co-Authored-By: Claude Opus 5.5 --- .../trace/util/ConcurrentHashtable.java | 58 +++++++++- .../util/ConcurrentHashtableSizingTest.java | 102 ++++++++++++++++++ 2 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 components/toolbox/src/test/java/datadog/trace/util/ConcurrentHashtableSizingTest.java diff --git a/components/toolbox/src/main/java/datadog/trace/util/ConcurrentHashtable.java b/components/toolbox/src/main/java/datadog/trace/util/ConcurrentHashtable.java index f860b6ad458..0c5ad332c1a 100644 --- a/components/toolbox/src/main/java/datadog/trace/util/ConcurrentHashtable.java +++ b/components/toolbox/src/main/java/datadog/trace/util/ConcurrentHashtable.java @@ -183,7 +183,8 @@ private D1(State> state) { /** * Creates a fixed-size table holding at most {@code maxCapacity} entries. {@code entryClass} is * used only to infer {@code TEntry}; entries are created by the {@code creator} functions - * passed to the insertion methods. The table does not resize. + * passed to the insertion methods. The table does not resize; its bucket count is {@link + * ConcurrentHashtable#bucketCountFor(int)}. */ @Nonnull @SuppressWarnings("unchecked") @@ -526,7 +527,8 @@ private D2(State> state) { /** * Creates a fixed-size table holding at most {@code maxCapacity} entries. {@code entryClass} is * used only to infer {@code TEntry}; entries are created by the {@code creator} functions - * passed to the insertion methods. The table does not resize. + * passed to the insertion methods. The table does not resize; its bucket count is {@link + * ConcurrentHashtable#bucketCountFor(int)}. */ @Nonnull @SuppressWarnings("unchecked") @@ -1027,11 +1029,61 @@ private State(AtomicReferenceArray buckets, int maxCapacity) { /** * Creates a bucket array for {@code maxCapacity} entries and pairs it with a manager enforcing * that cap. {@code entryClass} is used only to infer {@code TEntry}. + * + *

The array has {@link #bucketCountFor(int) bucketCountFor(maxCapacity)} buckets: one per + * entry for small tables, but bounded by the heap for large ones (see {@link + * #bucketCountFor(int)}). */ @Nonnull public static > State createBounded( @Nonnull Class entryClass, int maxCapacity) { - return new State<>(createFixedBuckets(entryClass, maxCapacity), maxCapacity); + return createBounded(entryClass, maxCapacity, bucketCountFor(maxCapacity)); + } + + /** + * Creates a table of {@code bucketCount} buckets holding at most {@code maxCapacity} entries. + * Chains bear any excess of entries over buckets, so a table sized for its expected population + * still accepts up to its cap, with longer chains. {@code bucketCount} is rounded up to a power + * of two. + */ + @Nonnull + public static > State createBounded( + @Nonnull Class entryClass, int maxCapacity, int bucketCount) { + return new State<>(createFixedBuckets(entryClass, bucketCount), maxCapacity); + } + + /** The fewest buckets {@link #bucketCountFor(int)} limits a table to. */ + static final int MIN_HEAP_BOUNDED_BUCKETS = 1 << 10; + + /** + * Returns the bucket count {@link #createBounded(Class, int)} uses for {@code maxCapacity} + * entries: {@link #sizeFor(int) sizeFor(maxCapacity)}, but no more than the heap warrants. + * + *

The table never resizes, so its bucket array is allocated at full size up front and + * retained, even if the table stays nearly empty. For a large cap on a small heap that is both a + * fixed cost out of proportion to the heap and, under G1, a humongous allocation. So the array is + * kept to at most 1/1024 of the maximum heap and below G1's humongous threshold, and never cut + * below {@value #MIN_HEAP_BOUNDED_BUCKETS} buckets. A table filled past its bucket count still + * works, with chains longer than one. + */ + public static int bucketCountFor(int maxCapacity) { + return bucketCountFor(maxCapacity, Runtime.getRuntime().maxMemory()); + } + + static int bucketCountFor(int maxCapacity, long maxHeapBytes) { + return Math.min(sizeFor(maxCapacity), maxBucketsForHeap(maxHeapBytes)); + } + + static int maxBucketsForHeap(long maxHeapBytes) { + // G1 aims for about 2,048 regions, a power of two between 1 and 32 MiB, and allocates any + // object of half a region or more as humongous. Leave room for the array header. + final long region = + Math.max(1L << 20, Math.min(1L << 25, Long.highestOneBit(maxHeapBytes >>> 11))); + final long arrayBytes = Math.min(region / 2 - 64, maxHeapBytes >>> 10); + // Compressed references, the default below 32 GiB, take 4 bytes; otherwise 8. + final long referenceBytes = maxHeapBytes < (32L << 30) ? 4 : 8; + final long buckets = Long.highestOneBit(Math.max(1, arrayBytes / referenceBytes)); + return (int) Math.max(MIN_HEAP_BOUNDED_BUCKETS, Math.min(MAX_BUCKETS, buckets)); } /** Live entries in {@code state}; see {@link SizeManager#estimateSize()}. Lock-free. */ diff --git a/components/toolbox/src/test/java/datadog/trace/util/ConcurrentHashtableSizingTest.java b/components/toolbox/src/test/java/datadog/trace/util/ConcurrentHashtableSizingTest.java new file mode 100644 index 00000000000..e4935d30a2e --- /dev/null +++ b/components/toolbox/src/test/java/datadog/trace/util/ConcurrentHashtableSizingTest.java @@ -0,0 +1,102 @@ +package datadog.trace.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.annotation.Nonnull; +import org.junit.jupiter.api.Test; +import org.tabletest.junit.TableTest; + +/** Exercises how {@link ConcurrentHashtable#createBounded} sizes its bucket array. */ +class ConcurrentHashtableSizingTest { + private static final long MIB = 1L << 20; + + private static final class TestEntry extends ConcurrentHashtable.Entry { + final int value; + + TestEntry(int value) { + super(value); + this.value = value; + } + + @Override + public boolean matches(@Nonnull TestEntry other) { + return value == other.value; + } + } + + @TableTest({ + "scenario | heapMiB | buckets", + "1/1024 of a small heap | 16 | 4096 ", + "1/1024 of a 128 MiB heap | 128 | 32768 ", + "below the 1 MiB-region threshold | 1024 | 65536 ", + "below the 2 MiB-region threshold | 4096 | 131072 ", + "8-byte references from 32 GiB | 65536 | 1048576", + "below the 32 MiB-region cap | 1048576 | 1048576", + "never below the floor | 1 | 1024 " + }) + void boundsTheBucketArrayByTheHeap(final long heapMiB, final int buckets) { + assertEquals(buckets, ConcurrentHashtable.maxBucketsForHeap(heapMiB * MIB)); + } + + @Test + void anUnboundedHeapStillGetsAFiniteBound() { + assertEquals(1 << 20, ConcurrentHashtable.maxBucketsForHeap(Long.MAX_VALUE)); + } + + @Test + void smallTablesGetABucketPerEntry() { + assertEquals(8, ConcurrentHashtable.bucketCountFor(8, 128 * MIB)); + assertEquals(1024, ConcurrentHashtable.bucketCountFor(1000, 128 * MIB)); + } + + @Test + void largeTablesAreBoundedByTheHeap() { + assertEquals(32768, ConcurrentHashtable.bucketCountFor(131_072, 128 * MIB)); + assertEquals(131_072, ConcurrentHashtable.bucketCountFor(131_072, 4096 * MIB)); + } + + @Test + void theDefaultSizingUsesThisHeap() { + final int expected = + ConcurrentHashtable.bucketCountFor(1 << 20, Runtime.getRuntime().maxMemory()); + + assertEquals(expected, ConcurrentHashtable.bucketCountFor(1 << 20)); + assertEquals( + expected, ConcurrentHashtable.createBounded(TestEntry.class, 1 << 20).buckets.length()); + } + + @Test + void aTableWithFewerBucketsThanEntriesFillsToItsCap() { + final ConcurrentHashtable.State state = + ConcurrentHashtable.createBounded(TestEntry.class, 100, 5); + assertEquals(8, state.buckets.length()); + + for (int key = 0; key < 100; key++) { + try (ConcurrentHashtable.Reservation r = + ConcurrentHashtable.tryReserve(state, key)) { + assertNotNull(r.tryGetOrInsertOrNull(key, TestEntry::new), "key " + key); + } + } + assertTrue(ConcurrentHashtable.isFull(state)); + assertEquals(100, ConcurrentHashtable.estimateSize(state)); + + for (int key = 0; key < 100; key++) { + TestEntry found = null; + for (TestEntry e = ConcurrentHashtable.bucketFor(state.buckets, key); + e != null; + e = e.next()) { + if (e.value == key) { + found = e; + } + } + assertNotNull(found, "key " + key); + try (ConcurrentHashtable.Reservation r = + ConcurrentHashtable.tryReserve(state, key)) { + assertSame(found, r.tryGetOrInsertOrNull(key, TestEntry::new)); + } + } + } +} From a54a61d4351324a874f0beca7f41b8e5374a53df Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Sat, 21 Nov 2026 14:53:48 -0500 Subject: [PATCH 2/6] Aggregate flag evaluations on the evaluating thread Each flag evaluation used to build a FlagEvalEvent and offer it to a bounded 4,096-slot queue, which a single worker thread drained into the aggregator. Under load the worker could not keep up, and the queue dropped 30-67% of evaluations. Record evaluations straight into a ConcurrentFlagAggregator instead. An evaluation that lands in an existing bucket costs a lock-free chain scan and a few atomic updates and allocates nothing; a new bucket is inserted under the toolbox ConcurrentHashtable's write lock, which also enforces the tier caps. The tiers, caps and bucket keys are unchanged, so the payloads are the same. Buckets are drained in place: a bucket whose count goes from zero to one pushes itself onto a lock-free stack, and the flush reads and resets the counters of the buckets on it. An evaluation racing the flush is counted in this flush or the next one, never lost. Buckets idle for a whole window are swept out, freeing their slots under the caps. The worker now only flushes, every flush interval and at shutdown. Evaluations recorded after close are still counted as closed drops, and close() counts those that land after the final flush. The queue-overflow drop metric goes away with the queue. Co-Authored-By: Claude Opus 5.5 --- dd-openfeature/build.gradle.kts | 3 - .../FlagEvalHookHotPathBenchmark.java | 32 +- ...gEvaluationEnqueueContentionBenchmark.java | 189 ------ .../FlagEvaluationHotPathBenchmark.java | 94 ++- .../com/datadog/openfeature/DDEvaluator.java | 6 +- .../openfeature/FlagEvalLoggingHook.java | 43 +- .../internal/FeatureFlagsRuntime.java | 1 - .../ConcurrentFlagAggregator.java | 531 +++++++++++++++ .../flagevaluation/FlagEvalEvent.java | 127 ---- .../FlagEvaluationAggregator.java | 621 ------------------ .../FlagEvaluationPayloads.java | 28 +- .../FlagEvaluationPipeline.java | 401 +++-------- .../flagevaluation/FlagEvaluationWriter.java | 42 +- .../EvaluationTelemetryOptOutTest.java | 47 +- .../datadog/openfeature/ExposureHookTest.java | 22 +- .../openfeature/FlagEvalLoggingHookTest.java | 130 ++-- .../internal/FeatureFlagsRuntimeTest.java | 3 +- .../ConcurrentFlagAggregatorTest.java | 601 +++++++++++++++++ .../flagevaluation/FlagEvalEventTest.java | 24 - .../FlagEvaluationAggregatorTest.java | 529 --------------- .../FlagEvaluationAggregatorTiersTest.java | 114 ---- .../FlagEvaluationPayloadsTest.java | 136 ++-- .../FlagEvaluationPipelineTest.java | 292 ++++---- .../FlagEvaluationTestSupport.java | 50 +- .../flagevaluation/RecordedEvaluation.java | 115 ++++ 25 files changed, 1782 insertions(+), 2399 deletions(-) delete mode 100644 dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationEnqueueContentionBenchmark.java create mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java delete mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEvent.java delete mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregator.java create mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java delete mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEventTest.java delete mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTest.java delete mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTiersTest.java create mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/RecordedEvaluation.java diff --git a/dd-openfeature/build.gradle.kts b/dd-openfeature/build.gradle.kts index b1e08fc3707..b83a5d4d2ac 100644 --- a/dd-openfeature/build.gradle.kts +++ b/dd-openfeature/build.gradle.kts @@ -48,7 +48,6 @@ extra["excludedClassesBranchCoverage"] = listOf( "com.datadog.openfeature.internal.delivery.Retries", "com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache.Key", "com.datadog.openfeature.internal.exposure.ExposurePipeline.ExposureSerializingHandler", - "com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent", "com.datadog.openfeature.internal.flagevaluation.FlagEvaluationPipeline", ) extra["excludedClassesInstructionCoverage"] = listOf( @@ -61,8 +60,6 @@ extra["excludedClassesInstructionCoverage"] = listOf( "com.datadog.openfeature.internal.delivery.HttpCall", "com.datadog.openfeature.internal.delivery.HttpCall.CancellableBodySubscriber", "com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache.Sent", - "com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent", - "com.datadog.openfeature.internal.flagevaluation.FlagEvaluationPipeline.SerializingHandlerForTest", "datadog.trace.api.openfeature.Provider", ) diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/FlagEvalHookHotPathBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/FlagEvalHookHotPathBenchmark.java index 0d0a554a892..65e19fa3fe3 100644 --- a/dd-openfeature/src/jmh/java/com/datadog/openfeature/FlagEvalHookHotPathBenchmark.java +++ b/dd-openfeature/src/jmh/java/com/datadog/openfeature/FlagEvalHookHotPathBenchmark.java @@ -4,7 +4,6 @@ import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.SECONDS; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter; import dev.openfeature.sdk.FlagEvaluationDetails; import dev.openfeature.sdk.FlagValueType; @@ -37,15 +36,15 @@ * Evaluation-thread benchmark for the OpenFeature hook: the inline cost a caller actually pays * inside FlagEvalLoggingHook.finallyAfter, across context shapes. * - *

This is the counterpart to FlagEvaluationHotPathBenchmark, which measures the writer queue and - * the worker-thread aggregation. + *

This is the counterpart to FlagEvaluationHotPathBenchmark, which measures recording into the + * writer's aggregate. * *

The dominant inline cost under consent-on is DDEvaluator.copyPrunedContext: one bounded walk * of the caller-owned EvaluationContext. contextCopy isolates that walk so its share of * hookFinallyAfter is directly readable. Under consent-off the hook skips the copy entirely, which * hookFinallyAfterConsentOff measures as the protected-path floor. * - *

The writer used here is a no-op that discards events, so no queue or worker cost is included. + *

The writer used here is a no-op that discards evaluations, so no aggregation cost is included. * *

Run: {@code ./gradlew :dd-openfeature:jmh -Pjmh.includes=FlagEvalHookHotPathBenchmark}. */ @@ -95,13 +94,13 @@ public void setUp() { hook = new FlagEvalLoggingHook<>(() -> writer); } - /** Total inline cost under consent-on: scalar extraction, bounded context copy, and enqueue. */ + /** Total inline cost under consent-on: scalar extraction, bounded context copy, and record. */ @Benchmark public void hookFinallyAfter() { hook.finallyAfter(hookContext, consentOnDetails, Collections.emptyMap()); } - /** Protected-path floor: consent-off skips the context copy, leaving scalar work plus enqueue. */ + /** Protected-path floor: consent-off skips the context copy, leaving scalar work plus record. */ @Benchmark public void hookFinallyAfterConsentOff() { hook.finallyAfter(hookContext, consentOffDetails, Collections.emptyMap()); @@ -169,18 +168,19 @@ private static MutableContext addFlat(final MutableContext ctx, final int count) return ctx; } - /** Discards events so only hook-inline work is measured. */ + /** Discards evaluations so only hook-inline work is measured. */ private static final class NoOpWriter implements FlagEvaluationWriter { @Override - public void enqueue(final FlagEvalEvent event) {} - - @Override - public boolean hasCapacityForEnqueue() { - return true; - } - - @Override - public void countPreQueueOverflow() {} + public void record( + final String flagKey, + final String variant, + final String allocationKey, + final String targetingKey, + final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + final Map attrs, + final Map features) {} @Override public void countContextTruncated(final String reason) {} diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationEnqueueContentionBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationEnqueueContentionBenchmark.java deleted file mode 100644 index 55fde9906c6..00000000000 --- a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationEnqueueContentionBenchmark.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; - -import com.datadog.openfeature.internal.RuntimeServices; -import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; -import de.thetaphi.forbiddenapis.SuppressForbidden; -import java.util.HashMap; -import java.util.Map; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Group; -import org.openjdk.jmh.annotations.GroupThreads; -import org.openjdk.jmh.annotations.Level; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; - -/** - * Producer-contention benchmark for {@link FlagEvaluationPipeline#enqueue}. - * - *

Every flag evaluation in every application thread calls {@code enqueue}, so producer-side - * scaling is the property that matters. {@link FlagEvaluationHotPathBenchmark#writerEnqueue} - * measures the single-threaded cost and cannot see contention; this benchmark measures how enqueue - * behaves as producer count grows. - * - *

Why groups rather than {@code @Threads}: the hand-off queue ({@code - * EventQueue}) is multi-producer, single-consumer. Calling {@code poll()} from several - * threads breaks that contract, so producer threads cannot drain their own events. Each group - * therefore pairs N producers with exactly one consumer. - * - *

Why the consumer drains in batches: with more producers than consumers the - * queue would otherwise saturate, after which {@code offer} fast-fails and the measurement turns - * into {@code AtomicLong} contention on the overflow counter instead of the enqueue path. The - * consumer polls up to {@link #DRAIN_BATCH} events per invocation to keep the queue off its - * capacity limit. Overflow is reported at the end of each iteration so a saturated - and therefore - * invalid - run is visible rather than silent. - * - *

Run: {@code ./gradlew :dd-openfeature:jmh - * -Pjmh.includes=FlagEvaluationEnqueueContentionBenchmark}. - */ -@State(Scope.Benchmark) -@Warmup(iterations = 3, time = 2, timeUnit = SECONDS) -@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) -@BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(NANOSECONDS) -@Fork(value = 1) -public class FlagEvaluationEnqueueContentionBenchmark { - - /** Events the single consumer drains per invocation, so it can keep up with N producers. */ - static final int DRAIN_BATCH = 32; - - private static final int NUM_FLAGS = 100; - private static final int NUM_USERS = 50; - private static final int NUM_FIELDS = 10; - - private Map attrs; - private String[] flagKeys; - private String[] targetingKeys; - private FlagEvaluationPipeline writer; - - /** Per-producer cursor: a shared counter would add its own cache-line contention. */ - @State(Scope.Thread) - public static class ProducerCursor { - int cursor; - } - - @Setup(Level.Iteration) - public void setUp() { - attrs = new HashMap<>(); - for (int i = 0; i < NUM_FIELDS; i++) { - attrs.put("field" + i, "value"); - } - flagKeys = keys("bench-flag-", NUM_FLAGS); - targetingKeys = keys("bench-user-", NUM_USERS); - - // Capacity well above what the batch-draining consumer should ever let build up. - writer = - new FlagEvaluationPipeline( - 1 << 20, - Long.MAX_VALUE, - NANOSECONDS, - (route, json) -> {}, - new HashMap<>(), - new RuntimeServices(HealthMetrics.NOOP)); - } - - /** - * Reports queue overflow so a saturated run - where the consumer failed to keep up and the - * numbers no longer describe the enqueue path - is visible in the benchmark output. - */ - @org.openjdk.jmh.annotations.TearDown(Level.Iteration) - @SuppressForbidden - public void reportOverflow() { - final long dropped = writer.droppedQueueOverflow(); - if (dropped > 0) { - System.out.println( - "\nWARNING: queue overflowed " - + dropped - + " times - consumer could not keep up, enqueue timings for this iteration are" - + " measuring overflow accounting, not the enqueue path."); - } - } - - // ---- 1 producer: uncontended baseline ---- - - @Benchmark - @Group("producers1") - @GroupThreads(1) - public void enqueue1(final ProducerCursor c) { - writer.enqueue(nextEvent(c)); - } - - @Benchmark - @Group("producers1") - @GroupThreads(1) - public void drain1(final Blackhole blackhole) { - drain(blackhole); - } - - // ---- 4 producers ---- - - @Benchmark - @Group("producers4") - @GroupThreads(4) - public void enqueue4(final ProducerCursor c) { - writer.enqueue(nextEvent(c)); - } - - @Benchmark - @Group("producers4") - @GroupThreads(1) - public void drain4(final Blackhole blackhole) { - drain(blackhole); - } - - // ---- 16 producers ---- - - @Benchmark - @Group("producers16") - @GroupThreads(16) - public void enqueue16(final ProducerCursor c) { - writer.enqueue(nextEvent(c)); - } - - @Benchmark - @Group("producers16") - @GroupThreads(1) - public void drain16(final Blackhole blackhole) { - drain(blackhole); - } - - private void drain(final Blackhole blackhole) { - for (int i = 0; i < DRAIN_BATCH; i++) { - final FlagEvalEvent event = writer.pollQueuedEventForTest(); - if (event == null) { - break; - } - blackhole.consume(event); - } - } - - private FlagEvalEvent nextEvent(final ProducerCursor c) { - final int i = c.cursor++; - return new FlagEvalEvent( - flagKeys[Math.floorMod(i, flagKeys.length)], - "variant-" + Math.floorMod(i, 4), - "alloc-" + Math.floorMod(i, flagKeys.length), - targetingKeys[Math.floorMod(i, targetingKeys.length)], - null, - 1_700_000_000_000L + i, - attrs); - } - - private static String[] keys(final String prefix, final int count) { - final String[] out = new String[count]; - for (int i = 0; i < count; i++) { - out[i] = prefix + i; - } - return out; - } -} diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java index 56fe23f2518..052705f885f 100644 --- a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java +++ b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java @@ -4,8 +4,8 @@ import static java.util.concurrent.TimeUnit.SECONDS; import com.datadog.openfeature.internal.RuntimeServices; -import com.datadog.openfeature.internal.bridge.v1.EventTransport; import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.openjdk.jmh.annotations.Benchmark; @@ -19,25 +19,28 @@ import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; /** - * Writer-side benchmark for EVP {@code flagevaluation} recording: the bounded-queue enqueue and the - * worker-thread aggregation of already-captured events. + * Evaluating-thread benchmark for EVP {@code flagevaluation} recording: {@link + * FlagEvaluationPipeline#record} into the caller-side aggregate, on one thread and on several + * contending for the same buckets. * - *

Scope limitation: this benchmark starts from a pre-built FlagEvalEvent - * holding a ready-made flat attribute map. It therefore does NOT cover the OpenFeature hook's own - * inline capture cost - in particular the bounded evaluation-context copy that - * FlagEvalLoggingHook.finallyAfter performs synchronously on the evaluation thread under - * consent-on. That cost is measured separately by FlagEvalHookHotPathBenchmark in the root package, - * where the hook and the OpenFeature context types live. Do not read writerEnqueue as the total + *

Scope limitation: this benchmark starts from already-captured fields and a + * ready-made flat attribute map. It therefore does NOT cover the OpenFeature hook's own inline + * capture cost - in particular the bounded evaluation-context copy that + * FlagEvalLoggingHook.finallyAfter performs under consent-on. That cost is measured separately by + * FlagEvalHookHotPathBenchmark in the root package. Do not read these numbers as the total * per-evaluation cost paid by the caller. * - *

Events are built with observeFullEvaluationData=true so the aggregator actually hashes and - * compares the context. Under the consent-off default it drops attrs and skips that work, which + *

Evaluations are recorded with observeFullEvaluationData=true so the aggregator actually hashes + * and compares the context. Under the consent-off default it drops attrs and skips that work, which * makes every field-count profile measure the same scalar-only work. * + *

The worker is never started, so nothing drains: after the first pass over the keys every + * evaluation lands in an existing bucket, the steady state of a flush window. + * *

Run: {@code ./gradlew :dd-openfeature:jmh -Pjmh.includes=FlagEvaluationHotPathBenchmark}. */ @State(Scope.Benchmark) @@ -58,9 +61,13 @@ public class FlagEvaluationHotPathBenchmark { private Map attrs; private String[] flagKeys; private String[] targetingKeys; - private int cursor; private FlagEvaluationPipeline writer; - private FlagEvaluationPipeline.SerializingHandlerForTest handler; + + /** Per-thread cursor: a shared counter would add its own cache-line contention. */ + @State(Scope.Thread) + public static class Cursor { + int next; + } @Setup(Level.Iteration) public void setUp() { @@ -72,59 +79,50 @@ public void setUp() { } flagKeys = keys("bench-flag-", p.numFlags); targetingKeys = keys("bench-user-", p.numUsers); - cursor = 0; - final EventTransport transport = (route, json) -> {}; - final RuntimeServices services = new RuntimeServices(HealthMetrics.NOOP); final Map ddContext = new HashMap<>(); ddContext.put("service", "bench-service"); - handler = new FlagEvaluationPipeline.SerializingHandlerForTest(transport, ddContext, services); - - // Capacity large enough that the benchmark never overflows within a measurement window. writer = new FlagEvaluationPipeline( - 1 << 20, Long.MAX_VALUE, NANOSECONDS, transport, ddContext, services); + Long.MAX_VALUE, + NANOSECONDS, + (route, json) -> {}, + ddContext, + new RuntimeServices(HealthMetrics.NOOP)); } - /** - * Queue-mechanics cost only: event allocation plus the non-blocking bounded-queue offer. Excludes - * the hook's inline context snapshot - see the class javadoc. - */ + /** Uncontended cost of recording one evaluation. */ @Benchmark - public void writerEnqueue(final Blackhole blackhole) { - final FlagEvalEvent event = nextEvent(); - writer.enqueue(event); - blackhole.consume(writer.pollQueuedEventForTest()); - blackhole.consume(event); + @Threads(1) + public void record(final Cursor cursor) { + recordNext(cursor); } - /** Worker-thread cost: hash the context and aggregate. */ + /** Recording from several threads into the same buckets. */ @Benchmark - public void workerAggregate(final Blackhole blackhole) { - final FlagEvalEvent event = nextEvent(); - handler.aggregateEvent(event); - if ((cursor % 10_000) == 0) { - handler.clearAggregationForTest(); - } - blackhole.consume(handler.fullTierSizeForTest()); + @Threads(8) + public void recordContended(final Cursor cursor) { + recordNext(cursor); } - private FlagEvalEvent nextEvent() { - final int i = cursor++; - // observeFullEvaluationData=true is required for this benchmark to mean anything: under the - // consent-off default the aggregator drops attrs and never hashes the context, so - // every field-count profile would collapse to the same scalar-only cost. - return new FlagEvalEvent( - flagKeys[Math.floorMod(i, flagKeys.length)], - "variant-" + Math.floorMod(i, 4), - "alloc-" + Math.floorMod(i, flagKeys.length), + private void recordNext(final Cursor cursor) { + final int i = cursor.next++; + final int flag = Math.floorMod(i, flagKeys.length); + writer.record( + flagKeys[flag], + VARIANTS[Math.floorMod(i, VARIANTS.length)], + ALLOCATIONS[Math.floorMod(flag, ALLOCATIONS.length)], targetingKeys[Math.floorMod(i, targetingKeys.length)], null, 1_700_000_000_000L + i, true, - attrs); + attrs, + Collections.emptyMap()); } + private static final String[] VARIANTS = {"variant-0", "variant-1", "variant-2", "variant-3"}; + private static final String[] ALLOCATIONS = {"alloc-0", "alloc-1"}; + private static String[] keys(final String prefix, final int count) { final String[] out = new String[count]; for (int i = 0; i < count; i++) { diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java index fe32150a5b5..d3c8919742b 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java @@ -45,8 +45,8 @@ class DDEvaluator implements Evaluator, Consumer { /** * Maximum number of top-level context fields retained by copyPrunedContext. Bounds the width of - * the caller-supplied context and, transitively, the size of every FlagEvalEvent sitting in the - * async hand-off queue. Kept aligned with the cross-SDK RFC. + * the caller-supplied context and, transitively, the size of every full-tier bucket of the flag + * evaluation aggregate. Kept aligned with the cross-SDK RFC. */ static final int MAX_CONTEXT_FIELDS = 256; @@ -419,7 +419,7 @@ static final class CopyResult { /** * Single-pass bounded copy of a caller-owned EvaluationContext into the flattened, pruned map - * stored on a FlagEvalEvent and later canonicalized by the aggregator. + * recorded with a flag evaluation and kept by the aggregator. * *

Every retained-size dimension is capped inline so the hot path performs work proportional to * what is kept, never to what the caller supplied: MAX_CONTEXT_FIELDS - stop iterating the diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/FlagEvalLoggingHook.java b/dd-openfeature/src/main/java/com/datadog/openfeature/FlagEvalLoggingHook.java index 504ce4a3337..f66a0552ccc 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/FlagEvalLoggingHook.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/FlagEvalLoggingHook.java @@ -6,7 +6,6 @@ import static com.datadog.openfeature.internal.evaluation.MetadataKeys.HAS_EVALUATION_FEATURES; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.OBSERVE_FULL_EVALUATION_DATA; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter; import dev.openfeature.sdk.FlagEvaluationDetails; import dev.openfeature.sdk.Hook; @@ -20,8 +19,8 @@ * OpenFeature Hook that captures flag evaluation events for EVP flagevaluation emission. * *

Contract: finallyAfter does scalar metadata extraction, a single bounded copy of the - * evaluation context, and a non-blocking offer to the writer's bounded queue. Aggregation and - * posting are deferred to the writer's worker thread. + * evaluation context, and records the evaluation into the writer's lock-free aggregate. Posting is + * deferred to the writer's worker thread. * *

Hot-path cost: under consent-on (observeFullEvaluationData=true) DDEvaluator.copyPrunedContext * performs one bounded walk of the caller-owned EvaluationContext, applying every retained-size cap @@ -46,8 +45,8 @@ class FlagEvalLoggingHook implements Hook { } /** - * Capture + non-blocking enqueue only; no flattening, aggregation, or I/O. Runs at the finally - * stage so it covers success, error, and default-value paths. + * Capture and record only; no flattening or I/O. Runs at the finally stage so it covers success, + * error, and default-value paths. * *

The context snapshot taken here scales with context size/nesting - see the class javadoc for * why it cannot be deferred. @@ -66,14 +65,6 @@ public void finallyAfter( return; } - // Pre-queue guard: if the queue is already saturated, avoid the context-copy work whose - // result would be discarded on offer(). Best-effort — the queue can still be full at - // offer() time — but flips a full snapshot into an O(1) check on the drop path. - if (!w.hasCapacityForEnqueue()) { - w.countPreQueueOverflow(); - return; - } - // Scalar extraction - individual typed metadata reads, no JSON, no flattening final String flagKey = details.getFlagKey(); final ImmutableMetadata metadata = details.getFlagMetadata(); @@ -125,8 +116,9 @@ public void finallyAfter( final Map attrs; if (observeFullEvaluationData && ctx != null && ctx.getCtx() != null) { // Bounded copy of the caller's mutable context (see DDEvaluator.copyPrunedContext for - // every retained-size cap). Runs inline because the event is consumed asynchronously - // and the source context is caller-owned; work is proportional to what is retained. + // every retained-size cap). Runs inline because the aggregate keeps the context past + // this call and the source context is caller-owned; work is proportional to what is + // retained. final DDEvaluator.CopyResult copy = DDEvaluator.copyPrunedContext(ctx.getCtx()); if (copy.truncatedReason != null) { w.countContextTruncated(copy.truncatedReason); @@ -142,17 +134,16 @@ public void finallyAfter( Boolean.TRUE.equals(values.get(HAS_EVALUATION_FEATURES)) ? SplitFeatureMetadata.featuresWithPrefix(metadata, EVALUATION_FEATURE_PREFIX) : Collections.emptyMap(); - w.enqueue( - new FlagEvalEvent( - flagKey, - variant, - allocationKey, - targetingKey, - errorMessage, - evalTimeMs, - observeFullEvaluationData, - attrs, - features)); + w.record( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); } catch (LinkageError e) { // Never let EVP recording break flag evaluation } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java index 79b149f1b5a..544ce8122b6 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java @@ -189,7 +189,6 @@ static FeatureFlagsRuntime create( final FlagEvaluationPipeline evaluations = settings.isEvaluationCountsEnabled() ? new FlagEvaluationPipeline( - FlagEvaluationPipeline.DEFAULT_CAPACITY, FlagEvaluationPipeline.FLUSH_INTERVAL_SECONDS, TimeUnit.SECONDS, transport, diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java new file mode 100644 index 00000000000..8d0055329d3 --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java @@ -0,0 +1,531 @@ +package com.datadog.openfeature.internal.flagevaluation; + +import datadog.trace.util.ConcurrentHashtable; +import datadog.trace.util.LongHashingUtils; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import javax.annotation.Nullable; + +/** + * Aggregates flag evaluations into rows on the evaluating threads, for the worker to flush. + * + *

Tiers. A full-tier bucket is keyed by every dimension: flag, variant, allocation, + * error, subject, context, consent and features. Without consent the context is dropped on emit, so + * it neither splits buckets nor is stored. The full tier holds at most {@link #GLOBAL_CAP} buckets, + * and {@link #PER_FLAG_CAP} per flag. Past either cap an evaluation spills to the degraded tier, + * keyed without the subject, the context and the consent (degraded rows never carry them), and + * AND-folds the consent of the evaluations it merges. The degraded tier holds at most {@link + * #DEGRADED_CAP} buckets; past it an evaluation is dropped and counted in {@link + * #droppedDegradedOverflow}. + * + *

Recording. Each tier is a toolbox {@link ConcurrentHashtable}. An evaluation that lands + * in an existing bucket costs a lock-free chain scan and a few atomic updates, and allocates + * nothing. A new bucket is inserted under the table's write lock, which also enforces the tier's + * cap. The per-flag cap is checked before that lock, so racing inserts can overshoot it by a few + * buckets. + * + *

Drained in place. Buckets live across flushes. A bucket whose count goes from zero to + * one pushes itself onto a lock-free stack of the buckets touched since the last drain. {@link + * #drain} takes the whole stack and, for each bucket, reads and resets its counters. An evaluation + * racing the drain is never lost: if its increment lands before the reset, this drain emits it; if + * after, it finds the count at zero and pushes the bucket onto the next drain's stack. Only its + * timestamps can land in the other flush. A drain costs time in proportion to the buckets touched, + * not to the table, and busy buckets are not recreated every window. + * + *

Idle sweep. {@link #sweepIdle} removes the buckets idle for a whole window, which frees + * their slots under the caps. An evaluation that finds a bucket just as it is swept still counts, + * in the next drain; a later one creates a fresh bucket. + */ +final class ConcurrentFlagAggregator { + // Design assumptions: the scale the caps are sized for. + static final int EXPECTED_FLAG_COUNT = 2_500; + static final int EXPECTED_FULL_BUCKETS_PER_FLAG = 50; + static final int EXPECTED_USERS_PER_FLAG = 1_000; + static final int PER_FLAG_HEADROOM_MULTIPLIER = 10; + static final int EXPECTED_DEGRADED_BUCKETS_PER_FLAG = 10; + + // Derived sizing: the math behind the caps below. + static final int FULL_BUCKET_SIZING_BASIS = + EXPECTED_FLAG_COUNT * EXPECTED_FULL_BUCKETS_PER_FLAG; // 125_000 + static final int PER_FLAG_BUCKET_SIZING_BASIS = + PER_FLAG_HEADROOM_MULTIPLIER * EXPECTED_USERS_PER_FLAG; // 10_000 + static final int DEGRADED_BUCKET_SIZING_BASIS = + EXPECTED_FLAG_COUNT * EXPECTED_DEGRADED_BUCKETS_PER_FLAG; // 25_000 + + // Enforced bucket caps. + static final int GLOBAL_CAP = 131_072; // nearest power of two above FULL_BUCKET_SIZING_BASIS + static final int PER_FLAG_CAP = PER_FLAG_BUCKET_SIZING_BASIS; + static final int DEGRADED_CAP = 32_768; // nearest power of two above DEGRADED_BUCKET_SIZING_BASIS + + /** + * The most flags whose full-tier buckets are counted. Counts are never removed, so the + * evaluations of further flags go to the degraded tier. + */ + static final int FLAG_COUNT_CAP = 65_536; + + /** Receives each drained row, with the counters it had at the drain. */ + interface Rows { + void row(Bucket bucket, long count, long firstEvalMs, long lastEvalMs, boolean defaultUsed); + } + + private static final AtomicReferenceFieldUpdater DIRTY = + AtomicReferenceFieldUpdater.newUpdater(ConcurrentFlagAggregator.class, Bucket.class, "dirty"); + + final AtomicLong droppedDegradedOverflow = new AtomicLong(); + private final ConcurrentHashtable.State full = + ConcurrentHashtable.createBounded(Bucket.class, GLOBAL_CAP); + private final ConcurrentHashtable.State degraded = + ConcurrentHashtable.createBounded(Bucket.class, DEGRADED_CAP); + private final ConcurrentHashtable.D1 flagCounts = + ConcurrentHashtable.D1.createBounded(FlagCount.class, FLAG_COUNT_CAP); + + // The buckets touched since the last drain, linked through Bucket.dirtyNext. + @Nullable private volatile Bucket dirty; + // Written by the draining thread only; read when a bucket is created. + private volatile int drains; + + /** Records one evaluation, on the evaluating thread. */ + void record( + final String flagKey, + @Nullable final String variant, + @Nullable final String allocationKey, + @Nullable final String targetingKey, + @Nullable final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + @Nullable final Map attrs, + @Nullable final Map features) { + final boolean isDefault = variant == null; + // Without consent the context is dropped on emit, so it must not fragment buckets or be + // stored: a high-cardinality field (request_id, timestamp) would blow out PER_FLAG_CAP and + // spill every later evaluation into the degraded tier. + final Map context = observeFullEvaluationData ? attrs : null; + final long contextHash = observeFullEvaluationData ? contextHash(context) : 0L; + // Features come from the flag configuration, not the evaluation context, so they split + // buckets in both tiers and whatever the consent. + final Map featureMap = + features == null ? Collections.emptyMap() : features; + final long featuresHash = featureMap.hashCode(); + + final long fullHash = + fullHash( + flagKey, + variant, + allocationKey, + errorMessage, + targetingKey, + contextHash, + observeFullEvaluationData, + featuresHash); + for (Bucket b = ConcurrentHashtable.bucketFor(full, fullHash); b != null; b = b.next()) { + if (b.keyHash == fullHash + && b.matchesFull( + flagKey, + variant, + allocationKey, + errorMessage, + targetingKey, + context, + observeFullEvaluationData, + featureMap)) { + b.merge(this, evalTimeMs, isDefault); + return; + } + } + + final FlagCount flagCount = flagCounts.tryGetOrCreateOrNull(flagKey, FlagCount::new); + if (flagCount != null && flagCount.count.get() < PER_FLAG_CAP) { + final Bucket bucket = + new Bucket( + false, + fullHash, + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + context, + observeFullEvaluationData, + featureMap, + flagCount, + drains); + final Bucket inserted; + try (ConcurrentHashtable.Reservation reservation = + ConcurrentHashtable.tryReserve(full, fullHash)) { + inserted = reservation.tryGetOrInsertOrNull(bucket); + } + if (inserted != null) { + if (inserted == bucket) { + flagCount.count.incrementAndGet(); + } + inserted.merge(this, evalTimeMs, isDefault); + return; + } + // The full tier is at its global cap: fall through to the degraded tier. + } + + final long degradedHash = + degradedHash(flagKey, variant, allocationKey, errorMessage, featuresHash); + for (Bucket b = ConcurrentHashtable.bucketFor(degraded, degradedHash); + b != null; + b = b.next()) { + if (b.keyHash == degradedHash + && b.matchesDegraded(flagKey, variant, allocationKey, errorMessage, featureMap)) { + b.mergeDegraded(this, evalTimeMs, isDefault, observeFullEvaluationData); + return; + } + } + final Bucket bucket = + new Bucket( + true, + degradedHash, + flagKey, + variant, + allocationKey, + null, + errorMessage, + null, + observeFullEvaluationData, + featureMap, + null, + drains); + final Bucket inserted; + try (ConcurrentHashtable.Reservation reservation = + ConcurrentHashtable.tryReserve(degraded, degradedHash)) { + inserted = reservation.tryGetOrInsertOrNull(bucket); + } + if (inserted == null) { + droppedDegradedOverflow.incrementAndGet(); + } else { + inserted.mergeDegraded(this, evalTimeMs, isDefault, observeFullEvaluationData); + } + } + + private void push(final Bucket bucket) { + Bucket head; + do { + head = dirty; + bucket.dirtyNext = head; + } while (!DIRTY.compareAndSet(this, head, bucket)); + } + + /** Whether no evaluation has been recorded since the last drain. */ + boolean isEmpty() { + return dirty == null; + } + + /** + * Passes every bucket touched since the last drain to {@code rows}, with its counters, and resets + * them. One draining thread at a time. + * + *

A row that throws does not stop the drain: every touched bucket is still read and reset, so + * none is left counting without being on the stack. The first failure is rethrown at the end. + * + * @return the number of rows drained. + */ + int drain(final Rows rows) { + final int drain = drains + 1; + drains = drain; + int drained = 0; + RuntimeException failure = null; + Bucket b = DIRTY.getAndSet(this, null); + while (b != null) { + // Unlink before resetting the count: once it is zero, an evaluation can push the bucket + // again, and that push writes dirtyNext. + final Bucket next = b.dirtyNext; + b.dirtyNext = null; + final long count = Bucket.COUNT.getAndSet(b, 0); + final long first = Bucket.FIRST.getAndSet(b, Long.MAX_VALUE); + final long last = Bucket.LAST.getAndSet(b, Long.MIN_VALUE); + final boolean defaultUsed = Bucket.DEFAULT_USED.getAndSet(b, 0) != 0; + b.lastDrain = drain; + if (count > 0) { + drained++; + try { + rows.row(b, count, first, last, defaultUsed); + } catch (RuntimeException e) { + if (failure == null) { + failure = e; + } + } + } + b = next; + } + if (failure != null) { + throw failure; + } + return drained; + } + + /** + * Removes the buckets that saw no evaluation in the last whole window, freeing their slots under + * the caps. The draining thread only, after {@link #drain}. + * + * @return the number of buckets removed. + */ + int sweepIdle() { + final int drain = drains; + final int[] removed = {0}; + ConcurrentHashtable.removeIf( + full, + b -> { + if (b.isIdleSince(drain)) { + b.flagCount.count.decrementAndGet(); + removed[0]++; + return true; + } + return false; + }); + ConcurrentHashtable.removeIf( + degraded, + b -> { + if (b.isIdleSince(drain)) { + removed[0]++; + return true; + } + return false; + }); + return removed[0]; + } + + /** The number of buckets in both tiers, touched or not. */ + int bucketCount() { + return ConcurrentHashtable.estimateSize(full) + ConcurrentHashtable.estimateSize(degraded); + } + + static long fullHash( + final String flagKey, + final String variant, + final String allocationKey, + final String errorMessage, + final String targetingKey, + final long contextHash, + final boolean observeFullEvaluationData, + final long featuresHash) { + long hash = LongHashingUtils.hash(flagKey, variant, allocationKey, errorMessage, targetingKey); + hash = LongHashingUtils.addToHash(hash, contextHash); + hash = LongHashingUtils.addToHash(hash, observeFullEvaluationData); + return LongHashingUtils.addToHash(hash, featuresHash); + } + + static long degradedHash( + final String flagKey, + final String variant, + final String allocationKey, + final String errorMessage, + final long featuresHash) { + final long hash = LongHashingUtils.hash(flagKey, variant, allocationKey, errorMessage); + return LongHashingUtils.addToHash(hash, featuresHash); + } + + /** + * Hashes a context the way {@link #sameContext} compares it: independent of iteration order, and + * distinguishing value types (Integer 1, Long 1 and Double 1.0 hash and compare apart). + * + *

Entries are combined by addition, so the order the map yields them in does not matter; each + * entry is mixed first, so that swapping values between keys changes the hash. + */ + static long contextHash(@Nullable final Map context) { + if (context == null || context.isEmpty()) { + return 0L; + } + long hash = context.size(); + for (final Map.Entry entry : context.entrySet()) { + final Object value = entry.getValue(); + final long valueHash = + value == null ? 0L : (((long) value.getClass().hashCode()) << 32) ^ value.hashCode(); + hash += mix(entry.getKey().hashCode() * 0x9E3779B97F4A7C15L + valueHash); + } + return hash; + } + + /** The finalizer of SplitMix64: spreads every input bit over the whole result. */ + private static long mix(long z) { + z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L; + z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL; + return z ^ (z >>> 31); + } + + /** + * Whether two contexts would be emitted the same. A missing context and an empty one are the + * same, and values compare with {@code equals}, which already tells value types apart. + */ + static boolean sameContext( + @Nullable final Map a, @Nullable final Map b) { + if (a == b) { + return true; + } + if (a == null || a.isEmpty()) { + return b == null || b.isEmpty(); + } + return a.equals(b); + } + + /** One aggregated row: its key, and counters updated by any evaluating thread. */ + static final class Bucket extends ConcurrentHashtable.Entry { + static final AtomicLongFieldUpdater COUNT = + AtomicLongFieldUpdater.newUpdater(Bucket.class, "count"); + static final AtomicLongFieldUpdater FIRST = + AtomicLongFieldUpdater.newUpdater(Bucket.class, "firstEvalMs"); + static final AtomicLongFieldUpdater LAST = + AtomicLongFieldUpdater.newUpdater(Bucket.class, "lastEvalMs"); + static final AtomicIntegerFieldUpdater DEFAULT_USED = + AtomicIntegerFieldUpdater.newUpdater(Bucket.class, "defaultUsed"); + + final boolean degraded; + final String flagKey; + @Nullable final String variant; + @Nullable final String allocationKey; + @Nullable final String targetingKey; + @Nullable final String errorMessage; + @Nullable final Map prunedAttrs; + final Map features; + // The full-tier bucket count of this bucket's flag; null for degraded buckets. + @Nullable final FlagCount flagCount; + // Fixed for a full-tier bucket, where consent is a key dimension. A degraded bucket AND-folds + // it, which is benign: degraded rows drop the targeting key and context whatever the consent. + volatile boolean observeFullEvaluationData; + + volatile long count; + volatile long firstEvalMs = Long.MAX_VALUE; + volatile long lastEvalMs = Long.MIN_VALUE; + volatile int defaultUsed; + + // The next bucket on the stack of buckets touched since the last drain. + @Nullable Bucket dirtyNext; + // The drain that last found this bucket touched, or the last drain before it was created. + int lastDrain; + + Bucket( + final boolean degraded, + final long keyHash, + final String flagKey, + @Nullable final String variant, + @Nullable final String allocationKey, + @Nullable final String targetingKey, + @Nullable final String errorMessage, + @Nullable final Map prunedAttrs, + final boolean observeFullEvaluationData, + final Map features, + @Nullable final FlagCount flagCount, + final int lastDrain) { + super(keyHash); + this.lastDrain = lastDrain; + this.degraded = degraded; + this.flagKey = flagKey; + this.variant = variant; + this.allocationKey = allocationKey; + this.targetingKey = targetingKey; + this.errorMessage = errorMessage; + this.prunedAttrs = prunedAttrs; + this.features = features; + this.flagCount = flagCount; + this.observeFullEvaluationData = observeFullEvaluationData; + } + + void merge( + final ConcurrentFlagAggregator owner, final long evalTimeMs, final boolean isDefault) { + // Timestamps first, so a drain that counts this evaluation also sees its timestamps. + updateTimes(evalTimeMs, isDefault); + if (COUNT.getAndIncrement(this) == 0) { + owner.push(this); + // A drain may have reset the timestamps between the update above and the increment. + updateTimes(evalTimeMs, isDefault); + } + } + + void mergeDegraded( + final ConcurrentFlagAggregator owner, + final long evalTimeMs, + final boolean isDefault, + final boolean observeFullEvaluationData) { + if (!observeFullEvaluationData && this.observeFullEvaluationData) { + this.observeFullEvaluationData = false; // a mixed bucket stays at consent-off + } + merge(owner, evalTimeMs, isDefault); + } + + /** Widens the time range to {@code evalTimeMs}, and marks a runtime default. */ + private void updateTimes(final long evalTimeMs, final boolean isDefault) { + long first; + while (evalTimeMs < (first = firstEvalMs) && !FIRST.compareAndSet(this, first, evalTimeMs)) { + // retry: another thread moved it + } + long last; + while (evalTimeMs > (last = lastEvalMs) && !LAST.compareAndSet(this, last, evalTimeMs)) { + // retry: another thread moved it + } + if (isDefault && defaultUsed == 0) { + defaultUsed = 1; + } + } + + /** Untouched through the whole window that ended with drain {@code drain}. */ + boolean isIdleSince(final int drain) { + return count == 0 && drain - lastDrain >= 1; + } + + boolean matchesFull( + final String flagKey, + @Nullable final String variant, + @Nullable final String allocationKey, + @Nullable final String errorMessage, + @Nullable final String targetingKey, + @Nullable final Map context, + final boolean observeFullEvaluationData, + final Map features) { + return this.observeFullEvaluationData == observeFullEvaluationData + && this.flagKey.equals(flagKey) + && Objects.equals(this.variant, variant) + && Objects.equals(this.allocationKey, allocationKey) + && Objects.equals(this.errorMessage, errorMessage) + && Objects.equals(this.targetingKey, targetingKey) + && this.features.equals(features) + && sameContext(this.prunedAttrs, context); + } + + boolean matchesDegraded( + final String flagKey, + @Nullable final String variant, + @Nullable final String allocationKey, + @Nullable final String errorMessage, + final Map features) { + return this.flagKey.equals(flagKey) + && Objects.equals(this.variant, variant) + && Objects.equals(this.allocationKey, allocationKey) + && Objects.equals(this.errorMessage, errorMessage) + && this.features.equals(features); + } + + @Override + public boolean matches(final Bucket other) { + return degraded + ? matchesDegraded( + other.flagKey, other.variant, other.allocationKey, other.errorMessage, other.features) + : matchesFull( + other.flagKey, + other.variant, + other.allocationKey, + other.errorMessage, + other.targetingKey, + other.prunedAttrs, + other.observeFullEvaluationData, + other.features); + } + } + + /** The number of full-tier buckets of a flag. */ + static final class FlagCount extends ConcurrentHashtable.D1.Entry { + final AtomicInteger count = new AtomicInteger(); + + FlagCount(final String flagKey) { + super(flagKey); + } + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEvent.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEvent.java deleted file mode 100644 index b5331265d41..00000000000 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEvent.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import java.util.Collections; -import java.util.Map; - -/** - * Lightweight data record capturing a single flag evaluation for EVP flagevaluation emission. - * - *

This is the currency passed from the FlagEvalLoggingHook (feature-flagging-api) to the - * FlagEvaluationWriter (feature-flagging-lib) via a non-blocking bounded queue. - * - *

Scalar fields and context attributes are captured at hook-fire time on the evaluation thread. - * No aggregation happens here. - */ -public final class FlagEvalEvent { - - /** The feature flag key. Never null. */ - public final String flagKey; - - /** - * The OpenFeature variant key selected for the evaluation. Null means the default value was - * returned (runtime default). - */ - public final String variant; - - /** The allocation key from flag metadata ("allocationKey"). May be null. */ - public final String allocationKey; - - /** The targeting key from the evaluation context. May be null. */ - public final String targetingKey; - - /** - * The evaluation error message when the evaluation failed, else null. Sourced from the - * OpenFeature evaluation details (error message, falling back to the error code). - */ - public final String errorMessage; - - /** - * Evaluation timestamp in milliseconds since epoch. Stamped at eval-entry time from flag metadata - * key __dd_eval_timestamp_ms, or falls back to hook-fire time when absent. This ensures - * first/last_evaluation reflect evaluation time, not hook-fire time. - */ - public final long evalTimeMs; - - /** - * Flattened evaluation context attributes. Used for the full-tier canonical context key. May be - * empty but never null. - */ - public final Map attrs; - - /** - * PII consent from the ServerConfiguration used by the evaluation. When false (privacy-preserving - * default), the targeting key is hashed and the per-evaluation context is omitted on emission. - */ - public final boolean observeFullEvaluationData; - - /** - * Split features routed to the EVALUATION destination. They come from the flag configuration, not - * the evaluation context, so they are kept whatever the consent. May be empty but never null. - */ - public final Map features; - - /** Convenience constructor; consent defaults to the privacy-preserving false. */ - public FlagEvalEvent( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final long evalTimeMs, - final Map attrs) { - this(flagKey, variant, allocationKey, targetingKey, null, evalTimeMs, false, attrs); - } - - /** Convenience constructor; consent defaults to the privacy-preserving false. */ - public FlagEvalEvent( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final String errorMessage, - final long evalTimeMs, - final Map attrs) { - this(flagKey, variant, allocationKey, targetingKey, errorMessage, evalTimeMs, false, attrs); - } - - public FlagEvalEvent( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final String errorMessage, - final long evalTimeMs, - final boolean observeFullEvaluationData, - final Map attrs) { - this( - flagKey, - variant, - allocationKey, - targetingKey, - errorMessage, - evalTimeMs, - observeFullEvaluationData, - attrs, - null); - } - - public FlagEvalEvent( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final String errorMessage, - final long evalTimeMs, - final boolean observeFullEvaluationData, - final Map attrs, - final Map features) { - this.flagKey = flagKey; - this.variant = variant; - this.allocationKey = allocationKey; - this.targetingKey = targetingKey; - this.errorMessage = errorMessage; - this.evalTimeMs = evalTimeMs; - this.observeFullEvaluationData = observeFullEvaluationData; - this.attrs = attrs != null ? attrs : Collections.emptyMap(); - this.features = features != null ? features : Collections.emptyMap(); - } -} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregator.java deleted file mode 100644 index 0251b7a5b3e..00000000000 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregator.java +++ /dev/null @@ -1,621 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import datadog.trace.util.FlatHashtable; -import datadog.trace.util.LongHashingUtils; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Objects; -import java.util.concurrent.atomic.AtomicLong; - -/** - * Aggregates the evaluations of a flush window into buckets, on the worker thread. - * - *

Each tier is a toolbox {@link FlatHashtable}, cleared as a whole at every flush. A bucket is - * its own table entry, carrying its key and the key's hash, and lookups go through one reused - * {@link Lookup}: a bucket costs one object for the whole window, and aggregating an event into an - * existing bucket allocates nothing. - */ -@SuppressFBWarnings( - value = {"AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE", "AT_STALE_THREAD_WRITE_OF_PRIMITIVE"}, - justification = "the worker thread is the only one aggregating") -final class FlagEvaluationAggregator { - // Design assumptions — document the scale we sized for - static final int EXPECTED_FLAG_COUNT = 2_500; - static final int EXPECTED_FULL_BUCKETS_PER_FLAG = 50; - static final int EXPECTED_USERS_PER_FLAG = 1_000; - static final int PER_FLAG_HEADROOM_MULTIPLIER = 10; - static final int EXPECTED_DEGRADED_BUCKETS_PER_FLAG = 10; - - // Derived sizing — show the math behind the bucket caps below - static final int FULL_BUCKET_SIZING_BASIS = - EXPECTED_FLAG_COUNT * EXPECTED_FULL_BUCKETS_PER_FLAG; // 125_000 - static final int PER_FLAG_BUCKET_SIZING_BASIS = - PER_FLAG_HEADROOM_MULTIPLIER * EXPECTED_USERS_PER_FLAG; // 10_000 - static final int DEGRADED_BUCKET_SIZING_BASIS = - EXPECTED_FLAG_COUNT * EXPECTED_DEGRADED_BUCKETS_PER_FLAG; // 25_000 - - // Enforced bucket caps - static final int GLOBAL_CAP = 131_072; // nearest power of two above FULL_BUCKET_SIZING_BASIS - static final int PER_FLAG_CAP = PER_FLAG_BUCKET_SIZING_BASIS; - static final int DEGRADED_CAP = 32_768; // nearest power of two above DEGRADED_BUCKET_SIZING_BASIS - - private static final int INITIAL_BUCKETS = 64; - private static final FullTier FULL_TIER = new FullTier(); - private static final DegradedTier DEGRADED_TIER = new DegradedTier(); - - private EvalBucket[] fullTier = FlatHashtable.create(INITIAL_BUCKETS, EvalBucket[]::new); - private int fullTierSize; - private EvalBucket[] degradedTier = FlatHashtable.create(INITIAL_BUCKETS, EvalBucket[]::new); - private int degradedTierSize; - final FlatHashtable.D1 perFlagCount = - FlatHashtable.D1.createGrowable(INITIAL_BUCKETS, FlagCount[]::new); - final AtomicLong droppedDegradedOverflow = new AtomicLong(0); - // One shared key and map for each distinct feature set in the flush window, so buckets of many - // subjects do not each keep their own copy until the flush. - final FlatHashtable.D1, FeatureSet> featureSets = - FlatHashtable.D1.createGrowable(INITIAL_BUCKETS, FeatureSet[]::new); - // The worker is the only thread aggregating, so one lookup serves every event. - private final Lookup lookup = new Lookup(); - - void aggregate(final FlagEvalEvent event) { - final boolean isDefault = event.variant == null; - final boolean observeFullEvaluationData = event.observeFullEvaluationData; - // On the protected path the context is dropped on emit, so it must not fragment buckets or be - // stored — otherwise a high-cardinality field (request_id, timestamp) blows out PER_FLAG_CAP - // and spills every subsequent evaluation into the degraded tier. - final Map prunedAttrs = observeFullEvaluationData ? event.attrs : null; - final long contextHash = observeFullEvaluationData ? contextHash(prunedAttrs) : 0L; - // Features come from the flag configuration, not the evaluation context, so they split buckets - // in both tiers and whatever the consent: rows with different features must not merge. - final FeatureSet featureSet = featureSet(event.features); - final Lookup lookup = this.lookup.of(event, prunedAttrs, contextHash, featureSet); - - EvalBucket bucket = FlatHashtable.get(fullTier, lookup, FULL_TIER); - if (bucket != null) { - // Consent is a full-tier key dimension, so the bucket's consent is already this event's. - bucket.merge(event.evalTimeMs, isDefault); - return; - } - - final FlagCount flagCount = perFlagCount.get(event.flagKey); - if (fullTierSize < GLOBAL_CAP && (flagCount == null || flagCount.count < PER_FLAG_CAP)) { - insertFull( - new EvalBucket( - lookup.fullTierHash(), - event.flagKey, - event.variant, - event.allocationKey, - event.targetingKey, - event.errorMessage, - event.evalTimeMs, - isDefault, - prunedAttrs, - observeFullEvaluationData, - featureSet.features)); - if (flagCount == null) { - perFlagCount.insert(new FlagCount(event.flagKey, 1)); - } else { - flagCount.count++; - } - return; - } - - bucket = FlatHashtable.get(degradedTier, lookup, DEGRADED_TIER); - if (bucket != null) { - bucket.merge(event.evalTimeMs, isDefault); - bucket.observeFullEvaluationData &= observeFullEvaluationData; - return; - } - - if (degradedTierSize < DEGRADED_CAP) { - insertDegraded( - new EvalBucket( - lookup.degradedTierHash(), - event.flagKey, - event.variant, - event.allocationKey, - null, - event.errorMessage, - event.evalTimeMs, - isDefault, - null, - observeFullEvaluationData, - featureSet.features)); - return; - } - - droppedDegradedOverflow.incrementAndGet(); - } - - private void insertFull(final EvalBucket bucket) { - // Grow at half full: open addressing slows down as the table fills. - if (fullTierSize >= fullTier.length >> 1) { - fullTier = FlatHashtable.resize(fullTier, EvalBucket[]::new); - } - FlatHashtable.insert(fullTier, bucket); - fullTierSize++; - } - - private void insertDegraded(final EvalBucket bucket) { - if (degradedTierSize >= degradedTier.length >> 1) { - degradedTier = FlatHashtable.resize(degradedTier, EvalBucket[]::new); - } - FlatHashtable.insert(degradedTier, bucket); - degradedTierSize++; - } - - boolean isEmpty() { - return fullTierSize == 0 && degradedTierSize == 0; - } - - int fullTierSize() { - return fullTierSize; - } - - long degradedEvaluationCount() { - long count = 0; - for (final EvalBucket bucket : degradedTier) { - if (bucket != null) { - count += bucket.count; - } - } - return count; - } - - int bucketCount() { - return fullTierSize + degradedTierSize; - } - - Iterable fullBuckets() { - final EvalBucket[] table = fullTier; - return () -> new Buckets(table); - } - - Iterable degradedBuckets() { - final EvalBucket[] table = degradedTier; - return () -> new Buckets(table); - } - - void clear() { - Arrays.fill(fullTier, null); - fullTierSize = 0; - Arrays.fill(degradedTier, null); - degradedTierSize = 0; - featureSets.clear(); - perFlagCount.clear(); - } - - private FeatureSet featureSet(final Map features) { - if (features == null || features.isEmpty()) { - return FeatureSet.NONE; - } - // Map equality and hashing are order-independent and type-aware (Integer 1 is not Long 1), and - // the same configuration map is usually passed every time, which equals() checks first. - FeatureSet featureSet = this.featureSets.get(features); - if (featureSet == null) { - featureSet = new FeatureSet(features); - this.featureSets.insert(featureSet); - } - return featureSet; - } - - /** - * The canonical instance of a distinct feature set in the window: buckets keep its map, so two - * buckets have the same features exactly when they hold the same map instance. - */ - static final class FeatureSet extends FlatHashtable.D1.Entry> { - static final FeatureSet NONE = new FeatureSet(Collections.emptyMap()); - - final Map features; - - FeatureSet(final Map features) { - super(features); - this.features = features; - } - } - - /** The number of full-tier buckets of a flag in the window. */ - static final class FlagCount extends FlatHashtable.D1.Entry { - int count; - - FlagCount(final String flagKey, final int count) { - super(flagKey); - this.count = count; - } - } - - /** The key of the event being aggregated, compared with the buckets in place. */ - // Package-private for FlagEvaluationAggregatorTiersTest. - static final class Lookup { - FlagEvalEvent event; - Map context; - FeatureSet featureSet; - private long fullHash; - private long degradedHash; - private boolean degradedHashed; - - Lookup of( - final FlagEvalEvent event, - final Map context, - final long contextHash, - final FeatureSet featureSet) { - this.event = event; - this.context = context; - this.featureSet = featureSet; - this.fullHash = - fullHash( - event.flagKey, - event.variant, - event.allocationKey, - event.errorMessage, - event.targetingKey, - contextHash, - event.observeFullEvaluationData, - featureSet.hash); - this.degradedHashed = false; - return this; - } - - long fullTierHash() { - return fullHash; - } - - /** Hashed on demand: most events stay in the full tier. */ - long degradedTierHash() { - if (!degradedHashed) { - degradedHash = - degradedHash( - event.flagKey, - event.variant, - event.allocationKey, - event.errorMessage, - featureSet.hash); - degradedHashed = true; - } - return degradedHash; - } - } - - static long fullHash( - final String flagKey, - final String variant, - final String allocationKey, - final String errorMessage, - final String targetingKey, - final long contextHash, - final boolean observeFullEvaluationData, - final long featuresHash) { - long hash = LongHashingUtils.hash(flagKey, variant, allocationKey, errorMessage, targetingKey); - hash = LongHashingUtils.addToHash(hash, contextHash); - hash = LongHashingUtils.addToHash(hash, observeFullEvaluationData); - return LongHashingUtils.addToHash(hash, featuresHash); - } - - static long degradedHash( - final String flagKey, - final String variant, - final String allocationKey, - final String errorMessage, - final long featuresHash) { - final long hash = LongHashingUtils.hash(flagKey, variant, allocationKey, errorMessage); - return LongHashingUtils.addToHash(hash, featuresHash); - } - - /** - * A full-tier bucket: every dimension, including the subject, its context and the consent. A null - * variant, the runtime default, is a dimension through the variant. - */ - static final class FullTier implements FlatHashtable.MatchingStrategy { - @Override - public long hashKey(final Lookup lookup) { - return lookup.fullTierHash(); - } - - @Override - public boolean matches(final EvalBucket bucket, final Lookup lookup) { - final FlagEvalEvent event = lookup.event; - return bucket.hash == lookup.fullTierHash() - && bucket.observeFullEvaluationData == event.observeFullEvaluationData - && Objects.equals(bucket.flagKey, event.flagKey) - && Objects.equals(bucket.variant, event.variant) - && Objects.equals(bucket.allocationKey, event.allocationKey) - && Objects.equals(bucket.errorMessage, event.errorMessage) - && Objects.equals(bucket.targetingKey, event.targetingKey) - && bucket.features == lookup.featureSet.features - && sameContext(bucket.prunedAttrs, lookup.context); - } - } - - /** - * A degraded-tier bucket, without the subject and its context. Unlike the full tier, consent is - * NOT a bucket dimension here: the wire serializer for degraded rows (FlagEvaluationPayloads - * .FlagEvaluationEvent.fromBucket with isFullTier=false) drops the targeting key and context - * unconditionally, so two degraded buckets differing only in consent would emit byte-identical - * JSON with evaluation_count split — halving effective DEGRADED_CAP for zero wire fidelity. - * Mixed-consent events merge into one bucket; the AND-fold on - * EvalBucket.observeFullEvaluationData still runs but has no downstream effect for degraded rows. - */ - static final class DegradedTier implements FlatHashtable.MatchingStrategy { - @Override - public long hashKey(final Lookup lookup) { - return lookup.degradedTierHash(); - } - - @Override - public boolean matches(final EvalBucket bucket, final Lookup lookup) { - final FlagEvalEvent event = lookup.event; - return bucket.hash == lookup.degradedTierHash() - && Objects.equals(bucket.flagKey, event.flagKey) - && Objects.equals(bucket.variant, event.variant) - && Objects.equals(bucket.allocationKey, event.allocationKey) - && Objects.equals(bucket.errorMessage, event.errorMessage) - && bucket.features == lookup.featureSet.features; - } - } - - /** The buckets of a table, for the flush. */ - static final class Buckets implements Iterator { - private final EvalBucket[] table; - private int next; - - Buckets(final EvalBucket[] table) { - this.table = table; - advance(); - } - - private void advance() { - while (next < table.length && table[next] == null) { - next++; - } - } - - @Override - public boolean hasNext() { - return next < table.length; - } - - @Override - public EvalBucket next() { - if (next >= table.length) { - throw new NoSuchElementException(); - } - final EvalBucket bucket = table[next++]; - advance(); - return bucket; - } - } - - AggregatedState snapshot() { - return new AggregatedState( - list(fullBuckets()), list(degradedBuckets()), droppedDegradedOverflow.get()); - } - - private static List list(final Iterable buckets) { - final List list = new ArrayList<>(); - for (final EvalBucket bucket : buckets) { - list.add(bucket); - } - return list; - } - - void simulateFullTierAtCap() { - for (int i = fullTierSize; i < GLOBAL_CAP; i++) { - final String key = "synthetic-full-" + i; - insertFull( - new EvalBucket( - fullHash(key, "on", "alloc", null, null, 0L, false, FeatureSet.NONE.hash), - key, - "on", - "alloc", - null, - null, - 1L, - false, - null, - false, - FeatureSet.NONE.features)); - final FlagCount flagCount = perFlagCount.get(key); - if (flagCount == null) { - perFlagCount.insert(new FlagCount(key, 1)); - } else { - flagCount.count++; - } - } - } - - void simulateDegradedTierAtCap() { - for (int i = degradedTierSize; i < DEGRADED_CAP; i++) { - final String key = "synthetic-dg-" + i; - addDegradedBucketForTest(key, "on", "alloc", null, 1L); - } - } - - void addDegradedBucketForTest( - final String flagKey, - final String variant, - final String allocationKey, - final String errorMessage, - final long evalTimeMs) { - insertDegraded( - new EvalBucket( - degradedHash(flagKey, variant, allocationKey, errorMessage, FeatureSet.NONE.hash), - flagKey, - variant, - allocationKey, - null, - errorMessage, - evalTimeMs, - variant == null, - null, - false, - FeatureSet.NONE.features)); - } - - /** Sets the number of full-tier buckets of a flag, for tests. */ - void setFlagCountForTest(final String flagKey, final int count) { - final FlagCount flagCount = perFlagCount.get(flagKey); - if (flagCount == null) { - perFlagCount.insert(new FlagCount(flagKey, count)); - } else { - flagCount.count = count; - } - } - - /** - * Hashes a context the way {@link #sameContext} compares it: independent of iteration order, and - * distinguishing value types (Integer 1, Long 1 and Double 1.0 hash and compare apart). - * - *

Entries are combined by addition, so the order the map yields them in does not matter; each - * entry is mixed first, so that swapping values between keys changes the hash. - */ - static long contextHash(final Map context) { - if (context == null || context.isEmpty()) { - return 0L; - } - long hash = context.size(); - for (final Map.Entry entry : context.entrySet()) { - final Object value = entry.getValue(); - final long valueHash = - value == null ? 0L : (((long) value.getClass().hashCode()) << 32) ^ value.hashCode(); - hash += mix(entry.getKey().hashCode() * 0x9E3779B97F4A7C15L + valueHash); - } - return hash; - } - - /** The finalizer of SplitMix64: spreads every input bit over the whole result. */ - private static long mix(long z) { - z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L; - z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL; - return z ^ (z >>> 31); - } - - /** - * Whether two contexts would be emitted the same. A missing context and an empty one are the - * same, and values compare with {@code equals}, which already tells value types apart. - */ - static boolean sameContext(final Map a, final Map b) { - if (a == b) { - return true; - } - if (a == null || a.isEmpty()) { - return b == null || b.isEmpty(); - } - return a.equals(b); - } - - /** - * A bucket, and its own entry in its tier's table: it carries its key and the key's hash, so a - * bucket is one object. - */ - static class EvalBucket extends FlatHashtable.Entry { - long count; - long firstEvalMs; - long lastEvalMs; - boolean runtimeDefaultUsed; - String flagKey; - String variant; - String allocationKey; - String targetingKey; - String errorMessage; - Map prunedAttrs; - // Identical for every evaluation in the bucket, because the features key is a bucket dimension. - Map features; - // Consent to emit raw PII. For full-tier buckets this is fixed at creation (consent is a key - // dimension), so only degraded merges change it. For degraded-tier buckets consent is NOT - // a key dimension — mixed-consent events merge here — so the AND-fold produces false whenever - // any consent-off event lands in the bucket. That's benign because the degraded wire path - // drops the targeting key and context regardless of consent, so this field has no downstream - // effect for degraded rows. - boolean observeFullEvaluationData; - - EvalBucket( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final String errorMessage, - final long evalTimeMs, - final boolean runtimeDefaultUsed, - final Map prunedAttrs, - final boolean observeFullEvaluationData, - final Map features) { - this( - 0, - flagKey, - variant, - allocationKey, - targetingKey, - errorMessage, - evalTimeMs, - runtimeDefaultUsed, - prunedAttrs, - observeFullEvaluationData, - features); - } - - EvalBucket( - final long hash, - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final String errorMessage, - final long evalTimeMs, - final boolean runtimeDefaultUsed, - final Map prunedAttrs, - final boolean observeFullEvaluationData, - final Map features) { - super(hash); - this.flagKey = flagKey; - this.variant = variant; - this.allocationKey = allocationKey; - this.targetingKey = targetingKey; - this.errorMessage = errorMessage; - this.firstEvalMs = evalTimeMs; - this.lastEvalMs = evalTimeMs; - this.count = 1; - this.runtimeDefaultUsed = runtimeDefaultUsed; - this.prunedAttrs = prunedAttrs; - this.observeFullEvaluationData = observeFullEvaluationData; - this.features = features; - } - - int prunedContextFieldCount() { - return prunedAttrs == null ? 0 : prunedAttrs.size(); - } - - void merge(final long evalTimeMs, final boolean isDefault) { - count++; - if (evalTimeMs < firstEvalMs) { - firstEvalMs = evalTimeMs; - } - if (evalTimeMs > lastEvalMs) { - lastEvalMs = evalTimeMs; - } - if (isDefault) { - runtimeDefaultUsed = true; - } - } - } - - static class AggregatedState { - final List fullTier; - final List degradedTier; - final long droppedDegradedOverflow; - - AggregatedState( - final List fullTier, - final List degradedTier, - final long droppedDegradedOverflow) { - this.fullTier = fullTier; - this.degradedTier = degradedTier; - this.droppedDegradedOverflow = droppedDegradedOverflow; - } - } -} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java index b829244524b..0cca2aa6ec9 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java @@ -249,24 +249,34 @@ static class FlagEvaluationEvent { this.features = (features != null && !features.isEmpty()) ? features : null; } + /** + * The row of a drained bucket. Timestamps that raced the drain (none left, so first is after + * last) read as the flush time. + */ static FlagEvaluationEvent fromBucket( - final FlagEvaluationAggregator.EvalBucket bucket, - final boolean isFullTier, - final boolean observeFullEvaluationData, + final ConcurrentFlagAggregator.Bucket bucket, + final long count, + final long firstEvalMs, + final long lastEvalMs, + final boolean runtimeDefaultUsed, final long flushTimeMs) { - final boolean includeRawContext = isFullTier && observeFullEvaluationData; + final boolean isFullTier = !bucket.degraded; + // Consent is the value snapshotted at evaluation time, not the current configuration's: a + // later configuration may have changed it since these evaluations happened. + final boolean observeFullEvaluationData = bucket.observeFullEvaluationData; + final boolean timed = firstEvalMs <= lastEvalMs; return new FlagEvaluationEvent( flushTimeMs, bucket.flagKey, - bucket.firstEvalMs, - bucket.lastEvalMs, - bucket.count, + timed ? firstEvalMs : flushTimeMs, + timed ? lastEvalMs : flushTimeMs, + count, bucket.variant, bucket.allocationKey, resolveTargetingKey(bucket.targetingKey, isFullTier, observeFullEvaluationData), - bucket.runtimeDefaultUsed, + runtimeDefaultUsed, bucket.errorMessage, - includeRawContext ? bucket.prunedAttrs : null, + isFullTier && observeFullEvaluationData ? bucket.prunedAttrs : null, // Features come from the flag configuration, so both tiers keep them whatever the // consent. bucket.features); diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java index 6bbf08837b0..137daf18e73 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java @@ -1,12 +1,11 @@ package com.datadog.openfeature.internal.flagevaluation; import static com.datadog.openfeature.internal.EventPipeline.awaitThread; +import static java.util.concurrent.TimeUnit.NANOSECONDS; import com.datadog.openfeature.internal.EventPipeline; -import com.datadog.openfeature.internal.EventQueue; import com.datadog.openfeature.internal.RuntimeServices; import com.datadog.openfeature.internal.bridge.v1.EventTransport; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -22,31 +21,21 @@ /** * EVP flagevaluation writer for Java. * - *

Uses the same event transport as the exposure pipeline, with two-tier aggregation replacing - * the single-exposure buffer. + *

Evaluations are aggregated on the evaluating threads, into a {@link ConcurrentFlagAggregator} + * (see it for the tiers, the caps and the bucket keys), so no evaluation waits on a queue or is + * dropped for lack of room in one. The worker thread only flushes: every flush interval it drains + * the buckets touched since the last flush into payloads, sweeps out the idle ones, and posts the + * payloads through the same event transport as the exposure pipeline. * - *

Two-tier aggregation contract: Full key: (flagKey, variant, allocationKey, runtimeDefault, - * errorMessage, targetingKey, canonical-context-key). Degraded key: (flagKey, variant, - * allocationKey, runtimeDefault, errorMessage) - no targetingKey/context. Canonical context key: - * sorted entries, type-tagged length-delimited encoding - NOT a hash (collision-safe, comparable - * string identity). Context pruning: deterministic (sort before cut), <=256 fields, string values - * <=256 chars; the pruned attributes are what gets aggregated and serialized. Caps: - * globalCap=131072, perFlagCap=10000, degradedCap=32768. Eval-time: min/max of - * firstEvalMs/lastEvalMs across events in the same bucket. Runtime default: absent variant means - * runtimeDefaultUsed=true. Flush interval: 10 seconds. Queue: bounded lock-free queue (capacity - * 2^12), non-blocking offer; on overflow the event is dropped and the droppedQueueOverflow counter - * is incremented and surfaced on flush. Enqueue: lock-free. Producers contend only on the MPSC - * queue, never on a monitor, so evaluation threads do not serialize against each other. Shutdown: - * close() drains the queue and performs a final flush before the worker thread exits. Because - * enqueue is lock-free, a producer can still offer during shutdown; close() sweeps the queue once - * the worker has been joined, counting any remainder as a closed drop so shutdown loss is - * observable rather than silent. + *

Shutdown: {@link #requestShutdown} stops accepting evaluations, counting later ones as closed + * drops, and wakes the worker for a final flush. Because recording is lock-free, an evaluation can + * still land after that flush; once the worker has exited, {@link #awaitTermination} drains the + * remainder and counts it as closed drops, so shutdown loss is observable rather than silent. */ public class FlagEvaluationPipeline implements FlagEvaluationWriter, EventPipeline { private static final Logger LOGGER = LoggerFactory.getLogger(FlagEvaluationPipeline.class); - public static final int DEFAULT_CAPACITY = 1 << 12; // 4096 elements, per cross-SDK RFC public static final int FLUSH_INTERVAL_SECONDS = 10; static final int FLAG_EVALUATION_PAYLOAD_SIZE_LIMIT_BYTES = 5 * 1024 * 1024; @@ -54,56 +43,42 @@ public class FlagEvaluationPipeline implements FlagEvaluationWriter, EventPipeli static final String FLAG_EVALUATION_DEGRADED_METRIC = "flagevaluation.rows.degraded"; static final String FLAG_EVALUATION_SPLITS_METRIC = "flagevaluation.payload.splits"; static final String FLAG_EVALUATION_CONTEXT_TRUNCATED_METRIC = "flagevaluation.context.truncated"; - static final String DROP_REASON_QUEUE_OVERFLOW = "queue_overflow"; static final String DROP_REASON_CLOSED = "closed"; static final String DROP_REASON_DEGRADED_CAP = "degraded_cap"; static final String DROP_REASON_PAYLOAD_LIMIT = "payload_limit"; static final String DEGRADED_REASON_CARDINALITY_CAP = "cardinality_cap"; static final String DEGRADED_REASON_PAYLOAD_LIMIT = "payload_limit"; private static final String FLAG_EVALUATION_ROUTE = "flagevaluation"; - private final RuntimeServices services; - private final EventQueue queue; + private final RuntimeServices services; + private final ConcurrentFlagAggregator aggregator = new ConcurrentFlagAggregator(); private final FlagEvaluationSerializingHandler serializer; private final Thread serializerThread; private final Object lifecycleLock = new Object(); private final AtomicBoolean closed = new AtomicBoolean(false); - private void countMetric(final String metricName, final long value, final String reason) { - services.countMetric(metricName, value, reason); - } - - /** - * Observable counter for events dropped because the bounded hand-off queue was full when the hook - * tried to enqueue (backpressure). Incremented on the hook thread, surfaced on flush. - */ - private final AtomicLong droppedQueueOverflow = new AtomicLong(0); - /** * Per-reason-tag counters for evaluations whose context was truncated by copyPrunedContext. Keyed * by the sorted comma-separated reason string (e.g. "max_key_length,max_value_length"). - * Incremented on the hook thread, drained and emitted on flush. + * Incremented on the evaluating thread, drained and emitted on flush. */ private final ConcurrentHashMap contextTruncatedCounts = new ConcurrentHashMap<>(); public FlagEvaluationPipeline( - final int capacity, final long flushInterval, final TimeUnit timeUnit, final EventTransport transport, final Map context, final RuntimeServices services) { this.services = services; - this.queue = new EventQueue<>(capacity); this.serializer = new FlagEvaluationSerializingHandler( transport, - queue, + aggregator, flushInterval, timeUnit, context, - droppedQueueOverflow, contextTruncatedCounts, FLAG_EVALUATION_PAYLOAD_SIZE_LIMIT_BYTES, services); @@ -120,16 +95,11 @@ public void start() { } } - /** Test seam: current full-tier bucket count in the worker's aggregator. */ - int aggregatorFullTierSizeForTest() { - return serializer.aggregator.fullTierSize(); - } - @Override public void requestShutdown() { synchronized (lifecycleLock) { if (closed.compareAndSet(false, true)) { - // The worker polls every 100 ms: do not interrupt a current or final HTTP request. + // Wakes the worker for its final flush; never interrupts a current or final HTTP request. serializer.requestShutdown(); // Logged last: a diagnostic must not keep the worker from being signaled. LOGGER.debug("Feature Flags flag evaluation shutdown requested"); @@ -143,91 +113,70 @@ public void awaitTermination(final long deadlineNanos) { return; } awaitThread(this.serializerThread, deadlineNanos); - // enqueue() is lock-free, so a producer that passed the closed check before close() ran can - // still land an event after the worker's final drain. Sweep the remainder so that loss is - // counted rather than silently stranded in a queue nobody polls again. Only safe once the - // worker is gone: the queue is single-consumer, so sweeping alongside a live worker would - // break that contract. If the wait timed out the worker is still draining, so skip the sweep. + // Recording is lock-free, so an evaluation that passed the closed check before close() ran can + // still land after the worker's final drain. Count the remainder rather than leave it silently + // stranded. Only once the worker is gone, since the aggregator has one draining thread at a + // time; if the wait timed out the worker is still flushing, so skip it. synchronized (lifecycleLock) { if (!this.serializerThread.isAlive()) { - sweepAndCountResidualEvents(); + countResidualEvaluations(); } } } - /** - * Counts events left in the queue after the worker has exited. Must only be called when the - * serializer thread is not alive - the queue permits a single consumer. - */ - private void sweepAndCountResidualEvents() { - long residual = 0; - while (queue.poll() != null) { - residual++; - } - countMetric(FLAG_EVALUATION_DROPPED_METRIC, residual, DROP_REASON_CLOSED); + /** Counts the evaluations recorded since the worker's last drain as closed drops. */ + private void countResidualEvaluations() { + final long[] residual = {0}; + aggregator.drain((bucket, count, first, last, defaultUsed) -> residual[0] += count); + countClosedDrops(residual[0]); } @Override - public void enqueue(final FlagEvalEvent event) { - if (event == null) { - return; - } + public void record( + final String flagKey, + final String variant, + final String allocationKey, + final String targetingKey, + final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + final Map attrs, + final Map features) { if (closed.get()) { - countClosedDrop(); + countClosedDrops(1); return; } - // Deliberately lock-free: the hand-off queue is MPSC by design, so serializing producers on a - // monitor here would negate that and turn every evaluation in every application thread into - // contention on one lock. A producer that passed the check above can still offer after close() - // has started; that residue is accounted for by the worker's bounded post-drain passes and by - // close()'s post-join sweep, so shutdown loss stays observable. - // - // Safe publication of the event (including the context snapshot built by the hook) comes from - // the queue's own offer/poll ordering, not from any monitor held here. - // - // Non-blocking offer. Count overflow so loss is observable rather than silent; the count is - // surfaced on the next flush. The hook's pre-queue guard (see FlagEvalLoggingHook) samples the - // queue depth before doing any context-copy work, so a saturated queue costs an - // AtomicInteger.get(); the offer here still races with the worker and can legitimately fail. - if (!queue.offer(event)) { - droppedQueueOverflow.incrementAndGet(); + try { + aggregator.record( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); + } catch (RuntimeException e) { + // Never let telemetry break the evaluation, e.g. on a context value whose hashCode throws. + LOGGER.debug("Could not record flag evaluation", e); } } - @Override - public boolean hasCapacityForEnqueue() { - return queue.size() < queue.capacity(); - } - - @Override - public void countPreQueueOverflow() { - droppedQueueOverflow.incrementAndGet(); - } - @Override public void countContextTruncated(final String reason) { contextTruncatedCounts.computeIfAbsent(reason, k -> new AtomicLong(0)).incrementAndGet(); } - private void countClosedDrop() { - countMetric(FLAG_EVALUATION_DROPPED_METRIC, 1, DROP_REASON_CLOSED); - } - - /** Returns the count of events dropped due to queue-overflow backpressure (observable). */ - long droppedQueueOverflow() { - return droppedQueueOverflow.get(); - } - - /** Test seam: returns one queued event without starting the worker. */ - FlagEvalEvent pollQueuedEventForTest() { - return queue.poll(); + private void countClosedDrops(final long count) { + services.countMetric(FLAG_EVALUATION_DROPPED_METRIC, count, DROP_REASON_CLOSED); } boolean isSerializerThreadAlive() { return serializerThread.isAlive(); } - /** Test seam: flushes serializer state without starting the worker. */ + /** Test seam: flushes without starting the worker. */ void flushForTest() { serializer.flush(); } @@ -236,55 +185,42 @@ void flushForTest() { static class FlagEvaluationSerializingHandler implements Runnable { private final RuntimeServices services; - - private void countMetric(String name, long value, String reason) { - services.countMetric(name, value, reason); - } - - private final EventQueue queue; - private final long ticksRequiredToFlush; - - @SuppressFBWarnings( - value = "AT_NONATOMIC_64BIT_PRIMITIVE", - justification = "the field is confined to the single serializer thread") - private long lastTicks; - private final EventTransport transport; + private final ConcurrentFlagAggregator aggregator; + private final long flushIntervalNanos; final Map context; - private final AtomicLong droppedQueueOverflow; private final ConcurrentHashMap contextTruncatedCounts; private final int payloadSizeLimitBytes; - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - // Shutdown coordination: set by close(), drives a final drain+flush before the worker exits. - private final AtomicBoolean shutdownRequested = new AtomicBoolean(false); - private final CountDownLatch finalFlushDone = new CountDownLatch(1); + // Counted down by requestShutdown(): wakes the worker for its final flush. + private final CountDownLatch shutdownRequested = new CountDownLatch(1); FlagEvaluationSerializingHandler( final EventTransport transport, - final EventQueue queue, + final ConcurrentFlagAggregator aggregator, final long flushInterval, final TimeUnit timeUnit, final Map context, - final AtomicLong droppedQueueOverflow, final ConcurrentHashMap contextTruncatedCounts, final int payloadSizeLimitBytes, final RuntimeServices services) { this.services = services; - this.queue = queue; this.transport = transport; + this.aggregator = aggregator; + this.flushIntervalNanos = timeUnit.toNanos(flushInterval); this.context = context; - this.droppedQueueOverflow = droppedQueueOverflow; this.contextTruncatedCounts = contextTruncatedCounts; this.payloadSizeLimitBytes = payloadSizeLimitBytes; - this.lastTicks = System.nanoTime(); - this.ticksRequiredToFlush = timeUnit.toNanos(flushInterval); LOGGER.debug("starting flag evaluation serializer"); } - /** Signals the worker to drain the queue and perform a final flush before exiting. */ + private void countMetric(final String name, final long value, final String reason) { + services.countMetric(name, value, reason); + } + + /** Signals the worker to perform a final flush and exit. */ void requestShutdown() { - shutdownRequested.set(true); + shutdownRequested.countDown(); } @Override @@ -294,18 +230,15 @@ public void run() { } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { - // On exit (interrupt or shutdown request), drain everything still buffered and flush it so - // queued events are not lost on shutdown. + // On exit (interrupt or shutdown request), flush what is aggregated so it is not lost. // // Normal close only signals shutdown. If an unrelated interrupt ends the duty cycle, clear // it during the final socket I/O and restore it afterwards: the flush otherwise fails - // fast, the aggregated rows are discarded by the clear() in flush(), and the loss is - // invisible to the drop counters because those rows already left the queue. + // fast, and the drained rows are lost, invisibly to the drop counters. final boolean wasInterrupted = Thread.interrupted(); try { - drainAndFlush(); + flush(); } finally { - finalFlushDone.countDown(); if (wasInterrupted) { Thread.currentThread().interrupt(); } @@ -314,55 +247,23 @@ public void run() { LOGGER.debug("flag evaluation processor worker exited."); } + /** Flushes every flush interval until shutdown is requested. */ private void runDutyCycle() throws InterruptedException { - final Thread thread = Thread.currentThread(); - while (!thread.isInterrupted() && !shutdownRequested.get()) { - final FlagEvalEvent event = queue.poll(100, TimeUnit.MILLISECONDS); - if (event != null) { - aggregateEvent(event); + long lastFlush = System.nanoTime(); + while (true) { + final long remaining = flushIntervalNanos - (System.nanoTime() - lastFlush); + if (remaining <= 0) { + flush(); + lastFlush = System.nanoTime(); + } else if (shutdownRequested.await(remaining, NANOSECONDS)) { + return; } - flushIfNecessary(); - } - } - - void drainAndFlush() { - FlagEvalEvent event; - while ((event = queue.poll()) != null) { - aggregateEvent(event); - } - flush(); - } - - // ---- Aggregation logic ---- - - /** Routes an event into the full tier or degraded tier, or drops and counts on overflow. */ - void aggregateEvent(final FlagEvalEvent event) { - try { - aggregator.aggregate(event); - } catch (LinkageError | RuntimeException e) { - LOGGER.debug("Could not aggregate flag evaluation event", e); } } // ---- Flush logic ---- - void flushIfNecessary() { - if (shouldFlush()) { - flush(); - } - } - void flush() { - // Surface backpressure (queue-overflow) drops as an observable warning even when there is - // nothing else to flush. - final long qDrops = droppedQueueOverflow.getAndSet(0); - countMetric(FLAG_EVALUATION_DROPPED_METRIC, qDrops, DROP_REASON_QUEUE_OVERFLOW); - if (qDrops > 0) { - LOGGER.warn( - "flag evaluation queue full - dropped {} evaluation(s) under backpressure" - + " (best-effort telemetry)", - qDrops); - } final long dgDrops = aggregator.droppedDegradedOverflow.getAndSet(0); countMetric(FLAG_EVALUATION_DROPPED_METRIC, dgDrops, DROP_REASON_DEGRADED_CAP); if (dgDrops > 0) { @@ -380,18 +281,26 @@ void flush() { } } - if (aggregator.isEmpty()) { - return; - } try { - countMetric( - FLAG_EVALUATION_DEGRADED_METRIC, - aggregator.degradedEvaluationCount(), - DEGRADED_REASON_CARDINALITY_CAP); - final List events = buildEventList(); + final long flushTimeMs = System.currentTimeMillis(); + final List events = new ArrayList<>(); + final long[] degradedCount = {0}; + // The drain resets the counters it reads: a failed delivery drops this batch rather than + // retrying it, so one unserializable value cannot block every later flush. + aggregator.drain((bucket, count, first, last, defaultUsed) -> { + if (bucket.degraded) { + degradedCount[0] += count; + } + events.add( + FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( + bucket, count, first, last, defaultUsed, flushTimeMs)); + }); + aggregator.sweepIdle(); if (events.isEmpty()) { return; } + countMetric( + FLAG_EVALUATION_DEGRADED_METRIC, degradedCount[0], DEGRADED_REASON_CARDINALITY_CAP); final FlagEvaluationPayloads.EncodedPayloads payloads = FlagEvaluationPayloads.buildPayloads(events, context, payloadSizeLimitBytes); countMetric( @@ -425,129 +334,7 @@ void flush() { "Feature Flags flag evaluation flush failed: {}; dropping the batch", e.toString(), e); } catch (Exception e) { LOGGER.error("Could not submit flag evaluations", e); - } finally { - // Best-effort: always clear the aggregator after a flush attempt. Retaining buckets across - // flushes on encode failure would let one unserializable value (for example a NaN Double a - // customer put in the context) permanently block every subsequent flush. - aggregator.clear(); - lastTicks = System.nanoTime(); - } - } - - private List buildEventList() { - final long flushTimeMs = System.currentTimeMillis(); - // Consent is read per bucket from the value each event snapshotted at evaluation time, not - // from the gateway here: CURRENT_CONFIG may have been overwritten by a later RC update since - // these evaluations happened, and reading it at flush would apply the wrong config's consent. - final List events = - new ArrayList<>(aggregator.bucketCount()); - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.fullBuckets()) { - events.add( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, true, bucket.observeFullEvaluationData, flushTimeMs)); - } - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.degradedBuckets()) { - events.add( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, false, bucket.observeFullEvaluationData, flushTimeMs)); - } - return events; - } - - private boolean shouldFlush() { - if (aggregator.isEmpty() && droppedQueueOverflow.get() == 0) { - return false; - } - final long nanoTime = System.nanoTime(); - final long ticks = nanoTime - lastTicks; - if (ticks > ticksRequiredToFlush) { - lastTicks = nanoTime; - return true; - } - return false; - } - } - - // ---- Test-seam inner class (package-private) ---- - - /** - * Test-accessible handler that exposes {@link #drainAndAggregate()} and {@link #flush()} without - * starting a real background thread. - */ - static class SerializingHandlerForTest extends FlagEvaluationSerializingHandler { - - SerializingHandlerForTest( - final EventTransport transport, - final Map context, - final RuntimeServices services) { - this(transport, context, FLAG_EVALUATION_PAYLOAD_SIZE_LIMIT_BYTES, services); - } - - SerializingHandlerForTest( - final EventTransport transport, - final Map context, - final int payloadSizeLimitBytes, - final RuntimeServices services) { - super( - transport, - new EventQueue<>(DEFAULT_CAPACITY), - Long.MAX_VALUE, // effectively never auto-flush - TimeUnit.NANOSECONDS, - context, - new AtomicLong(0), - new ConcurrentHashMap<>(), - payloadSizeLimitBytes, - services); - } - - private final List staged = new ArrayList<>(); - - /** Adds an event to the staged list (simulates hook enqueue). */ - void add(final FlagEvalEvent event) { - staged.add(event); - } - - /** Aggregates all staged events and returns the current aggregation state. */ - FlagEvaluationAggregator.AggregatedState drainAndAggregate() { - for (final FlagEvalEvent e : staged) { - aggregateEvent(e); } - staged.clear(); - return aggregator.snapshot(); - } - - /** Simulates filling the full tier to GLOBAL_CAP by injecting synthetic distinct buckets. */ - void simulateFullTierAtCap() { - aggregator.simulateFullTierAtCap(); - } - - /** - * Simulates filling the degraded tier to DEGRADED_CAP by injecting synthetic distinct buckets. - */ - void simulateDegradedTierAtCap() { - aggregator.simulateDegradedTierAtCap(); - } - - void addDroppedDegradedOverflowForTest(final long count) { - aggregator.droppedDegradedOverflow.addAndGet(count); - } - - void addDegradedBucketForTest( - final String flagKey, - final String variant, - final String allocationKey, - final String errorMessage, - final long evalTimeMs) { - aggregator.addDegradedBucketForTest( - flagKey, variant, allocationKey, errorMessage, evalTimeMs); - } - - void clearAggregationForTest() { - aggregator.clear(); - } - - int fullTierSizeForTest() { - return aggregator.fullTierSize(); } } } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationWriter.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationWriter.java index 5733ef94d34..6f820adcc88 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationWriter.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationWriter.java @@ -1,21 +1,9 @@ package com.datadog.openfeature.internal.flagevaluation; -/** Records flag evaluation events from the evaluation thread, without blocking. */ -public interface FlagEvaluationWriter { - /** - * Reports whether the async hand-off queue currently has room for another event. Producers - * consult this before performing any expensive context-copy work so a saturated queue is observed - * as an O(1) read rather than a full snapshot followed by a discarded offer. Best-effort only: - * the worker can drain (or a peer producer can fill) between this check and the subsequent - * enqueue, so callers must still tolerate offer failure. - * - * @return {@code true} if the queue currently has room for another event. - */ - boolean hasCapacityForEnqueue(); - - /** Counts one queue-overflow drop without offering an event. */ - void countPreQueueOverflow(); +import java.util.Map; +/** Records flag evaluations on the evaluating thread, without blocking on I/O. */ +public interface FlagEvaluationWriter { /** * Counts one evaluation whose context was truncated. * @@ -24,10 +12,26 @@ public interface FlagEvaluationWriter { void countContextTruncated(String reason); /** - * Non-blocking enqueue of a flag evaluation event. Drops the event, and counts the drop, if the - * queue is full. + * Records one evaluation into the writer's aggregate. * - * @param event the event to enqueue. + * @param flagKey the flag key. + * @param variant the selected variant, or {@code null} for the runtime default. + * @param allocationKey the allocation key, if any. + * @param targetingKey the subject's targeting key, if any. + * @param errorMessage the evaluation error, if any. + * @param evalTimeMs the evaluation time, in milliseconds since the epoch. + * @param observeFullEvaluationData whether the raw targeting key and context may be sent. + * @param attrs the pruned evaluation context; ignored without consent. The writer keeps it. + * @param features the split features routed to evaluations. The writer keeps it. */ - void enqueue(FlagEvalEvent event); + void record( + String flagKey, + String variant, + String allocationKey, + String targetingKey, + String errorMessage, + long evalTimeMs, + boolean observeFullEvaluationData, + Map attrs, + Map features); } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java index 42609eebf87..997e586230d 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java @@ -28,8 +28,8 @@ import com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache; import com.datadog.openfeature.internal.exposure.ExposureEvent; import com.datadog.openfeature.internal.exposure.ExposurePipeline; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationPipeline; +import com.datadog.openfeature.internal.flagevaluation.RecordedEvaluation; import com.datadog.openfeature.internal.ufc.Allocation; import com.datadog.openfeature.internal.ufc.ConditionConfiguration; import com.datadog.openfeature.internal.ufc.ConditionOperator; @@ -92,7 +92,7 @@ class EvaluationTelemetryOptOutTest { private static final String SYSTEM_PROPERTY = "dd.feature.flags.evaluation.telemetry.per.subject.opt.out.enabled"; - private final List events = new CopyOnWriteArrayList<>(); + private final List events = new CopyOnWriteArrayList<>(); private final List spans = new CopyOnWriteArrayList<>(); private final List exposures = new CopyOnWriteArrayList<>(); private final List customerEvaluations = new CopyOnWriteArrayList<>(); @@ -267,7 +267,8 @@ void telemetryAttributeRemainsAvailableForTargetingAndExposure(final boolean ena assertEquals(true, this.events.get(previousEvents + 1).attrs.get(PARAMETER)); } } else { - for (final FlagEvalEvent event : this.events.subList(previousEvents, this.events.size())) { + for (final RecordedEvaluation event : + this.events.subList(previousEvents, this.events.size())) { assertTrue(event.attrs.isEmpty(), "normal evaluation privacy rules still apply"); } } @@ -804,7 +805,7 @@ private Object exposureAttribute(final int index) { private void assertTelemetryCounts(final int expected) { assertAll( - () -> assertEquals(expected, this.events.size(), "evaluation EVP enqueue count"), + () -> assertEquals(expected, this.events.size(), "evaluation EVP record count"), () -> assertEquals(expected, this.metrics.get(), "OTel evaluation count"), () -> assertEquals(expected, this.spans.size(), "automatic span enrichment count")); } @@ -855,24 +856,36 @@ void recordExposure(final ExposureEvent event) { } } - /** A flag evaluation pipeline capturing enqueued events, never started. */ + /** A flag evaluation pipeline capturing recorded evaluations, never started. */ private static final class CapturingPipeline extends FlagEvaluationPipeline { - private final List events; + private final RecordedEvaluation.Recorder recorder; - CapturingPipeline(final List events) { - super( - 1, 1, SECONDS, (route, json) -> {}, emptyMap(), new RuntimeServices(HealthMetrics.NOOP)); - this.events = events; - } - - @Override - public boolean hasCapacityForEnqueue() { - return true; + CapturingPipeline(final List events) { + super(1, SECONDS, (route, json) -> {}, emptyMap(), new RuntimeServices(HealthMetrics.NOOP)); + this.recorder = new RecordedEvaluation.Recorder(events::add); } @Override - public void enqueue(final FlagEvalEvent event) { - this.events.add(event); + public void record( + final String flagKey, + final String variant, + final String allocationKey, + final String targetingKey, + final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + final Map attrs, + final Map features) { + recorder.record( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); } } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/ExposureHookTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/ExposureHookTest.java index b883d06123b..9f44eb62577 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/ExposureHookTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/ExposureHookTest.java @@ -18,14 +18,10 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import com.datadog.openfeature.Provider.Options; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter; +import com.datadog.openfeature.internal.flagevaluation.RecordedEvaluation; import com.datadog.openfeature.internal.ufc.Allocation; import com.datadog.openfeature.internal.ufc.Feature; import com.datadog.openfeature.internal.ufc.Flag; @@ -257,7 +253,7 @@ void aRepeatedKeyKeepsTheFirstFeatureForEveryDestination() throws Exception { @Test void sendsEvaluationFeaturesForAnAllocationThatDoesNotLogExposures() throws Exception { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); start( true, configuration( @@ -459,16 +455,8 @@ private static ServerConfiguration configuration( return new ServerConfiguration("", "", false, null, flags); } - private static FlagEvaluationWriter capturingWriter(final AtomicReference ref) { - final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(true); - doAnswer( - invocation -> { - ref.set(invocation.getArgument(0)); - return null; - }) - .when(writer) - .enqueue(any()); - return writer; + private static FlagEvaluationWriter capturingWriter( + final AtomicReference ref) { + return new RecordedEvaluation.Recorder(ref::set); } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/FlagEvalLoggingHookTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/FlagEvalLoggingHookTest.java index 1e4d2138515..00ab0012d86 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/FlagEvalLoggingHookTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/FlagEvalLoggingHookTest.java @@ -12,17 +12,17 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter; +import com.datadog.openfeature.internal.flagevaluation.RecordedEvaluation; import dev.openfeature.sdk.ErrorCode; import dev.openfeature.sdk.FlagEvaluationDetails; import dev.openfeature.sdk.FlagValueType; @@ -42,25 +42,17 @@ import org.tabletest.junit.TableTest; /** - * Unit tests for {@link FlagEvalLoggingHook}: context capture, non-blocking enqueue, eval-time - * metadata, absent-variant detection, and killswitch-via-writer-null behaviour. + * Unit tests for {@link FlagEvalLoggingHook}: context capture, recording, eval-time metadata, + * absent-variant detection, and killswitch-via-writer-null behaviour. */ class FlagEvalLoggingHookTest { // ---- helpers ---- - /** Creates a pipeline mock that captures the enqueued event for assertion. */ - private static FlagEvaluationWriter capturingWriter(final AtomicReference ref) { - final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(true); - doAnswer( - invocation -> { - ref.set(invocation.getArgument(0)); - return null; - }) - .when(writer) - .enqueue(any()); - return writer; + /** Creates a writer that captures the recorded evaluation for assertion. */ + private static FlagEvaluationWriter capturingWriter( + final AtomicReference ref) { + return new RecordedEvaluation.Recorder(ref::set); } private static FlagEvalLoggingHook hookWithWriter(final FlagEvaluationWriter writer) { @@ -95,11 +87,11 @@ private static HookContext hookCtxWithTargetingKey( .build(); } - // ---- test: hook calls writer.enqueue once with flagKey, variant, allocationKey ---- + // ---- test: hook calls writer.record once with flagKey, variant, allocationKey ---- @Test - void finallyAfterEnqueuesEventWithAllBasicFields() { - final AtomicReference captured = new AtomicReference<>(); + void finallyAfterRecordsEvaluationWithAllBasicFields() { + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -112,8 +104,8 @@ void finallyAfterEnqueuesEventWithAllBasicFields() { hook.finallyAfter(null, det, Collections.emptyMap()); - assertNotNull(captured.get(), "writer.enqueue must be called once"); - final FlagEvalEvent e = captured.get(); + assertNotNull(captured.get(), "writer.record must be called once"); + final RecordedEvaluation e = captured.get(); assertEquals("my-flag", e.flagKey); assertEquals("on", e.variant, "variant must be the OpenFeature variant key"); assertEquals("alloc-1", e.allocationKey); @@ -125,7 +117,7 @@ void finallyAfterEnqueuesEventWithAllBasicFields() { "consent off | false " }) void finallyAfterAddsEvaluationFeaturesWhateverTheConsent(final boolean consent) { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); hook.finallyAfter( @@ -153,7 +145,7 @@ void finallyAfterAddsEvaluationFeaturesWhateverTheConsent(final boolean consent) @Test void finallyAfterSkipsEvaluationFeaturesWithoutTheMarker() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); hook.finallyAfter( @@ -173,7 +165,7 @@ void finallyAfterSkipsEvaluationFeaturesWithoutTheMarker() { @Test void finallyAfterIgnoresMetadataOfTheWrongType() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); hook.finallyAfter( @@ -190,7 +182,7 @@ void finallyAfterIgnoresMetadataOfTheWrongType() { .build()), Collections.emptyMap()); - final FlagEvalEvent event = captured.get(); + final RecordedEvaluation event = captured.get(); assertNull(event.allocationKey); assertFalse(event.observeFullEvaluationData); assertTrue(event.evalTimeMs > 0); @@ -200,7 +192,7 @@ void finallyAfterIgnoresMetadataOfTheWrongType() { @Test void variantIsTheVariantKeyNotTheEvaluatedValue() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); // value and variant DIFFER, so a value-vs-variant mistake is detectable. @@ -225,7 +217,7 @@ void variantIsTheVariantKeyNotTheEvaluatedValue() { @Test void evalTimeMsComesFromMetadataWhenPresent() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final long expectedTimestamp = 1_700_000_000_000L; @@ -253,7 +245,7 @@ void evalTimeMsComesFromMetadataWhenPresent() { @Test void evalTimeMsFallsBackToCurrentTimeWhenMetadataAbsent() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final long before = System.currentTimeMillis(); @@ -274,7 +266,7 @@ void evalTimeMsFallsBackToCurrentTimeWhenMetadataAbsent() { @Test void absentVariantProducesNullVariant() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); // A runtime default returns the default value but no variant. @@ -293,7 +285,7 @@ void absentVariantProducesNullVariant() { void errorMessageCapturedFromDetailsUnderConsentOn() { // With observeFullEvaluationData=true the provider's raw message is preserved verbatim so // operators keep the diagnostic detail they opted in to. - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -321,7 +313,7 @@ void errorMessageReplacedByErrorCodeUnderConsentOff() { // provider, or a third-party provider that doesn't distinguish consent tiers), the hook must // substitute the ErrorCode name so raw PII from exception messages never reaches the wire. // Uses a PII-looking marker exactly like the wire-level guards in FlagEvaluationWriterImplTest. - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -343,15 +335,16 @@ void errorMessageReplacedByErrorCodeUnderConsentOff() { "consent-off must replace the raw message with the ErrorCode name"); assertFalse( captured.get().errorMessage.contains("jane.doe@datadoghq.com"), - "raw PII must never survive into the enqueued event under consent-off"); + "raw PII must never survive into the recorded evaluation under consent-off"); } @Test void errorMessageDroppedWhenConsentOffAndNoErrorCode() { // Edge case: no ErrorCode available (unusual — providers should set one for ERROR reason). - // Consent-off drops the message and there's nothing to substitute, so the enqueued event has + // Consent-off drops the message and there's nothing to substitute, so the recorded evaluation + // has // no error message at all. This is the strictest privacy-preserving outcome. - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -373,7 +366,7 @@ void errorMessageDroppedWhenConsentOffAndNoErrorCode() { @Test void errorCodeUsedAsFallbackWhenMessageEmpty() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -397,7 +390,7 @@ void errorCodeUsedAsFallbackWhenMessageEmpty() { @Test void successPathHasNullErrorMessage() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -412,9 +405,8 @@ void successPathHasNullErrorMessage() { // ---- test: hook does NO aggregation on the hook thread ---- @Test - void finallyAfterOnlyCallsEnqueueAndCapacityCheckOnWriter() { + void finallyAfterOnlyRecordsOnWriter() { final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(true); final FlagEvalLoggingHook hook = hookWithWriter(writer); final FlagEvaluationDetails det = @@ -422,33 +414,15 @@ void finallyAfterOnlyCallsEnqueueAndCapacityCheckOnWriter() { hook.finallyAfter(null, det, Collections.emptyMap()); - // Capacity check gates the enqueue; exactly one enqueue call; nothing else. - verify(writer, times(1)).hasCapacityForEnqueue(); - verify(writer, times(1)).enqueue(any(FlagEvalEvent.class)); + // Exactly one record call; nothing else. + verify(writer, times(1)) + .record(any(), any(), any(), any(), any(), anyLong(), anyBoolean(), any(), any()); verifyNoMoreInteractions(writer); } - @Test - void finallyAfterSkipsEnqueueAndCountsDropWhenQueueSaturated() { - final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(false); - final FlagEvalLoggingHook hook = hookWithWriter(writer); - - final FlagEvaluationDetails det = - details("flag", "v", "v", Reason.TARGETING_MATCH.name(), null); - - hook.finallyAfter(null, det, Collections.emptyMap()); - - // Pre-queue guard fires: one capacity check, one drop count, and no enqueue at all. - verify(writer, times(1)).hasCapacityForEnqueue(); - verify(writer, times(1)).countPreQueueOverflow(); - verify(writer, never()).enqueue(any(FlagEvalEvent.class)); - } - @Test void countContextTruncatedCalledWhenContextIsTruncated() { final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(true); final FlagEvalLoggingHook hook = hookWithWriter(writer); // Context with one oversized string value — triggers max_value_length. @@ -474,13 +448,13 @@ void countContextTruncatedCalledWhenContextIsTruncated() { hook.finallyAfter(hookCtx, det, Collections.emptyMap()); verify(writer, times(1)).countContextTruncated("max_value_length"); - verify(writer, times(1)).enqueue(any(FlagEvalEvent.class)); + verify(writer, times(1)) + .record(any(), any(), any(), any(), any(), anyLong(), anyBoolean(), any(), any()); } @Test void countContextTruncatedNotCalledWhenNoTruncation() { final FlagEvaluationWriter writer = mock(FlagEvaluationWriter.class); - when(writer.hasCapacityForEnqueue()).thenReturn(true); final FlagEvalLoggingHook hook = hookWithWriter(writer); final MutableContext ctx = new MutableContext("user-1"); @@ -503,7 +477,8 @@ void countContextTruncatedNotCalledWhenNoTruncation() { hook.finallyAfter(hookCtx, det, Collections.emptyMap()); verify(writer, never()).countContextTruncated(any()); - verify(writer, times(1)).enqueue(any(FlagEvalEvent.class)); + verify(writer, times(1)) + .record(any(), any(), any(), any(), any(), anyLong(), anyBoolean(), any(), any()); } // ---- test: writer=null -> no-op (killswitch off / not yet started) ---- @@ -514,7 +489,7 @@ void writerNullIsNoOp() { final FlagEvaluationDetails det = details("flag", "v", "v", Reason.TARGETING_MATCH.name(), null); - // Must not throw; nothing is enqueued + // Must not throw; nothing is recorded hook.finallyAfter(null, det, Collections.emptyMap()); } @@ -547,7 +522,7 @@ void detailsNullIsNoOp() { @Test void targetingKeyExtractedFromContext() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final FlagEvaluationDetails det = @@ -566,7 +541,7 @@ void targetingKeyExtractedFromContext() { @Test void contextAttributesAreFlattenedAndConvertedInline() { - final AtomicReference captured = new AtomicReference<>(); + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final Map profile = new HashMap<>(); @@ -604,8 +579,8 @@ void contextAttributesAreFlattenedAndConvertedInline() { } @Test - void contextAttributesUseEnqueueTimeSnapshot() { - final AtomicReference captured = new AtomicReference<>(); + void contextAttributesUseRecordTimeSnapshot() { + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final MutableContext context = new MutableContext("user-42"); @@ -649,25 +624,26 @@ void contextAttributesUseEnqueueTimeSnapshot() { @Test void readsObserveFullEvaluationDataTrueFromEvaluationMetadata() { - assertTrue(enqueuedEventWithConsentMetadata(true).observeFullEvaluationData); + assertTrue(recordedWithConsentMetadata(true).observeFullEvaluationData); } @Test void readsObserveFullEvaluationDataFalseFromEvaluationMetadata() { - assertFalse(enqueuedEventWithConsentMetadata(false).observeFullEvaluationData); + assertFalse(recordedWithConsentMetadata(false).observeFullEvaluationData); } @Test void observeFullEvaluationDataDefaultsToFalseWhenMetadataAbsent() { // No metadata at all: fail-closed toward privacy. - assertFalse(enqueuedEventWithConsentMetadata(null).observeFullEvaluationData); + assertFalse(recordedWithConsentMetadata(null).observeFullEvaluationData); } @Test void protectedPathSkipsEvaluationContextCapture() { // Consent off → the hook must not snapshot the evaluation context at all. Verified by mutating - // the context after finallyAfter returns and asserting the enqueued event still sees nothing. - final AtomicReference captured = new AtomicReference<>(); + // the context after finallyAfter returns and asserting the recorded evaluation still sees + // nothing. + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final MutableContext context = new MutableContext("user-1"); @@ -695,10 +671,10 @@ void protectedPathSkipsEvaluationContextCapture() { /** * Fires the hook once for a simple targeted evaluation whose metadata carries the given consent - * value ({@code null} = key absent) and returns the enqueued event. + * value ({@code null} = key absent) and returns the recorded evaluation. */ - private FlagEvalEvent enqueuedEventWithConsentMetadata(final Boolean consent) { - final AtomicReference captured = new AtomicReference<>(); + private RecordedEvaluation recordedWithConsentMetadata(final Boolean consent) { + final AtomicReference captured = new AtomicReference<>(); final FlagEvalLoggingHook hook = hookWithWriter(capturingWriter(captured)); final ImmutableMetadata metadata = consent == null @@ -708,7 +684,7 @@ private FlagEvalEvent enqueuedEventWithConsentMetadata(final Boolean consent) { hookCtxWithTargetingKey("obs-flag", "user-1"), details("obs-flag", "on", "on", Reason.TARGETING_MATCH.name(), metadata), Collections.emptyMap()); - assertNotNull(captured.get(), "writer.enqueue must be called once"); + assertNotNull(captured.get(), "writer.record must be called once"); return captured.get(); } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java index 028aea0a252..f5cb576a959 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java @@ -30,7 +30,6 @@ import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport; import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport.RecordingHealthMetrics; -import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.ufc.ServerConfiguration; import com.datadog.openfeature.testlog.CapturingServiceProvider; import java.io.IOException; @@ -520,7 +519,7 @@ void stopFlushesBothPipelinesWithinOneShutdownBudget() throws Exception { runtime.start(); try { runtime.recordExposure(exposure()); - runtime.evaluations().enqueue(new FlagEvalEvent("f", "v", "a", "s", 1L, emptyMap())); + runtime.evaluations().record("f", "v", "a", "s", null, 1L, false, emptyMap(), emptyMap()); final long started = System.nanoTime(); runtime.stop(); diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java new file mode 100644 index 00000000000..8c94435df7c --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java @@ -0,0 +1,601 @@ +package com.datadog.openfeature.internal.flagevaluation; + +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.DEGRADED_CAP; +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.FLAG_COUNT_CAP; +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.GLOBAL_CAP; +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.PER_FLAG_CAP; +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.contextHash; +import static com.datadog.openfeature.internal.flagevaluation.ConcurrentFlagAggregator.sameContext; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.toList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; + +class ConcurrentFlagAggregatorTest { + + private final ConcurrentFlagAggregator aggregator = new ConcurrentFlagAggregator(); + + // ---- Aggregation and bucket keys ---- + + @Test + void identicalEvaluationsAggregateIntoOneRow() { + record("flag-a", "on", "user-1", 2000L); + record("flag-a", "on", "user-1", 1000L); + + final List rows = drain(); + assertEquals(1, rows.size()); + assertEquals(2, rows.get(0).count); + assertEquals(1000L, rows.get(0).first); + assertEquals(2000L, rows.get(0).last); + assertFalse(rows.get(0).defaultUsed); + } + + @Test + void absentVariantSetsRuntimeDefaultUsed() { + record("flag-c", null, "user-1", 1000L); + + assertTrue(drain().get(0).defaultUsed); + } + + @Test + void contextValuesOfDifferentTypesSplitBuckets() { + full("flag-b", "user-1", singletonMap("score", 1)); + full("flag-b", "user-1", singletonMap("score", "1")); + full("flag-b", "user-1", singletonMap("score", 1L)); + full("flag-b", "user-1", singletonMap("score", 1)); + + assertEquals(3, drain().size()); + } + + @Test + void nulCharactersInKeyFieldsDoNotCollide() { + record("a\0b", "c", "user-1", 1000L); + record("a", "b\0c", "user-1", 1000L); + + assertEquals(2, drain().size()); + } + + @Test + void storesThePrunedContextVerbatim() { + // DDEvaluator#copyPrunedContext prunes on the evaluating thread; the aggregator keeps it as is. + final Map attrs = new HashMap<>(); + for (int i = 0; i < 100; i++) { + attrs.put("key" + i, "v" + i); + } + full("flag-d", "user-1", attrs); + + assertSame(attrs, drain().get(0).bucket.prunedAttrs); + } + + @Test + void fullTierSplitsBucketsOnEveryDimension() { + final Map context = singletonMap("plan", "pro"); + full("flag", "on", "alloc", "error", "user", context); + full("flag", "on", "alloc", "error", "user", new HashMap<>(context)); + full("other", "on", "alloc", "error", "user", context); + full("flag", "off", "alloc", "error", "user", context); + full("flag", null, "alloc", "error", "user", context); + full("flag", "on", "other", "error", "user", context); + full("flag", "on", "alloc", "other", "user", context); + full("flag", "on", "alloc", "error", "other", context); + full("flag", "on", "alloc", "error", "user", singletonMap("plan", "free")); + + final List rows = drain(); + assertEquals(8, rows.size()); + assertEquals(1, rows.stream().filter(r -> r.count == 2).count()); + } + + @Test + void mixedConsentEvaluationsOfOneSubjectLandInDistinctBuckets() { + // Consent is a full-tier key dimension: the two emit different rows (raw vs hashed targeting + // key, context vs none), and merging them would downgrade the consent-on row. + aggregator.record("fold-flag", "on", "a", "user-1", null, 1L, true, emptyMap(), emptyMap()); + aggregator.record("fold-flag", "on", "a", "user-1", null, 2L, false, emptyMap(), emptyMap()); + aggregator.record("fold-flag", "on", "a", "user-1", null, 3L, true, emptyMap(), emptyMap()); + + final List rows = drain(); + assertEquals(2, rows.size()); + for (final Row row : rows) { + assertEquals(row.bucket.observeFullEvaluationData ? 2 : 1, row.count); + } + } + + @Test + void withoutConsentDifferingContextsCollapseIntoOneBucket() { + // The context is dropped on emit, so it must not fragment buckets, or the per-flag cap blows + // out under real traffic; nor is it stored. + for (int i = 0; i < 3; i++) { + aggregator.record( + "checkout", + "on", + "a", + "alice", + null, + 1L, + false, + singletonMap("request_id", "req-" + i), + emptyMap()); + } + aggregator.record("checkout", "on", "a", "bob", null, 1L, false, emptyMap(), emptyMap()); + + final List rows = drain(); + assertEquals(2, rows.size(), "subjects still split"); + final Row alice = + rows.stream().filter(r -> "alice".equals(r.bucket.targetingKey)).findFirst().get(); + assertEquals(3, alice.count); + assertNull(alice.bucket.prunedAttrs); + } + + @Test + void withConsentDifferingContextsSplitBuckets() { + full("checkout", "alice", singletonMap("request_id", "req-1")); + full("checkout", "alice", singletonMap("request_id", "req-2")); + + assertEquals(2, drain().size()); + } + + @Test + void featuresSplitBucketsInBothTiersWhateverTheConsent() { + final Map p1 = singletonMap("bandit.policy_id", "p1"); + final Map p2 = singletonMap("bandit.policy_id", "p2"); + // Null and empty features are the same. + aggregator.record("feature-flag", "on", "a", "user-1", null, 1L, false, emptyMap(), null); + aggregator.record("feature-flag", "on", "a", "user-1", null, 1L, false, emptyMap(), emptyMap()); + aggregator.record("feature-flag", "on", "a", "user-1", null, 1L, false, emptyMap(), p1); + aggregator.record("feature-flag", "on", "a", "user-1", null, 1L, false, emptyMap(), p2); + aggregator.record("feature-flag", "on", "a", "user-1", null, 1L, false, emptyMap(), p1); + assertEquals(asList("{bandit.policy_id=p1}=2", "{bandit.policy_id=p2}=1", "{}=2"), features()); + + fillFullTier(); + aggregator.record("feature-flag", "on", "a", "user-2", null, 1L, true, emptyMap(), p1); + aggregator.record("feature-flag", "on", "a", "user-3", null, 1L, false, emptyMap(), p2); + aggregator.record("feature-flag", "on", "a", "user-4", null, 1L, false, emptyMap(), p1); + final List degraded = + drain().stream() + .filter(r -> r.bucket.degraded && "feature-flag".equals(r.bucket.flagKey)) + .collect(toList()); + assertEquals(2, degraded.size()); + for (final Row row : degraded) { + assertEquals(p1.equals(row.bucket.features) ? 2 : 1, row.count); + } + } + + // ---- Tiers and caps ---- + + @Test + void spillsIntoTheDegradedTierAtTheFlagCapAndKeepsMixedConsentOff() { + for (int i = 0; i < PER_FLAG_CAP; i++) { + aggregator.record("hot", "on", "alloc", "user-" + i, null, 1L, true, emptyMap(), emptyMap()); + } + // Over the cap: new subjects, with and without consent, share degraded rows. + aggregator.record("hot", "on", "alloc", "late-1", null, 5L, true, emptyMap(), emptyMap()); + aggregator.record("hot", null, "alloc", "late-2", null, 9L, false, emptyMap(), emptyMap()); + aggregator.record("hot", null, "alloc", "late-3", null, 7L, true, emptyMap(), emptyMap()); + // Another flag still has room in the full tier. + record("cool", "on", "user-1", 1L); + + final List degraded = new ArrayList<>(); + long full = 0; + for (final Row row : drain()) { + if (row.bucket.degraded) { + assertNull(row.bucket.targetingKey); + degraded.add( + row.bucket.variant + + " " + + row.count + + " " + + row.first + + "-" + + row.last + + " " + + row.defaultUsed + + " " + + row.bucket.observeFullEvaluationData); + } else { + full += row.count; + } + } + Collections.sort(degraded); + assertEquals(asList("null 2 7-9 true false", "on 1 5-5 false true"), degraded); + assertEquals(PER_FLAG_CAP + 1, full); + } + + @Test + void spillsIntoTheDegradedTierAtTheGlobalCap() { + fillFullTier(); + full("flag", "on", "alloc", "error", "user-1", emptyMap()); + full("flag", "on", "alloc", "error", "user-2", singletonMap("k", "v")); + full("other", "on", "alloc", "error", "user", emptyMap()); + full("flag", "off", "alloc", "error", "user", emptyMap()); + full("flag", null, "alloc", "error", "user", emptyMap()); + full("flag", "on", "other", "error", "user", emptyMap()); + full("flag", "on", "alloc", "other", "user", emptyMap()); + + final List degraded = drain().stream().filter(r -> r.bucket.degraded).collect(toList()); + assertEquals(6, degraded.size(), "the degraded tier splits on all but subject and context"); + assertEquals(0, aggregator.droppedDegradedOverflow.get()); + } + + @Test + void dropsAndCountsEvaluationsBeyondTheDegradedCap() { + fillFullTier(); + for (int i = 0; i < DEGRADED_CAP + 5; i++) { + aggregator.record("g" + i, "on", "a", "u", null, 1L, false, emptyMap(), emptyMap()); + } + // An existing degraded bucket still counts at the cap. + aggregator.record("g0", "on", "a", "u", null, 1L, false, emptyMap(), emptyMap()); + + assertEquals(5, aggregator.droppedDegradedOverflow.get()); + } + + @Test + void flagsBeyondTheFlagCountCapGoToTheDegradedTier() { + for (int i = 0; i < FLAG_COUNT_CAP; i++) { + record("flag-" + i, "on", "user", 1L); + } + record("one-too-many", "on", "user", 1L); + + final List degraded = drain().stream().filter(r -> r.bucket.degraded).collect(toList()); + assertEquals(1, degraded.size()); + assertEquals("one-too-many", degraded.get(0).bucket.flagKey); + } + + @Test + void capSizingUsesNamedScaleConstants() { + assertEquals(125_000, ConcurrentFlagAggregator.FULL_BUCKET_SIZING_BASIS); + assertEquals(10_000, ConcurrentFlagAggregator.PER_FLAG_BUCKET_SIZING_BASIS); + assertEquals(25_000, ConcurrentFlagAggregator.DEGRADED_BUCKET_SIZING_BASIS); + assertEquals(131_072, GLOBAL_CAP); + assertEquals(10_000, PER_FLAG_CAP); + assertEquals(32_768, DEGRADED_CAP); + } + + @Test + void bucketsMatchOnEveryKeyDimension() { + final Map ctx = singletonMap("k", "v"); + final Map feats = singletonMap("f", 1); + final ConcurrentFlagAggregator.Bucket full = + bucket(false, "f", "on", "a", "u", "e", ctx, true, feats); + assertTrue( + full.matches(bucket(false, "f", "on", "a", "u", "e", new HashMap<>(ctx), true, feats))); + assertFalse(full.matches(bucket(false, "x", "on", "a", "u", "e", ctx, true, feats))); + assertFalse(full.matches(bucket(false, "f", "off", "a", "u", "e", ctx, true, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "b", "u", "e", ctx, true, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "a", "w", "e", ctx, true, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "x", ctx, true, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", emptyMap(), true, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", ctx, false, feats))); + assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", ctx, true, emptyMap()))); + + final ConcurrentFlagAggregator.Bucket degraded = + bucket(true, "f", "on", "a", null, "e", null, false, feats); + assertTrue(degraded.matches(bucket(true, "f", "on", "a", "other", "e", ctx, true, feats))); + assertFalse(degraded.matches(bucket(true, "x", "on", "a", null, "e", null, false, feats))); + assertFalse(degraded.matches(bucket(true, "f", "off", "a", null, "e", null, false, feats))); + assertFalse(degraded.matches(bucket(true, "f", "on", "b", null, "e", null, false, feats))); + assertFalse(degraded.matches(bucket(true, "f", "on", "a", null, "x", null, false, feats))); + assertFalse(degraded.matches(bucket(true, "f", "on", "a", null, "e", null, false, emptyMap()))); + } + + // ---- Context hashing ---- + + @Test + void emptyAndMissingContextsAreTheSame() { + assertEquals(0L, contextHash(null)); + assertEquals(0L, contextHash(emptyMap())); + assertTrue(sameContext(null, emptyMap())); + assertTrue(sameContext(emptyMap(), null)); + assertFalse(sameContext(null, singletonMap("k", "v"))); + } + + @Test + void contextHashIgnoresOrderButNotValueTypes() { + final Map attrs = new LinkedHashMap<>(); + attrs.put("bool", true); + attrs.put("double", 1.5d); + attrs.put("float", 1.25f); + attrs.put("int", 1); + attrs.put("long", 2L); + attrs.put("null", null); + attrs.put("string", "value"); + final Map reversed = new LinkedHashMap<>(); + final List keys = new ArrayList<>(attrs.keySet()); + Collections.reverse(keys); + for (final String key : keys) { + reversed.put(key, attrs.get(key)); + } + + assertEquals(contextHash(attrs), contextHash(reversed)); + assertTrue(sameContext(attrs, reversed)); + + final Map intAsLong = new HashMap<>(attrs); + intAsLong.put("int", 1L); + assertNotEquals(contextHash(attrs), contextHash(intAsLong)); + assertFalse(sameContext(attrs, intAsLong)); + } + + @Test + void contextHashTellsSwappedValuesApart() { + final Map ab = new HashMap<>(); + ab.put("a", 1); + ab.put("b", 2); + final Map ba = new HashMap<>(); + ba.put("a", 2); + ba.put("b", 1); + + assertNotEquals(contextHash(ab), contextHash(ba)); + } + + // ---- Drain in place and idle sweep ---- + + @Test + void drainsOnlyTouchedBucketsAndResetsThem() { + assertTrue(aggregator.isEmpty()); + record("a", "on", "user", 1L); + record("a", "on", "user", 3L); + record("b", "on", "user", 2L); + assertFalse(aggregator.isEmpty()); + + assertEquals(asList("a 2 1-3", "b 1 2-2"), summaries(drain())); + assertTrue(aggregator.isEmpty()); + + record("a", "on", "user", 7L); + assertEquals(singletonList("a 1 7-7"), summaries(drain())); + assertEquals(2, aggregator.bucketCount(), "drained buckets stay for the next window"); + } + + @Test + void aRowThatThrowsDoesNotStrandTheOtherBuckets() { + record("a", "on", "user", 1L); + record("b", "on", "user", 1L); + final List seen = new ArrayList<>(); + final IllegalStateException failure = new IllegalStateException("boom"); + + assertSame( + failure, + assertThrows( + IllegalStateException.class, + () -> + aggregator.drain((bucket, count, first, last, defaultUsed) -> { + seen.add(bucket.flagKey); + throw failure; + }))); + + assertEquals(2, seen.size(), "every bucket is still drained"); + assertTrue(aggregator.isEmpty()); + record("a", "on", "user", 2L); + assertEquals(singletonList("a 1 2-2"), summaries(drain()), "and pushed again when touched"); + } + + @Test + void sweepsBucketsIdleForAWholeWindowAndFreesTheirFlagSlots() { + record("busy", "on", "user", 1L); + for (int i = 0; i < PER_FLAG_CAP; i++) { + aggregator.record("idle", "on", "alloc", "u" + i, null, 1L, false, emptyMap(), emptyMap()); + } + record("idle", "on", "spilled", 1L); + drain(); + assertEquals(0, aggregator.sweepIdle(), "all were touched in the window just drained"); + + record("busy", "on", "user", 2L); + drain(); + assertEquals(PER_FLAG_CAP + 1, aggregator.sweepIdle()); + assertEquals(1, aggregator.bucketCount()); + + // The idle buckets' flag slots were given back, so the flag fills the full tier again. + for (int i = 0; i < PER_FLAG_CAP; i++) { + aggregator.record("idle", "on", "alloc", "v" + i, null, 3L, false, emptyMap(), emptyMap()); + } + assertEquals(0, drain().stream().filter(r -> r.bucket.degraded).count()); + } + + @Test + void aBucketCreatedAfterADrainIsNotSweptBeforeItsWindowEnds() { + drain(); + record("new", "on", "user", 1L); + assertEquals(0, aggregator.sweepIdle()); + assertEquals(1, drain().size()); + } + + // ---- Concurrency ---- + + @Test + void countsEveryEvaluationFromManyThreads() throws Exception { + final int threads = 8; + final int perThread = 50_000; + runProducers(threads, perThread, () -> {}); + + final List rows = drain(); + assertEquals((long) threads * perThread, rows.stream().mapToLong(r -> r.count).sum()); + // i % 16 determines i % 4, so there are 16 distinct (flag, subject) keys, each inserted once. + assertEquals(16, rows.size()); + assertEquals(16, aggregator.bucketCount()); + } + + @Test + void losesNoEvaluationWhileDrainingConcurrently() throws Exception { + final int threads = 4; + final int perThread = 100_000; + final long[] total = {0}; + final ConcurrentFlagAggregator.Rows sum = + (bucket, count, first, last, defaultUsed) -> total[0] += count; + // Drain and sweep over and over while the producers run, then once more after. + runProducers( + threads, + perThread, + () -> { + aggregator.drain(sum); + aggregator.sweepIdle(); + }); + aggregator.drain(sum); + + assertEquals((long) threads * perThread, total[0]); + assertTrue(aggregator.isEmpty()); + } + + // ---- Helpers ---- + + /** A drained row. */ + private static final class Row { + final ConcurrentFlagAggregator.Bucket bucket; + final long count; + final long first; + final long last; + final boolean defaultUsed; + + Row( + final ConcurrentFlagAggregator.Bucket bucket, + final long count, + final long first, + final long last, + final boolean defaultUsed) { + this.bucket = bucket; + this.count = count; + this.first = first; + this.last = last; + this.defaultUsed = defaultUsed; + } + } + + private List drain() { + final List rows = new ArrayList<>(); + final int drained = + aggregator.drain((bucket, count, first, last, defaultUsed) -> + rows.add(new Row(bucket, count, first, last, defaultUsed))); + assertEquals(rows.size(), drained); + return rows; + } + + private static List summaries(final List rows) { + return rows.stream() + .map(r -> r.bucket.flagKey + " " + r.count + " " + r.first + "-" + r.last) + .sorted() + .collect(toList()); + } + + /** The drained full-tier rows' features and counts, sorted. */ + private List features() { + return drain().stream() + .filter(r -> !r.bucket.degraded) + .map(r -> r.bucket.features + "=" + r.count) + .sorted() + .collect(toList()); + } + + /** Fills the full tier to its global cap with distinct buckets, under the per-flag cap. */ + private void fillFullTier() { + for (int i = 0; i < GLOBAL_CAP; i++) { + aggregator.record( + "filler-" + (i % 20), "on", "a", "u" + i, null, 1L, false, emptyMap(), emptyMap()); + } + } + + private void record( + final String flagKey, final String variant, final String targetingKey, final long time) { + aggregator.record( + flagKey, variant, "alloc", targetingKey, null, time, false, emptyMap(), emptyMap()); + } + + /** Records an evaluation with consent, so its context is a key dimension. */ + private void full( + final String flagKey, final String targetingKey, final Map attrs) { + full(flagKey, "on", "alloc", null, targetingKey, attrs); + } + + private void full( + final String flagKey, + final String variant, + final String allocationKey, + final String errorMessage, + final String targetingKey, + final Map attrs) { + aggregator.record( + flagKey, variant, allocationKey, targetingKey, errorMessage, 1L, true, attrs, emptyMap()); + } + + private static ConcurrentFlagAggregator.Bucket bucket( + final boolean degraded, + final String flag, + final String variant, + final String allocation, + final String subject, + final String error, + final Map context, + final boolean consent, + final Map features) { + return new ConcurrentFlagAggregator.Bucket( + degraded, + 1L, + flag, + variant, + allocation, + subject, + error, + context, + consent, + features, + null, + 0); + } + + /** Runs producers on few keys, calling {@code meanwhile} repeatedly until they finish. */ + private void runProducers(final int threads, final int perThread, final Runnable meanwhile) + throws Exception { + final ExecutorService executor = Executors.newFixedThreadPool(threads); + final CountDownLatch start = new CountDownLatch(1); + try { + final List> futures = new ArrayList<>(); + for (int t = 0; t < threads; t++) { + final int thread = t; + futures.add( + executor.submit(() -> { + start.await(); + for (int i = 0; i < perThread; i++) { + // Few keys, so threads contend on the same buckets and race on insertion. + aggregator.record( + "flag-" + (i % 4), + (i & 1) == 0 ? "on" : null, + "alloc", + "user-" + (i % 16), + null, + 1_000L + thread + i, + false, + emptyMap(), + emptyMap()); + } + return null; + })); + } + start.countDown(); + for (final Future future : futures) { + while (!future.isDone()) { + meanwhile.run(); + } + future.get(60, TimeUnit.SECONDS); + } + } finally { + executor.shutdownNow(); + } + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEventTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEventTest.java deleted file mode 100644 index 2dc60610258..00000000000 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvalEventTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import static java.util.Collections.emptyMap; -import static java.util.Collections.singletonMap; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Map; -import org.junit.jupiter.api.Test; - -class FlagEvalEventTest { - @Test - void storesFeaturesAndDefaultsThemToEmpty() { - final Map features = singletonMap("bandit.policy_id", "p2"); - - assertSame( - features, - new FlagEvalEvent("f", "on", "a", "t", null, 1L, false, emptyMap(), features).features); - assertTrue( - new FlagEvalEvent("f", "on", "a", "t", null, 1L, false, emptyMap(), null) - .features.isEmpty()); - assertTrue(new FlagEvalEvent("f", "on", "a", "t", 1L, emptyMap()).features.isEmpty()); - } -} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTest.java deleted file mode 100644 index c2d0fff721b..00000000000 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTest.java +++ /dev/null @@ -1,529 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import static java.util.Collections.emptyMap; -import static java.util.Collections.singletonMap; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.Test; - -class FlagEvaluationAggregatorTest { - - @Test - void identicalEventsAggregateIntoOneBucketWithCount2() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.aggregate(event("flag-a", "on", "alloc1", "user-1", 1000L, emptyMap())); - aggregator.aggregate(event("flag-a", "on", "alloc1", "user-1", 2000L, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(1, state.fullTier.size()); - final FlagEvaluationAggregator.EvalBucket bucket = state.fullTier.get(0); - assertEquals(2, bucket.count); - assertEquals(1000L, bucket.firstEvalMs); - assertEquals(2000L, bucket.lastEvalMs); - assertTrue(bucket.firstEvalMs <= bucket.lastEvalMs); - } - - @Test - void differentValueTypesProduceDifferentBuckets() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map attrsInt = new HashMap<>(); - attrsInt.put("score", 1); - final Map attrsStr = new HashMap<>(); - attrsStr.put("score", "1"); - - aggregator.aggregate(event("flag-b", "on", "alloc1", "user-1", 1000L, true, attrsInt)); - aggregator.aggregate(event("flag-b", "on", "alloc1", "user-1", 1000L, true, attrsStr)); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(2, state.fullTier.size()); - } - - @Test - void nulCharactersInKeyFieldsDoNotCollide() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.aggregate(event("a\0b", "c", "alloc1", "user-1", 1000L, emptyMap())); - aggregator.aggregate(event("a", "b\0c", "alloc1", "user-1", 1000L, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(2, state.fullTier.size()); - } - - @Test - void globalCapOverflowRoutesToDegradedTier() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.simulateFullTierAtCap(); - aggregator.aggregate(simpleEvent("extra-flag", "on")); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertTrue(state.degradedTier.size() > 0); - assertEquals(0, state.droppedDegradedOverflow); - } - - @Test - void degradedCapOverflowIncrementsDroppedCounter() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.simulateFullTierAtCap(); - aggregator.simulateDegradedTierAtCap(); - aggregator.aggregate(simpleEvent("drop-flag", "on")); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertTrue(state.droppedDegradedOverflow > 0); - } - - @Test - void perFlagCapOverflowRoutesToDegradedTierAndMergesSameDegradedKey() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.setFlagCountForTest("hot-flag", FlagEvaluationAggregator.PER_FLAG_CAP); - - aggregator.aggregate(event("hot-flag", "on", "alloc1", "user-1", 1000L, emptyMap())); - aggregator.aggregate(event("hot-flag", "on", "alloc1", "user-2", 2000L, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(0, state.fullTier.size()); - assertEquals(1, state.degradedTier.size()); - final FlagEvaluationAggregator.EvalBucket bucket = state.degradedTier.get(0); - assertEquals(2, bucket.count); - assertEquals(1000L, bucket.firstEvalMs); - assertEquals(2000L, bucket.lastEvalMs); - } - - @Test - void mixedConsentDegradedEvaluationsMergeIntoOneBucket() { - // Mirror of mixedConsentEvaluationsForSameSubjectLandInDistinctBuckets, but for the degraded - // tier: the wire serializer for degraded rows drops the targeting key and context regardless - // of consent, so two events differing only in consent emit byte-identical JSON. They must - // share a bucket, otherwise DEGRADED_CAP is effectively halved for zero wire fidelity gain. - // The AND-fold on EvalBucket.observeFullEvaluationData still runs but the value has no - // downstream effect for degraded rows. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.setFlagCountForTest("hot-flag", FlagEvaluationAggregator.PER_FLAG_CAP); - - aggregator.aggregate(event("hot-flag", "on", "alloc1", "user-1", 1000L, true, emptyMap())); - aggregator.aggregate(event("hot-flag", "on", "alloc1", "user-2", 2000L, false, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(0, state.fullTier.size()); - assertEquals(1, state.degradedTier.size()); - final FlagEvaluationAggregator.EvalBucket bucket = state.degradedTier.get(0); - assertEquals(2, bucket.count); - // AND-fold collapses to consent-off; benign for degraded rows but a documented invariant. - assertFalse(bucket.observeFullEvaluationData); - } - - @Test - void absentVariantSetsRuntimeDefaultUsed() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.aggregate(event("flag-c", null, "alloc1", "user-1", 1000L, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(1, state.fullTier.size()); - final FlagEvaluationAggregator.EvalBucket bucket = state.fullTier.get(0); - assertTrue(bucket.runtimeDefaultUsed); - } - - @Test - void aggregatorStoresPrunedAttrsVerbatim() { - // Hot-path hook (DDEvaluator#copyPrunedContext) now delivers an already-pruned map. - // The aggregator no longer re-prunes; it stores what it is given. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map preprunedAttrs = new HashMap<>(); - for (int i = 0; i < 100; i++) { - preprunedAttrs.put("key" + i, "v" + i); - } - - aggregator.aggregate(event("flag-d", "on", "alloc1", "user-1", 1000L, true, preprunedAttrs)); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - final FlagEvaluationAggregator.EvalBucket bucket = state.fullTier.get(0); - assertEquals(100, bucket.prunedContextFieldCount()); - assertEquals(100, bucket.prunedAttrs.size()); - } - - @Test - void emptyAndMissingContextsAreTheSame() { - assertEquals(0L, FlagEvaluationAggregator.contextHash(null)); - assertEquals(0L, FlagEvaluationAggregator.contextHash(emptyMap())); - assertTrue(FlagEvaluationAggregator.sameContext(null, emptyMap())); - assertTrue(FlagEvaluationAggregator.sameContext(emptyMap(), null)); - assertFalse(FlagEvaluationAggregator.sameContext(null, singletonMap("k", "v"))); - } - - @Test - void contextHashIgnoresOrderButNotValueTypes() { - final Map attrs = new LinkedHashMap<>(); - attrs.put("bool", true); - attrs.put("double", 1.5d); - attrs.put("float", 1.25f); - attrs.put("int", 1); - attrs.put("long", 2L); - attrs.put("null", null); - attrs.put("string", "value"); - final Map reversed = new LinkedHashMap<>(); - final List keys = new ArrayList<>(attrs.keySet()); - Collections.reverse(keys); - for (final String key : keys) { - reversed.put(key, attrs.get(key)); - } - - assertEquals( - FlagEvaluationAggregator.contextHash(attrs), - FlagEvaluationAggregator.contextHash(reversed)); - assertTrue(FlagEvaluationAggregator.sameContext(attrs, reversed)); - - final Map intAsLong = new HashMap<>(attrs); - intAsLong.put("int", 1L); - assertNotEquals( - FlagEvaluationAggregator.contextHash(attrs), - FlagEvaluationAggregator.contextHash(intAsLong)); - assertFalse(FlagEvaluationAggregator.sameContext(attrs, intAsLong)); - } - - @Test - void contextHashTellsSwappedValuesApart() { - final Map ab = new HashMap<>(); - ab.put("a", 1); - ab.put("b", 2); - final Map ba = new HashMap<>(); - ba.put("a", 2); - ba.put("b", 1); - - assertNotEquals( - FlagEvaluationAggregator.contextHash(ab), FlagEvaluationAggregator.contextHash(ba)); - } - - @Test - void aggregatorStoresPrePrunedAttrsWithoutRePruning() { - // Value-length pruning moved to DDEvaluator#copyPrunedContext (hot path). The aggregator - // stores what it is given, so any long value present in the input remains present. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map preprunedAttrs = new HashMap<>(); - preprunedAttrs.put("short-val", "ok"); - - aggregator.aggregate(event("flag-e", "on", "alloc1", "user-1", 1000L, true, preprunedAttrs)); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - final FlagEvaluationAggregator.EvalBucket bucket = state.fullTier.get(0); - assertTrue(bucket.prunedAttrs.containsKey("short-val")); - } - - @Test - void capSizingUsesNamedScaleConstants() { - assertEquals(125_000, FlagEvaluationAggregator.FULL_BUCKET_SIZING_BASIS); - assertEquals(10_000, FlagEvaluationAggregator.PER_FLAG_BUCKET_SIZING_BASIS); - assertEquals(25_000, FlagEvaluationAggregator.DEGRADED_BUCKET_SIZING_BASIS); - assertEquals(131_072, FlagEvaluationAggregator.GLOBAL_CAP); - assertEquals(10_000, FlagEvaluationAggregator.PER_FLAG_CAP); - assertEquals(32_768, FlagEvaluationAggregator.DEGRADED_CAP); - } - - @Test - void flagEvalEventDoesNotCarryReason() { - final boolean hasReasonField = - Arrays.stream( - com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent.class - .getDeclaredFields()) - .anyMatch(field -> field.getName().equals("reason")); - - assertFalse(hasReasonField); - } - - @Test - void evalBucketTracksBoundsDefaultStateAndNullContextFieldCount() { - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "bucket-flag", "on", "alloc1", "user-1", null, 1000L, false, null, false, null); - - assertEquals(0, bucket.prunedContextFieldCount()); - - bucket.merge(900L, true); - bucket.merge(1100L, false); - bucket.merge(1000L, false); - - assertEquals(4, bucket.count); - assertEquals(900L, bucket.firstEvalMs); - assertEquals(1100L, bucket.lastEvalMs); - assertTrue(bucket.runtimeDefaultUsed); - } - - @Test - void fullTierSplitsBucketsOnEveryDimension() { - final Map context = singletonMap("plan", "pro"); - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "error", "user", context)); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "error", "user", context)); - assertEquals(1, aggregator.fullTierSize()); - - aggregator.aggregate(fullEvent("other", "on", "alloc", "error", "user", context)); - aggregator.aggregate(fullEvent("flag", "off", "alloc", "error", "user", context)); - aggregator.aggregate(fullEvent("flag", null, "alloc", "error", "user", context)); - aggregator.aggregate(fullEvent("flag", "on", "other", "error", "user", context)); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "other", "user", context)); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "error", "other", context)); - aggregator.aggregate( - fullEvent("flag", "on", "alloc", "error", "user", singletonMap("plan", "free"))); - - assertEquals(8, aggregator.fullTierSize()); - int merged = 0; - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.fullBuckets()) { - if (bucket.count == 2) { - merged++; - } - } - assertEquals(1, merged); - } - - @Test - void degradedTierSplitsBucketsWithoutTheSubject() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.simulateFullTierAtCap(); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "error", "user-1", emptyMap())); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "error", "user-2", emptyMap())); - assertEquals(1, aggregator.snapshot().degradedTier.size()); - - aggregator.aggregate(fullEvent("other", "on", "alloc", "error", "user", emptyMap())); - aggregator.aggregate(fullEvent("flag", "off", "alloc", "error", "user", emptyMap())); - aggregator.aggregate(fullEvent("flag", null, "alloc", "error", "user", emptyMap())); - aggregator.aggregate(fullEvent("flag", "on", "other", "error", "user", emptyMap())); - aggregator.aggregate(fullEvent("flag", "on", "alloc", "other", "user", emptyMap())); - - assertEquals(6, aggregator.snapshot().degradedTier.size()); - } - - @Test - void mixedConsentEvaluationsForSameSubjectLandInDistinctBuckets() { - // Consent is part of FullKey: two evaluations that differ only in consent produce different - // wire rows (raw vs hashed targeting key, context vs no context) and belong in different - // buckets. Merging them would silently downgrade the consent-on row to the protected shape. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.aggregate(event("fold-flag", "on", "alloc1", "user-1", 1000L, true, emptyMap())); - aggregator.aggregate(event("fold-flag", "on", "alloc1", "user-1", 2000L, false, emptyMap())); - - assertEquals(2, aggregator.fullTierSize()); - int onCount = 0; - int offCount = 0; - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.snapshot().fullTier) { - if (bucket.observeFullEvaluationData) { - onCount++; - } else { - offCount++; - } - } - assertEquals(1, onCount); - assertEquals(1, offCount); - } - - @Test - void sameConsentEvaluationsForSameSubjectMergeIntoOneBucket() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.aggregate(event("fold-flag", "on", "alloc1", "user-1", 1000L, true, emptyMap())); - aggregator.aggregate(event("fold-flag", "on", "alloc1", "user-1", 2000L, true, emptyMap())); - - final FlagEvaluationAggregator.EvalBucket bucket = aggregator.snapshot().fullTier.get(0); - assertEquals(2, bucket.count); - assertTrue(bucket.observeFullEvaluationData); - } - - @Test - void protectedPathCollapsesDifferingContextIntoOneBucket() { - // Same subject, different request-id contexts, consent off: the context is dropped on emit so - // it must not fragment full-tier buckets or the per-flag cap blows out under real traffic. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map ctx1 = new HashMap<>(); - ctx1.put("request_id", "req-1"); - final Map ctx2 = new HashMap<>(); - ctx2.put("request_id", "req-2"); - final Map ctx3 = new HashMap<>(); - ctx3.put("request_id", "req-3"); - - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 1000L, false, ctx1)); - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 2000L, false, ctx2)); - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 3000L, false, ctx3)); - - assertEquals(1, aggregator.fullTierSize()); - final FlagEvaluationAggregator.EvalBucket bucket = aggregator.snapshot().fullTier.get(0); - assertEquals(3, bucket.count); - assertFalse(bucket.observeFullEvaluationData); - assertEquals(0, bucket.prunedContextFieldCount()); - } - - @Test - void protectedPathSeparatesDifferentSubjects() { - // Different targeting keys must still fall into distinct buckets on the protected path — the - // (hashed) targeting key stays part of the aggregation identity. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 1000L, false, emptyMap())); - aggregator.aggregate(event("checkout", "on", "alloc1", "bob", 2000L, false, emptyMap())); - - assertEquals(2, aggregator.fullTierSize()); - } - - @Test - void fullPathStillSplitsBucketsOnDifferingContext() { - // Consent-on preserves the previous behaviour: distinct contexts remain distinct buckets so - // each raw context is emitted verbatim. - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map ctx1 = new HashMap<>(); - ctx1.put("plan", "pro"); - ctx1.put("request_id", "req-1"); - final Map ctx2 = new HashMap<>(); - ctx2.put("plan", "pro"); - ctx2.put("request_id", "req-2"); - - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 1000L, true, ctx1)); - aggregator.aggregate(event("checkout", "on", "alloc1", "alice", 2000L, true, ctx2)); - - assertEquals(2, aggregator.fullTierSize()); - } - - private static FlagEvalEvent event( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final long evalTimeMs, - final boolean observeFullEvaluationData, - final Map attrs) { - return new FlagEvalEvent( - flagKey, - variant, - allocationKey, - targetingKey, - null, - evalTimeMs, - observeFullEvaluationData, - attrs); - } - - private static FlagEvalEvent event( - final String flagKey, - final String variant, - final String allocationKey, - final String targetingKey, - final long evalTimeMs, - final Map attrs) { - return new FlagEvalEvent(flagKey, variant, allocationKey, targetingKey, evalTimeMs, attrs); - } - - private static FlagEvalEvent simpleEvent(final String flagKey, final String variant) { - return event(flagKey, variant, "alloc1", "user-1", 1000L, emptyMap()); - } - - private static FlagEvalEvent fullEvent( - final String flagKey, - final String variant, - final String allocationKey, - final String errorMessage, - final String targetingKey, - final Map attrs) { - return new FlagEvalEvent( - flagKey, variant, allocationKey, targetingKey, errorMessage, 1000L, true, attrs); - } - - @Test - void featuresSplitBucketsInBothTiersWhateverTheConsent() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - final Map p1 = singletonMap("bandit.policy_id", "p1"); - final Map p2 = singletonMap("bandit.policy_id", "p2"); - - aggregator.aggregate(featureEvent("user-1", 1000L, false, p1)); - aggregator.aggregate(featureEvent("user-1", 2000L, false, p2)); - aggregator.aggregate(featureEvent("user-1", 3000L, false, p1)); - - final FlagEvaluationAggregator.AggregatedState full = aggregator.snapshot(); - assertEquals(2, full.fullTier.size()); - for (final FlagEvaluationAggregator.EvalBucket bucket : full.fullTier) { - assertEquals(p1.equals(bucket.features) ? 2 : 1, bucket.count); - } - - aggregator.simulateFullTierAtCap(); - aggregator.aggregate(featureEvent("user-2", 4000L, false, p1)); - aggregator.aggregate(featureEvent("user-3", 5000L, false, p2)); - aggregator.aggregate(featureEvent("user-4", 6000L, false, p1)); - - final FlagEvaluationAggregator.AggregatedState degraded = aggregator.snapshot(); - assertEquals(2, degraded.degradedTier.size()); - for (final FlagEvaluationAggregator.EvalBucket bucket : degraded.degradedTier) { - assertEquals(p1.equals(bucket.features) ? 2 : 1, bucket.count); - } - } - - @Test - void bucketsShareOneCopyOfEachFeatureSetUntilTheWindowIsCleared() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.aggregate(featureEvent("user-1", 1000L, false, policy("p1"))); - aggregator.aggregate(featureEvent("user-2", 2000L, false, policy("p1"))); - aggregator.aggregate(featureEvent("user-3", 3000L, false, policy("p2"))); - - final List p1Buckets = new ArrayList<>(); - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.fullBuckets()) { - if (policy("p1").equals(bucket.features)) { - p1Buckets.add(bucket); - } - } - assertEquals(2, p1Buckets.size()); - assertSame(p1Buckets.get(0).features, p1Buckets.get(1).features); - assertEquals(2, aggregator.featureSets.size()); - - aggregator.simulateFullTierAtCap(); - aggregator.aggregate(featureEvent("user-4", 4000L, false, policy("p1"))); - for (final FlagEvaluationAggregator.EvalBucket bucket : aggregator.degradedBuckets()) { - assertSame(p1Buckets.get(0).features, bucket.features); - } - - aggregator.clear(); - assertEquals(0, aggregator.featureSets.size()); - } - - @Test - void bucketsWithoutFeaturesShareTheEmptyFeatureSet() { - final FlagEvaluationAggregator aggregator = new FlagEvaluationAggregator(); - - aggregator.aggregate(featureEvent("user-1", 1000L, false, null)); - aggregator.aggregate(featureEvent("user-1", 2000L, false, emptyMap())); - - final FlagEvaluationAggregator.AggregatedState state = aggregator.snapshot(); - assertEquals(1, state.fullTier.size()); - assertSame(FlagEvaluationAggregator.FeatureSet.NONE.features, state.fullTier.get(0).features); - assertEquals(0, aggregator.featureSets.size()); - } - - private static Map policy(final String id) { - final Map features = new HashMap<>(); - features.put("bandit.policy_id", id); - return features; - } - - private static FlagEvalEvent featureEvent( - final String targetingKey, - final long evalTimeMs, - final boolean observeFullEvaluationData, - final Map features) { - return new FlagEvalEvent( - "feature-flag", - "on", - "alloc1", - targetingKey, - null, - evalTimeMs, - observeFullEvaluationData, - emptyMap(), - features); - } -} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTiersTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTiersTest.java deleted file mode 100644 index 8206d01226c..00000000000 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationAggregatorTiersTest.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.datadog.openfeature.internal.flagevaluation; - -import static java.util.Collections.singletonMap; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.HashMap; -import java.util.Map; -import java.util.NoSuchElementException; -import org.junit.jupiter.api.Test; -import org.tabletest.junit.TableTest; - -/** - * The tier matching strategies compare every dimension after the hash. Real events essentially - * never collide on the 64-bit hash, so these tests give buckets the lookup's hash and change one - * dimension at a time. - */ -class FlagEvaluationAggregatorTiersTest { - private static final FlagEvalEvent EVENT = - new FlagEvalEvent("flag", "variant", "allocation", "subject", "error", 1L, true, null); - private static final Map CONTEXT = singletonMap("key", "value"); - private static final FlagEvaluationAggregator.FeatureSet FEATURES = - new FlagEvaluationAggregator.FeatureSet(singletonMap("feature", 1)); - - @TableTest({ - "dimension | matches", - "none | true ", - "hash | false ", - "consent | false ", - "flag | false ", - "variant | false ", - "allocation | false ", - "error | false ", - "subject | false ", - "context | false ", - "features | false " - }) - void fullTierComparesEveryDimension(final String dimension, final boolean matches) { - final FlagEvaluationAggregator.Lookup lookup = lookup(); - final FlagEvaluationAggregator.EvalBucket bucket = - bucket(lookup.fullTierHash(), dimension, true); - - assertEquals( - matches, new FlagEvaluationAggregator.FullTier().matches(bucket, lookup), dimension); - } - - @TableTest({ - "dimension | matches", - "none | true ", - "hash | false ", - "flag | false ", - "variant | false ", - "allocation | false ", - "error | false ", - "features | false ", - "consent | true ", - "subject | true ", - "context | true " - }) - void degradedTierIgnoresTheSubjectContextAndConsent( - final String dimension, final boolean matches) { - final FlagEvaluationAggregator.Lookup lookup = lookup(); - final FlagEvaluationAggregator.EvalBucket bucket = - bucket(lookup.degradedTierHash(), dimension, false); - - assertEquals( - matches, new FlagEvaluationAggregator.DegradedTier().matches(bucket, lookup), dimension); - } - - @Test - void bucketsSkipEmptySlotsAndEndWithNoSuchElement() { - final FlagEvaluationAggregator.EvalBucket only = bucket(1L, "none", true); - final FlagEvaluationAggregator.Buckets buckets = - new FlagEvaluationAggregator.Buckets( - new FlagEvaluationAggregator.EvalBucket[] {null, only, null}); - - assertTrue(buckets.hasNext()); - assertSame(only, buckets.next()); - assertFalse(buckets.hasNext()); - assertThrows(NoSuchElementException.class, buckets::next); - } - - private static FlagEvaluationAggregator.Lookup lookup() { - return new FlagEvaluationAggregator.Lookup() - .of(EVENT, CONTEXT, FlagEvaluationAggregator.contextHash(CONTEXT), FEATURES); - } - - /** - * A bucket equal to {@link #EVENT} in every dimension except {@code differs}. Its context is an - * equal copy, since contexts compare by value, but its features are the canonical instance, since - * feature sets compare by identity. - */ - private static FlagEvaluationAggregator.EvalBucket bucket( - final long hash, final String differs, final boolean fullTier) { - final String other = "other"; - return new FlagEvaluationAggregator.EvalBucket( - "hash".equals(differs) ? hash + 1 : hash, - "flag".equals(differs) ? other : EVENT.flagKey, - "variant".equals(differs) ? other : EVENT.variant, - "allocation".equals(differs) ? other : EVENT.allocationKey, - "subject".equals(differs) ? other : EVENT.targetingKey, - "error".equals(differs) ? other : EVENT.errorMessage, - 1L, - false, - "context".equals(differs) ? singletonMap("key", (Object) other) : new HashMap<>(CONTEXT), - "consent".equals(differs) - ? !EVENT.observeFullEvaluationData - : EVENT.observeFullEvaluationData, - "features".equals(differs) ? singletonMap("feature", (Object) 2) : FEATURES.features); - } -} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java index 29d07305cc1..9da5e2ae39f 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java @@ -51,20 +51,15 @@ void omitsNullMapEntriesAndPreservesNullArrayElements() throws Exception { final Map attrs = new HashMap<>(); attrs.put("present", "value"); attrs.put("missing", null); - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( + final ConcurrentFlagAggregator.Bucket bucket = + bucket( + true, "flag", - "on", - "alloc", "user", - null, - EVAL_MS, - false, attrs, true, singletonMap("items", asList(null, singletonMap("missing", null)))); - final FlagEvaluationPayloads.FlagEvaluationEvent row = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, true, true, EVAL_MS); + final FlagEvaluationPayloads.FlagEvaluationEvent row = row(bucket); final Map event = firstEvent(onlyPayload(row)); @@ -76,10 +71,8 @@ void omitsNullMapEntriesAndPreservesNullArrayElements() throws Exception { @Test void eventFromFullBucketUsesFlushTimeAndEvaluationBounds() throws Exception { - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "ts-flag", "on", "alloc1", "user-1", null, EVAL_MS, false, emptyMap(), true, null); - bucket.merge(EVAL_MS + 10, false); + final ConcurrentFlagAggregator.Bucket bucket = + bucket(true, "ts-flag", "user-1", emptyMap(), true, emptyMap()); final long flushTimeMs = EVAL_MS + 5_000; final Map json = @@ -87,7 +80,7 @@ void eventFromFullBucketUsesFlushTimeAndEvaluationBounds() throws Exception { FlagEvaluationPayloads.buildPayloads( java.util.Collections.singletonList( FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, true, true, flushTimeMs)), + bucket, 2, EVAL_MS, EVAL_MS + 10, false, flushTimeMs)), CONTEXT, 1_000_000)); @@ -100,18 +93,14 @@ void eventFromFullBucketUsesFlushTimeAndEvaluationBounds() throws Exception { @Test void degradedTierEventOmitsTargetingKeyAndContext() throws Exception { - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "dg-flag", "on", "alloc1", null, null, EVAL_MS, false, null, false, null); + // Consent on, so only the tier drops the targeting key and context. + final ConcurrentFlagAggregator.Bucket bucket = + bucket(false, "dg-flag", "user-1", singletonMap("region", "us-east-1"), true, emptyMap()); final Map json = firstPayload( FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, false, true, EVAL_MS)), - CONTEXT, - 1_000_000)); + java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000)); final Map ev = firstEvent(json); assertNull(ev.get("targeting_key")); @@ -122,27 +111,13 @@ void degradedTierEventOmitsTargetingKeyAndContext() throws Exception { void fullTierWithObserveFullEvaluationDataTrueEmitsRawTargetingKeyAndContext() throws Exception { final Map attrs = new HashMap<>(); attrs.put("region", "us-east-1"); - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "pii-flag", - "on", - "alloc1", - "jane.doe@datadoghq.com", - null, - EVAL_MS, - false, - attrs, - true, - null); + final ConcurrentFlagAggregator.Bucket bucket = + bucket(true, "pii-flag", "jane.doe@datadoghq.com", attrs, true, emptyMap()); final Map json = firstPayload( FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, true, true, EVAL_MS)), - CONTEXT, - 1_000_000)); + java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000)); final Map ev = firstEvent(json); assertEquals("jane.doe@datadoghq.com", ev.get("targeting_key")); @@ -158,26 +133,12 @@ void fullTierWithObserveFullEvaluationDataFalseHashesTargetingKeyAndOmitsContext throws Exception { final Map attrs = new HashMap<>(); attrs.put("region", "us-east-1"); - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "pii-flag", - "on", - "alloc1", - "jane.doe@datadoghq.com", - null, - EVAL_MS, - false, - attrs, - false, - null); + final ConcurrentFlagAggregator.Bucket bucket = + bucket(true, "pii-flag", "jane.doe@datadoghq.com", attrs, false, emptyMap()); final FlagEvaluationPayloads.EncodedPayloads payloads = FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, true, false, EVAL_MS)), - CONTEXT, - 1_000_000); + java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000); final String rawJson = new String(payloads.bodies.get(0), java.nio.charset.StandardCharsets.UTF_8); @@ -468,14 +429,10 @@ void rowsCarryFeaturesInEveryTierAndWithoutConsent() throws Exception { final Map features = new HashMap<>(); features.put("bandit.policy_id", "p2"); features.put("bandit.weight", 0.5); - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "feature-flag", "on", "alloc1", "user-1", null, EVAL_MS, false, null, false, features); - final FlagEvaluationPayloads.FlagEvaluationEvent full = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, true, false, EVAL_MS); + row(bucket(true, "feature-flag", "user-1", null, false, features)); final FlagEvaluationPayloads.FlagEvaluationEvent degraded = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, false, false, EVAL_MS); + row(bucket(false, "feature-flag", null, null, false, features)); assertEquals(features, firstEvent(onlyPayload(full)).get("features")); assertEquals(features, firstEvent(onlyPayload(degraded)).get("features")); @@ -484,17 +441,62 @@ void rowsCarryFeaturesInEveryTierAndWithoutConsent() throws Exception { @Test void rowsWithoutFeaturesOmitTheField() throws Exception { - final FlagEvaluationAggregator.EvalBucket bucket = - new FlagEvaluationAggregator.EvalBucket( - "plain-flag", "on", "alloc1", null, null, EVAL_MS, false, null, false, emptyMap()); - final FlagEvaluationPayloads.FlagEvaluationEvent row = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, false, false, EVAL_MS); + row(bucket(false, "plain-flag", null, null, false, emptyMap())); assertNull(row.features); assertFalse(firstEvent(onlyPayload(row)).containsKey("features")); } + /** A bucket of flag variant "on" and allocation "alloc1", in the full or the degraded tier. */ + private static ConcurrentFlagAggregator.Bucket bucket( + final boolean fullTier, + final String flagKey, + final String targetingKey, + final Map attrs, + final boolean observeFullEvaluationData, + final Map features) { + return new ConcurrentFlagAggregator.Bucket( + !fullTier, + 1L, + flagKey, + "on", + "alloc1", + targetingKey, + null, + attrs, + observeFullEvaluationData, + features, + null, + 0); + } + + /** The row of one evaluation of {@code bucket} at {@link #EVAL_MS}. */ + private static FlagEvaluationPayloads.FlagEvaluationEvent row( + final ConcurrentFlagAggregator.Bucket bucket) { + return FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( + bucket, 1, EVAL_MS, EVAL_MS, false, EVAL_MS); + } + + @Test + void rowsWhoseTimestampsRacedTheDrainReadAsTheFlushTime() { + final ConcurrentFlagAggregator.Bucket bucket = + bucket(true, "raced-flag", "user-1", null, false, emptyMap()); + + final FlagEvaluationPayloads.FlagEvaluationEvent untimed = + FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( + bucket, 1, Long.MAX_VALUE, Long.MIN_VALUE, false, 42L); + final FlagEvaluationPayloads.FlagEvaluationEvent timed = + FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, 1, 5L, 9L, true, 42L); + + assertEquals(42L, untimed.first_evaluation); + assertEquals(42L, untimed.last_evaluation); + assertNull(untimed.runtime_default_used); + assertEquals(5L, timed.first_evaluation); + assertEquals(9L, timed.last_evaluation); + assertEquals(Boolean.TRUE, timed.runtime_default_used); + } + private static Map onlyPayload( final FlagEvaluationPayloads.FlagEvaluationEvent row) throws Exception { return firstPayload( diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java index 433e8e26135..af191cc1caa 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java @@ -5,11 +5,11 @@ import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.event; import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.eventForFlag; import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.flushAndCapture; -import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.flushAndCaptureJson; import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.readJson; import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.repeat; import static com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.simpleEvent; import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -17,7 +17,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.datadog.openfeature.internal.EventQueue; import com.datadog.openfeature.internal.RuntimeServices; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.CapturingTransport; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationTestSupport.RecordingMetrics; @@ -25,10 +24,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; import org.slf4j.event.Level; @@ -38,22 +35,16 @@ class FlagEvaluationPipelineTest { private final CapturingTransport transport = new CapturingTransport(); private final RecordingMetrics metrics = new RecordingMetrics(); - private FlagEvaluationPipeline writer( - final int capacity, final long flushInterval, final TimeUnit unit) { + private FlagEvaluationPipeline writer(final long flushInterval, final TimeUnit unit) { return new FlagEvaluationPipeline( - capacity, - flushInterval, - unit, - this.transport, - context(), - new RuntimeServices(this.metrics)); + flushInterval, unit, this.transport, context(), new RuntimeServices(this.metrics)); } @Test void degradedCapOverflowTelemetryIsEmittedOnFlush() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.addDroppedDegradedOverflowForTest(3); + setup.aggregator.droppedDegradedOverflow.addAndGet(3); setup.handler.flush(); assertEquals( @@ -69,9 +60,9 @@ void reportsADeliveredFlushAndTheShutdownRequest() { CapturingServiceProvider.enable("com.datadog.openfeature", Level.DEBUG); try { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.add(simpleEvent("logged-flag", "on")); + setup.add(simpleEvent("logged-flag", "on")); flushAndCapture(setup); - writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS).requestShutdown(); + writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS).requestShutdown(); final List messages = CapturingServiceProvider.events().stream() @@ -93,7 +84,7 @@ void signalsTheWorkerWhenTheShutdownLogFails() { CapturingServiceProvider.reset(); CapturingServiceProvider.enable("com.datadog.openfeature", Level.DEBUG); CapturingServiceProvider.failOn("flag evaluation shutdown requested"); - final FlagEvaluationPipeline writer = writer(16, 10L, TimeUnit.SECONDS); + final FlagEvaluationPipeline writer = writer(10L, TimeUnit.SECONDS); writer.start(); try { assertThrows(IllegalStateException.class, writer::requestShutdown); @@ -106,141 +97,156 @@ void signalsTheWorkerWhenTheShutdownLogFails() { } @Test - void queueOverflowIncrementsObservableDropCounter() { - final FlagEvaluationPipeline writer = writer(2, 10L, TimeUnit.SECONDS); - - for (int i = 0; i < 100; i++) { - writer.enqueue(simpleEvent("of-flag", "on")); - } - - assertTrue(writer.droppedQueueOverflow() > 0); - final long queueDrops = writer.droppedQueueOverflow(); - writer.flushForTest(); - assertEquals( - queueDrops, - this.metrics.sum( - FlagEvaluationPipeline.FLAG_EVALUATION_DROPPED_METRIC, - "reason:" + FlagEvaluationPipeline.DROP_REASON_QUEUE_OVERFLOW)); - } - - @Test - void enqueueAfterCloseIsDroppedAndCounted() { - final FlagEvaluationPipeline writer = writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS); + void recordAfterCloseIsDroppedAndCounted() { + final FlagEvaluationPipeline writer = writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS); writer.close(); - writer.enqueue(simpleEvent("closed-flag", "on")); + simpleEvent("closed-flag", "on").recordInto(writer); + writer.flushForTest(); assertEquals( 1, this.metrics.sum( FlagEvaluationPipeline.FLAG_EVALUATION_DROPPED_METRIC, "reason:" + FlagEvaluationPipeline.DROP_REASON_CLOSED)); - assertNull(writer.pollQueuedEventForTest()); + assertTrue(this.transport.posts.isEmpty()); } @Test - void closeSweepsAndCountsEventsLeftInTheQueue() { - final FlagEvaluationPipeline writer = writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS); + void closeCountsEvaluationsTheWorkerNeverFlushed() { + final FlagEvaluationPipeline writer = writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS); - // The worker is never started, so nothing drains these; close() must account for them rather + // The worker is never started, so nothing flushes these; close() must account for them rather // than leave them silently stranded. Stands in for the narrow window where a lock-free - // producer offers after the worker's final drain. - writer.enqueue(simpleEvent("residual-flag-1", "on")); - writer.enqueue(simpleEvent("residual-flag-2", "on")); + // producer records after the worker's final drain. + simpleEvent("residual-flag-1", "on").recordInto(writer); + simpleEvent("residual-flag-1", "on").recordInto(writer); + simpleEvent("residual-flag-2", "on").recordInto(writer); writer.close(); assertEquals( - 2, + 3, this.metrics.sum( FlagEvaluationPipeline.FLAG_EVALUATION_DROPPED_METRIC, "reason:" + FlagEvaluationPipeline.DROP_REASON_CLOSED)); - assertNull(writer.pollQueuedEventForTest()); + writer.flushForTest(); + assertTrue(this.transport.posts.isEmpty(), "the residual evaluations were drained"); } @Test - void enqueueIgnoresNullEvent() { - final FlagEvaluationPipeline writer = writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS); + void recordAggregatesOnTheEvaluatingThread() throws Exception { + final FlagEvaluationPipeline writer = writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS); - writer.enqueue(null); + simpleEvent("caller-flag", "on").recordInto(writer); + simpleEvent("caller-flag", "on").recordInto(writer); + writer.flushForTest(); - assertNull(writer.pollQueuedEventForTest()); - assertEquals(0, writer.droppedQueueOverflow()); + assertEquals(1, this.transport.posts.size()); + final Map ev = + eventForFlag(readJson(this.transport.posts.get(0).body).parsed, "caller-flag"); + assertNotNull(ev); + assertEquals(2.0, ((Number) ev.get("evaluation_count")).doubleValue()); } @Test - void enqueueDoesNotAggregateOnTheCallingThread() { - final FlagEvaluationPipeline writer = writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS); + void recordNeverThrowsIntoTheEvaluation() { + final FlagEvaluationPipeline writer = writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS); + final Object unhashable = + new Object() { + @Override + public int hashCode() { + throw new IllegalStateException("boom"); + } + }; - writer.enqueue(simpleEvent("g2-flag", "on")); - writer.enqueue(simpleEvent("g2-flag", "on")); + writer.record( + "bad-flag", "on", "alloc1", "user-1", null, 1L, true, singletonMap("k", unhashable), null); + writer.flushForTest(); - assertEquals(0, writer.aggregatorFullTierSizeForTest()); - assertEquals(0, writer.droppedQueueOverflow()); + assertTrue(this.transport.posts.isEmpty()); } @Test - void flushIfNecessarySkipsEmptyStateAndWaitsForInterval() { + void flushWithNothingRecordedPostsNothing() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.flushIfNecessary(); - setup.handler.add(simpleEvent("pending-flag", "on")); - setup.handler.drainAndAggregate(); - setup.handler.flushIfNecessary(); + setup.handler.flush(); + setup.add(simpleEvent("once-flag", "on")); + setup.handler.flush(); + setup.handler.flush(); - assertEquals(1, setup.handler.fullTierSizeForTest()); + assertEquals(1, setup.transport.posts.size()); } @Test - void flushIfNecessaryDoesNotReturnEarlyWhenOnlyQueueDropsArePending() { - final AtomicLong queueDrops = new AtomicLong(1); - final FlagEvaluationPipeline.FlagEvaluationSerializingHandler handler = - new FlagEvaluationPipeline.FlagEvaluationSerializingHandler( - this.transport, - new EventQueue<>(16), - Long.MAX_VALUE, - TimeUnit.NANOSECONDS, - context(), - queueDrops, - new ConcurrentHashMap<>(), - FlagEvaluationPipeline.FLAG_EVALUATION_PAYLOAD_SIZE_LIMIT_BYTES, - new RuntimeServices(this.metrics)); - - handler.flushIfNecessary(); - - assertEquals(1, queueDrops.get()); + void flushSweepsBucketsIdleForAWholeWindow() { + final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); + + setup.add(simpleEvent("idle-flag", "on")); + setup.handler.flush(); + assertEquals(1, setup.aggregator.bucketCount(), "kept through the window it was touched in"); + setup.handler.flush(); + assertEquals(0, setup.aggregator.bucketCount()); } @Test - void degradedBucketsAreSerializedWithoutTargetingKeyOrContext() throws Exception { + void anUnrelatedInterruptStillFlushesWithoutTheFlagAndRestoresIt() throws Exception { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.addDegradedBucketForTest("degraded-flag", "on", "alloc1", null, 1000L); - - final Map json = flushAndCaptureJson(setup); - - final Map ev = eventForFlag(json, "degraded-flag"); - assertNotNull(ev); - assertNull(ev.get("targeting_key")); - assertNull(ev.get("context")); + final boolean[] interruptedDuringPost = {true}; + setup.transport.onPost = + () -> interruptedDuringPost[0] = Thread.currentThread().isInterrupted(); + final boolean[] interruptedAfter = {false}; + final Thread worker = + new Thread(() -> { + setup.handler.run(); + interruptedAfter[0] = Thread.currentThread().isInterrupted(); + }); + setup.add(simpleEvent("interrupted-flag", "on")); + + worker.start(); + worker.interrupt(); + worker.join(TimeUnit.SECONDS.toMillis(5)); + + assertFalse(worker.isAlive()); + assertEquals(1, setup.transport.posts.size()); + assertFalse(interruptedDuringPost[0], "the final flush must not run interrupted"); + assertTrue(interruptedAfter[0], "the interrupt is restored"); } @Test - void testHandlerCanSimulateAndClearDegradedTierAtCap() { + void degradedRowsAreSerializedWithoutTargetingKeyOrContextAndCounted() throws Exception { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); + for (int i = 0; i < ConcurrentFlagAggregator.PER_FLAG_CAP; i++) { + setup.add(event("hot-flag", "on", "alloc1", "user-" + i, 1000L, emptyMap())); + } + // Over the per-flag cap: the next subjects spill into one degraded row. + setup.add(event("hot-flag", "on", "alloc1", "late-1", 1000L, true, piiAttrs())); + setup.add(event("hot-flag", "on", "alloc1", "late-2", 1000L, true, piiAttrs())); - setup.handler.simulateDegradedTierAtCap(); - setup.handler.clearAggregationForTest(); - setup.handler.add(simpleEvent("after-clear", "on")); - setup.handler.drainAndAggregate(); + final FlagEvaluationTestSupport.CapturedJson captured = flushAndCapture(setup); - assertEquals(1, setup.handler.fullTierSizeForTest()); + @SuppressWarnings("unchecked") + final List> rows = + (List>) (List) captured.parsed.get("flagEvaluations"); + final List> degraded = + rows.stream().filter(r -> !r.containsKey("targeting_key")).collect(Collectors.toList()); + assertEquals(1, degraded.size()); + assertEquals(2.0, ((Number) degraded.get(0).get("evaluation_count")).doubleValue()); + assertNull(degraded.get(0).get("context")); + assertFalse(captured.raw.contains("late-1")); + assertFalse(captured.raw.contains("us-east-1")); + assertEquals( + 2, + setup.metrics.sum( + FlagEvaluationPipeline.FLAG_EVALUATION_DEGRADED_METRIC, + "reason:" + FlagEvaluationPipeline.DEGRADED_REASON_CARDINALITY_CAP)); } @Test void payloadLimitDropsAreCountedOnFlush() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(128); - setup.handler.add(event(repeat('f', 512), "on", "alloc1", "user-1", 1000L, emptyMap())); + setup.add(event(repeat('f', 512), "on", "alloc1", "user-1", 1000L, emptyMap())); - setup.handler.drainAndAggregate(); setup.handler.flush(); assertEquals( @@ -252,9 +258,8 @@ void payloadLimitDropsAreCountedOnFlush() { @Test void finalFlushRunsWithoutTheInterruptFlagSet() throws Exception { - // close() interrupts the worker to break it out of poll(). The final flush does socket I/O, - // which fails fast on an interrupted thread, so the flag must be clear by the time the - // transport is called. + // The final flush does socket I/O, which fails fast on an interrupted thread, so the flag must + // be clear by the time the transport is called. final CountDownLatch posted = new CountDownLatch(1); final boolean[] interruptedDuringPost = {true}; this.transport.onPost = @@ -262,10 +267,10 @@ void finalFlushRunsWithoutTheInterruptFlagSet() throws Exception { interruptedDuringPost[0] = Thread.currentThread().isInterrupted(); posted.countDown(); }; - final FlagEvaluationPipeline writer = writer(64, TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS); + final FlagEvaluationPipeline writer = writer(TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS); writer.start(); - writer.enqueue(simpleEvent("interrupt-flag", "on")); + simpleEvent("interrupt-flag", "on").recordInto(writer); writer.close(); assertTrue(posted.await(5, TimeUnit.SECONDS)); @@ -275,13 +280,13 @@ void finalFlushRunsWithoutTheInterruptFlagSet() throws Exception { } @Test - void closeDrainsAndFinalFlushesQueuedEvents() throws Exception { + void closeFinalFlushesRecordedEvaluations() throws Exception { final CountDownLatch posted = new CountDownLatch(1); this.transport.onPost = posted::countDown; - final FlagEvaluationPipeline writer = writer(64, TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS); + final FlagEvaluationPipeline writer = writer(TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS); writer.start(); - writer.enqueue(simpleEvent("shutdown-flag", "on")); + simpleEvent("shutdown-flag", "on").recordInto(writer); writer.close(); assertTrue(posted.await(5, TimeUnit.SECONDS)); @@ -291,14 +296,14 @@ void closeDrainsAndFinalFlushesQueuedEvents() throws Exception { @Test void continuousTrafficFlushesWithoutWaitingForIdle() throws Exception { - final FlagEvaluationPipeline writer = writer(1 << 12, 1, TimeUnit.MILLISECONDS); + final FlagEvaluationPipeline writer = writer(1, TimeUnit.MILLISECONDS); writer.start(); boolean posted = false; try { final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); while (System.nanoTime() < deadline) { - writer.enqueue(simpleEvent("busy-flag", "on")); + simpleEvent("busy-flag", "on").recordInto(writer); if (!this.transport.posts.isEmpty()) { posted = true; break; @@ -315,8 +320,7 @@ void continuousTrafficFlushesWithoutWaitingForIdle() throws Exception { void flushPostsToFlagevaluationEndpoint() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.add(event("flag-f", "on", "alloc1", "user-1", 1000L, emptyMap())); - setup.handler.drainAndAggregate(); + setup.add(event("flag-f", "on", "alloc1", "user-1", 1000L, emptyMap())); setup.handler.flush(); assertEquals(1, setup.transport.posts.size()); @@ -332,10 +336,9 @@ void splitPostFailureDoesNotRetryAlreadySentPayloads() { for (int i = 0; i < 4; i++) { final Map attrs = new HashMap<>(); attrs.put("payload", repeat('x', 180)); - setup.handler.add(event("split-failure-" + i, "on", "alloc1", "user-" + i, 1000L, attrs)); + setup.add(event("split-failure-" + i, "on", "alloc1", "user-" + i, 1000L, attrs)); } - setup.handler.drainAndAggregate(); setup.handler.flush(); assertEquals(2, setup.transport.posts.size()); setup.handler.flush(); @@ -347,10 +350,10 @@ void splitPostFailureDoesNotRetryAlreadySentPayloads() { @Test void observeFullEvaluationDataTrueEmitsRawTargetingKeyAndContext() throws Exception { - // Consent travels on the event (snapshotted by the hook at evaluation time); the writer honours - // it verbatim. + // Consent travels with the evaluation (snapshotted by the hook at evaluation time); the writer + // honours it verbatim. final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.add(piiEvent(true)); + setup.add(piiEvent(true)); final Map json = flushAndCapture(setup).parsed; @@ -369,14 +372,6 @@ void observeFullEvaluationDataFalseHashesTargetingKeyAndOmitsContext() throws Ex assertHashedTargetingKeyAndOmittedContext(piiEvent(false)); } - @Test - void flagEvalEventDefaultConsentHashesTargetingKeyAndOmitsContext() throws Exception { - // An event built without an explicit consent value defaults to the privacy-preserving false, so - // it must behave exactly like the explicit "false" case. This is the state the hook produces - // when no UFC has been received. - assertHashedTargetingKeyAndOmittedContext(piiEventDefaultConsent()); - } - @Test void consentOffPreservesErrorCodeSignalAndNeverLeaksPiiInErrorMessage() throws Exception { // Upstream contract: the hook substitutes the ErrorCode name for the raw exception message @@ -386,8 +381,8 @@ void consentOffPreservesErrorCodeSignalAndNeverLeaksPiiInErrorMessage() throws E // stable ErrorCode signal for operators and (b) never lets a PII-shaped string escape onto the // wire. Mirrors the existing PII guards on the targeting_key axis. final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.add( - new FlagEvalEvent( + setup.add( + new RecordedEvaluation( "err-flag", null, "alloc1", @@ -395,6 +390,7 @@ void consentOffPreservesErrorCodeSignalAndNeverLeaksPiiInErrorMessage() throws E "TYPE_MISMATCH", 1000L, false, + emptyMap(), emptyMap())); final FlagEvaluationTestSupport.CapturedJson captured = flushAndCapture(setup); @@ -416,14 +412,12 @@ void deliveryFailureClearsAggregatorSoLaterFlushesRecover() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); setup.transport.failingPost = index -> index == 1; - setup.handler.add(simpleEvent("failed-flag", "on")); - setup.handler.drainAndAggregate(); + setup.add(simpleEvent("failed-flag", "on")); setup.handler.flush(); assertEquals(1, setup.transport.posts.size()); - // The bucket must not survive the failed flush. A follow-up healthy event flushes cleanly. - setup.handler.add(simpleEvent("healthy-flag", "on")); - setup.handler.drainAndAggregate(); + // The drained row must not survive the failed flush. A follow-up healthy one flushes cleanly. + setup.add(simpleEvent("healthy-flag", "on")); setup.handler.flush(); assertEquals(2, setup.transport.posts.size()); final Map json = readJson(setup.transport.posts.get(1).body).parsed; @@ -436,7 +430,7 @@ void nonFiniteContextNumbersAreSerializedAsStrings() { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); final Map attrs = new HashMap<>(); attrs.put("bad-number", Double.NaN); - setup.handler.add(event("nan-flag", "on", "alloc1", "user-1", 1000L, true, attrs)); + setup.add(event("nan-flag", "on", "alloc1", "user-1", 1000L, true, attrs)); final FlagEvaluationTestSupport.CapturedJson captured = flushAndCapture(setup); @@ -445,7 +439,7 @@ void nonFiniteContextNumbersAreSerializedAsStrings() { @Test void countContextTruncatedAccumulatesPerReason() { - final FlagEvaluationPipeline writer = writer(16, Long.MAX_VALUE, TimeUnit.NANOSECONDS); + final FlagEvaluationPipeline writer = writer(Long.MAX_VALUE, TimeUnit.NANOSECONDS); writer.countContextTruncated("field_count"); writer.countContextTruncated("field_count"); @@ -463,36 +457,10 @@ void countContextTruncatedAccumulatesPerReason() { "reason:field_length")); } - @Test - void hasCapacityForEnqueueReflectsQueueSaturationAndCountsPreQueueOverflow() { - final int capacity = 2; - final FlagEvaluationPipeline writer = writer(capacity, Long.MAX_VALUE, TimeUnit.NANOSECONDS); - - assertTrue(writer.hasCapacityForEnqueue()); - - // Saturate the hand-off queue without starting the worker so no drain can free slots. - for (int i = 0; i < capacity; i++) { - writer.enqueue(simpleEvent("cap-flag-" + i, "on")); - } - assertFalse(writer.hasCapacityForEnqueue()); - - // Simulate a pre-queue overflow account by the hook and surface it on flush. - writer.countPreQueueOverflow(); - writer.flushForTest(); - - assertEquals( - 1, - this.metrics.sum( - FlagEvaluationPipeline.FLAG_EVALUATION_DROPPED_METRIC, - "reason:" + FlagEvaluationPipeline.DROP_REASON_QUEUE_OVERFLOW)); - - writer.close(); - } - - private void assertHashedTargetingKeyAndOmittedContext(final FlagEvalEvent piiEvent) + private void assertHashedTargetingKeyAndOmittedContext(final RecordedEvaluation piiEvent) throws Exception { final FlagEvaluationTestSupport.TestWriterSetup setup = buildTestWriter(); - setup.handler.add(piiEvent); + setup.add(piiEvent); final FlagEvaluationTestSupport.CapturedJson captured = flushAndCapture(setup); @@ -508,7 +476,7 @@ private void assertHashedTargetingKeyAndOmittedContext(final FlagEvalEvent piiEv assertFalse(captured.raw.contains("\"evaluation\":")); } - private static FlagEvalEvent piiEvent(final boolean observeFullEvaluationData) { + private static RecordedEvaluation piiEvent(final boolean observeFullEvaluationData) { return event( "pii-flag", "on", @@ -519,10 +487,6 @@ private static FlagEvalEvent piiEvent(final boolean observeFullEvaluationData) { piiAttrs()); } - private static FlagEvalEvent piiEventDefaultConsent() { - return event("pii-flag", "on", "alloc1", "jane.doe@datadoghq.com", 1000L, piiAttrs()); - } - private static Map piiAttrs() { final Map attrs = new HashMap<>(); attrs.put("region", "us-east-1"); diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationTestSupport.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationTestSupport.java index 68ec703e81f..df0e1811b39 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationTestSupport.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationTestSupport.java @@ -17,7 +17,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; import java.util.function.IntPredicate; final class FlagEvaluationTestSupport { @@ -26,17 +28,17 @@ final class FlagEvaluationTestSupport { private FlagEvaluationTestSupport() {} - static FlagEvalEvent event( + static RecordedEvaluation event( final String flagKey, final String variant, final String allocationKey, final String targetingKey, final long evalTimeMs, final Map attrs) { - return new FlagEvalEvent(flagKey, variant, allocationKey, targetingKey, evalTimeMs, attrs); + return event(flagKey, variant, allocationKey, targetingKey, evalTimeMs, false, attrs); } - static FlagEvalEvent event( + static RecordedEvaluation event( final String flagKey, final String variant, final String allocationKey, @@ -44,7 +46,7 @@ static FlagEvalEvent event( final long evalTimeMs, final boolean observeFullEvaluationData, final Map attrs) { - return new FlagEvalEvent( + return new RecordedEvaluation( flagKey, variant, allocationKey, @@ -52,16 +54,11 @@ static FlagEvalEvent event( null, evalTimeMs, observeFullEvaluationData, - attrs); + attrs, + Collections.emptyMap()); } - static FlagEvalEvent errorEvent( - final String flagKey, final String errorMessage, final long evalTimeMs) { - return new FlagEvalEvent( - flagKey, null, null, null, errorMessage, evalTimeMs, Collections.emptyMap()); - } - - static FlagEvalEvent simpleEvent(final String flagKey, final String variant) { + static RecordedEvaluation simpleEvent(final String flagKey, final String variant) { return event(flagKey, variant, "alloc1", "user-1", 1000L, Collections.emptyMap()); } @@ -84,10 +81,18 @@ static TestWriterSetup buildTestWriter() { static TestWriterSetup buildTestWriter(final int payloadSizeLimitBytes) { final CapturingTransport transport = new CapturingTransport(); final RecordingMetrics metrics = new RecordingMetrics(); - final FlagEvaluationPipeline.SerializingHandlerForTest handler = - new FlagEvaluationPipeline.SerializingHandlerForTest( - transport, context(), payloadSizeLimitBytes, new RuntimeServices(metrics)); - return new TestWriterSetup(handler, transport, metrics); + final ConcurrentFlagAggregator aggregator = new ConcurrentFlagAggregator(); + final FlagEvaluationPipeline.FlagEvaluationSerializingHandler handler = + new FlagEvaluationPipeline.FlagEvaluationSerializingHandler( + transport, + aggregator, + Long.MAX_VALUE, + TimeUnit.NANOSECONDS, + context(), + new ConcurrentHashMap<>(), + payloadSizeLimitBytes, + new RuntimeServices(metrics)); + return new TestWriterSetup(handler, aggregator, transport, metrics); } static CapturedJson flushAndCapture(final TestWriterSetup setup) { @@ -98,7 +103,6 @@ static CapturedJson flushAndCapture(final TestWriterSetup setup) { static List flushAndCaptureAll(final TestWriterSetup setup) { setup.transport.posts.clear(); - setup.handler.drainAndAggregate(); setup.handler.flush(); final List json = new ArrayList<>(); for (final Post post : setup.transport.posts) { @@ -200,18 +204,26 @@ long sum(final String metricName, final String tag) { } static class TestWriterSetup { - final FlagEvaluationPipeline.SerializingHandlerForTest handler; + final FlagEvaluationPipeline.FlagEvaluationSerializingHandler handler; + final ConcurrentFlagAggregator aggregator; final CapturingTransport transport; final RecordingMetrics metrics; TestWriterSetup( - final FlagEvaluationPipeline.SerializingHandlerForTest handler, + final FlagEvaluationPipeline.FlagEvaluationSerializingHandler handler, + final ConcurrentFlagAggregator aggregator, final CapturingTransport transport, final RecordingMetrics metrics) { this.handler = handler; + this.aggregator = aggregator; this.transport = transport; this.metrics = metrics; } + + /** Records an evaluation into the handler's aggregator. */ + void add(final RecordedEvaluation evaluation) { + evaluation.recordInto(aggregator); + } } static class CapturedJson { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/RecordedEvaluation.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/RecordedEvaluation.java new file mode 100644 index 00000000000..bd4b5305588 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/RecordedEvaluation.java @@ -0,0 +1,115 @@ +package com.datadog.openfeature.internal.flagevaluation; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Consumer; + +/** One evaluation as passed to {@link FlagEvaluationWriter#record}, for tests. */ +public final class RecordedEvaluation { + public final String flagKey; + public final String variant; + public final String allocationKey; + public final String targetingKey; + public final String errorMessage; + public final long evalTimeMs; + public final boolean observeFullEvaluationData; + public final Map attrs; + public final Map features; + + public RecordedEvaluation( + final String flagKey, + final String variant, + final String allocationKey, + final String targetingKey, + final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + final Map attrs, + final Map features) { + this.flagKey = flagKey; + this.variant = variant; + this.allocationKey = allocationKey; + this.targetingKey = targetingKey; + this.errorMessage = errorMessage; + this.evalTimeMs = evalTimeMs; + this.observeFullEvaluationData = observeFullEvaluationData; + this.attrs = attrs; + this.features = features; + } + + /** Records this evaluation through {@code writer}. */ + public void recordInto(final FlagEvaluationWriter writer) { + writer.record( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); + } + + /** Records this evaluation into {@code aggregator}. */ + void recordInto(final ConcurrentFlagAggregator aggregator) { + aggregator.record( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); + } + + /** A writer keeping what it is given. */ + public static class Recorder implements FlagEvaluationWriter { + public final List evaluations = new CopyOnWriteArrayList<>(); + public final List truncations = new CopyOnWriteArrayList<>(); + private final Consumer listener; + + public Recorder() { + this(evaluation -> {}); + } + + /** A recorder also passing each evaluation to {@code listener}. */ + public Recorder(final Consumer listener) { + this.listener = listener; + } + + @Override + public void countContextTruncated(final String reason) { + truncations.add(reason); + } + + @Override + public void record( + final String flagKey, + final String variant, + final String allocationKey, + final String targetingKey, + final String errorMessage, + final long evalTimeMs, + final boolean observeFullEvaluationData, + final Map attrs, + final Map features) { + final RecordedEvaluation evaluation = + new RecordedEvaluation( + flagKey, + variant, + allocationKey, + targetingKey, + errorMessage, + evalTimeMs, + observeFullEvaluationData, + attrs, + features); + evaluations.add(evaluation); + listener.accept(evaluation); + } + } +} From 2a98724c219e340448d141cca3d8857ced85e58b Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Mon, 23 Nov 2026 11:28:52 -0500 Subject: [PATCH 3/6] Name the aggregator's rows EvaluationTally, not Bucket ConcurrentFlagAggregator.Bucket held one row of the payload, a distinct evaluation key and its counters, but sat inside ConcurrentHashtable, whose buckets are its own array slots. Call it an EvaluationTally. Co-Authored-By: Claude Opus 5.5 --- .../FlagEvaluationHotPathBenchmark.java | 2 +- .../ConcurrentFlagAggregator.java | 161 +++++++++--------- .../FlagEvaluationPayloads.java | 24 +-- .../FlagEvaluationPipeline.java | 14 +- .../ConcurrentFlagAggregatorTest.java | 114 ++++++------- .../FlagEvaluationPayloadsTest.java | 62 +++---- .../FlagEvaluationPipelineTest.java | 4 +- 7 files changed, 193 insertions(+), 188 deletions(-) diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java index 052705f885f..de779386d05 100644 --- a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java +++ b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationHotPathBenchmark.java @@ -39,7 +39,7 @@ * makes every field-count profile measure the same scalar-only work. * *

The worker is never started, so nothing drains: after the first pass over the keys every - * evaluation lands in an existing bucket, the steady state of a flush window. + * evaluation lands in an existing tally, the steady state of a flush window. * *

Run: {@code ./gradlew :dd-openfeature:jmh -Pjmh.includes=FlagEvaluationHotPathBenchmark}. */ diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java index 8d0055329d3..c5b0fa0c353 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java @@ -15,32 +15,32 @@ /** * Aggregates flag evaluations into rows on the evaluating threads, for the worker to flush. * - *

Tiers. A full-tier bucket is keyed by every dimension: flag, variant, allocation, - * error, subject, context, consent and features. Without consent the context is dropped on emit, so - * it neither splits buckets nor is stored. The full tier holds at most {@link #GLOBAL_CAP} buckets, + *

Tiers. A full-tier tally is keyed by every dimension: flag, variant, allocation, error, + * subject, context, consent and features. Without consent the context is dropped on emit, so it + * neither splits tallies nor is stored. The full tier holds at most {@link #GLOBAL_CAP} tallies, * and {@link #PER_FLAG_CAP} per flag. Past either cap an evaluation spills to the degraded tier, * keyed without the subject, the context and the consent (degraded rows never carry them), and * AND-folds the consent of the evaluations it merges. The degraded tier holds at most {@link - * #DEGRADED_CAP} buckets; past it an evaluation is dropped and counted in {@link + * #DEGRADED_CAP} tallies; past it an evaluation is dropped and counted in {@link * #droppedDegradedOverflow}. * *

Recording. Each tier is a toolbox {@link ConcurrentHashtable}. An evaluation that lands - * in an existing bucket costs a lock-free chain scan and a few atomic updates, and allocates - * nothing. A new bucket is inserted under the table's write lock, which also enforces the tier's + * in an existing tally costs a lock-free chain scan and a few atomic updates, and allocates + * nothing. A new tally is inserted under the table's write lock, which also enforces the tier's * cap. The per-flag cap is checked before that lock, so racing inserts can overshoot it by a few - * buckets. + * tallies. * - *

Drained in place. Buckets live across flushes. A bucket whose count goes from zero to - * one pushes itself onto a lock-free stack of the buckets touched since the last drain. {@link - * #drain} takes the whole stack and, for each bucket, reads and resets its counters. An evaluation + *

Drained in place. Tallies live across flushes. A tally whose count goes from zero to + * one pushes itself onto a lock-free stack of the tallies touched since the last drain. {@link + * #drain} takes the whole stack and, for each tally, reads and resets its counters. An evaluation * racing the drain is never lost: if its increment lands before the reset, this drain emits it; if - * after, it finds the count at zero and pushes the bucket onto the next drain's stack. Only its - * timestamps can land in the other flush. A drain costs time in proportion to the buckets touched, - * not to the table, and busy buckets are not recreated every window. + * after, it finds the count at zero and pushes the tally onto the next drain's stack. Only its + * timestamps can land in the other flush. A drain costs time in proportion to the tallies touched, + * not to the table, and busy tallies are not recreated every window. * - *

Idle sweep. {@link #sweepIdle} removes the buckets idle for a whole window, which frees - * their slots under the caps. An evaluation that finds a bucket just as it is swept still counts, - * in the next drain; a later one creates a fresh bucket. + *

Idle sweep. {@link #sweepIdle} removes the tallies idle for a whole window, which frees + * their slots under the caps. An evaluation that finds a tally just as it is swept still counts, in + * the next drain; a later one creates a fresh tally. */ final class ConcurrentFlagAggregator { // Design assumptions: the scale the caps are sized for. @@ -58,36 +58,39 @@ final class ConcurrentFlagAggregator { static final int DEGRADED_BUCKET_SIZING_BASIS = EXPECTED_FLAG_COUNT * EXPECTED_DEGRADED_BUCKETS_PER_FLAG; // 25_000 - // Enforced bucket caps. + // Enforced tally caps. static final int GLOBAL_CAP = 131_072; // nearest power of two above FULL_BUCKET_SIZING_BASIS static final int PER_FLAG_CAP = PER_FLAG_BUCKET_SIZING_BASIS; static final int DEGRADED_CAP = 32_768; // nearest power of two above DEGRADED_BUCKET_SIZING_BASIS /** - * The most flags whose full-tier buckets are counted. Counts are never removed, so the + * The most flags whose full-tier tallies are counted. Counts are never removed, so the * evaluations of further flags go to the degraded tier. */ static final int FLAG_COUNT_CAP = 65_536; /** Receives each drained row, with the counters it had at the drain. */ interface Rows { - void row(Bucket bucket, long count, long firstEvalMs, long lastEvalMs, boolean defaultUsed); + void row( + EvaluationTally tally, long count, long firstEvalMs, long lastEvalMs, boolean defaultUsed); } - private static final AtomicReferenceFieldUpdater DIRTY = - AtomicReferenceFieldUpdater.newUpdater(ConcurrentFlagAggregator.class, Bucket.class, "dirty"); + private static final AtomicReferenceFieldUpdater + DIRTY = + AtomicReferenceFieldUpdater.newUpdater( + ConcurrentFlagAggregator.class, EvaluationTally.class, "dirty"); final AtomicLong droppedDegradedOverflow = new AtomicLong(); - private final ConcurrentHashtable.State full = - ConcurrentHashtable.createBounded(Bucket.class, GLOBAL_CAP); - private final ConcurrentHashtable.State degraded = - ConcurrentHashtable.createBounded(Bucket.class, DEGRADED_CAP); + private final ConcurrentHashtable.State full = + ConcurrentHashtable.createBounded(EvaluationTally.class, GLOBAL_CAP); + private final ConcurrentHashtable.State degraded = + ConcurrentHashtable.createBounded(EvaluationTally.class, DEGRADED_CAP); private final ConcurrentHashtable.D1 flagCounts = ConcurrentHashtable.D1.createBounded(FlagCount.class, FLAG_COUNT_CAP); - // The buckets touched since the last drain, linked through Bucket.dirtyNext. - @Nullable private volatile Bucket dirty; - // Written by the draining thread only; read when a bucket is created. + // The tallies touched since the last drain, linked through EvaluationTally.dirtyNext. + @Nullable private volatile EvaluationTally dirty; + // Written by the draining thread only; read when a tally is created. private volatile int drains; /** Records one evaluation, on the evaluating thread. */ @@ -102,13 +105,13 @@ void record( @Nullable final Map attrs, @Nullable final Map features) { final boolean isDefault = variant == null; - // Without consent the context is dropped on emit, so it must not fragment buckets or be + // Without consent the context is dropped on emit, so it must not fragment tallies or be // stored: a high-cardinality field (request_id, timestamp) would blow out PER_FLAG_CAP and // spill every later evaluation into the degraded tier. final Map context = observeFullEvaluationData ? attrs : null; final long contextHash = observeFullEvaluationData ? contextHash(context) : 0L; // Features come from the flag configuration, not the evaluation context, so they split - // buckets in both tiers and whatever the consent. + // tallies in both tiers and whatever the consent. final Map featureMap = features == null ? Collections.emptyMap() : features; final long featuresHash = featureMap.hashCode(); @@ -123,7 +126,9 @@ void record( contextHash, observeFullEvaluationData, featuresHash); - for (Bucket b = ConcurrentHashtable.bucketFor(full, fullHash); b != null; b = b.next()) { + for (EvaluationTally b = ConcurrentHashtable.bucketFor(full, fullHash); + b != null; + b = b.next()) { if (b.keyHash == fullHash && b.matchesFull( flagKey, @@ -141,8 +146,8 @@ void record( final FlagCount flagCount = flagCounts.tryGetOrCreateOrNull(flagKey, FlagCount::new); if (flagCount != null && flagCount.count.get() < PER_FLAG_CAP) { - final Bucket bucket = - new Bucket( + final EvaluationTally tally = + new EvaluationTally( false, fullHash, flagKey, @@ -155,13 +160,13 @@ void record( featureMap, flagCount, drains); - final Bucket inserted; - try (ConcurrentHashtable.Reservation reservation = + final EvaluationTally inserted; + try (ConcurrentHashtable.Reservation reservation = ConcurrentHashtable.tryReserve(full, fullHash)) { - inserted = reservation.tryGetOrInsertOrNull(bucket); + inserted = reservation.tryGetOrInsertOrNull(tally); } if (inserted != null) { - if (inserted == bucket) { + if (inserted == tally) { flagCount.count.incrementAndGet(); } inserted.merge(this, evalTimeMs, isDefault); @@ -172,7 +177,7 @@ void record( final long degradedHash = degradedHash(flagKey, variant, allocationKey, errorMessage, featuresHash); - for (Bucket b = ConcurrentHashtable.bucketFor(degraded, degradedHash); + for (EvaluationTally b = ConcurrentHashtable.bucketFor(degraded, degradedHash); b != null; b = b.next()) { if (b.keyHash == degradedHash @@ -181,8 +186,8 @@ void record( return; } } - final Bucket bucket = - new Bucket( + final EvaluationTally tally = + new EvaluationTally( true, degradedHash, flagKey, @@ -195,10 +200,10 @@ void record( featureMap, null, drains); - final Bucket inserted; - try (ConcurrentHashtable.Reservation reservation = + final EvaluationTally inserted; + try (ConcurrentHashtable.Reservation reservation = ConcurrentHashtable.tryReserve(degraded, degradedHash)) { - inserted = reservation.tryGetOrInsertOrNull(bucket); + inserted = reservation.tryGetOrInsertOrNull(tally); } if (inserted == null) { droppedDegradedOverflow.incrementAndGet(); @@ -207,12 +212,12 @@ void record( } } - private void push(final Bucket bucket) { - Bucket head; + private void push(final EvaluationTally tally) { + EvaluationTally head; do { head = dirty; - bucket.dirtyNext = head; - } while (!DIRTY.compareAndSet(this, head, bucket)); + tally.dirtyNext = head; + } while (!DIRTY.compareAndSet(this, head, tally)); } /** Whether no evaluation has been recorded since the last drain. */ @@ -221,10 +226,10 @@ boolean isEmpty() { } /** - * Passes every bucket touched since the last drain to {@code rows}, with its counters, and resets + * Passes every tally touched since the last drain to {@code rows}, with its counters, and resets * them. One draining thread at a time. * - *

A row that throws does not stop the drain: every touched bucket is still read and reset, so + *

A row that throws does not stop the drain: every touched tally is still read and reset, so * none is left counting without being on the stack. The first failure is rethrown at the end. * * @return the number of rows drained. @@ -234,16 +239,16 @@ int drain(final Rows rows) { drains = drain; int drained = 0; RuntimeException failure = null; - Bucket b = DIRTY.getAndSet(this, null); + EvaluationTally b = DIRTY.getAndSet(this, null); while (b != null) { - // Unlink before resetting the count: once it is zero, an evaluation can push the bucket + // Unlink before resetting the count: once it is zero, an evaluation can push the tally // again, and that push writes dirtyNext. - final Bucket next = b.dirtyNext; + final EvaluationTally next = b.dirtyNext; b.dirtyNext = null; - final long count = Bucket.COUNT.getAndSet(b, 0); - final long first = Bucket.FIRST.getAndSet(b, Long.MAX_VALUE); - final long last = Bucket.LAST.getAndSet(b, Long.MIN_VALUE); - final boolean defaultUsed = Bucket.DEFAULT_USED.getAndSet(b, 0) != 0; + final long count = EvaluationTally.COUNT.getAndSet(b, 0); + final long first = EvaluationTally.FIRST.getAndSet(b, Long.MAX_VALUE); + final long last = EvaluationTally.LAST.getAndSet(b, Long.MIN_VALUE); + final boolean defaultUsed = EvaluationTally.DEFAULT_USED.getAndSet(b, 0) != 0; b.lastDrain = drain; if (count > 0) { drained++; @@ -264,10 +269,10 @@ int drain(final Rows rows) { } /** - * Removes the buckets that saw no evaluation in the last whole window, freeing their slots under + * Removes the tallies that saw no evaluation in the last whole window, freeing their slots under * the caps. The draining thread only, after {@link #drain}. * - * @return the number of buckets removed. + * @return the number of tallies removed. */ int sweepIdle() { final int drain = drains; @@ -294,8 +299,8 @@ int sweepIdle() { return removed[0]; } - /** The number of buckets in both tiers, touched or not. */ - int bucketCount() { + /** The number of tallies in both tiers, touched or not. */ + int tallyCount() { return ConcurrentHashtable.estimateSize(full) + ConcurrentHashtable.estimateSize(degraded); } @@ -368,15 +373,15 @@ static boolean sameContext( } /** One aggregated row: its key, and counters updated by any evaluating thread. */ - static final class Bucket extends ConcurrentHashtable.Entry { - static final AtomicLongFieldUpdater COUNT = - AtomicLongFieldUpdater.newUpdater(Bucket.class, "count"); - static final AtomicLongFieldUpdater FIRST = - AtomicLongFieldUpdater.newUpdater(Bucket.class, "firstEvalMs"); - static final AtomicLongFieldUpdater LAST = - AtomicLongFieldUpdater.newUpdater(Bucket.class, "lastEvalMs"); - static final AtomicIntegerFieldUpdater DEFAULT_USED = - AtomicIntegerFieldUpdater.newUpdater(Bucket.class, "defaultUsed"); + static final class EvaluationTally extends ConcurrentHashtable.Entry { + static final AtomicLongFieldUpdater COUNT = + AtomicLongFieldUpdater.newUpdater(EvaluationTally.class, "count"); + static final AtomicLongFieldUpdater FIRST = + AtomicLongFieldUpdater.newUpdater(EvaluationTally.class, "firstEvalMs"); + static final AtomicLongFieldUpdater LAST = + AtomicLongFieldUpdater.newUpdater(EvaluationTally.class, "lastEvalMs"); + static final AtomicIntegerFieldUpdater DEFAULT_USED = + AtomicIntegerFieldUpdater.newUpdater(EvaluationTally.class, "defaultUsed"); final boolean degraded; final String flagKey; @@ -386,9 +391,9 @@ static final class Bucket extends ConcurrentHashtable.Entry { @Nullable final String errorMessage; @Nullable final Map prunedAttrs; final Map features; - // The full-tier bucket count of this bucket's flag; null for degraded buckets. + // The full-tier tally count of this tally's flag; null for degraded tallies. @Nullable final FlagCount flagCount; - // Fixed for a full-tier bucket, where consent is a key dimension. A degraded bucket AND-folds + // Fixed for a full-tier tally, where consent is a key dimension. A degraded tally AND-folds // it, which is benign: degraded rows drop the targeting key and context whatever the consent. volatile boolean observeFullEvaluationData; @@ -397,12 +402,12 @@ static final class Bucket extends ConcurrentHashtable.Entry { volatile long lastEvalMs = Long.MIN_VALUE; volatile int defaultUsed; - // The next bucket on the stack of buckets touched since the last drain. - @Nullable Bucket dirtyNext; - // The drain that last found this bucket touched, or the last drain before it was created. + // The next tally on the stack of tallies touched since the last drain. + @Nullable EvaluationTally dirtyNext; + // The drain that last found this tally touched, or the last drain before it was created. int lastDrain; - Bucket( + EvaluationTally( final boolean degraded, final long keyHash, final String flagKey, @@ -446,7 +451,7 @@ void mergeDegraded( final boolean isDefault, final boolean observeFullEvaluationData) { if (!observeFullEvaluationData && this.observeFullEvaluationData) { - this.observeFullEvaluationData = false; // a mixed bucket stays at consent-off + this.observeFullEvaluationData = false; // a mixed tally stays at consent-off } merge(owner, evalTimeMs, isDefault); } @@ -504,7 +509,7 @@ boolean matchesDegraded( } @Override - public boolean matches(final Bucket other) { + public boolean matches(final EvaluationTally other) { return degraded ? matchesDegraded( other.flagKey, other.variant, other.allocationKey, other.errorMessage, other.features) @@ -520,7 +525,7 @@ public boolean matches(final Bucket other) { } } - /** The number of full-tier buckets of a flag. */ + /** The number of full-tier tallies of a flag. */ static final class FlagCount extends ConcurrentHashtable.D1.Entry { final AtomicInteger count = new AtomicInteger(); diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java index 0cca2aa6ec9..a9326c727f5 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloads.java @@ -250,36 +250,36 @@ static class FlagEvaluationEvent { } /** - * The row of a drained bucket. Timestamps that raced the drain (none left, so first is after + * The row of a drained tally. Timestamps that raced the drain (none left, so first is after * last) read as the flush time. */ - static FlagEvaluationEvent fromBucket( - final ConcurrentFlagAggregator.Bucket bucket, + static FlagEvaluationEvent fromTally( + final ConcurrentFlagAggregator.EvaluationTally tally, final long count, final long firstEvalMs, final long lastEvalMs, final boolean runtimeDefaultUsed, final long flushTimeMs) { - final boolean isFullTier = !bucket.degraded; + final boolean isFullTier = !tally.degraded; // Consent is the value snapshotted at evaluation time, not the current configuration's: a // later configuration may have changed it since these evaluations happened. - final boolean observeFullEvaluationData = bucket.observeFullEvaluationData; + final boolean observeFullEvaluationData = tally.observeFullEvaluationData; final boolean timed = firstEvalMs <= lastEvalMs; return new FlagEvaluationEvent( flushTimeMs, - bucket.flagKey, + tally.flagKey, timed ? firstEvalMs : flushTimeMs, timed ? lastEvalMs : flushTimeMs, count, - bucket.variant, - bucket.allocationKey, - resolveTargetingKey(bucket.targetingKey, isFullTier, observeFullEvaluationData), + tally.variant, + tally.allocationKey, + resolveTargetingKey(tally.targetingKey, isFullTier, observeFullEvaluationData), runtimeDefaultUsed, - bucket.errorMessage, - isFullTier && observeFullEvaluationData ? bucket.prunedAttrs : null, + tally.errorMessage, + isFullTier && observeFullEvaluationData ? tally.prunedAttrs : null, // Features come from the flag configuration, so both tiers keep them whatever the // consent. - bucket.features); + tally.features); } private static String resolveTargetingKey( diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java index 137daf18e73..2c9195c37bc 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipeline.java @@ -22,9 +22,9 @@ * EVP flagevaluation writer for Java. * *

Evaluations are aggregated on the evaluating threads, into a {@link ConcurrentFlagAggregator} - * (see it for the tiers, the caps and the bucket keys), so no evaluation waits on a queue or is + * (see it for the tiers, the caps and the tally keys), so no evaluation waits on a queue or is * dropped for lack of room in one. The worker thread only flushes: every flush interval it drains - * the buckets touched since the last flush into payloads, sweeps out the idle ones, and posts the + * the tallies touched since the last flush into payloads, sweeps out the idle ones, and posts the * payloads through the same event transport as the exposure pipeline. * *

Shutdown: {@link #requestShutdown} stops accepting evaluations, counting later ones as closed @@ -127,7 +127,7 @@ public void awaitTermination(final long deadlineNanos) { /** Counts the evaluations recorded since the worker's last drain as closed drops. */ private void countResidualEvaluations() { final long[] residual = {0}; - aggregator.drain((bucket, count, first, last, defaultUsed) -> residual[0] += count); + aggregator.drain((tally, count, first, last, defaultUsed) -> residual[0] += count); countClosedDrops(residual[0]); } @@ -287,13 +287,13 @@ void flush() { final long[] degradedCount = {0}; // The drain resets the counters it reads: a failed delivery drops this batch rather than // retrying it, so one unserializable value cannot block every later flush. - aggregator.drain((bucket, count, first, last, defaultUsed) -> { - if (bucket.degraded) { + aggregator.drain((tally, count, first, last, defaultUsed) -> { + if (tally.degraded) { degradedCount[0] += count; } events.add( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, count, first, last, defaultUsed, flushTimeMs)); + FlagEvaluationPayloads.FlagEvaluationEvent.fromTally( + tally, count, first, last, defaultUsed, flushTimeMs)); }); aggregator.sweepIdle(); if (events.isEmpty()) { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java index 8c94435df7c..8b859bb48ff 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregatorTest.java @@ -36,7 +36,7 @@ class ConcurrentFlagAggregatorTest { private final ConcurrentFlagAggregator aggregator = new ConcurrentFlagAggregator(); - // ---- Aggregation and bucket keys ---- + // ---- Aggregation and tally keys ---- @Test void identicalEvaluationsAggregateIntoOneRow() { @@ -85,7 +85,7 @@ void storesThePrunedContextVerbatim() { } full("flag-d", "user-1", attrs); - assertSame(attrs, drain().get(0).bucket.prunedAttrs); + assertSame(attrs, drain().get(0).tally.prunedAttrs); } @Test @@ -117,13 +117,13 @@ void mixedConsentEvaluationsOfOneSubjectLandInDistinctBuckets() { final List rows = drain(); assertEquals(2, rows.size()); for (final Row row : rows) { - assertEquals(row.bucket.observeFullEvaluationData ? 2 : 1, row.count); + assertEquals(row.tally.observeFullEvaluationData ? 2 : 1, row.count); } } @Test void withoutConsentDifferingContextsCollapseIntoOneBucket() { - // The context is dropped on emit, so it must not fragment buckets, or the per-flag cap blows + // The context is dropped on emit, so it must not fragment tallies, or the per-flag cap blows // out under real traffic; nor is it stored. for (int i = 0; i < 3; i++) { aggregator.record( @@ -142,9 +142,9 @@ void withoutConsentDifferingContextsCollapseIntoOneBucket() { final List rows = drain(); assertEquals(2, rows.size(), "subjects still split"); final Row alice = - rows.stream().filter(r -> "alice".equals(r.bucket.targetingKey)).findFirst().get(); + rows.stream().filter(r -> "alice".equals(r.tally.targetingKey)).findFirst().get(); assertEquals(3, alice.count); - assertNull(alice.bucket.prunedAttrs); + assertNull(alice.tally.prunedAttrs); } @Test @@ -173,11 +173,11 @@ void featuresSplitBucketsInBothTiersWhateverTheConsent() { aggregator.record("feature-flag", "on", "a", "user-4", null, 1L, false, emptyMap(), p1); final List degraded = drain().stream() - .filter(r -> r.bucket.degraded && "feature-flag".equals(r.bucket.flagKey)) + .filter(r -> r.tally.degraded && "feature-flag".equals(r.tally.flagKey)) .collect(toList()); assertEquals(2, degraded.size()); for (final Row row : degraded) { - assertEquals(p1.equals(row.bucket.features) ? 2 : 1, row.count); + assertEquals(p1.equals(row.tally.features) ? 2 : 1, row.count); } } @@ -198,10 +198,10 @@ void spillsIntoTheDegradedTierAtTheFlagCapAndKeepsMixedConsentOff() { final List degraded = new ArrayList<>(); long full = 0; for (final Row row : drain()) { - if (row.bucket.degraded) { - assertNull(row.bucket.targetingKey); + if (row.tally.degraded) { + assertNull(row.tally.targetingKey); degraded.add( - row.bucket.variant + row.tally.variant + " " + row.count + " " @@ -211,7 +211,7 @@ void spillsIntoTheDegradedTierAtTheFlagCapAndKeepsMixedConsentOff() { + " " + row.defaultUsed + " " - + row.bucket.observeFullEvaluationData); + + row.tally.observeFullEvaluationData); } else { full += row.count; } @@ -232,7 +232,7 @@ void spillsIntoTheDegradedTierAtTheGlobalCap() { full("flag", "on", "other", "error", "user", emptyMap()); full("flag", "on", "alloc", "other", "user", emptyMap()); - final List degraded = drain().stream().filter(r -> r.bucket.degraded).collect(toList()); + final List degraded = drain().stream().filter(r -> r.tally.degraded).collect(toList()); assertEquals(6, degraded.size(), "the degraded tier splits on all but subject and context"); assertEquals(0, aggregator.droppedDegradedOverflow.get()); } @@ -243,7 +243,7 @@ void dropsAndCountsEvaluationsBeyondTheDegradedCap() { for (int i = 0; i < DEGRADED_CAP + 5; i++) { aggregator.record("g" + i, "on", "a", "u", null, 1L, false, emptyMap(), emptyMap()); } - // An existing degraded bucket still counts at the cap. + // An existing degraded tally still counts at the cap. aggregator.record("g0", "on", "a", "u", null, 1L, false, emptyMap(), emptyMap()); assertEquals(5, aggregator.droppedDegradedOverflow.get()); @@ -256,9 +256,9 @@ void flagsBeyondTheFlagCountCapGoToTheDegradedTier() { } record("one-too-many", "on", "user", 1L); - final List degraded = drain().stream().filter(r -> r.bucket.degraded).collect(toList()); + final List degraded = drain().stream().filter(r -> r.tally.degraded).collect(toList()); assertEquals(1, degraded.size()); - assertEquals("one-too-many", degraded.get(0).bucket.flagKey); + assertEquals("one-too-many", degraded.get(0).tally.flagKey); } @Test @@ -275,27 +275,27 @@ void capSizingUsesNamedScaleConstants() { void bucketsMatchOnEveryKeyDimension() { final Map ctx = singletonMap("k", "v"); final Map feats = singletonMap("f", 1); - final ConcurrentFlagAggregator.Bucket full = - bucket(false, "f", "on", "a", "u", "e", ctx, true, feats); + final ConcurrentFlagAggregator.EvaluationTally full = + tally(false, "f", "on", "a", "u", "e", ctx, true, feats); assertTrue( - full.matches(bucket(false, "f", "on", "a", "u", "e", new HashMap<>(ctx), true, feats))); - assertFalse(full.matches(bucket(false, "x", "on", "a", "u", "e", ctx, true, feats))); - assertFalse(full.matches(bucket(false, "f", "off", "a", "u", "e", ctx, true, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "b", "u", "e", ctx, true, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "a", "w", "e", ctx, true, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "x", ctx, true, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", emptyMap(), true, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", ctx, false, feats))); - assertFalse(full.matches(bucket(false, "f", "on", "a", "u", "e", ctx, true, emptyMap()))); - - final ConcurrentFlagAggregator.Bucket degraded = - bucket(true, "f", "on", "a", null, "e", null, false, feats); - assertTrue(degraded.matches(bucket(true, "f", "on", "a", "other", "e", ctx, true, feats))); - assertFalse(degraded.matches(bucket(true, "x", "on", "a", null, "e", null, false, feats))); - assertFalse(degraded.matches(bucket(true, "f", "off", "a", null, "e", null, false, feats))); - assertFalse(degraded.matches(bucket(true, "f", "on", "b", null, "e", null, false, feats))); - assertFalse(degraded.matches(bucket(true, "f", "on", "a", null, "x", null, false, feats))); - assertFalse(degraded.matches(bucket(true, "f", "on", "a", null, "e", null, false, emptyMap()))); + full.matches(tally(false, "f", "on", "a", "u", "e", new HashMap<>(ctx), true, feats))); + assertFalse(full.matches(tally(false, "x", "on", "a", "u", "e", ctx, true, feats))); + assertFalse(full.matches(tally(false, "f", "off", "a", "u", "e", ctx, true, feats))); + assertFalse(full.matches(tally(false, "f", "on", "b", "u", "e", ctx, true, feats))); + assertFalse(full.matches(tally(false, "f", "on", "a", "w", "e", ctx, true, feats))); + assertFalse(full.matches(tally(false, "f", "on", "a", "u", "x", ctx, true, feats))); + assertFalse(full.matches(tally(false, "f", "on", "a", "u", "e", emptyMap(), true, feats))); + assertFalse(full.matches(tally(false, "f", "on", "a", "u", "e", ctx, false, feats))); + assertFalse(full.matches(tally(false, "f", "on", "a", "u", "e", ctx, true, emptyMap()))); + + final ConcurrentFlagAggregator.EvaluationTally degraded = + tally(true, "f", "on", "a", null, "e", null, false, feats); + assertTrue(degraded.matches(tally(true, "f", "on", "a", "other", "e", ctx, true, feats))); + assertFalse(degraded.matches(tally(true, "x", "on", "a", null, "e", null, false, feats))); + assertFalse(degraded.matches(tally(true, "f", "off", "a", null, "e", null, false, feats))); + assertFalse(degraded.matches(tally(true, "f", "on", "b", null, "e", null, false, feats))); + assertFalse(degraded.matches(tally(true, "f", "on", "a", null, "x", null, false, feats))); + assertFalse(degraded.matches(tally(true, "f", "on", "a", null, "e", null, false, emptyMap()))); } // ---- Context hashing ---- @@ -362,7 +362,7 @@ void drainsOnlyTouchedBucketsAndResetsThem() { record("a", "on", "user", 7L); assertEquals(singletonList("a 1 7-7"), summaries(drain())); - assertEquals(2, aggregator.bucketCount(), "drained buckets stay for the next window"); + assertEquals(2, aggregator.tallyCount(), "drained tallies stay for the next window"); } @Test @@ -377,12 +377,12 @@ void aRowThatThrowsDoesNotStrandTheOtherBuckets() { assertThrows( IllegalStateException.class, () -> - aggregator.drain((bucket, count, first, last, defaultUsed) -> { - seen.add(bucket.flagKey); + aggregator.drain((tally, count, first, last, defaultUsed) -> { + seen.add(tally.flagKey); throw failure; }))); - assertEquals(2, seen.size(), "every bucket is still drained"); + assertEquals(2, seen.size(), "every tally is still drained"); assertTrue(aggregator.isEmpty()); record("a", "on", "user", 2L); assertEquals(singletonList("a 1 2-2"), summaries(drain()), "and pushed again when touched"); @@ -401,13 +401,13 @@ void sweepsBucketsIdleForAWholeWindowAndFreesTheirFlagSlots() { record("busy", "on", "user", 2L); drain(); assertEquals(PER_FLAG_CAP + 1, aggregator.sweepIdle()); - assertEquals(1, aggregator.bucketCount()); + assertEquals(1, aggregator.tallyCount()); - // The idle buckets' flag slots were given back, so the flag fills the full tier again. + // The idle tallies' flag slots were given back, so the flag fills the full tier again. for (int i = 0; i < PER_FLAG_CAP; i++) { aggregator.record("idle", "on", "alloc", "v" + i, null, 3L, false, emptyMap(), emptyMap()); } - assertEquals(0, drain().stream().filter(r -> r.bucket.degraded).count()); + assertEquals(0, drain().stream().filter(r -> r.tally.degraded).count()); } @Test @@ -430,7 +430,7 @@ void countsEveryEvaluationFromManyThreads() throws Exception { assertEquals((long) threads * perThread, rows.stream().mapToLong(r -> r.count).sum()); // i % 16 determines i % 4, so there are 16 distinct (flag, subject) keys, each inserted once. assertEquals(16, rows.size()); - assertEquals(16, aggregator.bucketCount()); + assertEquals(16, aggregator.tallyCount()); } @Test @@ -439,7 +439,7 @@ void losesNoEvaluationWhileDrainingConcurrently() throws Exception { final int perThread = 100_000; final long[] total = {0}; final ConcurrentFlagAggregator.Rows sum = - (bucket, count, first, last, defaultUsed) -> total[0] += count; + (tally, count, first, last, defaultUsed) -> total[0] += count; // Drain and sweep over and over while the producers run, then once more after. runProducers( threads, @@ -458,19 +458,19 @@ void losesNoEvaluationWhileDrainingConcurrently() throws Exception { /** A drained row. */ private static final class Row { - final ConcurrentFlagAggregator.Bucket bucket; + final ConcurrentFlagAggregator.EvaluationTally tally; final long count; final long first; final long last; final boolean defaultUsed; Row( - final ConcurrentFlagAggregator.Bucket bucket, + final ConcurrentFlagAggregator.EvaluationTally tally, final long count, final long first, final long last, final boolean defaultUsed) { - this.bucket = bucket; + this.tally = tally; this.count = count; this.first = first; this.last = last; @@ -481,15 +481,15 @@ private static final class Row { private List drain() { final List rows = new ArrayList<>(); final int drained = - aggregator.drain((bucket, count, first, last, defaultUsed) -> - rows.add(new Row(bucket, count, first, last, defaultUsed))); + aggregator.drain((tally, count, first, last, defaultUsed) -> + rows.add(new Row(tally, count, first, last, defaultUsed))); assertEquals(rows.size(), drained); return rows; } private static List summaries(final List rows) { return rows.stream() - .map(r -> r.bucket.flagKey + " " + r.count + " " + r.first + "-" + r.last) + .map(r -> r.tally.flagKey + " " + r.count + " " + r.first + "-" + r.last) .sorted() .collect(toList()); } @@ -497,13 +497,13 @@ private static List summaries(final List rows) { /** The drained full-tier rows' features and counts, sorted. */ private List features() { return drain().stream() - .filter(r -> !r.bucket.degraded) - .map(r -> r.bucket.features + "=" + r.count) + .filter(r -> !r.tally.degraded) + .map(r -> r.tally.features + "=" + r.count) .sorted() .collect(toList()); } - /** Fills the full tier to its global cap with distinct buckets, under the per-flag cap. */ + /** Fills the full tier to its global cap with distinct tallies, under the per-flag cap. */ private void fillFullTier() { for (int i = 0; i < GLOBAL_CAP; i++) { aggregator.record( @@ -534,7 +534,7 @@ private void full( flagKey, variant, allocationKey, targetingKey, errorMessage, 1L, true, attrs, emptyMap()); } - private static ConcurrentFlagAggregator.Bucket bucket( + private static ConcurrentFlagAggregator.EvaluationTally tally( final boolean degraded, final String flag, final String variant, @@ -544,7 +544,7 @@ private static ConcurrentFlagAggregator.Bucket bucket( final Map context, final boolean consent, final Map features) { - return new ConcurrentFlagAggregator.Bucket( + return new ConcurrentFlagAggregator.EvaluationTally( degraded, 1L, flag, @@ -572,7 +572,7 @@ private void runProducers(final int threads, final int perThread, final Runnable executor.submit(() -> { start.await(); for (int i = 0; i < perThread; i++) { - // Few keys, so threads contend on the same buckets and race on insertion. + // Few keys, so threads contend on the same tallies and race on insertion. aggregator.record( "flag-" + (i % 4), (i & 1) == 0 ? "on" : null, diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java index 9da5e2ae39f..5dd0a2c9ead 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPayloadsTest.java @@ -51,15 +51,15 @@ void omitsNullMapEntriesAndPreservesNullArrayElements() throws Exception { final Map attrs = new HashMap<>(); attrs.put("present", "value"); attrs.put("missing", null); - final ConcurrentFlagAggregator.Bucket bucket = - bucket( + final ConcurrentFlagAggregator.EvaluationTally tally = + tally( true, "flag", "user", attrs, true, singletonMap("items", asList(null, singletonMap("missing", null)))); - final FlagEvaluationPayloads.FlagEvaluationEvent row = row(bucket); + final FlagEvaluationPayloads.FlagEvaluationEvent row = row(tally); final Map event = firstEvent(onlyPayload(row)); @@ -71,16 +71,16 @@ void omitsNullMapEntriesAndPreservesNullArrayElements() throws Exception { @Test void eventFromFullBucketUsesFlushTimeAndEvaluationBounds() throws Exception { - final ConcurrentFlagAggregator.Bucket bucket = - bucket(true, "ts-flag", "user-1", emptyMap(), true, emptyMap()); + final ConcurrentFlagAggregator.EvaluationTally tally = + tally(true, "ts-flag", "user-1", emptyMap(), true, emptyMap()); final long flushTimeMs = EVAL_MS + 5_000; final Map json = firstPayload( FlagEvaluationPayloads.buildPayloads( java.util.Collections.singletonList( - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, 2, EVAL_MS, EVAL_MS + 10, false, flushTimeMs)), + FlagEvaluationPayloads.FlagEvaluationEvent.fromTally( + tally, 2, EVAL_MS, EVAL_MS + 10, false, flushTimeMs)), CONTEXT, 1_000_000)); @@ -94,13 +94,13 @@ void eventFromFullBucketUsesFlushTimeAndEvaluationBounds() throws Exception { @Test void degradedTierEventOmitsTargetingKeyAndContext() throws Exception { // Consent on, so only the tier drops the targeting key and context. - final ConcurrentFlagAggregator.Bucket bucket = - bucket(false, "dg-flag", "user-1", singletonMap("region", "us-east-1"), true, emptyMap()); + final ConcurrentFlagAggregator.EvaluationTally tally = + tally(false, "dg-flag", "user-1", singletonMap("region", "us-east-1"), true, emptyMap()); final Map json = firstPayload( FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000)); + java.util.Collections.singletonList(row(tally)), CONTEXT, 1_000_000)); final Map ev = firstEvent(json); assertNull(ev.get("targeting_key")); @@ -111,13 +111,13 @@ void degradedTierEventOmitsTargetingKeyAndContext() throws Exception { void fullTierWithObserveFullEvaluationDataTrueEmitsRawTargetingKeyAndContext() throws Exception { final Map attrs = new HashMap<>(); attrs.put("region", "us-east-1"); - final ConcurrentFlagAggregator.Bucket bucket = - bucket(true, "pii-flag", "jane.doe@datadoghq.com", attrs, true, emptyMap()); + final ConcurrentFlagAggregator.EvaluationTally tally = + tally(true, "pii-flag", "jane.doe@datadoghq.com", attrs, true, emptyMap()); final Map json = firstPayload( FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000)); + java.util.Collections.singletonList(row(tally)), CONTEXT, 1_000_000)); final Map ev = firstEvent(json); assertEquals("jane.doe@datadoghq.com", ev.get("targeting_key")); @@ -133,12 +133,12 @@ void fullTierWithObserveFullEvaluationDataFalseHashesTargetingKeyAndOmitsContext throws Exception { final Map attrs = new HashMap<>(); attrs.put("region", "us-east-1"); - final ConcurrentFlagAggregator.Bucket bucket = - bucket(true, "pii-flag", "jane.doe@datadoghq.com", attrs, false, emptyMap()); + final ConcurrentFlagAggregator.EvaluationTally tally = + tally(true, "pii-flag", "jane.doe@datadoghq.com", attrs, false, emptyMap()); final FlagEvaluationPayloads.EncodedPayloads payloads = FlagEvaluationPayloads.buildPayloads( - java.util.Collections.singletonList(row(bucket)), CONTEXT, 1_000_000); + java.util.Collections.singletonList(row(tally)), CONTEXT, 1_000_000); final String rawJson = new String(payloads.bodies.get(0), java.nio.charset.StandardCharsets.UTF_8); @@ -430,9 +430,9 @@ void rowsCarryFeaturesInEveryTierAndWithoutConsent() throws Exception { features.put("bandit.policy_id", "p2"); features.put("bandit.weight", 0.5); final FlagEvaluationPayloads.FlagEvaluationEvent full = - row(bucket(true, "feature-flag", "user-1", null, false, features)); + row(tally(true, "feature-flag", "user-1", null, false, features)); final FlagEvaluationPayloads.FlagEvaluationEvent degraded = - row(bucket(false, "feature-flag", null, null, false, features)); + row(tally(false, "feature-flag", null, null, false, features)); assertEquals(features, firstEvent(onlyPayload(full)).get("features")); assertEquals(features, firstEvent(onlyPayload(degraded)).get("features")); @@ -442,21 +442,21 @@ void rowsCarryFeaturesInEveryTierAndWithoutConsent() throws Exception { @Test void rowsWithoutFeaturesOmitTheField() throws Exception { final FlagEvaluationPayloads.FlagEvaluationEvent row = - row(bucket(false, "plain-flag", null, null, false, emptyMap())); + row(tally(false, "plain-flag", null, null, false, emptyMap())); assertNull(row.features); assertFalse(firstEvent(onlyPayload(row)).containsKey("features")); } - /** A bucket of flag variant "on" and allocation "alloc1", in the full or the degraded tier. */ - private static ConcurrentFlagAggregator.Bucket bucket( + /** A tally of flag variant "on" and allocation "alloc1", in the full or the degraded tier. */ + private static ConcurrentFlagAggregator.EvaluationTally tally( final boolean fullTier, final String flagKey, final String targetingKey, final Map attrs, final boolean observeFullEvaluationData, final Map features) { - return new ConcurrentFlagAggregator.Bucket( + return new ConcurrentFlagAggregator.EvaluationTally( !fullTier, 1L, flagKey, @@ -471,23 +471,23 @@ private static ConcurrentFlagAggregator.Bucket bucket( 0); } - /** The row of one evaluation of {@code bucket} at {@link #EVAL_MS}. */ + /** The row of one evaluation of {@code tally} at {@link #EVAL_MS}. */ private static FlagEvaluationPayloads.FlagEvaluationEvent row( - final ConcurrentFlagAggregator.Bucket bucket) { - return FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, 1, EVAL_MS, EVAL_MS, false, EVAL_MS); + final ConcurrentFlagAggregator.EvaluationTally tally) { + return FlagEvaluationPayloads.FlagEvaluationEvent.fromTally( + tally, 1, EVAL_MS, EVAL_MS, false, EVAL_MS); } @Test void rowsWhoseTimestampsRacedTheDrainReadAsTheFlushTime() { - final ConcurrentFlagAggregator.Bucket bucket = - bucket(true, "raced-flag", "user-1", null, false, emptyMap()); + final ConcurrentFlagAggregator.EvaluationTally tally = + tally(true, "raced-flag", "user-1", null, false, emptyMap()); final FlagEvaluationPayloads.FlagEvaluationEvent untimed = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket( - bucket, 1, Long.MAX_VALUE, Long.MIN_VALUE, false, 42L); + FlagEvaluationPayloads.FlagEvaluationEvent.fromTally( + tally, 1, Long.MAX_VALUE, Long.MIN_VALUE, false, 42L); final FlagEvaluationPayloads.FlagEvaluationEvent timed = - FlagEvaluationPayloads.FlagEvaluationEvent.fromBucket(bucket, 1, 5L, 9L, true, 42L); + FlagEvaluationPayloads.FlagEvaluationEvent.fromTally(tally, 1, 5L, 9L, true, 42L); assertEquals(42L, untimed.first_evaluation); assertEquals(42L, untimed.last_evaluation); diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java index af191cc1caa..c2a7d727b3f 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/flagevaluation/FlagEvaluationPipelineTest.java @@ -184,9 +184,9 @@ void flushSweepsBucketsIdleForAWholeWindow() { setup.add(simpleEvent("idle-flag", "on")); setup.handler.flush(); - assertEquals(1, setup.aggregator.bucketCount(), "kept through the window it was touched in"); + assertEquals(1, setup.aggregator.tallyCount(), "kept through the window it was touched in"); setup.handler.flush(); - assertEquals(0, setup.aggregator.bucketCount()); + assertEquals(0, setup.aggregator.tallyCount()); } @Test From f5eacc3f59462b5ba3e555bae9400d97f1bc85a6 Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Mon, 23 Nov 2026 12:19:54 -0500 Subject: [PATCH 4/6] Add an order-independent map hash and a 64-bit mixer to LongHashingUtils LongHashingUtils.mix is the SplitMix64 finalizer, for combining hashes whose raw values are correlated. unorderedEntryHash gives one entry's share of an order-independent hash, mixing it so that entries can't cancel each other the way Map.hashCode's key ^ value terms do, and unorderedHash adds those shares up over a map. The FFE aggregator keys evaluation contexts with this scheme. Co-Authored-By: Claude Opus 5.5 --- .../datadog/trace/util/LongHashingUtils.java | 51 +++++++++++++++ .../trace/util/LongHashingUtilsTest.java | 65 +++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/components/toolbox/src/main/java/datadog/trace/util/LongHashingUtils.java b/components/toolbox/src/main/java/datadog/trace/util/LongHashingUtils.java index 87d0482104e..a684158eefc 100644 --- a/components/toolbox/src/main/java/datadog/trace/util/LongHashingUtils.java +++ b/components/toolbox/src/main/java/datadog/trace/util/LongHashingUtils.java @@ -11,6 +11,57 @@ public final class LongHashingUtils { private LongHashingUtils() {} + /** + * Mixes a 64-bit value so that every input bit affects every output bit: the SplitMix64 + * finalizer. Use it to combine hashes whose raw values are correlated, such as the entries of an + * {@linkplain #unorderedEntryHash unordered hash}; to pick a table slot, a cheaper spread is + * enough. + * + * @param z the value to mix. + * @return the mixed value. + */ + public static long mix(long z) { + z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L; + z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL; + return z ^ (z >>> 31); + } + + /** + * One entry's share of an order-independent hash of a map: add the shares of all entries (and, to + * tell sizes apart cheaply, the size) to get a hash that doesn't depend on iteration order. + * + *

Each entry is mixed before it's added. {@link java.util.Map#hashCode}'s sum of {@code key ^ + * value} collides easily, because XOR lets one entry's bits cancel another's: {@code {a=0, b=0}} + * and {@code {a=1, b=1}} hash the same under it, but not here. No entry object is needed, so a + * caller can hash a map's contents, or a map it hasn't built yet, without allocating. + * + * @param keyHash the entry key's hash. + * @param valueHash the entry value's hash, which the caller chooses: for example one that also + * tells value types apart. + * @return the entry's share, to add to the others. + */ + public static long unorderedEntryHash(long keyHash, long valueHash) { + return mix(keyHash * 0x9E3779B97F4A7C15L + valueHash); + } + + /** + * An order-independent hash of a map's entries, built from {@link #unorderedEntryHash} with the + * keys' and values' own hash codes. Equal maps hash equally whatever their iteration order. + * + * @param map the map; {@code null} hashes like an empty map. + * @return the hash: {@code 0} for an empty map. + */ + public static long unorderedHash(@Nullable java.util.Map map) { + if (map == null || map.isEmpty()) { + return 0L; + } + long hash = map.size(); + for (final java.util.Map.Entry entry : map.entrySet()) { + hash += unorderedEntryHash(hash(entry.getKey()), hash(entry.getValue())); + } + return hash; + } + public static final long hash(Object obj) { return obj == null ? Long.MIN_VALUE : obj.hashCode(); } diff --git a/components/toolbox/src/test/java/datadog/trace/util/LongHashingUtilsTest.java b/components/toolbox/src/test/java/datadog/trace/util/LongHashingUtilsTest.java index 2837fa9ff73..2e9ee5e82cd 100644 --- a/components/toolbox/src/test/java/datadog/trace/util/LongHashingUtilsTest.java +++ b/components/toolbox/src/test/java/datadog/trace/util/LongHashingUtilsTest.java @@ -189,4 +189,69 @@ void multiArgHashTreatsNullAsZero() { int xHash = Objects.hashCode("x"); assertEquals(31L * 0 + xHash, hash(null, "x")); } + + @org.junit.jupiter.api.Test + public void mixSpreadsNearbyInputs() { + final java.util.Set mixed = new java.util.HashSet<>(); + for (long i = 0; i < 1_000; i++) { + mixed.add(LongHashingUtils.mix(i)); + } + org.junit.jupiter.api.Assertions.assertEquals(1_000, mixed.size()); + org.junit.jupiter.api.Assertions.assertNotEquals( + LongHashingUtils.mix(1L) >>> 32, LongHashingUtils.mix(2L) >>> 32); + } + + @org.junit.jupiter.api.Test + public void unorderedHashIgnoresIterationOrder() { + final java.util.Map forward = new java.util.LinkedHashMap<>(); + final java.util.Map backward = new java.util.LinkedHashMap<>(); + for (int i = 0; i < 20; i++) { + forward.put("k" + i, i); + backward.put("k" + (19 - i), 19 - i); + } + + org.junit.jupiter.api.Assertions.assertEquals( + LongHashingUtils.unorderedHash(forward), LongHashingUtils.unorderedHash(backward)); + } + + @org.junit.jupiter.api.Test + public void unorderedHashAvoidsMapHashCodesXorCollisions() { + final java.util.Map zeros = new java.util.HashMap<>(); + zeros.put("a", 0); + zeros.put("b", 0); + final java.util.Map ones = new java.util.HashMap<>(); + ones.put("a", 1); + ones.put("b", 1); + final java.util.Map swapped = new java.util.HashMap<>(); + swapped.put("a", 1); + swapped.put("b", 0); + final java.util.Map unswapped = new java.util.HashMap<>(); + unswapped.put("a", 0); + unswapped.put("b", 1); + + // Map.hashCode sums key ^ value, so these collide there. + org.junit.jupiter.api.Assertions.assertEquals(zeros.hashCode(), ones.hashCode()); + org.junit.jupiter.api.Assertions.assertNotEquals( + LongHashingUtils.unorderedHash(zeros), LongHashingUtils.unorderedHash(ones)); + org.junit.jupiter.api.Assertions.assertNotEquals( + LongHashingUtils.unorderedHash(swapped), LongHashingUtils.unorderedHash(unswapped)); + } + + @org.junit.jupiter.api.Test + public void unorderedHashOfEmptyAndNullMaps() { + org.junit.jupiter.api.Assertions.assertEquals(0L, LongHashingUtils.unorderedHash(null)); + org.junit.jupiter.api.Assertions.assertEquals( + 0L, LongHashingUtils.unorderedHash(java.util.Collections.emptyMap())); + org.junit.jupiter.api.Assertions.assertNotEquals( + 0L, LongHashingUtils.unorderedHash(java.util.Collections.singletonMap("k", null))); + } + + @org.junit.jupiter.api.Test + public void unorderedEntryHashIsWhatUnorderedHashAddsUp() { + final java.util.Map map = java.util.Collections.singletonMap("k", "v"); + + org.junit.jupiter.api.Assertions.assertEquals( + 1L + LongHashingUtils.unorderedEntryHash("k".hashCode(), "v".hashCode()), + LongHashingUtils.unorderedHash(map)); + } } From ea733e680b6e4b028cb3a8a12bac66108145709c Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Mon, 23 Nov 2026 12:21:29 -0500 Subject: [PATCH 5/6] Hash aggregation contexts with LongHashingUtils' unordered entry hash The aggregator's context hash kept its own SplitMix64 mixer and entry step; both now come from LongHashingUtils, with an identical result. The value hash, which tells value types apart, stays here. Co-Authored-By: Claude Opus 5.5 --- .../flagevaluation/ConcurrentFlagAggregator.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java index c5b0fa0c353..aa7d1f10c5b 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/flagevaluation/ConcurrentFlagAggregator.java @@ -345,18 +345,11 @@ static long contextHash(@Nullable final Map context) { final Object value = entry.getValue(); final long valueHash = value == null ? 0L : (((long) value.getClass().hashCode()) << 32) ^ value.hashCode(); - hash += mix(entry.getKey().hashCode() * 0x9E3779B97F4A7C15L + valueHash); + hash += LongHashingUtils.unorderedEntryHash(entry.getKey().hashCode(), valueHash); } return hash; } - /** The finalizer of SplitMix64: spreads every input bit over the whole result. */ - private static long mix(long z) { - z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L; - z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL; - return z ^ (z >>> 31); - } - /** * Whether two contexts would be emitted the same. A missing context and an empty one are the * same, and values compare with {@code equals}, which already tells value types apart. From 9b72a205f70c943cfab533033d9089442be5c718 Mon Sep 17 00:00:00 2001 From: Douglas Q Hawkins Date: Mon, 23 Nov 2026 12:56:47 -0500 Subject: [PATCH 6/6] Add the 32-bit counterparts to HashingUtils HashingUtils mirrors LongHashingUtils, so it gains mix (MurmurHash3's fmix32 finalizer), unorderedEntryHash and unorderedHash: a better-mixed alternative to Map.hashCode for a key's hashCode(). They are native 32-bit versions rather than truncations of the 64-bit ones, and the Javadoc points identity-critical keys to the 64-bit hash. Co-Authored-By: Claude Opus 5.5 --- .../java/datadog/trace/util/HashingUtils.java | 58 +++++++++++++++++++ .../datadog/trace/util/HashingUtilsTest.java | 49 ++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/components/toolbox/src/main/java/datadog/trace/util/HashingUtils.java b/components/toolbox/src/main/java/datadog/trace/util/HashingUtils.java index 3fbb61a0a12..8ded367029d 100644 --- a/components/toolbox/src/main/java/datadog/trace/util/HashingUtils.java +++ b/components/toolbox/src/main/java/datadog/trace/util/HashingUtils.java @@ -15,6 +15,64 @@ public static final int hashCode(@Nullable Object obj) { return obj != null ? obj.hashCode() : 0; } + /** + * Mixes a 32-bit value so that every input bit affects every output bit: the MurmurHash3 {@code + * fmix32} finalizer, the 32-bit counterpart of {@link LongHashingUtils#mix(long)}. Use it to + * combine hashes whose raw values are correlated, such as the entries of an {@linkplain + * #unorderedEntryHash unordered hash}. + * + * @param h the value to mix. + * @return the mixed value. + */ + public static int mix(int h) { + h ^= h >>> 16; + h *= 0x85EBCA6B; + h ^= h >>> 13; + h *= 0xC2B2AE35; + return h ^ (h >>> 16); + } + + /** + * One entry's share of a 32-bit order-independent hash of a map: add the shares of all entries + * (and the size) to get a hash that doesn't depend on iteration order. Each share is mixed, so + * entries can't cancel each other the way {@link java.util.Map#hashCode}'s {@code key ^ value} + * terms do. No entry object is needed. + * + *

32 bits collide sooner than 64: distinct maps are likely to collide somewhere past about 65k + * of them. That is fine for a table that also checks equality; where the hash is a key's + * identity, use {@link LongHashingUtils#unorderedEntryHash(long, long)}. + * + * @param keyHash the entry key's hash. + * @param valueHash the entry value's hash, which the caller chooses. + * @return the entry's share, to add to the others. + */ + public static int unorderedEntryHash(int keyHash, int valueHash) { + return mix(keyHash * 0x9E3779B9 + valueHash); + } + + /** + * A 32-bit order-independent hash of a map's entries, built from {@link #unorderedEntryHash} with + * the keys' and values' own hash codes: a better-mixed alternative to {@link + * java.util.Map#hashCode} for a key's {@code hashCode()}. + * + * @param map the map; {@code null} hashes like an empty map. + * @return the hash: {@code 0} for an empty map. + */ + public static int unorderedHash(@Nullable java.util.Map map) { + if (map == null || map.isEmpty()) { + return 0; + } + int hash = map.size(); + for (final java.util.Map.Entry entry : map.entrySet()) { + final Object key = entry.getKey(); + final Object value = entry.getValue(); + hash += + unorderedEntryHash( + key == null ? 0 : key.hashCode(), value == null ? 0 : value.hashCode()); + } + return hash; + } + public static final int hash(boolean value) { return Boolean.hashCode(value); } diff --git a/components/toolbox/src/test/java/datadog/trace/util/HashingUtilsTest.java b/components/toolbox/src/test/java/datadog/trace/util/HashingUtilsTest.java index a4997508f3b..063b1691084 100644 --- a/components/toolbox/src/test/java/datadog/trace/util/HashingUtilsTest.java +++ b/components/toolbox/src/test/java/datadog/trace/util/HashingUtilsTest.java @@ -270,4 +270,53 @@ static final String[] deepClone(String[] strings) { } return clones; } + + @org.junit.jupiter.api.Test + public void mixSpreadsNearbyInputs() { + final java.util.Set mixed = new java.util.HashSet<>(); + for (int i = 0; i < 1_000; i++) { + mixed.add(HashingUtils.mix(i)); + } + org.junit.jupiter.api.Assertions.assertEquals(1_000, mixed.size()); + org.junit.jupiter.api.Assertions.assertNotEquals( + HashingUtils.mix(1) >>> 16, HashingUtils.mix(2) >>> 16); + } + + @org.junit.jupiter.api.Test + public void unorderedHashIgnoresIterationOrder() { + final java.util.Map forward = new java.util.LinkedHashMap<>(); + final java.util.Map backward = new java.util.LinkedHashMap<>(); + for (int i = 0; i < 20; i++) { + forward.put("k" + i, i); + backward.put("k" + (19 - i), 19 - i); + } + + org.junit.jupiter.api.Assertions.assertEquals( + HashingUtils.unorderedHash(forward), HashingUtils.unorderedHash(backward)); + } + + @org.junit.jupiter.api.Test + public void unorderedHashAvoidsMapHashCodesXorCollisions() { + final java.util.Map zeros = new java.util.HashMap<>(); + zeros.put("a", 0); + zeros.put("b", 0); + final java.util.Map ones = new java.util.HashMap<>(); + ones.put("a", 1); + ones.put("b", 1); + + org.junit.jupiter.api.Assertions.assertEquals(zeros.hashCode(), ones.hashCode()); + org.junit.jupiter.api.Assertions.assertNotEquals( + HashingUtils.unorderedHash(zeros), HashingUtils.unorderedHash(ones)); + } + + @org.junit.jupiter.api.Test + public void unorderedHashOfEmptyNullAndNullEntries() { + org.junit.jupiter.api.Assertions.assertEquals(0, HashingUtils.unorderedHash(null)); + org.junit.jupiter.api.Assertions.assertEquals( + 0, HashingUtils.unorderedHash(java.util.Collections.emptyMap())); + final java.util.Map nulls = new java.util.HashMap<>(); + nulls.put(null, null); + org.junit.jupiter.api.Assertions.assertEquals( + 1 + HashingUtils.unorderedEntryHash(0, 0), HashingUtils.unorderedHash(nulls)); + } }