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.NamespaceDescriptor;
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 TestSpaceQuotaRemoval {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestSpaceQuotaRemoval.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 testSetQuotaAndThenRemoveInOneWithNoInserts() throws Exception {
070    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_INSERTS);
071  }
072
073  @Test
074  public void testSetQuotaAndThenRemoveInOneWithNoWrite() throws Exception {
075    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES);
076  }
077
078  @Test
079  public void testSetQuotaAndThenRemoveInOneWithNoWritesCompaction() throws Exception {
080    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
081  }
082
083  @Test
084  public void testSetQuotaAndThenRemoveInOneWithDisable() throws Exception {
085    setQuotaAndThenRemoveInOneAmongTwoTables(SpaceViolationPolicy.DISABLE);
086  }
087
088  @Test
089  public void testSetQuotaAndThenRemoveWithNoInserts() throws Exception {
090    setQuotaAndThenRemove(SpaceViolationPolicy.NO_INSERTS);
091  }
092
093  @Test
094  public void testSetQuotaAndThenRemoveWithNoWrite() throws Exception {
095    setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES);
096  }
097
098  @Test
099  public void testSetQuotaAndThenRemoveWithNoWritesCompactions() throws Exception {
100    setQuotaAndThenRemove(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
101  }
102
103  @Test
104  public void testSetQuotaAndThenRemoveWithDisable() throws Exception {
105    setQuotaAndThenRemove(SpaceViolationPolicy.DISABLE);
106  }
107
108  @Test
109  public void testSetQuotaAndThenDisableIncrEnableWithNoInserts() throws Exception {
110    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_INSERTS);
111  }
112
113  @Test
114  public void testSetQuotaAndThenDisableIncrEnableWithNoWrite() throws Exception {
115    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES);
116  }
117
118  @Test
119  public void testSetQuotaAndThenDisableIncrEnableWithNoWritesCompaction() throws Exception {
120    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.NO_WRITES_COMPACTIONS);
121  }
122
123  @Test
124  public void testSetQuotaAndThenDisableIncrEnableWithDisable() throws Exception {
125    setQuotaNextDisableThenIncreaseFinallyEnable(SpaceViolationPolicy.DISABLE);
126  }
127
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"));
132
133    // Do puts until we violate space policy
134    final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
135
136    // Now, remove the quota
137    helper.removeQuotaFromtable(tn);
138
139    // Put some rows now: should not violate as quota settings removed
140    helper.verifyNoViolation(tn, put);
141  }
142
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"));
148
149    SpaceViolationPolicy policy = SpaceViolationPolicy.NO_INSERTS;
150
151    //Create a namespace
152    String ns1 = "nsnew";
153    NamespaceDescriptor nsd = helper.createNamespace(ns1);
154
155    //Create 2nd namespace with name similar to ns1
156    String ns2 = ns1 + "test";
157    NamespaceDescriptor nsd2 = helper.createNamespace(ns2);
158
159    // Do puts until we violate space policy on table tn1 in namesapce ns1
160    final TableName tn1 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns1, policy, put);
161
162    // Do puts until we violate space policy on table tn2 in namespace ns2
163    final TableName tn2 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns2, policy, put);
164
165    // Now, remove the quota from namespace ns1 which will remove table usage snapshots for ns1
166    helper.removeQuotaFromNamespace(ns1);
167
168    // Verify that table usage snapshot for table tn2 in namespace ns2 exist
169    helper.verifyTableUsageSnapshotForSpaceQuotaExist(tn2);
170
171    // Put a new row on tn2: should violate as space quota exists on namespace ns2
172    helper.verifyViolation(policy, tn2, put);
173
174    // Put a new row on tn1: should not violate as quota settings removed from namespace ns1
175    helper.verifyNoViolation(tn1, put);
176  }
177
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"));
183
184    SpaceViolationPolicy policy = SpaceViolationPolicy.NO_INSERTS;
185
186    //Create namespace
187    NamespaceDescriptor nsd = helper.createNamespace();
188    String ns = nsd.getName();
189
190    // Do puts until we violate space policy on table tn1
191    final TableName tn1 = helper.writeUntilViolationAndVerifyViolationInNamespace(ns, policy, put);
192
193    // Now, remove the quota from namespace
194    helper.removeQuotaFromNamespace(ns);
195
196    // Put a new row now on tn1: should not violate as quota settings removed from namespace
197    helper.verifyNoViolation(tn1, put);
198  }
199
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"));
205
206    // Do puts until we violate space policy on table tn1
207    final TableName tn1 = helper.writeUntilViolationAndVerifyViolation(policy, put);
208
209    // Do puts until we violate space policy on table tn2
210    final TableName tn2 = helper.writeUntilViolationAndVerifyViolation(policy, put);
211
212    // Now, remove the quota from table tn1
213    helper.removeQuotaFromtable(tn1);
214
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);
219  }
220
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"));
226
227    // Do puts until we violate space policy
228    final TableName tn = helper.writeUntilViolationAndVerifyViolation(policy, put);
229
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);
234    }
235
236    // Now, increase limit and perform put
237    helper.setQuotaLimit(tn, policy, 4L);
238
239    // in case of disable policy quota manager will enable it
240    if (!policy.equals(SpaceViolationPolicy.DISABLE)) {
241      TEST_UTIL.getAdmin().enableTable(tn);
242    }
243    TEST_UTIL.waitTableEnabled(tn, 10000);
244
245    // Put some row now: should not violate as quota limit increased
246    helper.verifyNoViolation(tn, put);
247  }
248}