Skip to content

Commit 8d67468

Browse files
committed
polishing
1 parent affc36a commit 8d67468

20 files changed

+102
-84
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ private Spec() {
66
// restrict instantiation
77
}
88

9-
// TODO: Change the schema to 1.3 when updating splitclient
109
public static final String SPEC_1_3 = "1.3";
1110
public static final String SPEC_1_1 = "1.1";
1211
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
public final class HttpSplitChangeFetcher implements SplitChangeFetcher {
3434
private static final Logger _log = LoggerFactory.getLogger(HttpSplitChangeFetcher.class);
3535

36-
private final Object _lock = new Object();
3736
private static final String SINCE = "since";
3837
private static final String RB_SINCE = "rbSince";
3938
private static final String TILL = "till";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ private SplitChange processSplitChange(SplitChange splitChange, long changeNumbe
8080
return splitChangeToProcess;
8181
}
8282

83-
private byte[] getStringDigest(String Json) throws NoSuchAlgorithmException {
83+
private byte[] getStringDigest(String json) throws NoSuchAlgorithmException {
8484
MessageDigest digest = MessageDigest.getInstance("SHA-1");
8585
digest.reset();
86-
digest.update(Json.getBytes());
86+
digest.update(json.getBytes());
8787
// calculate the json sha
8888
return digest.digest();
8989
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.FileReader;
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
22+
import java.util.List;
2223
import java.util.Optional;
2324

2425
public class LegacyLocalhostSplitChangeFetcher implements SplitChangeFetcher {
@@ -70,12 +71,7 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
7071
split.trafficAllocation = LocalhostConstants.SIZE_100;
7172
split.trafficAllocationSeed = LocalhostConstants.SIZE_1;
7273

73-
Condition condition;
74-
if (featureTreatment.length == 2) {
75-
condition = LocalhostSanitizer.createCondition(null, featureTreatment[1]);
76-
} else {
77-
condition = LocalhostSanitizer.createCondition(featureTreatment[2], featureTreatment[1]);
78-
}
74+
Condition condition = checkCondition(featureTreatment);
7975
if(condition.conditionType != ConditionType.ROLLOUT){
8076
split.conditions.add(0, condition);
8177
} else {
@@ -103,4 +99,14 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
10399
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
104100
}
105101
}
102+
103+
private Condition checkCondition(String[] featureTreatment) {
104+
Condition condition;
105+
if (featureTreatment.length == 2) {
106+
condition = LocalhostSanitizer.createCondition(null, featureTreatment[1]);
107+
} else {
108+
condition = LocalhostSanitizer.createCondition(featureTreatment[2], featureTreatment[1]);
109+
}
110+
return condition;
111+
}
106112
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ protected SplitFactoryImpl(SplitClientConfig config) {
403403
SegmentCache segmentCache = new SegmentCacheInMemoryImpl();
404404
FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(config.getSetsFilter());
405405
SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter);
406-
RuleBasedSegmentCache _ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
406+
RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
407407
_splitCache = splitCache;
408408
_gates = new SDKReadinessGates();
409409
_segmentCache = segmentCache;
@@ -422,15 +422,15 @@ protected SplitFactoryImpl(SplitClientConfig config) {
422422
_telemetryStorageProducer,
423423
_splitCache,
424424
config.getThreadFactory(),
425-
_ruleBasedSegmentCache);
425+
ruleBasedSegmentCache);
426426

427427
// SplitFetcher
428428
SplitChangeFetcher splitChangeFetcher = createSplitChangeFetcher(config);
429429
SplitParser splitParser = new SplitParser();
430430
RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser();
431431

432432
_splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer,
433-
flagSetsFilter, ruleBasedSegmentParser, _ruleBasedSegmentCache);
433+
flagSetsFilter, ruleBasedSegmentParser, ruleBasedSegmentCache);
434434

435435
// SplitSynchronizationTask
436436
_splitSynchronizationTask = new SplitSynchronizationTask(_splitFetcher, splitCache,
@@ -442,7 +442,7 @@ protected SplitFactoryImpl(SplitClientConfig config) {
442442
_impressionsManager, null, null, null);
443443

444444
// Evaluator
445-
_evaluator = new EvaluatorImp(splitCache, segmentCache, _ruleBasedSegmentCache);
445+
_evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache);
446446

447447
EventsStorage eventsStorage = new NoopEventsStorageImp();
448448

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.split.client.dtos;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54

65
public class ChangeDto<T> {

client/src/main/java/io/split/client/utils/LocalhostSanitizer.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,41 @@ private LocalhostSanitizer() {
3030
}
3131

3232
public static SplitChange sanitization(SplitChange splitChange) {
33-
SecureRandom random = new SecureRandom();
34-
List<Split> splitsToRemove = new ArrayList<>();
35-
List<RuleBasedSegment> ruleBasedSegmentsToRemove = new ArrayList<>();
3633
splitChange = sanitizeTillAndSince(splitChange);
34+
splitChange.featureFlags.d = sanitizeFeatureFlags(splitChange.featureFlags.d);
35+
splitChange.ruleBasedSegments.d = sanitizeRuleBasedSegments(splitChange.ruleBasedSegments.d);
36+
37+
return splitChange;
38+
}
39+
40+
private static List<RuleBasedSegment> sanitizeRuleBasedSegments(List<RuleBasedSegment> ruleBasedSegments) {
41+
List<RuleBasedSegment> ruleBasedSegmentsToRemove = new ArrayList<>();
42+
if (ruleBasedSegments != null) {
43+
for (RuleBasedSegment ruleBasedSegment : ruleBasedSegments) {
44+
if (ruleBasedSegment.name == null) {
45+
ruleBasedSegmentsToRemove.add(ruleBasedSegment);
46+
continue;
47+
}
48+
ruleBasedSegment.trafficTypeName = sanitizeIfNullOrEmpty(ruleBasedSegment.trafficTypeName, LocalhostConstants.USER);
49+
ruleBasedSegment.status = sanitizeStatus(ruleBasedSegment.status);
50+
ruleBasedSegment.changeNumber = sanitizeChangeNumber(ruleBasedSegment.changeNumber, 0);
51+
ruleBasedSegment.conditions = sanitizeConditions((ArrayList<Condition>) ruleBasedSegment.conditions, false,
52+
ruleBasedSegment.trafficTypeName);
53+
ruleBasedSegment.excluded.segments = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.segments);
54+
ruleBasedSegment.excluded.keys = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.keys);
55+
}
56+
ruleBasedSegments.removeAll(ruleBasedSegmentsToRemove);
57+
} else {
58+
ruleBasedSegments = new ArrayList<>();
59+
}
60+
return ruleBasedSegments;
61+
}
3762

38-
if (splitChange.featureFlags.d != null) {
39-
for (Split split : splitChange.featureFlags.d) {
63+
private static List<Split> sanitizeFeatureFlags(List<Split> featureFlags) {
64+
List<Split> splitsToRemove = new ArrayList<>();
65+
SecureRandom random = new SecureRandom();
66+
if (featureFlags != null) {
67+
for (Split split : featureFlags) {
4068
if (split.name == null) {
4169
splitsToRemove.add(split);
4270
continue;
@@ -60,31 +88,11 @@ public static SplitChange sanitization(SplitChange splitChange) {
6088
}
6189
split.conditions = sanitizeConditions((ArrayList<Condition>) split.conditions, false, split.trafficTypeName);
6290
}
63-
splitChange.featureFlags.d.removeAll(splitsToRemove);
91+
featureFlags.removeAll(splitsToRemove);
6492
} else {
65-
splitChange.featureFlags.d = new ArrayList<>();
93+
featureFlags = new ArrayList<>();
6694
}
67-
68-
if (splitChange.ruleBasedSegments.d != null) {
69-
for (RuleBasedSegment ruleBasedSegment : splitChange.ruleBasedSegments.d) {
70-
if (ruleBasedSegment.name == null) {
71-
ruleBasedSegmentsToRemove.add(ruleBasedSegment);
72-
continue;
73-
}
74-
ruleBasedSegment.trafficTypeName = sanitizeIfNullOrEmpty(ruleBasedSegment.trafficTypeName, LocalhostConstants.USER);
75-
ruleBasedSegment.status = sanitizeStatus(ruleBasedSegment.status);
76-
ruleBasedSegment.changeNumber = sanitizeChangeNumber(ruleBasedSegment.changeNumber, 0);
77-
ruleBasedSegment.conditions = sanitizeConditions((ArrayList<Condition>) ruleBasedSegment.conditions, false,
78-
ruleBasedSegment.trafficTypeName);
79-
ruleBasedSegment.excluded.segments = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.segments);
80-
ruleBasedSegment.excluded.keys = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.keys);
81-
}
82-
splitChange.ruleBasedSegments.d.removeAll(ruleBasedSegmentsToRemove);
83-
} else {
84-
splitChange.ruleBasedSegments.d = new ArrayList<>();
85-
}
86-
87-
return splitChange;
95+
return featureFlags;
8896
}
8997

9098
private static ArrayList<Condition> sanitizeConditions(ArrayList<Condition> conditions, boolean createPartition, String trafficTypeName) {

client/src/main/java/io/split/client/utils/RuleBasedSegmentProcessor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
public class RuleBasedSegmentProcessor {
1717
private static final Logger _log = LoggerFactory.getLogger(RuleBasedSegmentProcessor.class);
1818

19+
private RuleBasedSegmentProcessor() {
20+
throw new IllegalStateException("Utility class");
21+
}
22+
1923
public static RuleBasedSegmentsToUpdate processRuleBasedSegmentChanges(RuleBasedSegmentParser ruleBasedSegmentParser,
2024
List<RuleBasedSegment> ruleBasedSegments) {
2125
List<ParsedRuleBasedSegment> toAdd = new ArrayList<>();
@@ -31,10 +35,10 @@ public static RuleBasedSegmentsToUpdate processRuleBasedSegmentChanges(RuleBased
3135
ParsedRuleBasedSegment parsedRuleBasedSegment = ruleBasedSegmentParser.parse(ruleBasedSegment);
3236
if (parsedRuleBasedSegment == null) {
3337
_log.debug(String.format("We could not parse the rule based segment definition for: %s", ruleBasedSegment.name));
34-
continue;
38+
} else {
39+
segments.addAll(parsedRuleBasedSegment.getSegmentsNames());
40+
toAdd.add(parsedRuleBasedSegment);
3541
}
36-
segments.addAll(parsedRuleBasedSegment.getSegmentsNames());
37-
toAdd.add(parsedRuleBasedSegment);
3842
}
3943
return new RuleBasedSegmentsToUpdate(toAdd, toRemove, segments);
4044
}

client/src/main/java/io/split/client/utils/RuleBasedSegmentsToUpdate.java

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

33
import io.split.engine.experiments.ParsedRuleBasedSegment;
4-
import io.split.engine.experiments.ParsedSplit;
54

65
import java.util.List;
76
import java.util.Set;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ public class EvaluatorImp implements Evaluator {
2424
private static final Logger _log = LoggerFactory.getLogger(EvaluatorImp.class);
2525

2626
private final SegmentCacheConsumer _segmentCacheConsumer;
27-
private final RuleBasedSegmentCacheConsumer _ruleBasedSegmentCacheConsumer;
2827
private final EvaluationContext _evaluationContext;
2928
private final SplitCacheConsumer _splitCacheConsumer;
3029

3130
public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache,
3231
RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) {
3332
_splitCacheConsumer = checkNotNull(splitCacheConsumer);
3433
_segmentCacheConsumer = checkNotNull(segmentCache);
35-
_ruleBasedSegmentCacheConsumer = checkNotNull(ruleBasedSegmentCacheConsumer);
3634
_evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer);
3735
}
3836

client/src/main/java/io/split/engine/experiments/ParserUtils.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import io.split.engine.matchers.strings.EndsWithAnyOfMatcher;
3333
import io.split.engine.matchers.strings.ContainsAnyOfMatcher;
3434
import io.split.engine.matchers.strings.RegularExpressionMatcher;
35-
import org.slf4j.Logger;
36-
import org.slf4j.LoggerFactory;
3735

3836
import java.util.List;
3937

@@ -42,9 +40,8 @@
4240

4341
public final class ParserUtils {
4442

45-
private static final Logger _log = LoggerFactory.getLogger(ParserUtils.class);
46-
47-
public ParserUtils() {
43+
private ParserUtils() {
44+
throw new IllegalStateException("Utility class");
4845
}
4946

5047
public static boolean checkUnsupportedMatcherExist(List<Matcher> matchers) {
@@ -58,8 +55,7 @@ public static boolean checkUnsupportedMatcherExist(List<Matcher> matchers) {
5855
break;
5956
}
6057
}
61-
if (typeCheck != null) return false;
62-
return true;
58+
return (typeCheck == null);
6359
}
6460

6561
public static ParsedCondition getTemplateCondition() {

client/src/main/java/io/split/engine/sse/dtos/CommonChangeNotification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.IOException;
1111
import java.io.UnsupportedEncodingException;
12+
import java.nio.charset.StandardCharsets;
1213
import java.util.Base64;
1314
import java.util.zip.DataFormatException;
1415

@@ -82,6 +83,6 @@ public String toString() {
8283
}
8384

8485
private void updateDefinition(byte[] decodedBytes) throws UnsupportedEncodingException {
85-
definition = (Y) Json.fromJson(new String(decodedBytes, "UTF-8"), _definitionClass);
86+
definition = (Y) Json.fromJson(new String(decodedBytes, StandardCharsets.UTF_8), _definitionClass);
8687
}
8788
}

client/src/main/java/io/split/engine/sse/workers/FeatureFlagWorkerImp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ protected void executeRefresh(IncomingNotification incomingNotification) {
7171
changeNumber = featureFlagChangeNotification.getChangeNumber();
7272
} else {
7373
CommonChangeNotification<RuleBasedSegment> ruleBasedSegmentChangeNotification = (CommonChangeNotification) incomingNotification;
74-
success = AddOrUpdateRuleBasedSegment(ruleBasedSegmentChangeNotification);
74+
success = addOrUpdateRuleBasedSegment(ruleBasedSegmentChangeNotification);
7575
changeNumberRBS = ruleBasedSegmentChangeNotification.getChangeNumber();
7676
}
7777
if (!success)
7878
_synchronizer.refreshSplits(changeNumber, changeNumberRBS);
7979
}
8080

81-
private boolean AddOrUpdateRuleBasedSegment(CommonChangeNotification ruleBasedSegmentChangeNotification) {
81+
private boolean addOrUpdateRuleBasedSegment(CommonChangeNotification ruleBasedSegmentChangeNotification) {
8282
if (ruleBasedSegmentChangeNotification.getChangeNumber() <= _ruleBasedSegmentCache.getChangeNumber()) {
8383
return true;
8484
}

client/src/main/java/io/split/service/SplitHttpClientImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<Str
9393
_log.warn(String.format("Response status was: %s. Reason: %s", code, statusMessage));
9494
}
9595

96-
String body = "";
97-
try {
98-
body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
99-
} catch (Exception e) {
100-
_log.warn("Error parsing Response.body", e);
101-
}
96+
String body = extractBodyFromResponse(response);
10297

10398
return new SplitHttpResponse(code,
10499
statusMessage,
@@ -113,6 +108,16 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<Str
113108
}
114109
}
115110

111+
private String extractBodyFromResponse(CloseableHttpResponse response) {
112+
String body = "";
113+
try {
114+
body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
115+
} catch (Exception e) {
116+
_log.warn("Error parsing Response.body", e);
117+
}
118+
return body;
119+
}
120+
116121
public SplitHttpResponse post(URI uri, String body, Map<String, List<String>> additionalHeaders)
117122
throws IOException {
118123

client/src/main/java/io/split/storages/RuleBasedSegmentCacheConsumer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.util.Collection;
66
import java.util.List;
7-
import java.util.Map;
87
import java.util.Set;
98

109
public interface RuleBasedSegmentCacheConsumer {

client/src/main/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.concurrent.ConcurrentMap;
1515
import java.util.concurrent.atomic.AtomicLong;
1616
import java.util.stream.Collectors;
17+
import java.util.Map;
1718

1819
public class RuleBasedSegmentCacheInMemoryImp implements RuleBasedSegmentCache {
1920

@@ -65,8 +66,8 @@ public void setChangeNumber(long changeNumber) {
6566
@Override
6667
public List<String> ruleBasedSegmentNames() {
6768
List<String> ruleBasedSegmentNamesList = new ArrayList<>();
68-
for (String key: _concurrentMap.keySet()) {
69-
ruleBasedSegmentNamesList.add(_concurrentMap.get(key).ruleBasedSegment());
69+
for (Map.Entry<String, ParsedRuleBasedSegment> key: _concurrentMap.entrySet()) {
70+
ruleBasedSegmentNamesList.add(key.getValue().ruleBasedSegment());
7071
}
7172
return ruleBasedSegmentNamesList;
7273
}
@@ -103,6 +104,6 @@ public Set<String> getSegments() {
103104

104105
@Override
105106
public boolean contains(Set<String> ruleBasedSegmentNames) {
106-
return getSegments().contains(ruleBasedSegmentNames);
107+
return getSegments().containsAll(ruleBasedSegmentNames);
107108
}
108109
}

client/src/main/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private List<ParsedRuleBasedSegment> stringsToParsedRuleBasedSegments(List<Strin
9696

9797
@Override
9898
public boolean contains(Set<String> ruleBasedSegmentNames) {
99-
return getSegments().contains(ruleBasedSegmentNames);
99+
return getSegments().containsAll(ruleBasedSegmentNames);
100100
}
101101

102102
}

client/src/main/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterProducer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.split.storages.pluggable.adapters;
22

3-
import io.split.client.dtos.RuleBasedSegment;
4-
import io.split.client.utils.Json;
53
import io.split.engine.experiments.ParsedRuleBasedSegment;
64
import io.split.storages.RuleBasedSegmentCacheProducer;
75
import io.split.storages.pluggable.domain.PrefixAdapter;

0 commit comments

Comments
 (0)