Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.datadog.openfeature.internal.config.Settings;
import com.datadog.openfeature.internal.delivery.FlagSelectionRequest;
import com.datadog.openfeature.internal.evaluation.ConfigurationStore;
import com.datadog.openfeature.internal.evaluation.EvaluationFeatureHandles;
import com.datadog.openfeature.internal.evaluation.FlagEvaluator;
import com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache;
import com.datadog.openfeature.internal.exposure.ExposureEvent;
Expand Down Expand Up @@ -154,6 +155,7 @@ FeatureFlagsRuntime runtime() {

@Override
public void accept(final ServerConfiguration config) {
EvaluationFeatureHandles.configurationChanged(config);
this.configuration.set(config);
if (config != null) {
this.configuration.markReady();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.datadog.openfeature;

import static com.datadog.openfeature.internal.evaluation.MetadataKeys.ALLOCATION_KEY;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURES_HANDLE;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVAL_TIMESTAMP_MS;
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.evaluation.EvaluationFeatureHandles;
import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent;
import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter;
import dev.openfeature.sdk.FlagEvaluationDetails;
Expand Down Expand Up @@ -140,7 +142,7 @@ public void finallyAfter(
// whatever the consent.
final Map<String, Object> features =
Boolean.TRUE.equals(values.get(HAS_EVALUATION_FEATURES))
? SplitFeatureMetadata.featuresWithPrefix(metadata, EVALUATION_FEATURE_PREFIX)
? evaluationFeatures(metadata, values.get(EVALUATION_FEATURES_HANDLE))
: Collections.<String, Object>emptyMap();
w.enqueue(
new FlagEvalEvent(
Expand All @@ -157,4 +159,20 @@ public void finallyAfter(
// Never let EVP recording break flag evaluation
}
}

/**
* The split's prepared evaluation features when the handle still resolves, so every evaluation of
* the split passes the same map; otherwise, as after a configuration change, they are rebuilt
* from the metadata entries.
*/
private static Map<String, Object> evaluationFeatures(
final ImmutableMetadata metadata, final Object handle) {
if (handle instanceof Long) {
final Map<String, Object> prepared = EvaluationFeatureHandles.lookup((Long) handle);
if (prepared != null) {
return prepared;
}
}
return SplitFeatureMetadata.featuresWithPrefix(metadata, EVALUATION_FEATURE_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.datadog.openfeature.internal.evaluation;

import com.datadog.openfeature.internal.ufc.ServerConfiguration;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
* Hands the flag evaluation hook each split's prepared evaluation features, so the hook doesn't
* rebuild them from the evaluation metadata on every evaluation.
*
* <p>OpenFeature metadata only carries primitives, so a prepared split puts a {@code long} handle
* in the metadata instead: the epoch of the configuration it was registered under, and its index in
* that epoch's table. A new configuration starts a new epoch, which drops the old table, so the
* table only ever holds the features of the configuration being served. A handle from an older
* epoch resolves to {@code null}, and the hook falls back to reading the metadata entries, which
* are still written.
*
* <p>Registration happens once per split, when it is first served, so it takes a lock; lookups
* don't.
*/
public final class EvaluationFeatureHandles {
/** Bounds the table if a configuration's splits are somehow prepared over and over. */
static final int MAX_HANDLES = 1 << 16;

private static final Object LOCK = new Object();
private static volatile Epoch current = new Epoch(1, null);

private EvaluationFeatureHandles() {}

/** Starts a new epoch when the configuration being served changes. */
public static void configurationChanged(final ServerConfiguration configuration) {
synchronized (LOCK) {
final Epoch epoch = current;
if (epoch.configuration != configuration) {
current = new Epoch(epoch.id + 1, configuration);
}
}
}

/**
* Registers prepared evaluation features in the current epoch.
*
* @return the handle, or {@code 0} if the table is full.
*/
static long register(final Map<String, Object> features) {
synchronized (LOCK) {
final Epoch epoch = current;
final int index = epoch.size;
if (index >= MAX_HANDLES) {
return 0L;
}
Map<String, Object>[] table = epoch.table;
if (index == table.length) {
table = Arrays.copyOf(table, table.length * 2);
}
table[index] = features;
epoch.size = index + 1;
// Written last, so a reader that sees the handle's slot also sees the map.
epoch.table = table;
return ((long) epoch.id << 32) | index;
}
}

/**
* Resolves a handle from the evaluation metadata.
*
* @return the prepared features, or {@code null} if the handle is from an older epoch.
*/
public static Map<String, Object> lookup(final long handle) {
final Epoch epoch = current;
if ((int) (handle >>> 32) != epoch.id) {
return null;
}
final int index = (int) handle;
final Map<String, Object>[] table = epoch.table;
return index >= 0 && index < table.length ? table[index] : null;
}

/**
* Prepared evaluation features: immutable, with their hash computed once. The flag evaluation
* aggregator keys its feature sets by map, so the same instance arriving on every evaluation of a
* split costs it one cached hash and an identity comparison.
*/
static Map<String, Object> prepared(final Map<String, Object> features) {
return new Prepared(features);
}

private static final class Epoch {
final int id;
final ServerConfiguration configuration;
int size;
volatile Map<String, Object>[] table;

@SuppressWarnings("unchecked")
Epoch(final int id, final ServerConfiguration configuration) {
this.id = id;
this.configuration = configuration;
this.table = new Map[16];
}
}

private static final class Prepared extends AbstractMap<String, Object> {
private final Map<String, Object> features;
private final int hash;

Prepared(final Map<String, Object> features) {
this.features = Collections.unmodifiableMap(features);
this.hash = features.hashCode();
}

@Override
public Set<Entry<String, Object>> entrySet() {
return features.entrySet();
}

@Override
public Object get(final Object key) {
return features.get(key);
}

@Override
public boolean containsKey(final Object key) {
return features.containsKey(key);
}

@Override
public int size() {
return features.size();
}

@Override
public int hashCode() {
return hash;
}

// AbstractMap.equals checks identity first, so the same instance compares in one step.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ public final class MetadataKeys {
*/
public static final String HAS_EVALUATION_FEATURES = "__dd_has_evaluation_features";

/**
* The {@link EvaluationFeatureHandles} handle of the split's prepared evaluation features, so the
* flag evaluation hook can take the prepared map instead of rebuilding it from the entries.
*/
public static final String EVALUATION_FEATURES_HANDLE = "__dd_evaluation_features_handle";

private MetadataKeys() {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datadog.openfeature.internal.evaluation;

import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURES_HANDLE;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_FEATURE_PREFIX;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.FEATURE_PREFIX;
Expand All @@ -8,8 +9,10 @@
import com.datadog.openfeature.internal.ufc.Feature;
import dev.openfeature.sdk.ImmutableMetadata;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
Expand All @@ -19,6 +22,9 @@
* The metadata entries of a split's features, resolved when the split is first served and then
* cached on it: the first feature of each key wins, each destination gets its prefixed key, and
* unknown destinations are dropped. Later evaluations only copy the entries.
*
* <p>The features routed to flag evaluations are also built once into a map, registered with {@link
* EvaluationFeatureHandles}, and passed to the flag evaluation hook by handle.
*/
final class SplitFeatures {
private static final Logger LOGGER = LoggerFactory.getLogger(SplitFeatures.class);
Expand All @@ -31,7 +37,7 @@ final class SplitFeatures {
static final Set<String> WARNED_FEATURE_DESTINATIONS = ConcurrentHashMap.newKeySet();

/** The prepared features of a split without metadata entries, shared by all such splits. */
static final SplitFeatures NONE = new SplitFeatures(new String[0], new Object[0], false);
static final SplitFeatures NONE = new SplitFeatures(new String[0], new Object[0], false, 0L);

private final String[] keys;

Expand All @@ -40,11 +46,18 @@ final class SplitFeatures {

private final boolean hasEvaluationFeatures;

/** The {@link EvaluationFeatureHandles} handle of the evaluation features, {@code 0} if none. */
private final long evaluationFeaturesHandle;

private SplitFeatures(
final String[] keys, final Object[] values, final boolean hasEvaluationFeatures) {
final String[] keys,
final Object[] values,
final boolean hasEvaluationFeatures,
final long evaluationFeaturesHandle) {
this.keys = keys;
this.values = values;
this.hasEvaluationFeatures = hasEvaluationFeatures;
this.evaluationFeaturesHandle = evaluationFeaturesHandle;
}

/**
Expand All @@ -59,6 +72,7 @@ static SplitFeatures prepare(final List<Feature> features) {
}
final List<String> keys = new ArrayList<>();
final List<Object> values = new ArrayList<>();
final Map<String, Object> evaluation = new HashMap<>();
final Set<String> seenKeys = new HashSet<>();
boolean evaluationFeatures = false;
for (final Feature feature : features) {
Expand All @@ -76,6 +90,9 @@ static SplitFeatures prepare(final List<Feature> features) {
if (value != null) {
keys.add(prefix + feature.key);
values.add(value);
if (EVALUATION_FEATURE_PREFIX.equals(prefix)) {
evaluation.put(feature.key, value);
}
}
evaluationFeatures |= EVALUATION_FEATURE_PREFIX.equals(prefix);
} else if (WARNED_FEATURE_DESTINATIONS.size() < MAX_WARNED_FEATURE_DESTINATIONS
Expand All @@ -87,8 +104,13 @@ static SplitFeatures prepare(final List<Feature> features) {
if (keys.isEmpty() && !evaluationFeatures) {
return NONE;
}
// Matches what the hook would read back from the metadata entries.
final long handle =
evaluation.isEmpty()
? 0L
: EvaluationFeatureHandles.register(EvaluationFeatureHandles.prepared(evaluation));
return new SplitFeatures(
keys.toArray(new String[0]), values.toArray(new Object[0]), evaluationFeatures);
keys.toArray(new String[0]), values.toArray(new Object[0]), evaluationFeatures, handle);
}

/** Adds the entries, and the evaluation-features marker when there are any, to metadata. */
Expand All @@ -106,6 +128,13 @@ void addTo(final ImmutableMetadata.ImmutableMetadataBuilder metadataBuilder) {
if (hasEvaluationFeatures) {
metadataBuilder.addBoolean(HAS_EVALUATION_FEATURES, true);
}
if (evaluationFeaturesHandle != 0L) {
metadataBuilder.addLong(EVALUATION_FEATURES_HANDLE, evaluationFeaturesHandle);
}
}

long evaluationFeaturesHandle() {
return evaluationFeaturesHandle;
}

/** The number of metadata entries, without the evaluation-features marker. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datadog.openfeature;

import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURES_HANDLE;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_FEATURE_PREFIX;
import static com.datadog.openfeature.internal.evaluation.MetadataKeys.FEATURE_PREFIX;
Expand All @@ -10,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand All @@ -21,6 +23,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.datadog.openfeature.internal.evaluation.EvaluationFeatureHandlesForTest;
import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent;
import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter;
import dev.openfeature.sdk.ErrorCode;
Expand Down Expand Up @@ -151,6 +154,44 @@ void finallyAfterAddsEvaluationFeaturesWhateverTheConsent(final boolean consent)
"only EVALUATION features reach the event, whatever the consent");
}

@Test
void finallyAfterPassesThePreparedFeaturesWhileTheHandleResolves() {
final Map<String, Object> prepared = Collections.singletonMap("bandit.policy_id", "p2");
final long handle = EvaluationFeatureHandlesForTest.register(prepared);
final AtomicReference<FlagEvalEvent> captured = new AtomicReference<>();
final FlagEvalLoggingHook<Object> hook = hookWithWriter(capturingWriter(captured));

hook.finallyAfter(null, featureDetails(handle), Collections.emptyMap());
final Map<String, Object> first = captured.get().features;
hook.finallyAfter(null, featureDetails(handle), Collections.emptyMap());

assertEquals(prepared, first);
assertSame(first, captured.get().features, "every evaluation of the split passes one map");
}

@Test
void finallyAfterRebuildsTheFeaturesWhenTheHandleIsStale() {
final AtomicReference<FlagEvalEvent> captured = new AtomicReference<>();
final FlagEvalLoggingHook<Object> hook = hookWithWriter(capturingWriter(captured));

hook.finallyAfter(null, featureDetails(Long.MAX_VALUE), Collections.emptyMap());

assertEquals(Collections.singletonMap("bandit.policy_id", "p2"), captured.get().features);
}

private static FlagEvaluationDetails<Object> featureDetails(final long handle) {
return details(
"my-flag",
"on-value",
"on",
Reason.TARGETING_MATCH.name(),
ImmutableMetadata.builder()
.addString(EVALUATION_FEATURE_PREFIX + "bandit.policy_id", "p2")
.addBoolean(HAS_EVALUATION_FEATURES, true)
.addLong(EVALUATION_FEATURES_HANDLE, handle)
.build());
}

@Test
void finallyAfterSkipsEvaluationFeaturesWithoutTheMarker() {
final AtomicReference<FlagEvalEvent> captured = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.datadog.openfeature.internal.evaluation;

import java.util.Map;

/** Registers evaluation features from tests outside this package. */
public final class EvaluationFeatureHandlesForTest {
private EvaluationFeatureHandlesForTest() {}

public static long register(final Map<String, Object> features) {
return EvaluationFeatureHandles.register(EvaluationFeatureHandles.prepared(features));
}
}
Loading
Loading