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.util.concurrent.atomic.AtomicLong;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.NamespaceDescriptor;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.AfterAll;
029import org.junit.jupiter.api.BeforeAll;
030import org.junit.jupiter.api.BeforeEach;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.junit.jupiter.api.TestInfo;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@Tag(LargeTests.TAG)
038public class TestSpaceQuotasWithRegionReplicas {
039
040  private static final Logger LOG =
041    LoggerFactory.getLogger(TestSpaceQuotasWithRegionReplicas.class);
042  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
043
044  private SpaceQuotaHelperForTests helper;
045
046  @BeforeAll
047  public static void setUp() throws Exception {
048    Configuration conf = TEST_UTIL.getConfiguration();
049    SpaceQuotaHelperForTests.updateConfigForQuotas(conf);
050    TEST_UTIL.startMiniCluster(1);
051  }
052
053  @AfterAll
054  public static void tearDown() throws Exception {
055    TEST_UTIL.shutdownMiniCluster();
056  }
057
058  @BeforeEach
059  public void removeAllQuotas(TestInfo testInfo) throws Exception {
060    helper = new SpaceQuotaHelperForTests(TEST_UTIL, () -> testInfo.getTestMethod().get().getName(),
061      new AtomicLong(0));
062    helper.removeAllQuotas();
063  }
064
065  @Test
066  public void testSetQuotaWithRegionReplicaSingleRegion() throws Exception {
067    for (SpaceViolationPolicy policy : SpaceViolationPolicy.values()) {
068      setQuotaAndVerifyForRegionReplication(1, 2, policy);
069    }
070  }
071
072  @Test
073  public void testSetQuotaWithRegionReplicaMultipleRegion() throws Exception {
074    for (SpaceViolationPolicy policy : SpaceViolationPolicy.values()) {
075      setQuotaAndVerifyForRegionReplication(6, 3, policy);
076    }
077  }
078
079  @Test
080  public void testSetQuotaWithSingleRegionZeroRegionReplica() throws Exception {
081    for (SpaceViolationPolicy policy : SpaceViolationPolicy.values()) {
082      setQuotaAndVerifyForRegionReplication(1, 0, policy);
083    }
084  }
085
086  @Test
087  public void testSetQuotaWithMultipleRegionZeroRegionReplicas() throws Exception {
088    for (SpaceViolationPolicy policy : SpaceViolationPolicy.values()) {
089      setQuotaAndVerifyForRegionReplication(6, 0, policy);
090    }
091  }
092
093  private void setQuotaAndVerifyForRegionReplication(int region, int replicatedRegion,
094    SpaceViolationPolicy policy) throws Exception {
095    TableName tn = helper.createTableWithRegions(TEST_UTIL.getAdmin(),
096      NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR, region, replicatedRegion);
097    helper.setQuotaLimit(tn, policy, 5L);
098    helper.writeData(tn, 5L * SpaceQuotaHelperForTests.ONE_MEGABYTE);
099    Put p = new Put(Bytes.toBytes("to_reject"));
100    p.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
101      Bytes.toBytes("reject"));
102    helper.verifyViolation(policy, tn, p);
103  }
104}