View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
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      // Utility class
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     * ========================================================================== QuotaSettings from
59     * the Quotas object
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    * ========================================================================== RPC Throttle
111    */
112 
113   /**
114    * Throttle the specified user.
115    * @param userName the user to throttle
116    * @param type the type of throttling
117    * @param limit the allowed number of request/data per timeUnit
118    * @param timeUnit the limit time unit
119    * @return the quota settings
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    * Throttle the specified user on the specified table.
128    * @param userName the user to throttle
129    * @param tableName the table to throttle
130    * @param type the type of throttling
131    * @param limit the allowed number of request/data per timeUnit
132    * @param timeUnit the limit time unit
133    * @return the quota settings
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    * Throttle the specified user on the specified namespace.
142    * @param userName the user to throttle
143    * @param namespace the namespace to throttle
144    * @param type the type of throttling
145    * @param limit the allowed number of request/data per timeUnit
146    * @param timeUnit the limit time unit
147    * @return the quota settings
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    * Remove the throttling for the specified user.
156    * @param userName the user
157    * @return the quota settings
158    */
159   public static QuotaSettings unthrottleUser(final String userName) {
160     return throttle(userName, null, null, null, 0, null);
161   }
162 
163   /**
164    * Remove the throttling for the specified user on the specified table.
165    * @param userName the user
166    * @param tableName the table
167    * @return the quota settings
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    * Remove the throttling for the specified user on the specified namespace.
175    * @param userName the user
176    * @param namespace the namespace
177    * @return the quota settings
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    * Throttle the specified table.
185    * @param tableName the table to throttle
186    * @param type the type of throttling
187    * @param limit the allowed number of request/data per timeUnit
188    * @param timeUnit the limit time unit
189    * @return the quota settings
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    * Remove the throttling for the specified table.
198    * @param tableName the table
199    * @return the quota settings
200    */
201   public static QuotaSettings unthrottleTable(final TableName tableName) {
202     return throttle(null, tableName, null, null, 0, null);
203   }
204 
205   /**
206    * Throttle the specified namespace.
207    * @param namespace the namespace to throttle
208    * @param type the type of throttling
209    * @param limit the allowed number of request/data per timeUnit
210    * @param timeUnit the limit time unit
211    * @return the quota settings
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    * Remove the throttling for the specified namespace.
220    * @param namespace the namespace
221    * @return the quota settings
222    */
223   public static QuotaSettings unthrottleNamespace(final String namespace) {
224     return throttle(null, null, namespace, null, 0, null);
225   }
226 
227   /* Throttle helper */
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    * ========================================================================== Global Settings
242    */
243 
244   /**
245    * Set the "bypass global settings" for the specified user
246    * @param userName the user to throttle
247    * @param bypassGlobals true if the global settings should be bypassed
248    * @return the quota settings
249    */
250   public static QuotaSettings bypassGlobals(final String userName, final boolean bypassGlobals) {
251     return new QuotaGlobalsSettingsBypass(userName, null, null, bypassGlobals);
252   }
253 }