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 * http://www.apache.org/licenses/LICENSE-2.0
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.hadoop.hbase.quotas;
017
018import java.util.concurrent.atomic.AtomicLong;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
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.AfterClass;
029import org.junit.Before;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
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}