Skip to content

Commit d199096

Browse files
authored
Ensure consistency of system flag on IndexMetadata after diff is applied (#16644)
* Ensure consistency of system flag on IndexMetadata after diff is applied Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent b1bf72f commit d199096

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6161
- Fix stale cluster state custom file deletion ([#16670](https://github.com/opensearch-project/OpenSearch/pull/16670))
6262
- [Tiered Caching] Fix bug in cache stats API ([#16560](https://github.com/opensearch-project/OpenSearch/pull/16560))
6363
- Bound the size of cache in deprecation logger ([16702](https://github.com/opensearch-project/OpenSearch/issues/16702))
64+
- Ensure consistency of system flag on IndexMetadata after diff is applied ([#16644](https://github.com/opensearch-project/OpenSearch/pull/16644))
6465

6566
### Security
6667

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
10611061
*
10621062
* @opensearch.internal
10631063
*/
1064-
private static class IndexMetadataDiff implements Diff<IndexMetadata> {
1064+
static class IndexMetadataDiff implements Diff<IndexMetadata> {
10651065

10661066
private final String index;
10671067
private final int routingNumShards;
@@ -1178,7 +1178,7 @@ public IndexMetadata apply(IndexMetadata part) {
11781178
builder.customMetadata.putAll(customData.apply(part.customData));
11791179
builder.inSyncAllocationIds.putAll(inSyncAllocationIds.apply(part.inSyncAllocationIds));
11801180
builder.rolloverInfos.putAll(rolloverInfos.apply(part.rolloverInfos));
1181-
builder.system(part.isSystem);
1181+
builder.system(isSystem);
11821182
builder.context(context);
11831183
return builder.build();
11841184
}

server/src/test/java/org/opensearch/cluster/metadata/IndexMetadataTests.java

+38
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232

3333
package org.opensearch.cluster.metadata;
3434

35+
import org.opensearch.Version;
3536
import org.opensearch.action.admin.indices.rollover.MaxAgeCondition;
3637
import org.opensearch.action.admin.indices.rollover.MaxDocsCondition;
3738
import org.opensearch.action.admin.indices.rollover.MaxSizeCondition;
3839
import org.opensearch.action.admin.indices.rollover.RolloverInfo;
40+
import org.opensearch.cluster.Diff;
41+
import org.opensearch.common.UUIDs;
3942
import org.opensearch.common.io.stream.BytesStreamOutput;
4043
import org.opensearch.common.settings.Settings;
4144
import org.opensearch.common.unit.TimeValue;
@@ -48,6 +51,7 @@
4851
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
4952
import org.opensearch.core.common.io.stream.StreamInput;
5053
import org.opensearch.core.common.unit.ByteSizeValue;
54+
import org.opensearch.core.index.Index;
5155
import org.opensearch.core.index.shard.ShardId;
5256
import org.opensearch.core.xcontent.MediaTypeRegistry;
5357
import org.opensearch.core.xcontent.NamedXContentRegistry;
@@ -88,6 +92,26 @@ protected NamedXContentRegistry xContentRegistry() {
8892
return new NamedXContentRegistry(IndicesModule.getNamedXContents());
8993
}
9094

95+
// Create the index metadata for a given index, with the specified version.
96+
private static IndexMetadata createIndexMetadata(final Index index, final long version) {
97+
return createIndexMetadata(index, version, false);
98+
}
99+
100+
private static IndexMetadata createIndexMetadata(final Index index, final long version, final boolean isSystem) {
101+
final Settings settings = Settings.builder()
102+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
103+
.put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID())
104+
.build();
105+
return IndexMetadata.builder(index.getName())
106+
.settings(settings)
107+
.numberOfShards(1)
108+
.numberOfReplicas(0)
109+
.creationDate(System.currentTimeMillis())
110+
.version(version)
111+
.system(isSystem)
112+
.build();
113+
}
114+
91115
public void testIndexMetadataSerialization() throws IOException {
92116
Integer numShard = randomFrom(1, 2, 4, 8, 16);
93117
int numberOfReplicas = randomIntBetween(0, 10);
@@ -568,4 +592,18 @@ public void testParseIndexNameCannotFormatNumber() {
568592
}
569593
}
570594

595+
/**
596+
* Test that changes to indices metadata are applied
597+
*/
598+
public void testIndicesMetadataDiffSystemFlagFlipped() {
599+
String indexUuid = UUIDs.randomBase64UUID();
600+
Index index = new Index("test-index", indexUuid);
601+
IndexMetadata previousIndexMetadata = createIndexMetadata(index, 1);
602+
IndexMetadata nextIndexMetadata = createIndexMetadata(index, 2, true);
603+
Diff<IndexMetadata> diff = new IndexMetadata.IndexMetadataDiff(previousIndexMetadata, nextIndexMetadata);
604+
IndexMetadata indexMetadataAfterDiffApplied = diff.apply(previousIndexMetadata);
605+
assertTrue(indexMetadataAfterDiffApplied.isSystem());
606+
assertThat(indexMetadataAfterDiffApplied.getVersion(), equalTo(nextIndexMetadata.getVersion()));
607+
}
608+
571609
}

0 commit comments

Comments
 (0)