Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure consistency of system flag on IndexMetadata after diff is applied #16644

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
*
* @opensearch.internal
*/
private static class IndexMetadataDiff implements Diff<IndexMetadata> {
static class IndexMetadataDiff implements Diff<IndexMetadata> {

private final String index;
private final int routingNumShards;
Expand Down Expand Up @@ -1178,7 +1178,7 @@ public IndexMetadata apply(IndexMetadata part) {
builder.customMetadata.putAll(customData.apply(part.customData));
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
builder.rolloverInfos.putAll(rolloverInfos.apply(part.rolloverInfos));
builder.system(part.isSystem);
builder.system(isSystem);
builder.context(context);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@

package org.opensearch.cluster.metadata;

import org.opensearch.Version;
import org.opensearch.action.admin.indices.rollover.MaxAgeCondition;
import org.opensearch.action.admin.indices.rollover.MaxDocsCondition;
import org.opensearch.action.admin.indices.rollover.MaxSizeCondition;
import org.opensearch.action.admin.indices.rollover.RolloverInfo;
import org.opensearch.cluster.Diff;
import org.opensearch.common.UUIDs;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
Expand All @@ -48,6 +51,7 @@
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.core.index.Index;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -88,6 +92,36 @@ protected NamedXContentRegistry xContentRegistry() {
return new NamedXContentRegistry(IndicesModule.getNamedXContents());
}

// Create the index metadata for a given index, with the specified version.
private static IndexMetadata createIndexMetadata(final Index index, final long version) {
final Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID())
.build();
return IndexMetadata.builder(index.getName())
.settings(settings)
.numberOfShards(1)
.numberOfReplicas(0)
.creationDate(System.currentTimeMillis())
.version(version)
.build();
}

private static IndexMetadata createIndexMetadata(final Index index, final long version, final boolean isSystem) {
final Settings settings = Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID())
.build();
return IndexMetadata.builder(index.getName())
.settings(settings)
.numberOfShards(1)
.numberOfReplicas(0)
.creationDate(System.currentTimeMillis())
.version(version)
.system(isSystem)
.build();
}

public void testIndexMetadataSerialization() throws IOException {
Integer numShard = randomFrom(1, 2, 4, 8, 16);
int numberOfReplicas = randomIntBetween(0, 10);
Expand Down Expand Up @@ -568,4 +602,18 @@ public void testParseIndexNameCannotFormatNumber() {
}
}

/**
* Test that changes to indices metadata are applied
*/
public void testIndicesMetadataDiffSystemFlagFlipped() {
String indexUuid = UUIDs.randomBase64UUID();
Index index = new Index("test-index", indexUuid);
IndexMetadata previousIndexMetadata = createIndexMetadata(index, 1);
IndexMetadata nextIndexMetadata = createIndexMetadata(index, 2, true);
Diff<IndexMetadata> diff = new IndexMetadata.IndexMetadataDiff(previousIndexMetadata, nextIndexMetadata);
IndexMetadata indexMetadataAfterDiffApplied = diff.apply(previousIndexMetadata);
assertTrue(indexMetadataAfterDiffApplied.isSystem());
assertThat(indexMetadataAfterDiffApplied.getVersion(), equalTo(nextIndexMetadata.getVersion()));
}

}
Loading