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.TableName;
024import org.apache.hadoop.hbase.client.Put;
025import org.apache.hadoop.hbase.testclassification.LargeTests;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.junit.AfterClass;
028import org.junit.Before;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Rule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.junit.rules.TestName;
035
036@Category(LargeTests.class)
037public class TestSpaceQuotaSwitchPolicies {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestSpaceQuotaSwitchPolicies.class);
042
043  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
044
045  @Rule
046  public TestName testName = new TestName();
047  private SpaceQuotaHelperForTests helper;
048
049  @BeforeClass
050  public static void setUp() throws Exception {
051    Configuration conf = TEST_UTIL.getConfiguration();
052    SpaceQuotaHelperForTests.updateConfigForQuotas(conf);
053    TEST_UTIL.startMiniCluster(1);
054  }
055
056  @AfterClass
057  public static void tearDown() throws Exception {
058    TEST_UTIL.shutdownMiniCluster();
059  }
060
061  @Before
062  public void removeAllQuotas() throws Exception {
063    helper = new SpaceQuotaHelperForTests(TEST_UTIL, testName, new AtomicLong(0));
064    helper.removeAllQuotas();
065  }
066
067  @Test
068  public void testSetQuotaFirstWithDisableNextNoWrites() throws Exception {
069    setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy.DISABLE,
070        SpaceViolationPolicy.NO_WRITES);
071  }
072
073  @Test
074  public void testSetQuotaFirstWithDisableNextAgainDisable() throws Exception {
075    setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy.DISABLE,
076        SpaceViolationPolicy.DISABLE);
077  }
078
079  @Test
080  public void testSetQuotaFirstWithDisableNextNoInserts() throws Exception {
081    setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy.DISABLE,
082        SpaceViolationPolicy.NO_INSERTS);
083  }
084
085  @Test
086  public void testSetQuotaFirstWithDisableNextNoWritesCompaction() throws Exception {
087    setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy.DISABLE,
088        SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
089  }
090
091  @Test
092  public void testSetQuotaFirstWithNoWritesNextWithDisable() throws Exception {
093    setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy.NO_WRITES,
094        SpaceViolationPolicy.DISABLE);
095  }
096
097  private void setQuotaAndViolateNextSwitchPoliciesAndValidate(SpaceViolationPolicy policy1,
098      SpaceViolationPolicy policy2) throws Exception {
099    Put put = new Put(Bytes.toBytes("to_reject"));
100    put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
101        Bytes.toBytes("reject"));
102
103    // Do puts until we violate space violation policy1
104    final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy1, put);
105
106    // Now, change violation policy to policy2
107    helper.setQuotaLimit(tn, policy2, 2L);
108
109    // The table should be in enabled state on changing violation policy
110    if (policy1.equals(SpaceViolationPolicy.DISABLE) && !policy1.equals(policy2)) {
111      TEST_UTIL.waitTableEnabled(tn, 20000);
112    }
113    // Put some row now: should still violate as quota limit still violated
114    helper.verifyViolation(policy2, tn, put);
115  }
116}
117