Skip to content

Commit 942765e

Browse files
authored
Add snapshot_path prefix to snapshot shards path file on S3 (#16267)
Signed-off-by: Ashish Singh <[email protected]>
1 parent 691f725 commit 942765e

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

server/src/main/java/org/opensearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
import static org.opensearch.index.remote.RemoteStoreEnums.PathHashAlgorithm.FNV_1A_COMPOSITE_1;
200200
import static org.opensearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo.canonicalName;
201201
import static org.opensearch.repositories.blobstore.ChecksumBlobStoreFormat.SNAPSHOT_ONLY_FORMAT_PARAMS;
202+
import static org.opensearch.snapshots.SnapshotShardPaths.getIndexId;
202203

203204
/**
204205
* BlobStore - based implementation of Snapshot Repository
@@ -2293,7 +2294,10 @@ private void remoteTranslogCleanupAsync(
22932294
* @return List of matching shard paths
22942295
*/
22952296
private List<String> findMatchingShardPaths(String indexId, Map<String, BlobMetadata> snapshotShardPaths) {
2296-
return snapshotShardPaths.keySet().stream().filter(s -> s.startsWith(indexId)).collect(Collectors.toList());
2297+
return snapshotShardPaths.keySet()
2298+
.stream()
2299+
.filter(s -> (s.startsWith(indexId) || s.startsWith(SnapshotShardPaths.FILE_PREFIX + indexId)))
2300+
.collect(Collectors.toList());
22972301
}
22982302

22992303
/**
@@ -2546,11 +2550,11 @@ public void finalizeSnapshot(
25462550
*/
25472551
private void cleanupRedundantSnapshotShardPaths(Set<String> updatedShardPathsIndexIds) {
25482552
Set<String> updatedIndexIds = updatedShardPathsIndexIds.stream()
2549-
.map(s -> s.split("\\" + SnapshotShardPaths.DELIMITER)[0])
2553+
.map(s -> getIndexId(s.split("\\" + SnapshotShardPaths.DELIMITER)[0]))
25502554
.collect(Collectors.toSet());
25512555
Set<String> indexIdShardPaths = getSnapshotShardPaths().keySet();
25522556
List<String> staleShardPaths = indexIdShardPaths.stream().filter(s -> updatedShardPathsIndexIds.contains(s) == false).filter(s -> {
2553-
String indexId = s.split("\\" + SnapshotShardPaths.DELIMITER)[0];
2557+
String indexId = getIndexId(s.split("\\" + SnapshotShardPaths.DELIMITER)[0]);
25542558
return updatedIndexIds.contains(indexId);
25552559
}).collect(Collectors.toList());
25562560
try {
@@ -2595,7 +2599,7 @@ String writeIndexShardPaths(IndexId indexId, SnapshotId snapshotId, int shardCou
25952599
List<String> paths = getShardPaths(indexId, shardCount);
25962600
int pathType = indexId.getShardPathType();
25972601
int pathHashAlgorithm = FNV_1A_COMPOSITE_1.getCode();
2598-
String blobName = String.join(
2602+
String name = String.join(
25992603
SnapshotShardPaths.DELIMITER,
26002604
indexId.getId(),
26012605
indexId.getName(),
@@ -2611,9 +2615,9 @@ String writeIndexShardPaths(IndexId indexId, SnapshotId snapshotId, int shardCou
26112615
PathType.fromCode(pathType),
26122616
PathHashAlgorithm.fromCode(pathHashAlgorithm)
26132617
);
2614-
SNAPSHOT_SHARD_PATHS_FORMAT.write(shardPaths, snapshotShardPathBlobContainer(), blobName);
2618+
SNAPSHOT_SHARD_PATHS_FORMAT.write(shardPaths, snapshotShardPathBlobContainer(), name);
26152619
logShardPathsOperationSuccess(indexId, snapshotId);
2616-
return blobName;
2620+
return SnapshotShardPaths.FILE_PREFIX + name;
26172621
} catch (IOException e) {
26182622
logShardPathsOperationWarning(indexId, snapshotId, e);
26192623
}

server/src/main/java/org/opensearch/snapshots/SnapshotShardPaths.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public class SnapshotShardPaths implements ToXContent {
2929

3030
public static final String DELIMITER = ".";
3131

32-
public static final String FILE_NAME_FORMAT = "%s";
32+
public static final String FILE_PREFIX = "snapshot_path_";
33+
public static final String FILE_NAME_FORMAT = FILE_PREFIX + "%s";
3334

3435
private static final String PATHS_FIELD = "paths";
3536
private static final String INDEX_ID_FIELD = "indexId";
@@ -101,14 +102,21 @@ public static ShardInfo parseShardPath(String shardPath) {
101102
throw new IllegalArgumentException("Invalid shard path format: " + shardPath);
102103
}
103104
try {
104-
IndexId indexId = new IndexId(parts[1], parts[0], Integer.parseInt(parts[3]));
105+
IndexId indexId = new IndexId(parts[1], getIndexId(parts[0]), Integer.parseInt(parts[3]));
105106
int shardCount = Integer.parseInt(parts[2]);
106107
return new ShardInfo(indexId, shardCount);
107108
} catch (NumberFormatException e) {
108109
throw new IllegalArgumentException("Invalid shard path format: " + shardPath, e);
109110
}
110111
}
111112

113+
public static String getIndexId(String indexIdField) {
114+
if (indexIdField.startsWith(FILE_PREFIX)) {
115+
return indexIdField.substring(FILE_PREFIX.length());
116+
}
117+
return indexIdField;
118+
}
119+
112120
/**
113121
* Represents parsed information from a shard path.
114122
* This class encapsulates the index ID and shard count extracted from a shard path string.

server/src/test/java/org/opensearch/repositories/blobstore/BlobStoreRepositoryTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public void testParseShardPath() {
382382
IndexId indexId = repoData.getIndices().values().iterator().next();
383383
int shardCount = repoData.shardGenerations().getGens(indexId).size();
384384

385+
// Version 2.17 has file name starting with indexId
385386
String shardPath = String.join(
386387
SnapshotShardPaths.DELIMITER,
387388
indexId.getId(),
@@ -391,7 +392,19 @@ public void testParseShardPath() {
391392
"1"
392393
);
393394
ShardInfo shardInfo = SnapshotShardPaths.parseShardPath(shardPath);
395+
assertEquals(shardInfo.getIndexId(), indexId);
396+
assertEquals(shardInfo.getShardCount(), shardCount);
394397

398+
// Version 2.17 has file name starting with snapshot_path_
399+
shardPath = String.join(
400+
SnapshotShardPaths.DELIMITER,
401+
SnapshotShardPaths.FILE_PREFIX + indexId.getId(),
402+
indexId.getName(),
403+
String.valueOf(shardCount),
404+
String.valueOf(indexId.getShardPathType()),
405+
"1"
406+
);
407+
shardInfo = SnapshotShardPaths.parseShardPath(shardPath);
395408
assertEquals(shardInfo.getIndexId(), indexId);
396409
assertEquals(shardInfo.getShardCount(), shardCount);
397410
}

0 commit comments

Comments
 (0)