Skip to content

Commit c6727a3

Browse files
committed
polish
1 parent 459ff37 commit c6727a3

20 files changed

+216
-213
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
8181
String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode())
8282
);
8383
}
84-
return GenericClientUtil.ExtractFeatureFlagsAndRuleBasedSegments(response.body());
84+
return Json.fromJson(response.body(), SplitChange.class);
8585
} catch (Exception e) {
8686
throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e);
8787
} finally {

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.split.client;
22

33
import com.google.gson.stream.JsonReader;
4+
import io.split.client.dtos.ChangeDto;
45
import io.split.client.dtos.SplitChange;
56
import io.split.client.utils.InputStreamProvider;
67
import io.split.client.utils.Json;
78
import io.split.client.utils.LocalhostSanitizer;
89
import io.split.engine.common.FetchOptions;
910
import io.split.engine.experiments.SplitChangeFetcher;
10-
import org.checkerframework.checker.units.qual.A;
11+
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

@@ -35,9 +36,10 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
3536
try {
3637
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8)));
3738
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
38-
splitChange.ruleBasedSegments = new ArrayList<>();
39-
splitChange.tillRBS = -1;
40-
splitChange.sinceRBS = -1;
39+
splitChange.ruleBasedSegments = new ChangeDto<>();
40+
splitChange.ruleBasedSegments.d = new ArrayList<>();
41+
splitChange.ruleBasedSegments.t = -1;
42+
splitChange.ruleBasedSegments.s = -1;
4143
return processSplitChange(splitChange, since);
4244
} catch (Exception e) {
4345
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
@@ -47,22 +49,22 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
4749
private SplitChange processSplitChange(SplitChange splitChange, long changeNumber) throws NoSuchAlgorithmException {
4850
SplitChange splitChangeToProcess = LocalhostSanitizer.sanitization(splitChange);
4951
// if the till is less than storage CN and different from the default till ignore the change
50-
if (splitChangeToProcess.till < changeNumber && splitChangeToProcess.till != -1) {
52+
if (splitChangeToProcess.featureFlags.t < changeNumber && splitChangeToProcess.featureFlags.t != -1) {
5153
_log.warn("The till is lower than the change number or different to -1");
5254
return null;
5355
}
54-
String splitJson = splitChange.splits.toString();
56+
String splitJson = splitChange.featureFlags.d.toString();
5557
MessageDigest digest = MessageDigest.getInstance("SHA-1");
5658
digest.reset();
5759
digest.update(splitJson.getBytes());
5860
// calculate the json sha
5961
byte [] currHash = digest.digest();
6062
//if sha exist and is equal to before sha, or if till is equal to default till returns the same segmentChange with till equals to storage CN
61-
if (Arrays.equals(lastHash, currHash) || splitChangeToProcess.till == -1) {
62-
splitChangeToProcess.till = changeNumber;
63+
if (Arrays.equals(lastHash, currHash) || splitChangeToProcess.featureFlags.t == -1) {
64+
splitChangeToProcess.featureFlags.t = changeNumber;
6365
}
6466
lastHash = currHash;
65-
splitChangeToProcess.since = changeNumber;
67+
splitChangeToProcess.featureFlags.s = changeNumber;
6668
return splitChangeToProcess;
6769
}
6870
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package io.split.client;
22

3-
import io.split.client.dtos.Condition;
4-
import io.split.client.dtos.ConditionType;
5-
import io.split.client.dtos.Split;
6-
import io.split.client.dtos.SplitChange;
7-
import io.split.client.dtos.Status;
3+
import io.split.client.dtos.*;
84
import io.split.client.utils.LocalhostConstants;
95
import io.split.client.utils.LocalhostSanitizer;
106
import io.split.engine.common.FetchOptions;
@@ -38,7 +34,8 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
3834

3935
try (BufferedReader reader = new BufferedReader(new FileReader(_splitFile))) {
4036
SplitChange splitChange = new SplitChange();
41-
splitChange.splits = new ArrayList<>();
37+
splitChange.featureFlags = new ChangeDto<>();
38+
splitChange.featureFlags.d = new ArrayList<>();
4239
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
4340
String lineTrim = line.trim();
4441
if (lineTrim.isEmpty() || lineTrim.startsWith("#")) {
@@ -51,15 +48,16 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
5148
_log.info("Ignoring line since it does not have 2 or 3 columns: " + lineTrim);
5249
continue;
5350
}
54-
Optional<Split> splitOptional = splitChange.splits.stream().filter(split -> split.name.equals(featureTreatment[0])).findFirst();
51+
Optional<Split> splitOptional = splitChange.featureFlags.d.stream().
52+
filter(split -> split.name.equals(featureTreatment[0])).findFirst();
5553
Split split = splitOptional.orElse(null);
5654
if(split == null) {
5755
split = new Split();
5856
split.name = featureTreatment[0];
5957
split.configurations = new HashMap<>();
6058
split.conditions = new ArrayList<>();
6159
} else {
62-
splitChange.splits.remove(split);
60+
splitChange.featureFlags.d.remove(split);
6361
}
6462
split.status = Status.ACTIVE;
6563
split.defaultTreatment = featureTreatment[1];
@@ -78,13 +76,14 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
7876
} else {
7977
split.conditions.add(condition);
8078
}
81-
splitChange.splits.add(split);
79+
splitChange.featureFlags.d.add(split);
8280
}
83-
splitChange.till = since;
84-
splitChange.since = since;
85-
splitChange.sinceRBS = -1;
86-
splitChange.tillRBS = -1;
87-
splitChange.ruleBasedSegments = new ArrayList<>();
81+
splitChange.featureFlags.t = since;
82+
splitChange.featureFlags.s = since;
83+
splitChange.ruleBasedSegments = new ChangeDto<>();
84+
splitChange.ruleBasedSegments.s = -1;
85+
splitChange.ruleBasedSegments.t = -1;
86+
splitChange.ruleBasedSegments.d = new ArrayList<>();
8887
return splitChange;
8988
} catch (FileNotFoundException f) {
9089
_log.warn("There was no file named " + _splitFile.getPath() + " found. " +

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package io.split.client;
22

3-
import io.split.client.dtos.Condition;
4-
import io.split.client.dtos.ConditionType;
5-
import io.split.client.dtos.Split;
6-
import io.split.client.dtos.SplitChange;
7-
import io.split.client.dtos.Status;
3+
import io.split.client.dtos.*;
84
import io.split.client.utils.InputStreamProvider;
95
import io.split.client.utils.LocalhostConstants;
106
import io.split.engine.common.FetchOptions;
@@ -37,20 +33,22 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
3733
Yaml yaml = new Yaml();
3834
List<Map<String, Map<String, Object>>> yamlSplits = yaml.load(_inputStreamProvider.get());
3935
SplitChange splitChange = new SplitChange();
40-
splitChange.splits = new ArrayList<>();
36+
splitChange.featureFlags = new ChangeDto<>();
37+
splitChange.featureFlags.d = new ArrayList<>();
4138
for(Map<String, Map<String, Object>> aSplit : yamlSplits) {
4239
// The outter map is a map with one key, the split name
4340
Map.Entry<String, Map<String, Object>> splitAndValues = aSplit.entrySet().iterator().next();
4441

45-
Optional<Split> splitOptional = splitChange.splits.stream().filter(split -> split.name.equals(splitAndValues.getKey())).findFirst();
42+
Optional<Split> splitOptional = splitChange.featureFlags.d.stream().
43+
filter(split -> split.name.equals(splitAndValues.getKey())).findFirst();
4644
Split split = splitOptional.orElse(null);
4745
if(split == null) {
4846
split = new Split();
4947
split.name = splitAndValues.getKey();
5048
split.configurations = new HashMap<>();
5149
split.conditions = new ArrayList<>();
5250
} else {
53-
splitChange.splits.remove(split);
51+
splitChange.featureFlags.d.remove(split);
5452
}
5553
String treatment = (String) splitAndValues.getValue().get("treatment");
5654
String configurations = splitAndValues.getValue().get("config") != null ? (String) splitAndValues.getValue().get("config") : null;
@@ -68,14 +66,14 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
6866
split.trafficTypeName = LocalhostConstants.USER;
6967
split.trafficAllocation = LocalhostConstants.SIZE_100;
7068
split.trafficAllocationSeed = LocalhostConstants.SIZE_1;
71-
72-
splitChange.splits.add(split);
69+
splitChange.featureFlags.d.add(split);
7370
}
74-
splitChange.till = since;
75-
splitChange.since = since;
76-
splitChange.sinceRBS = -1;
77-
splitChange.tillRBS = -1;
78-
splitChange.ruleBasedSegments = new ArrayList<>();
71+
splitChange.featureFlags.t = since;
72+
splitChange.featureFlags.s = since;
73+
splitChange.ruleBasedSegments = new ChangeDto<>();
74+
splitChange.ruleBasedSegments.s = -1;
75+
splitChange.ruleBasedSegments.t = -1;
76+
splitChange.ruleBasedSegments.d = new ArrayList<>();
7977
return splitChange;
8078
} catch (Exception e) {
8179
throw new IllegalStateException("Problem fetching splitChanges using a yaml file: " + e.getMessage(), e);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.split.client.dtos;
2+
3+
import java.util.List;
4+
5+
public class ChangeDto<T> {
6+
public long s;
7+
public long t;
8+
public List<T> d;
9+
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package io.split.client.dtos;
22

3-
import java.util.List;
3+
import com.google.gson.annotations.SerializedName;
44

55
public class SplitChange {
6-
public List<Split> splits;
7-
public long since;
8-
public long till;
9-
public List<RuleBasedSegment> ruleBasedSegments;
10-
public long sinceRBS;
11-
public long tillRBS;
6+
@SerializedName("ff")
7+
public ChangeDto<Split> featureFlags;
8+
@SerializedName("rbs")
9+
public ChangeDto<RuleBasedSegment> ruleBasedSegments;
1210
}

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,5 @@ public static<T> void process(List<T> data, URI endpoint, CloseableHttpClient cl
4646
}
4747

4848
}
49-
50-
public static SplitChange ExtractFeatureFlagsAndRuleBasedSegments(String responseBody) {
51-
JsonObject jsonBody = Json.fromJson(responseBody, JsonObject.class);
52-
JsonObject featureFlags = jsonBody.getAsJsonObject("ff");
53-
JsonObject ruleBasedSegments = jsonBody.getAsJsonObject("rbs");
54-
SplitChange splitChange = new SplitChange();
55-
splitChange.till = Long.parseLong(featureFlags.get("t").toString());
56-
splitChange.since = Long.parseLong(featureFlags.get("s").toString());
57-
splitChange.tillRBS = Long.parseLong(ruleBasedSegments.get("t").toString());
58-
splitChange.sinceRBS = Long.parseLong(ruleBasedSegments.get("s").toString());
59-
60-
splitChange.splits = new ArrayList<>();
61-
for (JsonElement split: featureFlags.get("d").getAsJsonArray()) {
62-
splitChange.splits.add(Json.fromJson(split.toString(), Split.class));
63-
}
64-
splitChange.ruleBasedSegments = new ArrayList<>();
65-
for (JsonElement rbs: ruleBasedSegments.get("d").getAsJsonArray()) {
66-
splitChange.ruleBasedSegments.add(Json.fromJson(rbs.toString(), RuleBasedSegment.class));
67-
}
68-
return splitChange;
69-
}
7049
}
50+

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ private LocalhostSanitizer() {
3030
public static SplitChange sanitization(SplitChange splitChange) {
3131
SecureRandom random = new SecureRandom();
3232
List<Split> splitsToRemove = new ArrayList<>();
33-
if (splitChange.till < LocalhostConstants.DEFAULT_TS || splitChange.till == 0) {
34-
splitChange.till = LocalhostConstants.DEFAULT_TS;
33+
if (splitChange.featureFlags.t < LocalhostConstants.DEFAULT_TS || splitChange.featureFlags.t == 0) {
34+
splitChange.featureFlags.t = LocalhostConstants.DEFAULT_TS;
3535
}
36-
if (splitChange.since < LocalhostConstants.DEFAULT_TS || splitChange.since > splitChange.till) {
37-
splitChange.since = splitChange.till;
36+
if (splitChange.featureFlags.s < LocalhostConstants.DEFAULT_TS || splitChange.featureFlags.s > splitChange.featureFlags.t) {
37+
splitChange.featureFlags.s = splitChange.featureFlags.t;
3838
}
39-
if (splitChange.splits != null) {
40-
for (Split split: splitChange.splits) {
39+
if (splitChange.featureFlags.d != null) {
40+
for (Split split: splitChange.featureFlags.d) {
4141
if (split.name == null){
4242
splitsToRemove.add(split);
4343
continue;
@@ -83,10 +83,10 @@ public static SplitChange sanitization(SplitChange splitChange) {
8383
split.conditions.add(createRolloutCondition(rolloutCondition, split.trafficTypeName, null));
8484
}
8585
}
86-
splitChange.splits.removeAll(splitsToRemove);
86+
splitChange.featureFlags.d.removeAll(splitsToRemove);
8787
return splitChange;
8888
}
89-
splitChange.splits = new ArrayList<>();
89+
splitChange.featureFlags.d = new ArrayList<>();
9090
return splitChange;
9191
}
9292
public static SegmentChange sanitization(SegmentChange segmentChange) {

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ private Set<String> runWithoutExceptionHandling(FetchOptions options) throws Int
124124
return segments;
125125
}
126126

127-
if (change.splits.isEmpty() || change.ruleBasedSegments.isEmpty()) {
128-
if (change.splits.isEmpty()) _splitCacheProducer.setChangeNumber(change.till);
129-
if (change.ruleBasedSegments.isEmpty())
130-
_ruleBasedSegmentCacheProducer.setChangeNumber(change.tillRBS);
131-
if (change.splits.isEmpty() && change.ruleBasedSegments.isEmpty()) return segments;
127+
if (change.featureFlags.d.isEmpty() || change.ruleBasedSegments.d.isEmpty()) {
128+
if (change.featureFlags.d.isEmpty()) _splitCacheProducer.setChangeNumber(change.featureFlags.t);
129+
if (change.ruleBasedSegments.d.isEmpty())
130+
_ruleBasedSegmentCacheProducer.setChangeNumber(change.ruleBasedSegments.t);
131+
if (change.featureFlags.d.isEmpty() && change.ruleBasedSegments.d.isEmpty()) return segments;
132132
}
133133

134134
synchronized (_lock) {
@@ -137,27 +137,29 @@ private Set<String> runWithoutExceptionHandling(FetchOptions options) throws Int
137137
// some other thread may have updated the shared state. exit
138138
return segments;
139139
}
140-
FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(_parser, change.splits, _flagSetsFilter);
140+
FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(_parser, change.featureFlags.d, _flagSetsFilter);
141141
segments = featureFlagsToUpdate.getSegments();
142-
_splitCacheProducer.update(featureFlagsToUpdate.getToAdd(), featureFlagsToUpdate.getToRemove(), change.till);
142+
_splitCacheProducer.update(featureFlagsToUpdate.getToAdd(), featureFlagsToUpdate.getToRemove(), change.featureFlags.t);
143143

144-
RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(_parserRBS, change.ruleBasedSegments);
144+
RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(_parserRBS,
145+
change.ruleBasedSegments.d);
145146
segments.addAll(ruleBasedSegmentsToUpdate.getSegments());
146-
_ruleBasedSegmentCacheProducer.update(ruleBasedSegmentsToUpdate.getToAdd(), ruleBasedSegmentsToUpdate.getToRemove(), change.tillRBS);
147+
_ruleBasedSegmentCacheProducer.update(ruleBasedSegmentsToUpdate.getToAdd(),
148+
ruleBasedSegmentsToUpdate.getToRemove(), change.ruleBasedSegments.t);
147149
_telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.SPLITS, System.currentTimeMillis());
148150
}
149151
return segments;
150152
}
151153

152154
private boolean checkExitConditions(SplitChange change) {
153-
return ((change.since != _splitCacheProducer.getChangeNumber() || change.till < _splitCacheProducer.getChangeNumber())
154-
|| (change.sinceRBS != _ruleBasedSegmentCacheProducer.getChangeNumber() ||
155-
change.tillRBS < _ruleBasedSegmentCacheProducer.getChangeNumber()));
155+
return ((change.featureFlags.s != _splitCacheProducer.getChangeNumber() || change.featureFlags.t < _splitCacheProducer.getChangeNumber())
156+
|| (change.ruleBasedSegments.s != _ruleBasedSegmentCacheProducer.getChangeNumber() ||
157+
change.ruleBasedSegments.t < _ruleBasedSegmentCacheProducer.getChangeNumber()));
156158
}
157159

158160
private boolean checkReturnConditions(SplitChange change) {
159-
return ((change.since != _splitCacheProducer.getChangeNumber() || change.till < _splitCacheProducer.getChangeNumber()) &&
160-
(change.sinceRBS != _ruleBasedSegmentCacheProducer.getChangeNumber() ||
161-
change.tillRBS < _ruleBasedSegmentCacheProducer.getChangeNumber()));
161+
return ((change.featureFlags.s != _splitCacheProducer.getChangeNumber() || change.featureFlags.t < _splitCacheProducer.getChangeNumber()) &&
162+
(change.ruleBasedSegments.s != _ruleBasedSegmentCacheProducer.getChangeNumber() ||
163+
change.ruleBasedSegments.t < _ruleBasedSegmentCacheProducer.getChangeNumber()));
162164
}
163165
}

client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ public void testFetcherWithSpecialCharacters() throws URISyntaxException, Invoca
9898
SplitChange change = fetcher.fetch(1234567, -1, new FetchOptions.Builder().cacheControlHeaders(true).build());
9999

100100
Assert.assertNotNull(change);
101-
Assert.assertEquals(1, change.splits.size());
102-
Assert.assertNotNull(change.splits.get(0));
101+
Assert.assertEquals(1, change.featureFlags.d.size());
102+
Assert.assertNotNull(change.featureFlags.d.get(0));
103103

104-
Split split = change.splits.get(0);
104+
Split split = change.featureFlags.d.get(0);
105105
Map<String, String> configs = split.configurations;
106106
Assert.assertEquals(2, configs.size());
107107
Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on"));

0 commit comments

Comments
 (0)