3434import java .util .Set ;
3535import java .util .concurrent .atomic .AtomicBoolean ;
3636
37- import static org .opensearch .gateway .remote .RemoteClusterStateService .GLOBAL_METADATA_FORMAT ;
38- import static org .opensearch .gateway .remote .RemoteClusterStateService .GLOBAL_METADATA_PATH_TOKEN ;
39- import static org .opensearch .gateway .remote .RemoteClusterStateService .INDEX_METADATA_FORMAT ;
40- import static org .opensearch .gateway .remote .RemoteClusterStateService .INDEX_PATH_TOKEN ;
41- import static org .opensearch .gateway .remote .RemoteClusterStateService .MANIFEST_FILE_PREFIX ;
42- import static org .opensearch .gateway .remote .RemoteClusterStateService .MANIFEST_PATH_TOKEN ;
37+ import static org .opensearch .gateway .remote .model .RemoteClusterMetadataManifest .MANIFEST ;
4338
4439/**
4540 * A Manager which provides APIs to clean up stale cluster state files and runs an async stale cleanup task
@@ -50,7 +45,7 @@ public class RemoteClusterStateCleanupManager implements Closeable {
5045
5146 public static final int RETAINED_MANIFESTS = 10 ;
5247 public static final int SKIP_CLEANUP_STATE_CHANGES = 10 ;
53- public static final TimeValue CLUSTER_STATE_CLEANUP_INTERVAL_DEFAULT = TimeValue .timeValueMinutes ( 5 );
48+ public static final TimeValue CLUSTER_STATE_CLEANUP_INTERVAL_DEFAULT = TimeValue .timeValueSeconds ( 15 );
5449 public static final TimeValue CLUSTER_STATE_CLEANUP_INTERVAL_MINIMUM = TimeValue .MINUS_ONE ;
5550
5651 /**
@@ -70,7 +65,7 @@ public class RemoteClusterStateCleanupManager implements Closeable {
7065 private BlobStoreTransferService blobStoreTransferService ;
7166 private TimeValue staleFileCleanupInterval ;
7267 private final AtomicBoolean deleteStaleMetadataRunning = new AtomicBoolean (false );
73- private volatile AsyncStaleFileDeletion staleFileDeletionTask ;
68+ private AsyncStaleFileDeletion staleFileDeletionTask ;
7469 private long lastCleanupAttemptStateVersion ;
7570 private final ThreadPool threadpool ;
7671 private final ClusterApplierService clusterApplierService ;
@@ -150,12 +145,7 @@ void cleanUpStaleFiles() {
150145
151146 private void addStaleGlobalMetadataPath (String fileName , Set <String > filesToKeep , Set <String > staleGlobalMetadataPaths ) {
152147 if (!filesToKeep .contains (fileName )) {
153- String [] splitPath = fileName .split ("/" );
154- staleGlobalMetadataPaths .add (
155- new BlobPath ().add (GLOBAL_METADATA_PATH_TOKEN ).buildAsString () + GLOBAL_METADATA_FORMAT .blobName (
156- splitPath [splitPath .length - 1 ]
157- )
158- );
148+ staleGlobalMetadataPaths .add (fileName );
159149 }
160150 }
161151
@@ -172,15 +162,24 @@ void deleteClusterMetadata(
172162 Set <String > staleIndexMetadataPaths = new HashSet <>();
173163 Set <String > staleGlobalMetadataPaths = new HashSet <>();
174164 activeManifestBlobMetadata .forEach (blobMetadata -> {
175- ClusterMetadataManifest clusterMetadataManifest = remoteClusterStateService .fetchRemoteClusterMetadataManifest (
176- clusterName ,
177- clusterUUID ,
178- blobMetadata .name ()
179- );
165+ ClusterMetadataManifest clusterMetadataManifest = remoteClusterStateService .getRemoteManifestManager ()
166+ .fetchRemoteClusterMetadataManifest (clusterName , clusterUUID , blobMetadata .name ());
180167 clusterMetadataManifest .getIndices ()
181- .forEach (uploadedIndexMetadata -> filesToKeep .add (uploadedIndexMetadata .getUploadedFilename ()));
168+ .forEach (
169+ uploadedIndexMetadata -> filesToKeep .add (
170+ RemoteClusterStateUtils .getFormattedFileName (
171+ uploadedIndexMetadata .getUploadedFilename (),
172+ clusterMetadataManifest .getCodecVersion ()
173+ )
174+ )
175+ );
182176 if (clusterMetadataManifest .getCodecVersion () == ClusterMetadataManifest .CODEC_V1 ) {
183- filesToKeep .add (clusterMetadataManifest .getGlobalMetadataFileName ());
177+ filesToKeep .add (
178+ RemoteClusterStateUtils .getFormattedFileName (
179+ clusterMetadataManifest .getGlobalMetadataFileName (),
180+ clusterMetadataManifest .getCodecVersion ()
181+ )
182+ );
184183 } else if (clusterMetadataManifest .getCodecVersion () >= ClusterMetadataManifest .CODEC_V2 ) {
185184 filesToKeep .add (clusterMetadataManifest .getCoordinationMetadata ().getUploadedFilename ());
186185 filesToKeep .add (clusterMetadataManifest .getSettingsMetadata ().getUploadedFilename ());
@@ -191,14 +190,21 @@ void deleteClusterMetadata(
191190 }
192191 });
193192 staleManifestBlobMetadata .forEach (blobMetadata -> {
194- ClusterMetadataManifest clusterMetadataManifest = remoteClusterStateService .fetchRemoteClusterMetadataManifest (
195- clusterName ,
196- clusterUUID ,
197- blobMetadata .name ()
193+ ClusterMetadataManifest clusterMetadataManifest = remoteClusterStateService .getRemoteManifestManager ()
194+ .fetchRemoteClusterMetadataManifest (clusterName , clusterUUID , blobMetadata .name ());
195+ staleManifestPaths .add (
196+ remoteClusterStateService .getRemoteManifestManager ().getManifestFolderPath (clusterName , clusterUUID ).buildAsString ()
197+ + blobMetadata .name ()
198198 );
199- staleManifestPaths .add (new BlobPath ().add (MANIFEST_PATH_TOKEN ).buildAsString () + blobMetadata .name ());
200199 if (clusterMetadataManifest .getCodecVersion () == ClusterMetadataManifest .CODEC_V1 ) {
201- addStaleGlobalMetadataPath (clusterMetadataManifest .getGlobalMetadataFileName (), filesToKeep , staleGlobalMetadataPaths );
200+ addStaleGlobalMetadataPath (
201+ RemoteClusterStateUtils .getFormattedFileName (
202+ clusterMetadataManifest .getGlobalMetadataFileName (),
203+ clusterMetadataManifest .getCodecVersion ()
204+ ),
205+ filesToKeep ,
206+ staleGlobalMetadataPaths
207+ );
202208 } else if (clusterMetadataManifest .getCodecVersion () >= ClusterMetadataManifest .CODEC_V2 ) {
203209 addStaleGlobalMetadataPath (
204210 clusterMetadataManifest .getCoordinationMetadata ().getUploadedFilename (),
@@ -225,8 +231,10 @@ void deleteClusterMetadata(
225231 clusterMetadataManifest .getIndices ().forEach (uploadedIndexMetadata -> {
226232 if (filesToKeep .contains (uploadedIndexMetadata .getUploadedFilename ()) == false ) {
227233 staleIndexMetadataPaths .add (
228- new BlobPath ().add (INDEX_PATH_TOKEN ).add (uploadedIndexMetadata .getIndexUUID ()).buildAsString ()
229- + INDEX_METADATA_FORMAT .blobName (uploadedIndexMetadata .getUploadedFilename ())
234+ RemoteClusterStateUtils .getFormattedFileName (
235+ uploadedIndexMetadata .getUploadedFilename (),
236+ clusterMetadataManifest .getCodecVersion ()
237+ )
230238 );
231239 }
232240 });
@@ -237,9 +245,9 @@ void deleteClusterMetadata(
237245 return ;
238246 }
239247
240- deleteStalePaths (clusterName , clusterUUID , new ArrayList <>(staleGlobalMetadataPaths ));
241- deleteStalePaths (clusterName , clusterUUID , new ArrayList <>(staleIndexMetadataPaths ));
242- deleteStalePaths (clusterName , clusterUUID , new ArrayList <>(staleManifestPaths ));
248+ deleteStalePaths (new ArrayList <>(staleGlobalMetadataPaths ));
249+ deleteStalePaths (new ArrayList <>(staleIndexMetadataPaths ));
250+ deleteStalePaths (new ArrayList <>(staleManifestPaths ));
243251 } catch (IllegalStateException e ) {
244252 logger .error ("Error while fetching Remote Cluster Metadata manifests" , e );
245253 } catch (IOException e ) {
@@ -267,8 +275,8 @@ void deleteStaleClusterMetadata(String clusterName, String clusterUUID, int mani
267275 try {
268276 getBlobStoreTransferService ().listAllInSortedOrderAsync (
269277 ThreadPool .Names .REMOTE_PURGE ,
270- remoteClusterStateService .getManifestFolderPath (clusterName , clusterUUID ),
271- MANIFEST_FILE_PREFIX ,
278+ remoteClusterStateService .getRemoteManifestManager (). getManifestFolderPath (clusterName , clusterUUID ),
279+ MANIFEST ,
272280 Integer .MAX_VALUE ,
273281 new ActionListener <>() {
274282 @ Override
@@ -312,7 +320,11 @@ void deleteStaleUUIDsClusterMetadata(String clusterName, List<String> clusterUUI
312320 clusterUUIDs .forEach (
313321 clusterUUID -> getBlobStoreTransferService ().deleteAsync (
314322 ThreadPool .Names .REMOTE_PURGE ,
315- remoteClusterStateService .getCusterMetadataBasePath (clusterName , clusterUUID ),
323+ RemoteClusterStateUtils .getCusterMetadataBasePath (
324+ remoteClusterStateService .getBlobStoreRepository (),
325+ clusterName ,
326+ clusterUUID
327+ ),
316328 new ActionListener <>() {
317329 @ Override
318330 public void onResponse (Void unused ) {
@@ -336,12 +348,9 @@ public void onFailure(Exception e) {
336348 }
337349
338350 // package private for testing
339- void deleteStalePaths (String clusterName , String clusterUUID , List <String > stalePaths ) throws IOException {
351+ void deleteStalePaths (List <String > stalePaths ) throws IOException {
340352 logger .debug (String .format (Locale .ROOT , "Deleting stale files from remote - %s" , stalePaths ));
341- getBlobStoreTransferService ().deleteBlobs (
342- remoteClusterStateService .getCusterMetadataBasePath (clusterName , clusterUUID ),
343- stalePaths
344- );
353+ getBlobStoreTransferService ().deleteBlobs (BlobPath .cleanPath (), stalePaths );
345354 }
346355
347356 /**
0 commit comments