diff --git a/server/src/main/java/org/opensearch/snapshots/SnapshotsService.java b/server/src/main/java/org/opensearch/snapshots/SnapshotsService.java index 6e66f8c958666..93a36575befb5 100644 --- a/server/src/main/java/org/opensearch/snapshots/SnapshotsService.java +++ b/server/src/main/java/org/opensearch/snapshots/SnapshotsService.java @@ -646,24 +646,32 @@ public TimeValue timeout() { } private void cleanOrphanTimestamp(String repoName, RepositoryData repositoryData) { - Collection snapshotUUIDs = repositoryData.getSnapshotIds().stream().map(SnapshotId::getUUID).collect(Collectors.toSet()); - Map> pinnedEntities = RemoteStorePinnedTimestampService.getPinnedEntities(); + boolean enterRepo = false; + try { + if (tryEnterRepoLoop(repoName) == false) { + logger.error("Concurrent snapshot . Bailing out"); + return; + } + enterRepo = true; + Collection snapshotUUIDs = repositoryData.getSnapshotIds().stream().map(SnapshotId::getUUID).collect(Collectors.toSet()); + Map> pinnedEntities = RemoteStorePinnedTimestampService.getPinnedEntities(); - List orphanPinnedEntities = pinnedEntities.keySet() - .stream() - .filter(pinnedEntity -> isOrphanPinnedEntity(repoName, snapshotUUIDs, pinnedEntity)) - .collect(Collectors.toList()); + List orphanPinnedEntities = pinnedEntities.keySet() + .stream() + .filter(pinnedEntity -> isOrphanPinnedEntity(repoName, snapshotUUIDs, pinnedEntity)) + .collect(Collectors.toList()); - if (orphanPinnedEntities.isEmpty()) { - return; - } + if (orphanPinnedEntities.isEmpty()) { + return; + } - logger.info("Found {} orphan timestamps. Cleaning it up now", orphanPinnedEntities.size()); - if (tryEnterRepoLoop(repoName)) { + // Repo data old + logger.info("Found {} orphan timestamps. Cleaning it up now", orphanPinnedEntities.size()); deleteOrphanTimestamps(pinnedEntities, orphanPinnedEntities); - leaveRepoLoop(repoName); - } else { - logger.info("Concurrent snapshot create/delete is happening. Skipping clean up of orphan timestamps"); + } finally { + if (enterRepo) { + leaveRepoLoop(repoName); + } } }