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 java.io.IOException;
021import java.util.UUID;
022import java.util.concurrent.TimeUnit;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.security.User;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category({ RegionServerTests.class, MediumTests.class })
041public class TestDefaultHandlerUsageQuota {
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestDefaultHandlerUsageQuota.class);
045  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046  private static final TableName TABLE_NAME = TableName.valueOf(UUID.randomUUID().toString());
047  private static final int REFRESH_TIME = 5;
048  private static final byte[] FAMILY = Bytes.toBytes("cf");
049  private static final byte[] QUALIFIER = Bytes.toBytes("q");
050
051  @AfterClass
052  public static void tearDown() throws Exception {
053    ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL);
054    EnvironmentEdgeManager.reset();
055    TEST_UTIL.deleteTable(TABLE_NAME);
056    TEST_UTIL.shutdownMiniCluster();
057  }
058
059  @BeforeClass
060  public static void setUp() throws Exception {
061    // quotas enabled, using block bytes scanned
062    TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
063    TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME);
064    // Set default to very strict
065    TEST_UTIL.getConfiguration()
066      .setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_REQUEST_HANDLER_USAGE_MS, 10);
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    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
074    TEST_UTIL.waitTableAvailable(TABLE_NAME);
075    QuotaCache.TEST_FORCE_REFRESH = true;
076    TEST_UTIL.flush(TABLE_NAME);
077  }
078
079  @Test
080  public void testDefaultHandlerUsageLimits() throws Exception {
081    // Should have a strict throttle by default
082    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) < 100);
083
084    // Add big quota and should be effectively unlimited
085    configureLenientThrottle();
086    // Should run without error
087    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) == 100);
088
089    // Remove all the limits, and should revert to strict default
090    unsetQuota();
091    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) < 100);
092  }
093
094  private void configureLenientThrottle() throws IOException {
095    try (Admin admin = TEST_UTIL.getAdmin()) {
096      admin.setQuota(QuotaSettingsFactory.throttleUser(getUserName(),
097        ThrottleType.REQUEST_HANDLER_USAGE_MS, 100_000, TimeUnit.SECONDS));
098    }
099  }
100
101  private static String getUserName() throws IOException {
102    return User.getCurrent().getShortName();
103  }
104
105  private void unsetQuota() throws Exception {
106    try (Admin admin = TEST_UTIL.getAdmin()) {
107      admin.setQuota(QuotaSettingsFactory.unthrottleUser(getUserName()));
108    }
109  }
110
111  private long runPutTest(int attempts) throws Exception {
112    try (Table table = getTable()) {
113      return ThrottleQuotaTestUtil.doPuts(attempts, FAMILY, QUALIFIER, table);
114    }
115  }
116
117  private Table getTable() throws IOException {
118    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100);
119    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
120    return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250)
121      .build();
122  }
123}