Skip to content

Commit 1cc4f20

Browse files
committed
Fix index creation bugs using context
Signed-off-by: Mohit Godwani <[email protected]>
1 parent e2363aa commit 1cc4f20

File tree

5 files changed

+112
-61
lines changed

5 files changed

+112
-61
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Class encapsulating the context metadata associated with an index template/index.
2727
*/
2828
@ExperimentalApi
29-
public class Context extends AbstractDiffable<ComposableIndexTemplate> implements ToXContentObject {
29+
public class Context extends AbstractDiffable<Context> implements ToXContentObject {
3030

3131
private static final ParseField NAME = new ParseField("name");
3232
private static final ParseField VERSION = new ParseField("version");
@@ -103,9 +103,9 @@ public void writeTo(StreamOutput out) throws IOException {
103103
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
104104
builder.startObject();
105105
builder.field(NAME.getPreferredName(), this.name);
106-
builder.field("version", this.version);
106+
builder.field(VERSION.getPreferredName(), this.version);
107107
if (params != null) {
108-
builder.field("params", this.params);
108+
builder.field(PARAMS.getPreferredName(), this.params);
109109
}
110110
builder.endObject();
111111
return builder;

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ public static APIBlock readFrom(StreamInput input) throws IOException {
639639
public static final String KEY_PRIMARY_TERMS = "primary_terms";
640640
public static final String REMOTE_STORE_CUSTOM_KEY = "remote_store";
641641
public static final String TRANSLOG_METADATA_KEY = "translog_metadata";
642+
public static final String CONTEXT_KEY = "context";
642643

643644
public static final String INDEX_STATE_FILE_PREFIX = "state-";
644645

@@ -689,6 +690,8 @@ public static APIBlock readFrom(StreamInput input) throws IOException {
689690

690691
private final int indexTotalShardsPerNodeLimit;
691692

693+
private final Context context;
694+
692695
private IndexMetadata(
693696
final Index index,
694697
final long version,
@@ -715,7 +718,8 @@ private IndexMetadata(
715718
final ActiveShardCount waitForActiveShards,
716719
final Map<String, RolloverInfo> rolloverInfos,
717720
final boolean isSystem,
718-
final int indexTotalShardsPerNodeLimit
721+
final int indexTotalShardsPerNodeLimit,
722+
final Context context
719723
) {
720724

721725
this.index = index;
@@ -751,6 +755,7 @@ private IndexMetadata(
751755
this.isSystem = isSystem;
752756
this.isRemoteSnapshot = IndexModule.Type.REMOTE_SNAPSHOT.match(this.settings);
753757
this.indexTotalShardsPerNodeLimit = indexTotalShardsPerNodeLimit;
758+
this.context = context;
754759
assert numberOfShards * routingFactor == routingNumShards : routingNumShards + " must be a multiple of " + numberOfShards;
755760
}
756761

@@ -1041,6 +1046,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
10411046
private final Diff<Map<Integer, Set<String>>> inSyncAllocationIds;
10421047
private final Diff<Map<String, RolloverInfo>> rolloverInfos;
10431048
private final boolean isSystem;
1049+
private final Context context;
10441050

10451051
IndexMetadataDiff(IndexMetadata before, IndexMetadata after) {
10461052
index = after.index.getName();
@@ -1063,6 +1069,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
10631069
);
10641070
rolloverInfos = DiffableUtils.diff(before.rolloverInfos, after.rolloverInfos, DiffableUtils.getStringKeySerializer());
10651071
isSystem = after.isSystem;
1072+
context = after.context;
10661073
}
10671074

10681075
private static final DiffableUtils.DiffableValueReader<String, AliasMetadata> ALIAS_METADATA_DIFF_VALUE_READER =
@@ -1094,6 +1101,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
10941101
);
10951102
rolloverInfos = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), ROLLOVER_INFO_DIFF_VALUE_READER);
10961103
isSystem = in.readBoolean();
1104+
context = in.readOptionalWriteable(Context::new);
10971105
}
10981106

10991107
@Override
@@ -1113,6 +1121,9 @@ public void writeTo(StreamOutput out) throws IOException {
11131121
inSyncAllocationIds.writeTo(out);
11141122
rolloverInfos.writeTo(out);
11151123
out.writeBoolean(isSystem);
1124+
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
1125+
out.writeOptionalWriteable(context);
1126+
}
11161127
}
11171128

11181129
@Override
@@ -1132,6 +1143,7 @@ public IndexMetadata apply(IndexMetadata part) {
11321143
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
11331144
builder.rolloverInfos.putAll(rolloverInfos.apply(part.rolloverInfos));
11341145
builder.system(part.isSystem);
1146+
builder.context(context);
11351147
return builder.build();
11361148
}
11371149
}
@@ -1173,6 +1185,10 @@ public static IndexMetadata readFrom(StreamInput in) throws IOException {
11731185
builder.putRolloverInfo(new RolloverInfo(in));
11741186
}
11751187
builder.system(in.readBoolean());
1188+
1189+
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
1190+
builder.context(in.readOptionalWriteable(Context::new));
1191+
}
11761192
return builder.build();
11771193
}
11781194

@@ -1210,6 +1226,10 @@ public void writeTo(StreamOutput out) throws IOException {
12101226
cursor.writeTo(out);
12111227
}
12121228
out.writeBoolean(isSystem);
1229+
1230+
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
1231+
out.writeOptionalWriteable(context);
1232+
}
12131233
}
12141234

12151235
public boolean isSystem() {
@@ -1251,6 +1271,7 @@ public static class Builder {
12511271
private final Map<String, RolloverInfo> rolloverInfos;
12521272
private Integer routingNumShards;
12531273
private boolean isSystem;
1274+
private Context context;
12541275

12551276
public Builder(String index) {
12561277
this.index = index;
@@ -1278,6 +1299,7 @@ public Builder(IndexMetadata indexMetadata) {
12781299
this.inSyncAllocationIds = new HashMap<>(indexMetadata.inSyncAllocationIds);
12791300
this.rolloverInfos = new HashMap<>(indexMetadata.rolloverInfos);
12801301
this.isSystem = indexMetadata.isSystem;
1302+
this.context = indexMetadata.context;
12811303
}
12821304

12831305
public Builder index(String index) {
@@ -1494,6 +1516,15 @@ public boolean isSystem() {
14941516
return isSystem;
14951517
}
14961518

1519+
public Builder context(Context context) {
1520+
this.context = context;
1521+
return this;
1522+
}
1523+
1524+
public Context context() {
1525+
return context;
1526+
}
1527+
14971528
public IndexMetadata build() {
14981529
final Map<String, AliasMetadata> tmpAliases = aliases;
14991530
Settings tmpSettings = settings;
@@ -1622,7 +1653,8 @@ public IndexMetadata build() {
16221653
waitForActiveShards,
16231654
rolloverInfos,
16241655
isSystem,
1625-
indexTotalShardsPerNodeLimit
1656+
indexTotalShardsPerNodeLimit,
1657+
context
16261658
);
16271659
}
16281660

@@ -1725,6 +1757,11 @@ public static void toXContent(IndexMetadata indexMetadata, XContentBuilder build
17251757
builder.endObject();
17261758
builder.field(KEY_SYSTEM, indexMetadata.isSystem);
17271759

1760+
if (indexMetadata.context != null) {
1761+
builder.field(CONTEXT_KEY);
1762+
indexMetadata.context.toXContent(builder, params);
1763+
}
1764+
17281765
builder.endObject();
17291766
}
17301767

@@ -1806,6 +1843,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
18061843
// simply ignored when upgrading from 2.x
18071844
assert Version.CURRENT.major <= 5;
18081845
parser.skipChildren();
1846+
} else if (CONTEXT_KEY.equals(currentFieldName)) {
1847+
builder.context(Context.fromXContent(parser));
18091848
} else {
18101849
// assume it's custom index metadata
18111850
builder.putCustom(currentFieldName, parser.mapStrings());

0 commit comments

Comments
 (0)