Skip to content

Commit 525808f

Browse files
committed
[ML] Reinstate ML daily maintenance actions (#47103)
A refactoring in 6.6 meant that the ML daily maintenance actions have not been run at all since then. This change installs the local master listener that schedules the ML daily maintenance, and also defends against some subtle race conditions that could occur in the future if a node flipped very quickly between master and non-master. Fixes #47003
1 parent c400c0e commit 525808f

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ private static TimeValue delayToNextTime(ClusterName clusterName) {
7979
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
8080
}
8181

82-
public void start() {
82+
public synchronized void start() {
8383
LOGGER.debug("Starting ML daily maintenance service");
8484
scheduleNext();
8585
}
8686

87-
public void stop() {
87+
public synchronized void stop() {
8888
LOGGER.debug("Stopping ML daily maintenance service");
8989
if (cancellable != null && cancellable.isCancelled() == false) {
9090
cancellable.cancel();
@@ -100,7 +100,7 @@ public void close() {
100100
stop();
101101
}
102102

103-
private void scheduleNext() {
103+
private synchronized void scheduleNext() {
104104
try {
105105
cancellable = threadPool.schedule(this::triggerTasks, schedulerProvider.get(), ThreadPool.Names.GENERIC);
106106
} catch (EsRejectedExecutionException e) {

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
3939
this.clusterService = clusterService;
4040
this.client = client;
4141
clusterService.addListener(this);
42+
clusterService.addLocalNodeMasterListener(this);
4243
}
4344

4445
@Override
@@ -80,7 +81,7 @@ public String executorName() {
8081
return ThreadPool.Names.GENERIC;
8182
}
8283

83-
private void installDailyMaintenanceService() {
84+
private synchronized void installDailyMaintenanceService() {
8485
if (mlDailyMaintenanceService == null) {
8586
mlDailyMaintenanceService = new MlDailyMaintenanceService(clusterService.getClusterName(), threadPool, client);
8687
mlDailyMaintenanceService.start();
@@ -93,7 +94,7 @@ public void beforeStop() {
9394
}
9495
}
9596

96-
private void uninstallDailyMaintenanceService() {
97+
private synchronized void uninstallDailyMaintenanceService() {
9798
if (mlDailyMaintenanceService != null) {
9899
mlDailyMaintenanceService.stop();
99100
mlDailyMaintenanceService = null;
@@ -106,7 +107,7 @@ MlDailyMaintenanceService getDailyMaintenanceService() {
106107
}
107108

108109
/** For testing */
109-
void setDailyMaintenanceService(MlDailyMaintenanceService service) {
110+
synchronized void setDailyMaintenanceService(MlDailyMaintenanceService service) {
110111
mlDailyMaintenanceService = service;
111112
}
112113
}

0 commit comments

Comments
 (0)