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/08/25 22:29:40 UTC

[GitHub] [incubator-pinot] jackjlli opened a new pull request #5922: Add max qps bucket count

jackjlli opened a new pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922


   ## Description
   This PR adds the ability of getting maximum qps count within a minute.
   
   In some monitoring system, metrics are emitted in some certain frequency, e.g. every 1 minute. So if there is a burst of qps hitting to the cluster, there is no way to reflect on the metrics. 
   This PR introduces a counter to get the maximum counts among all the seconds within a minute, so that we always know the real circumstances of the cluster.


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


[GitHub] [incubator-pinot] jackjlli merged pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli merged pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922


   


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


[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478743774



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {

Review comment:
       Basically it's the `maxTimeRangeInSeconds` that needs to be passed into the parent constructor instead of the default one. While `maxTimeRangeInSeconds` has to be calculated from the default one. Thus, we'd have to re-calculate the `maxTimeRangeInSeconds ` multiple times before we assign it to this class. Creating an extra constructor gives us the ability to reduce the duplicate calculation.




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


[GitHub] [incubator-pinot] jackjlli edited a comment on pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli edited a comment on pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#issuecomment-680647548


   > Shouldn't we be setting the value to "Max qps within a second since the time the callback was invoked last"? 
   
   We don't want to make the metrics system stateful. Every time the callback method gets called, it should return whatever the value should be.Thus, I don't think it's good to reset the counts when the callback gets called. Otherwise, we should have changed all the metrics to be stateful in Pinot cluster.
   
   > In some systems, the polling may be more often than 1 minute, and in others less often. So, we should keep a hit counter for some max time (say, 10m).
   
   I admit that the frequency of poll may vary. We can make it configurable. But the purpose of this new metric is to track the qps related statistics. It will only be emitted when the qps quota is set. If 10 mins is the granularity for a system to track qps, then I don't think they need to set qps quota for their tables.
   
   > The time interval could be close to a certain period, but can fluctuate by some small percentage either way. So, if the poll comes in 65 seconds, and our first second had a burst, we will lose it (as per your implementation).
   
   This is a rare case. In fact, what we are trying to solve is to find a way to detect the burst of queries which last for a while. They may be ignored, but will never be ignored all the time. Plus, if it happens quite often, then I think there is some issue on polling instead of adjusting our stateless metric system. The callback function never knows when it will be called in advance; when the callback function gets called, it should return the exact max qps within a minute based on the requirement. 
   


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


[GitHub] [incubator-pinot] jackjlli commented on pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#issuecomment-680647548


   > Shouldn't we be setting the value to "Max qps within a second since the time the callback was invoked last"? 
   
   We don't want to make the metrics system stateful. Every time the callback method gets called, it should return whatever the value should be.Thus, I don't think it's good to reset the counts when the callback gets called. Otherwise, we should have changed all the metrics to be stateful in Pinot cluster.
   
   > In some systems, the polling may be more often than 1 minute, and in others less often. So, we should keep a hit counter for some max time (say, 10m).
   
   I admit that the frequency of poll may vary. We can make it configurable. But the purpose of this new metric is to track the qps related statistics. If 10 mins is the granularity for a system to track qps, then I don't think they need to set qps quota for their tables.
   
   > The time interval could be close to a certain period, but can fluctuate by some small percentage either way. So, if the poll comes in 65 seconds, and our first second had a burst, we will lose it (as per your implementation).
   
   This is a rare case. In fact, what we are trying to solve is to find a way to detect the burst of queries which last for a while. They may be ignored, but will never be ignored all the time. Plus, if it happens quite often, then I think there is some issue on polling instead of adjusting our stateless metric system. The callback function never knows when it will be called in advance; when the callback function gets called, it should return the exact max qps within a minute based on the requirement. 
   


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


[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478687425



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {
+    super(timeRangeInSeconds, bucketCount);
+    _maxTimeRangeMs = timeRangeInSeconds * 1000L;
+    _defaultQueriedTimeRangeMs = defaultQueriedTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long timestamp) {
+    // If the hit counter didn't get queried for more than _maxTimeRangeMs
+    if (timestamp - _lastAccessTimestamp > _maxTimeRangeMs) {
+      _lastAccessTimestamp = timestamp - _defaultQueriedTimeRangeMs;
+    }
+    long startTimeUnits = _lastAccessTimestamp / _timeBucketWidthMs;
+    int startIndex = (int) (startTimeUnits % _bucketCount);
+
+    long numTimeUnits = timestamp / _timeBucketWidthMs;
+    int endIndex = (int) (numTimeUnits % _bucketCount);
+
+    int maxCount = 0;
+    // Since the start index was accessed last time, there is no need to query its bucket this time.

Review comment:
       Updated the comments here.




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


[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
mcvsubbu commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478757025



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {

Review comment:
       Can we make that constructor private then?




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


[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478743774



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {

Review comment:
       Basically it's the `maxTimeRangeInSeconds` that needs to be passed into the parent constructor instead of the default one. Thus, we'd have to re-calculate the `maxTimeRangeInSeconds ` multiple times before we assign it to this class. Creating an extra constructor gives us the ability to reduce the duplicate calculation.




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


[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
mcvsubbu commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478597673



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;

Review comment:
       should be volatile

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;

Review comment:
       final

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/QueryQuotaEntity.java
##########
@@ -24,15 +24,17 @@
 public class QueryQuotaEntity {
 
   private RateLimiter _rateLimiter;
-  private HitCounter _hitCounter;
+  private HitCounter _hitCounterInSecond;

Review comment:
       ```suggestion
     private HitCounter _qpsTracker;
   ```

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {

Review comment:
       ```suggestion
     public StatefulHitCounter(int queriedTimeRangeInSeconds) {
   ```
   Derive the other two locally. So, the timeRange we maintain could be `2*queriedTimeRangeInSeconds` or even `1.5` times . the bucket count is also something that could be decided by the statefulHitCounter.

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {

Review comment:
       Is `StatefulHitCounter` the right name? Should we call it maxHitRteTracker, since that is more specific?

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/metrics/BrokerGauge.java
##########
@@ -27,6 +27,7 @@
  */
 public enum BrokerGauge implements AbstractMetrics.Gauge {
   QUERY_QUOTA_CAPACITY_UTILIZATION_RATE("tables", false),
+  MAX_QPS_IN_ONE_MINUTE("tables", false),

Review comment:
       `MAX_QPS_SINCE_LAST_CALL` or better, `MAX_BURST_QPS`?

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {
+    super(timeRangeInSeconds, bucketCount);
+    _maxTimeRangeMs = timeRangeInSeconds * 1000L;
+    _defaultQueriedTimeRangeMs = defaultQueriedTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long timestamp) {

Review comment:
       ```suggestion
     int getMaxCountPerBucket(long now) {
   ```
   More intuitive name I think

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {
+    super(timeRangeInSeconds, bucketCount);
+    _maxTimeRangeMs = timeRangeInSeconds * 1000L;
+    _defaultQueriedTimeRangeMs = defaultQueriedTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long timestamp) {
+    // If the hit counter didn't get queried for more than _maxTimeRangeMs
+    if (timestamp - _lastAccessTimestamp > _maxTimeRangeMs) {
+      _lastAccessTimestamp = timestamp - _defaultQueriedTimeRangeMs;
+    }
+    long startTimeUnits = _lastAccessTimestamp / _timeBucketWidthMs;
+    int startIndex = (int) (startTimeUnits % _bucketCount);
+
+    long numTimeUnits = timestamp / _timeBucketWidthMs;
+    int endIndex = (int) (numTimeUnits % _bucketCount);
+
+    int maxCount = 0;
+    // Since the start index was accessed last time, there is no need to query its bucket this time.

Review comment:
       ```suggestion
       // Since the end index was accessed last time, there is no need to query its bucket this time.
   ```
   We are skipping the endIndex in the loop below, right?

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/QueryQuotaEntity.java
##########
@@ -24,15 +24,17 @@
 public class QueryQuotaEntity {
 
   private RateLimiter _rateLimiter;
-  private HitCounter _hitCounter;
+  private HitCounter _hitCounterInSecond;
+  private StatefulHitCounter _hitCounterInMinute;

Review comment:
       ```suggestion
     private StatefulHitCounter _maxQpsTracker;
   ```

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;

Review comment:
       final




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


[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478687225



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/StatefulHitCounter.java
##########
@@ -0,0 +1,73 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class StatefulHitCounter extends HitCounter {
+  private long _maxTimeRangeMs;
+  private long _defaultQueriedTimeRangeMs;
+  private long _lastAccessTimestamp;
+
+  public StatefulHitCounter(int timeRangeInSeconds, int bucketCount, int defaultQueriedTimeRangeInSeconds) {

Review comment:
       It can be used to track the max hit rate but the class itself is stateful. Updated the class name anyway.




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


[GitHub] [incubator-pinot] mcvsubbu commented on pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
mcvsubbu commented on pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#issuecomment-680976015


   > > Shouldn't we be setting the value to "Max qps within a second since the time the callback was invoked last"?
   > 
   > We don't want to make the metrics system stateful. Every time the callback method gets called, it should return whatever the value should be.Thus, I don't think it's good to reset the counts when the callback gets called. Otherwise, we should have changed all the metrics to be stateful in Pinot cluster.
   > 
   There is a difference between this one and all the others that we have. In the others, the state is still there, it is being maintained by the metrics library. How else do you think we get percentiles? In this case, we want a much finer granularity, and hence the need for it to clear state and report the max that it recorded since the last call.  Otherwise, we will be emitting metrics that do not reflect the real state as we want to measure it.
   
   > > In some systems, the polling may be more often than 1 minute, and in others less often. So, we should keep a hit counter for some max time (say, 10m).
   > 
   > I admit that the frequency of poll may vary. We can make it configurable. But the purpose of this new metric is to track the qps related statistics. It will only be emitted when the qps quota is set. If 10 mins is the granularity for a system to track qps, then I don't think they need to set qps quota for their tables.
   > 
   > > The time interval could be close to a certain period, but can fluctuate by some small percentage either way. So, if the poll comes in 65 seconds, and our first second had a burst, we will lose it (as per your implementation).
   > 
   > This is a rare case. In fact, what we are trying to solve is to find a way to detect the burst of queries which last for a while. They may be ignored, but will never be ignored all the time. Plus, if it happens quite often, then I think there is some issue on polling instead of adjusting our stateless metric system. The callback function never knows when it will be called in advance; when the callback function gets called, it should return the exact max qps within a minute based on the requirement.
   
   I am not sure how rare a case this is.


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


[GitHub] [incubator-pinot] jackjlli commented on pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#issuecomment-681033126


   > > > Shouldn't we be setting the value to "Max qps within a second since the time the callback was invoked last"?
   > > 
   > > 
   > > We don't want to make the metrics system stateful. Every time the callback method gets called, it should return whatever the value should be.Thus, I don't think it's good to reset the counts when the callback gets called. Otherwise, we should have changed all the metrics to be stateful in Pinot cluster.
   > 
   > There is a difference between this one and all the others that we have. In the others, the state is still there, it is being maintained by the metrics library. How else do you think we get percentiles? In this case, we want a much finer granularity, and hence the need for it to clear state and report the max that it recorded since the last call. Otherwise, we will be emitting metrics that do not reflect the real state as we want to measure it.
   
   I'm talking about the gauge values, not the meter values. The existing gauge values are maintained in a ConcurrentHashMap in `MetrcisHelper` class, which is Pinot's code. The emitted  gauge value is just the one when the callback function gets called. There is no state at all.
   > 
   > > > In some systems, the polling may be more often than 1 minute, and in others less often. So, we should keep a hit counter for some max time (say, 10m).
   > > 
   > > 
   > > I admit that the frequency of poll may vary. We can make it configurable. But the purpose of this new metric is to track the qps related statistics. It will only be emitted when the qps quota is set. If 10 mins is the granularity for a system to track qps, then I don't think they need to set qps quota for their tables.
   > > > The time interval could be close to a certain period, but can fluctuate by some small percentage either way. So, if the poll comes in 65 seconds, and our first second had a burst, we will lose it (as per your implementation).
   > > 
   > > 
   > > This is a rare case. In fact, what we are trying to solve is to find a way to detect the burst of queries which last for a while. They may be ignored, but will never be ignored all the time. Plus, if it happens quite often, then I think there is some issue on polling instead of adjusting our stateless metric system. The callback function never knows when it will be called in advance; when the callback function gets called, it should return the exact max qps within a minute based on the requirement.
   > 
   > I am not sure how rare a case this is.
   
   The case I mentioned here is that the it always neglects the burst of queries in the first second; the burst is so smart that it always happens at the time when the hit counter couldn't detect.
   In another case, if the callback gets invoked 2 consecutive times which gap is small (maybe caused by GC). The stateful won't give you the correct number, as the value has already been reset by the first call.


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


[GitHub] [incubator-pinot] jackjlli commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
jackjlli commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478745025



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {
+    super(maxTimeRangeInSeconds, (int) (maxTimeRangeInSeconds * 1000L / ONE_SECOND_BUCKET_WIDTH_MS));
+    _defaultTimeRangeMs = defaultTimeRangeInSeconds * 1000L;
+    _maxTimeRangeMs = maxTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long now) {

Review comment:
       No, it doesn't need to be synchronized.
   The callback function will be called by 1 single thread. Thus, `_lastAccessTimeStamp` will also be modified by the same thread. 
   The bucket belonging to the end index won't be queried, so there is no need to add the block on it. Plus, all the buckets have already been in AtomicIntegerArray. There is no need to add extra protection 




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


[GitHub] [incubator-pinot] mcvsubbu commented on a change in pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
mcvsubbu commented on a change in pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#discussion_r478735835



##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {

Review comment:
       why do we need this constructor?

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {
+    super(maxTimeRangeInSeconds, (int) (maxTimeRangeInSeconds * 1000L / ONE_SECOND_BUCKET_WIDTH_MS));
+    _defaultTimeRangeMs = defaultTimeRangeInSeconds * 1000L;
+    _maxTimeRangeMs = maxTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long now) {

Review comment:
       should this method be synchronized?
   I would code the method like this:
   - Get the value of _lastAccessTimeStamp in the beginning of the method (`then = _lastAccessTimeStamp`)
   - Use `then` throughout the method
   - Set the` _lastAccessTimestamp` to `now` at the end of the method
   
   That will protect us against multiple calls, if any (just in case).
   
   it will also prevent us from accessing the volatile variable repeatedly.

##########
File path: pinot-broker/src/main/java/org/apache/pinot/broker/queryquota/MaxHitRateTracker.java
##########
@@ -0,0 +1,80 @@
+/**
+ * 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.broker.queryquota;
+
+import com.google.common.annotations.VisibleForTesting;
+
+/**
+ * A stateful version of hit counter. Similar to the default hit counter, it maintains a list of buckets.
+ * Whereas it maintains an extra variable called _lastAccessTimestamp which tracks the last access time.
+ * If the stateful hit counter gets queried, it firstly compares the current timestamp and the last access timestamp,
+ * calculating the start index and end index among the buckets. Then, it traverses through all the valid candidate buckets.
+ * If the current timestamp has exceeded the current time range of all the buckets, this hit counter will use
+ * the current timestamp minus the default time queried time range to calculate the start time index.
+ */
+public class MaxHitRateTracker extends HitCounter {
+  private static int ONE_SECOND_BUCKET_WIDTH_MS = 1000;
+  private static int MAX_TIME_RANGE_FACTOR = 2;
+
+  private final long _maxTimeRangeMs;
+  private final long _defaultTimeRangeMs;
+  private volatile long _lastAccessTimestamp;
+
+  public MaxHitRateTracker(int timeRangeInSeconds) {
+    this(timeRangeInSeconds, timeRangeInSeconds * MAX_TIME_RANGE_FACTOR);
+  }
+
+  public MaxHitRateTracker(int defaultTimeRangeInSeconds, int maxTimeRangeInSeconds) {
+    super(maxTimeRangeInSeconds, (int) (maxTimeRangeInSeconds * 1000L / ONE_SECOND_BUCKET_WIDTH_MS));
+    _defaultTimeRangeMs = defaultTimeRangeInSeconds * 1000L;
+    _maxTimeRangeMs = maxTimeRangeInSeconds * 1000L;
+  }
+
+  /**
+   * Get the maximum count among the buckets
+   */
+  public int getMaxCountPerBucket() {
+    return getMaxCountPerBucket(System.currentTimeMillis());
+  }
+
+  @VisibleForTesting
+  int getMaxCountPerBucket(long now) {
+    // Update the last access timestamp if the hit counter didn't get queried for more than _maxTimeRangeMs.
+    if (now - _lastAccessTimestamp > _maxTimeRangeMs) {

Review comment:
       ```suggestion
       then = _lastAccessTimeStamp;
       if (now - then > _maxTimeRangeMs) {
           then = now - _defaultTimeRangeMs;
       }
       long startTimeUnits = then / _timeBucketWidthMs;
   ```




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


[GitHub] [incubator-pinot] mcvsubbu commented on pull request #5922: Add max qps bucket count

Posted by GitBox <gi...@apache.org>.
mcvsubbu commented on pull request #5922:
URL: https://github.com/apache/incubator-pinot/pull/5922#issuecomment-681183127


   Discussed offline. The "clearing" of values is logical. Implementation can be that we maintain a from/to handle on the circular buffer and count only those buckets populated since the last call.


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