15
15
import org .elasticsearch .index .shard .ShardId ;
16
16
17
17
import java .util .ArrayList ;
18
- import java .util .Collection ;
19
18
import java .util .Collections ;
20
19
import java .util .HashSet ;
21
20
import java .util .Iterator ;
26
25
import java .util .Objects ;
27
26
import java .util .Set ;
28
27
import java .util .function .Predicate ;
29
- import java .util .stream .Collectors ;
30
28
31
29
/**
32
30
* A {@link RoutingNode} represents a cluster node associated with a single {@link DiscoveryNode} including all shards
@@ -118,7 +116,14 @@ public int size() {
118
116
* @param shard Shard to create on this Node
119
117
*/
120
118
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 ) {
122
127
final ShardRouting existing = shards .putIfAbsent (shard .shardId (), shard );
123
128
if (existing != null ) {
124
129
final IllegalStateException e = new IllegalStateException (
@@ -142,11 +147,10 @@ void add(ShardRouting shard) {
142
147
relocatingShards .add (shard );
143
148
}
144
149
shardsByIndex .computeIfAbsent (shard .index (), k -> new HashSet <>()).add (shard );
145
- assert invariant ();
150
+ assert validate == false || invariant ();
146
151
}
147
152
148
153
void update (ShardRouting oldShard , ShardRouting newShard ) {
149
- assert invariant ();
150
154
if (shards .containsKey (oldShard .shardId ()) == false ) {
151
155
// Shard was already removed by routing nodes iterator
152
156
// TODO: change caller logic in RoutingNodes so that this check can go away
@@ -174,7 +178,6 @@ void update(ShardRouting oldShard, ShardRouting newShard) {
174
178
}
175
179
176
180
void remove (ShardRouting shard ) {
177
- assert invariant ();
178
181
ShardRouting previousValue = shards .remove (shard .shardId ());
179
182
assert previousValue == shard : "expected shard " + previousValue + " but was " + shard ;
180
183
if (shard .initializing ()) {
@@ -342,20 +345,24 @@ public boolean isEmpty() {
342
345
return shards .isEmpty ();
343
346
}
344
347
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 () );
350
353
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
+ }
355
363
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 );
359
366
assert shardRoutingsByIndex .equals (shardsByIndex );
360
367
361
368
return true ;
0 commit comments