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

[GitHub] [incubator-hudi] satishkotha commented on a change in pull request #1484: [HUDI-316] : Hbase qps repartition writestatus

satishkotha commented on a change in pull request #1484:
URL: https://github.com/apache/incubator-hudi/pull/1484#discussion_r418690538



##########
File path: hudi-common/src/main/java/org/apache/hudi/common/util/RateLimiter.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.hudi.common.util;
+
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.ThreadSafe;
+
+/*
+ * Note: Based on RateLimiter implementation in Google/Guava.
+ *         - adopted from com.google.common.util.concurrent
+ *           Copyright (C) 2012 The Guava Authors
+ *           Home page: https://github.com/google/guava
+ *           License: http://www.apache.org/licenses/LICENSE-2.0
+ */
+
+@ThreadSafe
+public abstract class RateLimiter {
+  private final RateLimiter.SleepingTicker ticker;
+  private final long offsetNanos;
+  double storedPermits;
+  double maxPermits;
+  volatile double stableIntervalMicros;
+  private final Object mutex;
+  private long nextFreeTicketMicros;
+
+  public static RateLimiter create(double permitsPerSecond) {
+    return create(RateLimiter.SleepingTicker.SYSTEM_TICKER, permitsPerSecond);
+  }
+
+  static RateLimiter create(RateLimiter.SleepingTicker ticker, double permitsPerSecond) {
+    RateLimiter rateLimiter = new RateLimiter.Bursty(ticker, 1.0D);
+    rateLimiter.setRate(permitsPerSecond);
+    return rateLimiter;
+  }
+
+  public static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit) {
+    return create(RateLimiter.SleepingTicker.SYSTEM_TICKER, permitsPerSecond, warmupPeriod, unit);
+  }
+
+  static RateLimiter create(RateLimiter.SleepingTicker ticker, double permitsPerSecond, long warmupPeriod, TimeUnit unit) {
+    RateLimiter rateLimiter = new RateLimiter.WarmingUp(ticker, warmupPeriod, unit);
+    rateLimiter.setRate(permitsPerSecond);
+    return rateLimiter;
+  }
+
+  private RateLimiter(RateLimiter.SleepingTicker ticker) {
+    this.mutex = new Object();
+    this.nextFreeTicketMicros = 0L;
+    this.ticker = ticker;
+    this.offsetNanos = ticker.read();
+  }
+
+  public final void setRate(double permitsPerSecond) {
+    checkArgument(permitsPerSecond > 0.0D && !Double.isNaN(permitsPerSecond), "rate must be positive");
+    Object var3 = this.mutex;
+    synchronized (this.mutex) {
+      this.resync(this.readSafeMicros());
+      double stableIntervalMicros = (double)TimeUnit.SECONDS.toMicros(1L) / permitsPerSecond;
+      this.stableIntervalMicros = stableIntervalMicros;
+      this.doSetRate(permitsPerSecond, stableIntervalMicros);
+    }
+  }
+
+  abstract void doSetRate(double var1, double var3);
+
+  public final double getRate() {
+    return (double)TimeUnit.SECONDS.toMicros(1L) / this.stableIntervalMicros;
+  }
+
+  public void acquire() {
+    this.acquire(1);
+  }
+
+  public void acquire(int permits) {

Review comment:
       any chance we can add these as metrics/logs? 




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