belliottsmith commented on a change in pull request #1045: URL: https://github.com/apache/cassandra/pull/1045#discussion_r679855372
########## 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: I'm not sure we really need burstiness, since we permit commands to execute immediately anyway, so that there's a kind of burst facility built in. If we implement our own (which we could, since it is quite simple), all I would say is that we might want to have some function of the maximum number of accumulated permits, i.e. a bound on the offset we are able to wait. This could be set to 1s, so that a period of quiescence can be carried forward. I'm not absolutely certain what licensing concerns there are, but I think we just need to include the original Guava license header. I'm sure there's some guidance somewhere. -- 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]

