13
13
import org .apache .logging .log4j .message .ParameterizedMessage ;
14
14
import org .apache .logging .log4j .util .Strings ;
15
15
import org .opensearch .cluster .ClusterState ;
16
+ import org .opensearch .cluster .routing .remote .RemoteRoutingTableService ;
16
17
import org .opensearch .cluster .service .ClusterApplierService ;
17
18
import org .opensearch .cluster .service .ClusterService ;
18
19
import org .opensearch .common .blobstore .BlobMetadata ;
@@ -72,8 +73,13 @@ public class RemoteClusterStateCleanupManager implements Closeable {
72
73
private final ThreadPool threadpool ;
73
74
private final ClusterApplierService clusterApplierService ;
74
75
private RemoteManifestManager remoteManifestManager ;
76
+ private final RemoteRoutingTableService remoteRoutingTableService ;
75
77
76
- public RemoteClusterStateCleanupManager (RemoteClusterStateService remoteClusterStateService , ClusterService clusterService ) {
78
+ public RemoteClusterStateCleanupManager (
79
+ RemoteClusterStateService remoteClusterStateService ,
80
+ ClusterService clusterService ,
81
+ RemoteRoutingTableService remoteRoutingTableService
82
+ ) {
77
83
this .remoteClusterStateService = remoteClusterStateService ;
78
84
this .remoteStateStats = remoteClusterStateService .getStats ();
79
85
ClusterSettings clusterSettings = clusterService .getClusterSettings ();
@@ -83,6 +89,7 @@ public RemoteClusterStateCleanupManager(RemoteClusterStateService remoteClusterS
83
89
// initialize with 0, a cleanup will be done when this node is elected master node and version is incremented more than threshold
84
90
this .lastCleanupAttemptStateVersion = 0 ;
85
91
clusterSettings .addSettingsUpdateConsumer (REMOTE_CLUSTER_STATE_CLEANUP_INTERVAL_SETTING , this ::updateCleanupInterval );
92
+ this .remoteRoutingTableService = remoteRoutingTableService ;
86
93
}
87
94
88
95
void start () {
@@ -170,6 +177,7 @@ void deleteClusterMetadata(
170
177
Set <String > staleManifestPaths = new HashSet <>();
171
178
Set <String > staleIndexMetadataPaths = new HashSet <>();
172
179
Set <String > staleGlobalMetadataPaths = new HashSet <>();
180
+ Set <String > staleIndexRoutingPaths = new HashSet <>();
173
181
activeManifestBlobMetadata .forEach (blobMetadata -> {
174
182
ClusterMetadataManifest clusterMetadataManifest = remoteManifestManager .fetchRemoteClusterMetadataManifest (
175
183
clusterName ,
@@ -192,6 +200,10 @@ void deleteClusterMetadata(
192
200
.values ()
193
201
.forEach (attribute -> filesToKeep .add (attribute .getUploadedFilename ()));
194
202
}
203
+ if (clusterMetadataManifest .getIndicesRouting () != null ) {
204
+ clusterMetadataManifest .getIndicesRouting ()
205
+ .forEach (uploadedIndicesRouting -> filesToKeep .add (uploadedIndicesRouting .getUploadedFilename ()));
206
+ }
195
207
});
196
208
staleManifestBlobMetadata .forEach (blobMetadata -> {
197
209
ClusterMetadataManifest clusterMetadataManifest = remoteManifestManager .fetchRemoteClusterMetadataManifest (
@@ -221,6 +233,19 @@ void deleteClusterMetadata(
221
233
.filter (file -> filesToKeep .contains (file ) == false )
222
234
.forEach (staleGlobalMetadataPaths ::add );
223
235
}
236
+ if (clusterMetadataManifest .getIndicesRouting () != null ) {
237
+ clusterMetadataManifest .getIndicesRouting ().forEach (uploadedIndicesRouting -> {
238
+ if (!filesToKeep .contains (uploadedIndicesRouting .getUploadedFilename ())) {
239
+ staleIndexRoutingPaths .add (uploadedIndicesRouting .getUploadedFilename ());
240
+ logger .debug (
241
+ () -> new ParameterizedMessage (
242
+ "Indices routing paths in stale manifest: {}" ,
243
+ uploadedIndicesRouting .getUploadedFilename ()
244
+ )
245
+ );
246
+ }
247
+ });
248
+ }
224
249
225
250
clusterMetadataManifest .getIndices ().forEach (uploadedIndexMetadata -> {
226
251
String fileName = RemoteClusterStateUtils .getFormattedIndexFileName (uploadedIndexMetadata .getUploadedFilename ());
@@ -238,6 +263,15 @@ void deleteClusterMetadata(
238
263
deleteStalePaths (new ArrayList <>(staleGlobalMetadataPaths ));
239
264
deleteStalePaths (new ArrayList <>(staleIndexMetadataPaths ));
240
265
deleteStalePaths (new ArrayList <>(staleManifestPaths ));
266
+ try {
267
+ remoteRoutingTableService .deleteStaleIndexRoutingPaths (new ArrayList <>(staleIndexRoutingPaths ));
268
+ } catch (IOException e ) {
269
+ logger .error (
270
+ () -> new ParameterizedMessage ("Error while deleting stale index routing files {}" , staleIndexRoutingPaths ),
271
+ e
272
+ );
273
+ remoteStateStats .indexRoutingFilesCleanupAttemptFailed ();
274
+ }
241
275
} catch (IllegalStateException e ) {
242
276
logger .error ("Error while fetching Remote Cluster Metadata manifests" , e );
243
277
} catch (IOException e ) {
0 commit comments