You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/09/08 14:09:13 UTC
[GitHub] [airflow] uranusjr opened a new pull request #18088: Make next_dagrun_info take a data interval
uranusjr opened a new pull request #18088:
URL: https://github.com/apache/airflow/pull/18088
From https://github.com/apache/airflow/pull/17552#discussion_r697817943
This modifies the `Timetable.next_dagrun_info()` to take the previous run's data interval instead of logical date. `DAG.next_dagrun_info()` is modified to accept `datetime | DataInterval | None` for compatibility, but the datetime form is deprecated. When receiving a datetime, a compatibility method `DAG.infer_automated_data_interval()` is called to reverse-infer the data interval from the logical date, which should work for all DAGs prior to AIP-39 implementation.
All code paths that need `DAG.infer_automated_data_interval()` become deprecated:
* `DAG.following_schedule()`
* `DAG.next_dagrun_info()` with a datetime argument
* `DAG.create_dagrun()` without passing in a data interval
* `DagModel.calculate_dagrun_date_fields()` with a datetime argument
All existing usages of the newly deprecated code paths are rewritten to avoid triggering the deprecation warning, the most significant one being `DAG.following_schedule()`. Most of its usages are to calculate the end of the interval, replaced by `data_interval.end` instead (except those already deprecated, which continue to use `DAG.following_schedule()` to avoid any unintended change).
For clarity, `Timetable.infer_data_interval()` is renamed to `Timetable.infer_manual_data_interval()`.
Hopefully this is the last big thing we need to do before 2.2.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
uranusjr commented on pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#issuecomment-916179086
Looks ready!
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ashb commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
ashb commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704505068
##########
File path: airflow/providers/google/cloud/operators/gcs.py
##########
@@ -793,14 +793,8 @@ def __init__(
def execute(self, context: dict) -> None:
# Define intervals and prefixes.
- timespan_start = context["execution_date"]
- timespan_end = context["dag"].following_schedule(timespan_start)
- if timespan_end is None:
- self.log.warning("No following schedule found, setting timespan end to max %s", timespan_end)
- timespan_end = datetime.datetime.max
-
- timespan_start = timespan_start.replace(tzinfo=timezone.utc)
- timespan_end = timespan_end.replace(tzinfo=timezone.utc)
+ timespan_start = context["data_interval_start"].in_timezone(timezone.utc)
+ timespan_end = context["data_interval_end"].in_timezone(timezone.utc)
Review comment:
If we change the operator, then we will need to add a requirement of `apache-airflow>=2.2` on this provider, otherwise it won't work.
In this case it might be easier to have the provider work with both new an old versions of Airflow?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r736205211
##########
File path: airflow/models/dag.py
##########
@@ -557,11 +599,77 @@ def previous_schedule(self, dttm):
)
if not isinstance(self.timetable, _DataIntervalTimetable):
return None
- return self.timetable._get_prev(pendulum.instance(dttm))
+ return self.timetable._get_prev(timezone.coerce_datetime(dttm))
+
+ def get_next_data_interval(self, dag_model: "DagModel") -> DataInterval:
+ """Get the data interval of the next scheduled run.
+
+ For compatibility, this method infers the data interval from the DAG's
+ schedule if the run does not have an explicit one set, which is possible for
+ runs created prior to AIP-39.
+
+ This function is private to Airflow core and should not be depended as a
+ part of the Python API.
+
+ :meta private:
+ """
+ if self.dag_id != dag_model.dag_id:
+ raise ValueError(f"Arguments refer to different DAGs: {self.dag_id} != {dag_model.dag_id}")
+ data_interval = dag_model.next_dagrun_data_interval
+ if data_interval is not None:
+ return data_interval
+ # Compatibility: runs scheduled before AIP-39 implementation don't have an
+ # explicit data interval. Try to infer from the logical date.
+ return self.infer_automated_data_interval(dag_model.next_dagrun)
+
+ def get_run_data_interval(self, run: DagRun) -> DataInterval:
+ """Get the data interval of this run.
+
+ For compatibility, this method infers the data interval from the DAG's
+ schedule if the run does not have an explicit one set, which is possible for
+ runs created prior to AIP-39.
+
+ This function is private to Airflow core and should not be depended as a
+ part of the Python API.
+
+ :meta private:
+ """
+ if run.dag_id is not None and run.dag_id != self.dag_id:
+ raise ValueError(f"Arguments refer to different DAGs: {self.dag_id} != {run.dag_id}")
+ data_interval = _get_model_data_interval(run, "data_interval_start", "data_interval_end")
+ if data_interval is not None:
+ return data_interval
+ # Compatibility: runs created before AIP-39 implementation don't have an
+ # explicit data interval. Try to infer from the logical date.
+ return self.infer_automated_data_interval(run.execution_date)
+
+ def infer_automated_data_interval(self, logical_date: datetime) -> DataInterval:
+ """Infer a data interval for a run against this DAG.
+
+ This method is used to bridge runs created prior to AIP-39
+ implementation, which do not have an explicit data interval. Therefore,
+ this method only considers ``schedule_interval`` values valid prior to
+ Airflow 2.2.
Review comment:
In 2.2, `dag.schedule_interval` can be "backfilled" from a custom timetable by calling `timetable.summary`, which is only used for UI display. This function does not account for those values, and only considers cases where `dag.schedule_interval` is an actual valid value supplied by the user, the only possible way to set a DAG's schedule prior to AIP-39.
The reason to this is that this function is only supposed to be used to "infer" a data interval for DAG runs created prior to 2.2. The AIP-39 implementation adds two extra columns `data_interval_start` and `data_interval_end` on `DagRun`, but for performance reasons, we don't backfill those values to existing `DagRun` instances, so runs created prior to the 2.2 upgrade would have both fields set to `None`. When we need to access the data interval for such a DAG run (mostly just for UI display), this function generates it dynamically from `schedule_interval`. Since it's impossible for a DAG run created prior to AIP-39 implementation to have a schedule backed by `DAG.timetable`, this function does not take the possibility of a user-supplied timetable into account, and only implements logic for cases that can be represented by `schedule_interval` before Airflow 2.2.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ashb commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
ashb commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704512887
##########
File path: airflow/sensors/time_delta.py
##########
@@ -23,12 +23,12 @@
class TimeDeltaSensor(BaseSensorOperator):
"""
- Waits for a timedelta after the task's execution_date + schedule_interval.
- In Airflow, the daily task stamped with ``execution_date``
- 2016-01-01 can only start running on 2016-01-02. The timedelta here
- represents the time after the execution period has closed.
+ Waits for a timedelta after the run's data interval.
+ In Airflow, the daily task for data interval 2016-01-01 can only start
+ running on 2016-01-02. The timedelta here represents the time after the
Review comment:
I mean this bit isn't true anymore -- that was half the point of the AIP-39 :)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ashb commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
ashb commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704502502
##########
File path: airflow/models/dag.py
##########
@@ -2690,15 +2819,27 @@ def dags_needing_dagruns(cls, session: Session):
return with_row_locks(query, of=cls, session=session, **skip_locked(session=session))
def calculate_dagrun_date_fields(
- self, dag: DAG, most_recent_dag_run: Optional[pendulum.DateTime]
+ self,
+ dag: DAG,
+ most_recent_dag_run: Union[None, datetime, DataInterval],
) -> None:
"""
Calculate ``next_dagrun`` and `next_dagrun_create_after``
:param dag: The DAG object
:param most_recent_dag_run: DateTime of most recent run of this dag, or none if not yet scheduled.
"""
- next_dagrun_info = dag.next_dagrun_info(most_recent_dag_run)
+ if isinstance(most_recent_dag_run, datetime):
+ warnings.warn(
+ "Passing a datetime to `DagModel.most_recent_dag_run` is deprecated. "
Review comment:
```suggestion
"Passing a datetime to `DagModel.calculate_dagrun_date_fields` is deprecated. "
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704863788
##########
File path: airflow/providers/google/cloud/operators/gcs.py
##########
@@ -793,14 +793,8 @@ def __init__(
def execute(self, context: dict) -> None:
# Define intervals and prefixes.
- timespan_start = context["execution_date"]
- timespan_end = context["dag"].following_schedule(timespan_start)
- if timespan_end is None:
- self.log.warning("No following schedule found, setting timespan end to max %s", timespan_end)
- timespan_end = datetime.datetime.max
-
- timespan_start = timespan_start.replace(tzinfo=timezone.utc)
- timespan_end = timespan_end.replace(tzinfo=timezone.utc)
+ timespan_start = context["data_interval_start"].in_timezone(timezone.utc)
+ timespan_end = context["data_interval_end"].in_timezone(timezone.utc)
Review comment:
You’re right, I’ll add some compatibility code 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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr merged pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
uranusjr merged pull request #18088:
URL: https://github.com/apache/airflow/pull/18088
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ashb commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
ashb commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704497220
##########
File path: airflow/models/dag.py
##########
@@ -559,9 +601,75 @@ def previous_schedule(self, dttm):
return None
return self.timetable._get_prev(pendulum.instance(dttm))
+ def get_next_data_interval(self, dag_model: "DagModel") -> DataInterval:
+ """Get the data interval of the next scheduled run.
+
+ For compatibility, this method infers the data interval from the DAG's
+ schedule if the run does not have an explicit one set, which is possible for
+ runs created prior to AIP-39.
+
+ This function is private to Airflow core and should not be depended as a
+ part of the Python API.
+
+ :meta private:
Review comment:
👍🏻
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ashb commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
ashb commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r704500099
##########
File path: airflow/models/dag.py
##########
@@ -2150,15 +2273,22 @@ def create_dagrun(
"Creating DagRun needs either `run_id` or both `run_type` and `execution_date`"
)
- if run_type == DagRunType.MANUAL and data_interval is None and execution_date is not None:
- data_interval = self.timetable.infer_data_interval(
- run_after=timezone.coerce_datetime(execution_date),
+ logical_date = timezone.coerce_datetime(execution_date)
+ if data_interval is None and logical_date is not None:
+ warnings.warn(
+ "Calling `DAG.create_dagrun()` without an explicit data interval is deprecated",
+ DeprecationWarning,
+ stacklevel=2,
Review comment:
```suggestion
stacklevel=3,
```
Because of the decorator.
(Oh how I wish decorators could handle this automatically for us)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] github-actions[bot] commented on pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#issuecomment-915334216
The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a change in pull request #18088: Make next_dagrun_info take a data interval
Posted by GitBox <gi...@apache.org>.
dstandish commented on a change in pull request #18088:
URL: https://github.com/apache/airflow/pull/18088#discussion_r736178960
##########
File path: airflow/models/dag.py
##########
@@ -557,11 +599,77 @@ def previous_schedule(self, dttm):
)
if not isinstance(self.timetable, _DataIntervalTimetable):
return None
- return self.timetable._get_prev(pendulum.instance(dttm))
+ return self.timetable._get_prev(timezone.coerce_datetime(dttm))
+
+ def get_next_data_interval(self, dag_model: "DagModel") -> DataInterval:
+ """Get the data interval of the next scheduled run.
+
+ For compatibility, this method infers the data interval from the DAG's
+ schedule if the run does not have an explicit one set, which is possible for
+ runs created prior to AIP-39.
+
+ This function is private to Airflow core and should not be depended as a
+ part of the Python API.
+
+ :meta private:
+ """
+ if self.dag_id != dag_model.dag_id:
+ raise ValueError(f"Arguments refer to different DAGs: {self.dag_id} != {dag_model.dag_id}")
+ data_interval = dag_model.next_dagrun_data_interval
+ if data_interval is not None:
+ return data_interval
+ # Compatibility: runs scheduled before AIP-39 implementation don't have an
+ # explicit data interval. Try to infer from the logical date.
+ return self.infer_automated_data_interval(dag_model.next_dagrun)
+
+ def get_run_data_interval(self, run: DagRun) -> DataInterval:
+ """Get the data interval of this run.
+
+ For compatibility, this method infers the data interval from the DAG's
+ schedule if the run does not have an explicit one set, which is possible for
+ runs created prior to AIP-39.
+
+ This function is private to Airflow core and should not be depended as a
+ part of the Python API.
+
+ :meta private:
+ """
+ if run.dag_id is not None and run.dag_id != self.dag_id:
+ raise ValueError(f"Arguments refer to different DAGs: {self.dag_id} != {run.dag_id}")
+ data_interval = _get_model_data_interval(run, "data_interval_start", "data_interval_end")
+ if data_interval is not None:
+ return data_interval
+ # Compatibility: runs created before AIP-39 implementation don't have an
+ # explicit data interval. Try to infer from the logical date.
+ return self.infer_automated_data_interval(run.execution_date)
+
+ def infer_automated_data_interval(self, logical_date: datetime) -> DataInterval:
+ """Infer a data interval for a run against this DAG.
+
+ This method is used to bridge runs created prior to AIP-39
+ implementation, which do not have an explicit data interval. Therefore,
+ this method only considers ``schedule_interval`` values valid prior to
+ Airflow 2.2.
Review comment:
@uranusjr I stumbled onto this comment. It's a little confusing.
In particular this:
```
this method only considers ``schedule_interval`` values valid prior to Airflow 2.2.
```
Could you possibly clarify what it means?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org