1
2
3
4
5
6
7
8
9
10
11
12 package org.apache.hadoop.hbase.quotas;
13
14 import java.util.concurrent.TimeUnit;
15
16 import org.apache.hadoop.hbase.classification.InterfaceAudience;
17 import org.apache.hadoop.hbase.classification.InterfaceStability;
18
19 import com.google.common.annotations.VisibleForTesting;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 @InterfaceAudience.Private
40 @InterfaceStability.Evolving
41 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="IS2_INCONSISTENT_SYNC",
42 justification="FindBugs seems confused; says limit and tlimit " +
43 "are mostly synchronized...but to me it looks like they are totally synchronized")
44 public abstract class RateLimiter {
45 public static final String QUOTA_RATE_LIMITER_CONF_KEY = "hbase.quota.rate.limiter";
46 private long tunit = 1000;
47 private long limit = Long.MAX_VALUE;
48 private long avail = Long.MAX_VALUE;
49
50
51
52
53
54
55 abstract long refill(long limit);
56
57
58
59
60
61
62
63
64 abstract long getWaitInterval(long limit, long available, long amount);
65
66
67
68
69
70
71
72 public synchronized void set(final long limit, final TimeUnit timeUnit) {
73 switch (timeUnit) {
74 case MILLISECONDS:
75 tunit = 1;
76 break;
77 case SECONDS:
78 tunit = 1000;
79 break;
80 case MINUTES:
81 tunit = 60 * 1000;
82 break;
83 case HOURS:
84 tunit = 60 * 60 * 1000;
85 break;
86 case DAYS:
87 tunit = 24 * 60 * 60 * 1000;
88 break;
89 default:
90 throw new RuntimeException("Unsupported " + timeUnit.name() + " TimeUnit.");
91 }
92 this.limit = limit;
93 this.avail = limit;
94 }
95
96 public String toString() {
97 String rateLimiter = this.getClass().getSimpleName();
98 if (getLimit() == Long.MAX_VALUE) {
99 return rateLimiter + "(Bypass)";
100 }
101 return rateLimiter + "(avail=" + getAvailable() + " limit=" + getLimit() +
102 " tunit=" + getTimeUnitInMillis() + ")";
103 }
104
105
106
107
108
109
110
111 public synchronized void update(final RateLimiter other) {
112 this.tunit = other.tunit;
113 if (this.limit < other.limit) {
114 this.avail += (other.limit - this.limit);
115 }
116 this.limit = other.limit;
117 }
118
119 public synchronized boolean isBypass() {
120 return getLimit() == Long.MAX_VALUE;
121 }
122
123 public synchronized long getLimit() {
124 return limit;
125 }
126
127 public synchronized long getAvailable() {
128 return avail;
129 }
130
131 protected synchronized long getTimeUnitInMillis() {
132 return tunit;
133 }
134
135
136
137
138
139 public boolean canExecute() {
140 return canExecute(1);
141 }
142
143
144
145
146
147
148 public synchronized boolean canExecute(final long amount) {
149 long refillAmount = refill(limit);
150 if (refillAmount == 0 && avail < amount) {
151 return false;
152 }
153
154 if (avail <= Long.MAX_VALUE - refillAmount) {
155 avail = Math.max(0, Math.min(avail + refillAmount, limit));
156 } else {
157 avail = Math.max(0, limit);
158 }
159 if (avail >= amount) {
160 return true;
161 }
162 return false;
163 }
164
165
166
167
168 public void consume() {
169 consume(1);
170 }
171
172
173
174
175
176 public synchronized void consume(final long amount) {
177 this.avail -= amount;
178 if (this.avail < 0) {
179 this.avail = 0;
180 }
181 }
182
183
184
185
186 public long waitInterval() {
187 return waitInterval(1);
188 }
189
190
191
192
193 public synchronized long waitInterval(final long amount) {
194
195 return (amount <= avail) ? 0 : getWaitInterval(getLimit(), avail, amount);
196 }
197
198
199 @VisibleForTesting
200 public abstract void setNextRefillTime(long nextRefillTime);
201
202 @VisibleForTesting
203 public abstract long getNextRefillTime();
204 }