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.AfterAll; 037import org.junit.jupiter.api.BeforeAll; 038import org.junit.jupiter.api.Tag; 039import org.junit.jupiter.api.Test; 040 041@Tag(RegionServerTests.TAG) 042@Tag(MediumTests.TAG) 043public class TestDefaultAtomicQuota { 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 = 5; 047 private static final byte[] FAMILY = Bytes.toBytes("cf"); 048 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 049 050 @AfterAll 051 public static 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 @BeforeAll 059 public static void setUpBeforeClass() 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_ATOMIC_READ_SIZE, 1); 064 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_ATOMIC_REQUEST_NUM, 1); 065 TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_ATOMIC_WRITE_SIZE, 1); 066 067 // don't cache blocks to make IO predictable 068 TEST_UTIL.getConfiguration().setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f); 069 070 TEST_UTIL.startMiniCluster(1); 071 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 072 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 073 TEST_UTIL.waitTableAvailable(TABLE_NAME); 074 TEST_UTIL.flush(TABLE_NAME); 075 } 076 077 @Test 078 public void testDefaultAtomicReadLimits() throws Exception { 079 // Should have a strict throttle by default 080 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 081 082 // Add big quota and should be effectively unlimited 083 configureLenientThrottle(ThrottleType.ATOMIC_READ_SIZE); 084 configureLenientThrottle(ThrottleType.ATOMIC_REQUEST_NUMBER); 085 refreshQuotas(); 086 // Should run without error 087 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) == 100); 088 089 // Remove all the limits, and should revert to strict default 090 unsetQuota(); 091 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 092 } 093 094 @Test 095 public void testDefaultAtomicWriteLimits() throws Exception { 096 // Should have a strict throttle by default 097 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 098 099 // Add big quota and should be effectively unlimited 100 configureLenientThrottle(ThrottleType.ATOMIC_WRITE_SIZE); 101 refreshQuotas(); 102 // Should run without error 103 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) == 100); 104 105 // Remove all the limits, and should revert to strict default 106 unsetQuota(); 107 TEST_UTIL.waitFor(60_000, () -> runIncTest(100) < 100); 108 } 109 110 private void configureLenientThrottle(ThrottleType throttleType) throws IOException { 111 try (Admin admin = TEST_UTIL.getAdmin()) { 112 admin.setQuota( 113 QuotaSettingsFactory.throttleUser(getUserName(), throttleType, 100_000, TimeUnit.SECONDS)); 114 } 115 } 116 117 private static String getUserName() throws IOException { 118 return User.getCurrent().getShortName(); 119 } 120 121 private void refreshQuotas() throws Exception { 122 triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME); 123 waitMinuteQuota(); 124 } 125 126 private void unsetQuota() throws Exception { 127 try (Admin admin = TEST_UTIL.getAdmin()) { 128 admin.setQuota(QuotaSettingsFactory.unthrottleUser(getUserName())); 129 } 130 refreshQuotas(); 131 } 132 133 private long runIncTest(int attempts) throws Exception { 134 refreshQuotas(); 135 try (Table table = getTable()) { 136 return ThrottleQuotaTestUtil.doIncrements(attempts, FAMILY, QUALIFIER, table); 137 } 138 } 139 140 private Table getTable() throws IOException { 141 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100); 142 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 143 return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250) 144 .build(); 145 } 146}