30
30
import java .util .ArrayList ;
31
31
import java .util .Collections ;
32
32
import java .util .List ;
33
+ import java .util .Optional ;
34
+ import java .util .concurrent .CountDownLatch ;
33
35
import java .util .concurrent .TimeUnit ;
34
36
35
37
import static org .opensearch .test .ClusterServiceUtils .setState ;
@@ -69,7 +71,7 @@ public static void afterClass() {
69
71
public void testDefaults () {
70
72
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
71
73
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
72
- return clusterService .getMasterService ().getMinNodeVersion ();
74
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
73
75
}, new ClusterManagerThrottlingStats ());
74
76
throttler .registerClusterManagerTask ("put-mapping" , true );
75
77
throttler .registerClusterManagerTask ("create-index" , true );
@@ -108,7 +110,7 @@ public void testValidateSettingsForDifferentVersion() {
108
110
}
109
111
}
110
112
111
- public void testValidateSettingsForTaskWihtoutRetryOnDataNode () {
113
+ public void testValidateSettingsForTaskWithoutRetryOnDataNode () {
112
114
DiscoveryNode clusterManagerNode = getClusterManagerNode (Version .V_2_5_0 );
113
115
DiscoveryNode dataNode = getDataNode (Version .V_2_5_0 );
114
116
setState (
@@ -139,7 +141,7 @@ public void testUpdateSettingsForNullValue() {
139
141
140
142
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
141
143
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
142
- return clusterService .getMasterService ().getMinNodeVersion ();
144
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
143
145
}, new ClusterManagerThrottlingStats ());
144
146
throttler .registerClusterManagerTask ("put-mapping" , true );
145
147
@@ -173,7 +175,7 @@ public void testSettingsOnBootstrap() {
173
175
.put ("cluster_manager.throttling.retry.max.delay" , maxDelay + "s" )
174
176
.build ();
175
177
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (initialSettings , clusterSettings , () -> {
176
- return clusterService .getMasterService ().getMinNodeVersion ();
178
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
177
179
}, new ClusterManagerThrottlingStats ());
178
180
throttler .registerClusterManagerTask ("put-mapping" , true );
179
181
@@ -187,7 +189,7 @@ public void testSettingsOnBootstrap() {
187
189
public void testUpdateRetryDelaySetting () {
188
190
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
189
191
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
190
- return clusterService .getMasterService ().getMinNodeVersion ();
192
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
191
193
}, new ClusterManagerThrottlingStats ());
192
194
193
195
// verify defaults
@@ -217,7 +219,7 @@ public void testValidateSettingsForUnknownTask() {
217
219
218
220
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
219
221
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
220
- return clusterService .getMasterService ().getMinNodeVersion ();
222
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
221
223
}, new ClusterManagerThrottlingStats ());
222
224
223
225
// set some limit for update snapshot tasks
@@ -236,7 +238,7 @@ public void testUpdateThrottlingLimitForBasicSanity() {
236
238
237
239
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
238
240
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
239
- return clusterService .getMasterService ().getMinNodeVersion ();
241
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
240
242
}, new ClusterManagerThrottlingStats ());
241
243
throttler .registerClusterManagerTask ("put-mapping" , true );
242
244
@@ -263,7 +265,7 @@ public void testValidateSettingForLimit() {
263
265
264
266
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
265
267
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
266
- return clusterService .getMasterService ().getMinNodeVersion ();
268
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
267
269
}, new ClusterManagerThrottlingStats ());
268
270
throttler .registerClusterManagerTask ("put-mapping" , true );
269
271
@@ -274,7 +276,7 @@ public void testValidateSettingForLimit() {
274
276
public void testUpdateLimit () {
275
277
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
276
278
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
277
- return clusterService .getMasterService ().getMinNodeVersion ();
279
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
278
280
}, new ClusterManagerThrottlingStats ());
279
281
throttler .registerClusterManagerTask ("put-mapping" , true );
280
282
@@ -309,7 +311,7 @@ public void testThrottlingForDisabledThrottlingTask() {
309
311
String taskKey = "test" ;
310
312
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
311
313
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
312
- return clusterService .getMasterService ().getMinNodeVersion ();
314
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
313
315
}, throttlingStats );
314
316
ClusterManagerTaskThrottler .ThrottlingKey throttlingKey = throttler .registerClusterManagerTask (taskKey , false );
315
317
@@ -321,6 +323,9 @@ public void testThrottlingForDisabledThrottlingTask() {
321
323
322
324
// Asserting that there was not any throttling for it
323
325
assertEquals (0L , throttlingStats .getThrottlingCount (taskKey ));
326
+
327
+ // Asserting value in tasksCount map to make sure it gets updated even when throttling is disabled
328
+ assertEquals (Optional .of (10L ).get (), throttler .tasksCount .get (taskKey ));
324
329
}
325
330
326
331
public void testThrottlingForInitialStaticSettingAndVersionCheck () {
@@ -339,7 +344,7 @@ public void testThrottlingForInitialStaticSettingAndVersionCheck() {
339
344
.put ("cluster_manager.throttling.thresholds.put-mapping.value" , put_mapping_threshold_value )
340
345
.build ();
341
346
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (initialSettings , clusterSettings , () -> {
342
- return clusterService .getMasterService ().getMinNodeVersion ();
347
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
343
348
}, throttlingStats );
344
349
ClusterManagerTaskThrottler .ThrottlingKey throttlingKey = throttler .registerClusterManagerTask ("put-mapping" , true );
345
350
@@ -367,7 +372,7 @@ public void testThrottling() {
367
372
String taskKey = "test" ;
368
373
ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
369
374
ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
370
- return clusterService .getMasterService ().getMinNodeVersion ();
375
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
371
376
}, throttlingStats );
372
377
ClusterManagerTaskThrottler .ThrottlingKey throttlingKey = throttler .registerClusterManagerTask (taskKey , true );
373
378
@@ -406,6 +411,58 @@ public void testThrottling() {
406
411
throttler .onBeginSubmit (getMockUpdateTaskList (taskKey , throttlingKey , 1 ));
407
412
}
408
413
414
+ public void testThrottlingWithLock () {
415
+ ClusterManagerThrottlingStats throttlingStats = new ClusterManagerThrottlingStats ();
416
+ String taskKey = "test" ;
417
+ ClusterSettings clusterSettings = new ClusterSettings (Settings .builder ().build (), ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
418
+ ClusterManagerTaskThrottler throttler = new ClusterManagerTaskThrottler (Settings .EMPTY , clusterSettings , () -> {
419
+ return clusterService .getClusterManagerService ().getMinNodeVersion ();
420
+ }, throttlingStats );
421
+ ClusterManagerTaskThrottler .ThrottlingKey throttlingKey = throttler .registerClusterManagerTask (taskKey , true );
422
+
423
+ throttler .updateLimit (taskKey , 5 );
424
+
425
+ // adding 3 tasks
426
+ throttler .onBeginSubmit (getMockUpdateTaskList (taskKey , throttlingKey , 3 ));
427
+
428
+ // adding 3 more tasks, these tasks should be throttled
429
+ // taskCount in Queue: 3 Threshold: 5
430
+ assertThrows (
431
+ ClusterManagerThrottlingException .class ,
432
+ () -> throttler .onBeginSubmit (getMockUpdateTaskList (taskKey , throttlingKey , 3 ))
433
+ );
434
+ assertEquals (3L , throttlingStats .getThrottlingCount (taskKey ));
435
+
436
+ // remove one task
437
+ throttler .onBeginProcessing (getMockUpdateTaskList (taskKey , throttlingKey , 1 ));
438
+
439
+ // add 3 tasks should pass now.
440
+ // taskCount in Queue: 2 Threshold: 5
441
+ throttler .onBeginSubmit (getMockUpdateTaskList (taskKey , throttlingKey , 3 ));
442
+
443
+ final CountDownLatch latch = new CountDownLatch (1 );
444
+
445
+ // Taking lock on tasksCount will not impact throttling behaviour now.
446
+ new Thread (() -> {
447
+ throttler .tasksCount .computeIfPresent (taskKey , (key , count ) -> {
448
+ try {
449
+ latch .await ();
450
+ } catch (InterruptedException e ) {
451
+ throw new RuntimeException (e );
452
+ }
453
+ return 10L ;
454
+ });
455
+ }).start ();
456
+
457
+ // adding one task will throttle
458
+ // taskCount in Queue: 5 Threshold: 5
459
+ assertThrows (
460
+ ClusterManagerThrottlingException .class ,
461
+ () -> throttler .onBeginSubmit (getMockUpdateTaskList (taskKey , throttlingKey , 1 ))
462
+ );
463
+ latch .countDown ();
464
+ }
465
+
409
466
private List <TaskBatcherTests .TestTaskBatcher .UpdateTask > getMockUpdateTaskList (
410
467
String taskKey ,
411
468
ClusterManagerTaskThrottler .ThrottlingKey throttlingKey ,
0 commit comments