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(sdk): Introduce linked_chunk::Updates #3384

Merged
merged 9 commits into from
May 12, 2024

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented May 6, 2024

This patch introduces linked_chunk::Updates, which is a new type to handle all updates of a LinkedChunk. Updates is basically a wrapper around Vec<Update>: a LinkedChunk emits Updates, that can be retrieved/read by Updates basically. Update is useful for replicating the LinkedChunk in a database for example, or for creating streams like the yet-to-be-implemented LinkedChunk::subscribe_as_vector.

This patch has started with 2 methods on Updates: take to read and to consume updates (that's useful for databases) and peek to read updates without consuming them (that's useful for subscriber, and later LinkedChunk::subscribe_as_vector). However, it has changed during the development of this patch to a multi-readers API. The code and the commit messages explain the whys and hows in details.

This patch finally introduces linked_chunk::UpdateSubscriber, which implements Stream. That's the foundation of LinkedChunk::subscribe_as_vector.

My advice is to review this PR commit by commit, it will be much easier.


…hunkUpdates` type.

This patch updates the `LinkedChunk::update_history` field from
a simple `Vec<LinkedChunkUpdate<Item, Gap>>` type to the new
`LinkedChunkUpdates<Item, Gap>` type (note the plural).

This is going to be helpul for the next patches.
@Hywan Hywan force-pushed the feat-sdk-observable-linked-chunk branch from 60cfc29 to 12bc2ed Compare May 6, 2024 15:24
This patch adds the `LinkedChunkUpdates::peek` method. This is
a new channel to read the updates without consuming them, like
`LinkedChunkUpdates::take` does.

The complexity is: when do we clear/drop the updates then? We don't
want to keep them in memory forever. Initially `take` was clearing
the updates, but now that we can read them with `peek` too, who's
responsible to clear them? Enter `garbage_collect`. First off,
we already need to maintain 2 index, resp. `last_taken_index` and
`last_peeked_index` so that `take` and `peek` don't return already
returned updates. They respectively know the index of the last update
that has been read. We can use this information to know which updates
must be garbage collected: that's all updates below the two index.
Tadaa. Simple. The only _drawback_ (if it can be considered as such)
is that the garbage collection happens on the next call to `take` or
`peek` (because of the borrow checker). That's not really a big deal in
practise. We could make it happens immediately when calling `take` or
`peek` but it needs more pointer arithmetic and a less straighforward
code.
@Hywan Hywan force-pushed the feat-sdk-observable-linked-chunk branch from 12bc2ed to d691579 Compare May 6, 2024 18:13
Hywan added 3 commits May 6, 2024 20:15
`LinkedChunkUpdate` implements `Clone` if and only if `Item` and `Gap`
both implement `Clone`.
This patch implements `LinkedChunkUpdates::subscribe` and
`LinkedChunkUpdateSubscriber`, which itself implements `Stream`.

This patch splits `LinkedChunkUpdates` into `LinkedChunkUpdatesInner`,
so that the latter can be shared with `LinkedChunkUpdatesSubscriber`.
@Hywan Hywan force-pushed the feat-sdk-observable-linked-chunk branch from 0db7664 to f492dfa Compare May 8, 2024 10:36
@Hywan Hywan marked this pull request as ready for review May 8, 2024 10:43
@Hywan Hywan requested a review from a team as a code owner May 8, 2024 10:43
@Hywan Hywan requested review from andybalaam and removed request for a team May 8, 2024 10:43
Copy link

codecov bot commented May 8, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 83.21%. Comparing base (2255cd5) to head (1f26822).
Report is 3 commits behind head on main.

Files Patch % Lines
...matrix-sdk/src/event_cache/linked_chunk/updates.rs 89.83% 6 Missing ⚠️
...tes/matrix-sdk/src/event_cache/linked_chunk/mod.rs 95.74% 2 Missing ⚠️
crates/matrix-sdk/src/event_cache/store.rs 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3384      +/-   ##
==========================================
+ Coverage   83.14%   83.21%   +0.07%     
==========================================
  Files         243      244       +1     
  Lines       25196    25238      +42     
==========================================
+ Hits        20950    21003      +53     
+ Misses       4246     4235      -11     

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

@Hywan Hywan changed the title feat(sdk): Introduce LinkedChunkUpdates feat(sdk): Introduce linked_chunk::Updates May 8, 2024
Hywan added 2 commits May 8, 2024 13:45
This patch removes the notion of `take` vs. `peek` from
`LinkedChunkUpdatesInner` and widens the problem to a more general
approach: `LinkedChunkUpdatesInner` must support multiple readers, not
only two (`take` was the first reader, `peek` was the second reader,
kind of).

Why do we need multiple readers? `LinkedChunkUpdates::take` is
clearly the first reader, it's part of the public API. But the private
API needs to read the updates too, without consuming them, like
`LinkedChunkUpdatesSubscriber`. `peek` was nice for that, but it's
possible to have multiple `LinkedChunkUpdatesSubscriber` at the same
time! Hence the need to widen the approach from 2 readers to many
readers.

This patch introduces a `ReaderToken` to identify readers. The last
indexes are now all stored in a `HashMap<ReaderToken, usize>`. The rest
of the modifications are the consequence of that.

The test `test_updates_take_and_peek` has been entirely rewritten to be
`test_updates_take_and_garbage_collector` where it tests 2 readers and
see how the garbage collector reacts to that.
@Hywan Hywan force-pushed the feat-sdk-observable-linked-chunk branch from f492dfa to 1493dc2 Compare May 8, 2024 11:45
Copy link
Member

@andybalaam andybalaam left a comment

Choose a reason for hiding this comment

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

Really nice commit messages, thank you!

@Hywan Hywan merged commit c0924c8 into matrix-org:main May 12, 2024
35 checks passed
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