001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.quotas; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.util.concurrent.TimeUnit; 023import org.apache.hadoop.hbase.HBaseTestingUtil; 024import org.apache.hadoop.hbase.TableName; 025import org.apache.hadoop.hbase.client.Admin; 026import org.apache.hadoop.hbase.testclassification.MediumTests; 027import org.apache.hadoop.hbase.testclassification.RegionServerTests; 028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 029import org.apache.hadoop.security.UserGroupInformation; 030import org.junit.jupiter.api.AfterEach; 031import org.junit.jupiter.api.BeforeEach; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034 035@Tag(RegionServerTests.TAG) 036@Tag(MediumTests.TAG) 037public class TestQuotaCache { 038 039 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 040 private static final int REFRESH_TIME_MS = 1000; 041 042 @AfterEach 043 public void tearDown() throws Exception { 044 ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL); 045 EnvironmentEdgeManager.reset(); 046 TEST_UTIL.shutdownMiniCluster(); 047 } 048 049 @BeforeEach 050 public void setUp() throws Exception { 051 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 052 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME_MS); 053 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_READ_NUM, 1000); 054 055 TEST_UTIL.startMiniCluster(1); 056 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 057 } 058 059 @Test 060 public void testUserQuotaLookup() throws Exception { 061 QuotaCache quotaCache = 062 ThrottleQuotaTestUtil.getQuotaCaches(TEST_UTIL).stream().findAny().get(); 063 final Admin admin = TEST_UTIL.getAdmin(); 064 admin.setQuota(QuotaSettingsFactory.throttleUser("my_user", ThrottleType.READ_NUMBER, 3737, 065 TimeUnit.MINUTES)); 066 067 // Setting a quota and then looking it up from the cache should work, even if the cache has not 068 // refreshed 069 UserGroupInformation ugi = UserGroupInformation.createRemoteUser("my_user"); 070 QuotaLimiter quotaLimiter = quotaCache.getUserLimiter(ugi, TableName.valueOf("my_table")); 071 assertEquals(3737, quotaLimiter.getReadNumLimit()); 072 073 // if no specific user quota, fall back to default 074 ugi = UserGroupInformation.createRemoteUser("my_user2"); 075 quotaLimiter = quotaCache.getUserLimiter(ugi, TableName.valueOf("my_table")); 076 assertEquals(1000, quotaLimiter.getReadNumLimit()); 077 078 // still works after refresh 079 quotaCache.forceSynchronousCacheRefresh(); 080 ugi = UserGroupInformation.createRemoteUser("my_user"); 081 quotaLimiter = quotaCache.getUserLimiter(ugi, TableName.valueOf("my_table")); 082 assertEquals(3737, quotaLimiter.getReadNumLimit()); 083 084 ugi = UserGroupInformation.createRemoteUser("my_user2"); 085 quotaLimiter = quotaCache.getUserLimiter(ugi, TableName.valueOf("my_table")); 086 assertEquals(1000, quotaLimiter.getReadNumLimit()); 087 } 088 089 @Test 090 public void testTableQuotaLookup() throws Exception { 091 QuotaCache quotaCache = 092 ThrottleQuotaTestUtil.getQuotaCaches(TEST_UTIL).stream().findAny().get(); 093 final Admin admin = TEST_UTIL.getAdmin(); 094 admin.setQuota(QuotaSettingsFactory.throttleTable(TableName.valueOf("my_table"), 095 ThrottleType.READ_NUMBER, 3737, TimeUnit.MINUTES)); 096 097 // Setting a quota and then looking it up from the cache should work, even if the cache has not 098 // refreshed 099 QuotaLimiter quotaLimiter = quotaCache.getTableLimiter(TableName.valueOf("my_table")); 100 assertEquals(3737, quotaLimiter.getReadNumLimit()); 101 102 // if no specific table quota, fall back to default 103 quotaLimiter = quotaCache.getTableLimiter(TableName.valueOf("my_table2")); 104 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 105 106 // still works after refresh 107 quotaCache.forceSynchronousCacheRefresh(); 108 quotaLimiter = quotaCache.getTableLimiter(TableName.valueOf("my_table")); 109 assertEquals(3737, quotaLimiter.getReadNumLimit()); 110 111 quotaLimiter = quotaCache.getTableLimiter(TableName.valueOf("my_table2")); 112 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 113 } 114 115 @Test 116 public void testNamespaceQuotaLookup() throws Exception { 117 QuotaCache quotaCache = 118 ThrottleQuotaTestUtil.getQuotaCaches(TEST_UTIL).stream().findAny().get(); 119 final Admin admin = TEST_UTIL.getAdmin(); 120 admin.setQuota(QuotaSettingsFactory.throttleNamespace("my_namespace", ThrottleType.READ_NUMBER, 121 3737, TimeUnit.MINUTES)); 122 123 // Setting a quota and then looking it up from the cache should work, even if the cache has not 124 // refreshed 125 QuotaLimiter quotaLimiter = quotaCache.getNamespaceLimiter("my_namespace"); 126 assertEquals(3737, quotaLimiter.getReadNumLimit()); 127 128 // if no specific namespace quota, fall back to default 129 quotaLimiter = quotaCache.getNamespaceLimiter("my_namespace2"); 130 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 131 132 // still works after refresh 133 quotaCache.forceSynchronousCacheRefresh(); 134 quotaLimiter = quotaCache.getNamespaceLimiter("my_namespace"); 135 assertEquals(3737, quotaLimiter.getReadNumLimit()); 136 137 quotaLimiter = quotaCache.getNamespaceLimiter("my_namespace2"); 138 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 139 } 140 141 @Test 142 public void testRegionServerQuotaLookup() throws Exception { 143 QuotaCache quotaCache = 144 ThrottleQuotaTestUtil.getQuotaCaches(TEST_UTIL).stream().findAny().get(); 145 final Admin admin = TEST_UTIL.getAdmin(); 146 admin.setQuota(QuotaSettingsFactory.throttleRegionServer("my_region_server", 147 ThrottleType.READ_NUMBER, 3737, TimeUnit.MINUTES)); 148 149 // Setting a quota and then looking it up from the cache should work, even if the cache has not 150 // refreshed 151 QuotaLimiter quotaLimiter = quotaCache.getRegionServerQuotaLimiter("my_region_server"); 152 assertEquals(3737, quotaLimiter.getReadNumLimit()); 153 154 // if no specific server quota, fall back to default 155 quotaLimiter = quotaCache.getRegionServerQuotaLimiter("my_region_server2"); 156 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 157 158 // still works after refresh 159 quotaCache.forceSynchronousCacheRefresh(); 160 quotaLimiter = quotaCache.getRegionServerQuotaLimiter("my_region_server"); 161 assertEquals(3737, quotaLimiter.getReadNumLimit()); 162 163 quotaLimiter = quotaCache.getRegionServerQuotaLimiter("my_region_server2"); 164 assertEquals(Long.MAX_VALUE, quotaLimiter.getReadNumLimit()); 165 } 166}