From f2a6c88f831674200ee922330680868f19376e70 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 6 Sep 2019 09:29:27 +0200 Subject: [PATCH] Add a system property to ignore awareness attributes (#46375) This is a follow up of #19191 for 7.x. This change adds a system property called "es.routing.search_ignore_awareness_attributes" that when set to true will effectively ignore allocation awareness attributes when routing search and get requests. This is now the default in 8.x so this commit adds a way to opt-in to this new behavior in a minor version of 7.x. Relates #45735 --- .../cluster/allocation_awareness.asciidoc | 9 ++-- .../routing/EvilSystemPropertyTests.java | 49 +++++++++++++++++++ .../cluster/routing/OperationRouting.java | 19 +++++-- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 qa/evil-tests/src/test/java/org/elasticsearch/cluster/routing/EvilSystemPropertyTests.java diff --git a/docs/reference/modules/cluster/allocation_awareness.asciidoc b/docs/reference/modules/cluster/allocation_awareness.asciidoc index 4381d0281cc..1643a5f9917 100644 --- a/docs/reference/modules/cluster/allocation_awareness.asciidoc +++ b/docs/reference/modules/cluster/allocation_awareness.asciidoc @@ -17,9 +17,12 @@ The allocation awareness settings can be configured in `elasticsearch.yml` and updated dynamically with the <> API. -{es} prefers using shards in the same location (with the same -awareness attribute values) to process search or GET requests. Using local -shards is usually faster than crossing rack or zone boundaries. +By default {es} uses <> +to route search or GET requests. However, with the presence of allocation awareness +attributes {es} will prefer using shards in the same location (with the same +awareness attribute values) to process these requests. This behavior can be +disabled by specifying `export ES_JAVA_OPTS="$ES_JAVA_OPTS -Des.search.ignore_awareness_attributes=true"` +system property on every node that is part of the cluster. NOTE: The number of attribute values determines how many shard copies are allocated in each location. If the number of nodes in each location is diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/cluster/routing/EvilSystemPropertyTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/routing/EvilSystemPropertyTests.java new file mode 100644 index 00000000000..5949360a835 --- /dev/null +++ b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/routing/EvilSystemPropertyTests.java @@ -0,0 +1,49 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.cluster.routing; + +import org.elasticsearch.common.SuppressForbidden; +import org.elasticsearch.common.settings.ClusterSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.Matchers.equalTo; + +public class EvilSystemPropertyTests extends ESTestCase { + + @SuppressForbidden(reason = "manipulates system properties for testing") + public void testDisableSearchAllocationAwareness() { + Settings indexSettings = Settings.builder() + .put("cluster.routing.allocation.awareness.attributes", "test") + .build(); + OperationRouting routing = new OperationRouting(indexSettings, + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); + assertThat(routing.getAwarenessAttributes().size(), equalTo(1)); + assertThat(routing.getAwarenessAttributes().get(0), equalTo("test")); + System.setProperty("es.search.ignore_awareness_attributes", "true"); + try { + routing = new OperationRouting(indexSettings, + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); + assertTrue(routing.getAwarenessAttributes().isEmpty()); + } finally { + System.clearProperty("es.search.ignore_awareness_attributes"); + } + + } +} diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java b/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java index 08dc3d1d709..ea2c2b5881d 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java @@ -42,6 +42,8 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import static org.elasticsearch.common.Booleans.parseBoolean; + public class OperationRouting { public static final Setting USE_ADAPTIVE_REPLICA_SELECTION_SETTING = @@ -52,10 +54,17 @@ public class OperationRouting { private boolean useAdaptiveReplicaSelection; public OperationRouting(Settings settings, ClusterSettings clusterSettings) { - this.awarenessAttributes = AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.get(settings); + // whether to ignore awareness attributes when routing requests + boolean ignoreAwarenessAttr = parseBoolean(System.getProperty("es.search.ignore_awareness_attributes"), false); + if (ignoreAwarenessAttr == false) { + awarenessAttributes = AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.get(settings); + clusterSettings.addSettingsUpdateConsumer(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING, + this::setAwarenessAttributes); + } else { + awarenessAttributes = Collections.emptyList(); + } + this.useAdaptiveReplicaSelection = USE_ADAPTIVE_REPLICA_SELECTION_SETTING.get(settings); - clusterSettings.addSettingsUpdateConsumer(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING, - this::setAwarenessAttributes); clusterSettings.addSettingsUpdateConsumer(USE_ADAPTIVE_REPLICA_SELECTION_SETTING, this::setUseAdaptiveReplicaSelection); } @@ -63,6 +72,10 @@ public class OperationRouting { this.useAdaptiveReplicaSelection = useAdaptiveReplicaSelection; } + List getAwarenessAttributes() { + return awarenessAttributes; + } + private void setAwarenessAttributes(List awarenessAttributes) { this.awarenessAttributes = awarenessAttributes; }