HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / quotas / ThrottleQuotaTestUtil.java
blob3b98cba05030398dbd2a7abc3c90e31358469582
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.quotas;
20 import java.io.IOException;
21 import java.util.Objects;
22 import org.apache.hadoop.hbase.HBaseTestingUtil;
23 import org.apache.hadoop.hbase.TableName;
24 import org.apache.hadoop.hbase.client.Get;
25 import org.apache.hadoop.hbase.client.Put;
26 import org.apache.hadoop.hbase.client.Table;
27 import org.apache.hadoop.hbase.security.User;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30 import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
31 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
32 import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
33 import org.apache.yetus.audience.InterfaceAudience;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 @InterfaceAudience.Private
38 public final class ThrottleQuotaTestUtil {
40 private final static Logger LOG = LoggerFactory.getLogger(ThrottleQuotaTestUtil.class);
41 private static ManualEnvironmentEdge envEdge = new ManualEnvironmentEdge();
42 private final static int REFRESH_TIME = 30 * 60000;
43 static {
44 envEdge.setValue(EnvironmentEdgeManager.currentTime());
45 EnvironmentEdgeManagerTestHelper.injectEdge(envEdge);
48 private ThrottleQuotaTestUtil() {
49 // Hide utility class constructor
50 LOG.debug("Call constructor of ThrottleQuotaTestUtil");
53 static int doPuts(int maxOps, byte[] family, byte[] qualifier, final Table... tables) {
54 return doPuts(maxOps, -1, family, qualifier, tables);
57 static int doPuts(int maxOps, int valueSize, byte[] family, byte[] qualifier,
58 final Table... tables) {
59 int count = 0;
60 try {
61 while (count < maxOps) {
62 Put put = new Put(Bytes.toBytes("row-" + count));
63 byte[] value;
64 if (valueSize < 0) {
65 value = Bytes.toBytes("data-" + count);
66 } else {
67 value = generateValue(valueSize);
69 put.addColumn(family, qualifier, value);
70 for (final Table table : tables) {
71 table.put(put);
73 count += tables.length;
75 } catch (IOException e) {
76 LOG.error("put failed after nRetries=" + count, e);
78 return count;
81 private static byte[] generateValue(int valueSize) {
82 byte[] bytes = new byte[valueSize];
83 for (int i = 0; i < valueSize; i++) {
84 bytes[i] = 'a';
86 return bytes;
89 static long doGets(int maxOps, final Table... tables) {
90 int count = 0;
91 try {
92 while (count < maxOps) {
93 Get get = new Get(Bytes.toBytes("row-" + count));
94 for (final Table table : tables) {
95 table.get(get);
97 count += tables.length;
99 } catch (IOException e) {
100 LOG.error("get failed after nRetries=" + count, e);
102 return count;
105 static void triggerUserCacheRefresh(HBaseTestingUtil testUtil, boolean bypass,
106 TableName... tables) throws Exception {
107 triggerCacheRefresh(testUtil, bypass, true, false, false, false, false, tables);
110 static void triggerTableCacheRefresh(HBaseTestingUtil testUtil, boolean bypass,
111 TableName... tables) throws Exception {
112 triggerCacheRefresh(testUtil, bypass, false, true, false, false, false, tables);
115 static void triggerNamespaceCacheRefresh(HBaseTestingUtil testUtil, boolean bypass,
116 TableName... tables) throws Exception {
117 triggerCacheRefresh(testUtil, bypass, false, false, true, false, false, tables);
120 static void triggerRegionServerCacheRefresh(HBaseTestingUtil testUtil, boolean bypass)
121 throws Exception {
122 triggerCacheRefresh(testUtil, bypass, false, false, false, true, false);
125 static void triggerExceedThrottleQuotaCacheRefresh(HBaseTestingUtil testUtil,
126 boolean exceedEnabled) throws Exception {
127 triggerCacheRefresh(testUtil, exceedEnabled, false, false, false, false, true);
130 private static void triggerCacheRefresh(HBaseTestingUtil testUtil, boolean bypass,
131 boolean userLimiter, boolean tableLimiter, boolean nsLimiter, boolean rsLimiter,
132 boolean exceedThrottleQuota, final TableName... tables) throws Exception {
133 envEdge.incValue(2 * REFRESH_TIME);
134 for (RegionServerThread rst : testUtil.getMiniHBaseCluster().getRegionServerThreads()) {
135 RegionServerRpcQuotaManager quotaManager =
136 rst.getRegionServer().getRegionServerRpcQuotaManager();
137 QuotaCache quotaCache = quotaManager.getQuotaCache();
139 quotaCache.triggerCacheRefresh();
140 // sleep for cache update
141 Thread.sleep(250);
143 for (TableName table : tables) {
144 quotaCache.getTableLimiter(table);
147 boolean isUpdated = false;
148 while (!isUpdated) {
149 quotaCache.triggerCacheRefresh();
150 isUpdated = true;
151 for (TableName table : tables) {
152 boolean isBypass = true;
153 if (userLimiter) {
154 isBypass = quotaCache.getUserLimiter(User.getCurrent().getUGI(), table).isBypass();
156 if (tableLimiter) {
157 isBypass &= quotaCache.getTableLimiter(table).isBypass();
159 if (nsLimiter) {
160 isBypass &= quotaCache.getNamespaceLimiter(table.getNamespaceAsString()).isBypass();
162 if (isBypass != bypass) {
163 envEdge.incValue(100);
164 isUpdated = false;
165 break;
168 if (rsLimiter) {
169 boolean rsIsBypass = quotaCache
170 .getRegionServerQuotaLimiter(QuotaTableUtil.QUOTA_REGION_SERVER_ROW_KEY).isBypass();
171 if (rsIsBypass != bypass) {
172 envEdge.incValue(100);
173 isUpdated = false;
176 if (exceedThrottleQuota) {
177 if (quotaCache.isExceedThrottleQuotaEnabled() != bypass) {
178 envEdge.incValue(100);
179 isUpdated = false;
184 LOG.debug("QuotaCache");
185 LOG.debug(Objects.toString(quotaCache.getNamespaceQuotaCache()));
186 LOG.debug(Objects.toString(quotaCache.getTableQuotaCache()));
187 LOG.debug(Objects.toString(quotaCache.getUserQuotaCache()));
188 LOG.debug(Objects.toString(quotaCache.getRegionServerQuotaCache()));
192 static void waitMinuteQuota() {
193 envEdge.incValue(70000);
196 static void clearQuotaCache(HBaseTestingUtil testUtil) {
197 for (RegionServerThread rst : testUtil.getMiniHBaseCluster().getRegionServerThreads()) {
198 RegionServerRpcQuotaManager quotaManager =
199 rst.getRegionServer().getRegionServerRpcQuotaManager();
200 QuotaCache quotaCache = quotaManager.getQuotaCache();
201 quotaCache.getNamespaceQuotaCache().clear();
202 quotaCache.getTableQuotaCache().clear();
203 quotaCache.getUserQuotaCache().clear();
204 quotaCache.getRegionServerQuotaCache().clear();