jason810496 commented on code in PR #75267:
URL: https://github.com/apache/airflow/pull/75267#discussion_r4735201936
##########
airflow-core/src/airflow/serialization/serialized_objects.py:
##########
@@ -2178,9 +2178,15 @@ def fill_config_defaults(cls, serialized_obj: dict[str,
Any]) -> None:
):
if key not in dag:
dag[key] = get(section, option)
- if (execution_timeout := conf.gettimedelta("core",
"default_task_execution_timeout")) is not None:
- task_defaults = serialized_obj.setdefault("client_defaults",
{}).setdefault("tasks", {})
- task_defaults.setdefault("execution_timeout",
execution_timeout.total_seconds())
+ # Native runtimes cannot read Airflow's task defaults, so supply the
table a Python Dag
+ # carries. generate_client_defaults() already drops every value equal
to its schema
+ # default, which leaves exactly the config-backed fields. A default
the payload already
+ # carries wins.
+ client_defaults = serialized_obj.setdefault("client_defaults", {})
+ client_defaults["tasks"] = {
+ **OperatorSerialization.generate_client_defaults(),
+ **client_defaults.get("tasks", {}),
+ }
Review Comment:
Since #76359, the coordinator uses the worker's `[core]
default_task_execution_timeout` only when `ti_context.execution_timeout` is
None. Filling it here makes the Dag processor's value always win.
I reproduced it on this PR rebased onto main. With 300 on the processor,
`get_execution_timeout()` returns 300.0. A worker set to 600 never applies its
value. main fills it too, but this PR reworks the table, so it's a good place
to fix. Could we leave `execution_timeout` unset and let the worker apply its
default?
Two smaller things in this block:
- It always writes `client_defaults: {"tasks": {}}`. That is why
`test_keeps_a_setting_the_dag_sets` fails in CI.
- It runs before the `try` in `lang_sdk_processor.py`. A `"client_defaults":
null` raises `AttributeError` instead of an import error.
The suggestion covers all three.
```suggestion
# Native runtimes cannot read Airflow's task defaults, so supply the
table a Python Dag
# carries. generate_client_defaults() already drops every value
equal to its schema
# default, which leaves exactly the config-backed fields. A default
the payload already
# carries wins. execution_timeout stays unset so the worker applies
its own default.
task_defaults = {
key: value
for key, value in
OperatorSerialization.generate_client_defaults().items()
if key != "execution_timeout"
}
if not task_defaults:
return
client_defaults = serialized_obj.setdefault("client_defaults", {})
if not isinstance(client_defaults, dict) or not
isinstance(client_defaults.get("tasks", {}), dict):
return # validate_serialized_dag reports the malformed payload.
client_defaults["tasks"] = {**task_defaults,
**client_defaults.get("tasks", {})}
```
##########
airflow-core/adr/lang-sdk/0004-dag-parsing.md:
##########
@@ -231,6 +231,16 @@ The language runtime must produce a `DagFileParsingResult`
that matches Python A
| `@continuous` | `airflow.timetables.simple.ContinuousTimetable` | `{}` |
| cron expr | `airflow.timetables.trigger.CronTriggerTimetable` |
`{expression, timezone, interval, run_immediately}` |
+**Task config defaults:** A language runtime writes every task field the
author set and leaves out
+the ones it did not; a value equal to the schema default is still written. A
missing task field is
+filled from the optional `client_defaults.tasks` table, which core supplies
from its own config
+because a language runtime cannot read it. Only config-backed task fields are
supplied (`owner`,
+`queue`, `retries`, `retry_delay`, `weight_rule`, `execution_timeout`,
`email_on_failure` and
+`email_on_retry`). A field present in the serialized task wins over the table,
so an explicit
Review Comment:
To match the `execution_timeout` change above:
```suggestion
because a language runtime cannot read it. Only config-backed task fields
are supplied (`owner`,
`queue`, `retries`, `retry_delay`, `weight_rule`, `email_on_failure` and
`email_on_retry`).
`execution_timeout` is not supplied, so the worker applies its own `[core]
default_task_execution_timeout`.
A field present in the serialized task wins over the table, so an explicit
```
##########
airflow-core/tests/unit/serialization/test_dag_serialization.py:
##########
@@ -5466,24 +5466,65 @@ def test_keeps_a_setting_the_dag_sets(self):
assert data == before
- @conf_vars({("core", "default_task_execution_timeout"): "300"})
- def
test_fills_the_default_execution_timeout_of_tasks_from_the_config(self):
- with DAG(dag_id="native_dag", schedule=None) as dag:
- BaseOperator(task_id="unset")
- BaseOperator(task_id="set",
execution_timeout=timedelta(seconds=60))
- MockOperator.partial(task_id="mapped").expand(arg1=[1, 2])
- data = DagSerialization.to_dict(dag)
- data.pop("client_defaults", None)
-
- DagSerialization.fill_config_defaults(data)
-
- tasks = DagSerialization.from_dict(data).task_dict
+ def
test_fills_the_default_execution_timeout_of_tasks_from_the_config(self,
operator_defaults):
+ with operator_defaults({"execution_timeout": timedelta(seconds=300)}):
+ with DAG(dag_id="native_dag", schedule=None) as dag:
+ BaseOperator(task_id="unset")
+ BaseOperator(task_id="set",
execution_timeout=timedelta(seconds=60))
+ MockOperator.partial(task_id="mapped").expand(arg1=[1, 2])
+ data = DagSerialization.to_dict(dag)
+ data.pop("client_defaults", None)
+
+ DagSerialization.fill_config_defaults(data)
+
+ tasks = DagSerialization.from_dict(data).task_dict
assert {task_id: task.execution_timeout for task_id, task in
tasks.items()} == {
"unset": timedelta(seconds=300),
"set": timedelta(seconds=60),
"mapped": timedelta(seconds=300),
}
Review Comment:
With `execution_timeout` left to the worker, this test would become:
```suggestion
def test_leaves_execution_timeout_to_the_worker(self, operator_defaults):
with operator_defaults({"execution_timeout":
timedelta(seconds=300)}):
with DAG(dag_id="native_dag", schedule=None) as dag:
BaseOperator(task_id="unset")
data = DagSerialization.to_dict(dag)
data.pop("client_defaults", None)
data["dag"]["tasks"][0]["__var"].pop("execution_timeout", None)
DagSerialization.fill_config_defaults(data)
assert "execution_timeout" not in data.get("client_defaults",
{}).get("tasks", {})
assert
DagSerialization.from_dict(data).get_task("unset").execution_timeout is None
```
##########
airflow-core/tests/unit/serialization/test_dag_serialization.py:
##########
@@ -5466,24 +5466,65 @@ def test_keeps_a_setting_the_dag_sets(self):
assert data == before
- @conf_vars({("core", "default_task_execution_timeout"): "300"})
- def
test_fills_the_default_execution_timeout_of_tasks_from_the_config(self):
- with DAG(dag_id="native_dag", schedule=None) as dag:
- BaseOperator(task_id="unset")
- BaseOperator(task_id="set",
execution_timeout=timedelta(seconds=60))
- MockOperator.partial(task_id="mapped").expand(arg1=[1, 2])
- data = DagSerialization.to_dict(dag)
- data.pop("client_defaults", None)
-
- DagSerialization.fill_config_defaults(data)
-
- tasks = DagSerialization.from_dict(data).task_dict
+ def
test_fills_the_default_execution_timeout_of_tasks_from_the_config(self,
operator_defaults):
+ with operator_defaults({"execution_timeout": timedelta(seconds=300)}):
+ with DAG(dag_id="native_dag", schedule=None) as dag:
+ BaseOperator(task_id="unset")
+ BaseOperator(task_id="set",
execution_timeout=timedelta(seconds=60))
+ MockOperator.partial(task_id="mapped").expand(arg1=[1, 2])
+ data = DagSerialization.to_dict(dag)
+ data.pop("client_defaults", None)
+
+ DagSerialization.fill_config_defaults(data)
+
+ tasks = DagSerialization.from_dict(data).task_dict
assert {task_id: task.execution_timeout for task_id, task in
tasks.items()} == {
"unset": timedelta(seconds=300),
"set": timedelta(seconds=60),
"mapped": timedelta(seconds=300),
}
+ def test_fills_task_defaults_without_replacing_explicit_values(self,
operator_defaults):
+ with operator_defaults({"retries": 3, "queue": "celery_q"}):
+ with DAG(dag_id="native_dag", schedule=None) as dag:
+ BaseOperator(task_id="unset")
+ BaseOperator(task_id="explicit", retries=0, queue="default")
+
+ data = DagSerialization.to_dict(dag)
+ data.pop("client_defaults", None)
+ task_vars = {task["__var"]["task_id"]: task["__var"] for task in
data["dag"]["tasks"]}
+ task_vars["unset"].pop("retries", None)
+ task_vars["unset"].pop("queue", None)
+ task_vars["explicit"]["retries"] = 0
+ task_vars["explicit"]["queue"] = "default"
+
+ DagSerialization.fill_config_defaults(data)
+ tasks = DagSerialization.from_dict(data).task_dict
+
+ assert data["client_defaults"]["tasks"]["retries"] == 3
+ assert data["client_defaults"]["tasks"]["queue"] == "celery_q"
+ assert tasks["unset"].retries == 3
+ assert tasks["unset"].queue == "celery_q"
+ assert tasks["explicit"].retries == 0
+ assert tasks["explicit"].queue == "default"
Review Comment:
Only `retries`, `queue` and `execution_timeout` are checked. `owner`,
`retry_delay`, `weight_rule` and `email_on_*` are never checked as filled or as
kept. Could we parametrize over every config-backed field? Something like:
```suggestion
@pytest.mark.parametrize(
("field", "configured", "explicit", "expected_unset",
"expected_explicit"),
[
pytest.param("owner", "data_team", "airflow", "data_team",
"airflow", id="owner"),
pytest.param("queue", "celery_q", "default", "celery_q",
"default", id="queue"),
pytest.param("retries", 3, 0, 3, 0, id="retries"),
pytest.param(
"retry_delay",
timedelta(seconds=60),
300.0,
timedelta(seconds=60),
timedelta(seconds=300),
id="retry_delay",
),
pytest.param(
"weight_rule",
"absolute",
"downstream",
validate_and_load_priority_weight_strategy("absolute"),
validate_and_load_priority_weight_strategy("downstream"),
id="weight_rule",
),
pytest.param("email_on_failure", False, True, False, True,
id="email_on_failure"),
pytest.param("email_on_retry", False, True, False, True,
id="email_on_retry"),
],
)
def test_fills_task_defaults_without_replacing_explicit_values(
self, operator_defaults, field, configured, explicit,
expected_unset, expected_explicit
):
with operator_defaults({field: configured}):
with DAG(dag_id="native_dag", schedule=None) as dag:
BaseOperator(task_id="unset")
BaseOperator(task_id="explicit")
data = DagSerialization.to_dict(dag)
data.pop("client_defaults", None)
task_vars = {task["__var"]["task_id"]: task["__var"] for task in
data["dag"]["tasks"]}
task_vars["unset"].pop(field, None)
task_vars["explicit"][field] = explicit
DagSerialization.fill_config_defaults(data)
tasks = DagSerialization.from_dict(data).task_dict
assert getattr(tasks["unset"], field) == expected_unset
assert getattr(tasks["explicit"], field) == expected_explicit
```
##########
airflow-core/adr/lang-sdk/0004-dag-parsing.md:
##########
@@ -231,6 +231,16 @@ The language runtime must produce a `DagFileParsingResult`
that matches Python A
| `@continuous` | `airflow.timetables.simple.ContinuousTimetable` | `{}` |
| cron expr | `airflow.timetables.trigger.CronTriggerTimetable` |
`{expression, timezone, interval, run_immediately}` |
+**Task config defaults:** A language runtime writes every task field the
author set and leaves out
+the ones it did not; a value equal to the schema default is still written. A
missing task field is
+filled from the optional `client_defaults.tasks` table, which core supplies
from its own config
+because a language runtime cannot read it. Only config-backed task fields are
supplied (`owner`,
+`queue`, `retries`, `retry_delay`, `weight_rule`, `execution_timeout`,
`email_on_failure` and
+`email_on_retry`). A field present in the serialized task wins over the table,
so an explicit
+`retries=0` is not replaced by a non-zero `[core] default_task_retries`. In Go
a non-pointer
+scalar at its zero value counts as unset and takes the configured default; a
pointer field
+expresses an explicit zero.
Review Comment:
This keeps the #74094 bug for Go. `Retries` is an `int` and
`ExecutionTimeout` a `time.Duration`, so zero reads as unset. I checked:
`TaskSpec{Retries: 0}` writes no `retries` key, so core fills `[core]
default_task_retries`. An explicit `ExecutionTimeout: 0` (no timeout) is lost
the same way.
Could we make both pointers, like `PoolSlots`? It's an override in
`go-sdk/internal/genspec/authoring.go`:
```go
"execution_timeout": {goType: "time.Duration", imports: []string{"time"},
pointer: true},
"retries": {goType: "int", pointer: true},
```
##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Serde.kt:
##########
@@ -156,14 +151,13 @@ private fun serializeTask(
// A trigger task takes the Dag's queue too, because the Java runtime runs
it.
val config =
if (dagQueue == null || "queue" in def.configValues) def.configValues else
def.configValues + ("queue" to dagQueue)
- // Emit only config entries that differ from their schema default, mirroring
- // Python BaseSerialization's "omit hard-coded default" behavior, which the
Go
- // and TypeScript SDKs mirror too. Operator fields are stored unwrapped, so
the
- // __type encoding is stripped. If core grows a task-level
fill_config_defaults,
- // every SDK has to keep explicitly set values instead, or an explicit
retries=0
- // reads as unset and picks up the configured default.
+ // Preserve every task config entry explicitly set by the Dag author, even
+ // when it equals the schema default. Core fills omitted config-backed fields
+ // from its own config; dropping an explicit default here would let that fill
+ // override the author's value. Operator fields are stored unwrapped, so
strip
+ // the __type encoding.
config.forEach { (key, value) ->
- if (key !in OMITTED_TASK_KEYS &&
!matchesSchemaDefault(SchemaFields.TASK[key], value)) {
+ if (key !in OMITTED_TASK_KEYS) {
Review Comment:
Nit: nothing reads `Field.defaultJson` in the generated `SchemaFields` now.
Could `build.gradle.kts` stop generating it, like the Go `schemaDefault`
removal here?
--
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]