diff --git a/server/src/main/java/org/opensearch/index/IndexSettings.java b/server/src/main/java/org/opensearch/index/IndexSettings.java index 878523226f23e..3a4c60167754f 100644 --- a/server/src/main/java/org/opensearch/index/IndexSettings.java +++ b/server/src/main/java/org/opensearch/index/IndexSettings.java @@ -1036,7 +1036,8 @@ public boolean isRemoteSnapshot() { /** * If this is a remote snapshot and the extended compatibility * feature flag is enabled, this returns the minimum {@link Version} - * supported. In all other cases, the return value is null. + * supported. In all other cases, the return value is the + * {@link Version#minimumIndexCompatibilityVersion()} of {@link Version#CURRENT}. */ public Version getExtendedCompatibilitySnapshotVersion() { return extendedCompatibilitySnapshotVersion; diff --git a/server/src/test/java/org/opensearch/repositories/IndexMetadataGenerationsTests.java b/server/src/test/java/org/opensearch/repositories/IndexMetadataGenerationsTests.java new file mode 100644 index 0000000000000..fda330ba7a7f3 --- /dev/null +++ b/server/src/test/java/org/opensearch/repositories/IndexMetadataGenerationsTests.java @@ -0,0 +1,99 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.repositories; + +import org.junit.Before; +import org.opensearch.snapshots.SnapshotId; +import org.opensearch.test.OpenSearchTestCase; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class IndexMetadataGenerationsTests extends OpenSearchTestCase { + + private final int MAX_TEST_INDICES = 10; + private final String SNAPSHOT = "snapshot"; + private final String INDEX_PREFIX = "index-"; + private final String BLOB_ID_PREFIX = "blob-"; + private IndexMetaDataGenerations indexMetaDataGenerations; + + @Before + public void setUp() throws Exception { + super.setUp(); + final int numIndices = randomIntBetween(1, MAX_TEST_INDICES); + Map indexMap = createIndexMetadataMap(1, numIndices); + Map identifierMap = createIdentifierMapFromIndexMetadata(indexMap, BLOB_ID_PREFIX); + Map> lookupMap = Collections.singletonMap(new SnapshotId(SNAPSHOT, SNAPSHOT), indexMap); + indexMetaDataGenerations = new IndexMetaDataGenerations(lookupMap, identifierMap); + } + + public void testEmpty() { + assertTrue(IndexMetaDataGenerations.EMPTY.isEmpty()); + assertNull(IndexMetaDataGenerations.EMPTY.getIndexMetaBlobId("test")); + } + + public void testBaseCase() { + assertFalse(indexMetaDataGenerations.isEmpty()); + assertEquals(BLOB_ID_PREFIX + 1, indexMetaDataGenerations.getIndexMetaBlobId(String.valueOf(1))); + } + + public void testIndexMetaBlobId() { + SnapshotId snapshotId = new SnapshotId(SNAPSHOT, SNAPSHOT); + IndexId indexId = new IndexId(INDEX_PREFIX + 1, INDEX_PREFIX + 1); + assertEquals(BLOB_ID_PREFIX + 1, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); + } + + public void testIndexMetaBlobIdFallback() { + SnapshotId snapshotId = new SnapshotId(SNAPSHOT, SNAPSHOT); + IndexId indexId = new IndexId("missingIndex", "missingIndex"); + assertEquals(SNAPSHOT, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); + + final String randomString = randomAlphaOfLength(8); + snapshotId = new SnapshotId(randomString, randomString); + assertEquals(randomString, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); + } + + public void testWithAddedSnapshot() { + // Construct a new snapshot + SnapshotId newSnapshot = new SnapshotId("newSnapshot", "newSnapshot"); + final String newIndexMetadataPrefix = "newIndexMetadata-"; + final String newBlobIdPrefix = "newBlob-"; + final int numIndices = randomIntBetween(2, MAX_TEST_INDICES); + Map newLookupMap = createIndexMetadataMap(2, numIndices); + Map identifierMap = createIdentifierMapFromIndexMetadata(newLookupMap, "newBlob-"); + + // Add the snapshot and verify that values have been updated as expected + IndexMetaDataGenerations updated = indexMetaDataGenerations.withAddedSnapshot(newSnapshot, newLookupMap, identifierMap); + assertEquals(newBlobIdPrefix + 2, updated.getIndexMetaBlobId(String.valueOf(2))); + assertEquals(newBlobIdPrefix + 2, updated.indexMetaBlobId(newSnapshot, new IndexId(INDEX_PREFIX + 2, INDEX_PREFIX + 2))); + // The first index should remain unchanged + assertEquals(BLOB_ID_PREFIX + 1, updated.getIndexMetaBlobId(String.valueOf(1))); + } + + public void testWithRemovedSnapshot() { + Set snapshotToRemove = Collections.singleton(new SnapshotId(SNAPSHOT, SNAPSHOT)); + assertEquals(IndexMetaDataGenerations.EMPTY, indexMetaDataGenerations.withRemovedSnapshots(snapshotToRemove)); + } + + private Map createIndexMetadataMap(int indexCountLowerBound, int numIndices) { + final int indexCountUpperBound = indexCountLowerBound + numIndices; + Map map = new HashMap<>(); + for (int i = indexCountLowerBound; i <= indexCountUpperBound; i++) { + map.put(new IndexId(INDEX_PREFIX + i, INDEX_PREFIX + i), String.valueOf(i)); + } + return map; + } + + private Map createIdentifierMapFromIndexMetadata(Map indexMetadataMap, String blobIdPrefix) { + return indexMetadataMap.values().stream().collect(Collectors.toMap(k -> k, v -> blobIdPrefix + v)); + } +}