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

DM-47770: Fix threadsafety of sqlalchemy MetaData access #1132

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dhirving
Copy link
Contributor

@dhirving dhirving commented Dec 10, 2024

Wrap all accesses to sqlalchemy.MetaData with a lock to avoid concurrency issues.

sqlalchemy.MetaData is documented to be threadsafe for reads, but not with concurrent modifications. We add tables dynamically at runtime, and the MetaData object is shared by all Database instances sharing the same connection pool.

Prior to adding the lock, Butler server database calls that added table definitions dynamically were sometimes failing with InvalidRequestError exceptions complaining about inconsistency of table definitions.

Checklist

  • ran Jenkins
  • added a release note for user-visible changes to doc/changes
  • (if changing dimensions.yaml) make a copy of dimensions.yaml in configs/old_dimensions

Copy link

codecov bot commented Dec 10, 2024

Codecov Report

Attention: Patch coverage is 90.69767% with 4 lines in your changes missing coverage. Please review.

Project coverage is 89.50%. Comparing base (1ba37b0) to head (4c0370c).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...n/lsst/daf/butler/registry/interfaces/_database.py 88.88% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1132   +/-   ##
=======================================
  Coverage   89.50%   89.50%           
=======================================
  Files         366      366           
  Lines       48998    49011   +13     
  Branches     5943     5941    -2     
=======================================
+ Hits        43855    43867   +12     
  Misses       3729     3729           
- Partials     1414     1415    +1     

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

@dhirving dhirving force-pushed the tickets/DM-47770 branch 2 times, most recently from 0180196 to 4736b46 Compare January 2, 2025 22:16
Wrap all accesses to sqlalchemy.MetaData with a lock to avoid concurrency issues.

sqlalchemy.MetaData is documented to be threadsafe for reads, but not with concurrent modifications.  We add tables dynamically at runtime, and the MetaData object is shared by all Database instances sharing the same connection pool.

Prior to adding the lock, Butler server database calls that added table definitions dynamically were sometimes failing with InvalidRequestError exceptions complaining about inconsistency of table definitions.
Fix an issue where a test for equality of filenames in the system temp directory were failing.  On MacOS, /private/tmp and /tmp refer to the same directory.
@dhirving dhirving marked this pull request as ready for review January 3, 2025 17:26
Copy link
Contributor

@andy-slac andy-slac left a comment

Choose a reason for hiding this comment

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

Looks good in general, but I'm not sure that name mangling is done consistently.

@@ -164,13 +165,9 @@ def addTable(self, name: str, spec: ddl.TableSpec) -> sqlalchemy.schema.Table:
to be declared in any order even in the presence of foreign key
relationships.
"""
name = self._db._mangleTableName(name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am confused, do you want DatabaseMetadata to handle table name mangling internally? I do not think it is done consistently now, you call add_table with the original name, but get_table is called with mangled name?

@@ -1192,7 +1189,7 @@ def getExistingTable(self, name: str, spec: ddl.TableSpec) -> sqlalchemy.schema.
"""
assert self._metadata is not None, "Static tables must be declared before dynamic tables."
name = self._mangleTableName(name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this one needs to be dropped too?

@@ -1206,10 +1203,7 @@ def getExistingTable(self, name: str, spec: ddl.TableSpec) -> sqlalchemy.schema.
)
if name in inspector.get_table_names(schema=self.namespace):
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, but this won't work if name is not mangled?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants