HBASE-21723 Remove ConnectionImplementation and related classes
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestAsyncQuotaAdminApi.java
blobfbf38a0effa95bb16445edfb72f4816a0a5b4b2e
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.client;
20 import static org.apache.hadoop.hbase.client.AsyncConnectionConfiguration.START_LOG_ERRORS_AFTER_COUNT_KEY;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
24 import java.util.Objects;
25 import java.util.concurrent.CompletableFuture;
26 import java.util.concurrent.TimeUnit;
27 import org.apache.hadoop.hbase.HBaseClassTestRule;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.quotas.QuotaCache;
31 import org.apache.hadoop.hbase.quotas.QuotaFilter;
32 import org.apache.hadoop.hbase.quotas.QuotaSettings;
33 import org.apache.hadoop.hbase.quotas.QuotaSettingsFactory;
34 import org.apache.hadoop.hbase.quotas.QuotaTableUtil;
35 import org.apache.hadoop.hbase.quotas.QuotaUtil;
36 import org.apache.hadoop.hbase.quotas.ThrottleType;
37 import org.apache.hadoop.hbase.security.User;
38 import org.apache.hadoop.hbase.testclassification.ClientTests;
39 import org.apache.hadoop.hbase.testclassification.MediumTests;
40 import org.junit.BeforeClass;
41 import org.junit.ClassRule;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.Parameterized;
47 @RunWith(Parameterized.class)
48 @Category({ ClientTests.class, MediumTests.class })
49 public class TestAsyncQuotaAdminApi extends TestAsyncAdminBase {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestAsyncQuotaAdminApi.class);
55 @BeforeClass
56 public static void setUpBeforeClass() throws Exception {
57 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
58 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 2000);
59 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
60 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
61 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
62 TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
63 TEST_UTIL.startMiniCluster(1);
64 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
65 ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
68 @Test
69 public void testThrottleType() throws Exception {
70 String userName = User.getCurrent().getShortName();
72 admin.setQuota(
73 QuotaSettingsFactory.throttleUser(userName, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES))
74 .get();
75 admin.setQuota(
76 QuotaSettingsFactory.throttleUser(userName, ThrottleType.WRITE_NUMBER, 12, TimeUnit.MINUTES))
77 .get();
78 admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true)).get();
80 int countThrottle = 0;
81 int countGlobalBypass = 0;
82 for (QuotaSettings settings : admin.getQuota(null).get()) {
83 switch (settings.getQuotaType()) {
84 case THROTTLE:
85 countThrottle++;
86 break;
87 case GLOBAL_BYPASS:
88 countGlobalBypass++;
89 break;
90 default:
91 fail("unexpected settings type: " + settings.getQuotaType());
94 assertEquals(2, countThrottle);
95 assertEquals(1, countGlobalBypass);
97 admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName)).get();
98 assertNumResults(1, null);
99 admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false)).get();
100 assertNumResults(0, null);
103 @Test
104 public void testQuotaRetrieverFilter() throws Exception {
105 TableName[] tables = new TableName[] { TableName.valueOf("T0"), TableName.valueOf("T01"),
106 TableName.valueOf("NS0:T2"), };
107 String[] namespaces = new String[] { "NS0", "NS01", "NS2" };
108 String[] users = new String[] { "User0", "User01", "User2" };
110 for (String user : users) {
111 admin.setQuota(
112 QuotaSettingsFactory.throttleUser(user, ThrottleType.REQUEST_NUMBER, 1, TimeUnit.MINUTES))
113 .get();
115 for (TableName table : tables) {
116 admin.setQuota(QuotaSettingsFactory.throttleUser(user, table, ThrottleType.REQUEST_NUMBER,
117 2, TimeUnit.MINUTES)).get();
120 for (String ns : namespaces) {
121 admin.setQuota(QuotaSettingsFactory.throttleUser(user, ns, ThrottleType.REQUEST_NUMBER, 3,
122 TimeUnit.MINUTES)).get();
125 assertNumResults(21, null);
127 for (TableName table : tables) {
128 admin.setQuota(
129 QuotaSettingsFactory.throttleTable(table, ThrottleType.REQUEST_NUMBER, 4, TimeUnit.MINUTES))
130 .get();
132 assertNumResults(24, null);
134 for (String ns : namespaces) {
135 admin.setQuota(QuotaSettingsFactory.throttleNamespace(ns, ThrottleType.REQUEST_NUMBER, 5,
136 TimeUnit.MINUTES)).get();
138 assertNumResults(27, null);
140 assertNumResults(7, new QuotaFilter().setUserFilter("User0"));
141 assertNumResults(0, new QuotaFilter().setUserFilter("User"));
142 assertNumResults(21, new QuotaFilter().setUserFilter("User.*"));
143 assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("T0"));
144 assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("NS.*"));
145 assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setTableFilter("T"));
146 assertNumResults(6, new QuotaFilter().setUserFilter("User.*").setTableFilter("T.*"));
147 assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS0"));
148 assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS"));
149 assertNumResults(9, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS.*"));
150 assertNumResults(6,
151 new QuotaFilter().setUserFilter("User.*").setTableFilter("T0").setNamespaceFilter("NS0"));
152 assertNumResults(1, new QuotaFilter().setTableFilter("T0"));
153 assertNumResults(0, new QuotaFilter().setTableFilter("T"));
154 assertNumResults(2, new QuotaFilter().setTableFilter("T.*"));
155 assertNumResults(3, new QuotaFilter().setTableFilter(".*T.*"));
156 assertNumResults(1, new QuotaFilter().setNamespaceFilter("NS0"));
157 assertNumResults(0, new QuotaFilter().setNamespaceFilter("NS"));
158 assertNumResults(3, new QuotaFilter().setNamespaceFilter("NS.*"));
160 for (String user : users) {
161 admin.setQuota(QuotaSettingsFactory.unthrottleUser(user)).get();
162 for (TableName table : tables) {
163 admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, table)).get();
165 for (String ns : namespaces) {
166 admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, ns)).get();
169 assertNumResults(6, null);
171 for (TableName table : tables) {
172 admin.setQuota(QuotaSettingsFactory.unthrottleTable(table)).get();
174 assertNumResults(3, null);
176 for (String ns : namespaces) {
177 admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(ns)).get();
179 assertNumResults(0, null);
182 @Test
183 public void testSwitchRpcThrottle() throws Exception {
184 CompletableFuture<Boolean> future1 = ASYNC_CONN.getAdmin().switchRpcThrottle(true);
185 assertEquals(true, future1.get().booleanValue());
186 CompletableFuture<Boolean> future2 = ASYNC_CONN.getAdmin().isRpcThrottleEnabled();
187 assertEquals(true, future2.get().booleanValue());
190 @Test
191 public void testSwitchExceedThrottleQuota() throws Exception {
192 AsyncAdmin admin = ASYNC_CONN.getAdmin();
193 assertEquals(false, admin.exceedThrottleQuotaSwitch(false).get().booleanValue());
196 private void assertNumResults(int expected, final QuotaFilter filter) throws Exception {
197 assertEquals(expected, countResults(filter));
200 private int countResults(final QuotaFilter filter) throws Exception {
201 int count = 0;
202 for (QuotaSettings settings : admin.getQuota(filter).get()) {
203 LOG.debug(Objects.toString(settings));
204 count++;
206 return count;