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.doPuts; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.Admin; 029import org.apache.hadoop.hbase.client.Table; 030import org.apache.hadoop.hbase.security.User; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.testclassification.RegionServerTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 035import org.junit.jupiter.api.AfterAll; 036import org.junit.jupiter.api.BeforeAll; 037import org.junit.jupiter.api.Tag; 038import org.junit.jupiter.api.Test; 039 040@Tag(RegionServerTests.TAG) 041@Tag(MediumTests.TAG) 042public class TestQuotaUserOverride { 043 044 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 045 private static final byte[] FAMILY = Bytes.toBytes("cf"); 046 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 047 private static final int NUM_SERVERS = 1; 048 private static final String CUSTOM_OVERRIDE_KEY = "foo"; 049 050 private static final TableName TABLE_NAME = TableName.valueOf("TestQuotaUserOverride"); 051 052 @BeforeAll 053 public static void setUpBeforeClass() throws Exception { 054 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 055 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 1_000); 056 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 1); 057 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 0); 058 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 500); 059 TEST_UTIL.getConfiguration().set(QuotaCache.QUOTA_USER_REQUEST_ATTRIBUTE_OVERRIDE_KEY, 060 CUSTOM_OVERRIDE_KEY); 061 TEST_UTIL.startMiniCluster(NUM_SERVERS); 062 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 063 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 064 } 065 066 @AfterAll 067 public static void tearDownAfterClass() throws Exception { 068 EnvironmentEdgeManager.reset(); 069 TEST_UTIL.shutdownMiniCluster(); 070 } 071 072 @Test 073 public void testUserGlobalThrottleWithCustomOverride() throws Exception { 074 final Admin admin = TEST_UTIL.getAdmin(); 075 final String userOverrideWithQuota = User.getCurrent().getShortName(); 076 077 // Add 6req/min limit 078 admin.setQuota(QuotaSettingsFactory.throttleUser(userOverrideWithQuota, 079 ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES)); 080 ThrottleQuotaTestUtil.triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME); 081 082 Table tableWithThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) 083 .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes(userOverrideWithQuota)).build(); 084 Table tableWithoutThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) 085 .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes("anotherUser")).build(); 086 087 // warm things up 088 doPuts(10, FAMILY, QUALIFIER, tableWithThrottle); 089 doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle); 090 091 // should reject some requests 092 assertTrue(10 > doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); 093 // should accept all puts 094 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); 095 096 // Remove all the limits 097 admin.setQuota(QuotaSettingsFactory.unthrottleUser(userOverrideWithQuota)); 098 ThrottleQuotaTestUtil.triggerUserCacheRefresh(TEST_UTIL, true, TABLE_NAME); 099 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); 100 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); 101 } 102 103}