Skip to content

Commit d0c503b

Browse files
authored
Merge pull request #598 from splitio/FME-9876-fallback-evaluator
Updated evaluator
2 parents 23e25e6 + be872d2 commit d0c503b

File tree

9 files changed

+243
-70
lines changed

9 files changed

+243
-70
lines changed

client/src/main/java/io/split/client/SplitFactoryImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
257257
config.getThreadFactory());
258258

259259
// Evaluator
260-
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache);
260+
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, null);
261261

262262
// SplitClient
263263
_client = new SplitClientImpl(this,
@@ -348,7 +348,8 @@ protected SplitFactoryImpl(String apiToken, SplitClientConfig config, CustomStor
348348
_telemetrySynchronizer = new TelemetryConsumerSubmitter(customStorageWrapper, _sdkMetadata);
349349
UserCustomRuleBasedSegmentAdapterConsumer userCustomRuleBasedSegmentAdapterConsumer =
350350
new UserCustomRuleBasedSegmentAdapterConsumer(customStorageWrapper);
351-
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer, userCustomRuleBasedSegmentAdapterConsumer);
351+
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer,
352+
userCustomRuleBasedSegmentAdapterConsumer, null);
352353
_impressionsSender = PluggableImpressionSender.create(customStorageWrapper);
353354
_uniqueKeysTracker = createUniqueKeysTracker(config);
354355
_impressionsManager = buildImpressionsManager(config, userCustomImpressionAdapterConsumer,
@@ -446,7 +447,7 @@ protected SplitFactoryImpl(SplitClientConfig config) {
446447
_impressionsManager, null, null, null);
447448

448449
// Evaluator
449-
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache);
450+
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, null);
450451

451452
EventsStorage eventsStorage = new NoopEventsStorageImp();
452453

client/src/main/java/io/split/client/dtos/FallbackTreatment.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ public class FallbackTreatment {
1010
public FallbackTreatment(String treatment, Map<String, Object> config) {
1111
_treatment = treatment;
1212
_config = config;
13-
_label = "fallback - ";
13+
_label = null;
1414
}
1515

1616
public FallbackTreatment(String treatment) {
1717
_treatment = treatment;
1818
_config = null;
19-
_label = "fallback - ";
19+
_label = null;
20+
}
21+
22+
public FallbackTreatment(String treatment, Map<String, Object> config, String label) {
23+
_treatment = treatment;
24+
_config = config;
25+
_label = label;
2026
}
2127

2228
public Map<String, Object> getConfig() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.split.client.dtos;
2+
3+
public interface FallbackTreatmentCalculator
4+
{
5+
FallbackTreatment resolve(String flagName, String label);
6+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.split.client.dtos;
2+
3+
import io.split.grammar.Treatments;
4+
5+
public class FallbackTreatmentCalculatorImp implements FallbackTreatmentCalculator
6+
{
7+
private final FallbackTreatmentsConfiguration _fallbackTreatmentsConfiguration;
8+
private final String labelPrefix = "fallback - ";
9+
10+
public FallbackTreatmentCalculatorImp(FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) {
11+
_fallbackTreatmentsConfiguration = fallbackTreatmentsConfiguration;
12+
}
13+
14+
public FallbackTreatment resolve(String flagName, String label) {
15+
if (_fallbackTreatmentsConfiguration != null) {
16+
if (_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment() != null
17+
&& _fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName) != null) {
18+
return copyWithLabel(_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName),
19+
resolveLabel(label));
20+
}
21+
if (_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment() != null) {
22+
return copyWithLabel(_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment(),
23+
resolveLabel(label));
24+
}
25+
}
26+
27+
return new FallbackTreatment(Treatments.CONTROL, null, label);
28+
}
29+
30+
private String resolveLabel(String label) {
31+
if (label == null) {
32+
return null;
33+
}
34+
return labelPrefix + label;
35+
}
36+
37+
private FallbackTreatment copyWithLabel(FallbackTreatment fallbackTreatment, String label) {
38+
return new FallbackTreatment(fallbackTreatment.getTreatment(), fallbackTreatment.getConfig(), label);
39+
}
40+
}

client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.split.engine.evaluator;
22

33
import io.split.client.dtos.ConditionType;
4+
import io.split.client.dtos.FallbackTreatment;
5+
import io.split.client.dtos.FallbackTreatmentCalculator;
6+
import io.split.client.dtos.FallbackTreatmentsConfiguration;
47
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
58
import io.split.engine.experiments.ParsedCondition;
69
import io.split.engine.experiments.ParsedSplit;
@@ -26,19 +29,22 @@ public class EvaluatorImp implements Evaluator {
2629
private final SegmentCacheConsumer _segmentCacheConsumer;
2730
private final EvaluationContext _evaluationContext;
2831
private final SplitCacheConsumer _splitCacheConsumer;
32+
private final FallbackTreatmentCalculator _fallbackTreatmentCalculator;
2933

3034
public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache,
31-
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) {
35+
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer,
36+
FallbackTreatmentCalculator fallbackTreatmentCalculator) {
3237
_splitCacheConsumer = checkNotNull(splitCacheConsumer);
3338
_segmentCacheConsumer = checkNotNull(segmentCache);
3439
_evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer);
40+
_fallbackTreatmentCalculator = fallbackTreatmentCalculator;
3541
}
3642

3743
@Override
3844
public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String featureFlag, Map<String,
3945
Object> attributes) {
4046
ParsedSplit parsedSplit = _splitCacheConsumer.get(featureFlag);
41-
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit);
47+
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit, featureFlag);
4248
}
4349

4450
@Override
@@ -49,7 +55,7 @@ public Map<String, TreatmentLabelAndChangeNumber> evaluateFeatures(String matchi
4955
if (parsedSplits == null) {
5056
return results;
5157
}
52-
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s))));
58+
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s), s)));
5359
return results;
5460
}
5561

@@ -172,18 +178,22 @@ private String getConfig(ParsedSplit parsedSplit, String returnedTreatment) {
172178
}
173179

174180
private TreatmentLabelAndChangeNumber evaluateParsedSplit(String matchingKey, String bucketingKey, Map<String, Object> attributes,
175-
ParsedSplit parsedSplit) {
181+
ParsedSplit parsedSplit, String featureName) {
176182
try {
183+
177184
if (parsedSplit == null) {
178-
return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND);
185+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.DEFINITION_NOT_FOUND);
186+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
179187
}
180188
return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes);
181189
} catch (ChangeNumberExceptionWrapper e) {
182190
_log.error("Evaluator Exception", e.wrappedException());
183-
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION, e.changeNumber());
191+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
192+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel(), e.changeNumber());
184193
} catch (Exception e) {
185194
_log.error("Evaluator Exception", e);
186-
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION);
195+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
196+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
187197
}
188198
}
189199

0 commit comments

Comments
 (0)