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.assertFalse;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024import static org.junit.Assert.fail;
025
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.List;
029import java.util.Objects;
030import java.util.concurrent.TimeUnit;
031
032import org.apache.hadoop.hbase.Cell;
033import org.apache.hadoop.hbase.CellScanner;
034import org.apache.hadoop.hbase.HBaseClassTestRule;
035import org.apache.hadoop.hbase.HBaseTestingUtility;
036import org.apache.hadoop.hbase.HConstants;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.client.Admin;
039import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
040import org.apache.hadoop.hbase.client.Put;
041import org.apache.hadoop.hbase.client.Result;
042import org.apache.hadoop.hbase.client.ResultScanner;
043import org.apache.hadoop.hbase.client.Scan;
044import org.apache.hadoop.hbase.client.Table;
045import org.apache.hadoop.hbase.client.TableDescriptor;
046import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
047import org.apache.hadoop.hbase.security.User;
048import org.apache.hadoop.hbase.testclassification.ClientTests;
049import org.apache.hadoop.hbase.testclassification.LargeTests;
050import org.apache.hadoop.hbase.util.Bytes;
051import org.apache.hadoop.hbase.util.JVMClusterUtil;
052import org.junit.After;
053import org.junit.AfterClass;
054import org.junit.Assert;
055import org.junit.BeforeClass;
056import org.junit.ClassRule;
057import org.junit.Test;
058import org.junit.experimental.categories.Category;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
063
064import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
065import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
066import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Quotas;
067import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceLimitRequest;
068
069/**
070 * minicluster tests that validate that quota  entries are properly set in the quota table
071 */
072@Category({ClientTests.class, LargeTests.class})
073public class TestQuotaAdmin {
074
075  @ClassRule
076  public static final HBaseClassTestRule CLASS_RULE =
077      HBaseClassTestRule.forClass(TestQuotaAdmin.class);
078
079  private static final Logger LOG = LoggerFactory.getLogger(TestQuotaAdmin.class);
080
081  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
082
083  private final static TableName[] TABLE_NAMES =
084      new TableName[] { TableName.valueOf("TestQuotaAdmin0"), TableName.valueOf("TestQuotaAdmin1"),
085          TableName.valueOf("TestQuotaAdmin2") };
086
087  private final static String[] NAMESPACES =
088      new String[] { "NAMESPACE01", "NAMESPACE02", "NAMESPACE03" };
089
090  @BeforeClass
091  public static void setUpBeforeClass() throws Exception {
092    TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
093    TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 2000);
094    TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
095    TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
096    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
097    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
098    TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
099    TEST_UTIL.startMiniCluster(1);
100    TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
101  }
102
103  @After
104  public void clearQuotaTable() throws Exception {
105    if (TEST_UTIL.getAdmin().tableExists(QuotaUtil.QUOTA_TABLE_NAME)) {
106      TEST_UTIL.getAdmin().disableTable(QuotaUtil.QUOTA_TABLE_NAME);
107      TEST_UTIL.getAdmin().truncateTable(QuotaUtil.QUOTA_TABLE_NAME, false);
108    }
109  }
110
111  @AfterClass
112  public static void tearDownAfterClass() throws Exception {
113    TEST_UTIL.shutdownMiniCluster();
114  }
115
116  @Test
117  public void testThrottleType() throws Exception {
118    Admin admin = TEST_UTIL.getAdmin();
119    String userName = User.getCurrent().getShortName();
120
121    admin.setQuota(
122        QuotaSettingsFactory.throttleUser(userName, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
123    admin.setQuota(QuotaSettingsFactory
124        .throttleUser(userName, ThrottleType.WRITE_NUMBER, 12, TimeUnit.MINUTES));
125    admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
126
127    try (QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
128      int countThrottle = 0;
129      int countGlobalBypass = 0;
130      for (QuotaSettings settings: scanner) {
131        switch (settings.getQuotaType()) {
132          case THROTTLE:
133            ThrottleSettings throttle = (ThrottleSettings)settings;
134            if (throttle.getSoftLimit() == 6) {
135              assertEquals(ThrottleType.READ_NUMBER, throttle.getThrottleType());
136            } else if (throttle.getSoftLimit() == 12) {
137              assertEquals(ThrottleType.WRITE_NUMBER, throttle.getThrottleType());
138            } else {
139              fail("should not come here, because don't set quota with this limit");
140            }
141            assertEquals(userName, throttle.getUserName());
142            assertEquals(null, throttle.getTableName());
143            assertEquals(null, throttle.getNamespace());
144            assertEquals(TimeUnit.MINUTES, throttle.getTimeUnit());
145            countThrottle++;
146            break;
147          case GLOBAL_BYPASS:
148            countGlobalBypass++;
149            break;
150          default:
151            fail("unexpected settings type: " + settings.getQuotaType());
152        }
153      }
154      assertEquals(2, countThrottle);
155      assertEquals(1, countGlobalBypass);
156    }
157
158    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
159    assertNumResults(1, null);
160    admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false));
161    assertNumResults(0, null);
162  }
163
164  @Test
165  public void testSimpleScan() throws Exception {
166    Admin admin = TEST_UTIL.getAdmin();
167    String userName = User.getCurrent().getShortName();
168
169    admin.setQuota(QuotaSettingsFactory
170      .throttleUser(userName, ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
171    admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
172
173    try (QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
174      int countThrottle = 0;
175      int countGlobalBypass = 0;
176      for (QuotaSettings settings: scanner) {
177        LOG.debug(Objects.toString(settings));
178        switch (settings.getQuotaType()) {
179          case THROTTLE:
180            ThrottleSettings throttle = (ThrottleSettings)settings;
181            assertEquals(userName, throttle.getUserName());
182            assertEquals(null, throttle.getTableName());
183            assertEquals(null, throttle.getNamespace());
184            assertEquals(null, throttle.getRegionServer());
185            assertEquals(6, throttle.getSoftLimit());
186            assertEquals(TimeUnit.MINUTES, throttle.getTimeUnit());
187            countThrottle++;
188            break;
189          case GLOBAL_BYPASS:
190            countGlobalBypass++;
191            break;
192          default:
193            fail("unexpected settings type: " + settings.getQuotaType());
194        }
195      }
196      assertEquals(1, countThrottle);
197      assertEquals(1, countGlobalBypass);
198    }
199
200    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
201    assertNumResults(1, null);
202    admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false));
203    assertNumResults(0, null);
204  }
205
206  @Test
207  public void testMultiQuotaThrottling() throws Exception {
208    byte[] FAMILY = Bytes.toBytes("testFamily");
209    byte[] ROW = Bytes.toBytes("testRow");
210    byte[] QUALIFIER = Bytes.toBytes("testQualifier");
211    byte[] VALUE = Bytes.toBytes("testValue");
212
213    Admin admin = TEST_UTIL.getAdmin();
214    TableName tableName = TableName.valueOf("testMultiQuotaThrottling");
215    TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
216        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
217    admin.createTable(desc);
218
219    // Set up the quota.
220    admin.setQuota(QuotaSettingsFactory.throttleTable(tableName, ThrottleType.WRITE_NUMBER, 6,
221        TimeUnit.SECONDS));
222
223    Thread.sleep(1000);
224    TEST_UTIL.getRSForFirstRegionInTable(tableName).getRegionServerRpcQuotaManager().
225        getQuotaCache().triggerCacheRefresh();
226    Thread.sleep(1000);
227
228    Table t =  TEST_UTIL.getConnection().getTable(tableName);
229    try {
230      int size = 5;
231      List actions = new ArrayList();
232      Object[] results = new Object[size];
233
234      for (int i = 0; i < size; i++) {
235        Put put1 = new Put(ROW);
236        put1.addColumn(FAMILY, QUALIFIER, VALUE);
237        actions.add(put1);
238      }
239      t.batch(actions, results);
240      t.batch(actions, results);
241    } catch (IOException e) {
242      fail("Not supposed to get ThrottlingExcepiton " + e);
243    } finally {
244      t.close();
245    }
246  }
247
248
249  @Test
250  public void testQuotaRetrieverFilter() throws Exception {
251    Admin admin = TEST_UTIL.getAdmin();
252    TableName[] tables = new TableName[] {
253      TableName.valueOf("T0"), TableName.valueOf("T01"), TableName.valueOf("NS0:T2"),
254    };
255    String[] namespaces = new String[] { "NS0", "NS01", "NS2" };
256    String[] users = new String[] { "User0", "User01", "User2" };
257
258    for (String user: users) {
259      admin.setQuota(QuotaSettingsFactory
260        .throttleUser(user, ThrottleType.REQUEST_NUMBER, 1, TimeUnit.MINUTES));
261
262      for (TableName table: tables) {
263        admin.setQuota(QuotaSettingsFactory
264          .throttleUser(user, table, ThrottleType.REQUEST_NUMBER, 2, TimeUnit.MINUTES));
265      }
266
267      for (String ns: namespaces) {
268        admin.setQuota(QuotaSettingsFactory
269          .throttleUser(user, ns, ThrottleType.REQUEST_NUMBER, 3, TimeUnit.MINUTES));
270      }
271    }
272    assertNumResults(21, null);
273
274    for (TableName table: tables) {
275      admin.setQuota(QuotaSettingsFactory
276        .throttleTable(table, ThrottleType.REQUEST_NUMBER, 4, TimeUnit.MINUTES));
277    }
278    assertNumResults(24, null);
279
280    for (String ns: namespaces) {
281      admin.setQuota(QuotaSettingsFactory
282        .throttleNamespace(ns, ThrottleType.REQUEST_NUMBER, 5, TimeUnit.MINUTES));
283    }
284    assertNumResults(27, null);
285
286    assertNumResults(7, new QuotaFilter().setUserFilter("User0"));
287    assertNumResults(0, new QuotaFilter().setUserFilter("User"));
288    assertNumResults(21, new QuotaFilter().setUserFilter("User.*"));
289    assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("T0"));
290    assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("NS.*"));
291    assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setTableFilter("T"));
292    assertNumResults(6, new QuotaFilter().setUserFilter("User.*").setTableFilter("T.*"));
293    assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS0"));
294    assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS"));
295    assertNumResults(9, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS.*"));
296    assertNumResults(6, new QuotaFilter().setUserFilter("User.*")
297                                            .setTableFilter("T0").setNamespaceFilter("NS0"));
298    assertNumResults(1, new QuotaFilter().setTableFilter("T0"));
299    assertNumResults(0, new QuotaFilter().setTableFilter("T"));
300    assertNumResults(2, new QuotaFilter().setTableFilter("T.*"));
301    assertNumResults(3, new QuotaFilter().setTableFilter(".*T.*"));
302    assertNumResults(1, new QuotaFilter().setNamespaceFilter("NS0"));
303    assertNumResults(0, new QuotaFilter().setNamespaceFilter("NS"));
304    assertNumResults(3, new QuotaFilter().setNamespaceFilter("NS.*"));
305
306    for (String user: users) {
307      admin.setQuota(QuotaSettingsFactory.unthrottleUser(user));
308      for (TableName table: tables) {
309        admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, table));
310      }
311      for (String ns: namespaces) {
312        admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, ns));
313      }
314    }
315    assertNumResults(6, null);
316
317    for (TableName table: tables) {
318      admin.setQuota(QuotaSettingsFactory.unthrottleTable(table));
319    }
320    assertNumResults(3, null);
321
322    for (String ns: namespaces) {
323      admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(ns));
324    }
325    assertNumResults(0, null);
326  }
327
328  @Test
329  public void testSetGetRemoveSpaceQuota() throws Exception {
330    Admin admin = TEST_UTIL.getAdmin();
331    final TableName tn = TableName.valueOf("sq_table1");
332    final long sizeLimit = 1024L * 1024L * 1024L * 1024L * 5L; // 5TB
333    final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_WRITES;
334    QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, sizeLimit, violationPolicy);
335    admin.setQuota(settings);
336
337    // Verify the Quotas in the table
338    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
339      ResultScanner scanner = quotaTable.getScanner(new Scan());
340      try {
341        Result r = Iterables.getOnlyElement(scanner);
342        CellScanner cells = r.cellScanner();
343        assertTrue("Expected to find a cell", cells.advance());
344        assertSpaceQuota(sizeLimit, violationPolicy, cells.current());
345      } finally {
346        scanner.close();
347      }
348    }
349
350    // Verify we can retrieve it via the QuotaRetriever API
351    QuotaRetriever scanner = QuotaRetriever.open(admin.getConfiguration());
352    try {
353      assertSpaceQuota(sizeLimit, violationPolicy, Iterables.getOnlyElement(scanner));
354    } finally {
355      scanner.close();
356    }
357
358    // Now, remove the quota
359    QuotaSettings removeQuota = QuotaSettingsFactory.removeTableSpaceLimit(tn);
360    admin.setQuota(removeQuota);
361
362    // Verify that the record doesn't exist in the table
363    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
364      ResultScanner rs = quotaTable.getScanner(new Scan());
365      try {
366        assertNull("Did not expect to find a quota entry", rs.next());
367      } finally {
368        rs.close();
369      }
370    }
371
372    // Verify that we can also not fetch it via the API
373    scanner = QuotaRetriever.open(admin.getConfiguration());
374    try {
375      assertNull("Did not expect to find a quota entry", scanner.next());
376    } finally {
377      scanner.close();
378    }
379  }
380
381  @Test
382  public void testSetModifyRemoveSpaceQuota() throws Exception {
383    Admin admin = TEST_UTIL.getAdmin();
384    final TableName tn = TableName.valueOf("sq_table2");
385    final long originalSizeLimit = 1024L * 1024L * 1024L * 1024L * 5L; // 5TB
386    final SpaceViolationPolicy violationPolicy = SpaceViolationPolicy.NO_WRITES;
387    QuotaSettings settings = QuotaSettingsFactory.limitTableSpace(tn, originalSizeLimit,
388        violationPolicy);
389    admin.setQuota(settings);
390
391    // Verify the Quotas in the table
392    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
393      ResultScanner scanner = quotaTable.getScanner(new Scan());
394      try {
395        Result r = Iterables.getOnlyElement(scanner);
396        CellScanner cells = r.cellScanner();
397        assertTrue("Expected to find a cell", cells.advance());
398        assertSpaceQuota(originalSizeLimit, violationPolicy, cells.current());
399      } finally {
400        scanner.close();
401      }
402    }
403
404    // Verify we can retrieve it via the QuotaRetriever API
405    QuotaRetriever quotaScanner = QuotaRetriever.open(admin.getConfiguration());
406    try {
407      assertSpaceQuota(originalSizeLimit, violationPolicy, Iterables.getOnlyElement(quotaScanner));
408    } finally {
409      quotaScanner.close();
410    }
411
412    // Setting a new size and policy should be reflected
413    final long newSizeLimit = 1024L * 1024L * 1024L * 1024L; // 1TB
414    final SpaceViolationPolicy newViolationPolicy = SpaceViolationPolicy.NO_WRITES_COMPACTIONS;
415    QuotaSettings newSettings = QuotaSettingsFactory.limitTableSpace(tn, newSizeLimit,
416        newViolationPolicy);
417    admin.setQuota(newSettings);
418
419    // Verify the new Quotas in the table
420    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
421      ResultScanner scanner = quotaTable.getScanner(new Scan());
422      try {
423        Result r = Iterables.getOnlyElement(scanner);
424        CellScanner cells = r.cellScanner();
425        assertTrue("Expected to find a cell", cells.advance());
426        assertSpaceQuota(newSizeLimit, newViolationPolicy, cells.current());
427      } finally {
428        scanner.close();
429      }
430    }
431
432    // Verify we can retrieve the new quota via the QuotaRetriever API
433    quotaScanner = QuotaRetriever.open(admin.getConfiguration());
434    try {
435      assertSpaceQuota(newSizeLimit, newViolationPolicy, Iterables.getOnlyElement(quotaScanner));
436    } finally {
437      quotaScanner.close();
438    }
439
440    // Now, remove the quota
441    QuotaSettings removeQuota = QuotaSettingsFactory.removeTableSpaceLimit(tn);
442    admin.setQuota(removeQuota);
443
444    // Verify that the record doesn't exist in the table
445    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
446      ResultScanner scanner = quotaTable.getScanner(new Scan());
447      try {
448        assertNull("Did not expect to find a quota entry", scanner.next());
449      } finally {
450        scanner.close();
451      }
452    }
453
454    // Verify that we can also not fetch it via the API
455    quotaScanner = QuotaRetriever.open(admin.getConfiguration());
456    try {
457      assertNull("Did not expect to find a quota entry", quotaScanner.next());
458    } finally {
459      quotaScanner.close();
460    }
461  }
462
463  private void assertNumResults(int expected, final QuotaFilter filter) throws Exception {
464    assertEquals(expected, countResults(filter));
465  }
466
467  @Test
468  public void testSetGetRemoveRPCQuota() throws Exception {
469    testSetGetRemoveRPCQuota(ThrottleType.REQUEST_SIZE);
470    testSetGetRemoveRPCQuota(ThrottleType.REQUEST_CAPACITY_UNIT);
471  }
472
473  private void testSetGetRemoveRPCQuota(ThrottleType throttleType) throws Exception {
474    Admin admin = TEST_UTIL.getAdmin();
475    final TableName tn = TableName.valueOf("sq_table1");
476    QuotaSettings settings =
477        QuotaSettingsFactory.throttleTable(tn, throttleType, 2L, TimeUnit.HOURS);
478    admin.setQuota(settings);
479
480    // Verify the Quota in the table
481    verifyRecordPresentInQuotaTable(throttleType, 2L, TimeUnit.HOURS);
482
483    // Verify we can retrieve it via the QuotaRetriever API
484    verifyFetchableViaAPI(admin, throttleType, 2L, TimeUnit.HOURS);
485
486    // Now, remove the quota
487    QuotaSettings removeQuota = QuotaSettingsFactory.unthrottleTable(tn);
488    admin.setQuota(removeQuota);
489
490    // Verify that the record doesn't exist in the table
491    verifyRecordNotPresentInQuotaTable();
492
493    // Verify that we can also not fetch it via the API
494    verifyNotFetchableViaAPI(admin);
495  }
496
497  @Test
498  public void testSetModifyRemoveRPCQuota() throws Exception {
499    Admin admin = TEST_UTIL.getAdmin();
500    final TableName tn = TableName.valueOf("sq_table1");
501    QuotaSettings settings =
502        QuotaSettingsFactory.throttleTable(tn, ThrottleType.REQUEST_SIZE, 2L, TimeUnit.HOURS);
503    admin.setQuota(settings);
504
505    // Verify the Quota in the table
506    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_SIZE, 2L, TimeUnit.HOURS);
507
508    // Verify we can retrieve it via the QuotaRetriever API
509    verifyFetchableViaAPI(admin, ThrottleType.REQUEST_SIZE, 2L, TimeUnit.HOURS);
510
511    // Setting a limit and time unit should be reflected
512    QuotaSettings newSettings =
513        QuotaSettingsFactory.throttleTable(tn, ThrottleType.REQUEST_SIZE, 3L, TimeUnit.DAYS);
514    admin.setQuota(newSettings);
515
516    // Verify the new Quota in the table
517    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_SIZE, 3L, TimeUnit.DAYS);
518
519    // Verify we can retrieve the new quota via the QuotaRetriever API
520    verifyFetchableViaAPI(admin, ThrottleType.REQUEST_SIZE, 3L, TimeUnit.DAYS);
521
522    // Now, remove the quota
523    QuotaSettings removeQuota = QuotaSettingsFactory.unthrottleTable(tn);
524    admin.setQuota(removeQuota);
525
526    // Verify that the record doesn't exist in the table
527    verifyRecordNotPresentInQuotaTable();
528
529    // Verify that we can also not fetch it via the API
530    verifyNotFetchableViaAPI(admin);
531
532  }
533
534  @Test
535  public void testSetAndRemoveRegionServerQuota() throws Exception {
536    Admin admin = TEST_UTIL.getAdmin();
537    String regionServer = QuotaTableUtil.QUOTA_REGION_SERVER_ROW_KEY;
538    QuotaFilter rsFilter = new QuotaFilter().setRegionServerFilter(regionServer);
539
540    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer,
541      ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES));
542    assertNumResults(1, rsFilter);
543    // Verify the Quota in the table
544    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES);
545
546    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer,
547      ThrottleType.REQUEST_NUMBER, 20, TimeUnit.MINUTES));
548    assertNumResults(1, rsFilter);
549    // Verify the Quota in the table
550    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 20, TimeUnit.MINUTES);
551
552    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer, ThrottleType.READ_NUMBER,
553      30, TimeUnit.SECONDS));
554    int count = 0;
555    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration(), rsFilter);
556    try {
557      for (QuotaSettings settings : scanner) {
558        assertTrue(settings.getQuotaType() == QuotaType.THROTTLE);
559        ThrottleSettings throttleSettings = (ThrottleSettings) settings;
560        assertEquals(regionServer, throttleSettings.getRegionServer());
561        count++;
562        if (throttleSettings.getThrottleType() == ThrottleType.REQUEST_NUMBER) {
563          assertEquals(20, throttleSettings.getSoftLimit());
564          assertEquals(TimeUnit.MINUTES, throttleSettings.getTimeUnit());
565        } else if (throttleSettings.getThrottleType() == ThrottleType.READ_NUMBER) {
566          assertEquals(30, throttleSettings.getSoftLimit());
567          assertEquals(TimeUnit.SECONDS, throttleSettings.getTimeUnit());
568        }
569      }
570    } finally {
571      scanner.close();
572    }
573    assertEquals(2, count);
574
575    admin.setQuota(QuotaSettingsFactory.unthrottleRegionServer(regionServer));
576    assertNumResults(0, new QuotaFilter().setRegionServerFilter(regionServer));
577  }
578
579  @Test
580  public void testRpcThrottleWhenStartup() throws IOException, InterruptedException {
581    TEST_UTIL.getAdmin().switchRpcThrottle(false);
582    assertFalse(TEST_UTIL.getAdmin().isRpcThrottleEnabled());
583    TEST_UTIL.killMiniHBaseCluster();
584
585    TEST_UTIL.startMiniHBaseCluster();
586    assertFalse(TEST_UTIL.getAdmin().isRpcThrottleEnabled());
587    for (JVMClusterUtil.RegionServerThread rs : TEST_UTIL.getHBaseCluster()
588        .getRegionServerThreads()) {
589      RegionServerRpcQuotaManager quotaManager =
590          rs.getRegionServer().getRegionServerRpcQuotaManager();
591      assertFalse(quotaManager.isRpcThrottleEnabled());
592    }
593    // enable rpc throttle
594    TEST_UTIL.getAdmin().switchRpcThrottle(true);
595    assertTrue(TEST_UTIL.getAdmin().isRpcThrottleEnabled());
596  }
597
598  @Test
599  public void testSwitchRpcThrottle() throws IOException {
600    Admin admin = TEST_UTIL.getAdmin();
601    testSwitchRpcThrottle(admin, true, true);
602    testSwitchRpcThrottle(admin, true, false);
603    testSwitchRpcThrottle(admin, false, false);
604    testSwitchRpcThrottle(admin, false, true);
605  }
606
607  @Test
608  public void testSwitchExceedThrottleQuota() throws IOException {
609    String regionServer = QuotaTableUtil.QUOTA_REGION_SERVER_ROW_KEY;
610    Admin admin = TEST_UTIL.getAdmin();
611
612    try {
613      admin.exceedThrottleQuotaSwitch(true);
614      fail("should not come here, because can't enable exceed throttle quota "
615          + "if there is no region server quota");
616    } catch (IOException e) {
617      LOG.warn("Expected exception", e);
618    }
619
620    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer,
621      ThrottleType.WRITE_NUMBER, 100, TimeUnit.SECONDS));
622    try {
623      admin.exceedThrottleQuotaSwitch(true);
624      fail("should not come here, because can't enable exceed throttle quota "
625          + "if there is no read region server quota");
626    } catch (IOException e) {
627      LOG.warn("Expected exception", e);
628    }
629
630    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer, ThrottleType.READ_NUMBER,
631      20, TimeUnit.MINUTES));
632    try {
633      admin.exceedThrottleQuotaSwitch(true);
634      fail("should not come here, because can't enable exceed throttle quota "
635          + "because not all region server quota are in seconds time unit");
636    } catch (IOException e) {
637      LOG.warn("Expected exception", e);
638    }
639    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(regionServer, ThrottleType.READ_NUMBER,
640      20, TimeUnit.SECONDS));
641
642    assertFalse(admin.exceedThrottleQuotaSwitch(true));
643    assertTrue(admin.exceedThrottleQuotaSwitch(true));
644    assertTrue(admin.exceedThrottleQuotaSwitch(false));
645    assertFalse(admin.exceedThrottleQuotaSwitch(false));
646    assertEquals(2, admin.getQuota(new QuotaFilter()).size());
647    admin.setQuota(QuotaSettingsFactory.unthrottleRegionServer(regionServer));
648  }
649
650  @Test
651  public void testQuotaScope() throws Exception {
652    Admin admin = TEST_UTIL.getAdmin();
653    String user = "user1";
654    String namespace = "testQuotaScope_ns";
655    TableName tableName = TableName.valueOf("testQuotaScope");
656    QuotaFilter filter = new QuotaFilter();
657
658    // set CLUSTER quota scope for namespace
659    admin.setQuota(QuotaSettingsFactory.throttleNamespace(namespace, ThrottleType.REQUEST_NUMBER,
660      10, TimeUnit.MINUTES, QuotaScope.CLUSTER));
661    assertNumResults(1, filter);
662    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
663      QuotaScope.CLUSTER);
664    admin.setQuota(QuotaSettingsFactory.throttleNamespace(namespace, ThrottleType.REQUEST_NUMBER,
665      10, TimeUnit.MINUTES, QuotaScope.MACHINE));
666    assertNumResults(1, filter);
667    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
668      QuotaScope.MACHINE);
669    admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(namespace));
670    assertNumResults(0, filter);
671
672    // set CLUSTER quota scope for table
673    admin.setQuota(QuotaSettingsFactory.throttleTable(tableName, ThrottleType.REQUEST_NUMBER, 10,
674      TimeUnit.MINUTES, QuotaScope.CLUSTER));
675    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
676      QuotaScope.CLUSTER);
677    admin.setQuota(QuotaSettingsFactory.unthrottleTable(tableName));
678
679    // set CLUSTER quota scope for user
680    admin.setQuota(QuotaSettingsFactory.throttleUser(user, ThrottleType.REQUEST_NUMBER, 10,
681      TimeUnit.MINUTES, QuotaScope.CLUSTER));
682    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
683      QuotaScope.CLUSTER);
684    admin.setQuota(QuotaSettingsFactory.unthrottleUser(user));
685
686      // set CLUSTER quota scope for user and table
687    admin.setQuota(QuotaSettingsFactory.throttleUser(user, tableName, ThrottleType.REQUEST_NUMBER,
688      10, TimeUnit.MINUTES, QuotaScope.CLUSTER));
689    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
690      QuotaScope.CLUSTER);
691    admin.setQuota(QuotaSettingsFactory.unthrottleUser(user));
692
693      // set CLUSTER quota scope for user and namespace
694    admin.setQuota(QuotaSettingsFactory.throttleUser(user, namespace, ThrottleType.REQUEST_NUMBER,
695      10, TimeUnit.MINUTES, QuotaScope.CLUSTER));
696    verifyRecordPresentInQuotaTable(ThrottleType.REQUEST_NUMBER, 10, TimeUnit.MINUTES,
697      QuotaScope.CLUSTER);
698    admin.setQuota(QuotaSettingsFactory.unthrottleUser(user));
699  }
700
701  private void testSwitchRpcThrottle(Admin admin, boolean oldRpcThrottle, boolean newRpcThrottle)
702      throws IOException {
703    boolean state = admin.switchRpcThrottle(newRpcThrottle);
704    Assert.assertEquals(oldRpcThrottle, state);
705    Assert.assertEquals(newRpcThrottle, admin.isRpcThrottleEnabled());
706    TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream()
707        .forEach(rs -> Assert.assertEquals(newRpcThrottle,
708          rs.getRegionServer().getRegionServerRpcQuotaManager().isRpcThrottleEnabled()));
709  }
710
711  private void verifyRecordPresentInQuotaTable(ThrottleType type, long limit, TimeUnit tu)
712      throws Exception {
713    verifyRecordPresentInQuotaTable(type, limit, tu, QuotaScope.MACHINE);
714  }
715
716  private void verifyRecordPresentInQuotaTable(ThrottleType type, long limit, TimeUnit tu,
717      QuotaScope scope) throws Exception {
718    // Verify the RPC Quotas in the table
719    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME);
720        ResultScanner scanner = quotaTable.getScanner(new Scan())) {
721      Result r = Iterables.getOnlyElement(scanner);
722      CellScanner cells = r.cellScanner();
723      assertTrue("Expected to find a cell", cells.advance());
724      assertRPCQuota(type, limit, tu, scope, cells.current());
725    }
726  }
727
728  private void verifyRecordNotPresentInQuotaTable() throws Exception {
729    // Verify that the record doesn't exist in the QuotaTableUtil.QUOTA_TABLE_NAME
730    try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME);
731        ResultScanner scanner = quotaTable.getScanner(new Scan())) {
732      assertNull("Did not expect to find a quota entry", scanner.next());
733    }
734  }
735
736  private void verifyFetchableViaAPI(Admin admin, ThrottleType type, long limit, TimeUnit tu)
737      throws Exception {
738    // Verify we can retrieve the new quota via the QuotaRetriever API
739    try (QuotaRetriever quotaScanner = QuotaRetriever.open(admin.getConfiguration())) {
740      assertRPCQuota(type, limit, tu, Iterables.getOnlyElement(quotaScanner));
741    }
742  }
743
744  private void verifyNotFetchableViaAPI(Admin admin) throws Exception {
745    // Verify that we can also not fetch it via the API
746    try (QuotaRetriever quotaScanner = QuotaRetriever.open(admin.getConfiguration())) {
747      assertNull("Did not expect to find a quota entry", quotaScanner.next());
748    }
749  }
750
751  private void assertRPCQuota(ThrottleType type, long limit, TimeUnit tu, QuotaScope scope,
752      Cell cell) throws Exception {
753    Quotas q = QuotaTableUtil
754        .quotasFromData(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
755    assertTrue("Quota should have rpc quota defined", q.hasThrottle());
756
757    QuotaProtos.Throttle rpcQuota = q.getThrottle();
758    QuotaProtos.TimedQuota t = null;
759
760    switch (type) {
761      case REQUEST_SIZE:
762        assertTrue(rpcQuota.hasReqSize());
763        t = rpcQuota.getReqSize();
764        break;
765      case READ_NUMBER:
766        assertTrue(rpcQuota.hasReadNum());
767        t = rpcQuota.getReadNum();
768        break;
769      case READ_SIZE:
770        assertTrue(rpcQuota.hasReadSize());
771        t = rpcQuota.getReadSize();
772        break;
773      case REQUEST_NUMBER:
774        assertTrue(rpcQuota.hasReqNum());
775        t = rpcQuota.getReqNum();
776        break;
777      case WRITE_NUMBER:
778        assertTrue(rpcQuota.hasWriteNum());
779        t = rpcQuota.getWriteNum();
780        break;
781      case WRITE_SIZE:
782        assertTrue(rpcQuota.hasWriteSize());
783        t = rpcQuota.getWriteSize();
784        break;
785      case REQUEST_CAPACITY_UNIT:
786        assertTrue(rpcQuota.hasReqCapacityUnit());
787        t = rpcQuota.getReqCapacityUnit();
788        break;
789      case READ_CAPACITY_UNIT:
790        assertTrue(rpcQuota.hasReadCapacityUnit());
791        t = rpcQuota.getReadCapacityUnit();
792        break;
793      case WRITE_CAPACITY_UNIT:
794        assertTrue(rpcQuota.hasWriteCapacityUnit());
795        t = rpcQuota.getWriteCapacityUnit();
796        break;
797      default:
798    }
799
800    assertEquals(scope, ProtobufUtil.toQuotaScope(t.getScope()));
801    assertEquals(t.getSoftLimit(), limit);
802    assertEquals(t.getTimeUnit(), ProtobufUtil.toProtoTimeUnit(tu));
803  }
804
805  private void assertRPCQuota(ThrottleType type, long limit, TimeUnit tu,
806      QuotaSettings actualSettings) throws Exception {
807    assertTrue(
808        "The actual QuotaSettings was not an instance of " + ThrottleSettings.class + " but of "
809            + actualSettings.getClass(), actualSettings instanceof ThrottleSettings);
810    QuotaProtos.ThrottleRequest throttleRequest = ((ThrottleSettings) actualSettings).getProto();
811    assertEquals(limit, throttleRequest.getTimedQuota().getSoftLimit());
812    assertEquals(ProtobufUtil.toProtoTimeUnit(tu), throttleRequest.getTimedQuota().getTimeUnit());
813    assertEquals(ProtobufUtil.toProtoThrottleType(type), throttleRequest.getType());
814  }
815
816  private void assertSpaceQuota(
817      long sizeLimit, SpaceViolationPolicy violationPolicy, Cell cell) throws Exception {
818    Quotas q = QuotaTableUtil.quotasFromData(
819        cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
820    assertTrue("Quota should have space quota defined", q.hasSpace());
821    QuotaProtos.SpaceQuota spaceQuota = q.getSpace();
822    assertEquals(sizeLimit, spaceQuota.getSoftLimit());
823    assertEquals(violationPolicy, ProtobufUtil.toViolationPolicy(spaceQuota.getViolationPolicy()));
824  }
825
826  private void assertSpaceQuota(
827      long sizeLimit, SpaceViolationPolicy violationPolicy, QuotaSettings actualSettings) {
828    assertTrue("The actual QuotaSettings was not an instance of " + SpaceLimitSettings.class
829        + " but of " + actualSettings.getClass(), actualSettings instanceof SpaceLimitSettings);
830    SpaceLimitRequest spaceLimitRequest = ((SpaceLimitSettings) actualSettings).getProto();
831    assertEquals(sizeLimit, spaceLimitRequest.getQuota().getSoftLimit());
832    assertEquals(violationPolicy,
833        ProtobufUtil.toViolationPolicy(spaceLimitRequest.getQuota().getViolationPolicy()));
834  }
835
836  private int countResults(final QuotaFilter filter) throws Exception {
837    QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration(), filter);
838    try {
839      int count = 0;
840      for (QuotaSettings settings: scanner) {
841        LOG.debug(Objects.toString(settings));
842        count++;
843      }
844      return count;
845    } finally {
846      scanner.close();
847    }
848  }
849
850  @Test
851  public void testUserUnThrottleByType() throws Exception {
852    final Admin admin = TEST_UTIL.getAdmin();
853    final String userName = User.getCurrent().getShortName();
854    String userName01 = "user01";
855    // Add 6req/min limit
856    admin.setQuota(QuotaSettingsFactory.throttleUser(userName, ThrottleType.REQUEST_NUMBER, 6,
857      TimeUnit.MINUTES));
858    admin.setQuota(
859      QuotaSettingsFactory.throttleUser(userName, ThrottleType.REQUEST_SIZE, 6, TimeUnit.MINUTES));
860    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, ThrottleType.REQUEST_NUMBER, 6,
861      TimeUnit.MINUTES));
862    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, ThrottleType.REQUEST_SIZE, 6,
863      TimeUnit.MINUTES));
864    admin.setQuota(
865      QuotaSettingsFactory.unthrottleUserByThrottleType(userName, ThrottleType.REQUEST_NUMBER));
866    assertEquals(3, getQuotaSettingCount(admin));
867    admin.setQuota(
868      QuotaSettingsFactory.unthrottleUserByThrottleType(userName, ThrottleType.REQUEST_SIZE));
869    assertEquals(2, getQuotaSettingCount(admin));
870    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName01));
871    assertEquals(0, getQuotaSettingCount(admin));
872  }
873
874  @Test
875  public void testUserTableUnThrottleByType() throws Exception {
876    final Admin admin = TEST_UTIL.getAdmin();
877    final String userName = User.getCurrent().getShortName();
878    String userName01 = "user01";
879    // Add 6req/min limit
880    admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[0],
881      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
882    admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[0],
883      ThrottleType.REQUEST_SIZE, 6, TimeUnit.MINUTES));
884    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, TABLE_NAMES[1],
885      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
886    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, TABLE_NAMES[1],
887      ThrottleType.REQUEST_SIZE, 6, TimeUnit.MINUTES));
888    admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(userName, TABLE_NAMES[0],
889      ThrottleType.REQUEST_NUMBER));
890    assertEquals(3, getQuotaSettingCount(admin));
891    admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(userName, TABLE_NAMES[0],
892      ThrottleType.REQUEST_SIZE));
893    assertEquals(2, getQuotaSettingCount(admin));
894    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName01));
895    assertEquals(0, getQuotaSettingCount(admin));
896  }
897
898  @Test
899  public void testUserNameSpaceUnThrottleByType() throws Exception {
900    final Admin admin = TEST_UTIL.getAdmin();
901    final String userName = User.getCurrent().getShortName();
902    String userName01 = "user01";
903    // Add 6req/min limit
904    admin.setQuota(QuotaSettingsFactory.throttleUser(userName, NAMESPACES[0],
905      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
906    admin.setQuota(QuotaSettingsFactory.throttleUser(userName, NAMESPACES[0],
907      ThrottleType.REQUEST_SIZE, 6, TimeUnit.MINUTES));
908    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, NAMESPACES[1],
909      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
910    admin.setQuota(QuotaSettingsFactory.throttleUser(userName01, NAMESPACES[1],
911      ThrottleType.REQUEST_SIZE, 6, TimeUnit.MINUTES));
912    admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(userName, NAMESPACES[0],
913      ThrottleType.REQUEST_NUMBER));
914    assertEquals(3, getQuotaSettingCount(admin));
915    admin.setQuota(QuotaSettingsFactory.unthrottleUserByThrottleType(userName, NAMESPACES[0],
916      ThrottleType.REQUEST_SIZE));
917    assertEquals(2, getQuotaSettingCount(admin));
918    admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName01));
919    assertEquals(0, getQuotaSettingCount(admin));
920  }
921
922  @Test
923  public void testTableUnThrottleByType() throws Exception {
924    final Admin admin = TEST_UTIL.getAdmin();
925    final String userName = User.getCurrent().getShortName();
926    // Add 6req/min limit
927    admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0], ThrottleType.REQUEST_NUMBER,
928      6, TimeUnit.MINUTES));
929    admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0], ThrottleType.REQUEST_SIZE, 6,
930      TimeUnit.MINUTES));
931    admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[1], ThrottleType.REQUEST_NUMBER,
932      6, TimeUnit.MINUTES));
933    admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[1], ThrottleType.REQUEST_SIZE, 6,
934      TimeUnit.MINUTES));
935    admin.setQuota(QuotaSettingsFactory.unthrottleTableByThrottleType(TABLE_NAMES[0],
936      ThrottleType.REQUEST_NUMBER));
937    assertEquals(3, getQuotaSettingCount(admin));
938    admin.setQuota(QuotaSettingsFactory.unthrottleTableByThrottleType(TABLE_NAMES[0],
939      ThrottleType.REQUEST_SIZE));
940    assertEquals(2, getQuotaSettingCount(admin));
941    admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[1]));
942    assertEquals(0, getQuotaSettingCount(admin));
943  }
944
945  @Test
946  public void testNameSpaceUnThrottleByType() throws Exception {
947    final Admin admin = TEST_UTIL.getAdmin();
948    final String userName = User.getCurrent().getShortName();
949    // Add 6req/min limit
950    admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACES[0],
951      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
952    admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACES[0], ThrottleType.REQUEST_SIZE,
953      6, TimeUnit.MINUTES));
954    admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACES[1],
955      ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
956    admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACES[1], ThrottleType.REQUEST_SIZE,
957      6, TimeUnit.MINUTES));
958    admin.setQuota(QuotaSettingsFactory.unthrottleNamespaceByThrottleType(NAMESPACES[0],
959      ThrottleType.REQUEST_NUMBER));
960    assertEquals(3, getQuotaSettingCount(admin));
961    admin.setQuota(QuotaSettingsFactory.unthrottleNamespaceByThrottleType(NAMESPACES[0],
962      ThrottleType.REQUEST_SIZE));
963    assertEquals(2, getQuotaSettingCount(admin));
964    admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(NAMESPACES[1]));
965    assertEquals(0, getQuotaSettingCount(admin));
966  }
967
968  @Test
969  public void testRegionServerUnThrottleByType() throws Exception {
970    final Admin admin = TEST_UTIL.getAdmin();
971    final String[] REGIONSERVER = { "RS01", "RS02" };
972
973    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(REGIONSERVER[0],
974      ThrottleType.READ_NUMBER, 4, TimeUnit.MINUTES));
975    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(REGIONSERVER[0],
976      ThrottleType.WRITE_NUMBER, 4, TimeUnit.MINUTES));
977    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(REGIONSERVER[1],
978      ThrottleType.READ_NUMBER, 4, TimeUnit.MINUTES));
979    admin.setQuota(QuotaSettingsFactory.throttleRegionServer(REGIONSERVER[1],
980      ThrottleType.WRITE_NUMBER, 4, TimeUnit.MINUTES));
981
982    admin.setQuota(QuotaSettingsFactory.unthrottleRegionServerByThrottleType(REGIONSERVER[0],
983      ThrottleType.READ_NUMBER));
984    assertEquals(3, getQuotaSettingCount(admin));
985    admin.setQuota(QuotaSettingsFactory.unthrottleRegionServerByThrottleType(REGIONSERVER[0],
986      ThrottleType.WRITE_NUMBER));
987    assertEquals(2, getQuotaSettingCount(admin));
988    admin.setQuota(QuotaSettingsFactory.unthrottleRegionServer(REGIONSERVER[1]));
989    assertEquals(0, getQuotaSettingCount(admin));
990  }
991
992  public int getQuotaSettingCount(Admin admin) throws IOException {
993    List<QuotaSettings> list_quotas = admin.getQuota(new QuotaFilter());
994    int quotaSettingCount = 0;
995    for (QuotaSettings setting : list_quotas) {
996      quotaSettingCount++;
997      LOG.info("Quota Setting:" + setting);
998    }
999    return quotaSettingCount;
1000  }
1001}