Skip to content

Commit fc0005d

Browse files
authored
Merge pull request #544 from splitio/rbs-consumer
Added consumer classes
2 parents 6f82e89 + 815c9f3 commit fc0005d

File tree

6 files changed

+317
-18
lines changed

6 files changed

+317
-18
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import io.split.storages.SplitCache;
6868
import io.split.storages.SplitCacheConsumer;
6969
import io.split.storages.SplitCacheProducer;
70-
import io.split.storages.RuleBasedSegmentCacheConsumer;
7170
import io.split.storages.RuleBasedSegmentCache;
7271
import io.split.storages.RuleBasedSegmentCacheProducer;
7372
import io.split.storages.enums.OperationMode;
@@ -80,6 +79,7 @@
8079
import io.split.storages.pluggable.adapters.UserCustomSegmentAdapterConsumer;
8180
import io.split.storages.pluggable.adapters.UserCustomSplitAdapterConsumer;
8281
import io.split.storages.pluggable.adapters.UserCustomTelemetryAdapterProducer;
82+
import io.split.storages.pluggable.adapters.UserCustomRuleBasedSegmentAdapterConsumer;
8383
import io.split.storages.pluggable.domain.UserStorageWrapper;
8484
import io.split.storages.pluggable.synchronizer.TelemetryConsumerSubmitter;
8585
import io.split.telemetry.storage.InMemoryTelemetryStorage;
@@ -341,8 +341,8 @@ protected SplitFactoryImpl(String apiToken, SplitClientConfig config, CustomStor
341341
_gates = new SDKReadinessGates();
342342

343343
_telemetrySynchronizer = new TelemetryConsumerSubmitter(customStorageWrapper, _sdkMetadata);
344-
// TODO Update the instance to UserCustomRuleBasedSegmentAdapterConsumer
345-
RuleBasedSegmentCacheConsumer userCustomRuleBasedSegmentAdapterConsumer = new RuleBasedSegmentCacheInMemoryImp();
344+
UserCustomRuleBasedSegmentAdapterConsumer userCustomRuleBasedSegmentAdapterConsumer =
345+
new UserCustomRuleBasedSegmentAdapterConsumer(customStorageWrapper);
346346
_evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer, userCustomRuleBasedSegmentAdapterConsumer);
347347
_impressionsSender = PluggableImpressionSender.create(customStorageWrapper);
348348
_uniqueKeysTracker = createUniqueKeysTracker(config);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.split.storages.pluggable.adapters;
2+
3+
import io.split.client.dtos.RuleBasedSegment;
4+
import io.split.client.utils.Json;
5+
import io.split.engine.experiments.ParsedRuleBasedSegment;
6+
import io.split.engine.experiments.RuleBasedSegmentParser;
7+
import io.split.storages.RuleBasedSegmentCacheConsumer;
8+
import io.split.storages.pluggable.domain.PrefixAdapter;
9+
import io.split.storages.pluggable.domain.UserStorageWrapper;
10+
import io.split.storages.pluggable.utils.Helper;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import pluggable.CustomStorageWrapper;
14+
15+
import java.util.ArrayList;
16+
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
import static com.google.common.base.Preconditions.checkNotNull;
22+
23+
public class UserCustomRuleBasedSegmentAdapterConsumer implements RuleBasedSegmentCacheConsumer {
24+
25+
private static final Logger _log = LoggerFactory.getLogger(UserCustomRuleBasedSegmentAdapterConsumer.class);
26+
27+
private final RuleBasedSegmentParser _ruleBasedSegmentParser;
28+
private final UserStorageWrapper _userStorageWrapper;
29+
30+
public UserCustomRuleBasedSegmentAdapterConsumer(CustomStorageWrapper customStorageWrapper) {
31+
_ruleBasedSegmentParser = new RuleBasedSegmentParser();
32+
_userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper));
33+
}
34+
35+
@Override
36+
public long getChangeNumber() {
37+
String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentChangeNumber());
38+
return Helper.responseToLong(wrapperResponse, -1L);
39+
}
40+
41+
@Override
42+
public ParsedRuleBasedSegment get(String name) {
43+
String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentKey(name));
44+
if(wrapperResponse == null) {
45+
return null;
46+
}
47+
RuleBasedSegment ruleBasedSegment = Json.fromJson(wrapperResponse, RuleBasedSegment.class);
48+
if(ruleBasedSegment == null) {
49+
_log.warn("Could not parse RuleBasedSegment.");
50+
return null;
51+
}
52+
return _ruleBasedSegmentParser.parse(ruleBasedSegment);
53+
}
54+
55+
@Override
56+
public Collection<ParsedRuleBasedSegment> getAll() {
57+
Set<String> keys = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment());
58+
if(keys == null) {
59+
return new ArrayList<>();
60+
}
61+
List<String> wrapperResponse = _userStorageWrapper.getMany(new ArrayList<>(keys));
62+
if(wrapperResponse == null) {
63+
return new ArrayList<>();
64+
}
65+
return stringsToParsedRuleBasedSegments(wrapperResponse);
66+
}
67+
68+
@Override
69+
public List<String> ruleBasedSegmentNames() {
70+
Set<String> ruleBasedSegmentNamesWithPrefix = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment());
71+
ruleBasedSegmentNamesWithPrefix = ruleBasedSegmentNamesWithPrefix.stream().
72+
map(key -> key.replace(PrefixAdapter.buildRuleBasedSegmentsPrefix(), "")).
73+
collect(Collectors.toSet());
74+
return new ArrayList<>(ruleBasedSegmentNamesWithPrefix);
75+
}
76+
77+
@Override
78+
public Set<String> getSegments() {
79+
return getAll().stream()
80+
.flatMap(parsedRuleBasedSegment -> parsedRuleBasedSegment.
81+
getSegmentsNames().stream()).collect(Collectors.toSet());
82+
}
83+
84+
private List<ParsedRuleBasedSegment> stringsToParsedRuleBasedSegments(List<String> elements) {
85+
List<ParsedRuleBasedSegment> result = new ArrayList<>();
86+
for(String s : elements) {
87+
if(s != null) {
88+
result.add(_ruleBasedSegmentParser.parse(Json.fromJson(s, RuleBasedSegment.class)));
89+
continue;
90+
}
91+
result.add(null);
92+
}
93+
return result;
94+
}
95+
}

client/src/main/java/io/split/storages/pluggable/domain/PrefixAdapter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class PrefixAdapter {
2020
private static final String EXCEPTIONS = "exceptions";
2121
private static final String INIT = "init";
2222
private static final String FLAG_SET = "flagSet";
23+
private static final String RULE_BASED_SEGMENT_PREFIX = "rbsegment";
24+
private static final String RULE_BASED_SEGMENTS_PREFIX = "rbsegments";
2325

2426
public static String buildSplitKey(String name) {
2527
return String.format(DEFAULT_PREFIX+ SPLIT_PREFIX +"%s", name);
@@ -37,6 +39,22 @@ public static String buildSplitsPrefix(){
3739
return DEFAULT_PREFIX+SPLIT_PREFIX;
3840
}
3941

42+
public static String buildRuleBasedSegmentKey(String name) {
43+
return String.format(DEFAULT_PREFIX+ RULE_BASED_SEGMENT_PREFIX +"%s", name);
44+
}
45+
46+
public static String buildRuleBasedSegmentsPrefix(){
47+
return DEFAULT_PREFIX+RULE_BASED_SEGMENT_PREFIX;
48+
}
49+
50+
public static String buildRuleBasedSegmentChangeNumber() {
51+
return DEFAULT_PREFIX+RULE_BASED_SEGMENTS_PREFIX+"till";
52+
}
53+
54+
public static String buildGetAllRuleBasedSegment() {
55+
return DEFAULT_PREFIX+RULE_BASED_SEGMENT_PREFIX+"*";
56+
}
57+
4058
public static String buildTrafficTypeExists(String trafficType) {
4159
return String.format(DEFAULT_PREFIX+TRAFFIC_TYPE_PREFIX+"%s", trafficType);
4260
}

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

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

3+
import io.split.client.dtos.Condition;
4+
import io.split.client.dtos.Excluded;
5+
import io.split.client.dtos.RuleBasedSegment;
6+
import io.split.client.dtos.Status;
37
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
48
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
59
import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -12,6 +16,8 @@
1216
import java.io.IOException;
1317
import java.lang.reflect.InvocationTargetException;
1418
import java.lang.reflect.Method;
19+
import java.util.ArrayList;
20+
import java.util.List;
1521

1622
public class TestHelper {
1723
public static CloseableHttpClient mockHttpClient(String jsonName, int httpStatus) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
@@ -36,4 +42,20 @@ public static CloseableHttpResponse classicResponseToCloseableMock(ClassicHttpRe
3642
adaptMethod.setAccessible(true);
3743
return (CloseableHttpResponse) adaptMethod.invoke(null, mocked);
3844
}
45+
46+
public static RuleBasedSegment makeRuleBasedSegment(String name, List<Condition> conditions, long changeNumber) {
47+
Excluded excluded = new Excluded();
48+
excluded.segments = new ArrayList<>();
49+
excluded.keys = new ArrayList<>();
50+
51+
RuleBasedSegment ruleBasedSegment = new RuleBasedSegment();
52+
ruleBasedSegment.name = name;
53+
ruleBasedSegment.status = Status.ACTIVE;
54+
ruleBasedSegment.conditions = conditions;
55+
ruleBasedSegment.trafficTypeName = "user";
56+
ruleBasedSegment.changeNumber = changeNumber;
57+
ruleBasedSegment.excluded = excluded;
58+
return ruleBasedSegment;
59+
}
60+
3961
}

client/src/test/java/io/split/engine/experiments/RuleBasedSegmentParserTest.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import java.util.stream.Collectors;
3232
import java.util.stream.Stream;
3333

34+
import static io.split.TestHelper.makeRuleBasedSegment;
3435
import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges;
36+
3537
import static org.junit.Assert.assertTrue;
3638

3739
/**
@@ -542,21 +544,6 @@ public void setMatcherTest(Condition c, io.split.engine.matchers.Matcher m) {
542544
Assert.assertEquals(actual, expected);
543545
}
544546

545-
private RuleBasedSegment makeRuleBasedSegment(String name, List<Condition> conditions, long changeNumber) {
546-
Excluded excluded = new Excluded();
547-
excluded.segments = new ArrayList<>();
548-
excluded.keys = new ArrayList<>();
549-
550-
RuleBasedSegment ruleBasedSegment = new RuleBasedSegment();
551-
ruleBasedSegment.name = name;
552-
ruleBasedSegment.status = Status.ACTIVE;
553-
ruleBasedSegment.conditions = conditions;
554-
ruleBasedSegment.trafficTypeName = "user";
555-
ruleBasedSegment.changeNumber = changeNumber;
556-
ruleBasedSegment.excluded = excluded;
557-
return ruleBasedSegment;
558-
}
559-
560547
private SegmentChange getSegmentChange(long since, long till, String segmentName){
561548
SegmentChange segmentChange = new SegmentChange();
562549
segmentChange.name = segmentName;

0 commit comments

Comments
 (0)