-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
chore: Cleanup database sessions #10427
chore: Cleanup database sessions #10427
Conversation
66cbad4
to
48d2435
Compare
eb87522
to
e72bca2
Compare
Codecov Report
@@ Coverage Diff @@
## master #10427 +/- ##
==========================================
+ Coverage 65.98% 71.38% +5.39%
==========================================
Files 604 408 -196
Lines 32376 13339 -19037
Branches 3282 3282
==========================================
- Hits 21364 9522 -11842
+ Misses 10824 3705 -7119
+ Partials 188 112 -76
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report at Codecov.
|
38d3c67
to
c5ea651
Compare
Note the async SQL Lab queries are failing but only for MySQL and I'm not entirely sure why. I speculate it may be due to the query status update not being committed. |
@john-bodley I have a similar problem on Vertica connections that I noticed recently. Not sure if it's related, was planning to look more closely at the problem but didn't yet have the time. I should be able to review this and the other PR in the coming days. |
c5ea651
to
17fffde
Compare
Note in terms of the failing tests I've reverted some of my changes in
which may be necessary as some of the Celery tasks first try to drop a table and may throw an exception if the table exists. Ending the transaction may be necessary prior to running the async query. |
Excellent work @john-bodley, but I would prefer the
instead of
|
@dpgaspar agreed especially given circular import concerns. I'll make the change. |
9f3ef4d
to
de08084
Compare
@@ -197,18 +197,17 @@ def set_database_uri(database_name: str, uri: str) -> None: | |||
) | |||
def refresh_druid(datasource: str, merge: bool) -> None: | |||
"""Refresh druid datasources""" | |||
session = db.session() |
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.
This is the same session per here and added unnecessary obfuscation.
@dpgaspar I've addressed your comments. |
1 similar comment
@dpgaspar I've addressed your comments. |
bb97099
to
c94f6f7
Compare
4db7eef
to
c4618ba
Compare
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.
Uhh, kudos for the cleanup effort..! I wish we had a linter that could ban passing a Session
to avoid this building up post this PR. Just one comment, happy to approve if you don't see any risk with removing the scoped session.
dbsession = db.create_scoped_session() | ||
schedule = dbsession.query(model_cls).get(schedule_id) | ||
schedule = db.session.query(model_cls).get(schedule_id) |
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.
This has me wondering, is there risk that this gets torn down prematurely by the initiating session's closure? My intuition says it shouldn't, but it seems strange that this would have been put here by accident.
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.
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 threw this into the codebase, and the result was False
:
>>> scoped_session = db.create_scoped_session()
>>> print(scoped_session is db.session)
False
Perhaps we should leave these out for now and address them in a follow-up PR?
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.
@villebro possibly, though the scoped session is merely running a query as far as I can tell. There’s no complex logic, transaction, etc. being used.
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.
@john-bodley good point.
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.
LGTM (needs a rebase).
c4618ba
to
6cc13eb
Compare
6cc13eb
to
4d1645f
Compare
Co-authored-by: John Bodley <john.bodley@airbnb.com>
Co-authored-by: John Bodley <john.bodley@airbnb.com>
This reverts commit 7645fc8.
This reverts commit 7645fc8.
This reverts commit 7645fc8.
Co-authored-by: John Bodley <john.bodley@airbnb.com>
This reverts commit 7645fc8.
SUMMARY
At Airbnb we've been running into issues with PyMySQL on Aurora MySQL 5.7. A Google search suggests the problem may be related to thread safety and possibly that Apache Superset may not be leveraging SQLAlchemy's scoped sessions correctly.
After grokking the code I realized that we were somewhat inconsistent with how we handle sessions with the metadata database. Ideally we should be leaning on a single instance of Flask-SQLAlchemy's scope session which is safely removed on teardown. I noticed examples of where we created additional new local sessions (rather than the global session) which were only sometimes closed and/or removed. Note locally scoped session refer to the same global scope session per here.
This PR simply ensures that all ORM interactions with the metadata database use the
db.session
construct wheredb
is the Flask-SQLAlchemy object provided by FAB. Also it seems redundant and adds unnecessary obfuscation to pass around thedb.session
and thus a number of function signatures have been updated.Note sadly this change did not remedy the issue we were seeing, and thus I wonder if the probably lies with a third party library or something else within Apache Superset. I've been able to successfully use the SQLAlchemy ORM using scoped sessions without issue via a toy example. That said it could help with any connection pool issues we sporadically see, i.e., using the global Flask-SQLAlchemy scoped session ensures that any existing transactional/connection resources will be released (which wasn't the case previously).
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TEST PLAN
CI.
ADDITIONAL INFORMATION