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    TEST_UTIL.flush(TABLE_NAME);
076  }
077
078  @Test
079  public void testDefaultHandlerUsageLimits() throws Exception {
080    // Should have a strict throttle by default
081    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) < 100);
082
083    // Add big quota and should be effectively unlimited
084    configureLenientThrottle();
085    // Should run without error
086    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) == 100);
087
088    // Remove all the limits, and should revert to strict default
089    unsetQuota();
090    TEST_UTIL.waitFor(60_000, () -> runPutTest(100) < 100);
091  }
092
093  private void configureLenientThrottle() throws IOException {
094    try (Admin admin = TEST_UTIL.getAdmin()) {
095      admin.setQuota(QuotaSettingsFactory.throttleUser(getUserName(),
096        ThrottleType.REQUEST_HANDLER_USAGE_MS, 100_000, TimeUnit.SECONDS));
097    }
098  }
099
100  private static String getUserName() throws IOException {
101    return User.getCurrent().getShortName();
102  }
103
104  private void unsetQuota() throws Exception {
105    try (Admin admin = TEST_UTIL.getAdmin()) {
106      admin.setQuota(QuotaSettingsFactory.unthrottleUser(getUserName()));
107    }
108  }
109
110  private long runPutTest(int attempts) throws Exception {
111    try (Table table = getTable()) {
112      return ThrottleQuotaTestUtil.doPuts(attempts, FAMILY, QUALIFIER, table);
113    }
114  }
115
116  private Table getTable() throws IOException {
117    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100);
118    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
119    return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250)
120      .build();
121  }
122}