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.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 TestQuotaUserOverride {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestQuotaUserOverride.class);
048
049  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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();
082
083    // Add 6req/min limit
084    admin.setQuota(QuotaSettingsFactory.throttleUser(userOverrideWithQuota,
085      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
086    ThrottleQuotaTestUtil.triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME);
087
088    Table tableWithThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null)
089      .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes(userOverrideWithQuota)).build();
090    Table tableWithoutThrottle = TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null)
091      .setRequestAttribute(CUSTOM_OVERRIDE_KEY, Bytes.toBytes("anotherUser")).build();
092
093    // warm things up
094    doPuts(10, FAMILY, QUALIFIER, tableWithThrottle);
095    doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle);
096
097    // should reject some requests
098    assertTrue(10 > doPuts(10, FAMILY, QUALIFIER, tableWithThrottle));
099    // should accept all puts
100    assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle));
101
102    // Remove all the limits
103    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userOverrideWithQuota));
104    ThrottleQuotaTestUtil.triggerUserCacheRefresh(TEST_UTIL, true, TABLE_NAME);
105    assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithThrottle));
106    assertEquals(10, doPuts(10, FAMILY, QUALIFIER, tableWithoutThrottle));
107  }
108
109}