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  
12  package org.apache.hadoop.hbase.quotas;
13  
14  import java.io.IOException;
15  import java.util.List;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.apache.hadoop.hbase.TableName;
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.hbase.ipc.RpcScheduler;
23  import org.apache.hadoop.hbase.ipc.RpcServer;
24  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
25  import org.apache.hadoop.hbase.regionserver.Region;
26  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
27  import org.apache.hadoop.hbase.security.User;
28  import org.apache.hadoop.security.UserGroupInformation;
29  
30  import com.google.common.annotations.VisibleForTesting;
31  
32  /**
33   * Region Server Quota Manager. It is responsible to provide access to the quota information of each
34   * user/table. The direct user of this class is the RegionServer that will get and check the
35   * user/table quota for each operation (put, get, scan). For system tables and user/table with a
36   * quota specified, the quota check will be a noop.
37   */
38  @InterfaceAudience.Private
39  @InterfaceStability.Evolving
40  public class RegionServerQuotaManager {
41    private static final Log LOG = LogFactory.getLog(RegionServerQuotaManager.class);
42  
43    private final RegionServerServices rsServices;
44  
45    private QuotaCache quotaCache = null;
46  
47    public RegionServerQuotaManager(final RegionServerServices rsServices) {
48      this.rsServices = rsServices;
49    }
50  
51    public void start(final RpcScheduler rpcScheduler) throws IOException {
52      if (!QuotaUtil.isQuotaEnabled(rsServices.getConfiguration())) {
53        LOG.info("Quota support disabled");
54        return;
55      }
56  
57      LOG.info("Initializing quota support");
58  
59      // Initialize quota cache
60      quotaCache = new QuotaCache(rsServices);
61      quotaCache.start();
62    }
63  
64    public void stop() {
65      if (isQuotaEnabled()) {
66        quotaCache.stop("shutdown");
67      }
68    }
69  
70    public boolean isQuotaEnabled() {
71      return quotaCache != null;
72    }
73  
74    @VisibleForTesting
75    QuotaCache getQuotaCache() {
76      return quotaCache;
77    }
78  
79    /**
80     * Returns the quota for an operation.
81     * @param ugi the user that is executing the operation
82     * @param table the table where the operation will be executed
83     * @return the OperationQuota
84     */
85    public OperationQuota getQuota(final UserGroupInformation ugi, final TableName table) {
86      if (isQuotaEnabled() && !table.isSystemTable()) {
87        UserQuotaState userQuotaState = quotaCache.getUserQuotaState(ugi);
88        QuotaLimiter userLimiter = userQuotaState.getTableLimiter(table);
89        boolean useNoop = userLimiter.isBypass();
90        if (userQuotaState.hasBypassGlobals()) {
91          if (LOG.isTraceEnabled()) {
92            LOG.trace("get quota for ugi=" + ugi + " table=" + table + " userLimiter=" + userLimiter);
93          }
94          if (!useNoop) {
95            return new DefaultOperationQuota(userLimiter);
96          }
97        } else {
98          QuotaLimiter nsLimiter = quotaCache.getNamespaceLimiter(table.getNamespaceAsString());
99          QuotaLimiter tableLimiter = quotaCache.getTableLimiter(table);
100         useNoop &= tableLimiter.isBypass() && nsLimiter.isBypass();
101         if (LOG.isTraceEnabled()) {
102           LOG.trace("get quota for ugi=" + ugi + " table=" + table + " userLimiter=" + userLimiter
103               + " tableLimiter=" + tableLimiter + " nsLimiter=" + nsLimiter);
104         }
105         if (!useNoop) {
106           return new DefaultOperationQuota(userLimiter, tableLimiter, nsLimiter);
107         }
108       }
109     }
110     return NoopOperationQuota.get();
111   }
112 
113   /**
114    * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
115    * available quota and to report the data/usage of the operation.
116    * @param region the region where the operation will be performed
117    * @param type the operation type
118    * @return the OperationQuota
119    * @throws ThrottlingException if the operation cannot be executed due to quota exceeded.
120    */
121   public OperationQuota checkQuota(final Region region, final OperationQuota.OperationType type)
122       throws IOException, ThrottlingException {
123     switch (type) {
124     case SCAN:
125       return checkQuota(region, 0, 0, 1);
126     case GET:
127       return checkQuota(region, 0, 1, 0);
128     case MUTATE:
129       return checkQuota(region, 1, 0, 0);
130     default:
131       throw new RuntimeException("Invalid operation type: " + type);
132     }
133   }
134 
135   /**
136    * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
137    * available quota and to report the data/usage of the operation.
138    * @param region the region where the operation will be performed
139    * @param actions the "multi" actions to perform
140    * @return the OperationQuota
141    * @throws ThrottlingException if the operation cannot be executed due to quota exceeded.
142    */
143   public OperationQuota checkQuota(final Region region, final List<ClientProtos.Action> actions)
144       throws IOException, ThrottlingException {
145     int numWrites = 0;
146     int numReads = 0;
147     for (final ClientProtos.Action action : actions) {
148       if (action.hasMutation()) {
149         numWrites++;
150       } else if (action.hasGet()) {
151         numReads++;
152       }
153     }
154     return checkQuota(region, numWrites, numReads, 0);
155   }
156 
157   /**
158    * Check the quota for the current (rpc-context) user. Returns the OperationQuota used to get the
159    * available quota and to report the data/usage of the operation.
160    * @param region the region where the operation will be performed
161    * @param numWrites number of writes to perform
162    * @param numReads number of short-reads to perform
163    * @param numScans number of scan to perform
164    * @return the OperationQuota
165    * @throws ThrottlingException if the operation cannot be executed due to quota exceeded.
166    */
167   private OperationQuota checkQuota(final Region region, final int numWrites, final int numReads,
168       final int numScans) throws IOException, ThrottlingException {
169     User user = RpcServer.getRequestUser();
170     UserGroupInformation ugi;
171     if (user != null) {
172       ugi = user.getUGI();
173     } else {
174       ugi = User.getCurrent().getUGI();
175     }
176     TableName table = region.getTableDesc().getTableName();
177 
178     OperationQuota quota = getQuota(ugi, table);
179     try {
180       quota.checkQuota(numWrites, numReads, numScans);
181     } catch (ThrottlingException e) {
182       LOG.debug("Throttling exception for user=" + ugi.getUserName() + " table=" + table
183           + " numWrites=" + numWrites + " numReads=" + numReads + " numScans=" + numScans + ": "
184           + e.getMessage());
185       throw e;
186     }
187     return quota;
188   }
189 }