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

fix(trino): db session error in handle cursor #31265

Merged

Conversation

justinpark
Copy link
Member

SUMMARY

In the thread execution function, the query is referenced, and since it occupies the session within the thread scope, it became impossible to perform session transactions related to the query in the handle_cursor that runs separately.
This resulted in errors for multi-statement queries. This commit changes the code to ensure that the database object is only referenced in the thread, thereby separating the session scope.
Additionally, it resolves the issue where handle_cursor would skip sleep when a multi-statement is executing, allowing access before execution.

2024-11-25 18:35:16,271:DEBUG:superset.db_engine_specs.trino:Query 18151072: Handling cursor
2024-11-25 18:35:16,271:DEBUG:superset.db_engine_specs.trino:Query 18151072: queryId 20241125_183516_08494_6vvqw found in cursor
2024-11-25 18:35:16,273:DEBUG:superset.sql_lab:Query 18151072: This session is in 'prepared' state; no further SQL can be emitted within this transaction.
2024-11-25 18:35:16,277:INFO:backoff:Backing off get_query(...) for 0.9s (superset.sql_lab.SqlLabException: Failed at getting query)
2024-11-25 18:35:16,277:ERROR:superset.sqllab.sql_json_executer:Query 18151072 failed unexpectedly
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/base.py", line 1094, in _commit_impl
    self.engine.dialect.do_commit(self.connection)
  File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/default.py", line 686, in do_commit
    dbapi_connection.commit()
MySQLdb.OperationalError: (2013, 'Lost connection to MySQL server during query')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/superset-internal/apache-superset/superset/sql_lab.py", line 302, in execute_sql_statement
    db_engine_spec.execute_with_cursor(cursor, sql, query)
  File "/srv/superset-internal/apache-superset/superset/db_engine_specs/trino.py", line 269, in execute_with_cursor
    cls.handle_cursor(cursor, query)

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

No error thrown

TESTING INSTRUCTIONS

Run a multi-statement query in trino db, such like select 1; select 2;

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added the data:connect:trino Related to Trino label Dec 3, 2024
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.80%. Comparing base (76d897e) to head (d8f1dad).
Report is 1125 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #31265       +/-   ##
===========================================
+ Coverage   60.48%   83.80%   +23.31%     
===========================================
  Files        1931      536     -1395     
  Lines       76236    38943    -37293     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    32637    -13477     
+ Misses      28017     6306    -21711     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 48.78% <0.00%> (-0.39%) ⬇️
javascript ?
mysql 76.57% <0.00%> (?)
postgres 76.62% <0.00%> (?)
presto 53.28% <0.00%> (-0.53%) ⬇️
python 83.80% <100.00%> (+20.31%) ⬆️
sqlite 76.09% <0.00%> (?)
unit 60.87% <100.00%> (+3.25%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -216,6 +216,8 @@ def handle_cursor(cls, cursor: Cursor, query: Query) -> None:
if tracking_url := cls.get_tracking_url(cursor):
query.tracking_url = tracking_url

db.session.commit()
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move the global override to the line?

Suggested change
db.session.commit()
db.session.commit() # pylint: disable=consider-using-transaction

@@ -283,6 +286,8 @@ def _execute(
)
execute_thread.start()

# Wait for the thread to start before continuing
time.sleep(0.1)
Copy link
Member

Choose a reason for hiding this comment

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

@villebro @betodealmeida this is being added to make the thread run once but I think it's not guaranteed. Do you think we could change the initial condition while not cursor.query_id to avoid having 2 sleeps?

Copy link
Member

Choose a reason for hiding this comment

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

@michael-s-molina I don't think that would work, as .set() is only called once the thread is completed. Should we perhaps be checking that execute_thread.is_alive() is returning True before proceeding? I think that's what we're trying to do here.

Copy link
Member

Choose a reason for hiding this comment

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

@justinpark Could you try @villebro's suggestion?

@pull-request-size pull-request-size bot added size/S and removed size/XS labels Dec 3, 2024
@justinpark justinpark merged commit 1e0c04f into apache:master Dec 3, 2024
38 checks passed
michael-s-molina pushed a commit that referenced this pull request Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants