-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Two-token mechanism for task execution to prevent token expiration while tasks wait in executor queues #60108
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
Open
anishgirianish
wants to merge
17
commits into
apache:main
Choose a base branch
from
anishgirianish:fix/token-expiration-worker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−61
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0c79eb0
added two token mechanism for task execution
anishgirianish b3e8509
fix failing tests
anishgirianish 2ac1c70
fix failing test
anishgirianish 98da680
further enhanced the implementation
anishgirianish f97cb69
further clean ups
anishgirianish 6e4aa84
clean ups
anishgirianish d4d29b7
test fixes
anishgirianish 520760c
fix jwt bearer class not registered
anishgirianish 8d9e9d2
clean ups uneccesary overrides
anishgirianish 5e31c4e
clean ups
anishgirianish 94c1db6
add back the override
anishgirianish ac0b227
clean up ovveride
anishgirianish f7a70ac
rollback to explicit override
anishgirianish dea02eb
use real container
anishgirianish 5f3f914
refactor in process container override
anishgirianish 540e381
bring back the override
anishgirianish a336f6e
refactored token checks and dependecy
anishgirianish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
anishgirianish marked this conversation as resolved.
Show resolved
Hide resolved
|
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
anishgirianish marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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 |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ | |
| import attrs | ||
| import structlog | ||
| from cadwyn import VersionedAPIRouter | ||
| from fastapi import Body, HTTPException, Query, status | ||
| from fastapi import Body, Depends, HTTPException, Query, Response, status | ||
| from pydantic import JsonValue | ||
| from sqlalchemy import func, or_, tuple_, update | ||
| from sqlalchemy.engine import CursorResult, Row | ||
|
|
@@ -38,6 +38,7 @@ | |
| from structlog.contextvars import bind_contextvars | ||
|
|
||
| from airflow._shared.timezones import timezone | ||
| from airflow.api_fastapi.auth.tokens import JWTGenerator | ||
| from airflow.api_fastapi.common.dagbag import DagBagDep, get_latest_version_of_dag | ||
| from airflow.api_fastapi.common.db.common import SessionDep | ||
| from airflow.api_fastapi.common.types import UtcDateTime | ||
|
|
@@ -59,7 +60,7 @@ | |
| TISuccessStatePayload, | ||
| TITerminalStatePayload, | ||
| ) | ||
| from airflow.api_fastapi.execution_api.deps import JWTBearerTIPathDep | ||
| from airflow.api_fastapi.execution_api.deps import DepContainer, JWTBearerTIPathDep, JWTBearerWorkloadScope | ||
| from airflow.exceptions import TaskNotFound | ||
| from airflow.models.asset import AssetActive | ||
| from airflow.models.dag import DagModel | ||
|
|
@@ -78,6 +79,12 @@ | |
| if TYPE_CHECKING: | ||
| from sqlalchemy.sql.dml import Update | ||
|
|
||
| log = structlog.get_logger(__name__) | ||
|
|
||
| JWTBearerWorkloadDep = Depends(JWTBearerWorkloadScope(path_param_name="task_instance_id")) | ||
|
|
||
| ti_run_router = VersionedAPIRouter(dependencies=[JWTBearerWorkloadDep]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need this extra router, when we already have |
||
|
|
||
| router = VersionedAPIRouter() | ||
|
|
||
| ti_id_router = VersionedAPIRouter( | ||
|
|
@@ -88,10 +95,7 @@ | |
| ) | ||
|
|
||
|
|
||
| log = structlog.get_logger(__name__) | ||
|
|
||
|
|
||
| @ti_id_router.patch( | ||
| @ti_run_router.patch( | ||
| "/{task_instance_id}/run", | ||
| status_code=status.HTTP_200_OK, | ||
| responses={ | ||
|
|
@@ -101,11 +105,13 @@ | |
| }, | ||
| response_model_exclude_unset=True, | ||
| ) | ||
| def ti_run( | ||
| async def ti_run( | ||
| task_instance_id: UUID, | ||
| ti_run_payload: Annotated[TIEnterRunningPayload, Body()], | ||
| session: SessionDep, | ||
| dag_bag: DagBagDep, | ||
| response: Response, | ||
| services=DepContainer, | ||
| ) -> TIRunContext: | ||
| """ | ||
| Run a TaskInstance. | ||
|
|
@@ -279,6 +285,11 @@ def ti_run( | |
| context.next_method = ti.next_method | ||
| context.next_kwargs = ti.next_kwargs | ||
|
|
||
| # Generate short-lived execution token for subsequent API calls | ||
| generator: JWTGenerator = await services.aget(JWTGenerator) | ||
| execution_token = generator.generate(extras={"sub": ti_id_str}) | ||
| response.headers["X-Execution-Token"] = execution_token | ||
|
|
||
| return context | ||
| except SQLAlchemyError: | ||
| log.exception("Error marking Task Instance state as running") | ||
|
|
||
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
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.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we use svcs? Why do we need to implement our version of it?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still getting familiar with the codebase, but from what I understand, I added this to fix ServiceNotFoundError failures in CI. With InProcessExecutionAPI, the svcs lifespan runs later (when transport is accessed), but services like JWTGenerator are needed before that. This container bypasses the lifecycle and returns pre-created instances from app.state. I may well be missing something - if there's a cleaner pattern you'd recommend, I'd really appreciate the guidance.