Skip to content

Commit 1469ed2

Browse files
authored
Minor cleanup in IndicesClusterStateService (#94623)
Removes some unnecessary parameters from internal methods. Extracted from #94545 to reduce noise.
1 parent 56b849d commit 1469ed2

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,11 @@ private void removeIndicesAndShards(final ClusterChangedEvent event) {
427427
* @param state new cluster state
428428
*/
429429
private void createIndicesAndUpdateShards(final ClusterState state) {
430-
DiscoveryNodes nodes = state.nodes();
431-
RoutingNode localRoutingNode = state.getRoutingNodes().node(nodes.getLocalNodeId());
430+
RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
432431
if (localRoutingNode == null) {
433432
return;
434433
}
435434

436-
RoutingTable routingTable = state.routingTable();
437-
438435
// create map of indices to create with shards to fail if index creation fails or create or update shards if an existing index
439436
// service is found
440437
final Map<Index, List<ShardRouting>> indicesToCreate = new HashMap<>();
@@ -455,7 +452,7 @@ private void createIndicesAndUpdateShards(final ClusterState state) {
455452
if (indexService == null) {
456453
indicesToCreate.computeIfAbsent(index, k -> new ArrayList<>()).add(shardRouting);
457454
} else {
458-
createOrUpdateShard(state, nodes, routingTable, shardRouting, indexService);
455+
createOrUpdateShard(state, shardRouting, indexService);
459456
}
460457
}
461458
}
@@ -485,24 +482,18 @@ private void createIndicesAndUpdateShards(final ClusterState state) {
485482
}
486483
// we succeeded in creating the index service, so now we can create the missing shards assigned to this node
487484
for (ShardRouting shardRouting : entry.getValue()) {
488-
createOrUpdateShard(state, nodes, routingTable, shardRouting, indexService);
485+
createOrUpdateShard(state, shardRouting, indexService);
489486
}
490487
}
491488
}
492489

493-
private void createOrUpdateShard(
494-
ClusterState state,
495-
DiscoveryNodes nodes,
496-
RoutingTable routingTable,
497-
ShardRouting shardRouting,
498-
AllocatedIndex<? extends Shard> indexService
499-
) {
490+
private void createOrUpdateShard(ClusterState state, ShardRouting shardRouting, AllocatedIndex<? extends Shard> indexService) {
500491
Shard shard = indexService.getShardOrNull(shardRouting.shardId().id());
501492
if (shard == null) {
502493
assert shardRouting.initializing() : shardRouting + " should have been removed by failMissingShards";
503-
createShard(nodes, routingTable, shardRouting, state);
494+
createShard(shardRouting, state);
504495
} else {
505-
updateShard(nodes, shardRouting, shard, routingTable, state);
496+
updateShard(shardRouting, shard, state);
506497
}
507498
}
508499

@@ -546,19 +537,21 @@ private void updateIndices(ClusterChangedEvent event) {
546537
}
547538
}
548539

549-
private void createShard(DiscoveryNodes nodes, RoutingTable routingTable, ShardRouting shardRouting, ClusterState state) {
540+
private void createShard(ShardRouting shardRouting, ClusterState state) {
550541
assert shardRouting.initializing() : "only allow shard creation for initializing shard but was " + shardRouting;
551542

552-
DiscoveryNode sourceNode = null;
553-
if (shardRouting.recoverySource().getType() == Type.PEER) {
554-
sourceNode = findSourceNodeForPeerRecovery(routingTable, nodes, shardRouting);
555-
if (sourceNode == null) {
556-
logger.trace("ignoring initializing shard {} - no source node can be found.", shardRouting.shardId());
557-
return;
543+
try {
544+
final DiscoveryNode sourceNode;
545+
if (shardRouting.recoverySource().getType() == Type.PEER) {
546+
sourceNode = findSourceNodeForPeerRecovery(state.routingTable(), state.nodes(), shardRouting);
547+
if (sourceNode == null) {
548+
logger.trace("ignoring initializing shard {} - no source node can be found.", shardRouting.shardId());
549+
return;
550+
}
551+
} else {
552+
sourceNode = null;
558553
}
559-
}
560554

561-
try {
562555
final long primaryTerm = state.metadata().index(shardRouting.index()).primaryTerm(shardRouting.id());
563556
logger.debug("{} creating shard with primary term [{}]", shardRouting.shardId(), primaryTerm);
564557
indicesService.createShard(
@@ -569,21 +562,15 @@ private void createShard(DiscoveryNodes nodes, RoutingTable routingTable, ShardR
569562
failedShardHandler,
570563
this::updateGlobalCheckpointForShard,
571564
retentionLeaseSyncer,
572-
nodes.getLocalNode(),
565+
state.nodes().getLocalNode(),
573566
sourceNode
574567
);
575568
} catch (Exception e) {
576569
failAndRemoveShard(shardRouting, true, "failed to create shard", e, state);
577570
}
578571
}
579572

580-
private void updateShard(
581-
DiscoveryNodes nodes,
582-
ShardRouting shardRouting,
583-
Shard shard,
584-
RoutingTable routingTable,
585-
ClusterState clusterState
586-
) {
573+
private void updateShard(ShardRouting shardRouting, Shard shard, ClusterState clusterState) {
587574
final ShardRouting currentRoutingEntry = shard.routingEntry();
588575
assert currentRoutingEntry.isSameAllocation(shardRouting)
589576
: "local shard has a different allocation id but wasn't cleaned by removeShards. "
@@ -597,7 +584,7 @@ private void updateShard(
597584
final IndexMetadata indexMetadata = clusterState.metadata().index(shard.shardId().getIndex());
598585
primaryTerm = indexMetadata.primaryTerm(shard.shardId().id());
599586
final Set<String> inSyncIds = indexMetadata.inSyncAllocationIds(shard.shardId().id());
600-
final IndexShardRoutingTable indexShardRoutingTable = routingTable.shardRoutingTable(shardRouting.shardId());
587+
final IndexShardRoutingTable indexShardRoutingTable = clusterState.routingTable().shardRoutingTable(shardRouting.shardId());
601588
shard.updateShardState(
602589
shardRouting,
603590
primaryTerm,
@@ -621,15 +608,15 @@ private void updateShard(
621608
"{} master marked shard as initializing, but shard has state [{}], resending shard started to {}",
622609
shardRouting.shardId(),
623610
state,
624-
nodes.getMasterNode()
611+
clusterState.nodes().getMasterNode()
625612
);
626613
}
627-
if (nodes.getMasterNode() != null) {
614+
if (clusterState.nodes().getMasterNode() != null) {
628615
shardStateAction.shardStarted(
629616
shardRouting,
630617
primaryTerm,
631618
"master "
632-
+ nodes.getMasterNode()
619+
+ clusterState.nodes().getMasterNode()
633620
+ " marked shard as initializing, but shard state is ["
634621
+ state
635622
+ "], mark shard as started",

0 commit comments

Comments
 (0)