-
Notifications
You must be signed in to change notification settings - Fork 16.4k
[AIRFLOW-6590] Use batch db operations in jobs #7370
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
Merged
turbaszek
merged 3 commits into
apache:master
from
PolideaInternal:AIRFLOW-6590-batch-updates
Feb 14, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -677,7 +677,7 @@ def _process_dags(self, dagbag, dags, tis_out): | |
| :param dagbag: a collection of DAGs to process | ||
| :type dagbag: airflow.models.DagBag | ||
| :param dags: the DAGs from the DagBag to process | ||
| :type dags: airflow.models.DAG | ||
| :type dags: List[airflow.models.DAG] | ||
| :param tis_out: A list to add generated TaskInstance objects | ||
| :type tis_out: list[TaskInstance] | ||
| :rtype: None | ||
|
|
@@ -796,15 +796,22 @@ def process_file(self, file_path, zombies, pickle_dags=False, session=None): | |
| # process and due to some unusual behavior. (empty() incorrectly | ||
| # returns true as described in https://bugs.python.org/issue23582 ) | ||
| ti_keys_to_schedule = [] | ||
| refreshed_tis = [] | ||
|
|
||
| self._process_dags(dagbag, dags, ti_keys_to_schedule) | ||
|
|
||
| for ti_key in ti_keys_to_schedule: | ||
| dag = dagbag.dags[ti_key[0]] | ||
| task = dag.get_task(ti_key[1]) | ||
| ti = models.TaskInstance(task, ti_key[2]) | ||
| # Refresh all task instances that will be scheduled | ||
| TI = models.TaskInstance | ||
| filter_for_tis = TI.filter_for_tis(ti_keys_to_schedule) | ||
|
|
||
| if filter_for_tis is not None: | ||
| refreshed_tis = session.query(TI).filter(filter_for_tis).with_for_update().all() | ||
|
|
||
| for ti in refreshed_tis: | ||
| # Add task to task instance | ||
| dag = dagbag.dags[ti.key[0]] | ||
| ti.task = dag.get_task(ti.key[1]) | ||
|
|
||
| ti.refresh_from_db(session=session, lock_for_update=True) | ||
| # We check only deps needed to set TI to SCHEDULED state here. | ||
| # Deps needed to set TI to QUEUED state will be batch checked later | ||
| # by the scheduler for better performance. | ||
|
|
@@ -994,8 +1001,7 @@ def _change_state_for_tis_without_dagrun(self, | |
| models.TaskInstance.task_id == subq.c.task_id, | ||
| models.TaskInstance.execution_date == | ||
| subq.c.execution_date)) \ | ||
| .update({models.TaskInstance.state: new_state}, | ||
| synchronize_session=False) | ||
| .update({models.TaskInstance.state: new_state}, synchronize_session=False) | ||
| session.commit() | ||
|
|
||
| if tis_changed > 0: | ||
|
|
@@ -1270,20 +1276,17 @@ def _change_state_for_executable_task_instances(self, task_instances, | |
| return [] | ||
|
|
||
| # set TIs to queued state | ||
| for task_instance in tis_to_set_to_queued: | ||
| task_instance.state = State.QUEUED | ||
| task_instance.queued_dttm = timezone.utcnow() | ||
| session.merge(task_instance) | ||
| filter_for_tis = TI.filter_for_tis(tis_to_set_to_queued) | ||
| session.query(TI).filter(filter_for_tis).update( | ||
| {TI.state: State.QUEUED, TI.queued_dttm: timezone.utcnow()}, synchronize_session=False | ||
| ) | ||
| session.commit() | ||
|
|
||
| # Generate a list of SimpleTaskInstance for the use of queuing | ||
| # them in the executor. | ||
| simple_task_instances = [SimpleTaskInstance(ti) for ti in | ||
| tis_to_set_to_queued] | ||
| simple_task_instances = [SimpleTaskInstance(ti) for ti in tis_to_set_to_queued] | ||
|
Contributor
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. @nuclearpinguin I believe that this should be before session.commit(). Session.commit() should flush the session, and then sqlalchemy will need to requery each TI individually to reconstruct the STI |
||
|
|
||
| task_instance_str = "\n\t".join( | ||
| [repr(x) for x in tis_to_set_to_queued]) | ||
|
|
||
| session.commit() | ||
| task_instance_str = "\n\t".join([repr(x) for x in tis_to_set_to_queued]) | ||
| self.log.info("Setting the following %s tasks to queued state:\n\t%s", | ||
| len(tis_to_set_to_queued), task_instance_str) | ||
| return simple_task_instances | ||
|
|
@@ -1398,9 +1401,12 @@ def _change_state_for_tasks_failed_to_execute(self, session): | |
| return | ||
|
|
||
| # set TIs to queued state | ||
| filter_for_tis = TI.filter_for_tis(tis_to_set_to_scheduled) | ||
| session.query(TI).filter(filter_for_tis).update( | ||
| {TI.state: State.SCHEDULED, TI.queued_dttm: None}, synchronize_session=False | ||
| ) | ||
|
|
||
| for task_instance in tis_to_set_to_scheduled: | ||
| task_instance.state = State.SCHEDULED | ||
| task_instance.queued_dttm = None | ||
| self.executor.queued_tasks.pop(task_instance.key) | ||
|
|
||
| task_instance_str = "\n\t".join( | ||
|
|
||
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.