forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteClusterStateCleanupManager.java
446 lines (414 loc) · 21.3 KB
/
RemoteClusterStateCleanupManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.gateway.remote;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Strings;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.routing.remote.RemoteRoutingTableService;
import org.opensearch.cluster.service.ClusterApplierService;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.blobstore.BlobMetadata;
import org.opensearch.common.blobstore.BlobPath;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.AbstractAsyncTask;
import org.opensearch.core.action.ActionListener;
import org.opensearch.index.translog.transfer.BlobStoreTransferService;
import org.opensearch.threadpool.ThreadPool;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.opensearch.gateway.remote.RemoteClusterStateUtils.GLOBAL_METADATA_PATH_TOKEN;
import static org.opensearch.gateway.remote.model.RemoteClusterMetadataManifest.MANIFEST;
import static org.opensearch.gateway.remote.model.RemoteGlobalMetadata.GLOBAL_METADATA_FORMAT;
/**
* A Manager which provides APIs to clean up stale cluster state files and runs an async stale cleanup task
*
* @opensearch.internal
*/
public class RemoteClusterStateCleanupManager implements Closeable {
public static final int RETAINED_MANIFESTS = 10;
public static final int SKIP_CLEANUP_STATE_CHANGES = 10;
public static final TimeValue CLUSTER_STATE_CLEANUP_INTERVAL_DEFAULT = TimeValue.timeValueMinutes(5);
public static final TimeValue CLUSTER_STATE_CLEANUP_INTERVAL_MINIMUM = TimeValue.MINUS_ONE;
/**
* Setting to specify the interval to do run stale file cleanup job
* Min value -1 indicates that the stale file cleanup job should be disabled
*/
public static final Setting<TimeValue> REMOTE_CLUSTER_STATE_CLEANUP_INTERVAL_SETTING = Setting.timeSetting(
"cluster.remote_store.state.cleanup_interval",
CLUSTER_STATE_CLEANUP_INTERVAL_DEFAULT,
CLUSTER_STATE_CLEANUP_INTERVAL_MINIMUM,
Setting.Property.NodeScope,
Setting.Property.Dynamic
);
private static final Logger logger = LogManager.getLogger(RemoteClusterStateCleanupManager.class);
private final RemoteClusterStateService remoteClusterStateService;
private final RemotePersistenceStats remoteStateStats;
private BlobStoreTransferService blobStoreTransferService;
private TimeValue staleFileCleanupInterval;
private final AtomicBoolean deleteStaleMetadataRunning = new AtomicBoolean(false);
private volatile AsyncStaleFileDeletion staleFileDeletionTask;
private long lastCleanupAttemptStateVersion;
private final ThreadPool threadpool;
private final ClusterApplierService clusterApplierService;
private RemoteManifestManager remoteManifestManager;
private final RemoteRoutingTableService remoteRoutingTableService;
public RemoteClusterStateCleanupManager(
RemoteClusterStateService remoteClusterStateService,
ClusterService clusterService,
RemoteRoutingTableService remoteRoutingTableService
) {
this.remoteClusterStateService = remoteClusterStateService;
this.remoteStateStats = remoteClusterStateService.getStats();
ClusterSettings clusterSettings = clusterService.getClusterSettings();
this.clusterApplierService = clusterService.getClusterApplierService();
this.staleFileCleanupInterval = clusterSettings.get(REMOTE_CLUSTER_STATE_CLEANUP_INTERVAL_SETTING);
this.threadpool = remoteClusterStateService.getThreadpool();
// initialize with 0, a cleanup will be done when this node is elected master node and version is incremented more than threshold
this.lastCleanupAttemptStateVersion = 0;
clusterSettings.addSettingsUpdateConsumer(REMOTE_CLUSTER_STATE_CLEANUP_INTERVAL_SETTING, this::updateCleanupInterval);
this.remoteRoutingTableService = remoteRoutingTableService;
}
void start() {
staleFileDeletionTask = new AsyncStaleFileDeletion(this);
remoteManifestManager = remoteClusterStateService.getRemoteManifestManager();
}
@Override
public void close() throws IOException {
if (staleFileDeletionTask != null) {
staleFileDeletionTask.close();
}
}
private BlobStoreTransferService getBlobStoreTransferService() {
if (blobStoreTransferService == null) {
blobStoreTransferService = new BlobStoreTransferService(remoteClusterStateService.getBlobStore(), threadpool);
}
return blobStoreTransferService;
}
private void updateCleanupInterval(TimeValue updatedInterval) {
this.staleFileCleanupInterval = updatedInterval;
logger.info("updated remote state cleanup interval to {}", updatedInterval);
// After updating the interval, we need to close the current task and create a new one which will run with updated interval
if (staleFileDeletionTask != null && !staleFileDeletionTask.getInterval().equals(updatedInterval)) {
staleFileDeletionTask.setInterval(updatedInterval);
}
}
// visible for testing
void cleanUpStaleFiles() {
ClusterState currentAppliedState = clusterApplierService.state();
if (currentAppliedState.nodes().isLocalNodeElectedClusterManager()) {
long cleanUpAttemptStateVersion = currentAppliedState.version();
assert Strings.isNotEmpty(currentAppliedState.getClusterName().value()) : "cluster name is not set";
assert Strings.isNotEmpty(currentAppliedState.metadata().clusterUUID()) : "cluster uuid is not set";
if (cleanUpAttemptStateVersion - lastCleanupAttemptStateVersion > SKIP_CLEANUP_STATE_CHANGES) {
logger.info(
"Cleaning up stale remote state files for cluster [{}] with uuid [{}]. Last clean was done before {} updates",
currentAppliedState.getClusterName().value(),
currentAppliedState.metadata().clusterUUID(),
cleanUpAttemptStateVersion - lastCleanupAttemptStateVersion
);
this.deleteStaleClusterMetadata(
currentAppliedState.getClusterName().value(),
currentAppliedState.metadata().clusterUUID(),
RETAINED_MANIFESTS
);
lastCleanupAttemptStateVersion = cleanUpAttemptStateVersion;
} else {
logger.debug(
"Skipping cleanup of stale remote state files for cluster [{}] with uuid [{}]. Last clean was done before {} updates, which is less than threshold {}",
currentAppliedState.getClusterName().value(),
currentAppliedState.metadata().clusterUUID(),
cleanUpAttemptStateVersion - lastCleanupAttemptStateVersion,
SKIP_CLEANUP_STATE_CHANGES
);
}
} else {
logger.debug("Skipping cleanup task as local node is not elected Cluster Manager");
}
}
private void addStaleGlobalMetadataPath(String fileName, Set<String> filesToKeep, Set<String> staleGlobalMetadataPaths) {
if (!filesToKeep.contains(fileName)) {
String[] splitPath = fileName.split("/");
staleGlobalMetadataPaths.add(
new BlobPath().add(GLOBAL_METADATA_PATH_TOKEN).buildAsString() + GLOBAL_METADATA_FORMAT.blobName(
splitPath[splitPath.length - 1]
)
);
}
}
// visible for testing
void deleteClusterMetadata(
String clusterName,
String clusterUUID,
List<BlobMetadata> activeManifestBlobMetadata,
List<BlobMetadata> staleManifestBlobMetadata
) {
try {
Set<String> filesToKeep = new HashSet<>();
Set<String> staleManifestPaths = new HashSet<>();
Set<String> staleIndexMetadataPaths = new HashSet<>();
Set<String> staleGlobalMetadataPaths = new HashSet<>();
Set<String> staleIndexRoutingPaths = new HashSet<>();
activeManifestBlobMetadata.forEach(blobMetadata -> {
ClusterMetadataManifest clusterMetadataManifest = remoteManifestManager.fetchRemoteClusterMetadataManifest(
clusterName,
clusterUUID,
blobMetadata.name()
);
clusterMetadataManifest.getIndices()
.forEach(
uploadedIndexMetadata -> filesToKeep.add(
RemoteClusterStateUtils.getFormattedIndexFileName(uploadedIndexMetadata.getUploadedFilename())
)
);
if (clusterMetadataManifest.getCodecVersion() == ClusterMetadataManifest.CODEC_V1) {
filesToKeep.add(clusterMetadataManifest.getGlobalMetadataFileName());
} else if (clusterMetadataManifest.getCodecVersion() >= ClusterMetadataManifest.CODEC_V2) {
filesToKeep.add(clusterMetadataManifest.getCoordinationMetadata().getUploadedFilename());
filesToKeep.add(clusterMetadataManifest.getSettingsMetadata().getUploadedFilename());
filesToKeep.add(clusterMetadataManifest.getTemplatesMetadata().getUploadedFilename());
clusterMetadataManifest.getCustomMetadataMap()
.values()
.forEach(attribute -> filesToKeep.add(attribute.getUploadedFilename()));
}
if (clusterMetadataManifest.getIndicesRouting() != null) {
clusterMetadataManifest.getIndicesRouting()
.forEach(uploadedIndicesRouting -> filesToKeep.add(uploadedIndicesRouting.getUploadedFilename()));
}
});
staleManifestBlobMetadata.forEach(blobMetadata -> {
ClusterMetadataManifest clusterMetadataManifest = remoteManifestManager.fetchRemoteClusterMetadataManifest(
clusterName,
clusterUUID,
blobMetadata.name()
);
staleManifestPaths.add(
remoteManifestManager.getManifestFolderPath(clusterName, clusterUUID).buildAsString() + blobMetadata.name()
);
if (clusterMetadataManifest.getCodecVersion() == ClusterMetadataManifest.CODEC_V1) {
addStaleGlobalMetadataPath(clusterMetadataManifest.getGlobalMetadataFileName(), filesToKeep, staleGlobalMetadataPaths);
} else if (clusterMetadataManifest.getCodecVersion() >= ClusterMetadataManifest.CODEC_V2) {
if (filesToKeep.contains(clusterMetadataManifest.getCoordinationMetadata().getUploadedFilename()) == false) {
staleGlobalMetadataPaths.add(clusterMetadataManifest.getCoordinationMetadata().getUploadedFilename());
}
if (filesToKeep.contains(clusterMetadataManifest.getSettingsMetadata().getUploadedFilename()) == false) {
staleGlobalMetadataPaths.add(clusterMetadataManifest.getSettingsMetadata().getUploadedFilename());
}
if (filesToKeep.contains(clusterMetadataManifest.getTemplatesMetadata().getUploadedFilename()) == false) {
staleGlobalMetadataPaths.add(clusterMetadataManifest.getTemplatesMetadata().getUploadedFilename());
}
clusterMetadataManifest.getCustomMetadataMap()
.values()
.stream()
.map(ClusterMetadataManifest.UploadedMetadataAttribute::getUploadedFilename)
.filter(file -> filesToKeep.contains(file) == false)
.forEach(staleGlobalMetadataPaths::add);
}
if (clusterMetadataManifest.getIndicesRouting() != null) {
clusterMetadataManifest.getIndicesRouting().forEach(uploadedIndicesRouting -> {
if (!filesToKeep.contains(uploadedIndicesRouting.getUploadedFilename())) {
staleIndexRoutingPaths.add(uploadedIndicesRouting.getUploadedFilename());
logger.debug(
() -> new ParameterizedMessage(
"Indices routing paths in stale manifest: {}",
uploadedIndicesRouting.getUploadedFilename()
)
);
}
});
}
clusterMetadataManifest.getIndices().forEach(uploadedIndexMetadata -> {
String fileName = RemoteClusterStateUtils.getFormattedIndexFileName(uploadedIndexMetadata.getUploadedFilename());
if (filesToKeep.contains(fileName) == false) {
staleIndexMetadataPaths.add(fileName);
}
});
});
if (staleManifestPaths.isEmpty()) {
logger.debug("No stale Remote Cluster Metadata files found");
return;
}
deleteStalePaths(new ArrayList<>(staleGlobalMetadataPaths));
deleteStalePaths(new ArrayList<>(staleIndexMetadataPaths));
deleteStalePaths(new ArrayList<>(staleManifestPaths));
try {
remoteRoutingTableService.deleteStaleIndexRoutingPaths(new ArrayList<>(staleIndexRoutingPaths));
} catch (IOException e) {
logger.error(
() -> new ParameterizedMessage("Error while deleting stale index routing files {}", staleIndexRoutingPaths),
e
);
remoteStateStats.indexRoutingFilesCleanupAttemptFailed();
}
} catch (IllegalStateException e) {
logger.error("Error while fetching Remote Cluster Metadata manifests", e);
} catch (IOException e) {
logger.error("Error while deleting stale Remote Cluster Metadata files", e);
remoteStateStats.cleanUpAttemptFailed();
} catch (Exception e) {
logger.error("Unexpected error while deleting stale Remote Cluster Metadata files", e);
remoteStateStats.cleanUpAttemptFailed();
}
}
/**
* Deletes older than last {@code versionsToRetain} manifests. Also cleans up unreferenced IndexMetadata associated with older manifests
*
* @param clusterName name of the cluster
* @param clusterUUID uuid of cluster state to refer to in remote
* @param manifestsToRetain no of latest manifest files to keep in remote
*/
// package private for testing
void deleteStaleClusterMetadata(String clusterName, String clusterUUID, int manifestsToRetain) {
if (deleteStaleMetadataRunning.compareAndSet(false, true) == false) {
logger.info("Delete stale cluster metadata task is already in progress.");
return;
}
try {
getBlobStoreTransferService().listAllInSortedOrderAsync(
ThreadPool.Names.REMOTE_PURGE,
remoteManifestManager.getManifestFolderPath(clusterName, clusterUUID),
MANIFEST,
Integer.MAX_VALUE,
new ActionListener<>() {
@Override
public void onResponse(List<BlobMetadata> blobMetadata) {
if (blobMetadata.size() > manifestsToRetain) {
deleteClusterMetadata(
clusterName,
clusterUUID,
blobMetadata.subList(0, manifestsToRetain),
blobMetadata.subList(manifestsToRetain, blobMetadata.size())
);
}
deleteStaleMetadataRunning.set(false);
}
@Override
public void onFailure(Exception e) {
logger.error(
new ParameterizedMessage(
"Exception occurred while deleting Remote Cluster Metadata for clusterUUIDs {}",
clusterUUID
)
);
deleteStaleMetadataRunning.set(false);
}
}
);
} catch (Exception e) {
deleteStaleMetadataRunning.set(false);
throw e;
}
}
/**
* Purges all remote cluster state against provided cluster UUIDs
*
* @param clusterName name of the cluster
* @param clusterUUIDs clusteUUIDs for which the remote state needs to be purged
*/
void deleteStaleUUIDsClusterMetadata(String clusterName, List<String> clusterUUIDs) {
clusterUUIDs.forEach(
clusterUUID -> getBlobStoreTransferService().deleteAsync(
ThreadPool.Names.REMOTE_PURGE,
RemoteClusterStateUtils.getClusterMetadataBasePath(
remoteClusterStateService.getBlobStoreRepository(),
clusterName,
clusterUUID
),
new ActionListener<>() {
@Override
public void onResponse(Void unused) {
logger.info("Deleted all remote cluster metadata for cluster UUID - {}", clusterUUID);
}
@Override
public void onFailure(Exception e) {
logger.error(
new ParameterizedMessage(
"Exception occurred while deleting all remote cluster metadata for cluster UUID {}",
clusterUUID
),
e
);
remoteStateStats.cleanUpAttemptFailed();
}
}
)
);
}
// package private for testing
void deleteStalePaths(List<String> stalePaths) throws IOException {
logger.debug(String.format(Locale.ROOT, "Deleting stale files from remote - %s", stalePaths));
getBlobStoreTransferService().deleteBlobs(BlobPath.cleanPath(), stalePaths);
}
/**
* Purges all remote cluster state against provided cluster UUIDs
* @param clusterState current state of the cluster
* @param committedManifest last committed ClusterMetadataManifest
*/
public void deleteStaleClusterUUIDs(ClusterState clusterState, ClusterMetadataManifest committedManifest) {
threadpool.executor(ThreadPool.Names.REMOTE_PURGE).execute(() -> {
String clusterName = clusterState.getClusterName().value();
logger.debug("Deleting stale cluster UUIDs data from remote [{}]", clusterName);
Set<String> allClustersUUIDsInRemote;
try {
allClustersUUIDsInRemote = new HashSet<>(
remoteClusterStateService.getAllClusterUUIDs(clusterState.getClusterName().value())
);
} catch (IOException e) {
logger.info(String.format(Locale.ROOT, "Error while fetching all cluster UUIDs for [%s]", clusterName));
return;
}
// Retain last 2 cluster uuids data
allClustersUUIDsInRemote.remove(committedManifest.getClusterUUID());
allClustersUUIDsInRemote.remove(committedManifest.getPreviousClusterUUID());
deleteStaleUUIDsClusterMetadata(clusterName, new ArrayList<>(allClustersUUIDsInRemote));
});
}
public TimeValue getStaleFileCleanupInterval() {
return this.staleFileCleanupInterval;
}
AsyncStaleFileDeletion getStaleFileDeletionTask() { // for testing
return this.staleFileDeletionTask;
}
RemotePersistenceStats getStats() {
return this.remoteStateStats;
}
static final class AsyncStaleFileDeletion extends AbstractAsyncTask {
private final RemoteClusterStateCleanupManager remoteClusterStateCleanupManager;
AsyncStaleFileDeletion(RemoteClusterStateCleanupManager remoteClusterStateCleanupManager) {
super(
logger,
remoteClusterStateCleanupManager.threadpool,
remoteClusterStateCleanupManager.getStaleFileCleanupInterval(),
true
);
this.remoteClusterStateCleanupManager = remoteClusterStateCleanupManager;
rescheduleIfNecessary();
}
@Override
protected boolean mustReschedule() {
return true;
}
@Override
protected void runInternal() {
remoteClusterStateCleanupManager.cleanUpStaleFiles();
}
@Override
protected String getThreadPool() {
return ThreadPool.Names.REMOTE_PURGE;
}
}
}