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 TestSpaceQuotaRemoval {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestSpaceQuotaRemoval.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 testSetQuotaAndThenRemoveInOneWithNoInserts() throws Exception {
069    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_INSERTS);
070  }
071
072  @Test
073  public void testSetQuotaAndThenRemoveInOneWithNoWrite() throws Exception {
074    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES);
075  }
076
077  @Test
078  public void testSetQuotaAndThenRemoveInOneWithNoWritesCompaction() throws Exception {
079    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
080  }
081
082  @Test
083  public void testSetQuotaAndThenRemoveInOneWithDisable() throws Exception {
084    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.DISABLE);
085  }
086
087  @Test
088  public void testSetQuotaAndThenRemoveWithNoInserts() throws Exception {
089    setQuotaAndThenRemove(SpaceViolationPolicy.NO_INSERTS);
090  }
091
092  @Test
093  public void testSetQuotaAndThenRemoveWithNoWrite() throws Exception {
094    setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES);
095  }
096
097  @Test
098  public void testSetQuotaAndThenRemoveWithNoWritesCompactions() throws Exception {
099    setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
100  }
101
102  @Test
103  public void testSetQuotaAndThenRemoveWithDisable() throws Exception {
104    setQuotaAndThenRemove(SpaceViolationPolicy.DISABLE);
105  }
106
107  @Test
108  public void testSetQuotaAndThenDisableIncrEnableWithNoInserts() throws Exception {
109    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_INSERTS);
110  }
111
112  @Test
113  public void testSetQuotaAndThenDisableIncrEnableWithNoWrite() throws Exception {
114    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES);
115  }
116
117  @Test
118  public void testSetQuotaAndThenDisableIncrEnableWithNoWritesCompaction() throws Exception {
119    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
120  }
121
122  @Test
123  public void testSetQuotaAndThenDisableIncrEnableWithDisable() throws Exception {
124    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.DISABLE);
125  }
126
127  private void setQuotaAndThenRemove(SpaceViolationPolicy policy) throws Exception {
128    Put put = new Put(Bytes.toBytes("to_reject"));
129    put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
130        Bytes.toBytes("reject"));
131
132    // Do puts until we violate space policy
133    final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
134
135    // Now, remove the quota
136    helper.removeQuotaFromtable(tn);
137
138    // Put some rows now: should not violate as quota settings removed
139    helper.verifyNoViolation(tn, put);
140  }
141
142  private void setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy policy)
143      throws Exception {
144    Put put = new Put(Bytes.toBytes("to_reject"));
145    put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
146        Bytes.toBytes("reject"));
147
148    // Do puts until we violate space policy on table tn1
149    final TableName tn1 = helper.writeUntilViolationAndVerifyViolation(policy, put);
150
151    // Do puts until we violate space policy on table tn2
152    final TableName tn2 = helper.writeUntilViolationAndVerifyViolation(policy, put);
153
154    // Now, remove the quota from table tn1
155    helper.removeQuotaFromtable(tn1);
156
157    // Put a new row now on tn1: should not violate as quota settings removed
158    helper.verifyNoViolation(tn1, put);
159    // Put a new row now on tn2: should violate as quota settings exists
160    helper.verifyViolation(policy, tn2, put);
161  }
162
163  private void setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy policy)
164      throws Exception {
165    Put put = new Put(Bytes.toBytes("to_reject"));
166    put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
167        Bytes.toBytes("reject"));
168
169    // Do puts until we violate space policy
170    final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
171
172    // Disable the table; in case of SpaceViolationPolicy.DISABLE already disabled
173    if (!policy.equals(SpaceViolationPolicy.DISABLE)) {
174      TEST_UTIL.getAdmin().disableTable(tn);
175      TEST_UTIL.waitTableDisabled(tn, 10000);
176    }
177
178    // Now, increase limit and perform put
179    helper.setQuotaLimit(tn, policy, 4L);
180
181    // in case of disable policy quota manager will enable it
182    if (!policy.equals(SpaceViolationPolicy.DISABLE)) {
183      TEST_UTIL.getAdmin().enableTable(tn);
184    }
185    TEST_UTIL.waitTableEnabled(tn, 10000);
186
187    // Put some row now: should not violate as quota limit increased
188    helper.verifyNoViolation(tn, put);
189  }
190}