Jackie-Jiang commented on code in PR #14894: URL: https://github.com/apache/pinot/pull/14894#discussion_r1929427439
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { Review Comment: Consider naming it `isServingQueries` to be more specific ########## pinot-spi/src/main/java/org/apache/pinot/spi/config/provider/PinotClusterConfigProvider.java: ########## @@ -0,0 +1,76 @@ +/** + * 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.config.provider; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + + +/** + * Interface for ZK cluster config providers. Will be registered with Helix to listen on cluster config changes and + * will propagate changes to all registered listeners + */ +public interface PinotClusterConfigProvider { + /** + * Get the cluster configs + * @return map of cluster configs + */ + Map<String, String> getClusterConfigs(); + + /** + * Register cluster config change listener + * @param clusterConfigChangeListener change listener to be registered to obtain cluster config changes + * @return returns 'true' if the registration was successful + */ + boolean registerClusterConfigChangeListener(PinotClusterConfigChangeListener clusterConfigChangeListener); + + /** + * Calculates the set of keys that changed in ZK cluster configs between the old and new + * @param oldProperties map of previously cached ZK cluster configs + * @param newProperties map of newly fetched ZK cluster configs + * @return set of changed (added/deleted/updated) cluster config keys + */ + default Set<String> getChangedProperties(Map<String, String> oldProperties, Map<String, String> newProperties) { + if (oldProperties == null || oldProperties.isEmpty()) { + return newProperties.keySet(); + } + + if (newProperties == null || newProperties.isEmpty()) { + return oldProperties.keySet(); + } + + Set<String> changedProperties = new HashSet<>(); + // Add all properties that are newly added or whose value changed + for (Map.Entry<String, String> entry : newProperties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!oldProperties.containsKey(key) || !oldProperties.get(key).equals(value)) { Review Comment: You can save a map lookup by: ```suggestion if (!value.equals(oldProperties.get(key))) { ``` ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { Review Comment: Consider changing the method name to `startServingQueries` to be more specific ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with empty clusterConfigs"); + return; + } + + if (changedConfigs == null || changedConfigs.isEmpty()) { Review Comment: (minor) This is equivalent to `CollectionUtils.isEmpty(changedConfigs)` ########## pinot-spi/src/main/java/org/apache/pinot/spi/config/provider/PinotClusterConfigProvider.java: ########## @@ -0,0 +1,76 @@ +/** + * 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.config.provider; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + + +/** + * Interface for ZK cluster config providers. Will be registered with Helix to listen on cluster config changes and + * will propagate changes to all registered listeners + */ +public interface PinotClusterConfigProvider { + /** + * Get the cluster configs + * @return map of cluster configs + */ + Map<String, String> getClusterConfigs(); + + /** + * Register cluster config change listener + * @param clusterConfigChangeListener change listener to be registered to obtain cluster config changes + * @return returns 'true' if the registration was successful + */ + boolean registerClusterConfigChangeListener(PinotClusterConfigChangeListener clusterConfigChangeListener); + + /** + * Calculates the set of keys that changed in ZK cluster configs between the old and new + * @param oldProperties map of previously cached ZK cluster configs + * @param newProperties map of newly fetched ZK cluster configs + * @return set of changed (added/deleted/updated) cluster config keys + */ + default Set<String> getChangedProperties(Map<String, String> oldProperties, Map<String, String> newProperties) { + if (oldProperties == null || oldProperties.isEmpty()) { + return newProperties.keySet(); + } + + if (newProperties == null || newProperties.isEmpty()) { + return oldProperties.keySet(); + } + + Set<String> changedProperties = new HashSet<>(); + // Add all properties that are newly added or whose value changed + for (Map.Entry<String, String> entry : newProperties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!oldProperties.containsKey(key) || !oldProperties.get(key).equals(value)) { + changedProperties.add(key); + } + } + + // Add all properties that were deleted + Set<String> originalPropertyKeys = oldProperties.keySet(); + originalPropertyKeys.removeAll(changedProperties); Review Comment: Also we probably need to make a copy of it. This will directly change the original property keys, which could be still in use ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with empty clusterConfigs"); + return; + } + + if (changedConfigs == null || changedConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with unchanged clusterConfigs"); + return; + } + + LOGGER.info("Updating SegmentPreprocessThrottler configs with latest clusterConfigs"); + handleMaxPreprocessConcurrencyChange(changedConfigs, clusterConfigs); + handleMaxPreprocessConcurrencyBeforeServingQueriesChange(changedConfigs, clusterConfigs); + LOGGER.info("Updated SegmentPreprocessThrottler configs with latest clusterConfigs, total permits: {}", + totalPermits()); + } + + private void handleMaxPreprocessConcurrencyChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (!changedConfigs.contains(CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM)) { + LOGGER.info("changedConfigs list indicates maxPreprocessConcurrency was not updated, skipping updates"); + return; + } + + String configName = CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String defaultConfigValue = CommonConstants.Helix.DEFAULT_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String maxParallelSegmentPreprocessesStr = clusterConfigs.getOrDefault(configName, defaultConfigValue); + int maxPreprocessConcurrency = Integer.parseInt(maxParallelSegmentPreprocessesStr); Review Comment: Consider putting it into a try-catch along with the value check ########## pinot-spi/src/main/java/org/apache/pinot/spi/config/provider/PinotClusterConfigProvider.java: ########## @@ -0,0 +1,76 @@ +/** + * 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.config.provider; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + + +/** + * Interface for ZK cluster config providers. Will be registered with Helix to listen on cluster config changes and + * will propagate changes to all registered listeners + */ +public interface PinotClusterConfigProvider { + /** + * Get the cluster configs + * @return map of cluster configs + */ + Map<String, String> getClusterConfigs(); + + /** + * Register cluster config change listener + * @param clusterConfigChangeListener change listener to be registered to obtain cluster config changes + * @return returns 'true' if the registration was successful + */ + boolean registerClusterConfigChangeListener(PinotClusterConfigChangeListener clusterConfigChangeListener); + + /** + * Calculates the set of keys that changed in ZK cluster configs between the old and new + * @param oldProperties map of previously cached ZK cluster configs + * @param newProperties map of newly fetched ZK cluster configs + * @return set of changed (added/deleted/updated) cluster config keys + */ + default Set<String> getChangedProperties(Map<String, String> oldProperties, Map<String, String> newProperties) { + if (oldProperties == null || oldProperties.isEmpty()) { Review Comment: Is it nullable? If so, we should annotate the parameter, and we need null handling for `newProperties` or it will NPE ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with empty clusterConfigs"); + return; + } + + if (changedConfigs == null || changedConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with unchanged clusterConfigs"); + return; + } + + LOGGER.info("Updating SegmentPreprocessThrottler configs with latest clusterConfigs"); + handleMaxPreprocessConcurrencyChange(changedConfigs, clusterConfigs); + handleMaxPreprocessConcurrencyBeforeServingQueriesChange(changedConfigs, clusterConfigs); + LOGGER.info("Updated SegmentPreprocessThrottler configs with latest clusterConfigs, total permits: {}", + totalPermits()); + } + + private void handleMaxPreprocessConcurrencyChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (!changedConfigs.contains(CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM)) { + LOGGER.info("changedConfigs list indicates maxPreprocessConcurrency was not updated, skipping updates"); + return; + } + + String configName = CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String defaultConfigValue = CommonConstants.Helix.DEFAULT_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String maxParallelSegmentPreprocessesStr = clusterConfigs.getOrDefault(configName, defaultConfigValue); + int maxPreprocessConcurrency = Integer.parseInt(maxParallelSegmentPreprocessesStr); + + if (maxPreprocessConcurrency == _maxPreprocessConcurrency) { + LOGGER.info("No ZK update for maxPreprocessConcurrency {}, total permits: {}", _maxPreprocessConcurrency, + totalPermits()); + return; + } + + if (maxPreprocessConcurrency <= 0) { + LOGGER.warn("Invalid maxPreprocessConcurrency set: {}, not making change, fix config and try again", + maxPreprocessConcurrency); + return; + } + + LOGGER.info("Updated maxPreprocessConcurrency from: {} to: {}", _maxPreprocessConcurrency, Review Comment: Move this log after actually set the permits, in case the `setPermits` failed ########## pinot-spi/src/main/java/org/apache/pinot/spi/config/provider/PinotClusterConfigProvider.java: ########## @@ -0,0 +1,76 @@ +/** + * 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.config.provider; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + + +/** + * Interface for ZK cluster config providers. Will be registered with Helix to listen on cluster config changes and + * will propagate changes to all registered listeners + */ +public interface PinotClusterConfigProvider { + /** + * Get the cluster configs + * @return map of cluster configs + */ + Map<String, String> getClusterConfigs(); + + /** + * Register cluster config change listener + * @param clusterConfigChangeListener change listener to be registered to obtain cluster config changes + * @return returns 'true' if the registration was successful + */ + boolean registerClusterConfigChangeListener(PinotClusterConfigChangeListener clusterConfigChangeListener); + + /** + * Calculates the set of keys that changed in ZK cluster configs between the old and new + * @param oldProperties map of previously cached ZK cluster configs + * @param newProperties map of newly fetched ZK cluster configs + * @return set of changed (added/deleted/updated) cluster config keys + */ + default Set<String> getChangedProperties(Map<String, String> oldProperties, Map<String, String> newProperties) { + if (oldProperties == null || oldProperties.isEmpty()) { + return newProperties.keySet(); + } + + if (newProperties == null || newProperties.isEmpty()) { + return oldProperties.keySet(); + } + + Set<String> changedProperties = new HashSet<>(); + // Add all properties that are newly added or whose value changed + for (Map.Entry<String, String> entry : newProperties.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!oldProperties.containsKey(key) || !oldProperties.get(key).equals(value)) { + changedProperties.add(key); + } + } + + // Add all properties that were deleted + Set<String> originalPropertyKeys = oldProperties.keySet(); + originalPropertyKeys.removeAll(changedProperties); Review Comment: Should this be: ```suggestion originalPropertyKeys.removeAll(newProperties.entrySet()); ``` ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with empty clusterConfigs"); + return; + } + + if (changedConfigs == null || changedConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with unchanged clusterConfigs"); + return; + } + + LOGGER.info("Updating SegmentPreprocessThrottler configs with latest clusterConfigs"); + handleMaxPreprocessConcurrencyChange(changedConfigs, clusterConfigs); + handleMaxPreprocessConcurrencyBeforeServingQueriesChange(changedConfigs, clusterConfigs); + LOGGER.info("Updated SegmentPreprocessThrottler configs with latest clusterConfigs, total permits: {}", + totalPermits()); + } + + private void handleMaxPreprocessConcurrencyChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (!changedConfigs.contains(CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM)) { + LOGGER.info("changedConfigs list indicates maxPreprocessConcurrency was not updated, skipping updates"); + return; + } + + String configName = CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String defaultConfigValue = CommonConstants.Helix.DEFAULT_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String maxParallelSegmentPreprocessesStr = clusterConfigs.getOrDefault(configName, defaultConfigValue); + int maxPreprocessConcurrency = Integer.parseInt(maxParallelSegmentPreprocessesStr); + + if (maxPreprocessConcurrency == _maxPreprocessConcurrency) { + LOGGER.info("No ZK update for maxPreprocessConcurrency {}, total permits: {}", _maxPreprocessConcurrency, + totalPermits()); + return; + } + + if (maxPreprocessConcurrency <= 0) { + LOGGER.warn("Invalid maxPreprocessConcurrency set: {}, not making change, fix config and try again", + maxPreprocessConcurrency); + return; + } + + LOGGER.info("Updated maxPreprocessConcurrency from: {} to: {}", _maxPreprocessConcurrency, + maxPreprocessConcurrency); + _maxPreprocessConcurrency = maxPreprocessConcurrency; + + if (_relaxThrottling) { + LOGGER.warn("Reset throttling hasn't been called yet, not updating the permits with maxPreprocessConcurrency"); + return; + } + _semaphore.setPermits(_maxPreprocessConcurrency); + } + + private void handleMaxPreprocessConcurrencyBeforeServingQueriesChange(Set<String> changedConfigs, + Map<String, String> clusterConfigs) { + if (!changedConfigs.contains( + CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM_BEFORE_SERVING_QUERIES)) { + LOGGER.info("changedConfigs list indicates maxPreprocessConcurrencyBeforeServingQueries was not updated, " + + "skipping updates"); + return; + } + + String configName = CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM_BEFORE_SERVING_QUERIES; + String defaultConfigValue = CommonConstants.Helix.DEFAULT_MAX_SEGMENT_PREPROCESS_PARALLELISM_BEFORE_SERVING_QUERIES; + String maxParallelSegmentPreprocessesBeforeServingQueriesStr = + clusterConfigs.getOrDefault(configName, defaultConfigValue); + int maxPreprocessConcurrencyBeforeServingQueries = + Integer.parseInt(maxParallelSegmentPreprocessesBeforeServingQueriesStr); + + if (maxPreprocessConcurrencyBeforeServingQueries == _maxPreprocessConcurrencyBeforeServingQueries) { + LOGGER.info("No ZK update for maxPreprocessConcurrencyBeforeServingQueries {}, total permits: {}", + _maxPreprocessConcurrencyBeforeServingQueries, totalPermits()); + return; + } + + if (maxPreprocessConcurrencyBeforeServingQueries <= 0) { + LOGGER.warn("Invalid maxPreprocessConcurrencyBeforeServingQueries set: {}, not making change, fix config " + + "and try again", maxPreprocessConcurrencyBeforeServingQueries); + return; + } + + LOGGER.info("Updated maxPreprocessConcurrencyBeforeServingQueries from: {} to: {}", + _maxPreprocessConcurrencyBeforeServingQueries, maxPreprocessConcurrencyBeforeServingQueries); + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + if (_relaxThrottling) { + LOGGER.warn("maxPreprocessConcurrencyBeforeServingQueries was updated before reset throttling was called, " Review Comment: This shouldn't be a warning. We can log an info if this is changed after serving queries ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with empty clusterConfigs"); + return; + } + + if (changedConfigs == null || changedConfigs.isEmpty()) { + LOGGER.info("Skip updating SegmentPreprocessThrottler configs with unchanged clusterConfigs"); + return; + } + + LOGGER.info("Updating SegmentPreprocessThrottler configs with latest clusterConfigs"); + handleMaxPreprocessConcurrencyChange(changedConfigs, clusterConfigs); + handleMaxPreprocessConcurrencyBeforeServingQueriesChange(changedConfigs, clusterConfigs); + LOGGER.info("Updated SegmentPreprocessThrottler configs with latest clusterConfigs, total permits: {}", + totalPermits()); + } + + private void handleMaxPreprocessConcurrencyChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (!changedConfigs.contains(CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM)) { + LOGGER.info("changedConfigs list indicates maxPreprocessConcurrency was not updated, skipping updates"); + return; + } + + String configName = CommonConstants.Helix.CONFIG_OF_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String defaultConfigValue = CommonConstants.Helix.DEFAULT_MAX_SEGMENT_PREPROCESS_PARALLELISM; + String maxParallelSegmentPreprocessesStr = clusterConfigs.getOrDefault(configName, defaultConfigValue); + int maxPreprocessConcurrency = Integer.parseInt(maxParallelSegmentPreprocessesStr); + + if (maxPreprocessConcurrency == _maxPreprocessConcurrency) { + LOGGER.info("No ZK update for maxPreprocessConcurrency {}, total permits: {}", _maxPreprocessConcurrency, + totalPermits()); + return; + } + + if (maxPreprocessConcurrency <= 0) { + LOGGER.warn("Invalid maxPreprocessConcurrency set: {}, not making change, fix config and try again", + maxPreprocessConcurrency); + return; + } + + LOGGER.info("Updated maxPreprocessConcurrency from: {} to: {}", _maxPreprocessConcurrency, + maxPreprocessConcurrency); + _maxPreprocessConcurrency = maxPreprocessConcurrency; + + if (_relaxThrottling) { + LOGGER.warn("Reset throttling hasn't been called yet, not updating the permits with maxPreprocessConcurrency"); Review Comment: This should be an info as this is regular case ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentPreprocessThrottler.java: ########## @@ -0,0 +1,212 @@ +/** + * 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.segment.local.utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.concurrency.AdjustableSemaphore; +import org.apache.pinot.spi.config.provider.PinotClusterConfigChangeListener; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Used to throttle the total concurrent index rebuilds that can happen on a given Pinot server. + * Code paths that do no need to rebuild the index or which don't happen on the server need not utilize this throttler. + */ +public class SegmentPreprocessThrottler implements PinotClusterConfigChangeListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentPreprocessThrottler.class); + + /** + * _maxPreprocessConcurrency and _maxConcurrentPreprocessesBeforeServingQueries must be >= 0. To effectively disable + * throttling, this can be set to a very high value + */ + private int _maxPreprocessConcurrency; + private int _maxPreprocessConcurrencyBeforeServingQueries; + private boolean _relaxThrottling; + private final AdjustableSemaphore _semaphore; + + /** + * @param maxPreprocessConcurrency configured preprocessing concurrency + * @param maxPreprocessConcurrencyBeforeServingQueries configured preprocessing concurrency before serving queries + * @param relaxThrottling whether to relax throttling prior to serving queries + */ + public SegmentPreprocessThrottler(int maxPreprocessConcurrency, int maxPreprocessConcurrencyBeforeServingQueries, + boolean relaxThrottling) { + LOGGER.info("Initializing SegmentPreprocessThrottler, maxPreprocessConcurrency: {}, " + + "maxPreprocessConcurrencyBeforeServingQueries: {}, relaxThrottling: {}", + maxPreprocessConcurrency, maxPreprocessConcurrencyBeforeServingQueries, relaxThrottling); + Preconditions.checkArgument(maxPreprocessConcurrency > 0, + "Max preprocess parallelism must be > 0, but found to be: " + maxPreprocessConcurrency); + Preconditions.checkArgument(maxPreprocessConcurrencyBeforeServingQueries > 0, + "Max preprocess parallelism before serving queries must be > 0, but found to be: " + + maxPreprocessConcurrencyBeforeServingQueries); + + _maxPreprocessConcurrency = maxPreprocessConcurrency; + _maxPreprocessConcurrencyBeforeServingQueries = maxPreprocessConcurrencyBeforeServingQueries; + _relaxThrottling = relaxThrottling; + + // maxConcurrentPreprocessesBeforeServingQueries is only used prior to serving queries and once the server is + // ready to serve queries this is not used again. Thus, it is safe to only pick up this configuration during + // server startup. There is no need to allow updates to this via the ZK CLUSTER config handler + int relaxThrottlingThreshold = Math.max(_maxPreprocessConcurrency, _maxPreprocessConcurrencyBeforeServingQueries); + int preprocessConcurrency = _maxPreprocessConcurrency; + if (relaxThrottling) { + preprocessConcurrency = relaxThrottlingThreshold; + LOGGER.info("Relax throttling enabled, setting preprocess concurrency to: {}", preprocessConcurrency); + } + _semaphore = new AdjustableSemaphore(preprocessConcurrency, true); + LOGGER.info("Created semaphore with total permits: {}, available permits: {}", totalPermits(), + availablePermits()); + } + + public synchronized void resetThrottling() { + LOGGER.info("Reset throttling threshold for segment preprocess concurrency, total permits: {}, available " + + "permits: {}", totalPermits(), availablePermits()); + _relaxThrottling = false; + _semaphore.setPermits(_maxPreprocessConcurrency); + LOGGER.info("Reset throttling completed, new concurrency: {}, total permits: {}, available permits: {}", + _maxPreprocessConcurrency, totalPermits(), availablePermits()); + } + + @Override + public synchronized void onChange(Set<String> changedConfigs, Map<String, String> clusterConfigs) { + if (clusterConfigs == null || clusterConfigs.isEmpty()) { Review Comment: We shouldn't check for empty cluster configs though. We should only check changed configs because user might have removed the config -- 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]
