Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17.7k
Optimize DAG serialization by excluding schema default values#55849
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
80 changes: 55 additions & 25 deletions
80 airflow-core/src/airflow/serialization/serialized_objects.py
This file contains hidden or 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 |
|---|---|---|
| @@ -1094,21 +1094,7 @@ def _deserialize_params_dict(cls, encoded_params: list[tuple[str, dict]]) -> Par | ||
| return ParamsDict(op_params) | ||
| @classmethod | ||
| def get_operator_optional_fields_from_schema(cls) -> set[str]: | ||
| schema_loader = cls._json_schema | ||
| if schema_loader is None: | ||
| return set() | ||
| schema_data = schema_loader.schema | ||
| operator_def = schema_data.get("definitions", {}).get("operator", {}) | ||
| operator_fields = set(operator_def.get("properties", {}).keys()) | ||
| required_fields = set(operator_def.get("required", [])) | ||
| optional_fields = operator_fields - required_fields | ||
| return optional_fields | ||
| @classmethod | ||
| @lru_cache(maxsize=4) # Cache for "operator", "dag", and a few others | ||
| def get_schema_defaults(cls, object_type: str) -> dict[str, Any]: | ||
| """ | ||
| Extract default values from JSON schema for any object type. | ||
| @@ -1713,6 +1699,22 @@ def set_task_dag_references(task: SerializedOperator | MappedOperator, dag: Seri | ||
| # Bypass set_upstream etc here - it does more than we want | ||
| dag.task_dict[task_id].upstream_task_ids.add(task.task_id) | ||
| @classmethod | ||
| @lru_cache(maxsize=1) # Only one type: "operator" | ||
| def get_operator_optional_fields_from_schema(cls) -> set[str]: | ||
| schema_loader = cls._json_schema | ||
| if schema_loader is None: | ||
| return set() | ||
| schema_data = schema_loader.schema | ||
| operator_def = schema_data.get("definitions", {}).get("operator", {}) | ||
| operator_fields = set(operator_def.get("properties", {}).keys()) | ||
| required_fields = set(operator_def.get("required", [])) | ||
| optional_fields = operator_fields - required_fields | ||
| return optional_fields | ||
| @classmethod | ||
| def deserialize_operator( | ||
| cls, | ||
| @@ -1814,7 +1816,7 @@ def detect_dependencies(cls, op: SdkOperator) -> set[DagDependency]: | ||
| return deps | ||
| @classmethod | ||
| def _matches_client_defaults(cls, var: Any, attrname: str, op: DAGNode) -> bool: | ||
| def _matches_client_defaults(cls, var: Any, attrname: str) -> bool: | ||
| """ | ||
| Check if a field value matches client_defaults and should be excluded. | ||
| @@ -1823,7 +1825,6 @@ def _matches_client_defaults(cls, var: Any, attrname: str, op: DAGNode) -> bool: | ||
| :param var: The value to check | ||
| :param attrname: The attribute name | ||
| :param op: The operator instance | ||
| :return: True if value matches client_defaults and should be excluded | ||
| """ | ||
| try: | ||
| @@ -1851,7 +1852,7 @@ def _is_excluded(cls, var: Any, attrname: str, op: DAGNode): | ||
| :return: True if a variable is excluded, False otherwise. | ||
| """ | ||
| # Check if value matches client_defaults (hierarchical defaults optimization) | ||
| if cls._matches_client_defaults(var, attrname, op): | ||
| if cls._matches_client_defaults(var, attrname): | ||
| return True | ||
| schema_defaults = cls.get_schema_defaults("operator") | ||
| @@ -2384,25 +2385,23 @@ class SerializedDAG(BaseSerialization): | ||
| _processor_dags_folder: str | ||
| def __init__(self, *, dag_id: str) -> None: | ||
| self.catchup = airflow_conf.getboolean("scheduler", "catchup_by_default") | ||
| self.catchup = False # Schema default | ||
| self.dag_id = self.dag_display_name = dag_id | ||
| self.dagrun_timeout = None | ||
| self.deadline = None | ||
| self.default_args = {} | ||
| self.description = None | ||
| self.disable_bundle_versioning = airflow_conf.getboolean("dag_processor", "disable_bundle_versioning") | ||
| self.disable_bundle_versioning = False | ||
| self.doc_md = None | ||
| self.edge_info = {} | ||
| self.end_date = None | ||
| self.fail_fast = False | ||
| self.has_on_failure_callback = False | ||
| self.has_on_success_callback = False | ||
| self.is_paused_upon_creation = None | ||
| self.max_active_runs = airflow_conf.getint("core", "max_active_runs_per_dag") | ||
kaxil marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.max_active_tasks = airflow_conf.getint("core", "max_active_tasks_per_dag") | ||
| self.max_consecutive_failed_dag_runs = airflow_conf.getint( | ||
| "core", "max_consecutive_failed_dag_runs_per_dag" | ||
| ) | ||
| self.max_active_runs = 16 # Schema default | ||
| self.max_active_tasks = 16 # Schema default | ||
| self.max_consecutive_failed_dag_runs = 0 # Schema default | ||
| self.owner_links = {} | ||
| self.params = ParamsDict() | ||
| self.partial = False | ||
| @@ -2624,8 +2623,38 @@ def _is_excluded(cls, var: Any, attrname: str, op: DAGNode): | ||
| return False | ||
| if attrname == "dag_display_name" and var == op.dag_id: | ||
| return True | ||
| # DAG schema defaults exclusion (same pattern as SerializedBaseOperator) | ||
| dag_schema_defaults = cls.get_schema_defaults("dag") | ||
| if attrname in dag_schema_defaults: | ||
| if dag_schema_defaults[attrname] == var: | ||
| return True | ||
| optional_fields = cls.get_dag_optional_fields_from_schema() | ||
| if var is None: | ||
| return True | ||
| if attrname in optional_fields: | ||
| if var in [[], (), set(), {}]: | ||
| return True | ||
| return super()._is_excluded(var, attrname, op) | ||
| @classmethod | ||
| @lru_cache(maxsize=1) # Only one type: "dag" | ||
| def get_dag_optional_fields_from_schema(cls) -> set[str]: | ||
| schema_loader = cls._json_schema | ||
| if schema_loader is None: | ||
| return set() | ||
| schema_data = schema_loader.schema | ||
| operator_def = schema_data.get("definitions", {}).get("dag", {}) | ||
| operator_fields = set(operator_def.get("properties", {}).keys()) | ||
| required_fields = set(operator_def.get("required", [])) | ||
| optional_fields = operator_fields - required_fields | ||
| return optional_fields | ||
| @classmethod | ||
| def to_dict(cls, var: Any) -> dict: | ||
| """Stringifies DAGs and operators contained by var and returns a dict of var.""" | ||
| @@ -3798,6 +3827,7 @@ class LazyDeserializedDAG(pydantic.BaseModel): | ||
| "dag_display_name", | ||
| "has_on_success_callback", | ||
| "has_on_failure_callback", | ||
| "tags", | ||
| # Attr properties that are nullable, or have a default that loads from config | ||
| "description", | ||
| "start_date", | ||
110 changes: 94 additions & 16 deletions
110 airflow-core/tests/unit/serialization/test_dag_serialization.py
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.