Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anshu Agarwal <anshukag@amazon.com>
  • Loading branch information
Anshu Agarwal committed Nov 15, 2022
1 parent 6ab7e70 commit 71105fd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import org.opensearch.action.admin.cluster.shards.routing.weighted.delete.ClusterDeleteWeightedRoutingResponse;
import org.opensearch.action.admin.cluster.shards.routing.weighted.get.ClusterGetWeightedRoutingResponse;
import org.opensearch.action.admin.cluster.shards.routing.weighted.put.ClusterPutWeightedRoutingResponse;
import org.opensearch.cluster.ClusterState;
import org.opensearch.common.settings.Settings;
import org.opensearch.test.OpenSearchIntegTestCase;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -348,4 +351,63 @@ public void testDeleteWeightedRouting_WeightsAreSet() {
assertTrue(deleteResponse.isAcknowledged());
assertNull(internalCluster().clusterService().state().metadata().weightedRoutingMetadata());
}

public void testPutWeightedRoutingWithVersioning() {
Settings commonSettings = Settings.builder()
.put("cluster.routing.allocation.awareness.attributes", "zone")
.put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c")
.build();

logger.info("--> starting 6 nodes on different zones");
int nodeCountPerAZ = 2;

logger.info("--> starting a dedicated cluster manager node");
internalCluster().startClusterManagerOnlyNode(Settings.builder().put(commonSettings).build());

logger.info("--> starting 1 nodes on zones 'a' & 'b' & 'c'");
List<String> nodes_in_zone_a = internalCluster().startDataOnlyNodes(
nodeCountPerAZ,
Settings.builder().put(commonSettings).put("node.attr.zone", "a").build()
);
List<String> nodes_in_zone_b = internalCluster().startDataOnlyNodes(
nodeCountPerAZ,
Settings.builder().put(commonSettings).put("node.attr.zone", "b").build()
);
List<String> nodes_in_zone_c = internalCluster().startDataOnlyNodes(
nodeCountPerAZ,
Settings.builder().put(commonSettings).put("node.attr.zone", "c").build()
);

logger.info("--> waiting for nodes to form a cluster");
ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("7").execute().actionGet();
assertThat(health.isTimedOut(), equalTo(false));

ensureGreen();

logger.info("--> setting shard routing weights for weighted round robin");

Future<ClusterPutWeightedRoutingResponse> response[] = new Future[50];
for (int i = 0; i < 50; i++) {
Map<String, Double> weights = new HashMap<>();
weights.put("a", 1.0);
weights.put("b", 2.0);
double weightc = (double) i;
weights.put("c", weightc);
WeightedRouting weightedRouting = new WeightedRouting("zone", weights);
response[i] = client().admin().cluster().prepareWeightedRouting().setWeightedRouting(weightedRouting).setVersion(0).execute();
}

for (int i = 0; i < 50; i++) {
try {
ClusterPutWeightedRoutingResponse weightedRoutingResponse = response[i].get();
logger.info("response from request -" + i);
logger.info(weightedRoutingResponse.isAcknowledged());
} catch (Exception t) {
logger.info("Exception is hit");
}
}

ClusterState stateafter = internalCluster().clusterService().state();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.action.admin.cluster.shards.routing.weighted;

import org.opensearch.OpenSearchException;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.rest.RestStatus;

import java.io.IOException;

public class WeightedRoutingVersionMismatchException extends OpenSearchException {

public WeightedRoutingVersionMismatchException() {
super("");
}

public WeightedRoutingVersionMismatchException(Throwable cause) {
super(cause);
}

public WeightedRoutingVersionMismatchException(String message) {
super(message);
}

@Override
public RestStatus status() {
return RestStatus.CONFLICT;
}

public WeightedRoutingVersionMismatchException(StreamInput in) throws IOException {
super(in);
}
}

0 comments on commit 71105fd

Please sign in to comment.