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

Merged
merged 3 commits into from
Jan 6, 2025

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 (952a2f8).
Report is 4 commits behind head on main.

✅ 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%   -0.01%     
==========================================
  Files         366      366              
  Lines       48998    49007       +9     
  Branches     5943     5941       -2     
==========================================
+ Hits        43855    43863       +8     
  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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am just going to remove it entirely. It's a no-op -- the only implementation returns the name unchanged. I think it is a holdover from the Oracle implementation.

The existing implementation was already incorrect because there were several places where name mangling was applied multiple times. I noticed that the two cases I already removed were double-applying the mangling (because it is done internally in _convertTableSpec) so I had removed those, but apparently the logic is also wrong in a lot of other places.

Copy link
Member

Choose a reason for hiding this comment

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

My recollection is that it was PostgreSQL, not Oracle, that needed name shrinking. It may have been index and constraint names more than table names where it was important, but I'm pretty sure PostgreSQL is the one with a 64-char limit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two different things. The name shrinking logic is still there.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, sorry for the noise.

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

This table name mangling logic is a holdover from the Oracle implementation and the current implementation is a no-op.  The logic was being inconsistently applied so none of this would have worked even in a new implementation that had a real implementation of the mangling function.
@dhirving dhirving merged commit eaace2a into main Jan 6, 2025
19 checks passed
@dhirving dhirving deleted the tickets/DM-47770 branch January 6, 2025 19:33
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.

3 participants