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(event cache store): use immediate transactions in handle_linked_chunk_updates #4410

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented Dec 12, 2024

This is unfortunate, but we're hitting a bad case of a read transaction upgrading to a write transaction, and that can plain fail if there's any other write transaction already happening in the background.

To work around this, we need to start the transaction in write mode, so start with the update statements themselves. This means some work is duplicated (fetching a linked chunk), but this should be fine since there's an index on the pair (chunk id, room id).

@bnjbvr bnjbvr requested a review from a team as a code owner December 12, 2024 13:45
@bnjbvr bnjbvr requested review from poljar and removed request for a team December 12, 2024 13:45
Copy link

codecov bot commented Dec 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.29%. Comparing base (0264e49) to head (0a2d5df).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4410      +/-   ##
==========================================
+ Coverage   85.27%   85.29%   +0.01%     
==========================================
  Files         282      282              
  Lines       31209    31219      +10     
==========================================
+ Hits        26615    26629      +14     
+ Misses       4594     4590       -4     

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

@bnjbvr bnjbvr force-pushed the bnjbvr/fix-sqlite-transaction-mode branch from 693dc8a to 1b79fa6 Compare December 12, 2024 14:09
@bnjbvr bnjbvr changed the title fix(event cache store): start RemoveChunk transactions in write mode fix(event cache store): use immediate transactions in handle_linked_chunk_updates Dec 12, 2024
Ok(())
})
.await?;
.await.unwrap()?;
Copy link
Contributor

Choose a reason for hiding this comment

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

What's that unwrap about?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copied from the implementation of with_transaction(); the only valid error this can return is because an underlying spawned task has panick'd, which is a valid reason to panic ourselves.

// Start the transaction in IMMEDIATE mode since all updates may cause writes, to
// avoid read transactions upgrading to write mode and causing SQLITE_BUSY errors.
// See also: https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
conn.set_transaction_behavior(TransactionBehavior::Immediate);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the thing go back to the default after we call drop() on the connection?

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 haven't seen any sign of such thing, but since most accesses are write anyways (even reading from the media cache causes a write access to update the last_touch time), I figured it was OK. That being said, it's not very future-proof, and there are also a few read transactions. So I think I can manually set it back to Deferred at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's set it back, otherwise me might slow down things or run into more database locked errors.

// Start the transaction in IMMEDIATE mode since all updates may cause writes, to
// avoid read transactions upgrading to write mode and causing SQLITE_BUSY errors.
// See also: https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
conn.set_transaction_behavior(TransactionBehavior::Immediate);
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's set it back, otherwise me might slow down things or run into more database locked errors.

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

Still looks good.

@bnjbvr bnjbvr force-pushed the bnjbvr/fix-sqlite-transaction-mode branch from cd16316 to 39b0f35 Compare December 12, 2024 15:18
@bnjbvr bnjbvr enabled auto-merge (rebase) December 12, 2024 15:18
…d chunk updates

If a linked chunk update starts with a RemoveChunk update, then the
transaction may start with a SELECT query and be considered a read
transaction. Soon enough, it will be upgraded into a write transaction,
because of the next UPDATE/DELETE operations that happen thereafter. If
there's another write transaction already happening, this may result in
a SQLITE_BUSY error, according to
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions

One solution is to always start the transaction in immediate mode. This
may also fail with SQLITE_BUSY according to the documentation, but it's
unclear whether it will happen in general, since we're using WAL mode
too. Let's try it out.
@bnjbvr bnjbvr force-pushed the bnjbvr/fix-sqlite-transaction-mode branch from 39b0f35 to 0a2d5df Compare December 12, 2024 16:36
@bnjbvr bnjbvr merged commit 150d9e4 into main Dec 12, 2024
40 checks passed
@bnjbvr bnjbvr deleted the bnjbvr/fix-sqlite-transaction-mode branch December 12, 2024 16:59
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