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 static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Set;
025import java.util.concurrent.atomic.AtomicLong;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HColumnDescriptor;
030import org.apache.hadoop.hbase.HTableDescriptor;
031import org.apache.hadoop.hbase.NamespaceDescriptor;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
036import org.apache.hadoop.hbase.master.HMaster;
037import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.junit.AfterClass;
040import org.junit.Before;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047
048/**
049 * Test class for {@link MasterSpaceQuotaObserver}.
050 */
051@Category(MediumTests.class)
052public class TestMasterSpaceQuotaObserver {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestMasterSpaceQuotaObserver.class);
057
058  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059  private static SpaceQuotaHelperForTests helper;
060
061  @Rule
062  public TestName testName = new TestName();
063
064  @BeforeClass
065  public static void setUp() throws Exception {
066    Configuration conf = TEST_UTIL.getConfiguration();
067    conf.setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
068    TEST_UTIL.startMiniCluster(1);
069  }
070
071  @AfterClass
072  public static void tearDown() throws Exception {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  @Before
077  public void removeAllQuotas() throws Exception {
078    if (helper == null) {
079      helper = new SpaceQuotaHelperForTests(TEST_UTIL, testName, new AtomicLong());
080    }
081    final Connection conn = TEST_UTIL.getConnection();
082    // Wait for the quota table to be created
083    if (!conn.getAdmin().tableExists(QuotaUtil.QUOTA_TABLE_NAME)) {
084      helper.waitForQuotaTable(conn);
085    } else {
086      // Or, clean up any quotas from previous test runs.
087      helper.removeAllQuotas(conn);
088      assertEquals(0, helper.listNumDefinedQuotas(conn));
089    }
090  }
091
092  @Test
093  public void testTableQuotaRemoved() throws Exception {
094    final Connection conn = TEST_UTIL.getConnection();
095    final Admin admin = conn.getAdmin();
096    final TableName tn = TableName.valueOf(testName.getMethodName());
097    // Drop the table if it somehow exists
098    if (admin.tableExists(tn)) {
099      admin.disableTable(tn);
100      admin.deleteTable(tn);
101    }
102
103    // Create a table
104    HTableDescriptor tableDesc = new HTableDescriptor(tn);
105    tableDesc.addFamily(new HColumnDescriptor("F1"));
106    admin.createTable(tableDesc);
107    assertEquals(0, getNumSpaceQuotas());
108
109    // Set a quota
110    QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(
111        tn, 1024L, SpaceViolationPolicy.NO_INSERTS);
112    admin.setQuota(settings);
113    assertEquals(1, getNumSpaceQuotas());
114
115    // Delete the table and observe the quota being automatically deleted as well
116    admin.disableTable(tn);
117    admin.deleteTable(tn);
118    assertEquals(0, getNumSpaceQuotas());
119  }
120
121  @Test
122  public void testNamespaceQuotaRemoved() throws Exception {
123    final Connection conn = TEST_UTIL.getConnection();
124    final Admin admin = conn.getAdmin();
125    final String ns = testName.getMethodName();
126    // Drop the ns if it somehow exists
127    if (namespaceExists(ns)) {
128      admin.deleteNamespace(ns);
129    }
130
131    // Create the ns
132    NamespaceDescriptor desc = NamespaceDescriptor.create(ns).build();
133    admin.createNamespace(desc);
134    assertEquals(0, getNumSpaceQuotas());
135
136    // Set a quota
137    QuotaSettings settings = QuotaSettingsFactory.limitNamespaceSpace(
138        ns, 1024L, SpaceViolationPolicy.NO_INSERTS);
139    admin.setQuota(settings);
140    assertEquals(1, getNumSpaceQuotas());
141
142    // Delete the table and observe the quota being automatically deleted as well
143    admin.deleteNamespace(ns);
144    assertEquals(0, getNumSpaceQuotas());
145  }
146
147  @Test
148  public void testObserverAddedByDefault() throws Exception {
149    final HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
150    final MasterCoprocessorHost cpHost = master.getMasterCoprocessorHost();
151    Set<String> coprocessorNames = cpHost.getCoprocessors();
152    assertTrue(
153        "Did not find MasterSpaceQuotaObserver in list of CPs: " + coprocessorNames,
154        coprocessorNames.contains(MasterSpaceQuotaObserver.class.getSimpleName()));
155  }
156
157  public boolean namespaceExists(String ns) throws IOException {
158    NamespaceDescriptor[] descs = TEST_UTIL.getAdmin().listNamespaceDescriptors();
159    for (NamespaceDescriptor desc : descs) {
160      if (ns.equals(desc.getName())) {
161        return true;
162      }
163    }
164    return false;
165  }
166
167  public int getNumSpaceQuotas() throws Exception {
168    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration());
169    int numSpaceQuotas = 0;
170    for (QuotaSettings quotaSettings : scanner) {
171      if (quotaSettings.getQuotaType() == QuotaType.SPACE) {
172        numSpaceQuotas++;
173      }
174    }
175    return numSpaceQuotas;
176  }
177}