Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
Expand All @@ -55,8 +54,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.opensearch.common.Booleans.parseBoolean;

public class OperationRouting {

public static final Setting<Boolean> USE_ADAPTIVE_REPLICA_SELECTION_SETTING = Setting.boolSetting(
Expand All @@ -66,53 +63,52 @@ public class OperationRouting {
Setting.Property.NodeScope
);

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(OperationRouting.class);
private static final String IGNORE_AWARENESS_ATTRIBUTES_PROPERTY = "opensearch.search.ignore_awareness_attributes";
static final String IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE =
"searches will not be routed based on awareness attributes starting in version 8.0.0; "
+ "to opt into this behaviour now please set the system property ["
+ IGNORE_AWARENESS_ATTRIBUTES_PROPERTY
+ "] to [true]";

private List<String> awarenessAttributes;
private boolean useAdaptiveReplicaSelection;
public static final String IGNORE_AWARENESS_ATTRIBUTES = "cluster.search.ignore_awareness_attributes";
public static final Setting<Boolean> IGNORE_AWARENESS_ATTRIBUTES_SETTING = Setting.boolSetting(
IGNORE_AWARENESS_ATTRIBUTES,
true,
Setting.Property.Dynamic,
Setting.Property.NodeScope
);
private volatile List<String> awarenessAttributes;
private volatile boolean useAdaptiveReplicaSelection;
private volatile boolean ignoreAwarenessAttr;

public OperationRouting(Settings settings, ClusterSettings clusterSettings) {
// whether to ignore awareness attributes when routing requests
boolean ignoreAwarenessAttr = parseBoolean(System.getProperty(IGNORE_AWARENESS_ATTRIBUTES_PROPERTY), false);
if (ignoreAwarenessAttr == false) {
awarenessAttributes = AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.get(settings);
if (awarenessAttributes.isEmpty() == false) {
deprecationLogger.deprecate("searches_not_routed_on_awareness_attributes", IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE);
}
clusterSettings.addSettingsUpdateConsumer(
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING,
this::setAwarenessAttributes
);
} else {
awarenessAttributes = Collections.emptyList();
}

this.ignoreAwarenessAttr = clusterSettings.get(IGNORE_AWARENESS_ATTRIBUTES_SETTING);
this.awarenessAttributes = AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.get(settings);
clusterSettings.addSettingsUpdateConsumer(
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING,
this::setAwarenessAttributes
);
this.useAdaptiveReplicaSelection = USE_ADAPTIVE_REPLICA_SELECTION_SETTING.get(settings);
clusterSettings.addSettingsUpdateConsumer(USE_ADAPTIVE_REPLICA_SELECTION_SETTING, this::setUseAdaptiveReplicaSelection);
clusterSettings.addSettingsUpdateConsumer(IGNORE_AWARENESS_ATTRIBUTES_SETTING, this::setIgnoreAwarenessAttributes);
}

void setUseAdaptiveReplicaSelection(boolean useAdaptiveReplicaSelection) {
this.useAdaptiveReplicaSelection = useAdaptiveReplicaSelection;
}

void setIgnoreAwarenessAttributes(boolean ignoreAwarenessAttributes) {
this.ignoreAwarenessAttr = ignoreAwarenessAttributes;
}

public boolean isIgnoreAwarenessAttr() {
return ignoreAwarenessAttr;
}

List<String> getAwarenessAttributes() {
return awarenessAttributes;
}

private void setAwarenessAttributes(List<String> awarenessAttributes) {
boolean ignoreAwarenessAttr = parseBoolean(System.getProperty(IGNORE_AWARENESS_ATTRIBUTES_PROPERTY), false);
if (ignoreAwarenessAttr == false) {
if (this.awarenessAttributes.isEmpty() && awarenessAttributes.isEmpty() == false) {
deprecationLogger.deprecate("searches_not_routed_on_awareness_attributes", IGNORE_AWARENESS_ATTRIBUTES_DEPRECATION_MESSAGE);
}
this.awarenessAttributes = awarenessAttributes;
}
this.awarenessAttributes = awarenessAttributes;
}

public boolean ignoreAwarenessAttributes() {
return this.awarenessAttributes.isEmpty() || this.ignoreAwarenessAttr;
}

public ShardIterator indexShards(ClusterState clusterState, String index, String id, @Nullable String routing) {
Expand Down Expand Up @@ -286,8 +282,7 @@ private ShardIterator preferenceActiveShardIterator(
// for a different element in the list by also incorporating the
// shard ID into the hash of the user-supplied preference key.
routingHash = 31 * routingHash + indexShard.shardId.hashCode();

if (awarenessAttributes.isEmpty()) {
if (ignoreAwarenessAttributes()) {
return indexShard.activeInitializingShardsIt(routingHash);
} else {
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes, routingHash);
Expand All @@ -300,7 +295,7 @@ private ShardIterator shardRoutings(
@Nullable ResponseCollectorService collectorService,
@Nullable Map<String, Long> nodeCounts
) {
if (awarenessAttributes.isEmpty()) {
if (ignoreAwarenessAttributes()) {
if (useAdaptiveReplicaSelection) {
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ public void apply(Settings value, Settings current, Settings previous) {
FastVectorHighlighter.SETTING_TV_HIGHLIGHT_MULTI_VALUE,
Node.BREAKER_TYPE_KEY,
OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING,
OperationRouting.IGNORE_AWARENESS_ATTRIBUTES_SETTING,
IndexGraveyard.SETTING_MAX_TOMBSTONES,
PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING,
EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.cluster.routing;

import org.junit.After;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchIntegTestCase;

import static org.hamcrest.Matchers.equalTo;
import static org.opensearch.cluster.routing.OperationRouting.IGNORE_AWARENESS_ATTRIBUTES;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;

public class OperationRoutingAwarenessTests extends OpenSearchIntegTestCase {

@After
public void cleanup() {
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().putNull("*")));
}

public void testToggleSearchAllocationAwareness() {
OperationRouting routing = internalCluster().clusterService().operationRouting();

// Update awareness settings
client().admin()
.cluster()
.prepareUpdateSettings()
.setTransientSettings(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "zone"))
.get();
assertThat(routing.getAwarenessAttributes().size(), equalTo(1));
assertThat(routing.getAwarenessAttributes().get(0), equalTo("zone"));
assertTrue(internalCluster().clusterService().operationRouting().ignoreAwarenessAttributes());

// Unset ignore awareness attributes
client().admin()
.cluster()
.prepareUpdateSettings()
.setTransientSettings(Settings.builder().put(IGNORE_AWARENESS_ATTRIBUTES, false))
.get();
// assert that awareness attributes hasn't changed
assertThat(routing.getAwarenessAttributes().size(), equalTo(1));
assertThat(routing.getAwarenessAttributes().get(0), equalTo("zone"));
assertFalse(internalCluster().clusterService().operationRouting().isIgnoreAwarenessAttr());
assertFalse(internalCluster().clusterService().operationRouting().ignoreAwarenessAttributes());

// Set ignore awareness attributes to true
client().admin()
.cluster()
.prepareUpdateSettings()
.setTransientSettings(Settings.builder().put(IGNORE_AWARENESS_ATTRIBUTES, true))
.get();
// assert that awareness attributes hasn't changed
assertThat(routing.getAwarenessAttributes().size(), equalTo(1));
assertThat(routing.getAwarenessAttributes().get(0), equalTo("zone"));
assertTrue(routing.isIgnoreAwarenessAttr());
assertTrue(internalCluster().clusterService().operationRouting().ignoreAwarenessAttributes());
}
}
Loading