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
25 changes: 1 addition & 24 deletions client/src/main/java/io/split/client/SplitClientImpl.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.split.client;

import com.google.common.annotations.VisibleForTesting;
import io.split.client.api.Key;
import io.split.client.api.SplitResult;
import io.split.client.dtos.Event;
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
import io.split.client.impressions.Impression;
import io.split.client.impressions.ImpressionsManager;
import io.split.engine.evaluator.Evaluator;
Expand Down Expand Up @@ -34,7 +32,6 @@ public final class SplitClientImpl implements SplitClient {
public static final SplitResult SPLIT_RESULT_CONTROL = new SplitResult(Treatments.CONTROL, null);

private static final String GET_TREATMENT_LABEL = "sdk.getTreatment";
private static final String EXCEPTION = "exception";

private static final Logger _log = LoggerFactory.getLogger(SplitClientImpl.class);

Expand Down Expand Up @@ -315,7 +312,7 @@ private SplitResult getTreatmentWithConfigInternal(String label, String matching

long start = System.currentTimeMillis();

EvaluatorImp.TreatmentLabelAndChangeNumber result = getTreatmentResultWithoutImpressions(matchingKey, bucketingKey, split, attributes);
EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(matchingKey, bucketingKey, split, attributes);

recordStats(
matchingKey,
Expand Down Expand Up @@ -350,21 +347,6 @@ private void recordStats(String matchingKey, String bucketingKey, String split,
}
}

private EvaluatorImp.TreatmentLabelAndChangeNumber getTreatmentResultWithoutImpressions(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes) {
EvaluatorImp.TreatmentLabelAndChangeNumber result;
try {
result = _evaluator.evaluateFeature(matchingKey, bucketingKey, split, attributes, this);
} catch (ChangeNumberExceptionWrapper e) {
result = new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, EXCEPTION, e.changeNumber());
_log.error("Exception", e.wrappedException());
} catch (Exception e) {
result = new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, EXCEPTION);
_log.error("Exception", e);
}

return result;
}

private Event createEvent(String key, String trafficType, String eventType) {
Event event = new Event();
event.eventTypeId = eventType;
Expand All @@ -373,9 +355,4 @@ private Event createEvent(String key, String trafficType, String eventType) {
event.timestamp = System.currentTimeMillis();
return event;
}

@VisibleForTesting
public String getTreatmentWithoutImpressions(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes) {
return getTreatmentResultWithoutImpressions(matchingKey, bucketingKey, split, attributes).treatment;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.split.engine.evaluator;

import io.split.client.SplitClientImpl;
import io.split.client.exceptions.ChangeNumberExceptionWrapper;

import java.util.Map;

public interface Evaluator {
EvaluatorImp.TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes, SplitClientImpl splitClient) throws ChangeNumberExceptionWrapper;
EvaluatorImp.TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes);
}
37 changes: 23 additions & 14 deletions client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.split.engine.evaluator;

import io.split.client.SplitClientImpl;
import io.split.client.dtos.ConditionType;
import io.split.client.exceptions.ChangeNumberExceptionWrapper;
import io.split.engine.SDKReadinessGates;
Expand All @@ -21,6 +20,7 @@ public class EvaluatorImp implements Evaluator {
private static final String DEFAULT_RULE = "default rule";
private static final String KILLED = "killed";
private static final String DEFINITION_NOT_FOUND = "definition not found";
private static final String EXCEPTION = "exception";

private static final Logger _log = LoggerFactory.getLogger(EvaluatorImp.class);

Expand All @@ -34,19 +34,28 @@ public EvaluatorImp(SDKReadinessGates gates,
}

@Override
public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes, SplitClientImpl splitClient) throws ChangeNumberExceptionWrapper {
ParsedSplit parsedSplit = _splitFetcher.fetch(split);

if (parsedSplit == null) {
if (_gates.isSDKReadyNow()) {
_log.warn(
"getTreatment: you passed \"" + split + "\" that does not exist in this environment, " +
"please double check what Splits exist in the web console.");
public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map<String, Object> attributes) {
try {
ParsedSplit parsedSplit = _splitFetcher.fetch(split);

if (parsedSplit == null) {
if (_gates.isSDKReadyNow()) {
_log.warn(
"getTreatment: you passed \"" + split + "\" that does not exist in this environment, " +
"please double check what Splits exist in the web console.");
}
return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, DEFINITION_NOT_FOUND);
}
return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, DEFINITION_NOT_FOUND);
}

return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes, splitClient);
return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes);
}
catch (ChangeNumberExceptionWrapper e) {
_log.error("Evaluator Exception", e.wrappedException());
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, EXCEPTION, e.changeNumber());
} catch (Exception e) {
_log.error("Evaluator Exception", e);
return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, EXCEPTION);
}
}

/**
Expand All @@ -57,7 +66,7 @@ public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String
* @return
* @throws ChangeNumberExceptionWrapper
*/
private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bucketingKey, ParsedSplit parsedSplit, Map<String, Object> attributes, SplitClientImpl splitClient) throws ChangeNumberExceptionWrapper {
private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bucketingKey, ParsedSplit parsedSplit, Map<String, Object> attributes) throws ChangeNumberExceptionWrapper {
try {
if (parsedSplit.killed()) {
String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(parsedSplit.defaultTreatment()) : null;
Expand Down Expand Up @@ -92,7 +101,7 @@ private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bu
inRollout = true;
}

if (parsedCondition.matcher().match(matchingKey, bucketingKey, attributes, splitClient)) {
if (parsedCondition.matcher().match(matchingKey, bucketingKey, attributes, this)) {
String treatment = Splitter.getTreatment(bk, parsedSplit.seed(), parsedCondition.partitions(), parsedSplit.algo());
String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(treatment) : null;
return new TreatmentLabelAndChangeNumber(treatment, parsedCondition.label(), parsedSplit.changeNumber(), config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand All @@ -12,7 +12,7 @@
public final class AllKeysMatcher implements Matcher {

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
if (matchValue == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;
import java.util.Objects;
Expand All @@ -27,9 +27,9 @@ public AttributeMatcher(String attribute, Matcher matcher, boolean negate) {
_matcher = new NegatableMatcher(matcher, negate);
}

public boolean match(String key, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(String key, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
if (_attribute == null) {
return _matcher.match(key, bucketingKey, attributes, splitClient);
return _matcher.match(key, bucketingKey, attributes, evaluator);
}

if (attributes == null) {
Expand Down Expand Up @@ -95,8 +95,8 @@ public NegatableMatcher(Matcher matcher, boolean negate) {


@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
boolean result = _delegate.match(matchValue, bucketingKey, attributes, splitClient);
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
boolean result = _delegate.match(matchValue, bucketingKey, attributes, evaluator);
return (_negate) ? !result : result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.client.dtos.DataType;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand Down Expand Up @@ -36,7 +36,7 @@ public BetweenMatcher(long start, long end, DataType dataType) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
Long keyAsLong;

if (_dataType == DataType.DATETIME) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand All @@ -14,7 +14,7 @@ public BooleanMatcher(boolean booleanValue) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
if (matchValue == null) {
return false;
}
Expand All @@ -26,7 +26,7 @@ public boolean match(Object matchValue, String bucketingKey, Map<String, Object>

@Override
public String toString() {
return "is " + Boolean.toString(_booleanValue);
return "is " + _booleanValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.Lists;
import io.split.client.SplitClientImpl;
import io.split.client.dtos.MatcherCombiner;
import io.split.engine.evaluator.Evaluator;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -38,24 +39,24 @@ public CombiningMatcher(MatcherCombiner combiner, List<AttributeMatcher> delegat
checkArgument(_delegates.size() > 0);
}

public boolean match(String key, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(String key, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
if (_delegates.isEmpty()) {
return false;
}

switch (_combiner) {
case AND:
return and(key, bucketingKey, attributes, splitClient);
return and(key, bucketingKey, attributes, evaluator);
default:
throw new IllegalArgumentException("Unknown combiner: " + _combiner);
}

}

private boolean and(String key, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
private boolean and(String key, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
boolean result = true;
for (AttributeMatcher delegate : _delegates) {
result &= (delegate.match(key, bucketingKey, attributes, splitClient));
result &= (delegate.match(key, bucketingKey, attributes, evaluator));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.engine.evaluator.Evaluator;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Supports the logic: if user is in split "feature" treatments ["on","off"]
Expand All @@ -18,7 +19,7 @@ public DependencyMatcher(String split, List<String> treatments) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
if (matchValue == null) {
return false;
}
Expand All @@ -27,16 +28,7 @@ public boolean match(Object matchValue, String bucketingKey, Map<String, Object>
return false;
}

String result = splitClient.getTreatmentWithoutImpressions(
(String) matchValue,
bucketingKey,
_split,
attributes
);

// if(Treatments.isControl(result)) {
// throw new ParentIsControlException();
// }
String result = evaluator.evaluateFeature((String) matchValue, bucketingKey, _split, attributes).treatment;

return _treatments.contains(result);
}
Expand All @@ -58,8 +50,8 @@ public boolean equals(Object o) {

DependencyMatcher that = (DependencyMatcher) o;

if (_split != null ? !_split.equals(that._split) : that._split != null) return false;
return _treatments != null ? _treatments.equals(that._treatments) : that._treatments == null;
if (!Objects.equals(_split, that._split)) return false;
return Objects.equals(_treatments, that._treatments);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.client.dtos.DataType;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand Down Expand Up @@ -29,7 +29,7 @@ public EqualToMatcher(long compareTo, DataType dataType) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
Long keyAsLong;

if (_dataType == DataType.DATETIME) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.client.dtos.DataType;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand Down Expand Up @@ -29,7 +29,7 @@ public GreaterThanOrEqualToMatcher(long compareTo, DataType dataType) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
Long keyAsLong;

if (_dataType == DataType.DATETIME) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.client.dtos.DataType;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

Expand All @@ -28,7 +28,7 @@ public LessThanOrEqualToMatcher(long compareTo, DataType dataType) {
}

@Override
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient) {
public boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator) {
Long keyAsLong;

if (_dataType == DataType.DATETIME) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/io/split/engine/matchers/Matcher.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.split.engine.matchers;

import io.split.client.SplitClientImpl;
import io.split.engine.evaluator.Evaluator;

import java.util.Map;

public interface Matcher {
boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, SplitClientImpl splitClient);
boolean match(Object matchValue, String bucketingKey, Map<String, Object> attributes, Evaluator evaluator);
}
Loading