Skip to content

Commit 7884e1d

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

File tree

5 files changed

+114
-61
lines changed

5 files changed

+114
-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: 43 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,9 @@ 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+
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
1105+
context = in.readOptionalWriteable(Context::new);
1106+
}
10971107
}
10981108

10991109
@Override
@@ -1113,6 +1123,9 @@ public void writeTo(StreamOutput out) throws IOException {
11131123
inSyncAllocationIds.writeTo(out);
11141124
rolloverInfos.writeTo(out);
11151125
out.writeBoolean(isSystem);
1126+
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
1127+
out.writeOptionalWriteable(context);
1128+
}
11161129
}
11171130

11181131
@Override
@@ -1132,6 +1145,7 @@ public IndexMetadata apply(IndexMetadata part) {
11321145
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
11331146
builder.rolloverInfos.putAll(rolloverInfos.apply(part.rolloverInfos));
11341147
builder.system(part.isSystem);
1148+
builder.context(context);
11351149
return builder.build();
11361150
}
11371151
}
@@ -1173,6 +1187,10 @@ public static IndexMetadata readFrom(StreamInput in) throws IOException {
11731187
builder.putRolloverInfo(new RolloverInfo(in));
11741188
}
11751189
builder.system(in.readBoolean());
1190+
1191+
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
1192+
builder.context(in.readOptionalWriteable(Context::new));
1193+
}
11761194
return builder.build();
11771195
}
11781196

@@ -1210,6 +1228,10 @@ public void writeTo(StreamOutput out) throws IOException {
12101228
cursor.writeTo(out);
12111229
}
12121230
out.writeBoolean(isSystem);
1231+
1232+
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
1233+
out.writeOptionalWriteable(context);
1234+
}
12131235
}
12141236

12151237
public boolean isSystem() {
@@ -1251,6 +1273,7 @@ public static class Builder {
12511273
private final Map<String, RolloverInfo> rolloverInfos;
12521274
private Integer routingNumShards;
12531275
private boolean isSystem;
1276+
private Context context;
12541277

12551278
public Builder(String index) {
12561279
this.index = index;
@@ -1278,6 +1301,7 @@ public Builder(IndexMetadata indexMetadata) {
12781301
this.inSyncAllocationIds = new HashMap<>(indexMetadata.inSyncAllocationIds);
12791302
this.rolloverInfos = new HashMap<>(indexMetadata.rolloverInfos);
12801303
this.isSystem = indexMetadata.isSystem;
1304+
this.context = indexMetadata.context;
12811305
}
12821306

12831307
public Builder index(String index) {
@@ -1494,6 +1518,15 @@ public boolean isSystem() {
14941518
return isSystem;
14951519
}
14961520

1521+
public Builder context(Context context) {
1522+
this.context = context;
1523+
return this;
1524+
}
1525+
1526+
public Context context() {
1527+
return context;
1528+
}
1529+
14971530
public IndexMetadata build() {
14981531
final Map<String, AliasMetadata> tmpAliases = aliases;
14991532
Settings tmpSettings = settings;
@@ -1622,7 +1655,8 @@ public IndexMetadata build() {
16221655
waitForActiveShards,
16231656
rolloverInfos,
16241657
isSystem,
1625-
indexTotalShardsPerNodeLimit
1658+
indexTotalShardsPerNodeLimit,
1659+
context
16261660
);
16271661
}
16281662

@@ -1725,6 +1759,11 @@ public static void toXContent(IndexMetadata indexMetadata, XContentBuilder build
17251759
builder.endObject();
17261760
builder.field(KEY_SYSTEM, indexMetadata.isSystem);
17271761

1762+
if (indexMetadata.context != null) {
1763+
builder.field(CONTEXT_KEY);
1764+
indexMetadata.context.toXContent(builder, params);
1765+
}
1766+
17281767
builder.endObject();
17291768
}
17301769

@@ -1806,6 +1845,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
18061845
// simply ignored when upgrading from 2.x
18071846
assert Version.CURRENT.major <= 5;
18081847
parser.skipChildren();
1848+
} else if (CONTEXT_KEY.equals(currentFieldName)) {
1849+
builder.context(Context.fromXContent(parser));
18091850
} else {
18101851
// assume it's custom index metadata
18111852
builder.putCustom(currentFieldName, parser.mapStrings());

0 commit comments

Comments
 (0)