jason810496 commented on code in PR #61153: URL: https://github.com/apache/airflow/pull/61153#discussion_r2850999398
########## airflow-core/src/airflow/executors/workloads/callback.py: ########## @@ -0,0 +1,158 @@ +# 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. +"""Callback workload schemas for executor communication.""" + +from __future__ import annotations + +from enum import Enum +from importlib import import_module +from pathlib import Path +from typing import TYPE_CHECKING, Literal +from uuid import UUID + +import structlog +from pydantic import BaseModel, Field, field_validator + +from airflow.executors.workloads.base import BaseDagBundleWorkload, BundleInfo + +if TYPE_CHECKING: + from airflow.api_fastapi.auth.tokens import JWTGenerator + from airflow.models import DagRun + from airflow.models.callback import Callback as CallbackModel, CallbackKey + +log = structlog.get_logger(__name__) + + +class CallbackFetchMethod(str, Enum): + """Methods used to fetch callback at runtime.""" + + # For future use once Dag Processor callbacks (on_success_callback/on_failure_callback) get moved to executors + DAG_ATTRIBUTE = "dag_attribute" + + # For deadline callbacks since they import callbacks through the import path + IMPORT_PATH = "import_path" + + +class CallbackDTO(BaseModel): Review Comment: > How strongly do you feel about this? Not really a strong opinion on `DTO` from my side, and I agree that `TaskInstance` is confusingly overloaded here. Based on your explanation above, fair point -- I think using `DTO` is a good fit here. ########## airflow-core/src/airflow/executors/workloads/callback.py: ########## @@ -0,0 +1,158 @@ +# 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. +"""Callback workload schemas for executor communication.""" + +from __future__ import annotations + +from enum import Enum +from importlib import import_module +from pathlib import Path +from typing import TYPE_CHECKING, Literal +from uuid import UUID + +import structlog +from pydantic import BaseModel, Field, field_validator + +from airflow.executors.workloads.base import BaseDagBundleWorkload, BundleInfo + +if TYPE_CHECKING: + from airflow.api_fastapi.auth.tokens import JWTGenerator + from airflow.models import DagRun + from airflow.models.callback import Callback as CallbackModel, CallbackKey + +log = structlog.get_logger(__name__) + + +class CallbackFetchMethod(str, Enum): + """Methods used to fetch callback at runtime.""" + + # For future use once Dag Processor callbacks (on_success_callback/on_failure_callback) get moved to executors + DAG_ATTRIBUTE = "dag_attribute" + + # For deadline callbacks since they import callbacks through the import path + IMPORT_PATH = "import_path" + + +class CallbackDTO(BaseModel): + """Schema for Callback with minimal required fields needed for Executors and Task SDK.""" + + id: str # A uuid.UUID stored as a string + fetch_method: CallbackFetchMethod + data: dict + + @field_validator("id", mode="before") + @classmethod + def validate_id(cls, v): + """Convert UUID to str if needed.""" + if isinstance(v, UUID): + return str(v) + return v + + @property + def key(self) -> CallbackKey: + """Return callback ID as key (CallbackKey = str).""" + return self.id + + +class ExecuteCallback(BaseDagBundleWorkload): + """Execute the given Callback.""" + + callback: CallbackDTO + + type: Literal["ExecuteCallback"] = Field(init=False, default="ExecuteCallback") + + @classmethod + def make( + cls, + callback: CallbackModel, + dag_run: DagRun, + dag_rel_path: Path | None = None, + generator: JWTGenerator | None = None, + bundle_info: BundleInfo | None = None, + ) -> ExecuteCallback: + """Create an ExecuteCallback workload from a Callback ORM model.""" + if not bundle_info: + bundle_info = BundleInfo( + name=dag_run.dag_model.bundle_name, + version=dag_run.bundle_version, + ) + fname = f"executor_callbacks/{callback.id}" # TODO: better log file template + + return cls( + callback=CallbackDTO.model_validate(callback, from_attributes=True), + dag_rel_path=dag_rel_path or Path(dag_run.dag_model.relative_fileloc or ""), + identity_token=cls.generate_token(str(callback.id), generator), + log_path=fname, + bundle_info=bundle_info, + ) + + +def execute_callback_workload( Review Comment: Sorry that I should be more clear. > The callback is now handled exactly the same as any other TaskInstance, however the executor of choice handles those. What I mean is that, in the case of `LocalExecutor`, for callbacks we call `_execute_callback`, which in turn calls `execute_callback_workload` directly and executes the callable. https://github.com/apache/airflow/pull/61153/changes#diff-e6c8f3d778a4f3366b23d8d46a1c9650608ccbf2184e7292f0f7bbf4df37dbebR114 In contrast, for tasks we call `_execute_task`, which calls the `supervise` execution-time entrypoint. https://github.com/apache/airflow/blob/a11ec7296ce52ae3693e46835a49d8c5e3677341/airflow-core/src/airflow/executors/local_executor.py#L111-L119 The `supervise` TaskSDK entrypoint serves as a kind of isolation layer for executing any callable in Airflow. That’s why I expected we would introduce something like `airflow.sdk.execution_time.supervisor.supervise_callback`, so that callbacks are executed via the TaskSDK execution-time layer instead of running raw Python callbacks directly in the executor process. -- 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]
