50
50
import org .opensearch .cluster .routing .allocation .decider .Decision .Type ;
51
51
import org .opensearch .env .ShardLockObtainFailedException ;
52
52
import org .opensearch .gateway .AsyncShardFetch .FetchResult ;
53
+ import org .opensearch .gateway .TransportNodesGatewayStartedShardHelper .NodeGatewayStartedShard ;
53
54
import org .opensearch .gateway .TransportNodesListGatewayStartedShards .NodeGatewayStartedShards ;
54
55
55
56
import java .util .ArrayList ;
@@ -125,27 +126,37 @@ public AllocateUnassignedDecision makeAllocationDecision(
125
126
return decision ;
126
127
}
127
128
final FetchResult <NodeGatewayStartedShards > shardState = fetchData (unassignedShard , allocation );
128
- List <NodeGatewayStartedShards > nodeShardStates = adaptToNodeStartedShardList (shardState );
129
+ List <NodeGatewayStartedShard > nodeShardStates = adaptToNodeStartedShardList (shardState );
129
130
return getAllocationDecision (unassignedShard , allocation , nodeShardStates , logger );
130
131
}
131
132
132
133
/**
133
- * Transforms {@link FetchResult} of {@link NodeGatewayStartedShards} to {@link List} of {@link NodeGatewayStartedShards }
134
+ * Transforms {@link FetchResult} of {@link NodeGatewayStartedShards} to {@link List} of {@link NodeGatewayStartedShard }
134
135
* Returns null if {@link FetchResult} does not have any data.
135
136
*/
136
- private static List <NodeGatewayStartedShards > adaptToNodeStartedShardList (FetchResult <NodeGatewayStartedShards > shardsState ) {
137
+ private static List <NodeGatewayStartedShard > adaptToNodeStartedShardList (FetchResult <NodeGatewayStartedShards > shardsState ) {
137
138
if (!shardsState .hasData ()) {
138
139
return null ;
139
140
}
140
- List <NodeGatewayStartedShards > nodeShardStates = new ArrayList <>();
141
- shardsState .getData ().forEach ((node , nodeGatewayStartedShard ) -> { nodeShardStates .add (nodeGatewayStartedShard ); });
141
+ List <NodeGatewayStartedShard > nodeShardStates = new ArrayList <>();
142
+ shardsState .getData ().forEach ((node , nodeGatewayStartedShard ) -> {
143
+ nodeShardStates .add (
144
+ new NodeGatewayStartedShard (
145
+ nodeGatewayStartedShard .getGatewayShardStarted ().allocationId (),
146
+ nodeGatewayStartedShard .getGatewayShardStarted ().primary (),
147
+ nodeGatewayStartedShard .getGatewayShardStarted ().replicationCheckpoint (),
148
+ nodeGatewayStartedShard .getGatewayShardStarted ().storeException (),
149
+ node
150
+ )
151
+ );
152
+ });
142
153
return nodeShardStates ;
143
154
}
144
155
145
156
protected AllocateUnassignedDecision getAllocationDecision (
146
157
ShardRouting unassignedShard ,
147
158
RoutingAllocation allocation ,
148
- List <NodeGatewayStartedShards > shardState ,
159
+ List <NodeGatewayStartedShard > shardState ,
149
160
Logger logger
150
161
) {
151
162
final boolean explain = allocation .debugDecision ();
@@ -236,7 +247,7 @@ protected AllocateUnassignedDecision getAllocationDecision(
236
247
nodesToAllocate = buildNodesToAllocate (allocation , nodeShardsResult .orderedAllocationCandidates , unassignedShard , true );
237
248
if (nodesToAllocate .yesNodeShards .isEmpty () == false ) {
238
249
final DecidedNode decidedNode = nodesToAllocate .yesNodeShards .get (0 );
239
- final NodeGatewayStartedShards nodeShardState = decidedNode .nodeShardState ;
250
+ final NodeGatewayStartedShard nodeShardState = decidedNode .nodeShardState ;
240
251
logger .debug (
241
252
"[{}][{}]: allocating [{}] to [{}] on forced primary allocation" ,
242
253
unassignedShard .index (),
@@ -296,11 +307,11 @@ protected AllocateUnassignedDecision getAllocationDecision(
296
307
*/
297
308
private static List <NodeAllocationResult > buildNodeDecisions (
298
309
NodesToAllocate nodesToAllocate ,
299
- List <NodeGatewayStartedShards > fetchedShardData ,
310
+ List <NodeGatewayStartedShard > fetchedShardData ,
300
311
Set <String > inSyncAllocationIds
301
312
) {
302
313
List <NodeAllocationResult > nodeResults = new ArrayList <>();
303
- Collection <NodeGatewayStartedShards > ineligibleShards = new ArrayList <>();
314
+ Collection <NodeGatewayStartedShard > ineligibleShards = new ArrayList <>();
304
315
if (nodesToAllocate != null ) {
305
316
final Set <DiscoveryNode > discoNodes = new HashSet <>();
306
317
nodeResults .addAll (
@@ -334,21 +345,21 @@ private static List<NodeAllocationResult> buildNodeDecisions(
334
345
return nodeResults ;
335
346
}
336
347
337
- private static ShardStoreInfo shardStoreInfo (NodeGatewayStartedShards nodeShardState , Set <String > inSyncAllocationIds ) {
348
+ private static ShardStoreInfo shardStoreInfo (NodeGatewayStartedShard nodeShardState , Set <String > inSyncAllocationIds ) {
338
349
final Exception storeErr = nodeShardState .storeException ();
339
350
final boolean inSync = nodeShardState .allocationId () != null && inSyncAllocationIds .contains (nodeShardState .allocationId ());
340
351
return new ShardStoreInfo (nodeShardState .allocationId (), inSync , storeErr );
341
352
}
342
353
343
- private static final Comparator <NodeGatewayStartedShards > NO_STORE_EXCEPTION_FIRST_COMPARATOR = Comparator .comparing (
344
- (NodeGatewayStartedShards state ) -> state .storeException () == null
354
+ private static final Comparator <NodeGatewayStartedShard > NO_STORE_EXCEPTION_FIRST_COMPARATOR = Comparator .comparing (
355
+ (NodeGatewayStartedShard state ) -> state .storeException () == null
345
356
).reversed ();
346
- private static final Comparator <NodeGatewayStartedShards > PRIMARY_FIRST_COMPARATOR = Comparator .comparing (
347
- NodeGatewayStartedShards ::primary
357
+ private static final Comparator <NodeGatewayStartedShard > PRIMARY_FIRST_COMPARATOR = Comparator .comparing (
358
+ NodeGatewayStartedShard ::primary
348
359
).reversed ();
349
360
350
- private static final Comparator <NodeGatewayStartedShards > HIGHEST_REPLICATION_CHECKPOINT_FIRST_COMPARATOR = Comparator .comparing (
351
- NodeGatewayStartedShards ::replicationCheckpoint ,
361
+ private static final Comparator <NodeGatewayStartedShard > HIGHEST_REPLICATION_CHECKPOINT_FIRST_COMPARATOR = Comparator .comparing (
362
+ NodeGatewayStartedShard ::replicationCheckpoint ,
352
363
Comparator .nullsLast (Comparator .naturalOrder ())
353
364
);
354
365
@@ -362,12 +373,12 @@ protected static NodeShardsResult buildNodeShardsResult(
362
373
boolean matchAnyShard ,
363
374
Set <String > ignoreNodes ,
364
375
Set <String > inSyncAllocationIds ,
365
- List <NodeGatewayStartedShards > shardState ,
376
+ List <NodeGatewayStartedShard > shardState ,
366
377
Logger logger
367
378
) {
368
- List <NodeGatewayStartedShards > nodeShardStates = new ArrayList <>();
379
+ List <NodeGatewayStartedShard > nodeShardStates = new ArrayList <>();
369
380
int numberOfAllocationsFound = 0 ;
370
- for (NodeGatewayStartedShards nodeShardState : shardState ) {
381
+ for (NodeGatewayStartedShard nodeShardState : shardState ) {
371
382
DiscoveryNode node = nodeShardState .getNode ();
372
383
String allocationId = nodeShardState .allocationId ();
373
384
@@ -432,21 +443,18 @@ protected static NodeShardsResult buildNodeShardsResult(
432
443
return new NodeShardsResult (nodeShardStates , numberOfAllocationsFound );
433
444
}
434
445
435
- private static Comparator <NodeGatewayStartedShards > createActiveShardComparator (
436
- boolean matchAnyShard ,
437
- Set <String > inSyncAllocationIds
438
- ) {
446
+ private static Comparator <NodeGatewayStartedShard > createActiveShardComparator (boolean matchAnyShard , Set <String > inSyncAllocationIds ) {
439
447
/**
440
448
* Orders the active shards copies based on below comparators
441
449
* 1. No store exception i.e. shard copy is readable
442
450
* 2. Prefer previous primary shard
443
451
* 3. Prefer shard copy with the highest replication checkpoint. It is NO-OP for doc rep enabled indices.
444
452
*/
445
- final Comparator <NodeGatewayStartedShards > comparator ; // allocation preference
453
+ final Comparator <NodeGatewayStartedShard > comparator ; // allocation preference
446
454
if (matchAnyShard ) {
447
455
// prefer shards with matching allocation ids
448
- Comparator <NodeGatewayStartedShards > matchingAllocationsFirst = Comparator .comparing (
449
- (NodeGatewayStartedShards state ) -> inSyncAllocationIds .contains (state .allocationId ())
456
+ Comparator <NodeGatewayStartedShard > matchingAllocationsFirst = Comparator .comparing (
457
+ (NodeGatewayStartedShard state ) -> inSyncAllocationIds .contains (state .allocationId ())
450
458
).reversed ();
451
459
comparator = matchingAllocationsFirst .thenComparing (NO_STORE_EXCEPTION_FIRST_COMPARATOR )
452
460
.thenComparing (PRIMARY_FIRST_COMPARATOR )
@@ -464,14 +472,14 @@ private static Comparator<NodeGatewayStartedShards> createActiveShardComparator(
464
472
*/
465
473
private static NodesToAllocate buildNodesToAllocate (
466
474
RoutingAllocation allocation ,
467
- List <NodeGatewayStartedShards > nodeShardStates ,
475
+ List <NodeGatewayStartedShard > nodeShardStates ,
468
476
ShardRouting shardRouting ,
469
477
boolean forceAllocate
470
478
) {
471
479
List <DecidedNode > yesNodeShards = new ArrayList <>();
472
480
List <DecidedNode > throttledNodeShards = new ArrayList <>();
473
481
List <DecidedNode > noNodeShards = new ArrayList <>();
474
- for (NodeGatewayStartedShards nodeShardState : nodeShardStates ) {
482
+ for (NodeGatewayStartedShard nodeShardState : nodeShardStates ) {
475
483
RoutingNode node = allocation .routingNodes ().node (nodeShardState .getNode ().getId ());
476
484
if (node == null ) {
477
485
continue ;
@@ -502,10 +510,10 @@ private static NodesToAllocate buildNodesToAllocate(
502
510
* This class encapsulates the result of a call to {@link #buildNodeShardsResult}
503
511
*/
504
512
static class NodeShardsResult {
505
- final List <NodeGatewayStartedShards > orderedAllocationCandidates ;
513
+ final List <NodeGatewayStartedShard > orderedAllocationCandidates ;
506
514
final int allocationsFound ;
507
515
508
- NodeShardsResult (List <NodeGatewayStartedShards > orderedAllocationCandidates , int allocationsFound ) {
516
+ NodeShardsResult (List <NodeGatewayStartedShard > orderedAllocationCandidates , int allocationsFound ) {
509
517
this .orderedAllocationCandidates = orderedAllocationCandidates ;
510
518
this .allocationsFound = allocationsFound ;
511
519
}
@@ -531,10 +539,10 @@ protected static class NodesToAllocate {
531
539
* by the allocator for allocating to the node that holds the shard copy.
532
540
*/
533
541
private static class DecidedNode {
534
- final NodeGatewayStartedShards nodeShardState ;
542
+ final NodeGatewayStartedShard nodeShardState ;
535
543
final Decision decision ;
536
544
537
- private DecidedNode (NodeGatewayStartedShards nodeShardState , Decision decision ) {
545
+ private DecidedNode (NodeGatewayStartedShard nodeShardState , Decision decision ) {
538
546
this .nodeShardState = nodeShardState ;
539
547
this .decision = decision ;
540
548
}
0 commit comments