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.Assert.assertTrue; 021 022import java.io.IOException; 023import java.util.UUID; 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 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.AfterClass; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042@Category({ RegionServerTests.class, MediumTests.class }) 043public class TestThreadHandlerUsageQuota { 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestThreadHandlerUsageQuota.class); 047 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 048 private static final TableName TABLE_NAME = TableName.valueOf(UUID.randomUUID().toString()); 049 private static final int REFRESH_TIME = 5; 050 private static final byte[] FAMILY = Bytes.toBytes("cf"); 051 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 052 private static final int MAX_OPS = 1000; 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 // Enable quotas 065 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 066 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME); 067 068 // Don't cache blocks to make IO predictable 069 TEST_UTIL.getConfiguration().setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f); 070 071 TEST_UTIL.startMiniCluster(1); 072 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 073 074 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 075 076 TEST_UTIL.waitTableAvailable(TABLE_NAME); 077 QuotaCache.TEST_FORCE_REFRESH = true; 078 TEST_UTIL.flush(TABLE_NAME); 079 } 080 081 @Test 082 public void testHandlerUsageThrottleForReads() throws Exception { 083 try (Table table = getTable()) { 084 unthrottleUser(); 085 long unthrottledAttempts = ThrottleQuotaTestUtil.doGets(MAX_OPS, FAMILY, QUALIFIER, table); 086 087 configureThrottle(); 088 long throttledAttempts = ThrottleQuotaTestUtil.doGets(MAX_OPS, FAMILY, QUALIFIER, table); 089 assertTrue("Throttled attempts should be less than unthrottled attempts", 090 throttledAttempts < unthrottledAttempts); 091 } 092 } 093 094 @Test 095 public void testHandlerUsageThrottleForWrites() throws Exception { 096 try (Table table = getTable()) { 097 unthrottleUser(); 098 long unthrottledAttempts = ThrottleQuotaTestUtil.doPuts(MAX_OPS, FAMILY, QUALIFIER, table); 099 100 configureThrottle(); 101 long throttledAttempts = ThrottleQuotaTestUtil.doPuts(MAX_OPS, FAMILY, QUALIFIER, table); 102 assertTrue("Throttled attempts should be less than unthrottled attempts", 103 throttledAttempts < unthrottledAttempts); 104 } 105 } 106 107 private void configureThrottle() throws IOException { 108 try (Admin admin = TEST_UTIL.getAdmin()) { 109 admin.setQuota(QuotaSettingsFactory.throttleUser(getUserName(), 110 ThrottleType.REQUEST_HANDLER_USAGE_MS, 10000, TimeUnit.SECONDS)); 111 } 112 } 113 114 private void unthrottleUser() throws Exception { 115 try (Admin admin = TEST_UTIL.getAdmin()) { 116 admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(getUserName(), 117 ThrottleType.REQUEST_HANDLER_USAGE_MS)); 118 } 119 } 120 121 private static String getUserName() throws IOException { 122 return User.getCurrent().getShortName(); 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}