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.apache.hadoop.hbase.quotas.ThrottleQuotaTestUtil.triggerUserCacheRefresh; 021import static org.apache.hadoop.hbase.quotas.ThrottleQuotaTestUtil.waitMinuteQuota; 022 023import java.io.IOException; 024import java.util.UUID; 025import java.util.concurrent.TimeUnit; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.Admin; 031import org.apache.hadoop.hbase.client.Table; 032import org.apache.hadoop.hbase.security.User; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.testclassification.RegionServerTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 037import org.junit.AfterClass; 038import org.junit.BeforeClass; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042 043@Category({ RegionServerTests.class, MediumTests.class }) 044public class TestDefaultAtomicQuota { 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestDefaultAtomicQuota.class); 048 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 049 private static final TableName TABLE_NAME = TableName.valueOf(UUID.randomUUID().toString()); 050 private static final int REFRESH_TIME = 5; 051 private static final byte[] FAMILY = Bytes.toBytes("cf"); 052 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 053 054 @AfterClass 055 public static void tearDown() throws Exception { 056 ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL); 057 EnvironmentEdgeManager.reset(); 058 TEST_UTIL.deleteTable(TABLE_NAME); 059 TEST_UTIL.shutdownMiniCluster(); 060 } 061 062 @BeforeClass 063 public static void setUpBeforeClass() throws Exception { 064 // quotas enabled, using block bytes scanned 065 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 066 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME); 067 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_ATOMIC_READ_SIZE, 1); 068 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_ATOMIC_REQUEST_NUM, 1); 069 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_ATOMIC_WRITE_SIZE, 1); 070 071 // don't cache blocks to make IO predictable 072 TEST_UTIL.getConfiguration().setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f); 073 074 TEST_UTIL.startMiniCluster(1); 075 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 076 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 077 TEST_UTIL.waitTableAvailable(TABLE_NAME); 078 QuotaCache.TEST_FORCE_REFRESH = true; 079 TEST_UTIL.flush(TABLE_NAME); 080 } 081 082 @Test 083 public void testDefaultAtomicReadLimits() throws Exception { 084 // No write throttling 085 configureLenientThrottle(ThrottleType.ATOMIC_WRITE_SIZE); 086 refreshQuotas(); 087 088 // Should have a strict throttle by default 089 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 090 091 // Add big quota and should be effectively unlimited 092 configureLenientThrottle(ThrottleType.ATOMIC_READ_SIZE); 093 configureLenientThrottle(ThrottleType.ATOMIC_REQUEST_NUMBER); 094 refreshQuotas(); 095 // Should run without error 096 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) == 100); 097 098 // Remove all the limits, and should revert to strict default 099 unsetQuota(); 100 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 101 } 102 103 @Test 104 public void testDefaultAtomicWriteLimits() throws Exception { 105 // No read throttling 106 configureLenientThrottle(ThrottleType.ATOMIC_REQUEST_NUMBER); 107 configureLenientThrottle(ThrottleType.ATOMIC_READ_SIZE); 108 refreshQuotas(); 109 110 // Should have a strict throttle by default 111 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 112 113 // Add big quota and should be effectively unlimited 114 configureLenientThrottle(ThrottleType.ATOMIC_WRITE_SIZE); 115 refreshQuotas(); 116 // Should run without error 117 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) == 100); 118 119 // Remove all the limits, and should revert to strict default 120 unsetQuota(); 121 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 122 } 123 124 private void configureLenientThrottle(ThrottleType throttleType) throws IOException { 125 try (Admin admin = TEST_UTIL.getAdmin()) { 126 admin.setQuota( 127 QuotaSettingsFactory.throttleUser(getUserName(), throttleType, 100_000, TimeUnit.SECONDS)); 128 } 129 } 130 131 private static String getUserName() throws IOException { 132 return User.getCurrent().getShortName(); 133 } 134 135 private void refreshQuotas() throws Exception { 136 triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME); 137 waitMinuteQuota(); 138 } 139 140 private void unsetQuota() throws Exception { 141 try (Admin admin = TEST_UTIL.getAdmin()) { 142 admin.setQuota(QuotaSettingsFactory.unthrottleUser(getUserName())); 143 } 144 refreshQuotas(); 145 } 146 147 private long runIncTest(int attempts) throws Exception { 148 refreshQuotas(); 149 try (Table table = getTable()) { 150 return ThrottleQuotaTestUtil.doIncrements(attempts, FAMILY, QUALIFIER, table); 151 } 152 } 153 154 private Table getTable() throws IOException { 155 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100); 156 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 157 return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250) 158 .build(); 159 } 160}