-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the LocalExecutor to run tasks using new Task SDK supervisor …
…code This also lays the groundwork for a more general purpose "workload" execution system, make a single interface for executors to run tasks and callbacks. Also in this PR we set up the supervise function to send Task logs to a file, and handle the task log template rendering in the scheduler before queueing the workload. Additionally we don't pass the activity directly to `supervise()` but instead the properties/fields of it to reduce the coupling between SDK and Executor. (More separation will appear in PRs over the next few weeks.) The big change of note here is that rather than sending an airflow command line to execute (`["airflow", "tasks", "run", ...]`) and going back in via the CLI parser we go directly to a special purpose function. Much simpler. It doesn't remove any of the old behaviour (CeleryExecutor still uses LocalTaskJob via the CLI parser etc.), nor does anything currently send callback requests via this new workload mechanism. The `airflow.executors.workloads` module currently needs to be shared between the Scheduler (or more specifically the Executor) and the "worker" side of things. In the future these will be separate python dists and this module will need to live somewhere else. Right now we check the if `executor.queue_workload` is different from the BaseExecutor version (which just raises an error right now) to see which executors support this new version. That check will be removed as soon as all the in-tree executors have been migrated.
- Loading branch information
Showing
13 changed files
with
355 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# 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 | ||
|
||
import os | ||
import uuid | ||
from typing import TYPE_CHECKING, Literal, Union | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
if TYPE_CHECKING: | ||
from airflow.models.taskinstance import TaskInstance as TIModel | ||
from airflow.models.taskinstancekey import TaskInstanceKey | ||
|
||
|
||
__all__ = [ | ||
"All", | ||
"ExecuteTask", | ||
] | ||
|
||
|
||
class BaseActivity(BaseModel): | ||
token: str | ||
"""The identity token for this workload""" | ||
|
||
|
||
class TaskInstance(BaseModel): | ||
"""Schema for TaskInstance with minimal required fields needed for Executors and Task SDK.""" | ||
|
||
id: uuid.UUID | ||
|
||
task_id: str | ||
dag_id: str | ||
run_id: str | ||
try_number: int | ||
map_index: int | None = None | ||
|
||
# TODO: Task-SDK: see if we can replace TIKey with this class entirely? | ||
@property | ||
def key(self) -> TaskInstanceKey: | ||
from airflow.models.taskinstancekey import TaskInstanceKey | ||
|
||
return TaskInstanceKey( | ||
dag_id=self.dag_id, | ||
task_id=self.task_id, | ||
run_id=self.run_id, | ||
try_number=self.try_number, | ||
map_index=-1 if self.map_index is None else self.map_index, | ||
) | ||
|
||
|
||
class ExecuteTask(BaseActivity): | ||
"""Execute the given Task.""" | ||
|
||
ti: TaskInstance | ||
"""The TaskInstance to execute""" | ||
dag_path: os.PathLike[str] | ||
"""The filepath where the DAG can be found (likely prefixed with `DAG_FOLDER/`)""" | ||
|
||
log_filename_suffix: str | None | ||
"""The rendered log filename template the task logs should be written to""" | ||
|
||
kind: Literal["ExecuteTask"] = Field(init=False, default="ExecuteTask") | ||
|
||
@classmethod | ||
def make(cls, ti: TIModel) -> ExecuteTask: | ||
from pathlib import Path | ||
|
||
from airflow.utils.helpers import log_filename_template_renderer | ||
|
||
ser_ti = TaskInstance.model_validate(ti, from_attributes=True) | ||
path = Path(ti.dag_run.dag_model.relative_fileloc) | ||
|
||
if path and not path.is_absolute(): | ||
# TODO: What about multiple dag sub folders | ||
path = "DAGS_FOLDER" / path | ||
|
||
fname = log_filename_template_renderer()(ti=ti) | ||
return cls(ti=ser_ti, dag_path=path, token="", log_filename_suffix=fname) | ||
|
||
|
||
All = Union[ExecuteTask] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.