ramitkataria commented on code in PR #55088:
URL: https://github.com/apache/airflow/pull/55088#discussion_r2356471577
##########
airflow-core/docs/howto/deadline-alerts.rst:
##########
@@ -97,6 +100,55 @@ Airflow provides several built-in reference points that you
can use with Deadlin
``DeadlineReference.FIXED_DATETIME``
Specifies a fixed point in time. Useful when Dags must complete by a
specific time.
+``DeadlineReference.AVERAGE_RUNTIME``
+ Calculates deadlines based on the average runtime of previous DAG runs.
This reference
+ analyzes historical execution data to predict when the current run should
complete.
+ The deadline is set to the current time plus the calculated average
runtime plus the interval.
+ If insufficient historical data exists, no deadline is created.
+
+ Parameters:
+ * ``limit`` (int, optional): Maximum number of recent DAG runs to
analyze. Defaults to 10.
+ * ``min_runs`` (int, optional): Minimum number of completed runs
required to calculate average. Defaults to same value as ``limit``.
+
+ Example usage:
+
+ .. code-block:: python
+
+ # Use default settings (analyze up to 10 runs, require 10 runs)
+ DeadlineReference.AVERAGE_RUNTIME()
+
+ # Analyze up to 20 runs but calculate with minimum 5 runs
+ DeadlineReference.AVERAGE_RUNTIME(limit=20, min_runs=5)
+
+ # Strict: require exactly 15 runs to calculate
+ DeadlineReference.AVERAGE_RUNTIME(limit=15, min_runs=15)
+
+Here's an example using average runtime:
+
+.. code-block:: python
+
+ with DAG(
+ dag_id="average_runtime_deadline",
+ deadline=DeadlineAlert(
+ reference=DeadlineReference.AVERAGE_RUNTIME(limit=15, min_runs=5),
+ interval=timedelta(minutes=30), # Alert if 30 minutes past
expected completion
+ callback=AsyncCallback(
+ SlackWebhookNotifier,
+ kwargs={"text": "🚨 DAG {{ dag_run.dag_id }} is running longer
than expected!"},
+ ),
+ ),
+ ):
+ EmptyOperator(task_id="data_processing")
+
+The timeline for this example would look like this:
+
+::
+
+ |------|----------|---------|------------|--------|
+ Queued Start Expected Deadline
+ 09:00 09:05 09:35 10:05
+ (avg+30min)
Review Comment:
```suggestion
(avg + 30 min)
```
##########
airflow-core/src/airflow/models/deadline.py:
##########
@@ -366,6 +367,76 @@ def _evaluate_with(self, *, session: Session, **kwargs:
Any) -> datetime:
return _fetch_from_db(DagRun.queued_at, session=session, **kwargs)
+ @dataclass
+ class AverageRuntimeDeadline(BaseDeadlineReference):
+ """A deadline that calculates the average runtime from past DAG
runs."""
+
+ DEFAULT_LIMIT = 10
+ limit: int
Review Comment:
+1
##########
airflow-core/docs/howto/deadline-alerts.rst:
##########
@@ -97,6 +100,55 @@ Airflow provides several built-in reference points that you
can use with Deadlin
``DeadlineReference.FIXED_DATETIME``
Specifies a fixed point in time. Useful when Dags must complete by a
specific time.
+``DeadlineReference.AVERAGE_RUNTIME``
+ Calculates deadlines based on the average runtime of previous DAG runs.
This reference
+ analyzes historical execution data to predict when the current run should
complete.
+ The deadline is set to the current time plus the calculated average
runtime plus the interval.
+ If insufficient historical data exists, no deadline is created.
+
+ Parameters:
+ * ``limit`` (int, optional): Maximum number of recent DAG runs to
analyze. Defaults to 10.
+ * ``min_runs`` (int, optional): Minimum number of completed runs
required to calculate average. Defaults to same value as ``limit``.
+
+ Example usage:
+
+ .. code-block:: python
+
+ # Use default settings (analyze up to 10 runs, require 10 runs)
+ DeadlineReference.AVERAGE_RUNTIME()
+
+ # Analyze up to 20 runs but calculate with minimum 5 runs
+ DeadlineReference.AVERAGE_RUNTIME(limit=20, min_runs=5)
+
+ # Strict: require exactly 15 runs to calculate
+ DeadlineReference.AVERAGE_RUNTIME(limit=15, min_runs=15)
+
+Here's an example using average runtime:
+
+.. code-block:: python
+
+ with DAG(
+ dag_id="average_runtime_deadline",
+ deadline=DeadlineAlert(
+ reference=DeadlineReference.AVERAGE_RUNTIME(limit=15, min_runs=5),
+ interval=timedelta(minutes=30), # Alert if 30 minutes past
expected completion
Review Comment:
```suggestion
interval=timedelta(minutes=30), # Alert if 30 minutes past
average runtime
```
##########
airflow-core/src/airflow/models/deadline.py:
##########
@@ -366,6 +367,95 @@ def _evaluate_with(self, *, session: Session, **kwargs:
Any) -> datetime:
return _fetch_from_db(DagRun.queued_at, session=session, **kwargs)
+ @dataclass
+ class AverageRuntimeDeadline(BaseDeadlineReference):
+ """A deadline that calculates the average runtime from past DAG
runs."""
+
+ DEFAULT_LIMIT = 10
+ limit: int
+ min_runs: int | None = None
Review Comment:
If we verify the user input and apply defaults in the Task SDK definition,
then I think we should just be able to assume that min_runs is provided and is
valid
```suggestion
min_runs: int
```
(and also remove `__post_init__` if we go ahead with this)
##########
task-sdk/src/airflow/sdk/definitions/deadline.py:
##########
@@ -299,6 +300,14 @@ class TYPES:
DAGRUN_LOGICAL_DATE: DeadlineReferenceType =
ReferenceModels.DagRunLogicalDateDeadline()
DAGRUN_QUEUED_AT: DeadlineReferenceType =
ReferenceModels.DagRunQueuedAtDeadline()
+ @classmethod
+ def AVERAGE_RUNTIME(cls, limit: int = 0, min_runs: int | None = None) ->
DeadlineReferenceType:
Review Comment:
```suggestion
def AVERAGE_RUNTIME(cls, limit: int =
cls.ReferenceModels.AverageRuntimeDeadline.DEFAULT_LIMIT, min_runs: int =
cls.ReferenceModels.AverageRuntimeDeadline.DEFAULT_LIMIT) ->
DeadlineReferenceType:
return cls.ReferenceModels.AverageRuntimeDeadline(limit, min_runs)
```
--
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]