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

feat: add add_cached_list to SlidingSyncBuilder and SlidingSync #1876

Merged
merged 18 commits into from
May 15, 2023

Conversation

bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented May 5, 2023

This has slightly drifted from the initial design I thought about in the issue.

Instead of having build() be fallible and mutably borrow some substate (namely BTreeMap<OwnedRoomId, SlidingSyncRoom>) from SlidingSync (that may or may not already exist), I've introduced a new add_cached_list method on SlidingSync and SlidingSyncBuilder. This method hides all the underlying machinery, and injects the room data read from the list cache into the sliding sync room map.

In particular, with these changes:

  • any list added with add_list won't be loaded from/written to the cache storage,
  • any list added with add_cached_list will be cached, and an attempt to reload it from the cache will be done during this call (hence async + Result).
  • SlidingSyncBuilder::build() now only fetches the SlidingSync data from the cache (assuming the storage key has been defined), not that of the lists anymore.

Also took the opportunity to rename pop_list to remove_list, as there's no stack/vec involved thus popping sounded a bit weird to me.

Fixes #1737.

@bnjbvr bnjbvr requested a review from a team as a code owner May 5, 2023 15:41
@bnjbvr bnjbvr requested review from jplatte and removed request for a team May 5, 2023 15:41
Signed-off-by: Benjamin Bouvier <[email protected]>
@bnjbvr
Copy link
Member Author

bnjbvr commented May 5, 2023

cc @stefanceriu, in case you want to test it, have feedback on the API, etc. :)

@codecov
Copy link

codecov bot commented May 5, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (c404e37) 76.27% compared to head (fd3c456) 76.27%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1876   +/-   ##
=======================================
  Coverage   76.27%   76.27%           
=======================================
  Files         141      141           
  Lines       16089    16089           
=======================================
  Hits        12272    12272           
  Misses       3817     3817           
Impacted Files Coverage Δ
crates/matrix-sdk/src/sliding_sync/list/builder.rs 0.00% <ø> (ø)
crates/matrix-sdk/src/sliding_sync/list/mod.rs 0.00% <ø> (ø)
crates/matrix-sdk/src/sliding_sync/mod.rs 0.00% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Hywan Hywan self-assigned this May 8, 2023
@Hywan
Copy link
Member

Hywan commented May 8, 2023

Also took the opportunity to rename pop_list to remove_list, as there's no stack/vec involved thus popping sounded a bit weird to me.

pop_list is being removed in #1758 :-).

@Hywan
Copy link
Member

Hywan commented May 8, 2023

General comment: We should merge this PR after #1758, as it could create conflicts in both case. #1758 changes the design of the builders quite a bit.

@Hywan Hywan self-requested a review May 8, 2023 07:21
Copy link
Member

@Hywan Hywan 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 to me so far. The design is nice and is definitely an improvement. Well done!

As I said, I would like to way on #1758 to be merged before merging this one as the behavior of add_list changes.

crates/matrix-sdk/src/sliding_sync/builder.rs Show resolved Hide resolved
crates/matrix-sdk/src/sliding_sync/builder.rs Outdated Show resolved Hide resolved
crates/matrix-sdk/src/sliding_sync/cache.rs Show resolved Hide resolved
crates/matrix-sdk/src/sliding_sync/cache.rs Outdated Show resolved Hide resolved

/// Total number of rooms that is possible to interact with the given list.
/// See also comment of [`SlidingSyncList::maximum_number_of_rooms`].
/// May be reloaded from the cache.
Copy link
Member

Choose a reason for hiding this comment

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

The only way to set those values are when it's reloaded from the cache. The doc' should emphasize that :-). It's not “may be reloaded from the cache” but rather “contains the default value, or the value from the cache, cannot be modified by the user”.

Question: Did we read those values from the cache previously?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we did! That was the data that was merged in set_from_cold, using observables (while in theory we should only have to use plain assignments, assuming reading from cache only happens at initialization).

Copy link
Member Author

Choose a reason for hiding this comment

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

And good point; I've actually tweaked the data stored here, by introducing a new temporary data structure that contains only the cached data. That way, we can decide in build() if we read the cached data or the default, and that puts the code assigning the defaults closer to the data read from the cache.

@stefanceriu
Copy link
Member

With #1758 now merged, should we rebase this and give it a shot?

@bnjbvr
Copy link
Member Author

bnjbvr commented May 9, 2023

Yep, this one is next on my rebase list; I can let you Stefan know once this is ready?

@bnjbvr bnjbvr requested review from Hywan and removed request for jplatte May 9, 2023 13:48
@bnjbvr
Copy link
Member Author

bnjbvr commented May 11, 2023

For what it's worth, modulo the doc issue, this is ready for review and testing.

@Hywan
Copy link
Member

Hywan commented May 11, 2023

Will review it.

Copy link
Member

@Hywan Hywan 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 to me!
@stefanceriu, can you test it before merging it please? Our tests are passing, but it's better testing than fixing.

@Hywan Hywan mentioned this pull request May 11, 2023
62 tasks
@stefanceriu
Copy link
Member

stefanceriu commented May 12, 2023

Okay so I gave it a shot but I'm a bit confused by how this is supposed to work.

If I do this: I get a A caching request was made but a storage key is missing in sliding sync error

            let slidingSync = try slidingSyncBuilder
                .addCachedList(v: visibleRoomsListBuilder)
//                .addList(listBuilder: visibleRoomsListBuilder)
//                .storageKey(name: "ElementX")

and if I do this: I get the same error

let slidingSync = try slidingSyncBuilder
                .addCachedList(v: visibleRoomsListBuilder)
//                .addList(listBuilder: visibleRoomsListBuilder)
                .storageKey(name: "ElementX")

If I do this instead I don't get an error anymore but the list doesn't publish its total count anymore either. It's always zero even though it has rooms in it

let slidingSync = try slidingSyncBuilder
                .storageKey(name: "ElementX")
                .addCachedList(v: visibleRoomsListBuilder)

I think we should get rid of the storage key entirely and only rely on the room name for the cache

L.E. Interestingly enough the other 2 lists I add later do publish their room count

2023-05-12T11:39:35.639990Z  INFO root: elementx: SlidingSyncListProxy.swift:54: AllRooms: Updated room count: 208
2023-05-12T11:39:35.640463Z  INFO root: elementx: SlidingSyncListProxy.swift:54: Invites: Updated room count: 0

@bnjbvr
Copy link
Member Author

bnjbvr commented May 12, 2023

Thanks for testing!

Indeed we should probably tweak the builders so the storage key has to be provided first, or the other method is not available until then (or have a sub-builder for caching that takes a storage key on creation). Right now the key for each list is composed of both the global storage key provided, as well as a suffix generated from the list's name. I assume that is so, in case the Rust SDK would be used in multiple apps (but in that case they would likely use different storage locations for the caches), or the same SDK used multiple times in the same app (less likely). Should we get rid of the storage key @Hywan?

Regarding the other issue, I'll need to investigate a bit more.

@bnjbvr
Copy link
Member Author

bnjbvr commented May 15, 2023

@stefanceriu confirmed (thanks!) that the latest commit fixes the issue about the initial value being not observed. I've added a test in #1922 that shows this behavior, and I can confirm it passes fine in this branch now (and didn't before the fix).

@bnjbvr
Copy link
Member Author

bnjbvr commented May 15, 2023

Regarding the possibility to remove the storage_key: I've read in another issue that the clients could support multiple accounts at some point, and that support for that in the Rust SDK would be as easy as creating another SDK client with different storage keys. So I think the storage key has to stay for that reason; I'll work on a more ergonomic API in a followup PR.

@stefanceriu
Copy link
Member

the Rust SDK would be as easy as creating another SDK client with different storage keys

So perhaps we should use the user's id as a key automagically? userId+roomName should be more than enough, no?

@poljar
Copy link
Contributor

poljar commented May 15, 2023

Regarding the possibility to remove the storage_key: I've read in another issue that the clients could support multiple accounts at some point, and that support for that in the Rust SDK would be as easy as creating another SDK client with different storage keys. So I think the storage key has to stay for that reason; I'll work on a more ergonomic API in a followup PR.

I don't think the SQLite store supports multiple users, you would need to use a completely different store path.

@stefanceriu
Copy link
Member

you would need to use a completely different store path

ah good point, the sql stores are already stored in different folders based on the user id

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

LGTM :-)

@bnjbvr bnjbvr force-pushed the reload-list-from-cache2 branch from 887394a to ea6c962 Compare May 15, 2023 15:50
@bnjbvr bnjbvr enabled auto-merge (squash) May 15, 2023 15:51
Signed-off-by: Benjamin Bouvier <[email protected]>
@bnjbvr bnjbvr merged commit 58dbe1e into matrix-org:main May 15, 2023
@bnjbvr bnjbvr deleted the reload-list-from-cache2 branch May 16, 2023 08:10
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.

[SlidingSync] Per room list cache controls
5 participants