Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.druid.indexing.common.task.Task;
import org.apache.druid.query.DruidMetrics;
import org.apache.druid.utils.CollectionUtils;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -70,6 +71,7 @@ public Selector(
public boolean evaluate(Task task)
{
boolean isMatch = true;

if (cxtTagsConditions != null) {
isMatch = cxtTagsConditions.entrySet().stream().allMatch(entry -> {
String tagKey = entry.getKey();
Expand All @@ -80,15 +82,15 @@ public boolean evaluate(Task task)
}
Object tagValue = tags.get(tagKey);

return tagValue == null ? false : tagValues.contains((String) tagValue);
return tagValue != null && tagValues.contains((String) tagValue);
});
}

if (isMatch && taskTypeCondition != null) {
if (isMatch && !CollectionUtils.isNullOrEmpty(taskTypeCondition)) {
isMatch = taskTypeCondition.contains(task.getType());
}

if (isMatch && dataSourceCondition != null) {
if (isMatch && !CollectionUtils.isNullOrEmpty(dataSourceCondition)) {
isMatch = dataSourceCondition.contains(task.getDataSource());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,68 @@
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SelectorTest
{
@Test
public void shouldReturnTrueWhenMatchTasksTagsAndEmptyDataSource()
{
Map<String, Set<String>> cxtTagsConditions = new HashMap<>();
cxtTagsConditions.put("tag1", Sets.newHashSet("tag1Value"));

Task task = NoopTask.create();
task.addToContext(DruidMetrics.TAGS, ImmutableMap.of("tag1", "tag1Value"));

Selector selector = new Selector(
"TestSelector",
cxtTagsConditions,
Sets.newHashSet(NoopTask.TYPE),
new HashSet<>()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add another test for passing in null for the datasource selector

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

existing tests already cover null value

);

Assert.assertTrue(selector.evaluate(task));
}

@Test
public void shouldReturnTrueWhenMatchDataSourceTagsAndEmptyTasks()
{
String datasource = "table";
Map<String, Set<String>> cxtTagsConditions = new HashMap<>();
cxtTagsConditions.put("tag1", Sets.newHashSet("tag1Value"));

Task task = NoopTask.forDatasource(datasource);
task.addToContext(DruidMetrics.TAGS, ImmutableMap.of("tag1", "tag1Value"));

Selector selector = new Selector(
"TestSelector",
cxtTagsConditions,
new HashSet<>(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add another test for passing in null for the task type selector

Sets.newHashSet(datasource)
);

Assert.assertTrue(selector.evaluate(task));
}

@Test
public void shouldReturnTrueWhenMatchDataSourceTasksAndEmptyTags()
{
String datasource = "table";
Map<String, Set<String>> cxtTagsConditions = new HashMap<>();

Task task = NoopTask.forDatasource(datasource);

Selector selector = new Selector(
"TestSelector",
cxtTagsConditions,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add another test for passing in null for the tags condition

Sets.newHashSet(NoopTask.TYPE),
Sets.newHashSet(datasource)
);

Assert.assertTrue(selector.evaluate(task));
}

@Test
public void shouldReturnTrueWhenAllTagsAndTasksMatch()
Expand Down