[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
This commit is contained in:
David Roberts 2019-09-26 15:24:40 +01:00
parent 467596871a
commit 0807d409bf
2 changed files with 7 additions and 6 deletions

View File

@ -79,12 +79,12 @@ public class MlDailyMaintenanceService implements Releasable {
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
} }
public void start() { public synchronized void start() {
LOGGER.debug("Starting ML daily maintenance service"); LOGGER.debug("Starting ML daily maintenance service");
scheduleNext(); scheduleNext();
} }
public void stop() { public synchronized void stop() {
LOGGER.debug("Stopping ML daily maintenance service"); LOGGER.debug("Stopping ML daily maintenance service");
if (cancellable != null && cancellable.isCancelled() == false) { if (cancellable != null && cancellable.isCancelled() == false) {
cancellable.cancel(); cancellable.cancel();
@ -100,7 +100,7 @@ public class MlDailyMaintenanceService implements Releasable {
stop(); stop();
} }
private void scheduleNext() { private synchronized void scheduleNext() {
try { try {
cancellable = threadPool.schedule(this::triggerTasks, schedulerProvider.get(), ThreadPool.Names.GENERIC); cancellable = threadPool.schedule(this::triggerTasks, schedulerProvider.get(), ThreadPool.Names.GENERIC);
} catch (EsRejectedExecutionException e) { } catch (EsRejectedExecutionException e) {

View File

@ -39,6 +39,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
this.clusterService = clusterService; this.clusterService = clusterService;
this.client = client; this.client = client;
clusterService.addListener(this); clusterService.addListener(this);
clusterService.addLocalNodeMasterListener(this);
} }
@Override @Override
@ -80,7 +81,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
return ThreadPool.Names.GENERIC; return ThreadPool.Names.GENERIC;
} }
private void installDailyMaintenanceService() { private synchronized void installDailyMaintenanceService() {
if (mlDailyMaintenanceService == null) { if (mlDailyMaintenanceService == null) {
mlDailyMaintenanceService = new MlDailyMaintenanceService(clusterService.getClusterName(), threadPool, client); mlDailyMaintenanceService = new MlDailyMaintenanceService(clusterService.getClusterName(), threadPool, client);
mlDailyMaintenanceService.start(); mlDailyMaintenanceService.start();
@ -93,7 +94,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
} }
} }
private void uninstallDailyMaintenanceService() { private synchronized void uninstallDailyMaintenanceService() {
if (mlDailyMaintenanceService != null) { if (mlDailyMaintenanceService != null) {
mlDailyMaintenanceService.stop(); mlDailyMaintenanceService.stop();
mlDailyMaintenanceService = null; mlDailyMaintenanceService = null;
@ -106,7 +107,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
} }
/** For testing */ /** For testing */
void setDailyMaintenanceService(MlDailyMaintenanceService service) { synchronized void setDailyMaintenanceService(MlDailyMaintenanceService service) {
mlDailyMaintenanceService = service; mlDailyMaintenanceService = service;
} }
} }