View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.quotas;
13  
14  import static org.junit.Assert.assertEquals;
15  
16  import java.util.concurrent.TimeUnit;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.hadoop.hbase.HBaseTestingUtility;
21  import org.apache.hadoop.hbase.HConstants;
22  import org.apache.hadoop.hbase.TableName;
23  import org.apache.hadoop.hbase.client.Admin;
24  import org.apache.hadoop.hbase.client.Get;
25  import org.apache.hadoop.hbase.client.HTable;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
28  import org.apache.hadoop.hbase.client.Table;
29  import org.apache.hadoop.hbase.security.User;
30  import org.apache.hadoop.hbase.testclassification.MediumTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
33  import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
34  import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
35  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
36  import org.junit.After;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  @Category({ MediumTests.class })
43  public class TestQuotaThrottle {
44    final Log LOG = LogFactory.getLog(getClass());
45  
46    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47  
48    private final static byte[] FAMILY = Bytes.toBytes("cf");
49    private final static byte[] QUALIFIER = Bytes.toBytes("q");
50  
51    private final static TableName[] TABLE_NAMES = new TableName[] {
52        TableName.valueOf("TestQuotaAdmin0"), TableName.valueOf("TestQuotaAdmin1"),
53        TableName.valueOf("TestQuotaAdmin2") };
54  
55    private static HTable[] tables;
56  
57    @BeforeClass
58    public static void setUpBeforeClass() throws Exception {
59      TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
60      TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
61      TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
62      TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
63      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
64      TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
65      TEST_UTIL.startMiniCluster(1);
66      TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
67      QuotaCache.setTEST_FORCE_REFRESH(true);
68  
69      tables = new HTable[TABLE_NAMES.length];
70      for (int i = 0; i < TABLE_NAMES.length; ++i) {
71        tables[i] = TEST_UTIL.createTable(TABLE_NAMES[i], FAMILY);
72      }
73    }
74  
75    @AfterClass
76    public static void tearDownAfterClass() throws Exception {
77      for (int i = 0; i < tables.length; ++i) {
78        if (tables[i] != null) {
79          tables[i].close();
80          TEST_UTIL.deleteTable(TABLE_NAMES[i]);
81        }
82      }
83  
84      TEST_UTIL.shutdownMiniCluster();
85    }
86  
87    @After
88    public void tearDown() throws Exception {
89      for (RegionServerThread rst : TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
90        RegionServerQuotaManager quotaManager = rst.getRegionServer().getRegionServerQuotaManager();
91        QuotaCache quotaCache = quotaManager.getQuotaCache();
92        quotaCache.getNamespaceQuotaCache().clear();
93        quotaCache.getTableQuotaCache().clear();
94        quotaCache.getUserQuotaCache().clear();
95      }
96    }
97  
98    @Test(timeout = 60000)
99    public void testUserGlobalThrottle() throws Exception {
100     final Admin admin = TEST_UTIL.getHBaseAdmin();
101     final String userName = User.getCurrent().getShortName();
102 
103     // Add 6req/min limit
104     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, ThrottleType.REQUEST_NUMBER, 6,
105       TimeUnit.MINUTES));
106     triggerUserCacheRefresh(false, TABLE_NAMES);
107 
108     // should execute at max 6 requests
109     assertEquals(6, doPuts(100, tables));
110 
111     // wait a minute and you should get other 6 requests executed
112     waitMinuteQuota();
113     assertEquals(6, doPuts(100, tables));
114 
115     // Remove all the limits
116     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
117     triggerUserCacheRefresh(true, TABLE_NAMES);
118     assertEquals(60, doPuts(60, tables));
119     assertEquals(60, doGets(60, tables));
120   }
121 
122   @Test(timeout=60000)
123   public void testUserGlobalReadAndWriteThrottle() throws Exception {
124     final Admin admin = TEST_UTIL.getHBaseAdmin();
125     final String userName = User.getCurrent().getShortName();
126 
127     // Add 6req/min limit for read request
128     admin.setQuota(QuotaSettingsFactory
129       .throttleUser(userName, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
130     triggerUserCacheRefresh(false, TABLE_NAMES);
131 
132     // not limit for write request and should execute at max 6 read requests
133     assertEquals(60, doPuts(60, tables));
134     assertEquals(6, doGets(100, tables));
135 
136     waitMinuteQuota();
137 
138     // Add 6req/min limit for write request
139     admin.setQuota(QuotaSettingsFactory
140       .throttleUser(userName, ThrottleType.WRITE_NUMBER, 6, TimeUnit.MINUTES));
141     triggerUserCacheRefresh(false, TABLE_NAMES);
142 
143     // should execute at max 6 read requests and at max 6 write write requests
144     assertEquals(6, doGets(100, tables));
145     assertEquals(6, doPuts(60, tables));
146 
147     // Remove all the limits
148     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
149     triggerUserCacheRefresh(true, TABLE_NAMES);
150     assertEquals(60, doPuts(60, tables));
151     assertEquals(60, doGets(60, tables));
152   }
153 
154   @Test(timeout = 60000)
155   public void testUserTableThrottle() throws Exception {
156     final Admin admin = TEST_UTIL.getHBaseAdmin();
157     final String userName = User.getCurrent().getShortName();
158 
159     // Add 6req/min limit
160     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[0],
161       ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
162     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
163 
164     // should execute at max 6 requests on tables[0] and have no limit on tables[1]
165     assertEquals(6, doPuts(100, tables[0]));
166     assertEquals(30, doPuts(30, tables[1]));
167 
168     // wait a minute and you should get other 6 requests executed
169     waitMinuteQuota();
170     assertEquals(6, doPuts(100, tables[0]));
171 
172     // Remove all the limits
173     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, TABLE_NAMES[0]));
174     triggerUserCacheRefresh(true, TABLE_NAMES);
175     assertEquals(60, doPuts(60, tables));
176     assertEquals(60, doGets(60, tables));
177   }
178 
179   @Test(timeout=60000)
180   public void testUserTableReadAndWriteThrottle() throws Exception {
181     final Admin admin = TEST_UTIL.getHBaseAdmin();
182     final String userName = User.getCurrent().getShortName();
183 
184     // Add 6req/min limit for write request on tables[0]
185     admin.setQuota(QuotaSettingsFactory
186       .throttleUser(userName, TABLE_NAMES[0], ThrottleType.WRITE_NUMBER, 6, TimeUnit.MINUTES));
187     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
188 
189     // should execute at max 6 write requests and have no limit for read request
190     assertEquals(6, doPuts(100, tables[0]));
191     assertEquals(60, doGets(60, tables[0]));
192 
193     // no limit on tables[1]
194     assertEquals(60, doPuts(60, tables[1]));
195     assertEquals(60, doGets(60, tables[1]));
196 
197     // wait a minute and you should get other 6  write requests executed
198     waitMinuteQuota();
199 
200     // Add 6req/min limit for read request on tables[0]
201     admin.setQuota(QuotaSettingsFactory
202       .throttleUser(userName, TABLE_NAMES[0], ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
203     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
204 
205     // should execute at max 6 read requests and at max 6 write requests
206     assertEquals(6, doPuts(100, tables[0]));
207     assertEquals(6, doGets(60, tables[0]));
208 
209     // no limit on tables[1]
210     assertEquals(30, doPuts(30, tables[1]));
211     assertEquals(30, doGets(30, tables[1]));
212 
213     // Remove all the limits
214     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, TABLE_NAMES[0]));
215     triggerUserCacheRefresh(true, TABLE_NAMES);
216     assertEquals(60, doPuts(60, tables));
217     assertEquals(60, doGets(60, tables));
218   }
219 
220   @Test(timeout = 60000)
221   public void testUserNamespaceThrottle() throws Exception {
222     final Admin admin = TEST_UTIL.getHBaseAdmin();
223     final String userName = User.getCurrent().getShortName();
224     final String NAMESPACE = "default";
225 
226     // Add 6req/min limit
227     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, NAMESPACE,
228       ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
229     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
230 
231     // should execute at max 6 requests on tables[0] and have no limit on tables[1]
232     assertEquals(6, doPuts(100, tables[0]));
233 
234     // wait a minute and you should get other 6 requests executed
235     waitMinuteQuota();
236     assertEquals(6, doPuts(100, tables[1]));
237 
238     // Remove all the limits
239     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, NAMESPACE));
240     triggerUserCacheRefresh(true, TABLE_NAMES);
241     assertEquals(60, doPuts(60, tables));
242     assertEquals(60, doGets(60, tables));
243   }
244 
245   @Test(timeout=60000)
246   public void testUserNamespaceReadAndWriteThrottle() throws Exception {
247     final Admin admin = TEST_UTIL.getHBaseAdmin();
248     final String userName = User.getCurrent().getShortName();
249     final String NAMESPACE = "default";
250 
251     // Add 6req/min limit for read request
252     admin.setQuota(QuotaSettingsFactory
253       .throttleUser(userName, NAMESPACE, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
254     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
255 
256     // should execute at max 6 read requests and have no limit for write request
257     assertEquals(6, doGets(60, tables[0]));
258     assertEquals(60, doPuts(60, tables[0]));
259 
260     waitMinuteQuota();
261 
262     // Add 6req/min limit for write request, too
263     admin.setQuota(QuotaSettingsFactory
264       .throttleUser(userName, NAMESPACE, ThrottleType.WRITE_NUMBER, 6, TimeUnit.MINUTES));
265     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
266 
267     // should execute at max 6 read requests and at max 6 write requests
268     assertEquals(6, doGets(60, tables[0]));
269     assertEquals(6, doPuts(60, tables[0]));
270 
271     // Remove all the limits
272     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, NAMESPACE));
273     triggerUserCacheRefresh(true, TABLE_NAMES);
274     assertEquals(60, doPuts(60, tables));
275     assertEquals(60, doGets(60, tables));
276   }
277 
278   @Test(timeout = 60000)
279   public void testTableGlobalThrottle() throws Exception {
280     final Admin admin = TEST_UTIL.getHBaseAdmin();
281 
282     // Add 6req/min limit
283     admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0], ThrottleType.REQUEST_NUMBER,
284       6, TimeUnit.MINUTES));
285     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
286 
287     // should execute at max 6 requests
288     assertEquals(6, doPuts(100, tables[0]));
289     // should have no limits
290     assertEquals(30, doPuts(30, tables[1]));
291 
292     // wait a minute and you should get other 6 requests executed
293     waitMinuteQuota();
294     assertEquals(6, doPuts(100, tables[0]));
295 
296     // Remove all the limits
297     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[0]));
298     triggerTableCacheRefresh(true, TABLE_NAMES[0]);
299     assertEquals(80, doGets(80, tables[0], tables[1]));
300   }
301 
302   @Test(timeout=60000)
303   public void testTableGlobalReadAndWriteThrottle() throws Exception {
304     final Admin admin = TEST_UTIL.getHBaseAdmin();
305 
306     // Add 6req/min limit for read request
307     admin.setQuota(QuotaSettingsFactory
308       .throttleTable(TABLE_NAMES[0], ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
309     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
310 
311     // should execute at max 6 read requests and have no limit for write request
312     assertEquals(6, doGets(100, tables[0]));
313     assertEquals(100, doPuts(100, tables[0]));
314     // should have no limits on tables[1]
315     assertEquals(30, doPuts(30, tables[1]));
316     assertEquals(30, doGets(30, tables[1]));
317 
318     // wait a minute and you should get other 6 requests executed
319     waitMinuteQuota();
320 
321     // Add 6req/min limit for write request, too
322     admin.setQuota(QuotaSettingsFactory
323       .throttleTable(TABLE_NAMES[0], ThrottleType.WRITE_NUMBER, 6, TimeUnit.MINUTES));
324     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
325 
326     // should execute at max 6 read requests and at max 6 write requests
327     assertEquals(6, doGets(100, tables[0]));
328     assertEquals(6, doPuts(100, tables[0]));
329     // should have no limits on tables[1]
330     assertEquals(30, doPuts(30, tables[1]));
331     assertEquals(30, doGets(30, tables[1]));
332 
333     // Remove all the limits
334     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[0]));
335     triggerTableCacheRefresh(true, TABLE_NAMES[0]);
336     assertEquals(80, doGets(80, tables[0], tables[1]));
337   }
338 
339   @Test(timeout = 60000)
340   public void testNamespaceGlobalThrottle() throws Exception {
341     final Admin admin = TEST_UTIL.getHBaseAdmin();
342     final String NAMESPACE = "default";
343 
344     // Add 6req/min limit
345     admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACE, ThrottleType.REQUEST_NUMBER,
346       6, TimeUnit.MINUTES));
347     triggerNamespaceCacheRefresh(false, TABLE_NAMES[0]);
348 
349     // should execute at max 6 requests
350     assertEquals(6, doPuts(100, tables[0]));
351 
352     // wait a minute and you should get other 6 requests executed
353     waitMinuteQuota();
354     assertEquals(6, doPuts(100, tables[1]));
355 
356     admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(NAMESPACE));
357     triggerNamespaceCacheRefresh(true, TABLE_NAMES[0]);
358     assertEquals(40, doPuts(40, tables[0]));
359   }
360 
361   @Test(timeout=60000)
362   public void testNamespaceGlobalReadAndWriteThrottle() throws Exception {
363     final Admin admin = TEST_UTIL.getHBaseAdmin();
364     final String NAMESPACE = "default";
365 
366     // Add 6req/min limit for write request
367     admin.setQuota(QuotaSettingsFactory
368       .throttleNamespace(NAMESPACE, ThrottleType.WRITE_NUMBER, 6, TimeUnit.MINUTES));
369     triggerNamespaceCacheRefresh(false, TABLE_NAMES[0]);
370 
371     // should execute at max 6 write requests and no limit for read request
372     assertEquals(6, doPuts(100, tables[0]));
373     assertEquals(100, doGets(100, tables[0]));
374 
375     // wait a minute and you should get other 6 requests executed
376     waitMinuteQuota();
377 
378     // Add 6req/min limit for read request, too
379     admin.setQuota(QuotaSettingsFactory
380       .throttleNamespace(NAMESPACE, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
381     triggerNamespaceCacheRefresh(false, TABLE_NAMES[0]);
382 
383     // should execute at max 6 write requests and at max 6 read requests
384     assertEquals(6, doPuts(100, tables[0]));
385     assertEquals(6, doGets(100, tables[0]));
386 
387     admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(NAMESPACE));
388     triggerNamespaceCacheRefresh(true, TABLE_NAMES[0]);
389     assertEquals(40, doPuts(40, tables[0]));
390   }
391 
392   @Test(timeout = 60000)
393   public void testUserAndTableThrottle() throws Exception {
394     final Admin admin = TEST_UTIL.getHBaseAdmin();
395     final String userName = User.getCurrent().getShortName();
396 
397     // Add 6req/min limit for the user on tables[0]
398     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[0],
399       ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
400     triggerUserCacheRefresh(false, TABLE_NAMES[0]);
401     // Add 12req/min limit for the user
402     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, ThrottleType.REQUEST_NUMBER, 12,
403       TimeUnit.MINUTES));
404     triggerUserCacheRefresh(false, TABLE_NAMES[1], TABLE_NAMES[2]);
405     // Add 8req/min limit for the tables[1]
406     admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[1], ThrottleType.REQUEST_NUMBER,
407       8, TimeUnit.MINUTES));
408     triggerTableCacheRefresh(false, TABLE_NAMES[1]);
409     // Add a lower table level throttle on tables[0]
410     admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0], ThrottleType.REQUEST_NUMBER,
411       3, TimeUnit.MINUTES));
412     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
413 
414     // should execute at max 12 requests
415     assertEquals(12, doGets(100, tables[2]));
416 
417     // should execute at max 8 requests
418     waitMinuteQuota();
419     assertEquals(8, doGets(100, tables[1]));
420 
421     // should execute at max 3 requests
422     waitMinuteQuota();
423     assertEquals(3, doPuts(100, tables[0]));
424 
425     // Remove all the throttling rules
426     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, TABLE_NAMES[0]));
427     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
428     triggerUserCacheRefresh(true, TABLE_NAMES[0], TABLE_NAMES[1]);
429 
430     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[1]));
431     triggerTableCacheRefresh(true, TABLE_NAMES[1]);
432     waitMinuteQuota();
433     assertEquals(40, doGets(40, tables[1]));
434 
435     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[0]));
436     triggerTableCacheRefresh(true, TABLE_NAMES[0]);
437     waitMinuteQuota();
438     assertEquals(40, doGets(40, tables[0]));
439   }
440 
441   @Test(timeout = 60000)
442   public void testUserGlobalBypassThrottle() throws Exception {
443     final Admin admin = TEST_UTIL.getHBaseAdmin();
444     final String userName = User.getCurrent().getShortName();
445     final String NAMESPACE = "default";
446 
447     // Add 6req/min limit for tables[0]
448     admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0], ThrottleType.REQUEST_NUMBER,
449       6, TimeUnit.MINUTES));
450     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
451     // Add 13req/min limit for the user
452     admin.setQuota(QuotaSettingsFactory.throttleNamespace(NAMESPACE, ThrottleType.REQUEST_NUMBER,
453       13, TimeUnit.MINUTES));
454     triggerNamespaceCacheRefresh(false, TABLE_NAMES[1]);
455 
456     // should execute at max 6 requests on table[0] and (13 - 6) on table[1]
457     assertEquals(6, doPuts(100, tables[0]));
458     assertEquals(7, doGets(100, tables[1]));
459     waitMinuteQuota();
460 
461     // Set the global bypass for the user
462     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
463     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[2],
464       ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
465     triggerUserCacheRefresh(false, TABLE_NAMES[2]);
466     assertEquals(30, doGets(30, tables[0]));
467     assertEquals(30, doGets(30, tables[1]));
468     waitMinuteQuota();
469 
470     // Remove the global bypass
471     // should execute at max 6 requests on table[0] and (13 - 6) on table[1]
472     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false));
473     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, TABLE_NAMES[2]));
474     triggerUserCacheRefresh(true, TABLE_NAMES[2]);
475     assertEquals(6, doPuts(100, tables[0]));
476     assertEquals(7, doGets(100, tables[1]));
477 
478     // unset throttle
479     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[0]));
480     admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(NAMESPACE));
481     waitMinuteQuota();
482     triggerTableCacheRefresh(true, TABLE_NAMES[0]);
483     triggerNamespaceCacheRefresh(true, TABLE_NAMES[1]);
484     assertEquals(30, doGets(30, tables[0]));
485     assertEquals(30, doGets(30, tables[1]));
486   }
487 
488   @Test
489   public void testTableExistsGetThrottle() throws Exception {
490     final Admin admin = TEST_UTIL.getHBaseAdmin();
491 
492     // Add throttle quota
493     admin.setQuota(QuotaSettingsFactory.throttleTable(TABLE_NAMES[0],
494         ThrottleType.REQUEST_NUMBER, 100, TimeUnit.MINUTES));
495     triggerTableCacheRefresh(false, TABLE_NAMES[0]);
496 
497     Table table = TEST_UTIL.getConnection().getTable(TABLE_NAMES[0]);
498     // An exists call when having throttle quota
499     table.exists(new Get(Bytes.toBytes("abc")));
500 
501     admin.setQuota(QuotaSettingsFactory.unthrottleTable(TABLE_NAMES[0]));
502     triggerTableCacheRefresh(true, TABLE_NAMES[0]);
503   }
504 
505   private int doPuts(int maxOps, final HTable... tables) throws Exception {
506     int count = 0;
507     try {
508       while (count < maxOps) {
509         Put put = new Put(Bytes.toBytes("row-" + count));
510         put.addColumn(FAMILY, QUALIFIER, Bytes.toBytes("data-" + count));
511         for (final HTable table : tables) {
512           table.put(put);
513         }
514         count += tables.length;
515       }
516     } catch (RetriesExhaustedWithDetailsException e) {
517       for (Throwable t : e.getCauses()) {
518         if (!(t instanceof ThrottlingException)) {
519           throw e;
520         }
521       }
522       LOG.error("put failed after nRetries=" + count, e);
523     }
524     return count;
525   }
526 
527   private long doGets(int maxOps, final HTable... tables) throws Exception {
528     int count = 0;
529     try {
530       while (count < maxOps) {
531         Get get = new Get(Bytes.toBytes("row-" + count));
532         for (final HTable table : tables) {
533           table.get(get);
534         }
535         count += tables.length;
536       }
537     } catch (ThrottlingException e) {
538       LOG.error("get failed after nRetries=" + count, e);
539     }
540     return count;
541   }
542 
543   private void triggerUserCacheRefresh(boolean bypass, TableName... tables) throws Exception {
544     triggerCacheRefresh(bypass, true, false, false, tables);
545   }
546 
547   private void triggerTableCacheRefresh(boolean bypass, TableName... tables) throws Exception {
548     triggerCacheRefresh(bypass, false, true, false, tables);
549   }
550 
551   private void triggerNamespaceCacheRefresh(boolean bypass, TableName... tables) throws Exception {
552     triggerCacheRefresh(bypass, false, false, true, tables);
553   }
554 
555   private void triggerCacheRefresh(boolean bypass, boolean userLimiter, boolean tableLimiter,
556       boolean nsLimiter, final TableName... tables) throws Exception {
557     for (RegionServerThread rst : TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
558       RegionServerQuotaManager quotaManager = rst.getRegionServer().getRegionServerQuotaManager();
559       QuotaCache quotaCache = quotaManager.getQuotaCache();
560 
561       quotaCache.triggerCacheRefresh();
562       Thread.sleep(250);
563 
564       for (TableName table : tables) {
565         quotaCache.getTableLimiter(table);
566       }
567 
568       boolean isUpdated = false;
569       while (!isUpdated) {
570         isUpdated = true;
571         for (TableName table : tables) {
572           boolean isBypass = true;
573           if (userLimiter) {
574             isBypass &= quotaCache.getUserLimiter(User.getCurrent().getUGI(), table).isBypass();
575           }
576           if (tableLimiter) {
577             isBypass &= quotaCache.getTableLimiter(table).isBypass();
578           }
579           if (nsLimiter) {
580             isBypass &= quotaCache.getNamespaceLimiter(table.getNamespaceAsString()).isBypass();
581           }
582           if (isBypass != bypass) {
583             isUpdated = false;
584             Thread.sleep(250);
585             break;
586           }
587         }
588       }
589 
590       LOG.debug("QuotaCache");
591       LOG.debug(quotaCache.getNamespaceQuotaCache());
592       LOG.debug(quotaCache.getTableQuotaCache());
593       LOG.debug(quotaCache.getUserQuotaCache());
594     }
595   }
596 
597   private void waitMinuteQuota() {
598     EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge(
599         EnvironmentEdgeManager.currentTime() + 70000));
600   }
601 }