From c6ed81c5f5badd3e9c1beed9e7549da49d743fd6 Mon Sep 17 00:00:00 2001 From: Anshu Agarwal Date: Wed, 7 Sep 2022 11:00:45 +0530 Subject: [PATCH] Update Integ tests for Search WRR to include tests post removing WRR --- .../org/opensearch/search/SearchWRRIT.java | 51 +++++++++++++------ .../ClusterDeleteWRRWeightsRequest.java | 3 +- .../wrr/get/TransportGetWRRWeightsAction.java | 6 +-- .../opensearch/client/ClusterAdminClient.java | 3 +- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/server/src/internalClusterTest/java/org/opensearch/search/SearchWRRIT.java b/server/src/internalClusterTest/java/org/opensearch/search/SearchWRRIT.java index 5168c64b1e827..325a4d83a6ccc 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/SearchWRRIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/SearchWRRIT.java @@ -9,6 +9,7 @@ package org.opensearch.search; import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; +import org.opensearch.action.admin.cluster.shards.routing.wrr.delete.ClusterDeleteWRRWeightsResponse; import org.opensearch.action.admin.cluster.shards.routing.wrr.put.ClusterPutWRRWeightsResponse; import org.opensearch.action.search.SearchResponse; import org.opensearch.cluster.node.DiscoveryNode; @@ -30,7 +31,7 @@ import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; -@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, minNumDataNodes = 2) +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, minNumDataNodes = 3) public class SearchWRRIT extends OpenSearchIntegTestCase { @Override protected int numberOfReplicas() { @@ -39,8 +40,7 @@ protected int numberOfReplicas() { public void testSearchWithWRRShardRouting() throws IOException { Settings commonSettings = Settings.builder() - .put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING.getKey() + "zone" + - ".values", "a,b,c") + .put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING.getKey() + "zone" + ".values", "a,b,c") .put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), "zone") .build(); @@ -62,23 +62,20 @@ public void testSearchWithWRRShardRouting() throws IOException { String C_1 = nodes.get(5); logger.info("--> waiting for nodes to form a cluster"); - ClusterHealthResponse health = - client().admin().cluster().prepareHealth().setWaitForNodes("6").execute().actionGet(); + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("6").execute().actionGet(); assertThat(health.isTimedOut(), equalTo(false)); assertAcked( - prepareCreate("test").setSettings( - Settings.builder().put("index.number_of_shards", 10).put("index.number_of_replicas", 2) - ) + prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 10).put("index.number_of_replicas", 2)) ); ensureGreen(); logger.info("--> creating indices for test"); for (int i = 0; i < 100; i++) { - client().prepareIndex("test_"+i).setId("" + i).setSource("field_"+i, "value_"+i).get(); + client().prepareIndex("test_" + i).setId("" + i).setSource("field_" + i, "value_" + i).get(); } logger.info("--> setting shard routing weights for weighted round robin"); - Map weights = Map.of("a", "1", "b", "1", "c", "0"); + Map weights = Map.of("a", "2", "b", "1", "c", "0"); WRRWeights wrrWeight = new WRRWeights("zone", weights); ClusterPutWRRWeightsResponse response = client().admin().cluster().prepareWRRWeights().setWRRWeights(wrrWeight).get(); @@ -87,28 +84,50 @@ public void testSearchWithWRRShardRouting() throws IOException { Set hitNodes = new HashSet<>(); // making search requests for (int i = 0; i < 50; i++) { - SearchResponse searchResponse = internalCluster().client(randomFrom(A_0, A_1, B_0, B_1)).prepareSearch() + SearchResponse searchResponse = internalCluster().client(randomFrom(A_0, A_1, B_0, B_1)) + .prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .get(); assertEquals(searchResponse.getFailedShards(), 0); - for (int j = 0; j nodeIdsFromZoneWithWeightZero = new ArrayList<>(); for (DiscoveryNode node : dataNodes) { - if(node.getAttributes().get("zone").equals("c")) - { + if (node.getAttributes().get("zone").equals("c")) { nodeIdsFromZoneWithWeightZero.add(node.getId()); } } - for(String nodeId : nodeIdsFromZoneWithWeightZero) { + for (String nodeId : nodeIdsFromZoneWithWeightZero) { assertFalse(hitNodes.contains(nodeId)); } + logger.info("--> deleted shard routing weights for weighted round robin"); + + ClusterDeleteWRRWeightsResponse deleteResponse = client().admin().cluster().prepareDeleteWRRWeights().get(); + assertEquals(response.isAcknowledged(), true); + + hitNodes = new HashSet<>(); + // making search requests + for (int i = 0; i < 50; i++) { + SearchResponse searchResponse = internalCluster().client(randomFrom(A_0, A_1, B_0, B_1)) + .prepareSearch() + .setQuery(QueryBuilders.matchAllQuery()) + .get(); + assertEquals(searchResponse.getFailedShards(), 0); + for (int j = 0; j < searchResponse.getHits().getHits().length; j++) { + hitNodes.add(searchResponse.getHits().getAt(j).getShard().getNodeId()); + } + } + + // Check shard routing requests hit data nodes in zone c + for (String nodeId : nodeIdsFromZoneWithWeightZero) { + assertFalse(!hitNodes.contains(nodeId)); + } } } diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/delete/ClusterDeleteWRRWeightsRequest.java b/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/delete/ClusterDeleteWRRWeightsRequest.java index cb732f82da38b..2fef74303cf3e 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/delete/ClusterDeleteWRRWeightsRequest.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/delete/ClusterDeleteWRRWeightsRequest.java @@ -21,8 +21,7 @@ * @opensearch.internal */ public class ClusterDeleteWRRWeightsRequest extends ClusterManagerNodeRequest { - public ClusterDeleteWRRWeightsRequest() { - } + public ClusterDeleteWRRWeightsRequest() {} public ClusterDeleteWRRWeightsRequest(StreamInput in) throws IOException { super(in); diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/get/TransportGetWRRWeightsAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/get/TransportGetWRRWeightsAction.java index 12771c092876b..11481f0e96914 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/get/TransportGetWRRWeightsAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/shards/routing/wrr/get/TransportGetWRRWeightsAction.java @@ -111,9 +111,9 @@ protected void clusterManagerOperation( String weight = null; if (weightedRoundRobinMetadata != null && weightedRoundRobinMetadata.getWrrWeight() != null) { WRRWeights wrrWeights = weightedRoundRobinMetadata.getWrrWeight(); - if (request.local()) { - DiscoveryNode localNode = state.getNodes().getLocalNode(); - if (localNode.getAttributes().get(request.getAwarenessAttribute())!=null) { + DiscoveryNode localNode = state.getNodes().getLocalNode(); + if (request.local() && !localNode.isClusterManagerNode()) { + if (localNode.getAttributes().get(request.getAwarenessAttribute()) != null) { String attrVal = localNode.getAttributes().get(request.getAwarenessAttribute()); if (wrrWeights.weights().containsKey(attrVal)) { weight = wrrWeights.weights().get(attrVal).toString(); diff --git a/server/src/main/java/org/opensearch/client/ClusterAdminClient.java b/server/src/main/java/org/opensearch/client/ClusterAdminClient.java index d5deae58ff689..6c29b15528965 100644 --- a/server/src/main/java/org/opensearch/client/ClusterAdminClient.java +++ b/server/src/main/java/org/opensearch/client/ClusterAdminClient.java @@ -306,7 +306,8 @@ public interface ClusterAdminClient extends OpenSearchClient { * sampled for the node ids specified in the request. Nodes usage of the * cluster. * - * @param request The nodes usage request + * @param request + * The nodes usage request * @return The result future * @see org.opensearch.client.Requests#nodesUsageRequest(String...) */