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.Assert.assertEquals; 022import static org.junit.Assert.assertTrue; 023 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseTestingUtility; 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 TestQuotaUserOverride { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestQuotaUserOverride.class); 048 049 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 050 private static final byte[] FAMILY = Bytes.toBytes("cf"); 051 private static final byte[] QUALIFIER = Bytes.toBytes("q"); 052 private static final int NUM_SERVERS = 1; 053 private static final String CUSTOM_OVERRIDE_KEY = "foo"; 054 055 private static final TableName TABLE_NAME = TableName.valueOf("TestQuotaUserOverride"); 056 057 @BeforeClass 058 public static void setUpBeforeClass() throws Exception { 059 TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true); 060 TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 1_000); 061 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 1); 062 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 0); 063 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 500); 064 TEST_UTIL.getConfiguration().set(QuotaCache.QUOTA_USER_REQUEST_ATTRIBUTE_OVERRIDE_KEY, 065 CUSTOM_OVERRIDE_KEY); 066 TEST_UTIL.startMiniCluster(NUM_SERVERS); 067 TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME); 068 QuotaCache.TEST_FORCE_REFRESH = true; 069 TEST_UTIL.createTable(TABLE_NAME, FAMILY); 070 } 071 072 @AfterClass 073 public static void tearDownAfterClass() throws Exception { 074 EnvironmentEdgeManager.reset(); 075 TEST_UTIL.shutdownMiniCluster(); 076 } 077 078 @Test 079 public void testUserGlobalThrottleWithCustomOverride() throws Exception { 080 final Admin admin = TEST_UTIL.getAdmin(); 081 final String userOverrideWithQuota = User.getCurrent().getShortName() + "123"; 082 083 // Add 6req/min limit 084 admin.setQuota(QuotaSettingsFactory.throttleUser(userOverrideWithQuota, 085 ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES)); 086 087 Table tableWithThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) 088 .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes(userOverrideWithQuota)).build(); 089 Table tableWithoutThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null) 090 .setRequestAttribute(QuotaCache.QUOTA_USER_REQUEST_ATTRIBUTE_OVERRIDE_KEY, 091 Bytes.toBytes(userOverrideWithQuota)) 092 .build(); 093 Table tableWithoutThrottle2 = 094 TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).build(); 095 096 // warm things up 097 doPuts(10, FAMILY, QUALIFIER, tableWithThrottle); 098 doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle); 099 doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2); 100 101 // should reject some requests 102 assertTrue(10 > doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); 103 // should accept all puts 104 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); 105 // should accept all puts 106 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2)); 107 108 // Remove all the limits 109 admin.setQuota(QuotaSettingsFactory.unthrottleUser(userOverrideWithQuota)); 110 Thread.sleep(60_000); 111 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithThrottle)); 112 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle)); 113 assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle2)); 114 } 115 116}