Skip to content

Commit 5f2084d

Browse files
committed
Updated evaluator
1 parent 57cac15 commit 5f2084d

File tree

6 files changed

+169
-68
lines changed

6 files changed

+169
-68
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/utils/Utils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.split.client.utils;
22

33
import io.split.client.dtos.ChangeDto;
4+
import io.split.client.dtos.FallbackTreatmentsConfiguration;
5+
import io.split.engine.evaluator.EvaluatorImp.TreatmentLabelAndChangeNumber;
46
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
57
import org.apache.hc.core5.http.ContentType;
68
import org.apache.hc.core5.http.HttpEntity;
@@ -45,4 +47,30 @@ public static URI appendPath(URI root, String pathToAppend) throws URISyntaxExce
4547
public static <T> boolean checkExitConditions(ChangeDto<T> change, long cn) {
4648
return change.t < cn && change.t != -1;
4749
}
50+
51+
public static TreatmentLabelAndChangeNumber checkFallbackTreatments(String treatment, String label,
52+
String feature_name, Long changeNumber,
53+
FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) {
54+
if (fallbackTreatmentsConfiguration != null) {
55+
if (fallbackTreatmentsConfiguration.getByFlagFallbackTreatment() != null
56+
&& fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(feature_name) != null
57+
&& !fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(feature_name).getTreatment().isEmpty()) {
58+
return new TreatmentLabelAndChangeNumber(
59+
fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(feature_name).getTreatment(),
60+
fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(feature_name).getLabel() + label,
61+
changeNumber);
62+
}
63+
64+
if (fallbackTreatmentsConfiguration.getGlobalFallbackTreatment() != null
65+
&& !fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getTreatment().isEmpty()) {
66+
return new TreatmentLabelAndChangeNumber(fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getTreatment(),
67+
fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getLabel() + label,
68+
changeNumber);
69+
}
70+
}
71+
72+
return new TreatmentLabelAndChangeNumber(treatment,
73+
label,
74+
changeNumber);
75+
}
4876
}

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

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

33
import io.split.client.dtos.ConditionType;
4+
import io.split.client.dtos.FallbackTreatmentsConfiguration;
45
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
56
import io.split.engine.experiments.ParsedCondition;
67
import io.split.engine.experiments.ParsedSplit;
@@ -19,26 +20,30 @@
1920
import java.util.Map;
2021

2122
import static com.google.common.base.Preconditions.checkNotNull;
23+
import static io.split.client.utils.Utils.checkFallbackTreatments;
2224

2325
public class EvaluatorImp implements Evaluator {
2426
private static final Logger _log = LoggerFactory.getLogger(EvaluatorImp.class);
2527

2628
private final SegmentCacheConsumer _segmentCacheConsumer;
2729
private final EvaluationContext _evaluationContext;
2830
private final SplitCacheConsumer _splitCacheConsumer;
31+
private final FallbackTreatmentsConfiguration _fallbackTreatmentsConfiguration;
2932

3033
public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache,
31-
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) {
34+
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer,
35+
FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) {
3236
_splitCacheConsumer = checkNotNull(splitCacheConsumer);
3337
_segmentCacheConsumer = checkNotNull(segmentCache);
3438
_evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer);
39+
_fallbackTreatmentsConfiguration = fallbackTreatmentsConfiguration;
3540
}
3641

3742
@Override
3843
public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String featureFlag, Map<String,
3944
Object> attributes) {
4045
ParsedSplit parsedSplit = _splitCacheConsumer.get(featureFlag);
41-
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit);
46+
return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit, featureFlag);
4247
}
4348

4449
@Override
@@ -49,7 +54,7 @@ public Map<String, TreatmentLabelAndChangeNumber> evaluateFeatures(String matchi
4954
if (parsedSplits == null) {
5055
return results;
5156
}
52-
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s))));
57+
featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s), s)));
5358
return results;
5459
}
5560

@@ -172,18 +177,18 @@ private String getConfig(ParsedSplit parsedSplit, String returnedTreatment) {
172177
}
173178

174179
private TreatmentLabelAndChangeNumber evaluateParsedSplit(String matchingKey, String bucketingKey, Map<String, Object> attributes,
175-
ParsedSplit parsedSplit) {
180+
ParsedSplit parsedSplit, String feature_name) {
176181
try {
177182
if (parsedSplit == null) {
178-
return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND);
183+
return checkFallbackTreatments(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND, feature_name, null, _fallbackTreatmentsConfiguration);
179184
}
180185
return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes);
181186
} catch (ChangeNumberExceptionWrapper e) {
182187
_log.error("Evaluator Exception", e.wrappedException());
183-
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION, e.changeNumber());
188+
return checkFallbackTreatments(Treatments.CONTROL, Labels.EXCEPTION, feature_name, e.changeNumber(), _fallbackTreatmentsConfiguration);
184189
} catch (Exception e) {
185190
_log.error("Evaluator Exception", e);
186-
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION);
191+
return checkFallbackTreatments(Treatments.CONTROL, Labels.EXCEPTION, feature_name, null, _fallbackTreatmentsConfiguration);
187192
}
188193
}
189194

0 commit comments

Comments
 (0)