sajjad-moradi commented on a change in pull request #6291: URL: https://github.com/apache/pinot/pull/6291#discussion_r795288541
########## File path: pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java ########## @@ -0,0 +1,119 @@ +/** + * 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.core.data.manager.realtime; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.util.concurrent.RateLimiter; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import org.apache.pinot.spi.stream.StreamConfig; +import org.apache.pinot.spi.stream.StreamConsumerFactory; +import org.apache.pinot.spi.stream.StreamConsumerFactoryProvider; +import org.apache.pinot.spi.stream.StreamMetadataProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class is responsible for creating realtime consumption rate limiters. The rate limit, specified in + * StreamConfig of table config, is for the entire topic. The effective rate limit for each partition is simply the + * specified rate limit divided by the partition count. + * This class leverages a cache for storing partition count for different topics as retrieving partition count from + * stream is a bit expensive and also the same count will be used of all partition consumers of the same topic. + */ +public class RealtimeConsumptionRateManager { + private static final Logger LOGGER = LoggerFactory.getLogger(RealtimeConsumptionRateManager.class); + private static final RealtimeConsumptionRateManager INSTANCE = new RealtimeConsumptionRateManager(); + private static final int CACHE_ENTRY_EXPIRATION_TIME_IN_MINUTES = 10; + + // stream config object is required for fetching the partition count from the stream + private final LoadingCache<StreamConfig, Integer> _streamConfigToTopicPartitionCountMap = buildCache(); + private volatile boolean _isThrottlingAllowed = false; + + private RealtimeConsumptionRateManager() { + } + + public static RealtimeConsumptionRateManager getInstance() { + return INSTANCE; + } + + public void enableThrottling() { + _isThrottlingAllowed = true; + } + + public ConsumptionRateLimiter createRateLimiter(StreamConfig streamConfig) { + if (!streamConfig.getTopicConsumptionRateLimit().isPresent()) { + return NOOP_RATE_LIMITER; + } + int partitionCount; + try { + partitionCount = _streamConfigToTopicPartitionCountMap.get(streamConfig); + } catch (ExecutionException e) { + // Exception here means for some reason, partition count cannot be fetched from stream! + throw new RuntimeException(e); + } + double topicRateLimit = streamConfig.getTopicConsumptionRateLimit().get(); + double partitionRateLimit = topicRateLimit / partitionCount; + LOGGER.info("A rate limiter is setup for topic {} with rate limit: {} (topic rate limit: {}, partition count: {})", Review comment: Sure, just added it. -- 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]
