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