kfaraz commented on code in PR #15991:
URL: https://github.com/apache/druid/pull/15991#discussion_r1515467043


##########
server/src/main/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitor.java:
##########
@@ -64,6 +78,10 @@ public boolean doMonitor(ServiceEmitter emitter)
       emit(emitter, "worker/taskSlot/idle/count", 
statsProvider.getWorkerIdleTaskSlotCount());
       emit(emitter, "worker/taskSlot/total/count", 
statsProvider.getWorkerTotalTaskSlotCount());
       emit(emitter, "worker/taskSlot/used/count", 
statsProvider.getWorkerUsedTaskSlotCount());
+    } else if (isIndexer) {

Review Comment:
   ```suggestion
       } else {
   ```



##########
server/src/main/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitor.java:
##########
@@ -28,14 +28,20 @@
 import org.apache.druid.java.util.metrics.AbstractMonitor;
 import org.apache.druid.query.DruidMetrics;
 
+import java.util.Map;
 import java.util.Set;
 
 public class WorkerTaskCountStatsMonitor extends AbstractMonitor
 {
   private final WorkerTaskCountStatsProvider statsProvider;
+  private final IndexerTaskCountStatsProvider indexerStatsProvider;
   private final String workerCategory;
   private final String workerVersion;
   private final boolean isMiddleManager;
+  private final boolean isIndexer;
+  private static final String WORKER_RUNNING_TASK_COUNT_METRICS = 
"worker/task/running/count";
+  private static final String WORKER_ASSIGNED_TASK_COUNT_METRIC = 
"worker/task/assigned/count";
+  private static final String WORKER_COMPLETED_TASK_COUNT_METRIC = 
"worker/task/completed/count";

Review Comment:
   Nit: Since these metric names are used in just the one place, you need not 
declare these constants. You could just do what is already being done for the 
other metrics in this class such as `worker/taskSlot/used/count` etc.



##########
indexing-service/src/main/java/org/apache/druid/indexing/worker/WorkerTaskManager.java:
##########
@@ -607,6 +624,37 @@ void addCompletedTask(final String taskId, final 
TaskAnnouncement taskAnnounceme
     completedTasks.put(taskId, taskAnnouncement);
   }
 
+  private <T> Map<String, Long> getNumTasksPerDatasource(Collection<T> 
taskList, Function<T, String> getDataSourceFunc)
+  {
+    String dataSource;
+    final Map<String, Long> dataSourceTaskMap = new HashMap<>();
+
+    for (T task : taskList) {
+      dataSource = getDataSourceFunc.apply(task);
+      dataSourceTaskMap.putIfAbsent(dataSource, 0L);
+      dataSourceTaskMap.put(dataSource, dataSourceTaskMap.get(dataSource) + 
1L);

Review Comment:
   There is no need to declare the `dataSource` variable outside the loop.
   ```suggestion
         dataSourceTaskMap.merge(getDataSourceFunc.apply(task), 1L, Long::sum);
   ```



##########
indexing-service/src/main/java/org/apache/druid/indexing/worker/WorkerTaskManager.java:
##########
@@ -607,6 +624,37 @@ void addCompletedTask(final String taskId, final 
TaskAnnouncement taskAnnounceme
     completedTasks.put(taskId, taskAnnouncement);
   }
 
+  private <T> Map<String, Long> getNumTasksPerDatasource(Collection<T> 
taskList, Function<T, String> getDataSourceFunc)
+  {
+    String dataSource;
+    final Map<String, Long> dataSourceTaskMap = new HashMap<>();

Review Comment:
   ```suggestion
       final Map<String, Long> dataSourceToTaskCount = new HashMap<>();
   ```



##########
server/src/test/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitorTest.java:
##########
@@ -193,6 +233,45 @@ public void testMonitor()
     );
   }
 
+  @Test
+  public void testMonitorIndexer()
+  {
+    final WorkerTaskCountStatsMonitor monitor =
+        new WorkerTaskCountStatsMonitor(injectorForIndexer, 
ImmutableSet.of(NodeRole.INDEXER));
+    final StubServiceEmitter emitter = new StubServiceEmitter("service", 
"host");
+    monitor.doMonitor(emitter);
+    Assert.assertEquals(6, emitter.getEvents().size());
+    emitter.verifyValue(
+        "worker/task/running/count",
+        ImmutableMap.of("dataSource", "dummyDS1"),

Review Comment:
   Nit: If possible, use better names than `dummyDS1`. You can try 
`datasource1`, `datasource2` or come up with other names.
   
   I generally tend to use the datasource names such as `wikipedia`, `koala` as 
these are the names of sample datasets used in various places Druid.



##########
server/src/test/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitorTest.java:
##########
@@ -89,6 +93,36 @@ public String getWorkerVersion()
       }
     };
 
+    indexerTaskStatsProvider = new IndexerTaskCountStatsProvider()
+    {
+      @Override
+      public Map<String, Long> getWorkerRunningTasks()
+      {
+        Map <String, Long> datasourceTaskCountMap = new HashMap<>();
+        datasourceTaskCountMap.put("dummyDS1", 2L);
+        datasourceTaskCountMap.put("dummyDS2", 3L);
+        return datasourceTaskCountMap;
+      }
+
+      @Override
+      public Map<String, Long> getWorkerAssignedTasks()
+      {
+        Map <String, Long> datasourceTaskCountMap = new HashMap<>();
+        datasourceTaskCountMap.put("dummyDS3", 3L);
+        datasourceTaskCountMap.put("dummyDS4", 7L);
+        return datasourceTaskCountMap;

Review Comment:
   You can use a short-hand for this and other methods:
   ```suggestion
           return ImmutableMap.of(
               "dummyDS3", 3L,
               "dummyDS4", 7L
           );
   ```



##########
server/src/main/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitor.java:
##########
@@ -44,12 +50,20 @@ public WorkerTaskCountStatsMonitor(
   )
   {
     this.isMiddleManager = nodeRoles.contains(NodeRole.MIDDLE_MANAGER);
+    this.isIndexer = nodeRoles.contains(NodeRole.INDEXER);

Review Comment:
   I don't think this flag is needed. This monitor is used only on workers 
(i.e. indexers and middle-managers). So, it can either be a middle-manager or 
an indexer.



##########
indexing-service/src/main/java/org/apache/druid/indexing/worker/WorkerTaskManager.java:
##########
@@ -179,6 +186,16 @@ public Map<String, TaskAnnouncement> getCompletedTasks()
     return completedTasks;
   }
 
+  public Map<String, Task> getRunningTasks()
+  {
+    return CollectionUtils.mapValues(runningTasks, detail -> detail.task);
+  }
+
+  public Map<String, Task> getAssignedTasks()
+  {
+    return assignedTasks;
+  }

Review Comment:
   unused methods?



##########
server/src/main/java/org/apache/druid/server/metrics/WorkerTaskCountStatsProvider.java:
##########
@@ -50,7 +50,6 @@ public interface WorkerTaskCountStatsProvider
    */
   Long getWorkerUsedTaskSlotCount();
 
-

Review Comment:
   Extra change not needed in this PR.



##########
server/src/main/java/org/apache/druid/server/metrics/WorkerTaskCountStatsMonitor.java:
##########
@@ -44,12 +50,20 @@ public WorkerTaskCountStatsMonitor(
   )
   {
     this.isMiddleManager = nodeRoles.contains(NodeRole.MIDDLE_MANAGER);
+    this.isIndexer = nodeRoles.contains(NodeRole.INDEXER);
     if (isMiddleManager) {
       this.statsProvider = 
injector.getInstance(WorkerTaskCountStatsProvider.class);
+      this.indexerStatsProvider = null;
       this.workerCategory = statsProvider.getWorkerCategory();
       this.workerVersion = statsProvider.getWorkerVersion();
+    } else if (isIndexer){
+      this.indexerStatsProvider = 
injector.getInstance(IndexerTaskCountStatsProvider.class);
+      this.statsProvider = null;
+      this.workerCategory = null;
+      this.workerVersion = null;
     } else {
       this.statsProvider = null;

Review Comment:
   ```suggestion
       } else {
         this.indexerStatsProvider = 
injector.getInstance(IndexerTaskCountStatsProvider.class);
   ```



##########
server/src/main/java/org/apache/druid/server/metrics/IndexerTaskCountStatsProvider.java:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.server.metrics;
+
+import java.util.Map;
+
+public interface IndexerTaskCountStatsProvider

Review Comment:
   Please add a javadoc for this interface and its methods.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to