kaxil commented on code in PR #67118: URL: https://github.com/apache/airflow/pull/67118#discussion_r3270208201
########## task-sdk/src/airflow/sdk/bases/resumablemixin.py: ########## @@ -0,0 +1,135 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +import structlog + +if TYPE_CHECKING: + from airflow.sdk.definitions.context import Context + +log = structlog.get_logger(__name__) + + +class ResumableJobMixin: + """ + Mixin for operators that submit one long-running job to an external system and poll for completion. + + On retry, reads the persisted external ID from task_state and reconnects to the existing job + instead of resubmitting from scratch. Reconnect can mean different for different kind of jobs. + + Usage: call ``execute_resumable(context)`` from the operator's ``execute()`` when reconnection + is supported. + + Subclasses must implement the methods specific to their external system. The mixin owns + only ``execute_resumable()`` and the task_state read/write logic. + + Example:: + + class MyOperator(ResumableJobMixin, BaseOperator): + external_id_key = "my_job_id" + + def execute(self, context): + return self.execute_resumable(context) + + def submit_job(self, context) -> str: + return self.hook.submit(...) + + def get_job_status(self, external_id: str) -> str: + return self.hook.get_status(external_id) + + def is_job_active(self, status: str) -> bool: + return status in ("RUNNING", "PENDING") + + def is_job_succeeded(self, status: str) -> bool: + return status == "SUCCEEDED" + + def poll_until_complete(self, external_id: str, context: Context) -> None: + self.hook.poll(external_id) + + def get_job_result(self, external_id: str, context: Context) -> Any: + return None + """ + + external_id_key: str = "remote_job_id" Review Comment: The `task_state` key here is doing two jobs that probably need separating in the docstring: 1. **Mixin-internal**: the key the sync-path uses to read back the external ID on retry. 2. **Cross-implementation contract**: from your reply to Ash above, this same key is intentionally meant to be readable by a future `SparkSubmitTrigger` (and similar triggers for other providers) so deferrable implementations can reconnect to the same job. Alexhans also raised the question of whether existing ad-hoc XCom-based recovery (e.g. `resume_glue_job_on_retry` from apache/airflow#59392) should eventually migrate onto this. That second role isn't visible from the code today. Worth documenting in the class docstring: - the `external_id_key` value is a stable contract that subclasses and corresponding triggers must agree on - guidance for picking a key (suggested convention, e.g. `<provider>_<resource>_id`) so the sync and deferrable paths in the same provider don't drift - a non-goal / migration note for ad-hoc XCom-based recovery patterns Without this, nothing stops a future change to `SparkSubmitOperator.external_id_key` from silently breaking the not-yet-written `SparkSubmitTrigger`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
