HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / quotas / TestSpaceQuotaRemoval.java
blobd94b0d5517a92cbe02464cc29a7b29ef1a7ec715
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.apache.hadoop.hbase.quotas;
18 import java.util.concurrent.atomic.AtomicLong;
20 import org.apache.hadoop.conf.Configuration;
21 import org.apache.hadoop.hbase.HBaseClassTestRule;
22 import org.apache.hadoop.hbase.HBaseTestingUtil;
23 import org.apache.hadoop.hbase.NamespaceDescriptor;
24 import org.apache.hadoop.hbase.TableName;
25 import org.apache.hadoop.hbase.client.Put;
26 import org.apache.hadoop.hbase.testclassification.LargeTests;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.ClassRule;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35 import org.junit.rules.TestName;
37 @Category(LargeTests.class)
38 public class TestSpaceQuotaRemoval {
40 @ClassRule
41 public static final HBaseClassTestRule CLASS_RULE =
42 HBaseClassTestRule.forClass(TestSpaceQuotaRemoval.class);
44 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
46 @Rule
47 public TestName testName = new TestName();
48 private SpaceQuotaHelperForTests helper;
50 @BeforeClass
51 public static void setUp() throws Exception {
52 Configuration conf = TEST_UTIL.getConfiguration();
53 SpaceQuotaHelperForTests.updateConfigForQuotas(conf);
54 TEST_UTIL.startMiniCluster(1);
57 @AfterClass
58 public static void tearDown() throws Exception {
59 TEST_UTIL.shutdownMiniCluster();
62 @Before
63 public void removeAllQuotas() throws Exception {
64 helper = new SpaceQuotaHelperForTests(TEST_UTIL, testName, new AtomicLong(0));
65 helper.removeAllQuotas();
68 @Test
69 public void testSetQuotaAndThenRemoveInOneWithNoInserts() throws Exception {
70 setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_INSERTS);
73 @Test
74 public void testSetQuotaAndThenRemoveInOneWithNoWrite() throws Exception {
75 setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES);
78 @Test
79 public void testSetQuotaAndThenRemoveInOneWithNoWritesCompaction() throws Exception {
80 setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
83 @Test
84 public void testSetQuotaAndThenRemoveInOneWithDisable() throws Exception {
85 setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.DISABLE);
88 @Test
89 public void testSetQuotaAndThenRemoveWithNoInserts() throws Exception {
90 setQuotaAndThenRemove(SpaceViolationPolicy.NO_INSERTS);
93 @Test
94 public void testSetQuotaAndThenRemoveWithNoWrite() throws Exception {
95 setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES);
98 @Test
99 public void testSetQuotaAndThenRemoveWithNoWritesCompactions() throws Exception {
100 setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
103 @Test
104 public void testSetQuotaAndThenRemoveWithDisable() throws Exception {
105 setQuotaAndThenRemove(SpaceViolationPolicy.DISABLE);
108 @Test
109 public void testSetQuotaAndThenDisableIncrEnableWithNoInserts() throws Exception {
110 setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_INSERTS);
113 @Test
114 public void testSetQuotaAndThenDisableIncrEnableWithNoWrite() throws Exception {
115 setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES);
118 @Test
119 public void testSetQuotaAndThenDisableIncrEnableWithNoWritesCompaction() throws Exception {
120 setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
123 @Test
124 public void testSetQuotaAndThenDisableIncrEnableWithDisable() throws Exception {
125 setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.DISABLE);
128 private void setQuotaAndThenRemove(SpaceViolationPolicy policy) throws Exception {
129 Put put = new Put(Bytes.toBytes("to_reject"));
130 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
131 Bytes.toBytes("reject"));
133 // Do puts until we violate space policy
134 final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
136 // Now, remove the quota
137 helper.removeQuotaFromtable(tn);
139 // Put some rows now: should not violate as quota settings removed
140 helper.verifyNoViolation(tn, put);
143 @Test
144 public void testDeleteTableUsageSnapshotsForNamespace() throws Exception {
145 Put put = new Put(Bytes.toBytes("to_reject"));
146 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
147 Bytes.toBytes("reject"));
149 SpaceViolationPolicy policy = SpaceViolationPolicy.NO_INSERTS;
151 //Create a namespace
152 String ns1 = "nsnew";
153 NamespaceDescriptor nsd = helper.createNamespace(ns1);
155 //Create 2nd namespace with name similar to ns1
156 String ns2 = ns1 + "test";
157 NamespaceDescriptor nsd2 = helper.createNamespace(ns2);
159 // Do puts until we violate space policy on table tn1 in namesapce ns1
160 final TableName tn1 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns1, policy, put);
162 // Do puts until we violate space policy on table tn2 in namespace ns2
163 final TableName tn2 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns2, policy, put);
165 // Now, remove the quota from namespace ns1 which will remove table usage snapshots for ns1
166 helper.removeQuotaFromNamespace(ns1);
168 // Verify that table usage snapshot for table tn2 in namespace ns2 exist
169 helper.verifyTableUsageSnapshotForSpaceQuotaExist(tn2);
171 // Put a new row on tn2: should violate as space quota exists on namespace ns2
172 helper.verifyViolation(policy, tn2, put);
174 // Put a new row on tn1: should not violate as quota settings removed from namespace ns1
175 helper.verifyNoViolation(tn1, put);
178 @Test
179 public void testSetNamespaceSizeQuotaAndThenRemove() throws Exception {
180 Put put = new Put(Bytes.toBytes("to_reject"));
181 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
182 Bytes.toBytes("reject"));
184 SpaceViolationPolicy policy = SpaceViolationPolicy.NO_INSERTS;
186 //Create namespace
187 NamespaceDescriptor nsd = helper.createNamespace();
188 String ns = nsd.getName();
190 // Do puts until we violate space policy on table tn1
191 final TableName tn1 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns, policy, put);
193 // Now, remove the quota from namespace
194 helper.removeQuotaFromNamespace(ns);
196 // Put a new row now on tn1: should not violate as quota settings removed from namespace
197 helper.verifyNoViolation(tn1, put);
200 private void setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy policy)
201 throws Exception {
202 Put put = new Put(Bytes.toBytes("to_reject"));
203 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
204 Bytes.toBytes("reject"));
206 // Do puts until we violate space policy on table tn1
207 final TableName tn1 = helper.writeUntilViolationAndVerifyViolation(policy, put);
209 // Do puts until we violate space policy on table tn2
210 final TableName tn2 = helper.writeUntilViolationAndVerifyViolation(policy, put);
212 // Now, remove the quota from table tn1
213 helper.removeQuotaFromtable(tn1);
215 // Put a new row now on tn1: should not violate as quota settings removed
216 helper.verifyNoViolation(tn1, put);
217 // Put a new row now on tn2: should violate as quota settings exists
218 helper.verifyViolation(policy, tn2, put);
221 private void setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy policy)
222 throws Exception {
223 Put put = new Put(Bytes.toBytes("to_reject"));
224 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
225 Bytes.toBytes("reject"));
227 // Do puts until we violate space policy
228 final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
230 // Disable the table; in case of SpaceViolationPolicy.DISABLE already disabled
231 if (!policy.equals(SpaceViolationPolicy.DISABLE)) {
232 TEST_UTIL.getAdmin().disableTable(tn);
233 TEST_UTIL.waitTableDisabled(tn, 10000);
236 // Now, increase limit and perform put
237 helper.setQuotaLimit(tn, policy, 4L);
239 // in case of disable policy quota manager will enable it
240 if (!policy.equals(SpaceViolationPolicy.DISABLE)) {
241 TEST_UTIL.getAdmin().enableTable(tn);
243 TEST_UTIL.waitTableEnabled(tn, 10000);
245 // Put some row now: should not violate as quota limit increased
246 helper.verifyNoViolation(tn, put);