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  import static org.junit.Assert.fail;
16  
17  import java.util.concurrent.TimeUnit;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.client.Admin;
25  import org.apache.hadoop.hbase.security.User;
26  import org.apache.hadoop.hbase.testclassification.MediumTests;
27  import org.junit.AfterClass;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  /**
33   * minicluster tests that validate that quota entries are properly set in the quota table
34   */
35  @Category({ MediumTests.class })
36  public class TestQuotaAdmin {
37    private static final Log LOG = LogFactory.getLog(TestQuotaAdmin.class);
38  
39    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
40  
41    @BeforeClass
42    public static void setUpBeforeClass() throws Exception {
43      TEST_UTIL.getConfiguration().setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
44      TEST_UTIL.getConfiguration().setInt(QuotaCache.REFRESH_CONF_KEY, 2000);
45      TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
46      TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
47      TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
48      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
49      TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
50      TEST_UTIL.startMiniCluster(1);
51      TEST_UTIL.waitTableAvailable(QuotaTableUtil.QUOTA_TABLE_NAME);
52    }
53  
54    @AfterClass
55    public static void tearDownAfterClass() throws Exception {
56      TEST_UTIL.shutdownMiniCluster();
57    }
58  
59    @Test
60    public void testThrottleType() throws Exception {
61      Admin admin = TEST_UTIL.getHBaseAdmin();
62      String userName = User.getCurrent().getShortName();
63  
64      admin.setQuota(QuotaSettingsFactory
65        .throttleUser(userName, ThrottleType.READ_NUMBER, 6, TimeUnit.MINUTES));
66      admin.setQuota(QuotaSettingsFactory
67        .throttleUser(userName, ThrottleType.WRITE_NUMBER, 12, TimeUnit.MINUTES));
68      admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
69  
70      try (QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
71        int countThrottle = 0;
72        int countGlobalBypass = 0;
73        for (QuotaSettings settings: scanner) {
74          switch (settings.getQuotaType()) {
75            case THROTTLE:
76              ThrottleSettings throttle = (ThrottleSettings)settings;
77              if (throttle.getSoftLimit() == 6) {
78                assertEquals(ThrottleType.READ_NUMBER, throttle.getThrottleType());
79              } else if (throttle.getSoftLimit() == 12) {
80                assertEquals(ThrottleType.WRITE_NUMBER, throttle.getThrottleType());
81              } else {
82                fail("should not come here, because don't set quota with this limit");
83              }
84              assertEquals(userName, throttle.getUserName());
85              assertEquals(null, throttle.getTableName());
86              assertEquals(null, throttle.getNamespace());
87              assertEquals(TimeUnit.MINUTES, throttle.getTimeUnit());
88              countThrottle++;
89              break;
90            case GLOBAL_BYPASS:
91              countGlobalBypass++;
92              break;
93            default:
94              fail("unexpected settings type: " + settings.getQuotaType());
95          }
96        }
97        assertEquals(2, countThrottle);
98        assertEquals(1, countGlobalBypass);
99      }
100 
101     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
102     assertNumResults(1, null);
103     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false));
104     assertNumResults(0, null);
105   }
106 
107   @Test
108   public void testSimpleScan() throws Exception {
109     Admin admin = TEST_UTIL.getHBaseAdmin();
110     String userName = User.getCurrent().getShortName();
111 
112     admin.setQuota(QuotaSettingsFactory.throttleUser(userName, ThrottleType.REQUEST_NUMBER, 6,
113       TimeUnit.MINUTES));
114     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, true));
115 
116     try (QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration())) {
117       int countThrottle = 0;
118       int countGlobalBypass = 0;
119       for (QuotaSettings settings : scanner) {
120         LOG.debug(settings);
121         switch (settings.getQuotaType()) {
122         case THROTTLE:
123           ThrottleSettings throttle = (ThrottleSettings) settings;
124           assertEquals(userName, throttle.getUserName());
125           assertEquals(null, throttle.getTableName());
126           assertEquals(null, throttle.getNamespace());
127           assertEquals(6, throttle.getSoftLimit());
128           assertEquals(TimeUnit.MINUTES, throttle.getTimeUnit());
129           countThrottle++;
130           break;
131         case GLOBAL_BYPASS:
132           countGlobalBypass++;
133           break;
134         default:
135           fail("unexpected settings type: " + settings.getQuotaType());
136         }
137       }
138       assertEquals(1, countThrottle);
139       assertEquals(1, countGlobalBypass);
140     }
141 
142     admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName));
143     assertNumResults(1, null);
144     admin.setQuota(QuotaSettingsFactory.bypassGlobals(userName, false));
145     assertNumResults(0, null);
146   }
147 
148   @Test
149   public void testQuotaRetrieverFilter() throws Exception {
150     Admin admin = TEST_UTIL.getHBaseAdmin();
151     TableName[] tables =
152         new TableName[] { TableName.valueOf("T0"), TableName.valueOf("T01"),
153             TableName.valueOf("NS0:T2"), };
154     String[] namespaces = new String[] { "NS0", "NS01", "NS2" };
155     String[] users = new String[] { "User0", "User01", "User2" };
156 
157     for (String user : users) {
158       admin.setQuota(QuotaSettingsFactory.throttleUser(user, ThrottleType.REQUEST_NUMBER, 1,
159         TimeUnit.MINUTES));
160 
161       for (TableName table : tables) {
162         admin.setQuota(QuotaSettingsFactory.throttleUser(user, table, ThrottleType.REQUEST_NUMBER,
163           2, TimeUnit.MINUTES));
164       }
165 
166       for (String ns : namespaces) {
167         admin.setQuota(QuotaSettingsFactory.throttleUser(user, ns, ThrottleType.REQUEST_NUMBER, 3,
168           TimeUnit.MINUTES));
169       }
170     }
171     assertNumResults(21, null);
172 
173     for (TableName table : tables) {
174       admin.setQuota(QuotaSettingsFactory.throttleTable(table, ThrottleType.REQUEST_NUMBER, 4,
175         TimeUnit.MINUTES));
176     }
177     assertNumResults(24, null);
178 
179     for (String ns : namespaces) {
180       admin.setQuota(QuotaSettingsFactory.throttleNamespace(ns, ThrottleType.REQUEST_NUMBER, 5,
181         TimeUnit.MINUTES));
182     }
183     assertNumResults(27, null);
184 
185     assertNumResults(7, new QuotaFilter().setUserFilter("User0"));
186     assertNumResults(0, new QuotaFilter().setUserFilter("User"));
187     assertNumResults(21, new QuotaFilter().setUserFilter("User.*"));
188     assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("T0"));
189     assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setTableFilter("NS.*"));
190     assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setTableFilter("T"));
191     assertNumResults(6, new QuotaFilter().setUserFilter("User.*").setTableFilter("T.*"));
192     assertNumResults(3, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS0"));
193     assertNumResults(0, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS"));
194     assertNumResults(9, new QuotaFilter().setUserFilter("User.*").setNamespaceFilter("NS.*"));
195     assertNumResults(6, new QuotaFilter().setUserFilter("User.*").setTableFilter("T0")
196         .setNamespaceFilter("NS0"));
197     assertNumResults(1, new QuotaFilter().setTableFilter("T0"));
198     assertNumResults(0, new QuotaFilter().setTableFilter("T"));
199     assertNumResults(2, new QuotaFilter().setTableFilter("T.*"));
200     assertNumResults(3, new QuotaFilter().setTableFilter(".*T.*"));
201     assertNumResults(1, new QuotaFilter().setNamespaceFilter("NS0"));
202     assertNumResults(0, new QuotaFilter().setNamespaceFilter("NS"));
203     assertNumResults(3, new QuotaFilter().setNamespaceFilter("NS.*"));
204 
205     for (String user : users) {
206       admin.setQuota(QuotaSettingsFactory.unthrottleUser(user));
207       for (TableName table : tables) {
208         admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, table));
209       }
210       for (String ns : namespaces) {
211         admin.setQuota(QuotaSettingsFactory.unthrottleUser(user, ns));
212       }
213     }
214     assertNumResults(6, null);
215 
216     for (TableName table : tables) {
217       admin.setQuota(QuotaSettingsFactory.unthrottleTable(table));
218     }
219     assertNumResults(3, null);
220 
221     for (String ns : namespaces) {
222       admin.setQuota(QuotaSettingsFactory.unthrottleNamespace(ns));
223     }
224     assertNumResults(0, null);
225   }
226 
227   private void assertNumResults(int expected, final QuotaFilter filter) throws Exception {
228     assertEquals(expected, countResults(filter));
229   }
230 
231   private int countResults(final QuotaFilter filter) throws Exception {
232     QuotaRetriever scanner = QuotaRetriever.open(TEST_UTIL.getConfiguration(), filter);
233     try {
234       int count = 0;
235       for (QuotaSettings settings : scanner) {
236         LOG.debug(settings);
237         count++;
238       }
239       return count;
240     } finally {
241       scanner.close();
242     }
243   }
244 }