Skip to content

Commit 392982c

Browse files
committed
Alloc. awareness should ignore nodes without attr
Today we count `null` (i.e. missing) as a valid attribute value in allocation awareness, even though allocation awareness forbids the allocation of shards to such a node. Prior to elastic#69334 this didn't matter, a data node without allocation attributes was pointless. However, elastic#69334 means we now can allocate shards to such a node: for instance, there is no need for nodes holding only enrich indices to have allocation attributes. Therefore we should stop counting `null` as one of the attribute values.
1 parent 9c7f0fe commit 392982c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ public ObjectIntHashMap<String> nodesPerAttributesCounts(String attributeName) {
237237
nodesPerAttributesCounts = new ObjectIntHashMap<>();
238238
for (RoutingNode routingNode : this) {
239239
String attrValue = routingNode.node().getAttributes().get(attributeName);
240-
nodesPerAttributesCounts.addTo(attrValue, 1);
240+
if (attrValue != null) {
241+
nodesPerAttributesCounts.addTo(attrValue, 1);
242+
}
241243
}
242244
nodesPerAttributeNames.put(attributeName, nodesPerAttributesCounts);
243245
return nodesPerAttributesCounts;

server/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.HashMap;
3131
import java.util.Map;
3232

33+
import static java.util.Collections.emptyMap;
3334
import static java.util.Collections.singletonMap;
3435
import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING;
3536
import static org.elasticsearch.cluster.routing.ShardRoutingState.RELOCATING;
@@ -904,4 +905,35 @@ public void testDisabledByAutoExpandReplicas() {
904905

905906
assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED), empty());
906907
}
908+
909+
public void testNodesWithoutAttributeAreIgnored() {
910+
final Settings settings = Settings.builder()
911+
.put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), "zone")
912+
.build();
913+
914+
final AllocationService strategy = createAllocationService(settings);
915+
916+
final Metadata metadata = Metadata.builder()
917+
.put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)
918+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
919+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2)))
920+
.build();
921+
922+
final ClusterState clusterState = applyStartedShardsUntilNoChange(
923+
ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(Settings.EMPTY))
924+
.metadata(metadata)
925+
.routingTable(RoutingTable.builder()
926+
.addAsNew(metadata.index("test"))
927+
.build())
928+
.nodes(DiscoveryNodes.builder()
929+
.add(newNode("A-0", singletonMap("zone", "a")))
930+
.add(newNode("A-1", singletonMap("zone", "a")))
931+
.add(newNode("B-0", singletonMap("zone", "b")))
932+
.add(newNode("B-1", singletonMap("zone", "b")))
933+
.add(newNode("X-0", emptyMap()))
934+
).build(), strategy);
935+
936+
assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED), empty());
937+
}
938+
907939
}

0 commit comments

Comments
 (0)