Skip to content
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

Merged

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Jul 25, 2020

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 where db is the Flask-SQLAlchemy object provided by FAB. Also it seems redundant and adds unnecessary obfuscation to pass around the db.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

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 2 times, most recently from 66cbad4 to 48d2435 Compare July 25, 2020 23:08
@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 2 times, most recently from eb87522 to e72bca2 Compare July 26, 2020 01:29
@codecov-commenter
Copy link

codecov-commenter commented Jul 26, 2020

Codecov Report

Merging #10427 into master will increase coverage by 5.39%.
The diff coverage is n/a.

Impacted file tree graph

@@            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     
Flag Coverage Δ
#cypress 54.48% <ø> (?)
#javascript 59.72% <ø> (-0.01%) ⬇️
#python ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...src/components/FilterableTable/FilterableTable.tsx 81.77% <0.00%> (-0.41%) ⬇️
superset/views/database/mixins.py
superset/examples/paris.py
superset/charts/commands/update.py
superset/charts/commands/exceptions.py
superset/dashboards/api.py
superset/models/annotations.py
superset/dashboards/commands/exceptions.py
superset/datasets/commands/refresh.py
superset/db_engine_specs/athena.py
... and 315 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update edaf785...c4618ba. Read the comment docs.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 4 times, most recently from 38d3c67 to c5ea651 Compare July 26, 2020 16:38
@john-bodley
Copy link
Member Author

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.

@villebro
Copy link
Member

@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.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch from c5ea651 to 17fffde Compare July 26, 2020 17:15
@john-bodley
Copy link
Member Author

john-bodley commented Jul 26, 2020

Note in terms of the failing tests I've reverted some of my changes in tests/celery_tests.py which removed some session.close() statements. Per the Session.close documentation,

This clears all items and ends any transaction in progress.

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.

@dpgaspar
Copy link
Member

Excellent work @john-bodley, but I would prefer the db.session imports to be:

from superset.extensions import db

instead of

from superset import db

@john-bodley
Copy link
Member Author

@dpgaspar agreed especially given circular import concerns. I'll make the change.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 2 times, most recently from 9f3ef4d to de08084 Compare July 26, 2020 21:46
@@ -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()
Copy link
Member Author

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.

@john-bodley
Copy link
Member Author

@dpgaspar I've addressed your comments.

1 similar comment
@john-bodley
Copy link
Member Author

@dpgaspar I've addressed your comments.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 3 times, most recently from bb97099 to c94f6f7 Compare July 30, 2020 05:36
@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch 3 times, most recently from 4db7eef to c4618ba Compare July 30, 2020 05:57
Copy link
Member

@villebro villebro left a 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)
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@villebro I'm unsure though I believe the scoped session is actually the same instance as db.session per here, i.e.,

>>> Session = scoped_session(session_factory)
>>> some_session = Session()
>>> some_other_session = Session()
>>> some_session is some_other_session
True

Copy link
Member

@villebro villebro Jul 30, 2020

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@john-bodley good point.

Copy link
Member

@villebro villebro left a 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).

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch from c4618ba to 6cc13eb Compare July 31, 2020 05:36
@john-bodley john-bodley force-pushed the john-bodley--cleanup-sessions branch from 6cc13eb to 4d1645f Compare July 31, 2020 05:45
@john-bodley john-bodley merged commit 7645fc8 into apache:master Jul 31, 2020
JasonD28 pushed a commit to JasonD28/incubator-superset that referenced this pull request Aug 3, 2020
Co-authored-by: John Bodley <john.bodley@airbnb.com>
JasonD28 pushed a commit to JasonD28/incubator-superset that referenced this pull request Aug 3, 2020
Co-authored-by: John Bodley <john.bodley@airbnb.com>
etr2460 pushed a commit to etr2460/incubator-superset that referenced this pull request Aug 6, 2020
etr2460 pushed a commit that referenced this pull request Aug 6, 2020
etr2460 pushed a commit to airbnb/superset-fork that referenced this pull request Aug 6, 2020
Ofeknielsen pushed a commit to ofekisr/incubator-superset that referenced this pull request Oct 5, 2020
auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
Co-authored-by: John Bodley <john.bodley@airbnb.com>
auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.38.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/XXL 🚢 0.38.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants