-
Notifications
You must be signed in to change notification settings - Fork 551
Feature:3963 Step HeartBeat components #4073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,9 @@ | |
| from zenml.orchestrators import utils as orchestrator_utils | ||
| from zenml.orchestrators.step_runner import StepRunner | ||
| from zenml.stack import Stack | ||
| from zenml.steps import StepHeartBeatTerminationException, StepHeartbeatWorker | ||
| from zenml.utils import env_utils, exception_utils, string_utils | ||
| from zenml.utils.exception_utils import ContextReraise | ||
| from zenml.utils.time_utils import utc_now | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -167,7 +169,6 @@ def signal_handler(signum: int, frame: Any) -> None: | |
|
|
||
| try: | ||
| client = Client() | ||
| pipeline_run = None | ||
|
|
||
| if self._step_run: | ||
| pipeline_run = client.get_pipeline_run( | ||
|
|
@@ -443,35 +444,66 @@ def _run_step( | |
| ) | ||
|
|
||
| start_time = time.time() | ||
| try: | ||
| if self._step.config.step_operator: | ||
| step_operator_name = None | ||
| if isinstance(self._step.config.step_operator, str): | ||
| step_operator_name = self._step.config.step_operator | ||
|
|
||
| self._run_step_with_step_operator( | ||
| step_operator_name=step_operator_name, | ||
| step_run_info=step_run_info, | ||
|
|
||
| # To have a cross-platform compatible handling of main thread termination | ||
| # we use Python's interrupt_main instead of termination signals (not Windows supported). | ||
| # Since interrupt_main raises KeyboardInterrupt we want in this context to capture it | ||
| # and handle it as a custom exception. | ||
|
|
||
| with ContextReraise( | ||
| source_exceptions=[KeyboardInterrupt], | ||
| target_exception=StepHeartBeatTerminationException, | ||
| message=f"Step {self._invocation_id} has been remotely stopped - terminating", | ||
| propagate_traceback=False, | ||
| ) as ctx_reraise: | ||
| logger.info( | ||
| f"Initiating heartbeat for step: {self._invocation_id}" | ||
| ) | ||
|
|
||
| heartbeat_worker = StepHeartbeatWorker(step_id=step_run.id) | ||
| heartbeat_worker.start() | ||
|
|
||
| try: | ||
| if self._step.config.step_operator: | ||
| step_operator_name = None | ||
| if isinstance(self._step.config.step_operator, str): | ||
| step_operator_name = self._step.config.step_operator | ||
|
|
||
| self._run_step_with_step_operator( | ||
| step_operator_name=step_operator_name, | ||
| step_run_info=step_run_info, | ||
| ) | ||
| else: | ||
| self._run_step_without_step_operator( | ||
| pipeline_run=pipeline_run, | ||
| step_run=step_run, | ||
| step_run_info=step_run_info, | ||
| input_artifacts=step_run.regular_inputs, | ||
| output_artifact_uris=output_artifact_uris, | ||
| ) | ||
| except StepHeartBeatTerminationException as exc: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm getting the logic wrong here, but this somehow seems off to me:
Shouldn't the logic rather be the following:
try:
heartbeat_worker.start()
run_step()
except KeyboardInterrupt:
if heartbeat_worker.is_terminated:
raise StepHeartBeatTerminationException(...)
else:
raise
except:
...
finally:
heartbeat_worker.stop()There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yy you are right this is much simpler |
||
| logger.info(ctx_reraise.message) | ||
| output_utils.remove_artifact_dirs( | ||
| artifact_uris=list(output_artifact_uris.values()) | ||
| ) | ||
| else: | ||
| self._run_step_without_step_operator( | ||
| pipeline_run=pipeline_run, | ||
| step_run=step_run, | ||
| step_run_info=step_run_info, | ||
| input_artifacts=step_run.regular_inputs, | ||
| output_artifact_uris=output_artifact_uris, | ||
| raise ( | ||
| exc | ||
| if heartbeat_worker.is_terminated | ||
| else KeyboardInterrupt | ||
| ) | ||
| except: # noqa: E722 | ||
| output_utils.remove_artifact_dirs( | ||
| artifact_uris=list(output_artifact_uris.values()) | ||
| ) | ||
| raise | ||
| except: # noqa: E722 | ||
| output_utils.remove_artifact_dirs( | ||
| artifact_uris=list(output_artifact_uris.values()) | ||
| ) | ||
| raise | ||
|
|
||
| duration = time.time() - start_time | ||
| logger.info( | ||
| f"Step `{self._invocation_id}` has finished in " | ||
| f"`{string_utils.get_human_readable_time(duration)}`." | ||
| ) | ||
| heartbeat_worker.stop() | ||
|
|
||
| duration = time.time() - start_time | ||
| logger.info( | ||
| f"Step `{self._invocation_id}` has finished in " | ||
| f"`{string_utils.get_human_readable_time(duration)}`." | ||
| ) | ||
|
|
||
| def _run_step_with_step_operator( | ||
| self, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Copyright (c) ZenML GmbH 2022. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at: | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| # or implied. See the License for the specific language governing | ||
| # permissions and limitations under the License. | ||
| """ZenML Step HeartBeat functionality.""" | ||
|
|
||
| import _thread | ||
| import logging | ||
| import threading | ||
| import time | ||
| from uuid import UUID | ||
|
|
||
| from zenml.enums import ExecutionStatus | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class StepHeartBeatTerminationException(Exception): | ||
| """Custom exception class for heartbeat termination.""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class StepHeartbeatWorker: | ||
| """Worker class implementing heartbeat polling and remote termination.""" | ||
|
|
||
| STEP_HEARTBEAT_INTERVAL_SECONDS = 60 | ||
|
|
||
| def __init__(self, step_id: UUID): | ||
| """Heartbeat worker constructor. | ||
|
|
||
| Args: | ||
| step_id: The step id heartbeat is running for. | ||
| """ | ||
| self._step_id = step_id | ||
|
|
||
| self._thread: threading.Thread | None = None | ||
| self._running: bool = False | ||
| self._terminated: bool = ( | ||
| False # one-shot guard to avoid repeated interrupts | ||
| ) | ||
|
|
||
| # properties | ||
|
|
||
| @property | ||
| def is_terminated(self) -> bool: | ||
| """Property function for termination signal. | ||
|
|
||
| Returns: | ||
| True if the worker has been terminated. | ||
| """ | ||
| return self._terminated | ||
|
|
||
| @property | ||
| def interval(self) -> int: | ||
| """Property function for heartbeat interval. | ||
|
|
||
| Returns: | ||
| The heartbeat polling interval value. | ||
| """ | ||
| return self.STEP_HEARTBEAT_INTERVAL_SECONDS | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Property function for heartbeat worker name. | ||
|
|
||
| Returns: | ||
| The name of the heartbeat worker. | ||
| """ | ||
| return f"HeartBeatWorker-{self.step_id}" | ||
|
|
||
| @property | ||
| def step_id(self) -> UUID: | ||
| """Property function for heartbeat worker step ID. | ||
|
|
||
| Returns: | ||
| The id of the step heartbeat is running for. | ||
| """ | ||
| return self._step_id | ||
|
|
||
| # public functions | ||
|
|
||
| def start(self) -> None: | ||
| """Start the heartbeat worker on a background thread.""" | ||
| if self._thread and self._thread.is_alive(): | ||
| return | ||
|
|
||
| self._running = True | ||
| self._terminated = False | ||
| self._thread = threading.Thread( | ||
| target=self._run, name=self.name, daemon=True | ||
| ) | ||
| self._thread.start() | ||
| logger.debug( | ||
| "Daemon thread %s started (interval=%s)", self.name, self.interval | ||
| ) | ||
|
|
||
| def stop(self) -> None: | ||
| """Stops the heartbeat worker.""" | ||
| if not self._running: | ||
| return | ||
| self._running = False | ||
| logger.debug("%s stop requested", self.name) | ||
|
|
||
| def is_alive(self) -> bool: | ||
| """Liveness of the heartbeat worker thread. | ||
|
|
||
| Returns: | ||
| True if the heartbeat worker thread is alive, False otherwise. | ||
| """ | ||
| t = self._thread | ||
| return bool(t and t.is_alive()) | ||
|
|
||
| def _run(self) -> None: | ||
| logger.debug("%s run() loop entered", self.name) | ||
| try: | ||
| while self._running: | ||
| try: | ||
| self._heartbeat() | ||
| except StepHeartBeatTerminationException: | ||
| # One-shot: signal the main thread and stop the loop. | ||
| if not self._terminated: | ||
| self._terminated = True | ||
| logger.info( | ||
| "%s received HeartBeatTerminationException; " | ||
| "interrupting main thread", | ||
| self.name, | ||
| ) | ||
| _thread.interrupt_main() # raises KeyboardInterrupt in main thread | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My dynamic pipelines PR introduces running multiple steps in different threads, which doesn't work with this I think. Can we somehow store the thread from which the heartbeat worker was started, and then interrupt that thread instead of the main one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that is an important change, good point. interrupt_main will not work here, we will need to change the pattern a bit. Should I work my changes from your branch? |
||
| # Ensure we stop our own loop as well. | ||
| self._running = False | ||
| except Exception as exc: | ||
| # Log-and-continue policy for all other errors. | ||
| logger.debug( | ||
| "%s heartbeat() failed with %s", self.name, str(exc) | ||
| ) | ||
| # Sleep after each attempt (even after errors, unless stopped). | ||
| if self._running: | ||
| time.sleep(self.interval) | ||
| finally: | ||
| logger.debug("%s run() loop exiting", self.name) | ||
|
|
||
| def _heartbeat(self) -> None: | ||
| from zenml.config.global_config import GlobalConfiguration | ||
|
|
||
| store = GlobalConfiguration().zen_store | ||
| response = store.update_step_heartbeat(step_run_id=self.step_id) | ||
|
|
||
| if response.status in { | ||
| ExecutionStatus.STOPPED, | ||
| ExecutionStatus.STOPPING, | ||
| }: | ||
| raise StepHeartBeatTerminationException( | ||
| f"Step {self.step_id} remotely stopped with status {response.status}." | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.