LakshSingla commented on code in PR #15025:
URL: https://github.com/apache/druid/pull/15025#discussion_r1337857801


##########
processing/src/main/java/org/apache/druid/java/util/metrics/MergeBufferPoolMonitor.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.java.util.metrics;
+
+import com.google.inject.Inject;
+import org.apache.druid.collections.BlockingPool;
+import org.apache.druid.guice.annotations.Merging;
+import org.apache.druid.java.util.emitter.service.ServiceEmitter;
+import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
+
+import java.nio.ByteBuffer;
+
+public class MergeBufferPoolMonitor extends AbstractMonitor
+{
+
+  private final BlockingPool<ByteBuffer> mergeBufferPool;
+
+  @Inject
+  public MergeBufferPoolMonitor(
+      @Merging BlockingPool<ByteBuffer> mergeBufferPoolIn

Review Comment:
   The monitor itself shouldn't care about the actual blocking pool. It should 
have a "stats provider" (interface) which exposes the method 
`getPendingQueries`. 
   
   You can take a look at the example: `QueryCountStatsProvider` and 
`QueryResource` for the pattern. You will need to wire it up in Guice as well.  



##########
processing/src/main/java/org/apache/druid/java/util/metrics/MergeBufferPoolMonitor.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.java.util.metrics;
+
+import com.google.inject.Inject;
+import org.apache.druid.collections.BlockingPool;
+import org.apache.druid.guice.annotations.Merging;
+import org.apache.druid.java.util.emitter.service.ServiceEmitter;
+import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
+
+import java.nio.ByteBuffer;
+
+public class MergeBufferPoolMonitor extends AbstractMonitor
+{
+
+  private final BlockingPool<ByteBuffer> mergeBufferPool;
+
+  @Inject
+  public MergeBufferPoolMonitor(
+      @Merging BlockingPool<ByteBuffer> mergeBufferPoolIn
+  )
+  {
+    this.mergeBufferPool = mergeBufferPoolIn;
+  }
+
+  @Override
+  public boolean doMonitor(ServiceEmitter emitter)
+  {
+    long pendingQueries = this.mergeBufferPool.getPendingQueries();
+
+    ServiceMetricEvent.Builder builder = 
ServiceMetricEvent.builder().setFeed("metrics");
+    emitter.emit(builder.setMetric("mergebuffer/pendingQueries", 
pendingQueries));

Review Comment:
   ```suggestion
       emitter.emit(builder.setMetric("mergeBuffer/pendingQueries", 
pendingQueries));
   ```
   Capitalization since merge buffer is not a single word  



##########
processing/src/test/java/org/apache/druid/java/util/metrics/MergeBufferPoolMonitorTest.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.java.util.metrics;
+
+import org.apache.druid.collections.BlockingPool;
+import org.apache.druid.collections.DefaultBlockingPool;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class MergeBufferPoolMonitorTest
+{
+  private ExecutorService executorService;
+
+  @Before
+  public void setUp()
+  {
+    executorService = Executors.newSingleThreadExecutor();
+  }
+
+  @After
+  public void tearDown()
+  {
+    executorService.shutdown();
+  }
+
+  @Test
+  public void testBlockingQueriesCount()
+  {
+    BlockingPool<ByteBuffer> pool = new DefaultBlockingPool(() -> 
ByteBuffer.allocate(1024), 1);
+    MergeBufferPoolMonitor monitor = new MergeBufferPoolMonitor(pool);
+
+    CountDownLatch latch = new CountDownLatch(1);
+    executorService.submit(() -> {
+      latch.countDown();
+      pool.takeBatch(10);
+    });
+
+    try {
+      // the latch returns from await() guarantees the above lamda to take 
buffer from the pool starting to run in the
+      // executorService thread
+      latch.await();
+
+      // give 1 sec for pool.takeBatch to run and blocking at the pool
+      Thread.sleep(1000);
+

Review Comment:
   Tests relying on sleep are slower than necessary and flaky. Can we instead 
use an already existing implementation to test the monitor?
   If we do decide to go with the test due to the lack of another method,  
please add a timeout `@Test(timeout = 2_000L)` just in case it starts running 
over the sleep value. 
    



##########
processing/src/main/java/org/apache/druid/collections/BlockingPool.java:
##########
@@ -40,8 +39,14 @@ public interface BlockingPool<T>
    * Take resources from the pool, waiting if necessary until the elements of 
the given number become available.
    *
    * @param elementNum number of resources to take
-   *
    * @return a list of resource holders. An empty list is returned if {@code 
elementNum} resources aren't available.
    */
   List<ReferenceCountingResourceHolder<T>> takeBatch(int elementNum);
+
+  /**
+   * Return the count of pending queries blocking in this queue for merge 
buffers

Review Comment:
   ```suggestion
      * Return the count of pending queries waiting for merge buffers
   ```



##########
processing/src/test/java/org/apache/druid/java/util/metrics/MergeBufferPoolMonitorTest.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.java.util.metrics;
+
+import org.apache.druid.collections.BlockingPool;
+import org.apache.druid.collections.DefaultBlockingPool;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class MergeBufferPoolMonitorTest
+{
+  private ExecutorService executorService;
+
+  @Before
+  public void setUp()
+  {
+    executorService = Executors.newSingleThreadExecutor();
+  }
+
+  @After
+  public void tearDown()
+  {
+    executorService.shutdown();
+  }
+
+  @Test
+  public void testBlockingQueriesCount()
+  {
+    BlockingPool<ByteBuffer> pool = new DefaultBlockingPool(() -> 
ByteBuffer.allocate(1024), 1);
+    MergeBufferPoolMonitor monitor = new MergeBufferPoolMonitor(pool);
+
+    CountDownLatch latch = new CountDownLatch(1);
+    executorService.submit(() -> {
+      latch.countDown();
+      pool.takeBatch(10);
+    });
+
+    try {
+      // the latch returns from await() guarantees the above lamda to take 
buffer from the pool starting to run in the
+      // executorService thread
+      latch.await();
+
+      // give 1 sec for pool.takeBatch to run and blocking at the pool

Review Comment:
   ```suggestion
         // give 1 sec for pool.takeBatch to run and block the pool
   ```



-- 
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