Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit d6e4a2b

Browse files
committed
Reintroducing backwards compatibility logic in certain classes
This reverts changes made in opensearch-project#4728 and opensearch-project#4702. These were only made in main and not backported to 2.x This change also adds unit tests for IndexMetadataGenerations Signed-off-by: Kartik Ganesh <[email protected]>
1 parent 61097a9 commit d6e4a2b

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1844,11 +1844,15 @@ public static IndexMetadata fromXContent(XContentParser parser) throws IOExcepti
18441844
// Reference:
18451845
// https://github.com/opensearch-project/OpenSearch/blob/4dde0f2a3b445b2fc61dab29c5a2178967f4a3e3/server/src/main/java/org/opensearch/cluster/metadata/IndexMetadata.java#L1620-L1628
18461846
Version legacyVersion = LegacyESVersion.fromId(6050099);
1847-
if (Assertions.ENABLED && Version.indexCreated(builder.settings).onOrAfter(legacyVersion)) {
1847+
Version indexCreatedVersion = Version.indexCreated(builder.settings);
1848+
if (Assertions.ENABLED && indexCreatedVersion.onOrAfter(legacyVersion)) {
18481849
assert mappingVersion : "mapping version should be present for indices";
18491850
assert settingsVersion : "settings version should be present for indices";
18501851
}
1851-
if (Assertions.ENABLED) {
1852+
// Reference:
1853+
// https://github.com/opensearch-project/OpenSearch/blob/2e4b27b243d8bd2c515f66cf86c6d1d6a601307f/server/src/main/java/org/opensearch/cluster/metadata/IndexMetadata.java#L1824
1854+
legacyVersion = LegacyESVersion.fromId(7020099);
1855+
if (Assertions.ENABLED && indexCreatedVersion.onOrAfter(legacyVersion)) {
18521856
assert aliasesVersion : "aliases version should be present for indices";
18531857
}
18541858
return builder.build();

server/src/main/java/org/opensearch/repositories/IndexMetaDataGenerations.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,21 @@ public String getIndexMetaBlobId(String metaIdentifier) {
9292
}
9393

9494
/**
95-
* Get the blob id by {@link SnapshotId} and {@link IndexId}.
95+
* Get the blob id by {@link SnapshotId} and {@link IndexId}. If none is found, we fall back to the value
96+
* of {@link SnapshotId#getUUID()} to allow for extended backwards compatibility use-cases with
97+
* {@link org.opensearch.LegacyESVersion} versions which used the snapshot UUID as the index metadata blob id.
9698
*
9799
* @param snapshotId Snapshot Id
98100
* @param indexId Index Id
99101
* @return blob id for the given index metadata
100102
*/
101103
public String indexMetaBlobId(SnapshotId snapshotId, IndexId indexId) {
102104
final String identifier = lookup.getOrDefault(snapshotId, Collections.emptyMap()).get(indexId);
103-
return identifiers.get(identifier);
105+
if (identifier == null) {
106+
return snapshotId.getUUID();
107+
} else {
108+
return identifiers.get(identifier);
109+
}
104110
}
105111

106112
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)