mcvsubbu commented on a change in pull request #6291:
URL: https://github.com/apache/pinot/pull/6291#discussion_r792955803



##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java
##########
@@ -0,0 +1,127 @@
+/**
+ * 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. 
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;
+
+  private final LoadingCache<StreamConfig, Integer> 
_streamConfigToTopicPartitionCountMap = buildCache();
+  private boolean _isThrottlingAllowed = false;

Review comment:
       volatile?

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java
##########
@@ -0,0 +1,127 @@
+/**
+ * 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. 
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;
+
+  private final LoadingCache<StreamConfig, Integer> 
_streamConfigToTopicPartitionCountMap = buildCache();

Review comment:
       Should topic name suffice as a key? Or, do we want the entire 
streamconfig just to support use cases in which the same server (for different 
pinot tables)  may consume the same topic name from different endpoints (or 
different streams) and therefore may have different characteristics? It may be 
good to note as a comment, at least.

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java
##########
@@ -0,0 +1,127 @@
+/**
+ * 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. 
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;
+
+  private final 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);
+      } catch (ExecutionException e) {
+        // Exception here means that for some reason, partition count cannot 
be retrieved from Kafka broker!
+        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: {})",
+        streamConfig.getTopicName(), partitionRateLimit, topicRateLimit, 
partitionCount);
+    return new RateLimiterImpl(partitionRateLimit);
+  }
+
+  private LoadingCache<StreamConfig, Integer> buildCache() {
+    return 
CacheBuilder.newBuilder().expireAfterWrite(CACHE_ENTRY_EXPIRATION_TIME_IN_MINUTES,
 TimeUnit.MINUTES)
+        .build(new CacheLoader<StreamConfig, Integer>() {
+          @Override
+          public Integer load(StreamConfig streamConfig)
+              throws Exception {
+            String clientId = streamConfig.getTopicName() + 
"-consumption.rate.manager";

Review comment:
       Since we will have only one instance of this cache per host, I suggest 
we use the same client ID for all topics. Some streams start to emit metrics 
for each new client, and this can cause number of metrics to increase (although 
not by much).
   
   Also, I am assuming CacehBuilder returns something like a concurrentHash

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManager.java
##########
@@ -0,0 +1,127 @@
+/**
+ * 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. 
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;
+
+  private final 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) {

Review comment:
       this method is not called at all. I assume you had HLC in mind when you 
created this. We don't need to support rate limiter in HLC, so you can remove 
this altogether? And also all comments about "if it is a multi-partition 
topic". In LLC, it is assumed that all topics are multi-partition.




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

Reply via email to