Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/sdk/server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ ext.versions = [
"guava": "33.8.0-jre",
"jackson": "2.11.2",
"jsr305": "3.0.2",
"launchdarklyJavaSdkCommon": "2.3.0",
"launchdarklyJavaSdkInternal": "1.14.0",
"launchdarklyJavaSdkCommon": "2.6.0",
"launchdarklyJavaSdkInternal": "1.15.0",
"launchdarklyLogging": "1.1.0",
"okhttp": "4.12.0", // specify this for the SDK build instead of relying on the transitive dependency from okhttp-eventsource
"okhttpEventsource": "5.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public static class SdkConfigParams {
SdkConfigHookParams hooks;
SdkConfigPersistentDataStoreParams persistentDataStore;
SdkConfigDataSystemParams dataSystem;
SdkConfigOverridesParams overrides;
}

/**
* Configuration for the SDK's file-based flag overrides (the "flag-overrides" capability).
*/
public static class SdkConfigOverridesParams {
/** Absolute paths of the override files, in precedence order. */
String[] filePaths;
/** "fail" (the default) or "ignore". */
String duplicateKeysHandling;
/** "polling" or "watching"; null means the SDK default. */
String changeDetection;
/** The polling interval in milliseconds; the SDK may raise it to its minimum. */
Long pollIntervalMs;
}

public static class SdkConfigStreamParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import com.launchdarkly.sdk.server.integrations.FDv2PollingInitializerBuilder;
import com.launchdarkly.sdk.server.integrations.FDv2PollingSynchronizerBuilder;
import com.launchdarkly.sdk.server.integrations.FDv2StreamingSynchronizerBuilder;
import com.launchdarkly.sdk.server.integrations.FileData;
import com.launchdarkly.sdk.server.integrations.FileOverrideSourceBuilder;
import com.launchdarkly.sdk.server.integrations.FileOverrides;
import com.launchdarkly.sdk.server.interfaces.BigSegmentStoreStatusProvider;
import com.launchdarkly.sdk.server.subsystems.DataSourceBuilder;
import com.launchdarkly.sdk.server.datasources.Initializer;
Expand Down Expand Up @@ -99,6 +102,7 @@
import sdktest.Representations.StoreMode;
import sdktest.Representations.SdkConfigDataSystemParams;
import sdktest.Representations.SdkConfigDataInitializerParams;
import sdktest.Representations.SdkConfigOverridesParams;
import sdktest.Representations.SdkConfigSynchronizerParams;
import sdktest.Representations.SdkConfigPollingParams;
import sdktest.Representations.SdkConfigStreamingParams;
Expand Down Expand Up @@ -667,7 +671,13 @@ private LDConfig buildSdkConfig(SdkConfigParams params, String tag) {
dataSystemBuilder.fDv1FallbackSynchronizer(fdv1Polling);
}

if (params.overrides != null) {
dataSystemBuilder.overrides(makeOverridesConfig(params.overrides));
}

builder.dataSystem(dataSystemBuilder);
} else if (params.overrides != null) {
throw new IllegalArgumentException("flag overrides require the data system to be configured");
}

return builder.build();
Expand Down Expand Up @@ -733,6 +743,41 @@ private static PersistentDataStoreBuilder buildPersistentStore(SdkConfigPersiste
return builder;
}

private static FileOverrideSourceBuilder makeOverridesConfig(SdkConfigOverridesParams params) {
FileOverrideSourceBuilder overrides = FileOverrides.source();
if (params.filePaths != null) {
overrides.filePaths(params.filePaths);
}
if (params.duplicateKeysHandling != null) {
switch (params.duplicateKeysHandling) {
case "fail":
overrides.duplicateKeysHandling(FileData.DuplicateKeysHandling.FAIL);
break;
case "ignore":
overrides.duplicateKeysHandling(FileData.DuplicateKeysHandling.IGNORE);
break;
default:
throw new IllegalArgumentException("unknown duplicate keys handling: " + params.duplicateKeysHandling);
}
}
if (params.changeDetection != null) {
switch (params.changeDetection) {
case "polling":
overrides.changeDetection(FileOverrides.ChangeDetection.POLLING);
break;
case "watching":
overrides.changeDetection(FileOverrides.ChangeDetection.WATCHING);
break;
default:
throw new IllegalArgumentException("unknown change detection mode: " + params.changeDetection);
}
}
if (params.pollIntervalMs != null) {
overrides.pollInterval(Duration.ofMillis(params.pollIntervalMs));
}
return overrides;
}

private DataSourceBuilder<Synchronizer> createSynchronizer(
SdkConfigSynchronizerParams synchronizer,
String payloadFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public class TestService {
"instance-id",
"retry-conformance-fdv1-streaming",
"retry-conformance-fdv1-polling",
"diagnostic-events"
"diagnostic-events",
"flag-overrides"
};

static final Gson gson = new GsonBuilder().serializeNulls().create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ static final class FeatureFlag implements VersionedData, JsonHelpers.PostProcess
private Migration migration;
private boolean excludeFromSummaries;

// True if this definition came from the SDK's override store rather than from LaunchDarkly. The
// field is transient so that it never appears in the JSON form of the flag and never takes part
// in deserialization. Only the override layer sets it, on a copy that it owns. Evaluation reads
// it to mark the evaluation as override-affected. Other readers treat a marked flag the same as
// any other flag.
//
// Flag overrides are currently experimental and subject to change.
private transient boolean isOverride;

/**
* Tells which kinds of client-side SDK can use the flag. The flag has this object only if its JSON
* has a "clientSideAvailability" object.
Expand Down Expand Up @@ -300,6 +309,31 @@ boolean isExcludeFromSummaries() {
return excludeFromSummaries;
}

/**
* Returns true if this definition came from the override store.
*
* @return true for an override entry
*/
boolean isOverride() {
return isOverride;
}

/**
* Returns a shallow copy of this flag that carries the override marker. The copy shares its
* nested lists and its preprocessing data with this flag and never writes to them. This flag is
* not modified.
*
* @return a marked copy
*/
FeatureFlag markedAsOverride() {
FeatureFlag copy = new FeatureFlag(key, version, on, prerequisites, salt, targets, contextTargets, rules,
fallthrough, offVariation, variations, clientSide, clientSideAvailability, trackEvents,
trackEventsFallthrough, debugEventsUntilDate, deleted, samplingRatio, migration, excludeFromSummaries);
copy.preprocessed = preprocessed;
copy.isOverride = true;
return copy;
}

public void afterDeserialized() {
// The "clientSideAvailability" object is in the newer schema. If the flag has the object, set
// "clientSide" to the value of "usingEnvironmentId". The SDK then writes the two properties
Expand Down Expand Up @@ -546,6 +580,10 @@ static final class Segment implements VersionedData, JsonHelpers.PostProcessingD
private ContextKind unboundedContextKind;
private Integer generation;

// True if this definition came from the SDK's override store rather than from LaunchDarkly. See
// the note on the same field in FeatureFlag.
private transient boolean isOverride;

Segment() {}

Segment(String key,
Expand Down Expand Up @@ -627,6 +665,28 @@ public Integer getGeneration() {
return generation;
}

/**
* Returns true if this definition came from the override store.
*
* @return true for an override entry
*/
boolean isOverride() {
return isOverride;
}

/**
* Returns a shallow copy of this segment that carries the override marker. The copy shares its
* nested collections with this segment and never writes to them. This segment is not modified.
*
* @return a marked copy
*/
Segment markedAsOverride() {
Segment copy = new Segment(key, included, excluded, includedContexts, excludedContexts, salt, rules,
version, deleted, unbounded, unboundedContextKind, generation);
copy.isOverride = true;
return copy;
}

public void afterDeserialized() {
DataModelPreprocessing.preprocessSegment(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ public int compare(DataKind o1, DataKind o2) {
/**
* Maintains a bidirectional dependency graph that can be updated whenever an item has changed.
*/
/**
* Dependency edges that live outside the LaunchDarkly data, such as those of the override
* layer's entries. Given an item, adds it and everything that depends on it through those edges.
*/
interface ExternalDependencies {
void addAffectedItems(Set<KindAndKey> itemsOut, KindAndKey item);
}

static final class DependencyTracker {
private final Map<KindAndKey, Set<KindAndKey>> dependenciesFrom = new HashMap<>();
private final Map<KindAndKey, Set<KindAndKey>> dependenciesTo = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Deque;
import java.util.Arrays;
import java.util.ArrayDeque;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -56,6 +59,10 @@ final class DataSourceUpdatesImpl implements DataSourceUpdateSink, DataSourceUpd
private final EventBroadcasterImpl<FlagChangeListener, FlagChangeEvent> flagChangeEventNotifier;
private final EventBroadcasterImpl<StatusListener, Status> dataSourceStatusNotifier;
private final DataModelDependencies.DependencyTracker dependencyTracker = new DataModelDependencies.DependencyTracker();
// Dependencies of definitions the data system does not hold. The override layer supplies its
// entries' dependencies, so that a change to a LaunchDarkly item also notifies the overridden
// flags that reference it. Null when no override source is configured.
private volatile DataModelDependencies.ExternalDependencies externalDependencies = null;
private final DataStoreStatusProvider dataStoreStatusProvider;
private final OutageTracker outageTracker;
private final Object stateLock = new Object();
Expand Down Expand Up @@ -84,6 +91,47 @@ final class DataSourceUpdatesImpl implements DataSourceUpdateSink, DataSourceUpd
currentStatus = new Status(State.INITIALIZING, Instant.now(), null);
}

/**
* Registers dependency edges that live outside the LaunchDarkly data, such as the override
* layer's. Called once during construction of the data system.
*/
void setExternalDependencies(DataModelDependencies.ExternalDependencies externalDependencies) {
this.externalDependencies = externalDependencies;
}

/**
* Adds the modified item and everything that depends on it to the set. The LaunchDarkly data and
* the external dependencies each know their own edges, and a chain can cross between them in
* either direction: an overridden flag can reference a LaunchDarkly segment, and a LaunchDarkly
* flag can have an overridden prerequisite. Each newly reached item is therefore expanded through
* both until nothing new is found.
*/
private void addAffectedItems(Set<KindAndKey> itemsOut, KindAndKey item) {
DataModelDependencies.ExternalDependencies external = externalDependencies;
if (external == null) {
dependencyTracker.addAffectedItems(itemsOut, item);
return;
}
Deque<KindAndKey> pending = new ArrayDeque<>();
pending.push(item);
while (!pending.isEmpty()) {
KindAndKey next = pending.pop();
// Each tracker gets its own set: a tracker skips an item that is already in the set it is
// given, so sharing one set would stop the second tracker at the starting item.
Set<KindAndKey> viaData = new HashSet<>();
dependencyTracker.addAffectedItems(viaData, next);
Set<KindAndKey> viaExternal = new HashSet<>();
external.addAffectedItems(viaExternal, next);
for (Set<KindAndKey> reached : Arrays.asList(viaData, viaExternal)) {
for (KindAndKey reachedItem : reached) {
if (itemsOut.add(reachedItem)) {
pending.push(reachedItem);
}
}
}
}
}

@Override
public boolean init(FullDataSet<ItemDescriptor> allData) {
Map<DataKind, Map<String, ItemDescriptor>> oldData = null;
Expand Down Expand Up @@ -132,7 +180,7 @@ public boolean upsert(DataKind kind, String key, ItemDescriptor item) {
dependencyTracker.updateDependenciesFrom(kind, key, item);
if (hasFlagChangeEventListeners()) {
Set<KindAndKey> affectedItems = new HashSet<>();
dependencyTracker.addAffectedItems(affectedItems, new KindAndKey(kind, key));
addAffectedItems(affectedItems, new KindAndKey(kind, key));
sendChangeEvents(affectedItems);
}
}
Expand Down Expand Up @@ -270,7 +318,7 @@ private Set<KindAndKey> computeChangedItemsForFullDataSet(Map<DataKind, Map<Stri
continue;
}
if (oldItem == null || newItem == null || oldItem.getVersion() < newItem.getVersion()) {
dependencyTracker.addAffectedItems(affectedItems, new KindAndKey(kind, key));
addAffectedItems(affectedItems, new KindAndKey(kind, key));
}
// Note that comparing the version numbers is sufficient; we don't have to compare every detail of the
// flag or segment configuration, because it's a basic underlying assumption of the entire LD data model
Expand Down Expand Up @@ -539,7 +587,7 @@ private Set<KindAndKey> handlePartialChangeset(
for (Map.Entry<String, ItemDescriptor> itemEntry: kindEntry.getValue().getItems()) {
String key = itemEntry.getKey();
dependencyTracker.updateDependenciesFrom(kind, key, itemEntry.getValue());
dependencyTracker.addAffectedItems(affectedItems, new KindAndKey(kind, key));
addAffectedItems(affectedItems, new KindAndKey(kind, key));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ interface DataSystem {
* @return the environment ID, or null
*/
String getEnvironmentId();

/**
* Returns the override layer, or null if no override source is configured. The layer holds the
* entries that take precedence over LaunchDarkly data; {@link #getStore()} already applies them.
* The client consults the layer directly only for the not-initialized short-circuit.
*
* @return the override layer, or null
*/
OverrideLayer getOverrideLayer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public void recordEvaluationEvent(LDContext context, String flagKey, int flagVer
LDValue value, EvaluationReason reason, LDValue defaultValue, String prerequisiteOfFlagKey,
boolean requireFullEvent, Long debugEventsUntilDate, boolean excludeFromSummaries,
Long samplingRatio) {
recordEvaluationEvent(context, flagKey, flagVersion, variation, value, reason, defaultValue, prerequisiteOfFlagKey,
requireFullEvent, debugEventsUntilDate, excludeFromSummaries, samplingRatio, false);
}

@Override
public void recordEvaluationEvent(LDContext context, String flagKey, int flagVersion, int variation,
LDValue value, EvaluationReason reason, LDValue defaultValue, String prerequisiteOfFlagKey,
boolean requireFullEvent, Long debugEventsUntilDate, boolean excludeFromSummaries,
Long samplingRatio, boolean overrideAffected) {
eventProcessor.sendEvent(new Event.FeatureRequest(
System.currentTimeMillis(),
flagKey,
Expand All @@ -48,7 +57,8 @@ public void recordEvaluationEvent(LDContext context, String flagKey, int flagVer
debugEventsUntilDate,
false,
samplingRatio != null ? samplingRatio : 1,
excludeFromSummaries
excludeFromSummaries,
overrideAffected
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ public EvaluationDetail<String> getAsString() {
*/
public boolean isForceReasonTracking() { return forceReasonTracking; }

/**
* Returns true if an override affected this evaluation, directly or transitively. The value is
* the reason's indicator, so the result and the reason it returns to the caller always agree.
* Flag overrides are currently experimental and subject to change.
* @return true if an override affected the evaluation
*/
public boolean isOverrideAffected() { return anyType.getReason().isOverrideAffected(); }

public List<PrerequisiteEvalRecord> getPrerequisiteEvalRecords() { return prerequisiteEvalRecords; }

/**
Expand All @@ -251,6 +259,17 @@ public EvalResult withForceReasonTracking(boolean newValue) {
public EvalResult withPrerequisiteEvalRecords(List<PrerequisiteEvalRecord> newValue) {
return this.prerequisiteEvalRecords == newValue ? this : new EvalResult(this, newValue);
}

/**
* Returns a transformed copy of this EvalResult whose reason carries the given override-affected
* indicator, or this same instance if the indicator is unchanged. Precomputed results are shared
* between evaluations, so a marked result is always a new instance.
* @param newValue the new value for the indicator
* @return a transformed copy
*/
public EvalResult withOverrideAffected(boolean newValue) {
return withReason(anyType.getReason().withOverrideAffected(newValue));
}

@Override
public boolean equals(Object other) {
Expand Down
Loading