sajjad-moradi commented on a change in pull request #6291:
URL: https://github.com/apache/incubator-pinot/pull/6291#discussion_r537125742



##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.annotations.VisibleForTesting;
+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.PartitionCountFetcher;
+import org.apache.pinot.spi.stream.StreamConfig;
+import org.apache.pinot.spi.utils.retry.RetryPolicies;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class is responsible for creating realtime consumption rate limiters. 
If rate limiter is used for
+ * multi-partition topics, the provided rate in StreamConfig is divided by 
partition count. This class leverages a
+ * cache for storing partition count for different topics as retrieving 
partition count from Kafka 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;
+
+  @VisibleForTesting
+  LoadingCache<StreamConfig, Integer> _streamConfigToTopicPartitionCountMap = 
buildCache();
+  private boolean _isThrottlingAllowed = false;
+
+  private RealtimeConsumptionRateManager() {
+  }
+
+  public static RealtimeConsumptionRateManager getInstance() {
+    return INSTANCE;
+  }
+
+  public void enableThrottling() {
+    _isThrottlingAllowed = true;
+  }
+
+  public ConsumptionRateLimiter 
createRateLimiterForSinglePartitionTopic(StreamConfig streamConfig) {
+    return createRateLimiter(streamConfig, false);
+  }
+
+  public ConsumptionRateLimiter 
createRateLimiterForMultiPartitionTopic(StreamConfig streamConfig) {
+    return createRateLimiter(streamConfig, true);
+  }
+
+  private ConsumptionRateLimiter createRateLimiter(StreamConfig streamConfig, 
boolean multiPartitionTopic) {
+    if (!streamConfig.getTopicConsumptionRateLimit().isPresent()) {
+      return NOOP_RATE_LIMITER;
+    }
+    int partitionCount = 1;
+    if (multiPartitionTopic) {
+      try {
+        partitionCount = 
_streamConfigToTopicPartitionCountMap.get(streamConfig);

Review comment:
       In the load method of the cache, we're using the existing class 
PartitionCountFetcher which requires the whole StreamConfig object to be 
provided. That's why the whole object is considered as the key for the cache.




----------------------------------------------------------------
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.

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