Skip to content

[jobs] Fix recovery race condition in JobManager - #40068

Merged
edoakes merged 14 commits into
ray-project:masterfrom
edoakes:fix-job-race
Oct 17, 2023
Merged

edoakes merged 14 commits into
ray-project:masterfrom
edoakes:fix-job-race

Conversation

@edoakes

@edoakes edoakes commented Oct 3, 2023 •

Copy link
Copy Markdown
Collaborator

Why are these changes needed?

There are multiple race conditions between JobManager.submit_job and JobManager._recover_running_jobs because the latter runs as an async task on the event loop. This can cause a duplicate monitoring task to be started for a job before the actor is event spawned. This has caused multiple issues:

This PR adds synchronization so submit_job will only run after _recover_running_jobs finishes (note that _recover_running_jobs is async and will exit after a single GCS read RPC, so this should have no practical impact).

Related issue number

Closes #40062

Checks

@edoakes edoakes changed the title [WIP][jobs] Fix recovery race condition in JobManager [jobs] Remove recovery code to fix race condition in JobManager Oct 16, 2023
@edoakes edoakes changed the title [jobs] Remove recovery code to fix race condition in JobManager [jobs] Fix recovery race condition in JobManager Oct 16, 2023
Signed-off-by: Edward Oakes <[email protected]>

@rkooo567 rkooo567 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I may be misunderstaing some parts, but is there a repro + test we can add, or is it purely a theoretical fix?

I have 2 parts that I feel sketchy;

  1. Currently run_background_task(self._monitor_job(job_id)) starts monitor_job asynchronously, so although we have this event, there's still a possibility _monitor_job_internal is triggered between Put job info <> actor creation?
  2. This part of code
                if job_supervisor is None:
                    job_supervisor = self._get_actor_for_job(job_id)
                if job_supervisor is None:
                    if job_status == JobStatus.PENDING:
                        # Maybe the job supervisor actor is not created yet.
                        # We will wait for the next loop.
                        continue

should avoid the race you mentioned from the original issue (because we never overwrites the PENDING state to others until we create an actor iiuc)?

I am actually curious if it is just

  1. supervisor actor is removed somehow (killed, failed to start, or whatever) after it is added to the monitor loop.
  2. monitor job "overwrites" the already failed job. It seems like the current logic allows to do it.

@edoakes

edoakes commented Oct 17, 2023

Copy link
Copy Markdown
Collaborator Author

@rkooo567 the race condition happens because the background task is started for the pending job. The fix in this PR will avoid kicking off the _monitor_internal task for jobs between when their status is added as PENDING and the actor is started.

The PENDING check is not sufficient because what happens is the actor actually fails to be created (in the in the linked issue it was because of the runtime_env being invalid). So it passes that check, tries to get the actor, fails, and overwrites the status with the weird system error message.

So sequence of events is:

  1. Constructor runs, schedules _recover_running_jobs task.
  2. submit_job runs, marks the job PENDING then yields.
  3. _recover_running_jobs runs, kicking off a _monitor_internal task for the job that's PENDING.
  4. submit_job tries to create the actor, fails, and marks the job as FAILED.
  5. _monitor_internal tries to get the actor, fails, and overwrites the status.

@rkooo567

Copy link
Copy Markdown
Contributor

So, it looks like if a supervisor actor fails to start, I can easily reproduce the error message. Can you add a unit test? (maybe we can provide a bad runtime env to the supervisor actor). I could verify this PR fixes the issue.

Regarding the problem, I have the impression the root cause is actually that we don't health check actor right after we create it, but we rely on monitor_internal which always calls get_actor first (which will always fail if actor fails to be created). In this case, isn't the right solution we just health check actor immediately to check if it is started or failed? For example, #40402. (and we should avoid overwriting the failed status message if it is already failed).

Regarding the current fix, I could verify the fix, but I feel like there's still fundamental possibility of race. The reason why I feel this way is because "monitor_job" is a task, so theoretically it is still able to be created in the middle of PENDING <> actor creation (since we await on putting the PENDING status RPC). For example, I feel like the following scenario is possible.

  1. submit_job, await event.wait
  2. _recover_running_jobs creates a task (not scheduled) _monitor_job and set event.
  3. _monitor_job scheduled and starts _monitor_job_internal. await on self._job_info_client.get_status(job_id)
  4. submit_job puts PENDING info (and await on new_key_added = await self._job_info_client.put_info)
  5. _monitor_job_internal returned and await on job_info = await self._job_info_client.get_info(job_id)
  6. submit_job fails to create an actor
  7. _monitor_job_internal returned from job_info = await self._job_info_client.get_info(job_id) and call job_supervisor = self._get_actor_for_job(job_id). It fails (because actors didn't start) and writes the incorrect status.

@rkooo567 rkooo567 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the current fix seems to work, I will leave it up to you which solution you chooses, but let's add a unit test before merging it!

@edoakes

edoakes commented Oct 17, 2023

Copy link
Copy Markdown
Collaborator Author
  1. submit_job, await event.wait
  2. _recover_running_jobs creates a task (not scheduled) _monitor_job and set event.
  3. _monitor_job scheduled and starts _monitor_job_internal. await on self._job_info_client.get_status(job_id)
  4. submit_job puts PENDING info (and await on new_key_added = await self._job_info_client.put_info)
  5. _monitor_job_internal returned and await on job_info = await self._job_info_client.get_info(job_id)
  6. submit_job fails to create an actor
  7. _monitor_job_internal returned from job_info = await self._job_info_client.get_info(job_id) and call job_supervisor = self._get_actor_for_job(job_id). It fails (because actors didn't start) and writes the incorrect status.

For a single job manager, this sequence of events cannot happen for a single job_id under the current solution. The reason it cannot happen is because submit_job can only run after _recover_running_jobs, which means the job ID cannot be present in self._job_info_client.get_all_jobs(), so _monitor_job_internal will not be started for it (until it hits the "happy path" in submit_job).

Re: your proposed solution about health checking immediately after starting the actor, that is effectively what happens in this case (with a bad runtime_env, the ActorCls.remote() call fails immediately).

@edoakes

edoakes commented Oct 17, 2023

Copy link
Copy Markdown
Collaborator Author

@rkooo567 I've added a test case: test_job_manager.py::test_actor_creation_error_not_overwritten.

Verified that this failed consistently w/o the fix.

Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
@architkulkarni architkulkarni self-assigned this Oct 17, 2023

@architkulkarni architkulkarni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix was discussed offline. Nice test!

@edoakes
edoakes merged commit c4ad3d5 into ray-project:master Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[jobs] Race condition in JobManager between _recover_running_jobs and submit_job

3 participants