Skip to content

Commit 7e134da

Browse files
author
Yannick Welsch
committed
Cache result of RoutingNodes.node(...) in ModelNode
1 parent eea791d commit 7e134da

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

core/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private boolean balance(boolean onlyAssign) {
349349
for (int i = 0; i < modelNodes.length; i++) {
350350
ModelNode modelNode = modelNodes[i];
351351
if (modelNode.getIndex(index) != null
352-
|| deciders.canAllocate(indexMetaData, routingNodes.node(modelNode.getNodeId()), allocation).type() != Type.NO) {
352+
|| deciders.canAllocate(indexMetaData, modelNode.getRoutingNode(routingNodes), allocation).type() != Type.NO) {
353353
// swap nodes at position i and relevantNodes
354354
modelNodes[i] = modelNodes[relevantNodes];
355355
modelNodes[relevantNodes] = modelNode;
@@ -527,7 +527,7 @@ public boolean move(ShardRouting shard, RoutingNode node ) {
527527
if (currentNode.getNodeId().equals(node.nodeId())) {
528528
continue;
529529
}
530-
RoutingNode target = routingNodes.node(currentNode.getNodeId());
530+
RoutingNode target = currentNode.getRoutingNode(routingNodes);
531531
Decision allocationDecision = allocation.deciders().canAllocate(shard, target, allocation);
532532
Decision rebalanceDecision = allocation.deciders().canRebalance(shard, allocation);
533533
Decision decision = new Decision.Multi().add(allocationDecision).add(rebalanceDecision);
@@ -661,7 +661,7 @@ public int compare(ShardRouting o1,
661661
* don't check deciders
662662
*/
663663
if (currentWeight <= minWeight) {
664-
Decision currentDecision = deciders.canAllocate(shard, routingNodes.node(node.getNodeId()), allocation);
664+
Decision currentDecision = deciders.canAllocate(shard, node.getRoutingNode(routingNodes), allocation);
665665
NOUPDATE:
666666
if (currentDecision.type() == Type.YES || currentDecision.type() == Type.THROTTLE) {
667667
if (currentWeight == minWeight) {
@@ -707,11 +707,11 @@ public int compare(ShardRouting o1,
707707
if (logger.isTraceEnabled()) {
708708
logger.trace("Assigned shard [{}] to [{}]", shard, minNode.getNodeId());
709709
}
710-
routingNodes.initialize(shard, routingNodes.node(minNode.getNodeId()).nodeId(), allocation.clusterInfo().getShardSize(shard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
710+
routingNodes.initialize(shard, minNode.getNodeId(), allocation.clusterInfo().getShardSize(shard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
711711
changed = true;
712712
continue; // don't add to ignoreUnassigned
713713
} else {
714-
final RoutingNode node = routingNodes.node(minNode.getNodeId());
714+
final RoutingNode node = minNode.getRoutingNode(routingNodes);
715715
if (deciders.canAllocate(node, allocation).type() != Type.YES) {
716716
if (logger.isTraceEnabled()) {
717717
logger.trace("Can not allocate on node [{}] remove from round decision [{}]", node, decision.type());
@@ -755,13 +755,12 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
755755
logger.trace("Try relocating shard for index index [{}] from node [{}] to node [{}]", idx, maxNode.getNodeId(),
756756
minNode.getNodeId());
757757
}
758-
final RoutingNode node = routingNodes.node(minNode.getNodeId());
759758
ShardRouting candidate = null;
760759
final AllocationDeciders deciders = allocation.deciders();
761760
for (ShardRouting shard : index.getAllShards()) {
762761
if (shard.started()) {
763762
// skip initializing, unassigned and relocating shards we can't relocate them anyway
764-
Decision allocationDecision = deciders.canAllocate(shard, node, allocation);
763+
Decision allocationDecision = deciders.canAllocate(shard, minNode.getRoutingNode(routingNodes), allocation);
765764
Decision rebalanceDecision = deciders.canRebalance(shard, allocation);
766765
if (((allocationDecision.type() == Type.YES) || (allocationDecision.type() == Type.THROTTLE))
767766
&& ((rebalanceDecision.type() == Type.YES) || (rebalanceDecision.type() == Type.THROTTLE))) {
@@ -793,11 +792,10 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
793792
}
794793
/* now allocate on the cluster - if we are started we need to relocate the shard */
795794
if (candidate.started()) {
796-
RoutingNode lowRoutingNode = routingNodes.node(minNode.getNodeId());
797-
routingNodes.relocate(candidate, lowRoutingNode.nodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
795+
routingNodes.relocate(candidate, minNode.getNodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
798796

799797
} else {
800-
routingNodes.initialize(candidate, routingNodes.node(minNode.getNodeId()).nodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
798+
routingNodes.initialize(candidate, minNode.getNodeId(), allocation.clusterInfo().getShardSize(candidate, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE));
801799
}
802800
return true;
803801

@@ -817,6 +815,8 @@ static class ModelNode implements Iterable<ModelIndex> {
817815
private final String id;
818816
private final Map<String, ModelIndex> indices = new HashMap<>();
819817
private int numShards = 0;
818+
// lazily calculated
819+
private RoutingNode routingNode;
820820

821821
public ModelNode(String id) {
822822
this.id = id;
@@ -830,6 +830,13 @@ public String getNodeId() {
830830
return id;
831831
}
832832

833+
public RoutingNode getRoutingNode(RoutingNodes routingNodes) {
834+
if (routingNode == null) {
835+
routingNode = routingNodes.node(id);
836+
}
837+
return routingNode;
838+
}
839+
833840
public int numShards() {
834841
return numShards;
835842
}

0 commit comments

Comments
 (0)