1515import org .elasticsearch .index .shard .ShardId ;
1616
1717import java .util .ArrayList ;
18- import java .util .Collection ;
1918import java .util .Collections ;
2019import java .util .HashSet ;
2120import java .util .Iterator ;
2625import java .util .Objects ;
2726import java .util .Set ;
2827import java .util .function .Predicate ;
29- import java .util .stream .Collectors ;
3028
3129/**
3230 * A {@link RoutingNode} represents a cluster node associated with a single {@link DiscoveryNode} including all shards
@@ -118,7 +116,14 @@ public int size() {
118116 * @param shard Shard to create on this Node
119117 */
120118 void add (ShardRouting shard ) {
121- assert invariant ();
119+ addInternal (shard , true );
120+ }
121+
122+ void addWithoutValidation (ShardRouting shard ) {
123+ addInternal (shard , false );
124+ }
125+
126+ private void addInternal (ShardRouting shard , boolean validate ) {
122127 final ShardRouting existing = shards .putIfAbsent (shard .shardId (), shard );
123128 if (existing != null ) {
124129 final IllegalStateException e = new IllegalStateException (
@@ -142,11 +147,10 @@ void add(ShardRouting shard) {
142147 relocatingShards .add (shard );
143148 }
144149 shardsByIndex .computeIfAbsent (shard .index (), k -> new HashSet <>()).add (shard );
145- assert invariant ();
150+ assert validate == false || invariant ();
146151 }
147152
148153 void update (ShardRouting oldShard , ShardRouting newShard ) {
149- assert invariant ();
150154 if (shards .containsKey (oldShard .shardId ()) == false ) {
151155 // Shard was already removed by routing nodes iterator
152156 // TODO: change caller logic in RoutingNodes so that this check can go away
@@ -174,7 +178,6 @@ void update(ShardRouting oldShard, ShardRouting newShard) {
174178 }
175179
176180 void remove (ShardRouting shard ) {
177- assert invariant ();
178181 ShardRouting previousValue = shards .remove (shard .shardId ());
179182 assert previousValue == shard : "expected shard " + previousValue + " but was " + shard ;
180183 if (shard .initializing ()) {
@@ -342,20 +345,24 @@ public boolean isEmpty() {
342345 return shards .isEmpty ();
343346 }
344347
345- private boolean invariant () {
346- // initializingShards must consistent with that in shards
347- Collection < ShardRouting > shardRoutingsInitializing = shards .values (). stream (). filter ( ShardRouting :: initializing ). toList ( );
348- assert initializingShards . size () == shardRoutingsInitializing . size ();
349- assert initializingShards . containsAll ( shardRoutingsInitializing );
348+ boolean invariant () {
349+ var shardRoutingsInitializing = new ArrayList < ShardRouting >( shards . size ());
350+ var shardRoutingsRelocating = new ArrayList < ShardRouting >( shards .size () );
351+ // this guess assumes 1 shard per index, this is not precise, but okay for assertion
352+ var shardRoutingsByIndex = Maps .< Index , Set < ShardRouting >> newHashMapWithExpectedSize ( shards . size () );
350353
351- // relocatingShards must consistent with that in shards
352- Collection <ShardRouting > shardRoutingsRelocating = shards .values ().stream ().filter (ShardRouting ::relocating ).toList ();
353- assert relocatingShards .size () == shardRoutingsRelocating .size ();
354- assert relocatingShards .containsAll (shardRoutingsRelocating );
354+ for (var shard : shards .values ()) {
355+ if (shard .initializing ()) {
356+ shardRoutingsInitializing .add (shard );
357+ }
358+ if (shard .relocating ()) {
359+ shardRoutingsRelocating .add (shard );
360+ }
361+ shardRoutingsByIndex .computeIfAbsent (shard .index (), k -> new HashSet <>(10 )).add (shard );
362+ }
355363
356- final Map <Index , Set <ShardRouting >> shardRoutingsByIndex = shards .values ()
357- .stream ()
358- .collect (Collectors .groupingBy (ShardRouting ::index , Collectors .toSet ()));
364+ assert initializingShards .size () == shardRoutingsInitializing .size () && initializingShards .containsAll (shardRoutingsInitializing );
365+ assert relocatingShards .size () == shardRoutingsRelocating .size () && relocatingShards .containsAll (shardRoutingsRelocating );
359366 assert shardRoutingsByIndex .equals (shardsByIndex );
360367
361368 return true ;
0 commit comments