|
| 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 |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 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. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.quotas; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hbase.quotas.ThrottleQuotaTestUtil.doPuts; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 26 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 27 | +import org.apache.hadoop.hbase.HConstants; |
| 28 | +import org.apache.hadoop.hbase.TableName; |
| 29 | +import org.apache.hadoop.hbase.client.Admin; |
| 30 | +import org.apache.hadoop.hbase.client.Table; |
| 31 | +import org.apache.hadoop.hbase.security.User; |
| 32 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 33 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; |
| 34 | +import org.apache.hadoop.hbase.util.Bytes; |
| 35 | +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; |
| 36 | +import org.junit.AfterClass; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.ClassRule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | + |
| 42 | +@Category({ RegionServerTests.class, MediumTests.class }) |
| 43 | +public class TestQuotaUserOverride { |
| 44 | + |
| 45 | + @ClassRule |
| 46 | + public static final HBaseClassTestRule CLASS_RULE = |
| 47 | + HBaseClassTestRule.forClass(TestQuotaUserOverride.class); |
| 48 | + |
| 49 | + private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); |
| 50 | + private static final byte[] FAMILY = Bytes.toBytes("cf"); |
| 51 | + private static final byte[] QUALIFIER = Bytes.toBytes("q"); |
| 52 | + private static final int NUM_SERVERS = 1; |
| 53 | + private static final String CUSTOM_OVERRIDE_KEY = "foo"; |
| 54 | + |
| 55 | + private static final TableName TABLE_NAME = TableName.valueOf("TestQuotaUserOverride"); |
| 56 | + |
| 57 | + @BeforeClass |
| 58 | + public static void setUpBeforeClass() throws Exception { |
| 59 | + TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); |
| 60 | + TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 1_000); |
| 61 | + TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 1); |
| 62 | + TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 0); |
| 63 | + TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 500); |
| 64 | + TEST_UTIL.getConfiguration().set(QuotaCache.QUOTA_USER_REQUEST_ATTRIBUTE_OVERRIDE_KEY, |
| 65 | + CUSTOM_OVERRIDE_KEY); |
| 66 | + TEST_UTIL.startMiniCluster(NUM_SERVERS); |
| 67 | + TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); |
| 68 | + QuotaCache.TEST_FORCE_REFRESH = true; |
| 69 | + TEST_UTIL.createTable(TABLE_NAME, FAMILY); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterClass |
| 73 | + public static void tearDownAfterClass() throws Exception { |
| 74 | + EnvironmentEdgeManager.reset(); |
| 75 | + TEST_UTIL.shutdownMiniCluster(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testUserGlobalThrottleWithCustomOverride() throws Exception { |
| 80 | + final Admin admin = TEST_UTIL.getAdmin(); |
| 81 | + final String userOverrideWithQuota = User.getCurrent().getShortName() + "123"; |
| 82 | + |
| 83 | + // Add 6req/min limit |
| 84 | + admin.setQuota(QuotaSettingsFactory.throttleUser(userOverrideWithQuota, |
| 85 | + ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES)); |
| 86 | + |
| 87 | + Table tableWithThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) |
| 88 | + .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes(userOverrideWithQuota)).build(); |
| 89 | + Table tableWithoutThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) |
| 90 | + .setRequestAttribute(QuotaCache.QUOTA_USER_REQUEST_ATTRIBUTE_OVERRIDE_KEY, |
| 91 | + Bytes.toBytes(userOverrideWithQuota)) |
| 92 | + .build(); |
| 93 | + Table tableWithoutThrottle2 = |
| 94 | + TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).build(); |
| 95 | + |
| 96 | + // warm things up |
| 97 | + doPuts(10, FAMILY, QUALIFIER, tableWithThrottle); |
| 98 | + doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle); |
| 99 | + doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2); |
| 100 | + |
| 101 | + // should reject some requests |
| 102 | + assertTrue(10 > doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); |
| 103 | + // should accept all puts |
| 104 | + assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); |
| 105 | + // should accept all puts |
| 106 | + assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2)); |
| 107 | + |
| 108 | + // Remove all the limits |
| 109 | + admin.setQuota(QuotaSettingsFactory.unthrottleUser(userOverrideWithQuota)); |
| 110 | + Thread.sleep(60_000); |
| 111 | + assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); |
| 112 | + assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); |
| 113 | + assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2)); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments