Skip to content

Commit 8431b8d

Browse files
authored
Merge pull request #559 from splitio/rbs-excluded-segments
Rbs excluded segments
2 parents 1fae45f + 1f9197b commit 8431b8d

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
public class Excluded {
66
public List<String> keys;
7-
public List<String> segments;
7+
public List<ExcludedSegments> segments;
88
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.split.client.dtos;
2+
3+
public class ExcludedSegments {
4+
public ExcludedSegments() {}
5+
public ExcludedSegments(String type, String name) {
6+
this.type = type;
7+
this.name = name;
8+
}
9+
10+
public String type;
11+
public String name;
12+
}

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

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

33
import com.google.common.collect.ImmutableList;
4+
import io.split.client.dtos.ExcludedSegments;
45
import io.split.engine.matchers.AttributeMatcher;
56
import io.split.engine.matchers.UserDefinedSegmentMatcher;
67

@@ -15,15 +16,15 @@ public class ParsedRuleBasedSegment {
1516
private final String _trafficTypeName;
1617
private final long _changeNumber;
1718
private final List<String> _excludedKeys;
18-
private final List<String> _excludedSegments;
19+
private final List<ExcludedSegments> _excludedSegments;
1920

2021
public static ParsedRuleBasedSegment createParsedRuleBasedSegmentForTests(
2122
String ruleBasedSegment,
2223
List<ParsedCondition> matcherAndSplits,
2324
String trafficTypeName,
2425
long changeNumber,
2526
List<String> excludedKeys,
26-
List<String> excludedSegments
27+
List<ExcludedSegments> excludedSegments
2728
) {
2829
return new ParsedRuleBasedSegment(
2930
ruleBasedSegment,
@@ -41,7 +42,7 @@ public ParsedRuleBasedSegment(
4142
String trafficTypeName,
4243
long changeNumber,
4344
List<String> excludedKeys,
44-
List<String> excludedSegments
45+
List<ExcludedSegments> excludedSegments
4546
) {
4647
_ruleBasedSegment = ruleBasedSegment;
4748
_parsedCondition = ImmutableList.copyOf(matcherAndSplits);
@@ -64,7 +65,7 @@ public List<ParsedCondition> parsedConditions() {
6465
public long changeNumber() {return _changeNumber;}
6566

6667
public List<String> excludedKeys() {return _excludedKeys;}
67-
public List<String> excludedSegments() {return _excludedSegments;}
68+
public List<ExcludedSegments> excludedSegments() {return _excludedSegments;}
6869

6970
@Override
7071
public int hashCode() {

client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java

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

3+
import io.split.client.dtos.ExcludedSegments;
34
import io.split.engine.evaluator.EvaluationContext;
45
import io.split.engine.experiments.ParsedCondition;
56
import io.split.engine.experiments.ParsedRuleBasedSegment;
@@ -17,6 +18,8 @@
1718
* @author adil
1819
*/
1920
public class RuleBasedSegmentMatcher implements Matcher {
21+
private final String standardType = "standard";
22+
2023
private final String _segmentName;
2124

2225
public RuleBasedSegmentMatcher(String segmentName) {
@@ -37,8 +40,8 @@ public boolean match(Object matchValue, String bucketingKey, Map<String, Object>
3740
return false;
3841
}
3942

40-
for (String segmentName: parsedRuleBasedSegment.excludedSegments()) {
41-
if (evaluationContext.getSegmentCache().isInSegment(segmentName, (String) matchValue)) {
43+
for (ExcludedSegments segment: parsedRuleBasedSegment.excludedSegments()) {
44+
if (segment.type.equals(standardType) && evaluationContext.getSegmentCache().isInSegment(segment.name, (String) matchValue)) {
4245
return false;
4346
}
4447
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.Lists;
44
import com.google.common.collect.Sets;
55
import io.split.client.dtos.ConditionType;
6+
import io.split.client.dtos.ExcludedSegments;
67
import io.split.client.dtos.MatcherCombiner;
78
import io.split.client.dtos.SplitChange;
89
import io.split.client.utils.Json;
@@ -15,18 +16,23 @@
1516
import org.junit.Test;
1617

1718
import java.util.ArrayList;
19+
import java.util.List;
1820

1921
import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges;
2022

2123
public class ParsedRuleBasedSegmentTest {
2224

2325
@Test
2426
public void works() {
27+
List<ExcludedSegments> excludedSegments = new ArrayList<>();
28+
excludedSegments.add(new ExcludedSegments("standard","segment1"));
29+
excludedSegments.add(new ExcludedSegments("standard","segment2"));
30+
2531
AttributeMatcher segmentMatcher = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher("employees"));
2632
CombiningMatcher segmentCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(segmentMatcher));
2733
ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("another_rule_based_segment",
28-
Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")),"user",
29-
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList("segment1", "segment2"));
34+
Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")), "user",
35+
123, Lists.newArrayList("mauro@test.io", "gaston@test.io"), excludedSegments);
3036

3137
Assert.assertEquals(Sets.newHashSet("employees"), parsedRuleBasedSegment.getSegmentsNames());
3238
Assert.assertEquals("another_rule_based_segment", parsedRuleBasedSegment.ruleBasedSegment());
@@ -48,15 +54,15 @@ public void worksWithoutExcluded() {
4854
Assert.assertTrue(toUpdate.getToAdd().get(0).excludedSegments().isEmpty());
4955

5056
load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\","
51-
+ "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[\"segment1\"]},\"conditions\": [{\"contitionType\": \"ROLLOUT\","
57+
+ "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[{\"type\": \"standard\",\"name\":\"segment1\"}]},\"conditions\": [{\"contitionType\": \"ROLLOUT\","
5258
+ "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}],"
5359
+ "\"combiner\": \"AND\"}}]}]}}";
5460
change = Json.fromJson(load, SplitChange.class);
5561
toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d);
5662
Assert.assertTrue(toUpdate.getToAdd().get(0).excludedKeys().isEmpty());
5763

5864
load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\","
59-
+ "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[\"segment1\"], \"keys\":null},\"conditions\": [{\"contitionType\": \"ROLLOUT\","
65+
+ "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[{\"type\": \"standard\",\"name\":\"segment1\"}], \"keys\":null},\"conditions\": [{\"contitionType\": \"ROLLOUT\","
6066
+ "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}],"
6167
+ "\"combiner\": \"AND\"}}]}]}}";
6268
change = Json.fromJson(load, SplitChange.class);

client/src/test/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImplTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.split.storages.memory;
22

33
import com.google.common.collect.Sets;
4+
import io.split.client.dtos.ExcludedSegments;
45
import io.split.client.dtos.MatcherCombiner;
56
import io.split.engine.experiments.ParsedRuleBasedSegment;
67
import io.split.engine.experiments.ParsedCondition;
@@ -14,6 +15,9 @@
1415
import org.junit.Test;
1516
import com.google.common.collect.Lists;
1617

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
1721
public class RuleBasedSegmentCacheInMemoryImplTest extends TestCase {
1822

1923
@Test
@@ -35,18 +39,25 @@ public void testAddAndDeleteSegment(){
3539

3640
@Test
3741
public void testMultipleSegment(){
42+
List<ExcludedSegments> excludedSegments = new ArrayList<>();
43+
excludedSegments.add(new ExcludedSegments("standard","segment1"));
44+
excludedSegments.add(new ExcludedSegments("standard","segment3"));
45+
3846
RuleBasedSegmentCacheInMemoryImp ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp();
3947
AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin")));
4048
CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher));
4149
ParsedRuleBasedSegment parsedRuleBasedSegment1 = new ParsedRuleBasedSegment("sample_rule_based_segment",
4250
Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, "label")),"user",
43-
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList(Lists.newArrayList("segment1", "segment3")));
51+
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), excludedSegments);
4452

53+
excludedSegments.clear();
54+
excludedSegments.add(new ExcludedSegments("standard","segment1"));
55+
excludedSegments.add(new ExcludedSegments("standard","segment2"));
4556
AttributeMatcher segmentMatcher = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher("employees"));
4657
CombiningMatcher segmentCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(segmentMatcher));
4758
ParsedRuleBasedSegment parsedRuleBasedSegment2 = new ParsedRuleBasedSegment("another_rule_based_segment",
4859
Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")),"user",
49-
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList("segment1", "segment2"));
60+
123, Lists.newArrayList("mauro@test.io","gaston@test.io"), excludedSegments);
5061

5162
ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment1, parsedRuleBasedSegment2), null, 123);
5263
assertEquals(Lists.newArrayList("another_rule_based_segment", "sample_rule_based_segment"), ruleBasedSegmentCache.ruleBasedSegmentNames());

0 commit comments

Comments
 (0)