Skip to content

Commit fe83f0d

Browse files
committed
polish
1 parent 00402eb commit fe83f0d

File tree

5 files changed

+30
-63
lines changed

5 files changed

+30
-63
lines changed

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

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import io.split.Spec;
66
import io.split.client.dtos.SplitChange;
77
import io.split.client.dtos.SplitHttpResponse;
8-
import io.split.client.dtos.RuleBasedSegment;
98
import io.split.client.dtos.SplitChangesOldPayloadDto;
10-
import io.split.client.dtos.ChangeDto;
11-
import io.split.client.dtos.Split;
129
import io.split.client.exceptions.UriTooLongException;
1310
import io.split.client.utils.Json;
1411
import io.split.client.utils.Utils;
@@ -25,7 +22,6 @@
2522

2623
import java.net.URI;
2724
import java.net.URISyntaxException;
28-
import java.util.ArrayList;
2925

3026
import static com.google.common.base.Preconditions.checkNotNull;
3127
import static io.split.Spec.SPEC_1_3;
@@ -100,41 +96,27 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
10096
String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode())
10197
);
10298
}
99+
100+
SplitChange splitChange = new SplitChange();
101+
if (specVersion.equals(Spec.SPEC_1_1)) {
102+
splitChange = convertBodyToOldSpec(response.body());
103+
} else {
104+
if (_lastProxyCheckTimestamp != 0) {
105+
splitChange.clearCache = true;
106+
_lastProxyCheckTimestamp = 0L;
107+
}
108+
splitChange = Json.fromJson(response.body(), SplitChange.class);
109+
}
110+
return splitChange;
103111
} catch (Exception e) {
104112
throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e);
105113
} finally {
106114
_telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SPLITS, System.currentTimeMillis() - start);
107115
}
108-
109-
SplitChange splitChange = new SplitChange();
110-
if (specVersion.equals(Spec.SPEC_1_1)) {
111-
splitChange.featureFlags = convertBodyToOldSpec(response.body());
112-
splitChange.ruleBasedSegments = createEmptyDTO();
113-
} else {
114-
splitChange = Json.fromJson(response.body(), SplitChange.class);
115-
}
116-
return splitChange;
117116
}
118117

119-
public Long getLastProxyCheckTimestamp() {
120-
return _lastProxyCheckTimestamp;
121-
}
122-
123-
public void setLastProxyCheckTimestamp(long lastProxyCheckTimestamp) {
124-
synchronized (_lock) {
125-
_lastProxyCheckTimestamp = lastProxyCheckTimestamp;
126-
}
127-
}
128-
129-
private ChangeDto<RuleBasedSegment> createEmptyDTO() {
130-
ChangeDto<RuleBasedSegment> dto = new ChangeDto<>();
131-
dto.d = new ArrayList<>();
132-
dto.t = -1;
133-
dto.s = -1;
134-
return dto;
135-
}
136-
private ChangeDto<Split> convertBodyToOldSpec(String body) {
137-
return Json.fromJson(body, SplitChangesOldPayloadDto.class).toSplitChange().featureFlags;
118+
private SplitChange convertBodyToOldSpec(String body) {
119+
return Json.fromJson(body, SplitChangesOldPayloadDto.class).toSplitChange();
138120
}
139121

140122
private URI buildURL(FetchOptions options, long since, long sinceRBS) throws URISyntaxException {

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.JsonObject;
44
import com.google.gson.stream.JsonReader;
5-
import io.split.client.dtos.ChangeDto;
65
import io.split.client.dtos.SplitChange;
76
import io.split.client.dtos.SplitChangesOldPayloadDto;
87
import io.split.client.utils.InputStreamProvider;
@@ -19,9 +18,10 @@
1918
import java.nio.charset.StandardCharsets;
2019
import java.security.MessageDigest;
2120
import java.security.NoSuchAlgorithmException;
22-
import java.util.ArrayList;
2321
import java.util.Arrays;
2422

23+
import static io.split.client.utils.Utils.checkExitConditions;
24+
2525
public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher {
2626

2727
private static final Logger _log = LoggerFactory.getLogger(JsonLocalhostSplitChangeFetcher.class);
@@ -40,10 +40,7 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
4040
try {
4141
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8)));
4242
if (checkOldSpec(new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8))))) {
43-
SplitChange splitChange = new SplitChange();
44-
splitChange.featureFlags = Json.fromJson(jsonReader, SplitChangesOldPayloadDto.class).toSplitChange().featureFlags;
45-
splitChange.ruleBasedSegments = ChangeDto.createEmptyDto();
46-
return splitChange;
43+
return Json.fromJson(jsonReader, SplitChangesOldPayloadDto.class).toSplitChange();
4744
}
4845
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
4946
return processSplitChange(splitChange, since, sinceRBS);
@@ -82,10 +79,6 @@ private SplitChange processSplitChange(SplitChange splitChange, long changeNumbe
8279
return splitChangeToProcess;
8380
}
8481

85-
private <T> boolean checkExitConditions(ChangeDto<T> change, long cn) {
86-
return change.t < cn && change.t != -1;
87-
}
88-
8982
private byte[] getStringDigest(String Json) throws NoSuchAlgorithmException {
9083
MessageDigest digest = MessageDigest.getInstance("SHA-1");
9184
digest.reset();

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

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

3-
import io.split.client.dtos.ChangeDto;
43
import io.split.client.dtos.SplitChange;
54
import io.split.client.exceptions.UriTooLongException;
65
import io.split.client.interceptors.FlagSetsFilter;
@@ -20,6 +19,7 @@
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120
import static io.split.client.utils.FeatureFlagProcessor.processFeatureFlagChanges;
2221
import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges;
22+
import static io.split.client.utils.Utils.checkExitConditions;
2323

2424
/**
2525
* An ExperimentFetcher that refreshes experiment definitions periodically.
@@ -122,6 +122,11 @@ private Set<String> runWithoutExceptionHandling(FetchOptions options) throws Int
122122
throw new IllegalStateException("SplitChange was null");
123123
}
124124

125+
if (change.clearCache) {
126+
_splitCacheProducer.clear();
127+
_ruleBasedSegmentCacheProducer.clear();
128+
}
129+
125130
if (checkExitConditions(change.featureFlags, _splitCacheProducer.getChangeNumber()) ||
126131
checkExitConditions(change.ruleBasedSegments, _ruleBasedSegmentCacheProducer.getChangeNumber())) {
127132
return segments;
@@ -161,8 +166,4 @@ private Set<String> runWithoutExceptionHandling(FetchOptions options) throws Int
161166

162167
return segments;
163168
}
164-
165-
private <T> boolean checkExitConditions(ChangeDto<T> change, long cn) {
166-
return change.s != cn || change.t < cn;
167-
}
168169
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void getTreatmentWithStreamingDisabled() throws Exception {
210210

211211
@Test
212212
public void managerSplitsWithStreamingEnabled() throws Exception {
213-
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}");
213+
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}");
214214
Queue responses = new LinkedList<>();
215215
responses.add(response);
216216
SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder()
@@ -250,9 +250,9 @@ public void managerSplitsWithStreamingEnabled() throws Exception {
250250

251251
@Test
252252
public void splitClientOccupancyNotifications() throws Exception {
253-
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}");
254-
MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850110, \"t\":1585948850110}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}");
255-
MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850111, \"t\":1585948850111}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}");
253+
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}");
254+
MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850110, \"t\":1585948850110}, \"rbs\":{\"d\":[],\"s\":1585948850110,\"t\":1585948850110}}");
255+
MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850111, \"t\":1585948850111}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}");
256256
Queue responses = new LinkedList<>();
257257
responses.add(response);
258258
Queue responses2 = new LinkedList<>();
@@ -420,7 +420,7 @@ public void splitClientControlNotifications() throws Exception {
420420

421421
@Test
422422
public void splitClientMultiFactory() throws Exception {
423-
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}");
423+
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}");
424424
Queue responses = new LinkedList<>();
425425
responses.add(response);
426426
responses.add(response);
@@ -752,8 +752,8 @@ public void testPluggableMode() throws IOException, URISyntaxException {
752752

753753
@Test
754754
public void getTreatmentFlagSetWithPolling() throws Exception {
755-
MockResponse response = new MockResponse().setBody("{\"splits\":[{\"trafficTypeName\":\"client\",\"name\":\"workm\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set1\",\"set2\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"client\",\"name\":\"workm_set_3\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set3\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]}],\"since\":-1,\"till\":1602796638344}");
756-
MockResponse responseFlag = new MockResponse().setBody("{\"splits\": [], \"since\":1602796638344, \"till\":1602796638344}");
755+
MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\":[{\"trafficTypeName\":\"client\",\"name\":\"workm\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set1\",\"set2\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"client\",\"name\":\"workm_set_3\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set3\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":1602796638344},\"rbs\":{\"d\":[],\"t\":-1,\"s\":-1}}");
756+
MockResponse responseFlag = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1602796638344, \"t\":1602796638344},\"rbs\":{\"d\":[],\"t\":-1,\"s\":-1}}");
757757
MockResponse segmentResponse = new MockResponse().setBody("{\"name\":\"new_segment\",\"added\":[\"user-1\"],\"removed\":[\"user-2\",\"user-3\"],\"since\":-1,\"till\":-1}");
758758
Queue responses = new LinkedList<>();
759759
responses.add(response);

0 commit comments

Comments
 (0)