@@ -349,7 +349,7 @@ private boolean balance(boolean onlyAssign) {
349
349
for (int i = 0 ; i < modelNodes .length ; i ++) {
350
350
ModelNode modelNode = modelNodes [i ];
351
351
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 ) {
353
353
// swap nodes at position i and relevantNodes
354
354
modelNodes [i ] = modelNodes [relevantNodes ];
355
355
modelNodes [relevantNodes ] = modelNode ;
@@ -527,7 +527,7 @@ public boolean move(ShardRouting shard, RoutingNode node ) {
527
527
if (currentNode .getNodeId ().equals (node .nodeId ())) {
528
528
continue ;
529
529
}
530
- RoutingNode target = routingNodes . node ( currentNode .getNodeId () );
530
+ RoutingNode target = currentNode .getRoutingNode ( routingNodes );
531
531
Decision allocationDecision = allocation .deciders ().canAllocate (shard , target , allocation );
532
532
Decision rebalanceDecision = allocation .deciders ().canRebalance (shard , allocation );
533
533
Decision decision = new Decision .Multi ().add (allocationDecision ).add (rebalanceDecision );
@@ -661,7 +661,7 @@ public int compare(ShardRouting o1,
661
661
* don't check deciders
662
662
*/
663
663
if (currentWeight <= minWeight ) {
664
- Decision currentDecision = deciders .canAllocate (shard , routingNodes . node ( node . getNodeId () ), allocation );
664
+ Decision currentDecision = deciders .canAllocate (shard , node . getRoutingNode ( routingNodes ), allocation );
665
665
NOUPDATE :
666
666
if (currentDecision .type () == Type .YES || currentDecision .type () == Type .THROTTLE ) {
667
667
if (currentWeight == minWeight ) {
@@ -707,11 +707,11 @@ public int compare(ShardRouting o1,
707
707
if (logger .isTraceEnabled ()) {
708
708
logger .trace ("Assigned shard [{}] to [{}]" , shard , minNode .getNodeId ());
709
709
}
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 ));
711
711
changed = true ;
712
712
continue ; // don't add to ignoreUnassigned
713
713
} else {
714
- final RoutingNode node = routingNodes . node ( minNode .getNodeId () );
714
+ final RoutingNode node = minNode .getRoutingNode ( routingNodes );
715
715
if (deciders .canAllocate (node , allocation ).type () != Type .YES ) {
716
716
if (logger .isTraceEnabled ()) {
717
717
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
755
755
logger .trace ("Try relocating shard for index index [{}] from node [{}] to node [{}]" , idx , maxNode .getNodeId (),
756
756
minNode .getNodeId ());
757
757
}
758
- final RoutingNode node = routingNodes .node (minNode .getNodeId ());
759
758
ShardRouting candidate = null ;
760
759
final AllocationDeciders deciders = allocation .deciders ();
761
760
for (ShardRouting shard : index .getAllShards ()) {
762
761
if (shard .started ()) {
763
762
// 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 );
765
764
Decision rebalanceDecision = deciders .canRebalance (shard , allocation );
766
765
if (((allocationDecision .type () == Type .YES ) || (allocationDecision .type () == Type .THROTTLE ))
767
766
&& ((rebalanceDecision .type () == Type .YES ) || (rebalanceDecision .type () == Type .THROTTLE ))) {
@@ -793,11 +792,10 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
793
792
}
794
793
/* now allocate on the cluster - if we are started we need to relocate the shard */
795
794
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 ));
798
796
799
797
} 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 ));
801
799
}
802
800
return true ;
803
801
@@ -817,6 +815,8 @@ static class ModelNode implements Iterable<ModelIndex> {
817
815
private final String id ;
818
816
private final Map <String , ModelIndex > indices = new HashMap <>();
819
817
private int numShards = 0 ;
818
+ // lazily calculated
819
+ private RoutingNode routingNode ;
820
820
821
821
public ModelNode (String id ) {
822
822
this .id = id ;
@@ -830,6 +830,13 @@ public String getNodeId() {
830
830
return id ;
831
831
}
832
832
833
+ public RoutingNode getRoutingNode (RoutingNodes routingNodes ) {
834
+ if (routingNode == null ) {
835
+ routingNode = routingNodes .node (id );
836
+ }
837
+ return routingNode ;
838
+ }
839
+
833
840
public int numShards () {
834
841
return numShards ;
835
842
}
0 commit comments