[Jobs] Implement graceful cancel for managed jobs#8772
[Jobs] Implement graceful cancel for managed jobs#8772
Conversation
Summary of ChangesHello @kyuds, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to the job cancellation mechanism by adding a 'graceful' option. This feature ensures that when a managed job is cancelled, any cached data is safely uploaded before termination, preventing potential data loss. The changes involve updating the command-line interface, the Python SDK, the job server's core logic, and the underlying communication protocols to support this new, more robust cancellation behavior. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a good start for adding the --graceful cancel feature for managed jobs. The new options are correctly plumbed through the CLI, SDK, and server components. However, the core implementation seems to be missing, as indicated by a TODO in sky/jobs/utils.py where the new parameters are deleted. I've left a few comments on this and other minor issues, such as an incorrect version comment and a log level that might be too subtle for an important warning.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a --graceful cancellation feature for managed jobs. The implementation cleverly uses the msg parameter of asyncio.Task.cancel() to pass the graceful shutdown signal to the job's exception handler. The changes are well-integrated across the CLI, client SDK, server, and controller components. My review focuses on improving the robustness and clarity of this new feature. I've identified a couple of areas for improvement: an inconsistency in handling graceful cancellation for pool-based jobs and a broad exception handler that could obscure potential issues. With the suggested changes, this will be a solid addition.
--graceful cancel feature--graceful cancel feature
--graceful cancel feature|
/smoke-test |
|
/quicktest-core |
|
same across multiple PRs: only azure bucket smoke tests are failing |
|
/smoke-test |
|
/quicktest-core |
| # Send the signal to the jobs controller. | ||
| signal_file = (pathlib.Path( | ||
| managed_job_constants.SIGNAL_FILE_PREFIX.format(job_id))) | ||
| # Filelock is needed to prevent race condition between signal | ||
| # check/removal and signal writing. | ||
| with filelock.FileLock(str(signal_file) + '.lock'): | ||
| with signal_file.open('w', encoding='utf-8') as f: | ||
| f.write(UserSignal.CANCEL.value) | ||
| f.flush() | ||
| if graceful: | ||
| logger.warning(f'Job {job_id} is on legacy controller, ' | ||
| 'graceful shutdown not supported.') | ||
| else: | ||
| # New controller process. | ||
| try: | ||
| signal_file = pathlib.Path( | ||
| managed_job_constants.CONSOLIDATED_SIGNAL_PATH, f'{job_id}') | ||
| signal_file.touch() | ||
| if graceful: | ||
| content = 'graceful' | ||
| if graceful_timeout is not None: | ||
| content = f'graceful:{graceful_timeout}' | ||
| signal_file.write_text(content, encoding='utf-8') | ||
| else: | ||
| signal_file.touch() |
There was a problem hiding this comment.
Do we also need a filelock now that we're writing something to signal_file, similar to the if block above?
| graceful_timeout = int( | ||
| content.split(':')[1]) | ||
| except (ValueError, IndexError): | ||
| pass |
There was a problem hiding this comment.
Nit: We hardcode 'graceful' here and in cancel_jobs_by_id(). Could we define it as a const, and maybe also extract this to smth like decode_cancel_signal() and move it to sky/jobs/utils.py?
| except Exception: # pylint: disable=broad-except | ||
| content = '' |
There was a problem hiding this comment.
should we do a logger.warning/debug here?
| except (ValueError, IndexError): | ||
| pass |
| logger.info(f'Job {job_id} graceful cancel: ' | ||
| f'graceful={graceful}, timeout={graceful_timeout}') |
There was a problem hiding this comment.
nit: should we turn this down to debug?
Resolves #8519
--gracefulflag for managed jobs. The flag fordownandstopis already implemented. We reuse a lot of the actual "graceful" handling mechanism from that pr.Tested:
Yaml for testing:
Tested (run the relevant ones):
bash format.sh/smoke-test(CI) orpytest tests/test_smoke.py(local)/smoke-test -k test_name(CI) orpytest tests/test_smoke.py::test_name(local)/quicktest-core(CI) orpytest tests/smoke_tests/test_backward_compat.py(local)