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.triggerUserCacheRefresh;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.hamcrest.Matchers.lessThan;
023
024import java.io.IOException;
025import java.util.UUID;
026import java.util.concurrent.TimeUnit;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.security.User;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.RegionServerTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
037import org.junit.jupiter.api.AfterAll;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041
042@Tag(RegionServerTests.TAG)
043@Tag(MediumTests.TAG)
044public class TestThreadHandlerUsageQuota {
045
046  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
047  private static final TableName TABLE_NAME = TableName.valueOf(UUID.randomUUID().toString());
048  private static final byte[] FAMILY = Bytes.toBytes("cf");
049  private static final byte[] QUALIFIER = Bytes.toBytes("q");
050  private static final int MAX_OPS = 1000;
051
052  @AfterAll
053  public static void tearDown() throws Exception {
054    ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL);
055    EnvironmentEdgeManager.reset();
056    TEST_UTIL.deleteTable(TABLE_NAME);
057    TEST_UTIL.shutdownMiniCluster();
058  }
059
060  @BeforeAll
061  public static void setUpBeforeClass() throws Exception {
062    // Enable quotas
063    TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
064
065    // Don't cache blocks to make IO predictable
066    TEST_UTIL.getConfiguration().setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f);
067
068    TEST_UTIL.startMiniCluster(1);
069    TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
070
071    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
072
073    TEST_UTIL.waitTableAvailable(TABLE_NAME);
074    TEST_UTIL.flush(TABLE_NAME);
075  }
076
077  @Test
078  public void testHandlerUsageThrottleForReads() throws Exception {
079    try (Table table = getTable()) {
080      unthrottleUser();
081      long unthrottledAttempts = ThrottleQuotaTestUtil.doGets(MAX_OPS, FAMILY, QUALIFIER, table);
082
083      configureThrottle();
084      long throttledAttempts = ThrottleQuotaTestUtil.doGets(MAX_OPS, FAMILY, QUALIFIER, table);
085      assertThat("Throttled attempts should be less than unthrottled attempts", throttledAttempts,
086        lessThan(unthrottledAttempts));
087    }
088  }
089
090  @Test
091  public void testHandlerUsageThrottleForWrites() throws Exception {
092    try (Table table = getTable()) {
093      unthrottleUser();
094      long unthrottledAttempts = ThrottleQuotaTestUtil.doPuts(MAX_OPS, FAMILY, QUALIFIER, table);
095
096      configureThrottle();
097      long throttledAttempts = ThrottleQuotaTestUtil.doPuts(MAX_OPS, FAMILY, QUALIFIER, table);
098      assertThat("Throttled attempts should be less than unthrottled attempts", throttledAttempts,
099        lessThan(unthrottledAttempts));
100    }
101  }
102
103  private void configureThrottle() throws Exception {
104    try (Admin admin = TEST_UTIL.getAdmin()) {
105      admin.setQuota(QuotaSettingsFactory.throttleUser(getUserName(),
106        ThrottleType.REQUEST_HANDLER_USAGE_MS, 1, TimeUnit.SECONDS));
107    }
108    triggerUserCacheRefresh(TEST_UTIL, false, TABLE_NAME);
109    // increase the tick so we can calculate a positive rpc request time, and then consume all the
110    // available quota and make request fail next time
111    ThrottleQuotaTestUtil.envEdge.setValue(System.currentTimeMillis() + 10000);
112  }
113
114  private void unthrottleUser() throws Exception {
115    try (Admin admin = TEST_UTIL.getAdmin()) {
116      admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(getUserName(),
117        ThrottleType.REQUEST_HANDLER_USAGE_MS));
118    }
119    triggerUserCacheRefresh(TEST_UTIL, true, TABLE_NAME);
120    // increase the tick here too to make sure that we truly skip the quota check, not because we
121    // pass the quota check
122    ThrottleQuotaTestUtil.envEdge.setValue(System.currentTimeMillis() + 10000);
123  }
124
125  private static String getUserName() throws IOException {
126    return User.getCurrent().getShortName();
127  }
128
129  private Table getTable() throws IOException {
130    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 100);
131    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
132    return TEST_UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(250)
133      .build();
134  }
135}