Skip to content

Commit a79f554

Browse files
committed
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 521cef1 commit a79f554

File tree

7 files changed

+2768
-19
lines changed

7 files changed

+2768
-19
lines changed

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

Lines changed: 158 additions & 17 deletions
Large diffs are not rendered by default.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
@ExperimentalApi
22+
public class ShardRange implements Comparable<ShardRange>, ToXContentFragment, Writeable {
23+
private final int shardId;
24+
private final int start;
25+
private final int end;
26+
27+
public ShardRange(int shardId, int start, int end) {
28+
this.shardId = shardId;
29+
this.start = start;
30+
this.end = end;
31+
}
32+
33+
/**
34+
* Constructs a new shard range from a stream.
35+
* @param in the stream to read from
36+
* @throws IOException if an error occurs while reading from the stream
37+
* @see #writeTo(StreamOutput)
38+
*/
39+
public ShardRange(StreamInput in) throws IOException {
40+
shardId = in.readVInt();
41+
start = in.readInt();
42+
end = in.readInt();
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (this == o) return true;
48+
if (!(o instanceof ShardRange)) return false;
49+
50+
ShardRange that = (ShardRange) o;
51+
52+
if (shardId != that.shardId) return false;
53+
if (start != that.start) return false;
54+
return end == that.end;
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
int result = shardId;
60+
result = 31 * result + start;
61+
result = 31 * result + end;
62+
return result;
63+
}
64+
65+
public int getShardId() {
66+
return shardId;
67+
}
68+
69+
public int getStart() {
70+
return start;
71+
}
72+
73+
public int getEnd() {
74+
return end;
75+
}
76+
77+
public ShardRange copy() {
78+
return new ShardRange(shardId, start, end);
79+
}
80+
81+
public boolean contains(long hash) {
82+
return hash >= start && hash <= end;
83+
}
84+
85+
@Override
86+
public int compareTo(ShardRange o) {
87+
return Integer.compare(start, o.start);
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "ShardRange{" + "shardId=" + shardId + ", start=" + start + ", end=" + end + '}';
93+
}
94+
95+
@Override
96+
public void writeTo(StreamOutput out) throws IOException {
97+
out.writeVInt(shardId);
98+
out.writeInt(start);
99+
out.writeInt(end);
100+
}
101+
102+
@Override
103+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
104+
builder.startObject().field("shard_id", shardId).field("start", start).field("end", end);
105+
builder.endObject();
106+
return builder;
107+
}
108+
109+
public static ShardRange parse(XContentParser parser) throws IOException {
110+
int shardId = -1, start = -1, end = -1;
111+
XContentParser.Token token;
112+
String fieldName = null;
113+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
114+
if (token == XContentParser.Token.FIELD_NAME) {
115+
fieldName = parser.currentName();
116+
} else if (token == XContentParser.Token.VALUE_NUMBER) {
117+
if ("shard_id".equals(fieldName)) {
118+
shardId = parser.intValue();
119+
} else if ("start".equals(fieldName)) {
120+
start = parser.intValue();
121+
} else if ("end".equals(fieldName)) {
122+
end = parser.intValue();
123+
}
124+
}
125+
}
126+
127+
return new ShardRange(shardId, start, end);
128+
}
129+
}

0 commit comments

Comments
 (0)