Skip to content

Commit

Permalink
fix(p2p): cache responses to serve without roundtrip to db (#2352)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
<!-- List of related issues/PRs -->
- the intermittent outages on testnet

## Description
<!-- List of detailed changes -->
~When we request transactions for a given block range, we shouldn't only
keep using the same peer causing pressure on it. we should pick a random
one with the same height and try to get the transactions from that
instead.~

This PR caches p2p responses (ttl 10 seconds by default) and serves
requests from cache falling back to db for others.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [ ] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?

---------

Co-authored-by: Mårten Blankfors <[email protected]>
Co-authored-by: Rafał Chabowski <[email protected]>
Co-authored-by: green <[email protected]>
  • Loading branch information
4 people authored Nov 19, 2024
1 parent 2ef18cf commit be4e33c
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2310](https://github.com/FuelLabs/fuel-core/pull/2310): The `metrics` command-line parameter has been replaced with `disable-metrics`. Metrics are now enabled by default, with the option to disable them entirely or on a per-module basis.
- [2341](https://github.com/FuelLabs/fuel-core/pull/2341): The maximum number of processed coins from the `coins_to_spend` query is limited to `max_inputs`.

### Fixed

- [2352](https://github.com/FuelLabs/fuel-core/pull/2352): Cache p2p responses to serve without roundtrip to db.

## [Version 0.39.0]

### Added
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions crates/metrics/src/p2p_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ use std::sync::OnceLock;
pub struct P2PMetrics {
pub unique_peers: Counter,
pub blocks_requested: Gauge,
pub p2p_req_res_cache_hits: Counter,
pub p2p_req_res_cache_misses: Counter,
}

impl P2PMetrics {
fn new() -> Self {
let unique_peers = Counter::default();
let blocks_requested = Gauge::default();
let p2p_req_res_cache_hits = Counter::default();
let p2p_req_res_cache_misses = Counter::default();

let metrics = P2PMetrics {
unique_peers,
blocks_requested,
p2p_req_res_cache_hits,
p2p_req_res_cache_misses,
};

let mut registry = global_registry().registry.lock();
Expand All @@ -33,6 +39,18 @@ impl P2PMetrics {
metrics.blocks_requested.clone()
);

registry.register(
"P2p_Req_Res_Cache_Hits",
"A Counter which keeps track of the number of cache hits for the p2p req/res protocol",
metrics.p2p_req_res_cache_hits.clone()
);

registry.register(
"P2p_Req_Res_Cache_Misses",
"A Counter which keeps track of the number of cache misses for the p2p req/res protocol",
metrics.p2p_req_res_cache_misses.clone()
);

metrics
}
}
Expand All @@ -50,3 +68,11 @@ pub fn increment_unique_peers() {
pub fn set_blocks_requested(count: usize) {
p2p_metrics().blocks_requested.set(count as i64);
}

pub fn increment_p2p_req_res_cache_hits() {
p2p_metrics().p2p_req_res_cache_hits.inc();
}

pub fn increment_p2p_req_res_cache_misses() {
p2p_metrics().p2p_req_res_cache_misses.inc();
}
1 change: 1 addition & 0 deletions crates/services/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ postcard = { workspace = true, features = ["use-std"] }
prometheus-client = { workspace = true }
quick-protobuf = "0.8.1"
quick-protobuf-codec = "0.3.0"
quick_cache = "0.6.9"
rand = { workspace = true }
rayon = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
Loading

0 comments on commit be4e33c

Please sign in to comment.