Skip to content

Commit 8d02fc7

Browse files
vikasvb90andrross
authored andcommitted
Adding metadata to support shard split and to resolve shard ids based on child shard hash
Signed-off-by: vikasvb90 <vikasvb@amazon.com>
1 parent cfebc67 commit 8d02fc7

File tree

10 files changed

+2832
-36
lines changed

10 files changed

+2832
-36
lines changed

server/src/main/java/org/opensearch/cluster/metadata/IndexMetadata.java

Lines changed: 184 additions & 34 deletions
Large diffs are not rendered by default.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.cluster.metadata;
10+
11+
import org.opensearch.common.annotation.ExperimentalApi;
12+
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.core.common.io.stream.StreamOutput;
14+
import org.opensearch.core.common.io.stream.Writeable;
15+
import org.opensearch.core.xcontent.ToXContentFragment;
16+
import org.opensearch.core.xcontent.XContentBuilder;
17+
import org.opensearch.core.xcontent.XContentParser;
18+
19+
import java.io.IOException;
20+
21+
/**
22+
* Represents the hash range assigned to a shard.
23+
*
24+
* @opensearch.experimental
25+
*/
26+
@ExperimentalApi
27+
public record ShardRange(int shardId, int start, int end) implements Comparable<ShardRange>, ToXContentFragment, Writeable {
28+
29+
/**
30+
* Constructs a new shard range from a stream.
31+
* @param in the stream to read from
32+
* @throws IOException if an error occurs while reading from the stream
33+
* @see #writeTo(StreamOutput)
34+
*/
35+
public ShardRange(StreamInput in) throws IOException {
36+
this(in.readVInt(), in.readInt(), in.readInt());
37+
}
38+
39+
public boolean contains(int hash) {
40+
return hash >= start && hash <= end;
41+
}
42+
43+
@Override
44+
public int compareTo(ShardRange o) {
45+
return Integer.compare(start, o.start);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "ShardRange{" + "shardId=" + shardId + ", start=" + start + ", end=" + end + '}';
51+
}
52+
53+
@Override
54+
public void writeTo(StreamOutput out) throws IOException {
55+
out.writeVInt(shardId);
56+
out.writeInt(start);
57+
out.writeInt(end);
58+
}
59+
60+
@Override
61+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
62+
builder.startObject().field("shard_id", shardId).field("start", start).field("end", end);
63+
builder.endObject();
64+
return builder;
65+
}
66+
67+
public static ShardRange parse(XContentParser parser) throws IOException {
68+
int shardId = -1, start = -1, end = -1;
69+
XContentParser.Token token;
70+
String fieldName = null;
71+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
72+
if (token == XContentParser.Token.FIELD_NAME) {
73+
fieldName = parser.currentName();
74+
} else if (token == XContentParser.Token.VALUE_NUMBER) {
75+
if ("shard_id".equals(fieldName)) {
76+
shardId = parser.intValue();
77+
} else if ("start".equals(fieldName)) {
78+
start = parser.intValue();
79+
} else if ("end".equals(fieldName)) {
80+
end = parser.intValue();
81+
}
82+
}
83+
}
84+
85+
return new ShardRange(shardId, start, end);
86+
}
87+
}

0 commit comments

Comments
 (0)