Skip to content

Commit a5d02b7

Browse files
author
David Roberts
committed
[ML] Fix possible race condition when closing an opening job (#42506)
This change fixes a race condition that would result in an in-memory data structure becoming out-of-sync with persistent tasks in cluster state. If repeated often enough this could result in it being impossible to open any ML jobs on the affected node, as the master node would think the node had capacity to open another job but the chosen node would error during the open sequence due to its in-memory data structure being full. The race could be triggered by opening a job and then closing it a tiny fraction of a second later. It is unlikely a user of the UI could open and close the job that fast, but a script or program calling the REST API could. The nasty thing is, from the externally observable states and stats everything would appear to be fine - the fast open then close sequence would appear to leave the job in the closed state. It's only later that the leftovers in the in-memory data structure might build up and cause a problem.
1 parent b24800d commit a5d02b7

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManager.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,12 @@ protected void doRun() {
455455
logger.debug("Aborted opening job [{}] as it has been closed", jobId);
456456
return;
457457
}
458-
if (processContext.getState() != ProcessContext.ProcessStateName.NOT_RUNNING) {
459-
logger.debug("Cannot open job [{}] when its state is [{}]",
460-
jobId, processContext.getState().getClass().getName());
461-
return;
462-
}
463458

464459
try {
465-
createProcessAndSetRunning(processContext, job, params, closeHandler);
466-
processContext.getAutodetectCommunicator().restoreState(params.modelSnapshot());
467-
setJobState(jobTask, JobState.OPENED);
460+
if (createProcessAndSetRunning(processContext, job, params, closeHandler)) {
461+
processContext.getAutodetectCommunicator().restoreState(params.modelSnapshot());
462+
setJobState(jobTask, JobState.OPENED);
463+
}
468464
} catch (Exception e1) {
469465
// No need to log here as the persistent task framework will log it
470466
try {
@@ -501,19 +497,25 @@ protected void doRun() {
501497
ElasticsearchMappings::resultsMapping, client, clusterState, resultsMappingUpdateHandler);
502498
}
503499

504-
private void createProcessAndSetRunning(ProcessContext processContext,
505-
Job job,
506-
AutodetectParams params,
507-
BiConsumer<Exception, Boolean> handler) throws IOException {
500+
private boolean createProcessAndSetRunning(ProcessContext processContext,
501+
Job job,
502+
AutodetectParams params,
503+
BiConsumer<Exception, Boolean> handler) throws IOException {
508504
// At this point we lock the process context until the process has been started.
509505
// The reason behind this is to ensure closing the job does not happen before
510506
// the process is started as that can result to the job getting seemingly closed
511507
// but the actual process is hanging alive.
512508
processContext.tryLock();
513509
try {
510+
if (processContext.getState() != ProcessContext.ProcessStateName.NOT_RUNNING) {
511+
logger.debug("Cannot open job [{}] when its state is [{}]",
512+
job.getId(), processContext.getState().getClass().getName());
513+
return false;
514+
}
514515
AutodetectCommunicator communicator = create(processContext.getJobTask(), job, params, handler);
515516
communicator.writeHeader();
516517
processContext.setRunning(communicator);
518+
return true;
517519
} finally {
518520
// Now that the process is running and we have updated its state we can unlock.
519521
// It is important to unlock before we initialize the communicator (ie. load the model state)
@@ -643,6 +645,8 @@ public void closeJob(JobTask jobTask, boolean restart, String reason) {
643645
try {
644646
if (processContext.setDying() == false) {
645647
logger.debug("Cannot close job [{}] as it has been marked as dying", jobId);
648+
// The only way we can get here is if 2 close requests are made very close together.
649+
// The other close has done the work so it's safe to return here without doing anything.
646650
return;
647651
}
648652

@@ -656,10 +660,10 @@ public void closeJob(JobTask jobTask, boolean restart, String reason) {
656660
if (communicator == null) {
657661
logger.debug("Job [{}] is being closed before its process is started", jobId);
658662
jobTask.markAsCompleted();
659-
return;
663+
} else {
664+
communicator.close(restart, reason);
660665
}
661666

662-
communicator.close(restart, reason);
663667
processByAllocation.remove(allocationId);
664668
} catch (Exception e) {
665669
// If the close failed because the process has explicitly been killed by us then just pass on that exception
@@ -679,7 +683,7 @@ public void closeJob(JobTask jobTask, boolean restart, String reason) {
679683
try {
680684
removeTmpStorage(jobId);
681685
} catch (IOException e) {
682-
logger.error(new ParameterizedMessage("[{}]Failed to delete temporary files", jobId), e);
686+
logger.error(new ParameterizedMessage("[{}] Failed to delete temporary files", jobId), e);
683687
}
684688
}
685689

0 commit comments

Comments
 (0)