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

Keep data in fails cases in sync service #2361

Open
wants to merge 36 commits into
base: master
Choose a base branch
from

Conversation

AurelienFT
Copy link
Contributor

@AurelienFT AurelienFT commented Oct 15, 2024

Linked Issues/PRs

Closes #2357

Description

This pull request introduces a caching mechanism to the sync service to avoid redundant data fetching from the network. The most important changes include adding a cache module, modifying the Import struct to include a cache, and updating related methods to utilize this cache.

Caching Mechanism:

  • crates/services/sync/src/import.rs: Added a new cache module and integrated it into the Import struct. Updated methods to use the cache for fetching and storing headers and blocks.
  • Cache mechanism allow use to retrieve a stream of batches of either cached headers, cached full blocks, or range to fetch data.

Test Updates:

  • Update the P2P port in mocks to use async to simulate more complex tests needed for this feature.

This PR contains 50% of changes in the tests and addition of tests in the cache.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

@AurelienFT AurelienFT marked this pull request as ready for review October 16, 2024 16:44
@AurelienFT AurelienFT requested a review from a team October 16, 2024 16:44
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

I don't understand the import task well enough to approve right now. I need clarification on the following points:

  1. How do we ensure this cache doesn't grow forever? Is the Import task short-lived? While the import task launches short-lived streams, it seems like a long-living task to me.
  2. How can we be sure we'll query exactly the same ranges as we have cached? Where is that invariant maintained.

Let me know if you want to jump on a call to chat about this, or just write if I'm missing something obvious here.

crates/services/sync/src/import.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import.rs Show resolved Hide resolved
crates/services/sync/src/import.rs Outdated Show resolved Hide resolved
@AurelienFT
Copy link
Contributor Author

AurelienFT commented Oct 16, 2024

@netrome Thanks for taking the time to review this Regarding your interrogations :
1 - Yes for me it will leave a long time but all asked data should be ok at some point and so be cleared otherwise we will only have batch_size as number of element in the cache. But I'm not very sure about this that's why I placed a comment about this in "Interrogation" in the PR. Maybe we need a pruning management.
2 - I was thinking that we re-ask all the same ranges because the batch_size doesn't change but the starting point can change to the last synced block and so ranges can change. I think you are right then the ranges can change I will ask few questions to @xgreenx

@AurelienFT AurelienFT changed the base branch from release/v0.40.0 to master October 16, 2024 21:21
Copy link
Contributor

@rafal-ch rafal-ch left a comment

Choose a reason for hiding this comment

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

So far looks good, I need to have a deeper look at the tests though.

CHANGELOG.md Outdated Show resolved Hide resolved
crates/services/sync/src/import.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import/back_pressure_tests.rs Outdated Show resolved Hide resolved
@AurelienFT AurelienFT marked this pull request as draft October 17, 2024 09:17
@AurelienFT
Copy link
Contributor Author

Convert to draft because of big refacto.

@AurelienFT AurelienFT marked this pull request as ready for review October 18, 2024 10:33
@rymnc rymnc requested a review from Copilot November 21, 2024 10:18

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 10 changed files in this pull request and generated no suggestions.

Files not reviewed (4)
  • crates/services/sync/src/import/test_helpers/pressure_peer_to_peer.rs: Evaluated as low risk
  • crates/services/sync/src/import/tests.rs: Evaluated as low risk
  • crates/services/sync/src/ports.rs: Evaluated as low risk
  • CHANGELOG.md: Evaluated as low risk
Copy link
Collaborator

@xgreenx xgreenx left a comment

Choose a reason for hiding this comment

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

The change looks really good=)

crates/services/sync/src/import.rs Show resolved Hide resolved
}
}
BlockHeaderData::Cached(CachedDataBatch::None(_)) => {
unreachable!()
Copy link
Collaborator

Choose a reason for hiding this comment

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

While it is true, let's return an error and print a log that this place shouldn't be reachable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a log and I returned a malformed batch which is used as error in this whole process. I don't want to change the whole architecture of the module for this error. (the other solution is to panic like it's done here :

.expect("We checked headers are not empty above"),
)

crates/services/sync/src/import.rs Show resolved Hide resolved
crates/services/sync/src/import.rs Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import/tests.rs Outdated Show resolved Hide resolved
crates/services/sync/src/import/tests.rs Show resolved Hide resolved
@AurelienFT
Copy link
Contributor Author

@xgreenx Thanks for the kind comment and I have addressed all of your concerns some may still need some answers :)

Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Nice stuff! Some minor questions and comments from me, but overall looks good.

crates/services/sync/src/import/cache.rs Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Show resolved Hide resolved
crates/services/sync/src/import/cache.rs Show resolved Hide resolved
crates/services/sync/src/ports.rs Show resolved Hide resolved
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Missed this previously, but saw the clippy error in CI regarding the arithmetic operation and want to ensure we avoid the array slicing as well since that can also panic.

crates/services/sync/src/import/cache.rs Outdated Show resolved Hide resolved
netrome
netrome previously approved these changes Nov 28, 2024
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Thanks for the update 🙏

crates/services/sync/src/ports.rs Show resolved Hide resolved
@xgreenx xgreenx requested a review from netrome November 29, 2024 13:46
@AurelienFT
Copy link
Contributor Author

@xgreenx Why you prefer to make the check inside the loop instead of the split at the end to simplify the logic in the loop ?

@xgreenx
Copy link
Collaborator

xgreenx commented Nov 29, 2024

Why you prefer to make the check inside the loop instead of the split at the end to simplify the logic in the loop ?

It is much easier to read. It is more performant, because the old implementation was doing into_iter, chunks, collect each batch_size blocks.

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.

fuel-core-sync should cache the result of responses instead of throwing them away
4 participants