|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.repositories; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.opensearch.snapshots.SnapshotId; |
| 13 | +import org.opensearch.test.OpenSearchTestCase; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +public class IndexMetadataGenerationsTests extends OpenSearchTestCase { |
| 22 | + |
| 23 | + private final int MAX_TEST_INDICES = 10; |
| 24 | + private final String SNAPSHOT = "snapshot"; |
| 25 | + private final String INDEX_PREFIX = "index-"; |
| 26 | + private final String BLOB_ID_PREFIX = "blob-"; |
| 27 | + private IndexMetaDataGenerations indexMetaDataGenerations; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setUp() throws Exception { |
| 31 | + super.setUp(); |
| 32 | + final int numIndices = randomIntBetween(1, MAX_TEST_INDICES); |
| 33 | + Map<IndexId, String> indexMap = createIndexMetadataMap(1, numIndices); |
| 34 | + Map<String, String> identifierMap = createIdentifierMapFromIndexMetadata(indexMap, BLOB_ID_PREFIX); |
| 35 | + Map<SnapshotId, Map<IndexId, String>> lookupMap = Collections.singletonMap(new SnapshotId(SNAPSHOT, SNAPSHOT), indexMap); |
| 36 | + indexMetaDataGenerations = new IndexMetaDataGenerations(lookupMap, identifierMap); |
| 37 | + } |
| 38 | + |
| 39 | + public void testEmpty() { |
| 40 | + assertTrue(IndexMetaDataGenerations.EMPTY.isEmpty()); |
| 41 | + assertNull(IndexMetaDataGenerations.EMPTY.getIndexMetaBlobId("test")); |
| 42 | + } |
| 43 | + |
| 44 | + public void testBaseCase() { |
| 45 | + assertFalse(indexMetaDataGenerations.isEmpty()); |
| 46 | + assertEquals(BLOB_ID_PREFIX + 1, indexMetaDataGenerations.getIndexMetaBlobId(String.valueOf(1))); |
| 47 | + } |
| 48 | + |
| 49 | + public void testIndexMetaBlobId() { |
| 50 | + SnapshotId snapshotId = new SnapshotId(SNAPSHOT, SNAPSHOT); |
| 51 | + IndexId indexId = new IndexId(INDEX_PREFIX + 1, INDEX_PREFIX + 1); |
| 52 | + assertEquals(BLOB_ID_PREFIX + 1, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); |
| 53 | + } |
| 54 | + |
| 55 | + public void testIndexMetaBlobIdFallback() { |
| 56 | + SnapshotId snapshotId = new SnapshotId(SNAPSHOT, SNAPSHOT); |
| 57 | + IndexId indexId = new IndexId("missingIndex", "missingIndex"); |
| 58 | + assertEquals(SNAPSHOT, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); |
| 59 | + |
| 60 | + final String randomString = randomAlphaOfLength(8); |
| 61 | + snapshotId = new SnapshotId(randomString, randomString); |
| 62 | + assertEquals(randomString, indexMetaDataGenerations.indexMetaBlobId(snapshotId, indexId)); |
| 63 | + } |
| 64 | + |
| 65 | + public void testWithAddedSnapshot() { |
| 66 | + // Construct a new snapshot |
| 67 | + SnapshotId newSnapshot = new SnapshotId("newSnapshot", "newSnapshot"); |
| 68 | + final String newIndexMetadataPrefix = "newIndexMetadata-"; |
| 69 | + final String newBlobIdPrefix = "newBlob-"; |
| 70 | + final int numIndices = randomIntBetween(2, MAX_TEST_INDICES); |
| 71 | + Map<IndexId, String> newLookupMap = createIndexMetadataMap(2, numIndices); |
| 72 | + Map<String, String> identifierMap = createIdentifierMapFromIndexMetadata(newLookupMap, "newBlob-"); |
| 73 | + |
| 74 | + // Add the snapshot and verify that values have been updated as expected |
| 75 | + IndexMetaDataGenerations updated = indexMetaDataGenerations.withAddedSnapshot(newSnapshot, newLookupMap, identifierMap); |
| 76 | + assertEquals(newBlobIdPrefix + 2, updated.getIndexMetaBlobId(String.valueOf(2))); |
| 77 | + assertEquals(newBlobIdPrefix + 2, updated.indexMetaBlobId(newSnapshot, new IndexId(INDEX_PREFIX + 2, INDEX_PREFIX + 2))); |
| 78 | + // The first index should remain unchanged |
| 79 | + assertEquals(BLOB_ID_PREFIX + 1, updated.getIndexMetaBlobId(String.valueOf(1))); |
| 80 | + } |
| 81 | + |
| 82 | + public void testWithRemovedSnapshot() { |
| 83 | + Set<SnapshotId> snapshotToRemove = Collections.singleton(new SnapshotId(SNAPSHOT, SNAPSHOT)); |
| 84 | + assertEquals(IndexMetaDataGenerations.EMPTY, indexMetaDataGenerations.withRemovedSnapshots(snapshotToRemove)); |
| 85 | + } |
| 86 | + |
| 87 | + private Map<IndexId, String> createIndexMetadataMap(int indexCountLowerBound, int numIndices) { |
| 88 | + final int indexCountUpperBound = indexCountLowerBound + numIndices; |
| 89 | + Map<IndexId, String> map = new HashMap<>(); |
| 90 | + for (int i = indexCountLowerBound; i <= indexCountUpperBound; i++) { |
| 91 | + map.put(new IndexId(INDEX_PREFIX + i, INDEX_PREFIX + i), String.valueOf(i)); |
| 92 | + } |
| 93 | + return map; |
| 94 | + } |
| 95 | + |
| 96 | + private Map<String, String> createIdentifierMapFromIndexMetadata(Map<IndexId, String> indexMetadataMap, String blobIdPrefix) { |
| 97 | + return indexMetadataMap.values().stream().collect(Collectors.toMap(k -> k, v -> blobIdPrefix + v)); |
| 98 | + } |
| 99 | +} |
0 commit comments