1
2
3
4
5
6
7
8
9
10
11 package org.apache.hadoop.hbase.quotas;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.TimeUnit;
16
17 import org.apache.hadoop.hbase.TableName;
18 import org.apache.hadoop.hbase.classification.InterfaceAudience;
19 import org.apache.hadoop.hbase.classification.InterfaceStability;
20 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
21 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetQuotaRequest;
22 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos;
23 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.Quotas;
24
25 @InterfaceAudience.Public
26 @InterfaceStability.Evolving
27 public class QuotaSettingsFactory {
28 private QuotaSettingsFactory() {
29
30 }
31
32 static class QuotaGlobalsSettingsBypass extends QuotaSettings {
33 private final boolean bypassGlobals;
34
35 QuotaGlobalsSettingsBypass(final String userName, final TableName tableName,
36 final String namespace, final boolean bypassGlobals) {
37 super(userName, tableName, namespace);
38 this.bypassGlobals = bypassGlobals;
39 }
40
41 @Override
42 public QuotaType getQuotaType() {
43 return QuotaType.GLOBAL_BYPASS;
44 }
45
46 @Override
47 protected void setupSetQuotaRequest(SetQuotaRequest.Builder builder) {
48 builder.setBypassGlobals(bypassGlobals);
49 }
50
51 @Override
52 public String toString() {
53 return "GLOBAL_BYPASS => " + bypassGlobals;
54 }
55 }
56
57
58
59
60
61 static List<QuotaSettings> fromUserQuotas(final String userName, final Quotas quotas) {
62 return fromQuotas(userName, null, null, quotas);
63 }
64
65 static List<QuotaSettings> fromUserQuotas(final String userName, final TableName tableName,
66 final Quotas quotas) {
67 return fromQuotas(userName, tableName, null, quotas);
68 }
69
70 static List<QuotaSettings> fromUserQuotas(final String userName, final String namespace,
71 final Quotas quotas) {
72 return fromQuotas(userName, null, namespace, quotas);
73 }
74
75 static List<QuotaSettings> fromTableQuotas(final TableName tableName, final Quotas quotas) {
76 return fromQuotas(null, tableName, null, quotas);
77 }
78
79 static List<QuotaSettings> fromNamespaceQuotas(final String namespace, final Quotas quotas) {
80 return fromQuotas(null, null, namespace, quotas);
81 }
82
83 private static List<QuotaSettings> fromQuotas(final String userName, final TableName tableName,
84 final String namespace, final Quotas quotas) {
85 List<QuotaSettings> settings = new ArrayList<QuotaSettings>();
86 if (quotas.hasThrottle()) {
87 settings.addAll(fromThrottle(userName, tableName, namespace, quotas.getThrottle()));
88 }
89 if (quotas.getBypassGlobals() == true) {
90 settings.add(new QuotaGlobalsSettingsBypass(userName, tableName, namespace, true));
91 }
92 return settings;
93 }
94
95 private static List<QuotaSettings> fromThrottle(final String userName, final TableName tableName,
96 final String namespace, final QuotaProtos.Throttle throttle) {
97 List<QuotaSettings> settings = new ArrayList<QuotaSettings>();
98 if (throttle.hasReqNum()) {
99 settings.add(ThrottleSettings.fromTimedQuota(userName, tableName, namespace,
100 ThrottleType.REQUEST_NUMBER, throttle.getReqNum()));
101 }
102 if (throttle.hasReqSize()) {
103 settings.add(ThrottleSettings.fromTimedQuota(userName, tableName, namespace,
104 ThrottleType.REQUEST_SIZE, throttle.getReqSize()));
105 }
106 return settings;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public static QuotaSettings throttleUser(final String userName, final ThrottleType type,
122 final long limit, final TimeUnit timeUnit) {
123 return throttle(userName, null, null, type, limit, timeUnit);
124 }
125
126
127
128
129
130
131
132
133
134
135 public static QuotaSettings throttleUser(final String userName, final TableName tableName,
136 final ThrottleType type, final long limit, final TimeUnit timeUnit) {
137 return throttle(userName, tableName, null, type, limit, timeUnit);
138 }
139
140
141
142
143
144
145
146
147
148
149 public static QuotaSettings throttleUser(final String userName, final String namespace,
150 final ThrottleType type, final long limit, final TimeUnit timeUnit) {
151 return throttle(userName, null, namespace, type, limit, timeUnit);
152 }
153
154
155
156
157
158
159 public static QuotaSettings unthrottleUser(final String userName) {
160 return throttle(userName, null, null, null, 0, null);
161 }
162
163
164
165
166
167
168
169 public static QuotaSettings unthrottleUser(final String userName, final TableName tableName) {
170 return throttle(userName, tableName, null, null, 0, null);
171 }
172
173
174
175
176
177
178
179 public static QuotaSettings unthrottleUser(final String userName, final String namespace) {
180 return throttle(userName, null, namespace, null, 0, null);
181 }
182
183
184
185
186
187
188
189
190
191 public static QuotaSettings throttleTable(final TableName tableName, final ThrottleType type,
192 final long limit, final TimeUnit timeUnit) {
193 return throttle(null, tableName, null, type, limit, timeUnit);
194 }
195
196
197
198
199
200
201 public static QuotaSettings unthrottleTable(final TableName tableName) {
202 return throttle(null, tableName, null, null, 0, null);
203 }
204
205
206
207
208
209
210
211
212
213 public static QuotaSettings throttleNamespace(final String namespace, final ThrottleType type,
214 final long limit, final TimeUnit timeUnit) {
215 return throttle(null, null, namespace, type, limit, timeUnit);
216 }
217
218
219
220
221
222
223 public static QuotaSettings unthrottleNamespace(final String namespace) {
224 return throttle(null, null, namespace, null, 0, null);
225 }
226
227
228 private static QuotaSettings throttle(final String userName, final TableName tableName,
229 final String namespace, final ThrottleType type, final long limit, final TimeUnit timeUnit) {
230 QuotaProtos.ThrottleRequest.Builder builder = QuotaProtos.ThrottleRequest.newBuilder();
231 if (type != null) {
232 builder.setType(ProtobufUtil.toProtoThrottleType(type));
233 }
234 if (timeUnit != null) {
235 builder.setTimedQuota(ProtobufUtil.toTimedQuota(limit, timeUnit, QuotaScope.MACHINE));
236 }
237 return new ThrottleSettings(userName, tableName, namespace, builder.build());
238 }
239
240
241
242
243
244
245
246
247
248
249
250 public static QuotaSettings bypassGlobals(final String userName, final boolean bypassGlobals) {
251 return new QuotaGlobalsSettingsBypass(userName, null, null, bypassGlobals);
252 }
253 }