albertobastos commented on code in PR #15143: URL: https://github.com/apache/pinot/pull/15143#discussion_r1977026454
########## pinot-core/src/main/java/org/apache/pinot/core/query/executor/ServerQueryExecutorV1Impl.java: ########## @@ -125,8 +125,8 @@ public synchronized void init(PinotConfiguration config, InstanceDataManager ins _planMaker.init(config); _defaultTimeoutMs = config.getProperty(Server.TIMEOUT, Server.DEFAULT_QUERY_EXECUTOR_TIMEOUT_MS); _enablePrefetch = Boolean.parseBoolean(config.getProperty(ENABLE_PREFETCH)); - LOGGER.info("Initialized query executor with defaultTimeoutMs: {}, enablePrefetch: {}", _defaultTimeoutMs, - _enablePrefetch); + LOGGER.info("Initialized query executor with defaultTimeoutMs: {}, enablePrefetch: {}", + _defaultTimeoutMs, _enablePrefetch); Review Comment: I have the rules properly set in my IDE, so not sure why this keeps happening. Have enforced them for the files changed by this PR, but I'm afraid there's a lot of files in the repository that should be reviewed as well (I tried to, but don't want my PR to pass from 6 to +400 files changed). ########## pinot-spi/src/main/java/org/apache/pinot/spi/utils/CommonConstants.java: ########## @@ -248,6 +248,9 @@ public static class Instance { public static final String CONFIG_OF_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS = "pinot.beta.multistage.engine.max.server.query.threads"; public static final String DEFAULT_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_THREADS = "-1"; + public static final String CONFIG_OF_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_HARDLIMIT_FACTOR = + "pinot.beta.multistage.engine.max.server.query.threads.hardlimit.factor"; + public static final String DEFAULT_MULTI_STAGE_ENGINE_MAX_SERVER_QUERY_HARDLIMIT_FACTOR = "4"; Review Comment: I see your point, but kind of resist to not having a way to make that factor configurable. I'd rather see how this performs in real scenarios and have that option in case we need to fine-tune it. So far, I added an (ugly) check to avoid setting a hard limit in case thread limit is already non-positive. ########## 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: It is usually a good practice to do so, according to some readings about Java Concurrency to avoid spurious thread wake ups... although it is specially recommended when using condition locks, so maybe in this particular scenario is not that helpful. But I'd say better safe than sorry. ########## 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(); + return () -> { + checkTaskAllowed(); + try { + _running.getAndIncrement(); Review Comment: Yeah, good catch. -- 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