Przeglądaj źródła

Allow awareness attributes to be reset via the API

Currently we don't allow resetting the awareness
attribute via the API since it requires at least one
non-empty string to update the setting. This commit
allows resetting this using an empty string.

Closes #3931
Simon Willnauer 12 lat temu
rodzic
commit
f749db26e8

+ 4 - 0
src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AwarenessAllocationDecider.java

@@ -26,6 +26,7 @@ import org.elasticsearch.cluster.routing.MutableShardRouting;
 import org.elasticsearch.cluster.routing.RoutingNode;
 import org.elasticsearch.cluster.routing.ShardRouting;
 import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
+import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.inject.Inject;
 import org.elasticsearch.common.settings.ImmutableSettings;
 import org.elasticsearch.common.settings.Settings;
@@ -88,6 +89,9 @@ public class AwarenessAllocationDecider extends AllocationDecider {
         @Override
         public void onRefreshSettings(Settings settings) {
             String[] awarenessAttributes = settings.getAsArray(CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTES, null);
+            if (awarenessAttributes == null && "".equals(settings.get(CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTES, null))) {
+                awarenessAttributes = Strings.EMPTY_ARRAY; // the empty string resets this
+            }
             if (awarenessAttributes != null) {
                 logger.info("updating [cluster.routing.allocation.awareness.attributes] from [{}] to [{}]", AwarenessAllocationDecider.this.awarenessAttributes, awarenessAttributes);
                 AwarenessAllocationDecider.this.awarenessAttributes = awarenessAttributes;

+ 47 - 0
src/test/java/org/elasticsearch/cluster/allocation/AwarenessAllocationTests.java

@@ -179,5 +179,52 @@ public class AwarenessAllocationTests extends AbstractIntegrationTest {
         assertThat(counts.get(A_0), equalTo(5));
         assertThat(counts.get(B_0), equalTo(3));
         assertThat(counts.get(B_1), equalTo(2));
+        
+        String noZoneNode = cluster().startNode();
+        health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").execute().actionGet();
+        assertThat(health.isTimedOut(), equalTo(false));
+        client().admin().cluster().prepareReroute().get();
+        health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").setWaitForActiveShards(10).setWaitForRelocatingShards(0).execute().actionGet();
+
+        assertThat(health.isTimedOut(), equalTo(false));
+        clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
+
+        counts = new ObjectIntOpenHashMap<String>();
+
+        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
+            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
+                for (ShardRouting shardRouting : indexShardRoutingTable) {
+                    counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).name(), 1);
+                }
+            }
+        }
+        
+        assertThat(counts.get(A_0), equalTo(5));
+        assertThat(counts.get(B_0), equalTo(3));
+        assertThat(counts.get(B_1), equalTo(2));
+        assertThat(counts.containsKey(noZoneNode), equalTo(false));
+        client().admin().cluster().prepareUpdateSettings().setTransientSettings(ImmutableSettings.settingsBuilder().put("cluster.routing.allocation.awareness.attributes", "").build()).get();
+        
+        
+        client().admin().cluster().prepareReroute().get();
+        health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").setWaitForActiveShards(10).setWaitForRelocatingShards(0).execute().actionGet();
+
+        assertThat(health.isTimedOut(), equalTo(false));
+        clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
+
+        counts = new ObjectIntOpenHashMap<String>();
+
+        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
+            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
+                for (ShardRouting shardRouting : indexShardRoutingTable) {
+                    counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).name(), 1);
+                }
+            }
+        }
+        
+        assertThat(counts.get(A_0), equalTo(3));
+        assertThat(counts.get(B_0), equalTo(3));
+        assertThat(counts.get(B_1), equalTo(2));
+        assertThat(counts.get(noZoneNode), equalTo(2));
     }
 }