-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GET api to get weighted round robin shard routing weights
Signed-off-by: Anshu Agarwal <anshukag@amazon.com>
- Loading branch information
Anshu Agarwal
committed
Aug 22, 2022
1 parent
026d3ab
commit 1f2c535
Showing
12 changed files
with
512 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
rest-api-spec/src/main/resources/rest-api-spec/api/cluster.get_wrr_weights.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
server/src/internalClusterTest/java/org/opensearch/cluster/routing/WRRRoutingIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...rg/opensearch/action/admin/cluster/shards/routing/wrr/get/ClusterGetWRRWeightsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...g/opensearch/action/admin/cluster/shards/routing/wrr/get/ClusterGetWRRWeightsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...earch/action/admin/cluster/shards/routing/wrr/get/ClusterGetWRRWeightsRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
.../opensearch/action/admin/cluster/shards/routing/wrr/get/ClusterGetWRRWeightsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.