Skip to content
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

Add dry run for backfill #45062

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions airflow/api_fastapi/core_api/datamodels/backfills.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BackfillPostBody(BaseModel):
dag_run_conf: dict = {}
reprocess_behavior: ReprocessBehavior = ReprocessBehavior.NONE
max_active_runs: int = 10
dry_run: bool = False


class BackfillResponse(BaseModel):
Expand All @@ -56,3 +57,15 @@ class BackfillCollectionResponse(BaseModel):

backfills: list[BackfillResponse]
total_entries: int


class BackfillRunInfo(BaseModel):
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved
"""Data model for run information during a backfill operation."""
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved

logical_date: datetime
pierrejeambrun marked this conversation as resolved.
Show resolved Hide resolved


class BackfillDryRunResponse(BaseModel):
"""Serializer for responses in dry-run mode for backfill operations."""
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved

run_info_list: list[BackfillRunInfo]
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved
76 changes: 76 additions & 0 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,55 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/backfills/dry_run:
post:
tags:
- Backfill
summary: Create Backfill Dry Run
operationId: create_backfill_dry_run
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/BackfillPostBody'
required: true
responses:
'200':
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/BackfillDryRunResponse'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
'403':
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
'409':
description: Conflict
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/connections/{connection_id}:
delete:
tags:
Expand Down Expand Up @@ -6216,6 +6265,18 @@ components:
- total_entries
title: BackfillCollectionResponse
description: Backfill Collection serializer for responses.
BackfillDryRunResponse:
properties:
run_info_list:
items:
$ref: '#/components/schemas/BackfillRunInfo'
type: array
title: Run Info List
type: object
required:
- run_info_list
title: BackfillDryRunResponse
description: Serializer for responses in dry-run mode for backfill operations.
BackfillPostBody:
properties:
dag_id:
Expand Down Expand Up @@ -6244,6 +6305,10 @@ components:
type: integer
title: Max Active Runs
default: 10
dry_run:
type: boolean
title: Dry Run
default: false
type: object
required:
- dag_id
Expand Down Expand Up @@ -6307,6 +6372,17 @@ components:
- updated_at
title: BackfillResponse
description: Base serializer for Backfill.
BackfillRunInfo:
properties:
logical_date:
type: string
format: date-time
title: Logical Date
type: object
required:
- logical_date
title: BackfillRunInfo
description: Data model for run information during a backfill operation.
BaseInfoResponse:
properties:
status:
Expand Down
61 changes: 60 additions & 1 deletion airflow/api_fastapi/core_api/routes/public/backfills.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Annotated

from fastapi import Depends, HTTPException, status
from sqlalchemy import select, update
from sqlalchemy import desc, select, update

from airflow.api_fastapi.common.db.common import (
AsyncSessionDep,
Expand All @@ -30,8 +30,10 @@
from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.core_api.datamodels.backfills import (
BackfillCollectionResponse,
BackfillDryRunResponse,
BackfillPostBody,
BackfillResponse,
BackfillRunInfo,
)
from airflow.api_fastapi.core_api.openapi.exceptions import (
create_openapi_http_exception_doc,
Expand All @@ -41,9 +43,14 @@
AlreadyRunningBackfill,
Backfill,
BackfillDagRun,
BackfillDagRunExceptionReason,
ReprocessBehavior,
_create_backfill,
_get_info_list,
)
from airflow.models.serialized_dag import SerializedDagModel
from airflow.utils import timezone
from airflow.utils.sqlalchemy import nulls_first
from airflow.utils.state import DagRunState

backfills_router = AirflowRouter(tags=["Backfill"], prefix="/backfills")
Expand Down Expand Up @@ -206,3 +213,55 @@ def create_backfill(
status_code=status.HTTP_409_CONFLICT,
detail=f"There is already a running backfill for dag {backfill_request.dag_id}",
)


@backfills_router.post(
path="/dry_run",
responses=create_openapi_http_exception_doc(
[
status.HTTP_404_NOT_FOUND,
status.HTTP_409_CONFLICT,
]
),
)
def create_backfill_dry_run(
backfill_request: BackfillPostBody,
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved
session: SessionDep,
) -> BackfillDryRunResponse:
from_date = timezone.coerce_datetime(backfill_request.from_date)
to_date = timezone.coerce_datetime(backfill_request.to_date)
serdag = session.scalar(SerializedDagModel.latest_item_select_object(backfill_request.dag_id))
if not serdag:
raise HTTPException(status_code=404, detail=f"Could not find dag {backfill_request.dag_id}")

info_list = _get_info_list(
dag=serdag.dag,
from_date=from_date,
to_date=to_date,
reverse=backfill_request.run_backwards,
prabhusneha marked this conversation as resolved.
Show resolved Hide resolved
)
backfill_response_item = []
for info in info_list:
dr = session.scalar(
select(DagRun)
.where(DagRun.logical_date == info.logical_date)
.order_by(nulls_first(desc(DagRun.start_date), session))
.limit(1)
)
pierrejeambrun marked this conversation as resolved.
Show resolved Hide resolved

if dr:
non_create_reason = None
if dr.state not in (DagRunState.SUCCESS, DagRunState.FAILED):
non_create_reason = BackfillDagRunExceptionReason.IN_FLIGHT
elif backfill_request.reprocess_behavior is ReprocessBehavior.NONE:
non_create_reason = BackfillDagRunExceptionReason.ALREADY_EXISTS
elif backfill_request.reprocess_behavior is ReprocessBehavior.FAILED:
if dr.state != DagRunState.FAILED:
non_create_reason = BackfillDagRunExceptionReason.ALREADY_EXISTS
if not non_create_reason:
backfill_response_item.append(BackfillRunInfo(logical_date=info.logical_date))

else:
backfill_response_item.append(BackfillRunInfo(logical_date=info.logical_date))
pierrejeambrun marked this conversation as resolved.
Show resolved Hide resolved

return BackfillDryRunResponse(run_info_list=backfill_response_item)
3 changes: 3 additions & 0 deletions airflow/ui/openapi-gen/queries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,9 @@ export type AssetServiceCreateAssetEventMutationResult = Awaited<
export type BackfillServiceCreateBackfillMutationResult = Awaited<
ReturnType<typeof BackfillService.createBackfill>
>;
export type BackfillServiceCreateBackfillDryRunMutationResult = Awaited<
ReturnType<typeof BackfillService.createBackfillDryRun>
>;
export type ConnectionServicePostConnectionMutationResult = Awaited<
ReturnType<typeof ConnectionService.postConnection>
>;
Expand Down
38 changes: 38 additions & 0 deletions airflow/ui/openapi-gen/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,44 @@ export const useBackfillServiceCreateBackfill = <
}) as unknown as Promise<TData>,
...options,
});
/**
* Create Backfill Dry Run
* @param data The data for the request.
* @param data.requestBody
* @returns BackfillDryRunResponse Successful Response
* @throws ApiError
*/
export const useBackfillServiceCreateBackfillDryRun = <
TData = Common.BackfillServiceCreateBackfillDryRunMutationResult,
TError = unknown,
TContext = unknown,
>(
options?: Omit<
UseMutationOptions<
TData,
TError,
{
requestBody: BackfillPostBody;
},
TContext
>,
"mutationFn"
>,
) =>
useMutation<
TData,
TError,
{
requestBody: BackfillPostBody;
},
TContext
>({
mutationFn: ({ requestBody }) =>
BackfillService.createBackfillDryRun({
requestBody,
}) as unknown as Promise<TData>,
...options,
});
/**
* Post Connection
* Create connection entry.
Expand Down
36 changes: 36 additions & 0 deletions airflow/ui/openapi-gen/requests/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,23 @@ export const $BackfillCollectionResponse = {
description: "Backfill Collection serializer for responses.",
} as const;

export const $BackfillDryRunResponse = {
properties: {
run_info_list: {
items: {
$ref: "#/components/schemas/BackfillRunInfo",
},
type: "array",
title: "Run Info List",
},
},
type: "object",
required: ["run_info_list"],
title: "BackfillDryRunResponse",
description:
"Serializer for responses in dry-run mode for backfill operations.",
} as const;

export const $BackfillPostBody = {
properties: {
dag_id: {
Expand Down Expand Up @@ -385,6 +402,11 @@ export const $BackfillPostBody = {
title: "Max Active Runs",
default: 10,
},
dry_run: {
type: "boolean",
title: "Dry Run",
default: false,
},
},
type: "object",
required: ["dag_id", "from_date", "to_date"],
Expand Down Expand Up @@ -468,6 +490,20 @@ export const $BackfillResponse = {
description: "Base serializer for Backfill.",
} as const;

export const $BackfillRunInfo = {
properties: {
logical_date: {
type: "string",
format: "date-time",
title: "Logical Date",
},
},
type: "object",
required: ["logical_date"],
title: "BackfillRunInfo",
description: "Data model for run information during a backfill operation.",
} as const;

export const $BaseInfoResponse = {
properties: {
status: {
Expand Down
27 changes: 27 additions & 0 deletions airflow/ui/openapi-gen/requests/services.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import type {
UnpauseBackfillResponse,
CancelBackfillData,
CancelBackfillResponse,
CreateBackfillDryRunData,
CreateBackfillDryRunResponse,
DeleteConnectionData,
DeleteConnectionResponse,
GetConnectionData,
Expand Down Expand Up @@ -960,6 +962,31 @@ export class BackfillService {
},
});
}

/**
* Create Backfill Dry Run
* @param data The data for the request.
* @param data.requestBody
* @returns BackfillDryRunResponse Successful Response
* @throws ApiError
*/
public static createBackfillDryRun(
data: CreateBackfillDryRunData,
): CancelablePromise<CreateBackfillDryRunResponse> {
return __request(OpenAPI, {
method: "POST",
url: "/public/backfills/dry_run",
body: data.requestBody,
mediaType: "application/json",
errors: {
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
409: "Conflict",
422: "Validation Error",
},
});
}
}

export class ConnectionService {
Expand Down
Loading