Skip to content

Commit

Permalink
Backporting unit tests for IndexMetadataGenerations and a javadoc fix…
Browse files Browse the repository at this point in the history
… from #5812 (#5922)

* Backporting unit tests for IndexMetadataGenerations from #5812

Signed-off-by: Kartik Ganesh <[email protected]>

* Fix javadoc for getExtendedCompatibilitySnapshotVersion

Signed-off-by: Kartik Ganesh <[email protected]>

Signed-off-by: Kartik Ganesh <[email protected]>
  • Loading branch information
kartg authored and kotwanikunal committed Jan 25, 2023
1 parent d3b8633 commit 0067a07
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/src/main/java/org/opensearch/index/IndexSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IndexId, String> indexMap = createIndexMetadataMap(1, numIndices);
Map<String, String> identifierMap = createIdentifierMapFromIndexMetadata(indexMap, BLOB_ID_PREFIX);
Map<SnapshotId, Map<IndexId, String>> 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<IndexId, String> newLookupMap = createIndexMetadataMap(2, numIndices);
Map<String, String> 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<SnapshotId> snapshotToRemove = Collections.singleton(new SnapshotId(SNAPSHOT, SNAPSHOT));
assertEquals(IndexMetaDataGenerations.EMPTY, indexMetaDataGenerations.withRemovedSnapshots(snapshotToRemove));
}

private Map<IndexId, String> createIndexMetadataMap(int indexCountLowerBound, int numIndices) {
final int indexCountUpperBound = indexCountLowerBound + numIndices;
Map<IndexId, String> 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<String, String> createIdentifierMapFromIndexMetadata(Map<IndexId, String> indexMetadataMap, String blobIdPrefix) {
return indexMetadataMap.values().stream().collect(Collectors.toMap(k -> k, v -> blobIdPrefix + v));
}
}

0 comments on commit 0067a07

Please sign in to comment.