39
39
import org .elasticsearch .cluster .routing .RoutingTable ;
40
40
import org .elasticsearch .cluster .routing .ShardRoutingState ;
41
41
import org .elasticsearch .cluster .routing .UnassignedInfo ;
42
+ import org .elasticsearch .common .CheckedConsumer ;
42
43
import org .elasticsearch .common .Priority ;
43
44
import org .elasticsearch .common .settings .Settings ;
44
45
import org .elasticsearch .common .xcontent .XContentFactory ;
55
56
56
57
import java .io .IOException ;
57
58
import java .util .List ;
59
+ import java .util .Map ;
58
60
import java .util .concurrent .TimeUnit ;
61
+ import java .util .function .Function ;
62
+ import java .util .stream .Collectors ;
63
+ import java .util .stream .Stream ;
59
64
60
65
import static org .elasticsearch .action .support .WriteRequest .RefreshPolicy .IMMEDIATE ;
61
66
import static org .elasticsearch .index .query .QueryBuilders .matchAllQuery ;
@@ -369,14 +374,7 @@ public void testRecoverBrokenIndexMetadata() throws Exception {
369
374
// this one is not validated ahead of time and breaks allocation
370
375
.put ("index.analysis.filter.myCollator.type" , "icu_collation" )
371
376
).build ();
372
- internalCluster ().fullRestart (new RestartCallback (){
373
- @ Override
374
- public Settings onNodeStopped (String nodeName ) throws Exception {
375
- final MetaStateService metaStateService = internalCluster ().getInstance (MetaStateService .class , nodeName );
376
- metaStateService .writeIndexAndUpdateManifest ("broken metadata" , brokenMeta );
377
- return super .onNodeStopped (nodeName );
378
- }
379
- });
377
+ writeBrokenMeta (metaStateService -> metaStateService .writeIndexAndUpdateManifest ("broken metadata" , brokenMeta ));
380
378
381
379
// check that the cluster does not keep reallocating shards
382
380
assertBusy (() -> {
@@ -443,14 +441,7 @@ public void testRecoverMissingAnalyzer() throws Exception {
443
441
final IndexMetaData metaData = state .getMetaData ().index ("test" );
444
442
final IndexMetaData brokenMeta = IndexMetaData .builder (metaData ).settings (metaData .getSettings ()
445
443
.filter ((s ) -> "index.analysis.analyzer.test.tokenizer" .equals (s ) == false )).build ();
446
- internalCluster ().fullRestart (new RestartCallback (){
447
- @ Override
448
- public Settings onNodeStopped (String nodeName ) throws Exception {
449
- final MetaStateService metaStateService = internalCluster ().getInstance (MetaStateService .class , nodeName );
450
- metaStateService .writeIndexAndUpdateManifest ("broken metadata" , brokenMeta );
451
- return super .onNodeStopped (nodeName );
452
- }
453
- });
444
+ writeBrokenMeta (metaStateService -> metaStateService .writeIndexAndUpdateManifest ("broken metadata" , brokenMeta ));
454
445
455
446
// check that the cluster does not keep reallocating shards
456
447
assertBusy (() -> {
@@ -495,14 +486,7 @@ public void testArchiveBrokenClusterSettings() throws Exception {
495
486
final MetaData brokenMeta = MetaData .builder (metaData ).persistentSettings (Settings .builder ()
496
487
.put (metaData .persistentSettings ()).put ("this.is.unknown" , true )
497
488
.put (MetaData .SETTING_CLUSTER_MAX_SHARDS_PER_NODE .getKey (), "broken" ).build ()).build ();
498
- internalCluster ().fullRestart (new RestartCallback (){
499
- @ Override
500
- public Settings onNodeStopped (String nodeName ) throws Exception {
501
- final MetaStateService metaStateService = internalCluster ().getInstance (MetaStateService .class , nodeName );
502
- metaStateService .writeGlobalStateAndUpdateManifest ("broken metadata" , brokenMeta );
503
- return super .onNodeStopped (nodeName );
504
- }
505
- });
489
+ writeBrokenMeta (metaStateService -> metaStateService .writeGlobalStateAndUpdateManifest ("broken metadata" , brokenMeta ));
506
490
507
491
ensureYellow ("test" ); // wait for state recovery
508
492
state = client ().admin ().cluster ().prepareState ().get ().getState ();
@@ -519,4 +503,17 @@ public Settings onNodeStopped(String nodeName) throws Exception {
519
503
+ MetaData .SETTING_CLUSTER_MAX_SHARDS_PER_NODE .getKey ()));
520
504
assertHitCount (client ().prepareSearch ().setQuery (matchAllQuery ()).get (), 1L );
521
505
}
506
+
507
+ private void writeBrokenMeta (CheckedConsumer <MetaStateService , IOException > writer ) throws Exception {
508
+ Map <String , MetaStateService > metaStateServices = Stream .of (internalCluster ().getNodeNames ())
509
+ .collect (Collectors .toMap (Function .identity (), nodeName -> internalCluster ().getInstance (MetaStateService .class , nodeName )));
510
+ internalCluster ().fullRestart (new RestartCallback (){
511
+ @ Override
512
+ public Settings onNodeStopped (String nodeName ) throws Exception {
513
+ final MetaStateService metaStateService = metaStateServices .get (nodeName );
514
+ writer .accept (metaStateService );
515
+ return super .onNodeStopped (nodeName );
516
+ }
517
+ });
518
+ }
522
519
}
0 commit comments