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.waitMinuteQuota;
021import static org.junit.Assert.assertEquals;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.testclassification.RegionServerTests;
027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
028import org.apache.hadoop.security.UserGroupInformation;
029import org.junit.After;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({ RegionServerTests.class, MediumTests.class })
036public class TestQuotaCache {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestQuotaCache.class);
041
042  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
043  private static final int REFRESH_TIME = 30_000;
044
045  @After
046  public void tearDown() throws Exception {
047    ThrottleQuotaTestUtil.clearQuotaCache(TEST_UTIL);
048    EnvironmentEdgeManager.reset();
049    TEST_UTIL.shutdownMiniCluster();
050  }
051
052  @BeforeClass
053  public static void setUpBeforeClass() throws Exception {
054    TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
055    TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, REFRESH_TIME);
056    TEST_UTIL.getConfiguration().setInt(QuotaUtil.QUOTA_DEFAULT_USER_MACHINE_READ_NUM, 1000);
057
058    TEST_UTIL.startMiniCluster(1);
059    TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
060  }
061
062  @Test
063  public void testDefaultUserRefreshFrequency() throws Exception {
064    QuotaCache.TEST_BLOCK_REFRESH = true;
065
066    QuotaCache quotaCache =
067      ThrottleQuotaTestUtil.getQuotaCaches(TEST_UTIL).stream().findAny().get();
068    UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
069
070    UserQuotaState userQuotaState = quotaCache.getUserQuotaState(ugi);
071    assertEquals(userQuotaState.getLastUpdate(), 0);
072
073    QuotaCache.TEST_BLOCK_REFRESH = false;
074    // new user should have refreshed immediately
075    TEST_UTIL.waitFor(5_000, () -> userQuotaState.getLastUpdate() != 0);
076    long lastUpdate = userQuotaState.getLastUpdate();
077
078    // refresh should not apply to recently refreshed quota
079    quotaCache.triggerCacheRefresh();
080    Thread.sleep(250);
081    long newLastUpdate = userQuotaState.getLastUpdate();
082    assertEquals(lastUpdate, newLastUpdate);
083
084    quotaCache.triggerCacheRefresh();
085    waitMinuteQuota();
086    // should refresh after time has passed
087    TEST_UTIL.waitFor(5_000, () -> lastUpdate != userQuotaState.getLastUpdate());
088  }
089}