Skip to content

Commit 038aa7c

Browse files
committed
polishing and updating tests
1 parent d0b6f45 commit 038aa7c

32 files changed

+419
-555
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ private Spec() {
77
}
88

99
// TODO: Change the schema to 1.3 when updating splitclient
10-
public static String SPEC_VERSION = "1.1";
1110
public static final String SPEC_1_3 = "1.3";
1211
public static final String SPEC_1_1 = "1.1";
12+
public static String SPEC_VERSION = SPEC_1_3;
1313
}
1414

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

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,40 @@ long makeRandomTill() {
6666
@Override
6767
public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
6868
long start = System.currentTimeMillis();
69-
for (int i=0; i<2; i++) {
70-
try {
71-
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + SPEC_VERSION);
72-
uriBuilder.addParameter(SINCE, "" + since);
73-
if (SPEC_VERSION.equals(Spec.SPEC_1_3)) {
74-
uriBuilder.addParameter(RB_SINCE, "" + sinceRBS);
75-
}
76-
if (!options.flagSetsFilter().isEmpty()) {
77-
uriBuilder.addParameter(SETS, "" + options.flagSetsFilter());
78-
}
79-
if (options.hasCustomCN()) {
80-
uriBuilder.addParameter(TILL, "" + options.targetCN());
81-
}
82-
URI uri = uriBuilder.build();
83-
SplitHttpResponse response = _client.get(uri, options, null);
69+
try {
70+
URI uri = buildURL(options, since, sinceRBS);
71+
SplitHttpResponse response = _client.get(uri, options, null);
8472

85-
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
86-
if (response.statusCode() == HttpStatus.SC_REQUEST_URI_TOO_LONG) {
87-
_log.error("The amount of flag sets provided are big causing uri length error.");
88-
throw new UriTooLongException(String.format("Status code: %s. Message: %s", response.statusCode(), response.statusMessage()));
89-
}
90-
if (response.statusCode() == HttpStatus.SC_BAD_REQUEST && response.statusMessage().equals("unknown spec")) {
91-
_log.warn(String.format("Detected old spec response, falling back to spec 1.1"));
92-
SPEC_VERSION = Spec.SPEC_1_1;
93-
continue;
94-
}
95-
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SPLIT_SYNC, response.statusCode());
96-
throw new IllegalStateException(
97-
String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode())
98-
);
73+
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
74+
if (response.statusCode() == HttpStatus.SC_REQUEST_URI_TOO_LONG) {
75+
_log.error("The amount of flag sets provided are big causing uri length error.");
76+
throw new UriTooLongException(String.format("Status code: %s. Message: %s", response.statusCode(), response.statusMessage()));
9977
}
100-
if (SPEC_VERSION.equals(Spec.SPEC_1_1)) {
101-
return Json.fromJson(response.body(), SplitChange.class);
102-
}
103-
return GenericClientUtil.ExtractFeatureFlagsAndRuleBasedSegments(response.body());
104-
} catch (Exception e) {
105-
throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e);
106-
} finally {
107-
_telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SPLITS, System.currentTimeMillis() - start);
78+
79+
_telemetryRuntimeProducer.recordSyncError(ResourceEnum.SPLIT_SYNC, response.statusCode());
80+
throw new IllegalStateException(
81+
String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode())
82+
);
10883
}
84+
return GenericClientUtil.ExtractFeatureFlagsAndRuleBasedSegments(response.body());
85+
} catch (Exception e) {
86+
throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e);
87+
} finally {
88+
_telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SPLITS, System.currentTimeMillis() - start);
89+
}
90+
}
91+
92+
private URI buildURL(FetchOptions options, long since, long sinceRBS) throws URISyntaxException {
93+
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + SPEC_VERSION);
94+
uriBuilder.addParameter(SINCE, "" + since);
95+
uriBuilder.addParameter(RB_SINCE, "" + sinceRBS);
96+
if (!options.flagSetsFilter().isEmpty()) {
97+
uriBuilder.addParameter(SETS, "" + options.flagSetsFilter());
98+
}
99+
if (options.hasCustomCN()) {
100+
uriBuilder.addParameter(TILL, "" + options.targetCN());
109101
}
110-
return null;
102+
return uriBuilder.build();
111103
}
112104

113105
@VisibleForTesting

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.split.client.utils.LocalhostSanitizer;
88
import io.split.engine.common.FetchOptions;
99
import io.split.engine.experiments.SplitChangeFetcher;
10+
import org.checkerframework.checker.units.qual.A;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

@@ -15,6 +16,7 @@
1516
import java.nio.charset.StandardCharsets;
1617
import java.security.MessageDigest;
1718
import java.security.NoSuchAlgorithmException;
19+
import java.util.ArrayList;
1820
import java.util.Arrays;
1921

2022
public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher {
@@ -33,6 +35,9 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
3335
try {
3436
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8)));
3537
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
38+
splitChange.ruleBasedSegments = new ArrayList<>();
39+
splitChange.tillRBS = -1;
40+
splitChange.sinceRBS = -1;
3641
return processSplitChange(splitChange, since);
3742
} catch (Exception e) {
3843
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
8282
}
8383
splitChange.till = since;
8484
splitChange.since = since;
85+
splitChange.sinceRBS = -1;
86+
splitChange.tillRBS = -1;
87+
splitChange.ruleBasedSegments = new ArrayList<>();
8588
return splitChange;
8689
} catch (FileNotFoundException f) {
8790
_log.warn("There was no file named " + _splitFile.getPath() + " found. " +

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
7373
}
7474
splitChange.till = since;
7575
splitChange.since = since;
76+
splitChange.sinceRBS = -1;
77+
splitChange.tillRBS = -1;
78+
splitChange.ruleBasedSegments = new ArrayList<>();
7679
return splitChange;
7780
} catch (Exception e) {
7881
throw new IllegalStateException("Problem fetching splitChanges using a yaml file: " + e.getMessage(), e);

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

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

3-
import io.split.client.dtos.RuleBasedSegment;
43
import io.split.client.dtos.Split;
54
import io.split.client.dtos.Status;
65
import io.split.client.interceptors.FlagSetsFilter;
7-
import io.split.engine.experiments.ParsedRuleBasedSegment;
86
import io.split.engine.experiments.ParsedSplit;
9-
import io.split.engine.experiments.RuleBasedSegmentParser;
107
import io.split.engine.experiments.SplitParser;
11-
import io.split.storages.RuleBasedSegmentCacheProducer;
128
import org.slf4j.Logger;
139
import org.slf4j.LoggerFactory;
1410

@@ -44,27 +40,4 @@ public static FeatureFlagsToUpdate processFeatureFlagChanges(SplitParser splitPa
4440
}
4541
return new FeatureFlagsToUpdate(toAdd, toRemove, segments);
4642
}
47-
48-
public static RuleBasedSegmentsToUpdate processRuleBasedSegmentChanges(RuleBasedSegmentParser ruleBasedSegmentParser,
49-
List<RuleBasedSegment> ruleBasedSegments) {
50-
List<ParsedRuleBasedSegment> toAdd = new ArrayList<>();
51-
List<String> toRemove = new ArrayList<>();
52-
Set<String> segments = new HashSet<>();
53-
for (RuleBasedSegment ruleBasedSegment : ruleBasedSegments) {
54-
if (ruleBasedSegment.status != Status.ACTIVE) {
55-
// archive.
56-
toRemove.add(ruleBasedSegment.name);
57-
continue;
58-
}
59-
ParsedRuleBasedSegment parsedRuleBasedSegment = ruleBasedSegmentParser.parse(ruleBasedSegment);
60-
if (parsedRuleBasedSegment == null) {
61-
_log.debug(String.format("We could not parse the rule based segment definition for: %s", ruleBasedSegment.name));
62-
continue;
63-
}
64-
segments.addAll(parsedRuleBasedSegment.getSegmentsNames());
65-
toAdd.add(parsedRuleBasedSegment);
66-
}
67-
return new RuleBasedSegmentsToUpdate(toAdd, toRemove, segments);
68-
}
69-
7043
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.split.client.utils;
2+
3+
import io.split.client.dtos.RuleBasedSegment;
4+
import io.split.client.dtos.Status;
5+
import io.split.engine.experiments.ParsedRuleBasedSegment;
6+
import io.split.engine.experiments.RuleBasedSegmentParser;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.util.ArrayList;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Set;
14+
15+
public class RuleBasedSegmentProcessor {
16+
private static final Logger _log = LoggerFactory.getLogger(RuleBasedSegmentProcessor.class);
17+
18+
public static RuleBasedSegmentsToUpdate processRuleBasedSegmentChanges(RuleBasedSegmentParser ruleBasedSegmentParser,
19+
List<RuleBasedSegment> ruleBasedSegments) {
20+
List<ParsedRuleBasedSegment> toAdd = new ArrayList<>();
21+
List<String> toRemove = new ArrayList<>();
22+
Set<String> segments = new HashSet<>();
23+
for (RuleBasedSegment ruleBasedSegment : ruleBasedSegments) {
24+
if (ruleBasedSegment.status != Status.ACTIVE) {
25+
// archive.
26+
toRemove.add(ruleBasedSegment.name);
27+
continue;
28+
}
29+
ParsedRuleBasedSegment parsedRuleBasedSegment = ruleBasedSegmentParser.parse(ruleBasedSegment);
30+
if (parsedRuleBasedSegment == null) {
31+
_log.debug(String.format("We could not parse the rule based segment definition for: %s", ruleBasedSegment.name));
32+
continue;
33+
}
34+
segments.addAll(parsedRuleBasedSegment.getSegmentsNames());
35+
toAdd.add(parsedRuleBasedSegment);
36+
}
37+
return new RuleBasedSegmentsToUpdate(toAdd, toRemove, segments);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)