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