forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse _run_task_session in mapped render_template_fields (apache#33309)
The `render_template_fields` method of mapped operator needs to use database session object to render mapped fields, but it cannot get the session passed by @provide_session decorator, because it is used in derived classes and we cannot change the signature without impacting those classes. So far it was done by creating new session in mapped_operator, but it has the drawback of creating an extra session while one is already created (remnder_template_fields is always run in the context of task run and it always has a session created already in _run_raw_task). It also causes problems in our tests where two opened database session accessed database at the same time and it cases sqlite exception on concurrent access and mysql error on running operations out of sync - likely when the same object was modified in both sessions. This PR changes the approach - rather than creating a new session in the mapped_operator, we are retrieving the session from one stored by the _run_raw_task. It is done by context manager and adequate protection has been added to make sure that: a) the call is made within the context manager b) context manageer is never initialized twice in the same call stack After this change, resources used by running task will be smaller, and mapped tasks will not always open 2 DB sesions. Fixes: apache#33178
- Loading branch information
Showing
8 changed files
with
377 additions
and
300 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import contextlib | ||
import logging | ||
import traceback | ||
from typing import TYPE_CHECKING | ||
|
||
from airflow.utils.session import create_session | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy.orm import Session | ||
|
||
__current_task_instance_session: Session | None = None | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def get_current_task_instance_session() -> Session: | ||
global __current_task_instance_session | ||
if not __current_task_instance_session: | ||
log.warning("No task session set for this task. Continuing but this likely causes a resource leak.") | ||
log.warning("Please report this and stacktrace below to https://github.com/apache/airflow/issues") | ||
for filename, line_number, name, line in traceback.extract_stack(): | ||
log.warning('File: "%s", %s , in %s', filename, line_number, name) | ||
if line: | ||
log.warning(" %s", line.strip()) | ||
__current_task_instance_session = create_session() | ||
return __current_task_instance_session | ||
|
||
|
||
@contextlib.contextmanager | ||
def set_current_task_instance_session(session: Session): | ||
global __current_task_instance_session | ||
if __current_task_instance_session: | ||
raise RuntimeError( | ||
"Session already set for this task. " | ||
"You can only have one 'set_current_task_session' context manager active at a time." | ||
) | ||
__current_task_instance_session = session | ||
try: | ||
yield | ||
finally: | ||
__current_task_instance_session = None |
This file contains 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.