Skip to content

Commit

Permalink
Fix index creation bugs using context
Browse files Browse the repository at this point in the history
  • Loading branch information
mgodwan committed Aug 20, 2024
1 parent e2363aa commit 600acc1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Class encapsulating the context metadata associated with an index template/index.
*/
@ExperimentalApi
public class Context extends AbstractDiffable<ComposableIndexTemplate> implements ToXContentObject {
public class Context extends AbstractDiffable<Context> implements ToXContentObject {

private static final ParseField NAME = new ParseField("name");
private static final ParseField VERSION = new ParseField("version");
Expand Down Expand Up @@ -103,9 +103,9 @@ public void writeTo(StreamOutput out) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(NAME.getPreferredName(), this.name);
builder.field("version", this.version);
builder.field(VERSION.getPreferredName(), this.version);
if (params != null) {
builder.field("params", this.params);
builder.field(PARAMS.getPreferredName(), this.params);
}
builder.endObject();
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ public static APIBlock readFrom(StreamInput input) throws IOException {
public static final String KEY_PRIMARY_TERMS = "primary_terms";
public static final String REMOTE_STORE_CUSTOM_KEY = "remote_store";
public static final String TRANSLOG_METADATA_KEY = "translog_metadata";
public static final String CONTEXT_KEY = "context";

public static final String INDEX_STATE_FILE_PREFIX = "state-";

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

private final int indexTotalShardsPerNodeLimit;

private final Context context;

private IndexMetadata(
final Index index,
final long version,
Expand All @@ -715,7 +718,8 @@ private IndexMetadata(
final ActiveShardCount waitForActiveShards,
final Map<String, RolloverInfo> rolloverInfos,
final boolean isSystem,
final int indexTotalShardsPerNodeLimit
final int indexTotalShardsPerNodeLimit,
final Context context
) {

this.index = index;
Expand Down Expand Up @@ -751,6 +755,7 @@ private IndexMetadata(
this.isSystem = isSystem;
this.isRemoteSnapshot = IndexModule.Type.REMOTE_SNAPSHOT.match(this.settings);
this.indexTotalShardsPerNodeLimit = indexTotalShardsPerNodeLimit;
this.context = context;
assert numberOfShards * routingFactor == routingNumShards : routingNumShards + " must be a multiple of " + numberOfShards;
}

Expand Down Expand Up @@ -1041,6 +1046,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
private final Diff<Map<Integer, Set<String>>> inSyncAllocationIds;
private final Diff<Map<String, RolloverInfo>> rolloverInfos;
private final boolean isSystem;
private final Context context;

IndexMetadataDiff(IndexMetadata before, IndexMetadata after) {
index = after.index.getName();
Expand All @@ -1063,6 +1069,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> {
);
rolloverInfos = DiffableUtils.diff(before.rolloverInfos, after.rolloverInfos, DiffableUtils.getStringKeySerializer());
isSystem = after.isSystem;
context = after.context;
}

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

@Override
Expand All @@ -1113,6 +1121,9 @@ public void writeTo(StreamOutput out) throws IOException {
inSyncAllocationIds.writeTo(out);
rolloverInfos.writeTo(out);
out.writeBoolean(isSystem);
if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
out.writeOptionalWriteable(context);
}
}

@Override
Expand All @@ -1132,6 +1143,7 @@ public IndexMetadata apply(IndexMetadata part) {
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
builder.rolloverInfos.putAll(rolloverInfos.apply(part.rolloverInfos));
builder.system(part.isSystem);
builder.context(context);
return builder.build();
}
}
Expand Down Expand Up @@ -1173,6 +1185,10 @@ public static IndexMetadata readFrom(StreamInput in) throws IOException {
builder.putRolloverInfo(new RolloverInfo(in));
}
builder.system(in.readBoolean());

if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
builder.context(in.readOptionalWriteable(Context::new));
}
return builder.build();
}

Expand Down Expand Up @@ -1210,6 +1226,10 @@ public void writeTo(StreamOutput out) throws IOException {
cursor.writeTo(out);
}
out.writeBoolean(isSystem);

if (out.getVersion().onOrAfter(Version.V_3_0_0)) {
out.writeOptionalWriteable(context);
}
}

public boolean isSystem() {
Expand Down Expand Up @@ -1251,6 +1271,7 @@ public static class Builder {
private final Map<String, RolloverInfo> rolloverInfos;
private Integer routingNumShards;
private boolean isSystem;
private Context context;

public Builder(String index) {
this.index = index;
Expand Down Expand Up @@ -1278,6 +1299,7 @@ public Builder(IndexMetadata indexMetadata) {
this.inSyncAllocationIds = new HashMap<>(indexMetadata.inSyncAllocationIds);
this.rolloverInfos = new HashMap<>(indexMetadata.rolloverInfos);
this.isSystem = indexMetadata.isSystem;
this.context = indexMetadata.context;
}

public Builder index(String index) {
Expand Down Expand Up @@ -1494,6 +1516,15 @@ public boolean isSystem() {
return isSystem;
}

public Builder context(Context context) {
this.context = context;
return this;
}

public Context context() {
return context;
}

public IndexMetadata build() {
final Map<String, AliasMetadata> tmpAliases = aliases;
Settings tmpSettings = settings;
Expand Down Expand Up @@ -1622,7 +1653,8 @@ public IndexMetadata build() {
waitForActiveShards,
rolloverInfos,
isSystem,
indexTotalShardsPerNodeLimit
indexTotalShardsPerNodeLimit,
context
);
}

Expand Down Expand Up @@ -1725,6 +1757,10 @@ public static void toXContent(IndexMetadata indexMetadata, XContentBuilder build
builder.endObject();
builder.field(KEY_SYSTEM, indexMetadata.isSystem);

if (indexMetadata.context != null) {
indexMetadata.context.toXContent(builder, params);
}

builder.endObject();
}

Expand Down Expand Up @@ -1806,6 +1842,8 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
// simply ignored when upgrading from 2.x
assert Version.CURRENT.major <= 5;
parser.skipChildren();
} else if (CONTEXT_KEY.equals(currentFieldName)) {
builder.context(Context.fromXContent(parser));
} else {
// assume it's custom index metadata
builder.putCustom(currentFieldName, parser.mapStrings());
Expand Down
Loading

0 comments on commit 600acc1

Please sign in to comment.