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.HBaseTestingUtil; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.Admin; 030import org.apache.hadoop.hbase.client.Table; 031import org.apache.hadoop.hbase.security.User; 032import org.apache.hadoop.hbase.testclassification.MediumTests; 033import org.apache.hadoop.hbase.testclassification.RegionServerTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 036import org.junit.jupiter.api.AfterEach; 037import org.junit.jupiter.api.BeforeEach; 038import org.junit.jupiter.api.Tag; 039import org.junit.jupiter.api.Test; 040 041@Tag(RegionServerTests.TAG) 042@Tag(MediumTests.TAG) 043public class TestDefaultQuota { 044 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 045 private static final TableName TABLE_NAME = TableName.valueOf(UUID.randomUUID().toString()); 046 private static final int REFRESH_TIME = 5000; 047 private static final byte[] FAMILY = Bytes.toBytes("cf"); 048 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 049 050 @AfterEach 051 public void tearDown() throws Exception { 052 ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL); 053 EnvironmentEdgeManager.reset(); 054 TEST_UTIL.deleteTable(TABLE_NAME); 055 TEST_UTIL.shutdownMiniCluster(); 056 } 057 058 @BeforeEach 059 public void setUp() throws Exception { 060 // quotas enabled, using block bytes scanned 061 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 062 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME); 063 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_READ_NUM, 1); 064 065 // don't cache blocks to make IO predictable 066 TEST_UTIL.getConfiguration().setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f); 067 068 TEST_UTIL.startMiniCluster(1); 069 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 070 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 071 TEST_UTIL.waitTableAvailable(TABLE_NAME); 072 073 try (Admin admin = TEST_UTIL.getAdmin()) { 074 ThrottleQuotaTestUtil.doPuts(1_000, FAMILY, QUALIFIER, 075 admin.getConnection().getTable(TABLE_NAME)); 076 } 077 TEST_UTIL.flush(TABLE_NAME); 078 } 079 080 @Test 081 public void testDefaultUserReadNum() throws Exception { 082 // Should have a strict throttle by default 083 TEST_UTIL.waitFor(60_000, () -> runGetsTest(100) < 100); 084 085 // Add big quota and should be effectively unlimited 086 configureLenientThrottle(); 087 refreshQuotas(); 088 // Should run without error 089 TEST_UTIL.waitFor(60_000, () -> runGetsTest(100) == 100); 090 091 // Remove all the limits, and should revert to strict default 092 unsetQuota(); 093 TEST_UTIL.waitFor(60_000, () -> runGetsTest(100) < 100); 094 } 095 096 private void configureLenientThrottle() throws IOException { 097 try (Admin admin = TEST_UTIL.getAdmin()) { 098 admin.setQuota(QuotaSettingsFactory.throttleUser(getUserName(), ThrottleType.READ_NUMBER, 099 100_000, TimeUnit.SECONDS)); 100 } 101 } 102 103 private static String getUserName() throws IOException { 104 return User.getCurrent().getShortName(); 105 } 106 107 private void refreshQuotas() throws Exception { 108 triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME); 109 waitMinuteQuota(); 110 } 111 112 private void unsetQuota() throws Exception { 113 try (Admin admin = TEST_UTIL.getAdmin()) { 114 admin.setQuota(QuotaSettingsFactory.unthrottleUser(getUserName())); 115 } 116 refreshQuotas(); 117 } 118 119 private long runGetsTest(int attempts) throws Exception { 120 try (Table table = getTable()) { 121 return ThrottleQuotaTestUtil.doGets(attempts, FAMILY, QUALIFIER, table); 122 } 123 } 124 125 private Table getTable() throws IOException { 126 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100); 127 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 128 return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250) 129 .build(); 130 } 131 132}