Allow retrieving Variables, Connections, XCom without breaking HA Locks#14016
Closed
kaxil wants to merge 1 commit intoapache:masterfrom
Closed
Allow retrieving Variables, Connections, XCom without breaking HA Locks#14016kaxil wants to merge 1 commit intoapache:masterfrom
kaxil wants to merge 1 commit intoapache:masterfrom
Conversation
kaxil
commented
Feb 2, 2021
airflow/utils/session.py
Outdated
Member
Author
There was a problem hiding this comment.
Both the file changes are not strictly needed. Either of the two file changes are sufficient.
In one cases we commit an empty session and in other case we don't commit a session if it is empty.
ashb
requested changes
Feb 2, 2021
airflow/utils/sqlalchemy.py
Outdated
Member
There was a problem hiding this comment.
This can't be here - even if the session is clean, if we allow a COMMIT the lock will be released
Member
Author
There was a problem hiding this comment.
Yeah, I tried with session.execute("update dag set is_active=False where dag_id='example_branch_operator'") where session._is_clean() is True -- so this logic does not work -- going to close the PR
airflow/utils/sqlalchemy.py
Outdated
drdenzy
reviewed
Feb 2, 2021
closes apache#13811 Previously, because we use `create_session` context manager with or without `@provide_session`: we always used to commit the session if the session is not explicitly passed to a function example: ``` dag_run = task_instance.get_dagrun() File "/usr/local/lib/python3.7/site-packages/airflow/utils/session.py", line 65, in wrapper return func(*args, session=session, **kwargs) File "/usr/local/lib/python3.7/contextlib.py", line 119, in __exit__ next(self.gen) File "/usr/local/lib/python3.7/site-packages/airflow/utils/session.py", line 32, in create_session session.commit() File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1042, in commit self.transaction.commit() File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 504, in commit self._prepare_impl() File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl self.session.dispatch.before_commit(self.session) File "/usr/local/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 322, in __call__ fn(*args, **kw) File "/usr/local/lib/python3.7/site-packages/airflow/utils/sqlalchemy.py", line 217, in _validate_commit raise RuntimeError("UNEXPECTED COMMIT - THIS WILL BREAK HA LOCKS!") ``` The same happens when using `Variable.get()` because of https://github.com/apache/airflow/blob/594069ee061e9839b2b12aa43aa3a23e05beed86/airflow/secrets/metastore.py#L55-L70 This commit makes sure that we only commit the session if we add a new object or modify an existing object in the session by using `session._is_clean`: https://github.com/sqlalchemy/sqlalchemy/blob/25ee5a05df0daeb7dc7ba432172d6abc76ffab56/lib/sqlalchemy/orm/session.py#L3236-L3241 ```python def _is_clean(self): return ( not self.identity_map.check_modified() and not self._deleted and not self._new ) ```
1b02611 to
6482502
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
closes #13811
Previously, because we use
create_sessioncontext manager with orwithout
@provide_session: we always used to commit the session ifthe session is not explicitly passed to a function example:
The same happens when using
Variable.get()because ofairflow/airflow/secrets/metastore.py
Lines 55 to 70 in 594069e
This commit makes sure that we only commit the session if we add a new object or modify an existing object in the session
by using
session._is_clean:https://github.com/sqlalchemy/sqlalchemy/blob/25ee5a05df0daeb7dc7ba432172d6abc76ffab56/lib/sqlalchemy/orm/session.py#L3236-L3241
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.