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(schema): reconnect closed connection #804

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

shcheklein
Copy link
Member

@shcheklein shcheklein commented Jan 8, 2025

Fixes #660

Refactors DataTable by removing direct access to sqlachemy (and thus requiring direct access to sa.Engine, etc). This allows us to do a check and reconnect if connection was already closed.

Copy link

cloudflare-workers-and-pages bot commented Jan 8, 2025

Deploying datachain-documentation with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8e9b78a
Status: ✅  Deploy successful!
Preview URL: https://f84e35e5.datachain-documentation.pages.dev
Branch Preview URL: https://fix-660-unexpected-closed-co.datachain-documentation.pages.dev

View logs

Copy link

codecov bot commented Jan 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.44%. Comparing base (fc8f940) to head (8e9b78a).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #804   +/-   ##
=======================================
  Coverage   87.43%   87.44%           
=======================================
  Files         128      128           
  Lines       11332    11338    +6     
  Branches     1529     1530    +1     
=======================================
+ Hits         9908     9914    +6     
  Misses       1046     1046           
  Partials      378      378           
Flag Coverage Δ
datachain 87.37% <100.00%> (+<0.01%) ⬆️

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.

@shcheklein shcheklein force-pushed the fix-660/unexpected-closed-connection branch from d60772f to 8e9b78a Compare January 9, 2025 02:19
@@ -150,14 +151,12 @@ class DataTable:
def __init__(
self,
name: str,
engine: "Engine",
metadata: Optional["sa.MetaData"] = None,
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure what was the case for it to be None and for this code below:

self.metadata: sa.MetaData = metadata if metadata is not None else sa.MetaData()

that initializes its own copy of MetaData()

I haven't found any use case so far. It is simpler this way when internals of the DatabaseEngine are not exposed into DataTable.

Let's see if tests pass.

if self.is_closed:
# Reconnect in case of being closed previously.
self._reconnect()
return super().get_table(name)
Copy link
Member Author

Choose a reason for hiding this comment

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

get_table might trigger a connection and that triggers closed database

@shcheklein shcheklein marked this pull request as ready for review January 9, 2025 02:35
@shcheklein shcheklein self-assigned this Jan 9, 2025
@shcheklein shcheklein added the bug Something isn't working label Jan 9, 2025
@shcheklein shcheklein requested a review from a team January 9, 2025 02:36
Copy link
Contributor

@dreadatour dreadatour left a comment

Choose a reason for hiding this comment

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

👍

@shcheklein shcheklein merged commit e3e617c into main Jan 9, 2025
39 checks passed
@shcheklein shcheklein deleted the fix-660/unexpected-closed-connection branch January 9, 2025 02:43
@@ -220,7 +219,7 @@ def dataset_select_paginated(
num_yielded = 0

# Ensure we're using a thread-local connection
with self.clone() as wh:
with self.clone(use_new_connection=True) as wh:
Copy link
Member

Choose a reason for hiding this comment

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

Was this added because clone() could reuse the closed connection by default? Maybe this could be fixed on clickhouse backend instead of having to add this flag everywhere?

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 flag was added for CH as far as I remember, good catch @skshetry I'm not sure it is needed in this place

(not sure it worth refactoring this flag atm, I'll just revert this flag in this place)

Comment on lines +190 to +192
if self.is_closed:
# Reconnect in case of being closed previously.
self._reconnect()
Copy link
Member

@skshetry skshetry Jan 9, 2025

Choose a reason for hiding this comment

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

Ideally, this should be automatic.

@property
def db(self):
  if self._db.is_closed:
      self._reconnect()
  return self._db

I see executemany, execute_str and other methods that are not reconnecting.
Although engine and metadata do complicate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

After some failures datachain is getting stuck in closed database error
3 participants