albertobastos commented on code in PR #15143: URL: https://github.com/apache/pinot/pull/15143#discussion_r1975907262
########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java: ########## @@ -177,6 +178,12 @@ public void init(PinotConfiguration config, InstanceDataManager instanceDataMana _executorService = ExecutorServiceUtils.create(config, Server.MULTISTAGE_EXECUTOR_CONFIG_PREFIX, "query-runner-on-" + port, Server.DEFAULT_MULTISTAGE_EXECUTOR_TYPE); + + int maxThreads = config.getProperty(Server.CONFIG_OF_MSE_THREADS_HARD_LIMIT, Server.DEFAULT_MSE_THREADS_HARD_LIMIT); + if (maxThreads > 0) { + _executorService = new MaxTasksExecutor(maxThreads, _executorService); + } Review Comment: After some second though I agree with @gortiz, reusing the Provider API for this task gets kind of messy once we want to make it "derived" from the already existing pinot.beta.* parameter. a974159d74a63116ef234b0617ca60aaac5b042f ########## pinot-core/src/main/java/org/apache/pinot/core/query/executor/ServerQueryExecutorV1Impl.java: ########## @@ -142,25 +140,8 @@ public synchronized void shutDown() { @Override public InstanceResponseBlock execute(ServerQueryRequest queryRequest, ExecutorService executorService, @Nullable ResultsBlockStreamer streamer) { - MdcExecutor mdcExecutor = new MdcExecutor(executorService) { - @Override - protected boolean alreadyRegistered() { - return LoggerConstants.QUERY_ID_KEY.isRegistered(); - } - - @Override - protected void registerInMdc() { - queryRequest.registerInMdc(); - } - - @Override - protected void unregisterFromMdc() { - queryRequest.unregisterFromMdc(); - } - }; Review Comment: My bad. After realising the hard limit should not be applied in this class, I removed too much code. a974159d74a63116ef234b0617ca60aaac5b042f ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/server/ServerPlanRequestUtils.java: ########## @@ -106,7 +106,7 @@ public static OpChain compileLeafStage(OpChainExecutionContext executionContext, BiConsumer<PlanNode, MultiStageOperator> relationConsumer, boolean explain) { long queryArrivalTimeMs = System.currentTimeMillis(); - MdcExecutor mdcExecutor = new MdcExecutor(executorService) { + ExecutorService decoratedExecutor = new MdcExecutor(executorService) { Review Comment: You guessed it... it made sense before, but no longer does. Undone. a974159d74a63116ef234b0617ca60aaac5b042f ########## pinot-spi/src/main/java/org/apache/pinot/spi/executor/ExecutorServiceUtils.java: ########## @@ -132,4 +133,14 @@ public static void close(ExecutorService executorService, long terminationMillis throw new RuntimeException(e); } } + + /** + * 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) { + return config.getProperty(CommonConstants.Helix.CONFIG_OF_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS, 0) + * CommonConstants.Helix.MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS_HARDLIMIT_FACTOR; + } Review Comment: Yeah, I agree, moved it to the Executor itself. a974159d74a63116ef234b0617ca60aaac5b042f ########## pinot-common/src/main/java/org/apache/pinot/common/utils/HardLimitExecutorServicePlugin.java: ########## @@ -0,0 +1,63 @@ +/** + * 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.common.utils; + +import com.google.auto.service.AutoService; +import java.util.concurrent.ExecutorService; +import org.apache.pinot.spi.executor.ExecutorServicePlugin; +import org.apache.pinot.spi.executor.ExecutorServiceProvider; +import org.apache.pinot.spi.executor.ExecutorServiceUtils; +import org.apache.pinot.spi.executor.HardLimitExecutor; +import org.apache.pinot.spi.utils.CommonConstants; + + +/** + * This is the plugin for the cached executor service with a hard limit for the amount of created threads. + * + * The provider included in this plugin creates cached thread pools, which are the recommended executor service for + * cases where the tasks are short-lived and not CPU bound. + * + * If that is not the case, this executor may create a large number of threads that will be competing for CPU resources, + * which may lead to performance degradation and even system instability. + * In that case {@link FixedExecutorServicePlugin} could be used, but it may need changes to the code to avoid + * deadlocks. Deployments using Java 21 or above could consider using a virtual thread executor service plugin. + * + * @see org.apache.pinot.spi.executor.ExecutorServiceUtils + */ +@AutoService(ExecutorServicePlugin.class) +public class HardLimitExecutorServicePlugin implements ExecutorServicePlugin { + @Override + public String id() { + return "hardlimit"; + } + + @Override + public ExecutorServiceProvider provider() { + return (conf, confPrefix, baseName) -> { + int limit = ExecutorServiceUtils.getMultiStageExecutorHardLimit(conf); + ExecutorService executorService = ExecutorServiceUtils.create( + conf, CommonConstants.Server.MULTISTAGE_EXECUTOR_CONFIG_PREFIX, baseName, + CommonConstants.Server.DEFAULT_MULTISTAGE_EXECUTOR_TYPE); + if (limit > 0) { + executorService = new HardLimitExecutor(limit, executorService); + } + return executorService; + }; + } Review Comment: Is no longer needed once we decide to not use the Provider API for that. a974159d74a63116ef234b0617ca60aaac5b042f ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/QueryRunner.java: ########## @@ -174,9 +174,14 @@ public void init(PinotConfiguration config, InstanceDataManager instanceDataMana String joinOverflowModeStr = config.getProperty(CommonConstants.MultiStageQueryRunner.KEY_OF_JOIN_OVERFLOW_MODE); _joinOverflowMode = joinOverflowModeStr != null ? JoinOverFlowMode.valueOf(joinOverflowModeStr) : null; - _executorService = - ExecutorServiceUtils.create(config, Server.MULTISTAGE_EXECUTOR_CONFIG_PREFIX, "query-runner-on-" + port, - Server.DEFAULT_MULTISTAGE_EXECUTOR_TYPE); + int hardLimit = ExecutorServiceUtils.getMultiStageExecutorHardLimit(config); + String executorType = Server.DEFAULT_MULTISTAGE_EXECUTOR_TYPE; + if (hardLimit > 0) { + executorType = "hardlimit"; + } + _executorService = ExecutorServiceUtils.create(config, Server.MULTISTAGE_EXECUTOR_CONFIG_PREFIX, + "query-runner-on-" + port, executorType); Review Comment: Yep, definitely I overstepped trying to use the Provider API for that. a974159d74a63116ef234b0617ca60aaac5b042f -- 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