1
2
3
4
5
6
7
8
9
10
11 package org.apache.hadoop.hbase.quotas;
12
13 import org.apache.hadoop.hbase.classification.InterfaceAudience;
14 import org.apache.hadoop.hbase.classification.InterfaceStability;
15 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
16 import com.google.common.annotations.VisibleForTesting;
17
18
19
20
21
22
23 @InterfaceAudience.Private
24 @InterfaceStability.Evolving
25 public class AverageIntervalRateLimiter extends RateLimiter {
26 private long nextRefillTime = -1L;
27
28 @Override
29 public long refill(long limit) {
30 final long now = EnvironmentEdgeManager.currentTime();
31 if (nextRefillTime == -1) {
32
33 nextRefillTime = EnvironmentEdgeManager.currentTime();
34 return limit;
35 }
36
37 long timeInterval = now - nextRefillTime;
38 long delta = 0;
39 long timeUnitInMillis = super.getTimeUnitInMillis();
40 if (timeInterval >= timeUnitInMillis) {
41 delta = limit;
42 } else if (timeInterval > 0) {
43 double r = ((double)timeInterval / (double)timeUnitInMillis) * limit;
44 delta = (long)r;
45 }
46
47 if (delta > 0) {
48 this.nextRefillTime = now;
49 }
50
51 return delta;
52 }
53
54 @Override
55 public long getWaitInterval(long limit, long available, long amount) {
56 if (nextRefillTime == -1) {
57 return 0;
58 }
59
60 double r = ((double)(amount - available)) * super.getTimeUnitInMillis() / limit;
61 return (long)r;
62 }
63
64
65 @VisibleForTesting
66 @Override
67 public void setNextRefillTime(long nextRefillTime) {
68 this.nextRefillTime = nextRefillTime;
69 }
70
71 @VisibleForTesting
72 @Override
73 public long getNextRefillTime() {
74 return this.nextRefillTime;
75 }
76
77 }