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
7 changes: 4 additions & 3 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
config.getThreadFactory());

// Evaluator
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache);
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, null);

// SplitClient
_client = new SplitClientImpl(this,
Expand Down Expand Up @@ -348,7 +348,8 @@ protected SplitFactoryImpl(String apiToken, SplitClientConfig config, CustomStor
_telemetrySynchronizer = new TelemetryConsumerSubmitter(customStorageWrapper, _sdkMetadata);
UserCustomRuleBasedSegmentAdapterConsumer userCustomRuleBasedSegmentAdapterConsumer =
new UserCustomRuleBasedSegmentAdapterConsumer(customStorageWrapper);
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer, userCustomRuleBasedSegmentAdapterConsumer);
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer,
userCustomRuleBasedSegmentAdapterConsumer, null);
_impressionsSender = PluggableImpressionSender.create(customStorageWrapper);
_uniqueKeysTracker = createUniqueKeysTracker(config);
_impressionsManager = buildImpressionsManager(config, userCustomImpressionAdapterConsumer,
Expand Down Expand Up @@ -446,7 +447,7 @@ protected SplitFactoryImpl(SplitClientConfig config) {
_impressionsManager, null, null, null);

// Evaluator
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache);
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, null);

EventsStorage eventsStorage = new NoopEventsStorageImp();

Expand Down
10 changes: 8 additions & 2 deletions client/src/main/java/io/split/client/dtos/FallbackTreatment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ public class FallbackTreatment {
public FallbackTreatment(String treatment, Map<String, Object> config) {
_treatment = treatment;
_config = config;
_label = "fallback - ";
_label = null;
}

public FallbackTreatment(String treatment) {
_treatment = treatment;
_config = null;
_label = "fallback - ";
_label = null;
}

public FallbackTreatment(String treatment, Map<String, Object> config, String label) {
_treatment = treatment;
_config = config;
_label = label;
}

public Map<String, Object> getConfig() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.split.client.dtos;

public interface FallbackTreatmentCalculator
{
FallbackTreatment resolve(String flagName, String label);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.split.client.dtos;

import io.split.grammar.Treatments;

public class FallbackTreatmentCalculatorImp implements FallbackTreatmentCalculator
{
private final FallbackTreatmentsConfiguration _fallbackTreatmentsConfiguration;
private final String labelPrefix = "fallback - ";

public FallbackTreatmentCalculatorImp(FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) {
_fallbackTreatmentsConfiguration = fallbackTreatmentsConfiguration;
}

public FallbackTreatment resolve(String flagName, String label) {
if (_fallbackTreatmentsConfiguration != null) {
if (_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment() != null
&& _fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName) != null) {
return copyWithLabel(_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName),
resolveLabel(label));
}
if (_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment() != null) {
return copyWithLabel(_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment(),
resolveLabel(label));
}
}

return new FallbackTreatment(Treatments.CONTROL, null, label);
}

private String resolveLabel(String label) {
if (label == null) {
return null;
}
return labelPrefix + label;
}

private FallbackTreatment copyWithLabel(FallbackTreatment fallbackTreatment, String label) {
return new FallbackTreatment(fallbackTreatment.getTreatment(), fallbackTreatment.getConfig(), label);
}
}
24 changes: 17 additions & 7 deletions client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.split.engine.evaluator;

import io.split.client.dtos.ConditionType;
import io.split.client.dtos.FallbackTreatment;
import io.split.client.dtos.FallbackTreatmentCalculator;
import io.split.client.dtos.FallbackTreatmentsConfiguration;
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedSplit;
Expand All @@ -26,19 +29,22 @@ public class EvaluatorImp implements Evaluator {
private final SegmentCacheConsumer _segmentCacheConsumer;
private final EvaluationContext _evaluationContext;
private final SplitCacheConsumer _splitCacheConsumer;
private final FallbackTreatmentCalculator _fallbackTreatmentCalculator;

public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache,
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) {
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer,
FallbackTreatmentCalculator fallbackTreatmentCalculator) {
_splitCacheConsumer = checkNotNull(splitCacheConsumer);
_segmentCacheConsumer = checkNotNull(segmentCache);
_evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer);
_fallbackTreatmentCalculator = fallbackTreatmentCalculator;
}

@Override
public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String featureFlag, Map<String,
Object> attributes) {
ParsedSplit parsedSplit = _splitCacheConsumer.get(featureFlag);
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit);
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit, featureFlag);
}

@Override
Expand All @@ -49,7 +55,7 @@ public Map<String, TreatmentLabelAndChangeNumber> evaluateFeatures(String matchi
if (parsedSplits == null) {
return results;
}
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s))));
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s), s)));
return results;
}

Expand Down Expand Up @@ -172,18 +178,22 @@ private String getConfig(ParsedSplit parsedSplit, String returnedTreatment) {
}

private TreatmentLabelAndChangeNumber evaluateParsedSplit(String matchingKey, String bucketingKey, Map<String, Object> attributes,
ParsedSplit parsedSplit) {
ParsedSplit parsedSplit, String featureName) {
try {

if (parsedSplit == null) {
return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND);
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.DEFINITION_NOT_FOUND);
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
}
return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes);
} catch (ChangeNumberExceptionWrapper e) {
_log.error("Evaluator Exception", e.wrappedException());
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION, e.changeNumber());
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel(), e.changeNumber());
} catch (Exception e) {
_log.error("Evaluator Exception", e);
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION);
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
}
}

Expand Down
Loading