Skip to content

Commit 3161386

Browse files
authored
Move allocation awareness attributes to list setting (#30626)
Allows the setting to be specified using proper array syntax, for example: "cluster.routing.allocation.awareness.attributes": [ "foo", "bar", "baz" ] Closes #30617
1 parent 9434f25 commit 3161386

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -544,20 +544,20 @@ public Set<String> getAllAllocationIds() {
544544

545545
static class AttributesKey {
546546

547-
final String[] attributes;
547+
final List<String> attributes;
548548

549-
AttributesKey(String[] attributes) {
549+
AttributesKey(List<String> attributes) {
550550
this.attributes = attributes;
551551
}
552552

553553
@Override
554554
public int hashCode() {
555-
return Arrays.hashCode(attributes);
555+
return attributes.hashCode();
556556
}
557557

558558
@Override
559559
public boolean equals(Object obj) {
560-
return obj instanceof AttributesKey && Arrays.equals(attributes, ((AttributesKey) obj).attributes);
560+
return obj instanceof AttributesKey && attributes.equals(((AttributesKey) obj).attributes);
561561
}
562562
}
563563

@@ -621,11 +621,11 @@ private static List<ShardRouting> collectAttributeShards(AttributesKey key, Disc
621621
return Collections.unmodifiableList(to);
622622
}
623623

624-
public ShardIterator preferAttributesActiveInitializingShardsIt(String[] attributes, DiscoveryNodes nodes) {
624+
public ShardIterator preferAttributesActiveInitializingShardsIt(List<String> attributes, DiscoveryNodes nodes) {
625625
return preferAttributesActiveInitializingShardsIt(attributes, nodes, shuffler.nextSeed());
626626
}
627627

628-
public ShardIterator preferAttributesActiveInitializingShardsIt(String[] attributes, DiscoveryNodes nodes, int seed) {
628+
public ShardIterator preferAttributesActiveInitializingShardsIt(List<String> attributes, DiscoveryNodes nodes, int seed) {
629629
AttributesKey key = new AttributesKey(attributes);
630630
AttributesRoutings activeRoutings = getActiveAttribute(key, nodes);
631631
AttributesRoutings initializingRoutings = getInitializingAttribute(key, nodes);

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Arrays;
4040
import java.util.Collections;
4141
import java.util.HashSet;
42+
import java.util.List;
4243
import java.util.Map;
4344
import java.util.Set;
4445
import java.util.stream.Collectors;
@@ -49,7 +50,7 @@ public class OperationRouting extends AbstractComponent {
4950
Setting.boolSetting("cluster.routing.use_adaptive_replica_selection", true,
5051
Setting.Property.Dynamic, Setting.Property.NodeScope);
5152

52-
private String[] awarenessAttributes;
53+
private List<String> awarenessAttributes;
5354
private boolean useAdaptiveReplicaSelection;
5455

5556
public OperationRouting(Settings settings, ClusterSettings clusterSettings) {
@@ -65,7 +66,7 @@ void setUseAdaptiveReplicaSelection(boolean useAdaptiveReplicaSelection) {
6566
this.useAdaptiveReplicaSelection = useAdaptiveReplicaSelection;
6667
}
6768

68-
private void setAwarenessAttributes(String[] awarenessAttributes) {
69+
private void setAwarenessAttributes(List<String> awarenessAttributes) {
6970
this.awarenessAttributes = awarenessAttributes;
7071
}
7172

@@ -139,7 +140,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
139140
@Nullable ResponseCollectorService collectorService,
140141
@Nullable Map<String, Long> nodeCounts) {
141142
if (preference == null || preference.isEmpty()) {
142-
if (awarenessAttributes.length == 0) {
143+
if (awarenessAttributes.isEmpty()) {
143144
if (useAdaptiveReplicaSelection) {
144145
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
145146
} else {
@@ -174,7 +175,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
174175
}
175176
// no more preference
176177
if (index == -1 || index == preference.length() - 1) {
177-
if (awarenessAttributes.length == 0) {
178+
if (awarenessAttributes.isEmpty()) {
178179
if (useAdaptiveReplicaSelection) {
179180
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
180181
} else {
@@ -218,7 +219,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
218219
// shard ID into the hash of the user-supplied preference key.
219220
routingHash = 31 * routingHash + indexShard.shardId.hashCode();
220221
}
221-
if (awarenessAttributes.length == 0) {
222+
if (awarenessAttributes.isEmpty()) {
222223
return indexShard.activeInitializingShardsIt(routingHash);
223224
} else {
224225
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes, routingHash);

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AwarenessAllocationDecider.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.function.Function;
2526

2627
import com.carrotsearch.hppc.ObjectIntHashMap;
2728
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -34,6 +35,8 @@
3435
import org.elasticsearch.common.settings.Setting.Property;
3536
import org.elasticsearch.common.settings.Settings;
3637

38+
import static java.util.Collections.emptyList;
39+
3740
/**
3841
* This {@link AllocationDecider} controls shard allocation based on
3942
* {@code awareness} key-value pairs defined in the node configuration.
@@ -78,13 +81,13 @@ public class AwarenessAllocationDecider extends AllocationDecider {
7881

7982
public static final String NAME = "awareness";
8083

81-
public static final Setting<String[]> CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING =
82-
new Setting<>("cluster.routing.allocation.awareness.attributes", "", s -> Strings.tokenizeToStringArray(s, ","), Property.Dynamic,
84+
public static final Setting<List<String>> CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING =
85+
Setting.listSetting("cluster.routing.allocation.awareness.attributes", emptyList(), Function.identity(), Property.Dynamic,
8386
Property.NodeScope);
8487
public static final Setting<Settings> CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING =
8588
Setting.groupSetting("cluster.routing.allocation.awareness.force.", Property.Dynamic, Property.NodeScope);
8689

87-
private volatile String[] awarenessAttributes;
90+
private volatile List<String> awarenessAttributes;
8891

8992
private volatile Map<String, List<String>> forcedAwarenessAttributes;
9093

@@ -109,7 +112,7 @@ private void setForcedAwarenessAttributes(Settings forceSettings) {
109112
this.forcedAwarenessAttributes = forcedAwarenessAttributes;
110113
}
111114

112-
private void setAwarenessAttributes(String[] awarenessAttributes) {
115+
private void setAwarenessAttributes(List<String> awarenessAttributes) {
113116
this.awarenessAttributes = awarenessAttributes;
114117
}
115118

@@ -124,7 +127,7 @@ public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAl
124127
}
125128

126129
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {
127-
if (awarenessAttributes.length == 0) {
130+
if (awarenessAttributes.isEmpty()) {
128131
return allocation.decision(Decision.YES, NAME,
129132
"allocation awareness is not enabled, set cluster setting [%s] to enable it",
130133
CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey());
@@ -138,7 +141,7 @@ private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, Rout
138141
return allocation.decision(Decision.NO, NAME,
139142
"node does not contain the awareness attribute [%s]; required attributes cluster setting [%s=%s]",
140143
awarenessAttribute, CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(),
141-
allocation.debugDecision() ? Strings.arrayToCommaDelimitedString(awarenessAttributes) : null);
144+
allocation.debugDecision() ? Strings.collectionToCommaDelimitedString(awarenessAttributes) : null);
142145
}
143146

144147
// build attr_value -> nodes map

server/src/test/java/org/elasticsearch/cluster/routing/IndexShardRoutingTableTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.elasticsearch.test.ESTestCase;
2525

2626
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.List;
2729

2830
public class IndexShardRoutingTableTests extends ESTestCase {
2931
public void testEqualsAttributesKey() {
30-
String[] attr1 = {"a"};
31-
String[] attr2 = {"b"};
32+
List<String> attr1 = Arrays.asList("a");
33+
List<String> attr2 = Arrays.asList("b");
3234
IndexShardRoutingTable.AttributesKey attributesKey1 = new IndexShardRoutingTable.AttributesKey(attr1);
3335
IndexShardRoutingTable.AttributesKey attributesKey2 = new IndexShardRoutingTable.AttributesKey(attr1);
3436
IndexShardRoutingTable.AttributesKey attributesKey3 = new IndexShardRoutingTable.AttributesKey(attr2);

server/src/test/java/org/elasticsearch/cluster/structure/RoutingIteratorTests.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.common.settings.Settings;
4242
import org.elasticsearch.index.shard.ShardId;
4343

44+
import java.util.Arrays;
4445
import java.util.Collections;
4546
import java.util.HashMap;
4647
import java.util.Iterator;
@@ -50,7 +51,6 @@
5051
import static java.util.Collections.unmodifiableMap;
5152
import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING;
5253
import static org.hamcrest.Matchers.anyOf;
53-
import static org.hamcrest.Matchers.containsString;
5454
import static org.hamcrest.Matchers.equalTo;
5555
import static org.hamcrest.Matchers.not;
5656
import static org.hamcrest.Matchers.notNullValue;
@@ -224,11 +224,16 @@ public void testRandomRouting() {
224224
}
225225

226226
public void testAttributePreferenceRouting() {
227-
AllocationService strategy = createAllocationService(Settings.builder()
228-
.put("cluster.routing.allocation.node_concurrent_recoveries", 10)
229-
.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always")
230-
.put("cluster.routing.allocation.awareness.attributes", "rack_id,zone")
231-
.build());
227+
Settings.Builder settings = Settings.builder()
228+
.put("cluster.routing.allocation.node_concurrent_recoveries", 10)
229+
.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always");
230+
if (randomBoolean()) {
231+
settings.put("cluster.routing.allocation.awareness.attributes", " rack_id, zone ");
232+
} else {
233+
settings.putList("cluster.routing.allocation.awareness.attributes", "rack_id", "zone");
234+
}
235+
236+
AllocationService strategy = createAllocationService(settings.build());
232237

233238
MetaData metaData = MetaData.builder()
234239
.put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1))
@@ -258,15 +263,15 @@ public void testAttributePreferenceRouting() {
258263
clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
259264

260265
// after all are started, check routing iteration
261-
ShardIterator shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(new String[]{"rack_id"}, clusterState.nodes());
266+
ShardIterator shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(Arrays.asList("rack_id"), clusterState.nodes());
262267
ShardRouting shardRouting = shardIterator.nextOrNull();
263268
assertThat(shardRouting, notNullValue());
264269
assertThat(shardRouting.currentNodeId(), equalTo("node1"));
265270
shardRouting = shardIterator.nextOrNull();
266271
assertThat(shardRouting, notNullValue());
267272
assertThat(shardRouting.currentNodeId(), equalTo("node2"));
268273

269-
shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(new String[]{"rack_id"}, clusterState.nodes());
274+
shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(Arrays.asList("rack_id"), clusterState.nodes());
270275
shardRouting = shardIterator.nextOrNull();
271276
assertThat(shardRouting, notNullValue());
272277
assertThat(shardRouting.currentNodeId(), equalTo("node1"));

0 commit comments

Comments
 (0)