35
35
import org .elasticsearch .cluster .routing .allocation .decider .ThrottlingAllocationDecider ;
36
36
import org .elasticsearch .common .Priority ;
37
37
import org .elasticsearch .common .io .FileSystemUtils ;
38
- import org .elasticsearch .common .logging .Loggers ;
39
38
import org .elasticsearch .common .settings .Settings ;
40
39
import org .elasticsearch .core .IOUtils ;
41
40
import org .elasticsearch .core .TimeValue ;
@@ -402,7 +401,7 @@ public void testRerouteExplain() {
402
401
assertThat (explanation .decisions ().type (), equalTo (Decision .Type .YES ));
403
402
}
404
403
405
- public void testMessageLogging () throws Exception {
404
+ public void testMessageLogging () {
406
405
final Settings settings = Settings .builder ()
407
406
.put (EnableAllocationDecider .CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING .getKey (), Allocation .NONE .name ())
408
407
.put (EnableAllocationDecider .CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING .getKey (), EnableAllocationDecider .Rebalance .NONE .name ())
@@ -431,10 +430,7 @@ public void testMessageLogging() throws Exception {
431
430
.execute ()
432
431
.actionGet ();
433
432
434
- Logger actionLogger = LogManager .getLogger (TransportClusterRerouteAction .class );
435
-
436
433
MockLogAppender dryRunMockLog = new MockLogAppender ();
437
- dryRunMockLog .start ();
438
434
dryRunMockLog .addExpectation (
439
435
new MockLogAppender .UnseenEventExpectation (
440
436
"no completed message logged on dry run" ,
@@ -443,28 +439,26 @@ public void testMessageLogging() throws Exception {
443
439
"allocated an empty primary*"
444
440
)
445
441
);
446
- Loggers .addAppender (actionLogger , dryRunMockLog );
447
-
448
- AllocationCommand dryRunAllocation = new AllocateEmptyPrimaryAllocationCommand (indexName , 0 , nodeName1 , true );
449
- ClusterRerouteResponse dryRunResponse = client ().admin ()
450
- .cluster ()
451
- .prepareReroute ()
452
- .setExplain (randomBoolean ())
453
- .setDryRun (true )
454
- .add (dryRunAllocation )
455
- .execute ()
456
- .actionGet ();
457
-
458
- // during a dry run, messages exist but are not logged or exposed
459
- assertThat (dryRunResponse .getExplanations ().getYesDecisionMessages (), hasSize (1 ));
460
- assertThat (dryRunResponse .getExplanations ().getYesDecisionMessages ().get (0 ), containsString ("allocated an empty primary" ));
461
442
462
- dryRunMockLog .assertAllExpectationsMatched ();
463
- dryRunMockLog .stop ();
464
- Loggers .removeAppender (actionLogger , dryRunMockLog );
443
+ try (var ignored = dryRunMockLog .capturing (TransportClusterRerouteAction .class )) {
444
+ AllocationCommand dryRunAllocation = new AllocateEmptyPrimaryAllocationCommand (indexName , 0 , nodeName1 , true );
445
+ ClusterRerouteResponse dryRunResponse = client ().admin ()
446
+ .cluster ()
447
+ .prepareReroute ()
448
+ .setExplain (randomBoolean ())
449
+ .setDryRun (true )
450
+ .add (dryRunAllocation )
451
+ .execute ()
452
+ .actionGet ();
453
+
454
+ // during a dry run, messages exist but are not logged or exposed
455
+ assertThat (dryRunResponse .getExplanations ().getYesDecisionMessages (), hasSize (1 ));
456
+ assertThat (dryRunResponse .getExplanations ().getYesDecisionMessages ().get (0 ), containsString ("allocated an empty primary" ));
457
+
458
+ dryRunMockLog .assertAllExpectationsMatched ();
459
+ }
465
460
466
461
MockLogAppender allocateMockLog = new MockLogAppender ();
467
- allocateMockLog .start ();
468
462
allocateMockLog .addExpectation (
469
463
new MockLogAppender .SeenEventExpectation (
470
464
"message for first allocate empty primary" ,
@@ -481,26 +475,25 @@ public void testMessageLogging() throws Exception {
481
475
"allocated an empty primary*" + nodeName2 + "*"
482
476
)
483
477
);
484
- Loggers .addAppender (actionLogger , allocateMockLog );
485
-
486
- AllocationCommand yesDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand (indexName , 0 , nodeName1 , true );
487
- AllocationCommand noDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand ("noexist" , 1 , nodeName2 , true );
488
- ClusterRerouteResponse response = client ().admin ()
489
- .cluster ()
490
- .prepareReroute ()
491
- .setExplain (true ) // so we get a NO decision back rather than an exception
492
- .add (yesDecisionAllocation )
493
- .add (noDecisionAllocation )
494
- .execute ()
495
- .actionGet ();
496
-
497
- assertThat (response .getExplanations ().getYesDecisionMessages (), hasSize (1 ));
498
- assertThat (response .getExplanations ().getYesDecisionMessages ().get (0 ), containsString ("allocated an empty primary" ));
499
- assertThat (response .getExplanations ().getYesDecisionMessages ().get (0 ), containsString (nodeName1 ));
500
-
501
- allocateMockLog .assertAllExpectationsMatched ();
502
- allocateMockLog .stop ();
503
- Loggers .removeAppender (actionLogger , allocateMockLog );
478
+ try (var ignored = allocateMockLog .capturing (TransportClusterRerouteAction .class )) {
479
+
480
+ AllocationCommand yesDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand (indexName , 0 , nodeName1 , true );
481
+ AllocationCommand noDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand ("noexist" , 1 , nodeName2 , true );
482
+ ClusterRerouteResponse response = client ().admin ()
483
+ .cluster ()
484
+ .prepareReroute ()
485
+ .setExplain (true ) // so we get a NO decision back rather than an exception
486
+ .add (yesDecisionAllocation )
487
+ .add (noDecisionAllocation )
488
+ .execute ()
489
+ .actionGet ();
490
+
491
+ assertThat (response .getExplanations ().getYesDecisionMessages (), hasSize (1 ));
492
+ assertThat (response .getExplanations ().getYesDecisionMessages ().get (0 ), containsString ("allocated an empty primary" ));
493
+ assertThat (response .getExplanations ().getYesDecisionMessages ().get (0 ), containsString (nodeName1 ));
494
+
495
+ allocateMockLog .assertAllExpectationsMatched ();
496
+ }
504
497
}
505
498
506
499
public void testClusterRerouteWithBlocks () {
0 commit comments