You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2020/12/04 04:49:19 UTC

[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #6291: Add consumption rate limiter for LLConsumer

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



##########
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) {

Review comment:
       Can you add a description of `multiPartitionTopic` on how this get used?

##########
File path: pinot-core/src/test/java/org/apache/pinot/core/data/manager/realtime/RealtimeConsumptionRateManagerTest.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.core.data.manager.realtime;
+
+import com.google.common.cache.LoadingCache;
+import java.util.Optional;
+import java.util.function.Function;
+import org.apache.pinot.core.data.manager.realtime.RealtimeConsumptionRateManager.ConsumptionRateLimiter;
+import org.apache.pinot.spi.stream.StreamConfig;
+import org.testng.annotations.Test;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.*;
+
+
+public class RealtimeConsumptionRateManagerTest {
+
+  private static final int EPSILON = 50; // msec
+
+  @Test
+  public void testCreateRateLimiter()
+      throws Exception {
+
+    RealtimeConsumptionRateManager rateManager = RealtimeConsumptionRateManager.getInstance();
+
+    // throttling is not enabled
+    StreamConfig streamConfig = mock(StreamConfig.class);
+    when(streamConfig.getTopicConsumptionRateLimit()).thenReturn(Optional.of(2.0));
+    ConsumptionRateLimiter rateLimiter = rateManager.createRateLimiterForSinglePartitionTopic(streamConfig);
+    assertTiming(() -> {
+      rateLimiter.throttle();
+      rateLimiter.throttle();
+      rateLimiter.throttle();
+    }, totalTimeMs -> totalTimeMs < 1000);
+
+    // consumption rate limit is set to 2 per second for a single partition topic
+    rateManager.enableThrottling();
+    when(streamConfig.getTopicConsumptionRateLimit()).thenReturn(Optional.of(2.0));
+    ConsumptionRateLimiter rateLimiter2 = rateManager.createRateLimiterForSinglePartitionTopic(streamConfig);
+    assertTiming(() -> {
+      rateLimiter2.throttle();
+      rateLimiter2.throttle();
+      rateLimiter2.throttle();
+    }, totalTimeMs -> totalTimeMs > 1000 - EPSILON);
+
+    // consumption rate limit is set to 20 per second for a topic with 10 partitions
+    when(streamConfig.getTopicConsumptionRateLimit()).thenReturn(Optional.of(20.0));
+    LoadingCache<StreamConfig, Integer> mockLoadingCache = mock(LoadingCache.class);
+    when(mockLoadingCache.get(any(StreamConfig.class))).thenReturn(10);
+    rateManager._streamConfigToTopicPartitionCountMap = mockLoadingCache;
+    ConsumptionRateLimiter rateLimiter3 = rateManager.createRateLimiterForMultiPartitionTopic(streamConfig);
+    assertTiming(() -> {
+      rateLimiter3.throttle();
+      rateLimiter3.throttle();
+      rateLimiter3.throttle();
+    }, totalTimeMs -> totalTimeMs > 1000 - EPSILON);
+
+    // no config for rate limiter means no throttling
+    when(streamConfig.getTopicConsumptionRateLimit()).thenReturn(Optional.empty());
+    ConsumptionRateLimiter rateLimiter4 = rateManager.createRateLimiterForSinglePartitionTopic(streamConfig);
+    assertTiming(() -> {
+      rateLimiter4.throttle();
+      rateLimiter4.throttle();
+      rateLimiter4.throttle();
+    }, totalTimeMs -> totalTimeMs < 1000);
+
+  }
+
+  private void assertTiming(Runnable runnable, Function<Long, Boolean> assertFunc) {
+    long start = System.currentTimeMillis();
+    runnable.run();
+    long totalTimeMs = System.currentTimeMillis() - start;
+    assertTrue(assertFunc.apply(totalTimeMs));
+  }
+}

Review comment:
       need one extra line

##########
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:
       Do we need to store the complete streamConfig here? Can we just use table name as key?




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org