Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDKS-7608] Fix null pointer when a feature flag has a set that is not in the storage #450

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,32 @@ public Set<String> getSegments() {

private void addToFlagSets(ParsedSplit featureFlag) {
HashSet<String> sets = featureFlag.flagSets();
if( sets != null) {
for (String set: sets) {
if (!_flagSetsFilter.Intersect(set)) {
continue;
}
HashSet<String> features = _flagSets.get(set);
if (features == null) {
features = new HashSet<>();
}
features.add(featureFlag.feature());
_flagSets.put(set, features);
if(sets == null) {
return;
}
for (String set: sets) {
if (!_flagSetsFilter.Intersect(set)) {
continue;
}
HashSet<String> features = _flagSets.get(set);
if (features == null) {
features = new HashSet<>();
}
features.add(featureFlag.feature());
_flagSets.put(set, features);
}
}

private void removeFromFlagSets(String featureFlagName, HashSet<String> sets) {
if (sets != null) {
for (String set : sets) {
HashSet<String> features = _flagSets.get(set);
features.remove(featureFlagName);
_flagSets.put(set, features);
if(sets == null) {
return;
}
for (String set : sets) {
HashSet<String> features = _flagSets.get(set);
if (features == null){
continue;
}
features.remove(featureFlagName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void before() {

@Test
public void putAndGetSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split).collect(Collectors.toList()));

ParsedSplit result = _cache.get("split_name");
Expand All @@ -47,8 +47,8 @@ public void putAndGetSplit() {

@Test
public void putDuplicateSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split2 = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split, split2).collect(Collectors.toList()));

int result = _cache.getAll().size();
Expand All @@ -58,7 +58,7 @@ public void putDuplicateSplit() {

@Test
public void getInExistentSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name");
_cache.putMany(Stream.of(split).collect(Collectors.toList()));

ParsedSplit result = _cache.get("split_name_2");
Expand All @@ -67,15 +67,37 @@ public void getInExistentSplit() {

@Test
public void removeSplit() {
ParsedSplit split = getParsedSplit("split_name");
ParsedSplit split2 = getParsedSplit("split_name_2");
_cache.putMany(Stream.of(split, split2).collect(Collectors.toList()));
ParsedSplit splitWithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name");
ParsedSplit split2WithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name_2");
ParsedSplit splitWithFlagSetsNotSameStorage = getParsedSplitWithFlagSetsNotSameStorage("split_name_3");
ParsedSplit splitFlagSetsEmpty = getParsedSplitFlagSetsEmpty("split_name_4");
ParsedSplit splitFlagSetsNull = getParsedSplitFlagSetsNull("split_name_5");
_cache.putMany(Stream.of(splitWithFlagSetsSameStorage, split2WithFlagSetsSameStorage, splitWithFlagSetsNotSameStorage,
splitFlagSetsEmpty, splitFlagSetsNull).collect(Collectors.toList()));

int result = _cache.getAll().size();
Assert.assertEquals(2, result);
Assert.assertEquals(5, result);
Map<String, HashSet<String>> namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2"));
Assert.assertTrue(namesByFlagSets.get("set1").contains("split_name"));
Assert.assertTrue(namesByFlagSets.get("set2").contains("split_name"));

_cache.remove("split_name");
result = _cache.getAll().size();
Assert.assertEquals(4, result);
namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2"));
Assert.assertFalse(namesByFlagSets.get("set1").contains("split_name"));
Assert.assertFalse(namesByFlagSets.get("set2").contains("split_name"));

_cache.remove("split_name_3");
result = _cache.getAll().size();
Assert.assertEquals(3, result);

_cache.remove("split_name_4");
result = _cache.getAll().size();
Assert.assertEquals(2, result);

_cache.remove("split_name_5");
result = _cache.getAll().size();
Assert.assertEquals(1, result);

Assert.assertNull(_cache.get("split_name"));
Expand All @@ -95,10 +117,10 @@ public void setAndGetChangeNumber() {

@Test
public void getMany() {
ParsedSplit split = getParsedSplit("split_name_1");
ParsedSplit split2 = getParsedSplit("split_name_2");
ParsedSplit split3 = getParsedSplit("split_name_3");
ParsedSplit split4 = getParsedSplit("split_name_4");
ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name_1");
ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name_2");
ParsedSplit split3 = getParsedSplitWithFlagSetsSameStorage("split_name_3");
ParsedSplit split4 = getParsedSplitWithFlagSetsSameStorage("split_name_4");
_cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList()));

List<String> names = new ArrayList<>();
Expand Down Expand Up @@ -150,13 +172,25 @@ public void testSegmentNames() {

}

private ParsedSplit getParsedSplit(String splitName) {
private ParsedSplit getParsedSplitWithFlagSetsSameStorage(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1", "set2")));
}

private ParsedSplit getParsedSplitWithFlagSetsNotSameStorage(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set3")));
}

private ParsedSplit getParsedSplitFlagSetsNull(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, null);
}

private ParsedSplit getParsedSplitFlagSetsEmpty(String splitName) {
return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>());
}

@Test
public void testPutMany() {
_cache.putMany(Stream.of(getParsedSplit("split_name_1"),getParsedSplit("split_name_2"),getParsedSplit("split_name_3"),getParsedSplit("split_name_4")).collect(Collectors.toList()));
_cache.putMany(Stream.of(getParsedSplitWithFlagSetsSameStorage("split_name_1"), getParsedSplitWithFlagSetsSameStorage("split_name_2"), getParsedSplitWithFlagSetsSameStorage("split_name_3"), getParsedSplitWithFlagSetsSameStorage("split_name_4")).collect(Collectors.toList()));
List<String> names = Stream.of("split_name_1","split_name_2","split_name_3","split_name_4").collect(Collectors.toList());

Map<String, ParsedSplit> result = _cache.fetchMany(names);
Expand Down