Skip to content

Commit

Permalink
Add GET api to get weighted round robin shard routing weights
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 Aug 22, 2022
1 parent 026d3ab commit 1f2c535
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cluster.get_wrr_weights": {
"documentation": {
"url": "TBA",
"description": "TBA"
},
"stability": "experimental",
"url": {
"paths": [
{
"path": "/_cluster/routing/awareness/{attribute}/weights",
"methods": [
"GET"
],
"parts": {
"attribute": {
"type": "string",
"description": "Awareness attribute name"
}
}
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.admin.cluster.shards.routing.wrr.get.ClusterGetWRRWeightsResponse;
import org.opensearch.action.admin.cluster.shards.routing.wrr.put.ClusterPutWRRWeightsResponse;

import org.opensearch.test.OpenSearchIntegTestCase;

import java.util.Map;

import static org.opensearch.test.OpenSearchIntegTestCase.Scope.TEST;

@OpenSearchIntegTestCase.ClusterScope(scope = TEST, supportsDedicatedMasters = true, numDataNodes = 3)
public class WRRRoutingIT extends OpenSearchIntegTestCase {

public void testUpdateWRRWeights() {

Map<String, Object> weights = Map.of("a", "1", "b", "1", "c", "0");
WRRWeight wrrWeight = new WRRWeight("zone", weights);
ClusterPutWRRWeightsResponse response = client().admin().cluster().prepareWRRWeights().setWRRWeights(wrrWeight).get();
assertEquals(response.isAcknowledged(), true);
}

public void testUpdateWRRWeights_MoreThanOneZoneHasZeroWeight() {

Map<String, Object> weights = Map.of("a", "1", "b", "0", "c", "0");
WRRWeight wrrWeight = new WRRWeight("zone", weights);
assertThrows(
ActionRequestValidationException.class,
() -> client().admin().cluster().prepareWRRWeights().setWRRWeights(wrrWeight).get()
);
}

public void testGetWRRWeights() {

Map<String, Object> weights = Map.of("a", "1", "b", "1", "c", "1");
WRRWeight wrrWeight = new WRRWeight("zone", weights);
ClusterPutWRRWeightsResponse response = client().admin().cluster().prepareWRRWeights().setWRRWeights(wrrWeight).get();
ClusterGetWRRWeightsResponse response1 = client().admin().cluster().prepareGetWRRWeights().get();
assertEquals(response1.weights(), wrrWeight);
}
}
5 changes: 5 additions & 0 deletions server/src/main/java/org/opensearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
import org.opensearch.action.admin.cluster.settings.TransportClusterUpdateSettingsAction;
import org.opensearch.action.admin.cluster.shards.ClusterSearchShardsAction;
import org.opensearch.action.admin.cluster.shards.TransportClusterSearchShardsAction;
import org.opensearch.action.admin.cluster.shards.routing.wrr.get.ClusterGetWRRWeightsAction;
import org.opensearch.action.admin.cluster.shards.routing.wrr.put.ClusterPutWRRWeightsAction;
import org.opensearch.action.admin.cluster.shards.routing.wrr.get.TransportGetWRRWeightsAction;
import org.opensearch.action.admin.cluster.shards.routing.wrr.put.TransportPutWRRWeightsAction;
import org.opensearch.action.admin.cluster.snapshots.clone.CloneSnapshotAction;
import org.opensearch.action.admin.cluster.snapshots.clone.TransportCloneSnapshotAction;
Expand Down Expand Up @@ -293,6 +295,7 @@
import org.opensearch.rest.action.admin.cluster.RestCloneSnapshotAction;
import org.opensearch.rest.action.admin.cluster.RestClusterAllocationExplainAction;
import org.opensearch.rest.action.admin.cluster.RestClusterGetSettingsAction;
import org.opensearch.rest.action.admin.cluster.RestClusterGetWRRWeightsAction;
import org.opensearch.rest.action.admin.cluster.RestClusterHealthAction;
import org.opensearch.rest.action.admin.cluster.RestClusterPutWRRWeightsAction;
import org.opensearch.rest.action.admin.cluster.RestClusterRerouteAction;
Expand Down Expand Up @@ -561,6 +564,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg
actions.register(RestoreSnapshotAction.INSTANCE, TransportRestoreSnapshotAction.class);
actions.register(SnapshotsStatusAction.INSTANCE, TransportSnapshotsStatusAction.class);
actions.register(ClusterPutWRRWeightsAction.INSTANCE, TransportPutWRRWeightsAction.class);
actions.register(ClusterGetWRRWeightsAction.INSTANCE, TransportGetWRRWeightsAction.class);
actions.register(IndicesStatsAction.INSTANCE, TransportIndicesStatsAction.class);
actions.register(IndicesSegmentsAction.INSTANCE, TransportIndicesSegmentsAction.class);
actions.register(IndicesShardStoresAction.INSTANCE, TransportIndicesShardStoresAction.class);
Expand Down Expand Up @@ -742,6 +746,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestOpenIndexAction());
registerHandler.accept(new RestAddIndexBlockAction());
registerHandler.accept(new RestClusterPutWRRWeightsAction());
registerHandler.accept(new RestClusterGetWRRWeightsAction());

registerHandler.accept(new RestUpdateSettingsAction());
registerHandler.accept(new RestGetSettingsAction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.wrr.get;

import org.opensearch.action.ActionType;

/**
* Action to get weights for weighted round-robin search routing policy.
*
* @opensearch.internal
*/
public class ClusterGetWRRWeightsAction extends ActionType<ClusterGetWRRWeightsResponse> {
public static final ClusterGetWRRWeightsAction INSTANCE = new ClusterGetWRRWeightsAction();
public static final String NAME = "cluster:admin/routing/awareness/weights/get";

private ClusterGetWRRWeightsAction() {
super(NAME, ClusterGetWRRWeightsResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.wrr.get;

import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadRequest;
import org.opensearch.cluster.routing.WRRWeight;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.util.List;

/**
* Request to get weights for weighted round-robin search routing policy.
*
* @opensearch.internal
*/
public class ClusterGetWRRWeightsRequest extends ClusterManagerNodeReadRequest<ClusterGetWRRWeightsRequest> {
String awarenessAttribute;

public String getAwarenessAttribute() {
return awarenessAttribute;
}

public void setAwarenessAttribute(String awarenessAttribute) {
this.awarenessAttribute = awarenessAttribute;
}

public ClusterGetWRRWeightsRequest() {
}

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

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
}

@Override
public ActionRequestValidationException validate() {
return null;
}

public ClusterGetWRRWeightsRequest weights(List<WRRWeight> weights) {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.wrr.get;

import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadOperationRequestBuilder;
import org.opensearch.client.OpenSearchClient;
import org.opensearch.cluster.routing.WRRWeight;

import java.util.List;

/**
* Request builder to get weights for weighted round-robin search routing policy.
*
* @opensearch.internal
*/
public class ClusterGetWRRWeightsRequestBuilder extends ClusterManagerNodeReadOperationRequestBuilder<
ClusterGetWRRWeightsRequest,
ClusterGetWRRWeightsResponse,
ClusterGetWRRWeightsRequestBuilder> {

public ClusterGetWRRWeightsRequestBuilder(OpenSearchClient client, ClusterGetWRRWeightsAction action) {
super(client, action, new ClusterGetWRRWeightsRequest());
}

public ClusterGetWRRWeightsRequestBuilder setWeights(List<WRRWeight> weights) {
request.weights(weights);
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.wrr.get;

import org.opensearch.action.ActionResponse;

import org.opensearch.cluster.metadata.WeightedRoundRobinMetadata;
import org.opensearch.cluster.routing.WRRWeight;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Map;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* Response from fetching weights for weighted round-robin search routing policy.
*
* @opensearch.internal
*/
public class ClusterGetWRRWeightsResponse extends ActionResponse implements ToXContentObject {
private WRRWeight wrrWeight;
private Object localNodeWeight;

ClusterGetWRRWeightsResponse() {
this.wrrWeight = null;
}

ClusterGetWRRWeightsResponse(Object localNodeWeight, WRRWeight wrrWeight) {
this.localNodeWeight = localNodeWeight;
this.wrrWeight = wrrWeight;

}

ClusterGetWRRWeightsResponse(WRRWeight wrrWeights) {
this.wrrWeight = wrrWeights;
}

ClusterGetWRRWeightsResponse(WeightedRoundRobinMetadata metadata) {

this.wrrWeight = metadata.getWrrWeight();
}

ClusterGetWRRWeightsResponse(StreamInput in) throws IOException {
if (in.available() != 0) {
this.wrrWeight = new WRRWeight(in);

}
}

/**
* List of weights to return
*
* @return list or weights
*/
public WRRWeight weights() {
return this.wrrWeight;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
if (wrrWeight != null) {
wrrWeight.writeTo(out);

}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (this.wrrWeight != null) {
for (Map.Entry<String, Object> entry : wrrWeight.weights().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
if (localNodeWeight != null) {
builder.field("node_weight", localNodeWeight.toString());
}
} else if (localNodeWeight != null) {
builder.field("node_weight", localNodeWeight.toString());
} else {
builder.field("msg", "Weights are not set");
}

builder.endObject();

return builder;
}

public static ClusterGetWRRWeightsResponse fromXContent(XContentParser parser) throws IOException {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return new ClusterGetWRRWeightsResponse(WeightedRoundRobinMetadata.fromXContent(parser));
}
}
Loading

0 comments on commit 1f2c535

Please sign in to comment.