HBASE-23741 Data loss when WAL split to HFile enabled (#1254)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / quotas / TestSpaceQuotaDropTable.java
blob11b9ec67699bf521a13c62c12d996fbc8480c6bb
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.List;
19 import java.util.Map;
20 import java.util.concurrent.atomic.AtomicLong;
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.HBaseClassTestRule;
24 import org.apache.hadoop.hbase.HBaseTestingUtility;
25 import org.apache.hadoop.hbase.MetaTableAccessor;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.Waiter;
28 import org.apache.hadoop.hbase.client.Put;
29 import org.apache.hadoop.hbase.client.RegionInfo;
30 import org.apache.hadoop.hbase.master.HMaster;
31 import org.apache.hadoop.hbase.testclassification.LargeTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.AfterClass;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41 import org.junit.rules.TestName;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @Category(LargeTests.class)
46 public class TestSpaceQuotaDropTable {
48 @ClassRule
49 public static final HBaseClassTestRule CLASS_RULE =
50 HBaseClassTestRule.forClass(TestSpaceQuotaDropTable.class);
52 private static final Logger LOG = LoggerFactory.getLogger(TestSpaceQuotaDropTable.class);
53 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55 @Rule
56 public TestName testName = new TestName();
57 private SpaceQuotaHelperForTests helper;
59 @BeforeClass
60 public static void setUp() throws Exception {
61 Configuration conf = TEST_UTIL.getConfiguration();
62 SpaceQuotaHelperForTests.updateConfigForQuotas(conf);
63 TEST_UTIL.startMiniCluster(1);
66 @AfterClass
67 public static void tearDown() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
71 @Before
72 public void removeAllQuotas() throws Exception {
73 helper = new SpaceQuotaHelperForTests(TEST_UTIL, testName, new AtomicLong(0));
74 helper.removeAllQuotas();
77 @Test
78 public void testSetQuotaAndThenDropTableWithNoInserts() throws Exception {
79 setQuotaAndThenDropTable(SpaceViolationPolicy.NO_INSERTS);
82 @Test
83 public void testSetQuotaAndThenDropTableWithNoWrite() throws Exception {
84 setQuotaAndThenDropTable(SpaceViolationPolicy.NO_WRITES);
87 @Test
88 public void testSetQuotaAndThenDropTableWithNoWritesCompactions() throws Exception {
89 setQuotaAndThenDropTable(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
92 @Test
93 public void testSetQuotaAndThenDropTableWithDisable() throws Exception {
94 setQuotaAndThenDropTable(SpaceViolationPolicy.DISABLE);
97 @Test
98 public void testSetQuotaAndThenDropTableWithRegionReport() throws Exception {
99 final TableName tn = helper.createTable();
100 helper.setQuotaLimit(tn, SpaceViolationPolicy.NO_INSERTS, 1L);
101 helper.writeData(tn, 2L);
103 final HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
104 final MasterQuotaManager quotaManager = master.getMasterQuotaManager();
106 // Make sure the master has report for the table.
107 Waiter.waitFor(TEST_UTIL.getConfiguration(), 30 * 1000, new Waiter.Predicate<Exception>() {
108 @Override
109 public boolean evaluate() throws Exception {
110 Map<RegionInfo, Long> regionSizes = quotaManager.snapshotRegionSizes();
111 List<RegionInfo> tableRegions =
112 MetaTableAccessor.getTableRegions(TEST_UTIL.getConnection(), tn);
113 return regionSizes.containsKey(tableRegions.get(0));
117 boolean hasRegionSize = false;
119 // region report should be present before dropping the table.
120 for (Map.Entry<RegionInfo, Long> entry : quotaManager.snapshotRegionSizes().entrySet()) {
121 if (entry.getKey().getTable().equals(tn)) {
122 hasRegionSize = true;
123 break;
127 // regionSize report for the given table should be present before dropping the table.
128 Assert.assertTrue(hasRegionSize);
130 // drop the table
131 TEST_UTIL.getAdmin().disableTable(tn);
132 TEST_UTIL.getAdmin().deleteTable(tn);
134 // check if deleted table region report still present in the map.
135 for (Map.Entry<RegionInfo, Long> entry : quotaManager.snapshotRegionSizes().entrySet()) {
136 if (entry.getKey().getTable().equals(tn)) {
137 Assert.fail("Dropped table regionSizes were not deleted during the drop command");
142 private void setQuotaAndThenDropTable(SpaceViolationPolicy policy) throws Exception {
143 Put put = new Put(Bytes.toBytes("to_reject"));
144 put.addColumn(Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("to"),
145 Bytes.toBytes("reject"));
147 // Do puts until we violate space policy
148 final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
150 // Now, drop the table
151 TEST_UTIL.deleteTable(tn);
152 LOG.debug("Successfully deleted table ", tn);
154 // Now re-create the table
155 TEST_UTIL.createTable(tn, Bytes.toBytes(SpaceQuotaHelperForTests.F1));
156 LOG.debug("Successfully re-created table ", tn);
158 // Put some rows now: should not violate as table/quota was dropped
159 helper.verifyNoViolation(tn, put);