gortiz commented on code in PR #15143:
URL: https://github.com/apache/pinot/pull/15143#discussion_r1977302043


##########
pinot-spi/src/main/java/org/apache/pinot/spi/executor/HardLimitExecutor.java:
##########
@@ -0,0 +1,94 @@
+/**
+ * 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.pinot.spi.executor;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+/**
+ * An Executor that allows a maximum of tasks running at the same time, 
rejecting immediately any excess.
+ */
+public class HardLimitExecutor extends DecoratorExecutorService {
+
+  private final AtomicInteger _running;
+  private final int _max;
+
+  public HardLimitExecutor(int max, ExecutorService executorService) {
+    super(executorService);
+    _running = new AtomicInteger(0);
+    _max = max;
+  }
+
+  /**
+   * Returns the hard limit of the number of threads that can be used by the 
multi-stage executor.
+   * @param config Pinot configuration
+   * @return hard limit of the number of threads that can be used by the 
multi-stage executor (no hard limit if <= 0)
+   */
+  public static int getMultiStageExecutorHardLimit(PinotConfiguration config) {
+    try {
+      return Integer.parseInt(config.getProperty(
+          
CommonConstants.Helix.CONFIG_OF_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS,
+          
CommonConstants.Helix.DEFAULT_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS
+      )) * Integer.parseInt(config.getProperty(
+          
CommonConstants.Helix.CONFIG_OF_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_HARDLIMIT_FACTOR,
+          
CommonConstants.Helix.DEFAULT_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_HARDLIMIT_FACTOR
+      ));
+    } catch (NumberFormatException e) {
+      return 
Integer.parseInt(CommonConstants.Helix.DEFAULT_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS);
+    }
+  }
+
+  protected void checkTaskAllowed() {
+    if (_running.get() >= _max) {
+      throw new IllegalStateException("Tasks limit exceeded.");
+    }
+  }
+
+  @Override
+  protected <T> Callable<T> decorate(Callable<T> task) {
+    checkTaskAllowed();

Review Comment:
   If we only check inside the task, more tasks than expected can be scheduled. 
Although they would fail once they start, they must consume that thread. 
Imagine we are above the limit and receive 10 concurrent requests. All 10 will 
be allowed to start and allocate their threads just to fail once they actually 
start to execute the task code.
   
   If we only check on the decorate method, we can have more tasks running than 
expected. Imagine we are 1 permit below the limit and we receive 10 requests. 
The 10 will start and then increase the limit above the expected value.



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to