-
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.
Weighted round-robin scheduling policy for shard coordination traffic…
… routing Signed-off-by: Anshu Agarwal <anshukag@amazon.com>
- Loading branch information
Anshu Agarwal
committed
Aug 17, 2022
1 parent
280b938
commit c2eee63
Showing
7 changed files
with
671 additions
and
9 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
server/src/main/java/org/opensearch/cluster/metadata/WeightedRoundRobinMetadata.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,157 @@ | ||
/* | ||
* 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.metadata; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.OpenSearchParseException; | ||
import org.opensearch.Version; | ||
import org.opensearch.cluster.AbstractNamedDiffable; | ||
import org.opensearch.cluster.NamedDiff; | ||
import org.opensearch.cluster.routing.WRRWeight; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.xcontent.ToXContent; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Contains metadata for weighted round-robin shard routing weights | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class WeightedRoundRobinMetadata extends AbstractNamedDiffable<Metadata.Custom> implements Metadata.Custom { | ||
private static final Logger logger = LogManager.getLogger(WeightedRoundRobinMetadata.class); | ||
public static final String TYPE = "wrr_shard_routing"; | ||
private WRRWeight wrrWeight; | ||
|
||
public WRRWeight getWrrWeight() { | ||
return wrrWeight; | ||
} | ||
|
||
public WeightedRoundRobinMetadata setWrrWeight(WRRWeight wrrWeight) { | ||
this.wrrWeight = wrrWeight; | ||
return this; | ||
} | ||
|
||
public WeightedRoundRobinMetadata(StreamInput in) throws IOException { | ||
this.wrrWeight = new WRRWeight(in); | ||
} | ||
|
||
public WeightedRoundRobinMetadata(WRRWeight wrrWeight) { | ||
this.wrrWeight = wrrWeight; | ||
} | ||
|
||
@Override | ||
public EnumSet<Metadata.XContentContext> context() { | ||
return Metadata.API_AND_GATEWAY; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return TYPE; | ||
} | ||
|
||
@Override | ||
public Version getMinimalSupportedVersion() { | ||
// TODO: Check if this needs to be changed | ||
return Version.CURRENT.minimumCompatibilityVersion(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
wrrWeight.writeTo(out); | ||
} | ||
|
||
public static NamedDiff<Metadata.Custom> readDiffFrom(StreamInput in) throws IOException { | ||
return readDiffFrom(Metadata.Custom.class, TYPE, in); | ||
} | ||
|
||
public static WeightedRoundRobinMetadata fromXContent(XContentParser parser) throws IOException { | ||
String attrKey = null; | ||
Object attrValue; | ||
String attributeName = null; | ||
Map<String, Object> weights = new HashMap<>(); | ||
WRRWeight wrrWeight = null; | ||
XContentParser.Token token; | ||
// move to the first alias | ||
parser.nextToken(); | ||
String awarenessField = null; | ||
|
||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
awarenessField = parser.currentName(); | ||
if (parser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
throw new OpenSearchParseException("failed to parse wrr metadata [{}], expected object", awarenessField); | ||
} | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
attributeName = parser.currentName(); | ||
if (parser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
throw new OpenSearchParseException("failed to parse wrr metadata [{}], expected object", attributeName); | ||
} | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
attrKey = parser.currentName(); | ||
} else if (token == XContentParser.Token.VALUE_STRING) { | ||
attrValue = parser.text(); | ||
weights.put(attrKey, attrValue); | ||
} else { | ||
throw new OpenSearchParseException("failed to parse wrr metadata attribute [{}], unknown type", attributeName); | ||
} | ||
} | ||
} | ||
} else { | ||
throw new OpenSearchParseException("failed to parse wrr metadata attribute [{}]", attributeName); | ||
} | ||
} | ||
wrrWeight = new WRRWeight(attributeName, weights); | ||
return new WeightedRoundRobinMetadata(wrrWeight); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
WeightedRoundRobinMetadata that = (WeightedRoundRobinMetadata) o; | ||
return wrrWeight.equals(that.wrrWeight); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return wrrWeight.hashCode(); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
toXContent(wrrWeight, builder); | ||
return builder; | ||
} | ||
|
||
public static void toXContent(WRRWeight wrrWeight, XContentBuilder builder) throws IOException { | ||
builder.startObject("awareness"); | ||
builder.startObject(wrrWeight.attributeName()); | ||
for (Map.Entry<String, Object> entry : wrrWeight.weights().entrySet()) { | ||
builder.field(entry.getKey(), entry.getValue()); | ||
} | ||
builder.endObject(); | ||
builder.endObject(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.