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

Fix Sampling rules not being deleted #244

Merged
merged 3 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,25 @@ public void putRules(List<SamplingRule> inputs, Instant now) {
boolean invalidate = false;

Map<String, CentralizedRule> rules = this.rules;
List<String> inputNames = new ArrayList<>(inputs.size());

for (SamplingRule i : inputs) {
if (i.getRuleName().equals(CentralizedRule.DEFAULT_RULE_NAME)) {
putDefaultRule(i);
} else {
inputNames.add(i.getRuleName());
invalidate = putCustomRule(rules, i);
}
}
// Check if any rule was removed
if (!invalidate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if inputs has more than one items but the last item gets true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your question.
If we receive a set with a new rule invalidate will be true and the whole set will be rebuilt so we don't need to do anything, otherwise, we check if there is a rule that is no longer present and if that's the case these changes trigger a rebuild to remove it and reconstruct the rule priorities.

for (CentralizedRule rule : rules.values()) {
if (!inputNames.contains(rule.getName())) {
invalidate = true;
break;
}
}
}

if (invalidate) {
this.rules = rebuild(rules, inputs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ void testRebuildOnPriorityChange() {
Assertions.assertEquals(1, rules2.size());
}

@Test
void testRebuildOnRuleDeletion() {
CentralizedManifest manifest = new CentralizedManifest();

manifest.putRules(Arrays.asList(rule("r1"), rule("r2")), Instant.now());
Map<String, CentralizedRule> rules1 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);

manifest.putRules(Arrays.asList(rule("r2")), Instant.now());
Map<String, CentralizedRule> rules2 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);

// The map of rules should be rebuilt, resulting in a new object
Assertions.assertFalse(rules1 == rules2);

Assertions.assertEquals(2, rules1.size());
Assertions.assertEquals(1, rules2.size());
}

@Test
void testManifestSizeWithDefaultRule() {
CentralizedManifest m = new CentralizedManifest();
Expand Down