23
23
import static org .apache .hadoop .hbase .quotas .ThrottleQuotaTestUtil .doScans ;
24
24
import static org .apache .hadoop .hbase .quotas .ThrottleQuotaTestUtil .triggerUserCacheRefresh ;
25
25
import static org .apache .hadoop .hbase .quotas .ThrottleQuotaTestUtil .waitMinuteQuota ;
26
- import static org .junit .Assert .assertEquals ;
27
- import static org .junit .Assert .assertTrue ;
28
26
27
+ import java .util .concurrent .Callable ;
29
28
import java .util .concurrent .TimeUnit ;
30
29
import org .apache .hadoop .hbase .HBaseClassTestRule ;
31
30
import org .apache .hadoop .hbase .HBaseTestingUtil ;
@@ -71,7 +70,6 @@ public static void setUpBeforeClass() throws Exception {
71
70
// quotas enabled, using block bytes scanned
72
71
TEST_UTIL .getConfiguration ().setBoolean (QuotaUtil .QUOTA_CONF_KEY , true );
73
72
TEST_UTIL .getConfiguration ().setInt (QuotaCache .REFRESH_CONF_KEY , REFRESH_TIME );
74
- TEST_UTIL .getConfiguration ().setBoolean (OperationQuota .USE_BLOCK_BYTES_SCANNED_KEY , true );
75
73
76
74
// don't cache blocks to make IO predictable
77
75
TEST_UTIL .getConfiguration ().setFloat (HConstants .HFILE_BLOCK_CACHE_SIZE_KEY , 0.0f );
@@ -106,22 +104,22 @@ public void testBBSGet() throws Exception {
106
104
TEST_UTIL .flush (TABLE_NAME );
107
105
108
106
// Add ~10 block/min limit
109
- admin .setQuota (QuotaSettingsFactory .throttleUser (userName , ThrottleType .REQUEST_SIZE ,
107
+ admin .setQuota (QuotaSettingsFactory .throttleUser (userName , ThrottleType .READ_SIZE ,
110
108
Math .round (10.1 * blockSize ), TimeUnit .MINUTES ));
111
109
triggerUserCacheRefresh (TEST_UTIL , false , TABLE_NAME );
112
110
113
111
// should execute at max 10 requests
114
- assertEquals ( 10 , doGets (20 , FAMILY , QUALIFIER , table ));
112
+ testTraffic (() -> doGets (20 , FAMILY , QUALIFIER , table ), 10 , 1 );
115
113
116
114
// wait a minute and you should get another 10 requests executed
117
115
waitMinuteQuota ();
118
- assertEquals ( 10 , doGets (20 , FAMILY , QUALIFIER , table ));
116
+ testTraffic (() -> doGets (20 , FAMILY , QUALIFIER , table ), 10 , 1 );
119
117
120
118
// Remove all the limits
121
119
admin .setQuota (QuotaSettingsFactory .unthrottleUser (userName ));
122
120
triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
123
- assertEquals ( 100 , doGets (100 , FAMILY , QUALIFIER , table ));
124
- assertEquals ( 100 , doGets (100 , FAMILY , QUALIFIER , table ));
121
+ testTraffic (() -> doGets (100 , FAMILY , QUALIFIER , table ), 100 , 0 );
122
+ testTraffic (() -> doGets (100 , FAMILY , QUALIFIER , table ), 100 , 0 );
125
123
}
126
124
127
125
@ Test
@@ -142,30 +140,27 @@ public void testBBSScan() throws Exception {
142
140
waitMinuteQuota ();
143
141
144
142
// should execute 1 request
145
- assertEquals ( 1 , doScans (5 , table ));
143
+ testTraffic (() -> doScans (5 , table ), 1 , 0 );
146
144
147
145
// Remove all the limits
148
146
admin .setQuota (QuotaSettingsFactory .unthrottleUser (userName ));
149
147
triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
150
- assertEquals ( 100 , doScans (100 , table ));
151
- assertEquals ( 100 , doScans (100 , table ));
148
+ testTraffic (() -> doScans (100 , table ), 100 , 0 );
149
+ testTraffic (() -> doScans (100 , table ), 100 , 0 );
152
150
153
151
// Add ~3 block/min limit. This should support >1 scans
154
152
admin .setQuota (QuotaSettingsFactory .throttleUser (userName , ThrottleType .REQUEST_SIZE ,
155
153
Math .round (3.1 * blockSize ), TimeUnit .MINUTES ));
156
154
triggerUserCacheRefresh (TEST_UTIL , false , TABLE_NAME );
157
155
158
156
// should execute some requests, but not all
159
- long successfulScans = doScans (100 , table );
160
- LOG .info ("successfulScans = " + successfulScans );
161
- assertTrue (successfulScans < 100 );
162
- assertTrue (successfulScans > 0 );
157
+ testTraffic (() -> doScans (100 , table ), 100 , 90 );
163
158
164
159
// Remove all the limits
165
160
admin .setQuota (QuotaSettingsFactory .unthrottleUser (userName ));
166
161
triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
167
- assertEquals ( 100 , doScans (100 , table ));
168
- assertEquals ( 100 , doScans (100 , table ));
162
+ testTraffic (() -> doScans (100 , table ), 100 , 0 );
163
+ testTraffic (() -> doScans (100 , table ), 100 , 0 );
169
164
}
170
165
171
166
@ Test
@@ -187,34 +182,52 @@ public void testBBSMultiGet() throws Exception {
187
182
waitMinuteQuota ();
188
183
189
184
// should execute 1 request
190
- assertEquals ( 1 , doMultiGets (10 , 10 , rowCount , FAMILY , QUALIFIER , table ));
185
+ testTraffic (() -> doMultiGets (10 , 10 , rowCount , FAMILY , QUALIFIER , table ), 1 , 1 );
191
186
192
187
// Remove all the limits
193
188
admin .setQuota (QuotaSettingsFactory .unthrottleUser (userName ));
194
189
triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
195
- assertEquals ( 100 , doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ));
196
- assertEquals ( 100 , doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ));
190
+ testTraffic (() -> doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ), 100 , 0 );
191
+ testTraffic (() -> doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ), 100 , 0 );
197
192
198
193
// Add ~100 block/min limit
199
194
admin .setQuota (QuotaSettingsFactory .throttleUser (userName , ThrottleType .REQUEST_SIZE ,
200
195
Math .round (100.1 * blockSize ), TimeUnit .MINUTES ));
201
196
triggerUserCacheRefresh (TEST_UTIL , false , TABLE_NAME );
202
197
203
198
// should execute approximately 10 batches of 10 requests
204
- long successfulMultiGets = doMultiGets (20 , 10 , rowCount , FAMILY , QUALIFIER , table );
205
- assertTrue (successfulMultiGets >= 9 );
206
- assertTrue (successfulMultiGets <= 11 );
199
+ testTraffic (() -> doMultiGets (20 , 10 , rowCount , FAMILY , QUALIFIER , table ), 10 , 1 );
207
200
208
201
// wait a minute and you should get another ~10 batches of 10 requests
209
202
waitMinuteQuota ();
210
- successfulMultiGets = doMultiGets (20 , 10 , rowCount , FAMILY , QUALIFIER , table );
211
- assertTrue (successfulMultiGets >= 9 );
212
- assertTrue (successfulMultiGets <= 11 );
203
+ testTraffic (() -> doMultiGets (20 , 10 , rowCount , FAMILY , QUALIFIER , table ), 10 , 1 );
213
204
214
205
// Remove all the limits
215
206
admin .setQuota (QuotaSettingsFactory .unthrottleUser (userName ));
216
207
triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
217
- assertEquals (100 , doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ));
218
- assertEquals (100 , doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ));
208
+ testTraffic (() -> doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ), 100 , 0 );
209
+ testTraffic (() -> doMultiGets (100 , 10 , rowCount , FAMILY , QUALIFIER , table ), 100 , 0 );
210
+ }
211
+
212
+ private void testTraffic (Callable <Long > trafficCallable , long expectedSuccess , long marginOfError )
213
+ throws Exception {
214
+ TEST_UTIL .waitFor (90_000 , () -> {
215
+ long actualSuccess ;
216
+ try {
217
+ actualSuccess = trafficCallable .call ();
218
+ } catch (Exception e ) {
219
+ throw new RuntimeException (e );
220
+ }
221
+ LOG .info ("Traffic test yielded {} successful requests. Expected {} +/- {}" , actualSuccess ,
222
+ expectedSuccess , marginOfError );
223
+ boolean success = (actualSuccess >= expectedSuccess - marginOfError )
224
+ && (actualSuccess <= expectedSuccess + marginOfError );
225
+ if (!success ) {
226
+ triggerUserCacheRefresh (TEST_UTIL , true , TABLE_NAME );
227
+ waitMinuteQuota ();
228
+ Thread .sleep (15_000L );
229
+ }
230
+ return success ;
231
+ });
219
232
}
220
233
}
0 commit comments