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

Nested transactions #3788

Merged
merged 7 commits into from
Jan 25, 2024
Merged

Nested transactions #3788

merged 7 commits into from
Jan 25, 2024

Conversation

codyebberson
Copy link
Member

@codyebberson codyebberson commented Jan 23, 2024

Formalizes our use of database transactions across all database usage.

This is in service of proper FHIR Bundle transaction support, described here: #1369

Once this is shipped, implementing Bundle.type = 'transaction' is this:

image

Old conventions:

  • The only concrete transactions were in the multi-table write operations
  • Otherwise, grab a database connection from getClient() as needed

New conventions:

  • By default, you should only ever use the server Repository class for database connections
  • If you don't need a transaction, use repo.getClient() for database access
    • If in an existing transaction, this will reuse the same connection and be part of the transaction
    • If not, it falls back to the existing behavior and executes a one-off query
  • If you need a transaction, wrap with repo.withTransaction((client) => { ... });
    • If already in a transaction, this updates a depth counter and reuses the connection
    • If not, it creates a new transaction
    • This adds the necessary wrappers for commit, rollback and release

Exceptions:

  • We have a few cases of database cursors for processing all rows in a table
    • For example, "reindex all resources"
    • Full table cursors are incompatible with this model, and will lock the connection
    • So we still call getClient() (not repo.getClient()) in those cases
  • Specialty operations
    • For example, Project/$expunge and CodeSystem/$import
    • These have a high degree of special logic
    • Perhaps someday they should be converted to this same model, but that can be separate work

Known issues:

  • There is one serious known issue with Redis cache cleanup in transaction rollback
  • As a team, we have discussed a number of possible solutions
    • For example, track resources to add to Redis, and only add on final COMMIT
    • Or, track all affected resource IDs, and flush those from the cache on ROLLBACK
    • Or, revisit our use of Redis cache entirely
  • This issue does not affect any of our current uses of withTransaction(), so it does not effect any current runtime implementation
  • However, it is a blocker before we can fully implement FHIR transaction bundles
  • The next step is to run some experiments and performance tests on the various strategy

Also:

  • There were a few unexpected warnings in the postgres logs for a clean run of server tests
  • This PR cleans up all of them except for a "unique constraint" error, which is expected and correct

Before:

image

After:

image

@codyebberson codyebberson requested a review from a team as a code owner January 23, 2024 16:50
Copy link

vercel bot commented Jan 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
medplum-app ⬜️ Ignored (Inspect) Visit Preview Jan 25, 2024 1:29am
medplum-storybook ⬜️ Ignored (Inspect) Visit Preview Jan 25, 2024 1:29am
medplum-www ⬜️ Ignored (Inspect) Visit Preview Jan 25, 2024 1:29am

Copy link

github-actions bot commented Jan 23, 2024

Messages
📖 @medplum/core: 153.9 kB
📖 @medplum/react: 337.0 kB

Generated by 🚫 dangerJS against 6a46e40

packages/server/src/fhir/repo.ts Show resolved Hide resolved
packages/server/src/fhir/transaction.test.ts Outdated Show resolved Hide resolved
packages/server/src/fhir/transaction.test.ts Outdated Show resolved Hide resolved
Copy link

sonarcloud bot commented Jan 25, 2024

@reshmakh reshmakh added the fhir-datastore Related to the FHIR datastore, includes API and FHIR operations label Jan 25, 2024
@codyebberson codyebberson added this pull request to the merge queue Jan 25, 2024
Merged via the queue into main with commit 2f0c3f2 Jan 25, 2024
21 checks passed
@codyebberson codyebberson deleted the cody-nested-transactions branch January 25, 2024 19:48
medplumbot added a commit that referenced this pull request Jan 31, 2024
Fixes #3794 - MeasureReport.period search (#3850)
Extra check for vmcontext bots (#3863)
Add and use vite-plugin-turbosnap (#3849)
Downgrade chromatic (#3848)
Repo sql fixes for cockroachdb (#3844)
Remove Health Gorilla from medplum-demo-bots (#3845)
fix-3815 cache presigned s3 binary urls (#3834)
Use tsvector index for token text search (#3791)
rate limit should return `OperationOutcome` (#3843)
Add global var "module" to vm context bots (#3842)
Fix lookup table tsv indexes (#3841)
Always use estimate count first (#3840)
Disambiguate getClient (#3839)
Fix invalid mermaid graph in diagnostic catalog docs (#3836)
fix-3809 race condition in Subscription extension fhir-path-criteria-expression %previous value lookup (#3810)
Fix Sonar code smells: mark React props readonly (#3832)
RDS proxy (#3827)
Fixed lookup tables in migration generator (#3830)
Fixed deprecated jest matchers (#3831)
Update README.md (#3828)
Update fhir-basics.md (#3829)
Case study content and images (#3820)
Added rdsReaderInstanceType and RDS upgrade docs (#3826)
Dependency upgrades (#3825)
Separate search popup menus for 'text' and 'token' (#3824)
Improve performance of token sort (#3823)
Additional logging (#3790)
Fix calendar input button style (#3817)
Don't add _total default in SearchControl (#3818)
Dark mode (#3814)
Fixes #3812 - FHIR profile cache bug (#3813)
Document using medplum client to integrate with external FHIR servers (#3811)
Use specific advisory locks (#3805)
Nested transactions (#3788)
Fix signin page on graphiql (#3802)
fix(heartbeat): start heartbeat on first bind to sub (#3793)
Fix async job tests (#3795)
Document using vm context bots (#3784)
Refactored access policy docs based on customer feedback (#3785)
Support Redis TLS config from Env (#3787)
feat(subscriptions): add `heartbeat` for WS subs (#3740)
Update Bot metrics (#3763)
github-merge-queue bot pushed a commit that referenced this pull request Jan 31, 2024
Fixes #3794 - MeasureReport.period search (#3850)
Extra check for vmcontext bots (#3863)
Add and use vite-plugin-turbosnap (#3849)
Downgrade chromatic (#3848)
Repo sql fixes for cockroachdb (#3844)
Remove Health Gorilla from medplum-demo-bots (#3845)
fix-3815 cache presigned s3 binary urls (#3834)
Use tsvector index for token text search (#3791)
rate limit should return `OperationOutcome` (#3843)
Add global var "module" to vm context bots (#3842)
Fix lookup table tsv indexes (#3841)
Always use estimate count first (#3840)
Disambiguate getClient (#3839)
Fix invalid mermaid graph in diagnostic catalog docs (#3836)
fix-3809 race condition in Subscription extension fhir-path-criteria-expression %previous value lookup (#3810)
Fix Sonar code smells: mark React props readonly (#3832)
RDS proxy (#3827)
Fixed lookup tables in migration generator (#3830)
Fixed deprecated jest matchers (#3831)
Update README.md (#3828)
Update fhir-basics.md (#3829)
Case study content and images (#3820)
Added rdsReaderInstanceType and RDS upgrade docs (#3826)
Dependency upgrades (#3825)
Separate search popup menus for 'text' and 'token' (#3824)
Improve performance of token sort (#3823)
Additional logging (#3790)
Fix calendar input button style (#3817)
Don't add _total default in SearchControl (#3818)
Dark mode (#3814)
Fixes #3812 - FHIR profile cache bug (#3813)
Document using medplum client to integrate with external FHIR servers (#3811)
Use specific advisory locks (#3805)
Nested transactions (#3788)
Fix signin page on graphiql (#3802)
fix(heartbeat): start heartbeat on first bind to sub (#3793)
Fix async job tests (#3795)
Document using vm context bots (#3784)
Refactored access policy docs based on customer feedback (#3785)
Support Redis TLS config from Env (#3787)
feat(subscriptions): add `heartbeat` for WS subs (#3740)
Update Bot metrics (#3763)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fhir-datastore Related to the FHIR datastore, includes API and FHIR operations
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants