diff --git a/extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/execution/Selector.java b/extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/execution/Selector.java index a314a69b3811..29e31218ad01 100644 --- a/extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/execution/Selector.java +++ b/extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/execution/Selector.java @@ -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; @@ -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(); @@ -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()); } diff --git a/extensions-contrib/kubernetes-overlord-extensions/src/test/java/org/apache/druid/k8s/overlord/execution/SelectorTest.java b/extensions-contrib/kubernetes-overlord-extensions/src/test/java/org/apache/druid/k8s/overlord/execution/SelectorTest.java index 0ecff67408e3..14e2ffb21439 100644 --- a/extensions-contrib/kubernetes-overlord-extensions/src/test/java/org/apache/druid/k8s/overlord/execution/SelectorTest.java +++ b/extensions-contrib/kubernetes-overlord-extensions/src/test/java/org/apache/druid/k8s/overlord/execution/SelectorTest.java @@ -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> 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<>() + ); + + Assert.assertTrue(selector.evaluate(task)); + } + + @Test + public void shouldReturnTrueWhenMatchDataSourceTagsAndEmptyTasks() + { + String datasource = "table"; + Map> 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<>(), + Sets.newHashSet(datasource) + ); + + Assert.assertTrue(selector.evaluate(task)); + } + + @Test + public void shouldReturnTrueWhenMatchDataSourceTasksAndEmptyTags() + { + String datasource = "table"; + Map> cxtTagsConditions = new HashMap<>(); + + Task task = NoopTask.forDatasource(datasource); + + Selector selector = new Selector( + "TestSelector", + cxtTagsConditions, + Sets.newHashSet(NoopTask.TYPE), + Sets.newHashSet(datasource) + ); + + Assert.assertTrue(selector.evaluate(task)); + } @Test public void shouldReturnTrueWhenAllTagsAndTasksMatch()