Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Dispute-coordinator: add LRU rw cache with write batching #5434

Closed
wants to merge 16 commits into from

Conversation

sandreim
Copy link
Contributor

@sandreim sandreim commented May 2, 2022

#5359

More details: TBD.

sandreim and others added 11 commits April 28, 2022 09:40
Signed-off-by: Andrei Sandu <[email protected]>

add logs and fix tests

Signed-off-by: Andrei Sandu <[email protected]>

this works

Signed-off-by: Andrei Sandu <[email protected]>

fix

Signed-off-by: Andrei Sandu <[email protected]>

buffer approval voting and fix tests

Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
Signed-off-by: Andrei Sandu <[email protected]>
@github-actions github-actions bot added the A3-in_progress Pull request is in progress. No review needed at this stage. label May 2, 2022
Signed-off-by: Andrei Sandu <[email protected]>
@sandreim sandreim added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels May 2, 2022
Signed-off-by: Andrei Sandu <[email protected]>
self.inner.load_recent_disputes()
let read_timer = self.metrics.time_db_read_operation("recent_disputes");
let disputes = self.inner.load_recent_disputes()?;
drop(read_timer);
Copy link
Member

Choose a reason for hiding this comment

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

For a fair comparision, we should probably include the cache update?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I missed that.

Copy link
Contributor Author

@sandreim sandreim May 3, 2022

Choose a reason for hiding this comment

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

Looking at it now - the questions is do we want to include the in-memory update time in the db read operation ? If so, I must measure the entire time spent in all of these Backend fns.

}
}

let entry = entry.expect("tested above; qed");
Copy link
Member

Choose a reason for hiding this comment

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

Can't we get rid of this expect, by just using pattern matching above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will never panic, but yeah, to play it safe I'll use a match.

@@ -11,6 +11,8 @@ parity-scale-codec = "3.1.2"
kvdb = "0.11.0"
thiserror = "1.0.30"
lru = "0.7.5"
lru-cache = "0.1.2"
Copy link
Member

Choose a reason for hiding this comment

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

why are we using two different crates for the same thing?
lru-cache is also in maintenance mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, I switched to this while I was trying to refactor using references, but I don't recall the exact reason 🥲 . I'll drop it.

/// The entry is in memory and was modified (needs to be persisted later).
Dirty,
/// The entry is cached and has been persisted in the DB.
Persisted,
Copy link
Member

@ordian ordian May 2, 2022

Choose a reason for hiding this comment

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

how Persisted is different from Cached?
why do we use a state enum instead of keeping dirty stuff separately (ala data-oriented design)?

Copy link
Contributor Author

@sandreim sandreim May 3, 2022

Choose a reason for hiding this comment

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

Persisted is the same as Cached, plus the fact that we know we wrote it to disk at least once. I was hoping to expose the state transitions as metrics in the future if this cache is useful. So that's why I am using the extra Persisted state.

We do keep stuff separately, but that's just what we evict from the LRU cache. This leads to more code and increases the complexity, as we need to lookup multiple hashmaps. I am trying to avoid this for everything else.

Copy link
Member

Choose a reason for hiding this comment

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

I meant the following layout instead:

State {
    earliest_session: Option<SessionIndex>,
    recent_disputes: Option<RecentDisputes>,
    candidate_votes: LruCache<(SessionIndex, CandidateHash), CandidateVotes>,
}
OverlayCache {
    cached: State,
    dirty: State,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would work as well, but the same applies regarding complexity. The wrapper type also enables us to observe lifetime of cache entries in the future.

#[cfg(test)]
pub const WRITE_BACK_INTERVAL: Duration = Duration::from_secs(0);

/// A read-only cache (LRU) which records changes and outputs them as `BackendWriteOp` which can
Copy link
Member

Choose a reason for hiding this comment

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

I find the read-only part confusing

@sandreim sandreim closed this Aug 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A3-in_progress Pull request is in progress. No review needed at this stage. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants