You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2021/07/26 17:11:26 UTC

[GitHub] [cassandra] maedhroz commented on a change in pull request #1045: CASSANDRA-16663 Request-Based Native Transport Rate-Limiting

maedhroz commented on a change in pull request #1045:
URL: https://github.com/apache/cassandra/pull/1045#discussion_r676792522



##########
File path: src/java/org/apache/cassandra/utils/NoWaitRateLimiter.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.cassandra.utils;
+
+import java.util.concurrent.TimeUnit;
+
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.NotThreadSafe;
+import javax.annotation.concurrent.ThreadSafe;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Stopwatch;
+import com.google.common.base.Ticker;
+import com.google.common.math.LongMath;
+
+@ThreadSafe
+@SuppressWarnings("UnstableApiUsage")
+public class NoWaitRateLimiter
+{
+    @GuardedBy("this")
+    private Stopwatch stopwatch;
+
+    @GuardedBy("this")
+    private SmoothRateLimiter delegate;
+
+    private NoWaitRateLimiter(SmoothRateLimiter delegate, Ticker ticker)
+    {
+        this.delegate = delegate;
+        this.stopwatch = Stopwatch.createStarted(ticker);
+    }
+    
+    public static NoWaitRateLimiter create(double permitsPerSecond)
+    {
+        return create(permitsPerSecond, Ticker.systemTicker());
+    }
+
+    public static NoWaitRateLimiter create(double permitsPerSecond, Ticker ticker)
+    {
+        return create(permitsPerSecond, 1.0D, ticker);
+    }
+
+    public static NoWaitRateLimiter create(double permitsPerSecond, double burstSeconds)
+    {
+        return create(permitsPerSecond, burstSeconds, Ticker.systemTicker());
+    }
+
+    public static NoWaitRateLimiter create(double permitsPerSecond, double burstSeconds, Ticker ticker)
+    {
+        SmoothRateLimiter delegate = new SmoothRateLimiter(burstSeconds);
+        NoWaitRateLimiter limiter = new NoWaitRateLimiter(delegate, ticker);
+        limiter.setRate(permitsPerSecond);
+        return limiter;
+    }
+    
+    @VisibleForTesting
+    public synchronized void reset(double permitsPerSecond, double burstSeconds, Ticker ticker)
+    {
+        this.stopwatch = Stopwatch.createStarted(ticker);
+        this.delegate = new SmoothRateLimiter(burstSeconds);
+        setRate(permitsPerSecond);
+    }
+
+    public final void setRate(double permitsPerSecond) 
+    {
+        Preconditions.checkArgument(permitsPerSecond > 0.0D && !Double.isNaN(permitsPerSecond), "rate must be positive");
+        
+        synchronized (this) 
+        {
+            delegate.doSetRate(permitsPerSecond, stopwatch.elapsed(TimeUnit.MICROSECONDS));
+        }
+    }
+
+    public synchronized double getRate() 
+    {
+        return delegate.getRate();
+    }
+
+    /**
+     * Forces the acquisition of a single permit.
+     * 
+     * @return microseconds until the acquired permit is available, and zero if it already is
+     */
+    public synchronized long reserveAndGetWaitLength() 
+    {
+        long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS);
+        long momentAvailable = delegate.reserveEarliestAvailable(1, nowMicros);
+        return Math.max(momentAvailable - nowMicros, 0L);
+    }
+
+    public synchronized boolean tryAcquire()
+    {
+        long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS);
+
+        if (!delegate.canAcquire(nowMicros)) 
+        {
+            return false;
+        }
+
+        delegate.reserveEarliestAvailable(1, nowMicros);
+            
+        return true;
+    }
+
+    public synchronized boolean canAcquire() 
+    {
+        long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS);
+        return delegate.canAcquire(nowMicros);
+    }
+
+    public synchronized long waitTimeMicros()
+    {
+        long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS);
+        return delegate.waitTimeMicros(nowMicros);
+    }
+
+    @VisibleForTesting
+    public synchronized long getNextFreeTicketMicros()
+    {
+        return delegate.nextFreeTicketMicros;
+    }
+
+    public synchronized double getStoredPermits()
+    {
+        return delegate.getStoredPermits();
+    }
+    
+    @Override
+    public String toString()
+    {
+        return String.format("Maximum requests/second is %.2f. %.2f stored permits available.", 
+                             getRate(), getStoredPermits());
+    }
+
+    @NotThreadSafe
+    private static class SmoothRateLimiter
+    {
+        private double storedPermits;

Review comment:
       For reviewers, carrying the concept of stored permits into this was partially a consequence of copy/paste from the Guava `SmoothBursty` rate limiter in the initial prototype. It's possible that we really don't need this burst capability at all, especially in a first cut of this.
   
   ...and of course, before this commits/merges, we should probably determine whether any licensing matter needs to decorate `SmoothRateLimiter`, although the Guava rate limiter is Apache 2.0 licensed.
   
   CC @belliottsmith 




-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org