Skip to content

Conversation

@bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented Aug 27, 2025

See also MSC4308 for details about the bumpstamps. They allow to order two subscriptions, and avoid bad race conditions where one would get a thread subscription by both paginating and sync'ing them.

It's a bit of a shame that the code comparing bumpstamps looks a bit duplicated, but note that, among the 3 implementations, only 2 are duplicated, for real: the last one uses the PersistedThreadSubscription in lieu of StoreThreadSubscription type.

Part of #5038.

@bnjbvr bnjbvr requested a review from a team as a code owner August 27, 2025 15:53
@bnjbvr bnjbvr requested review from poljar and removed request for a team August 27, 2025 15:53
@codecov
Copy link

codecov bot commented Aug 27, 2025

Codecov Report

❌ Patch coverage is 81.88406% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.65%. Comparing base (371ed49) to head (32ef255).
⚠️ Report is 4 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...tes/matrix-sdk-base/src/store/integration_tests.rs 77.46% 0 Missing and 16 partials ⚠️
crates/matrix-sdk-sqlite/src/state_store.rs 71.42% 3 Missing and 5 partials ⚠️
crates/matrix-sdk-base/src/store/mod.rs 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5589      +/-   ##
==========================================
- Coverage   88.65%   88.65%   -0.01%     
==========================================
  Files         345      345              
  Lines       96132    96225      +93     
  Branches    96132    96225      +93     
==========================================
+ Hits        85230    85312      +82     
+ Misses       6676     6674       -2     
- Partials     4226     4239      +13     

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

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 27, 2025

CodSpeed Performance Report

Merging #5589 will not alter performance

Comparing bnjbvr/thread-sub-bumpstamps (32ef255) with main (371ed49)

Summary

✅ 49 untouched benchmarks

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.

Not really requesting many changes but I'm not sure if I misunderstood something of if this is incorrect in some edge cases.

Comment on lines +486 to +503
impl ThreadSubscriptionStatus {
/// Represent the status as a static string ref, for it to be stored into a
/// persistent format.
///
/// Note: this is serialized in some databases implementations, so make sure
/// to not change it lightly, and keep it in sync with
/// [`Self::from_str`].
pub fn as_str(&self) -> &'static str {
match self {
ThreadSubscriptionStatus::Subscribed { automatic } => {
if *automatic {
"automatic"
} else {
"manual"
}
}
ThreadSubscriptionStatus::Unsubscribed => "unsubscribed",
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why we're using as_str() for database storage instead of our usual Serialize serde stuff?

Additionally if we use any of the more usual traits for this, then we can use insta to add more protection to this. Display, Debug, or Serialize would work.

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes for a denser format to use as_str() (it flattens the subscription into a string, instead of a JSON map), and it allows future use cases like "find the list of all subscribed threads in this room" (which we couldn't do if we serialized the ThreadSubscription as a blob inside the DB, b/o encrypted DBs).

Copy link
Contributor

Choose a reason for hiding this comment

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

Well I'm not saying that you shouldn't flatten out the serialized format, just that a different trait is usually used for this. A custom Serialize implementation can produce the same serialized format as you already have.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it'd be wrong to use a custom Serialize impl in the sqlite impl, which has status and bump_stamps columns.

Comment on lines 980 to 982
(Some(prev_bump), Some(new_bump)) if new_bump <= prev_bump => {
return Ok(());
}
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 will avoid a transition from unsubscribed to subscribed if the bump stamps are equal, no?

Is such a transition impossible or undesirable?

Furthermore, as you said, this logic is duplicated a couple of times. Can't we make the bump stamp a separate type BumpStamp(u64) and the implement either a is_better() method or even add a PartialEq implementation that will contain this logic?

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 think it's not possible, because the bump stamps will increase if the thread subscription goes from subscribed to unsubscribed. My understanding is that same bump stamp = same subscription information; it forms an "index" when combined with the (room id, thread root event id) pair. Will ask to clarify this in the MSC, and maybe I can add a small comment here.

But yeah, the duplication isn't great, will find a way to avoid it.

@bnjbvr bnjbvr requested a review from poljar August 29, 2025 10:07
…nal consumers, and rename previous one to `StoredThreadSubscription`

External consumers are likely not interested about unsubscriptions and
the bump stamp values themselves, so let's not expose these to them.
@bnjbvr bnjbvr force-pushed the bnjbvr/thread-sub-bumpstamps branch from bb71983 to 32ef255 Compare September 1, 2025 08:18
@bnjbvr bnjbvr enabled auto-merge (rebase) September 1, 2025 08:19
@bnjbvr bnjbvr merged commit 9f22f55 into main Sep 1, 2025
51 checks passed
@bnjbvr bnjbvr deleted the bnjbvr/thread-sub-bumpstamps branch September 1, 2025 08:38
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