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
115
116 long diff = other.limit - this.limit;
117 if (this.avail <= Long.MAX_VALUE - diff) {
118 this.avail += diff;
119 this.avail = Math.min(this.avail, other.limit);
120 } else {
121 this.avail = other.limit;
122 }
123 }
124 this.limit = other.limit;
125 }
126
127 public synchronized boolean isBypass() {
128 return getLimit() == Long.MAX_VALUE;
129 }
130
131 public synchronized long getLimit() {
132 return limit;
133 }
134
135 public synchronized long getAvailable() {
136 return avail;
137 }
138
139 protected synchronized long getTimeUnitInMillis() {
140 return tunit;
141 }
142
143
144
145
146
147 public boolean canExecute() {
148 return canExecute(1);
149 }
150
151
152
153
154
155
156 public synchronized boolean canExecute(final long amount) {
157 if (isBypass()) {
158 return true;
159 }
160
161 long refillAmount = refill(limit);
162 if (refillAmount == 0 && avail < amount) {
163 return false;
164 }
165
166 if (avail <= Long.MAX_VALUE - refillAmount) {
167 avail = Math.max(0, Math.min(avail + refillAmount, limit));
168 } else {
169 avail = Math.max(0, limit);
170 }
171 if (avail >= amount) {
172 return true;
173 }
174 return false;
175 }
176
177
178
179
180 public void consume() {
181 consume(1);
182 }
183
184
185
186
187
188 public synchronized void consume(final long amount) {
189
190 if (isBypass()) {
191 return;
192 }
193
194 if (amount >= 0 ) {
195 this.avail -= amount;
196 if (this.avail < 0) {
197 this.avail = 0;
198 }
199 } else {
200 if (this.avail <= Long.MAX_VALUE + amount) {
201 this.avail -= amount;
202 this.avail = Math.min(this.avail, this.limit);
203 } else {
204 this.avail = this.limit;
205 }
206 }
207 }
208
209
210
211
212 public long waitInterval() {
213 return waitInterval(1);
214 }
215
216
217
218
219 public synchronized long waitInterval(final long amount) {
220
221 return (amount <= avail) ? 0 : getWaitInterval(getLimit(), avail, amount);
222 }
223
224
225 @VisibleForTesting
226 public abstract void setNextRefillTime(long nextRefillTime);
227
228 @VisibleForTesting
229 public abstract long getNextRefillTime();
230 }