|
32 | 32 |
|
33 | 33 | package org.opensearch.cluster.metadata; |
34 | 34 |
|
| 35 | +import org.opensearch.Version; |
35 | 36 | import org.opensearch.action.admin.indices.rollover.MaxAgeCondition; |
36 | 37 | import org.opensearch.action.admin.indices.rollover.MaxDocsCondition; |
37 | 38 | import org.opensearch.action.admin.indices.rollover.MaxSizeCondition; |
38 | 39 | import org.opensearch.action.admin.indices.rollover.RolloverInfo; |
| 40 | +import org.opensearch.cluster.Diff; |
| 41 | +import org.opensearch.common.UUIDs; |
39 | 42 | import org.opensearch.common.io.stream.BytesStreamOutput; |
40 | 43 | import org.opensearch.common.settings.Settings; |
41 | 44 | import org.opensearch.common.unit.TimeValue; |
|
48 | 51 | import org.opensearch.core.common.io.stream.NamedWriteableRegistry; |
49 | 52 | import org.opensearch.core.common.io.stream.StreamInput; |
50 | 53 | import org.opensearch.core.common.unit.ByteSizeValue; |
| 54 | +import org.opensearch.core.index.Index; |
51 | 55 | import org.opensearch.core.index.shard.ShardId; |
52 | 56 | import org.opensearch.core.xcontent.MediaTypeRegistry; |
53 | 57 | import org.opensearch.core.xcontent.NamedXContentRegistry; |
@@ -88,6 +92,26 @@ protected NamedXContentRegistry xContentRegistry() { |
88 | 92 | return new NamedXContentRegistry(IndicesModule.getNamedXContents()); |
89 | 93 | } |
90 | 94 |
|
| 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 | + |
91 | 115 | public void testIndexMetadataSerialization() throws IOException { |
92 | 116 | Integer numShard = randomFrom(1, 2, 4, 8, 16); |
93 | 117 | int numberOfReplicas = randomIntBetween(0, 10); |
@@ -568,4 +592,18 @@ public void testParseIndexNameCannotFormatNumber() { |
568 | 592 | } |
569 | 593 | } |
570 | 594 |
|
| 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 | + |
571 | 609 | } |
0 commit comments