Skip to content

Commit be872d2

Browse files
committed
added fallback calculator
1 parent cda0248 commit be872d2

File tree

7 files changed

+112
-40
lines changed

7 files changed

+112
-40
lines changed

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/client/utils/Utils.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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;
64
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
75
import org.apache.hc.core5.http.ContentType;
86
import org.apache.hc.core5.http.HttpEntity;
@@ -47,30 +45,4 @@ public static URI appendPath(URI root, String pathToAppend) throws URISyntaxExce
4745
public static <T> boolean checkExitConditions(ChangeDto<T> change, long cn) {
4846
return change.t < cn && change.t != -1;
4947
}
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-
}
7648
}

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,8 @@
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;
46
import io.split.client.dtos.FallbackTreatmentsConfiguration;
57
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
68
import io.split.engine.experiments.ParsedCondition;
@@ -20,23 +22,22 @@
2022
import java.util.Map;
2123

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

2526
public class EvaluatorImp implements Evaluator {
2627
private static final Logger _log = LoggerFactory.getLogger(EvaluatorImp.class);
2728

2829
private final SegmentCacheConsumer _segmentCacheConsumer;
2930
private final EvaluationContext _evaluationContext;
3031
private final SplitCacheConsumer _splitCacheConsumer;
31-
private final FallbackTreatmentsConfiguration _fallbackTreatmentsConfiguration;
32+
private final FallbackTreatmentCalculator _fallbackTreatmentCalculator;
3233

3334
public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache,
3435
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer,
35-
FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) {
36+
FallbackTreatmentCalculator fallbackTreatmentCalculator) {
3637
_splitCacheConsumer = checkNotNull(splitCacheConsumer);
3738
_segmentCacheConsumer = checkNotNull(segmentCache);
3839
_evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer);
39-
_fallbackTreatmentsConfiguration = fallbackTreatmentsConfiguration;
40+
_fallbackTreatmentCalculator = fallbackTreatmentCalculator;
4041
}
4142

4243
@Override
@@ -179,16 +180,20 @@ private String getConfig(ParsedSplit parsedSplit, String returnedTreatment) {
179180
private TreatmentLabelAndChangeNumber evaluateParsedSplit(String matchingKey, String bucketingKey, Map<String, Object> attributes,
180181
ParsedSplit parsedSplit, String featureName) {
181182
try {
183+
182184
if (parsedSplit == null) {
183-
return checkFallbackTreatments(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND, featureName, null, _fallbackTreatmentsConfiguration);
185+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.DEFINITION_NOT_FOUND);
186+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
184187
}
185188
return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes);
186189
} catch (ChangeNumberExceptionWrapper e) {
187190
_log.error("Evaluator Exception", e.wrappedException());
188-
return checkFallbackTreatments(Treatments.CONTROL, Labels.EXCEPTION, featureName, e.changeNumber(), _fallbackTreatmentsConfiguration);
191+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
192+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel(), e.changeNumber());
189193
} catch (Exception e) {
190194
_log.error("Evaluator Exception", e);
191-
return checkFallbackTreatments(Treatments.CONTROL, Labels.EXCEPTION, featureName, null, _fallbackTreatmentsConfiguration);
195+
FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION);
196+
return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel());
192197
}
193198
}
194199

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 org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.hamcrest.core.Is.is;
10+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
11+
import static org.hamcrest.core.IsNull.notNullValue;
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class FallbackTreatmentCalculationImpTest {
16+
17+
@Test
18+
public void TestWorks() {
19+
FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"), null);
20+
FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
21+
assertEquals("on", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment());
22+
assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel());
23+
24+
fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"),
25+
new HashMap<String, FallbackTreatment>() {{ put("flag", new FallbackTreatment("off")); }} );
26+
fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
27+
assertEquals("on", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment());
28+
assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel());
29+
assertEquals("off", fallbackTreatmentCalculator.resolve("flag", "exception").getTreatment());
30+
assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("flag", "exception").getLabel());
31+
32+
fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(null,
33+
new HashMap<String, FallbackTreatment>() {{ put("flag", new FallbackTreatment("off")); }} );
34+
fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
35+
assertEquals("control", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment());
36+
assertEquals("exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel());
37+
assertEquals("off", fallbackTreatmentCalculator.resolve("flag", "exception").getTreatment());
38+
assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("flag", "exception").getLabel());
39+
}
40+
}

client/src/test/java/io/split/engine/evaluator/EvaluatorTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public void evaluateWithPrerequisites() {
229229
public void evaluateFallbackTreatmentWorks() {
230230
Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null);
231231
FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"), null);
232-
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentsConfiguration);
232+
FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
233+
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator);
233234

234235
EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null);
235236
assertEquals("on", result.treatment);
@@ -245,7 +246,8 @@ public void evaluateFallbackTreatmentWorks() {
245246
Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null);
246247
Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(null);
247248
fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(null, new HashMap<String, FallbackTreatment>() {{ put(SPLIT_NAME, new FallbackTreatment("off")); }} );
248-
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentsConfiguration);
249+
fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
250+
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator);
249251

250252
result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null);
251253
assertEquals("off", result.treatment);
@@ -271,7 +273,8 @@ public void evaluateFallbackTreatmentWorks() {
271273
Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null);
272274
Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(null);
273275
fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"), new HashMap<String, FallbackTreatment>() {{ put(SPLIT_NAME, new FallbackTreatment("off")); }} );
274-
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentsConfiguration);
276+
fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration);
277+
_evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator);
275278

276279
result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null);
277280
assertEquals("off", result.treatment);

0 commit comments

Comments
 (0)