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