From a0a044c48aa6ccbe13cb69646932a4575c3221c2 Mon Sep 17 00:00:00 2001 From: June Han Date: Tue, 6 Feb 2024 08:51:18 +0900 Subject: [PATCH 1/4] fix: compute accurate jobs_query_capacity --- protocols/kad/src/behaviour.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index f7e2fb3a032..146d0aa5fb2 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -31,7 +31,7 @@ use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; use crate::record::{ self, store::{self, RecordStore}, - ProviderRecord, Record, + ProviderRecord, Record, Key }; use crate::K_VALUE; use fnv::{FnvHashMap, FnvHashSet}; @@ -2433,14 +2433,17 @@ where // Run the periodic provider announcement job. if let Some(mut job) = self.add_provider_job.take() { let num = usize::min(JOBS_MAX_NEW_QUERIES, jobs_query_capacity); + let mut ready_provider_keys: Vec = Vec::new(); for _ in 0..num { if let Poll::Ready(r) = job.poll(cx, &mut self.store, now) { - self.start_add_provider(r.key, AddProviderContext::Republish) - } else { - break; + ready_provider_keys.push(r.key) } } - jobs_query_capacity -= num; + if !ready_provider_keys.is_empty() { + // Republish the first provider we polled + self.start_add_provider(ready_provider_keys[0].clone(), AddProviderContext::Republish) + } + jobs_query_capacity = jobs_query_capacity - (num - ready_provider_keys.len()); self.add_provider_job = Some(job); } From b30757cbd2713272064be40bba0417aac7118746 Mon Sep 17 00:00:00 2001 From: June Han Date: Wed, 14 Feb 2024 17:11:35 +0900 Subject: [PATCH 2/4] fix: subtract the right amount from the job capacity --- protocols/kad/src/behaviour.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 146d0aa5fb2..98f8af9a779 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -31,7 +31,7 @@ use crate::query::{Query, QueryConfig, QueryId, QueryPool, QueryPoolState}; use crate::record::{ self, store::{self, RecordStore}, - ProviderRecord, Record, Key + ProviderRecord, Record, }; use crate::K_VALUE; use fnv::{FnvHashMap, FnvHashSet}; @@ -2433,17 +2433,14 @@ where // Run the periodic provider announcement job. if let Some(mut job) = self.add_provider_job.take() { let num = usize::min(JOBS_MAX_NEW_QUERIES, jobs_query_capacity); - let mut ready_provider_keys: Vec = Vec::new(); - for _ in 0..num { + for i in 0..num { if let Poll::Ready(r) = job.poll(cx, &mut self.store, now) { - ready_provider_keys.push(r.key) + self.start_add_provider(r.key, AddProviderContext::Republish) + } else { + jobs_query_capacity -= i; + break; } } - if !ready_provider_keys.is_empty() { - // Republish the first provider we polled - self.start_add_provider(ready_provider_keys[0].clone(), AddProviderContext::Republish) - } - jobs_query_capacity = jobs_query_capacity - (num - ready_provider_keys.len()); self.add_provider_job = Some(job); } From 0d121b65e2f2873d195180f412f8f54ff25b44d7 Mon Sep 17 00:00:00 2001 From: June Han Date: Wed, 14 Feb 2024 17:40:51 +0900 Subject: [PATCH 3/4] chore: update versions --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/kad/CHANGELOG.md | 5 +++++ protocols/kad/Cargo.toml | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81a2c993a03..58021eb3ed8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2851,7 +2851,7 @@ dependencies = [ [[package]] name = "libp2p-kad" -version = "0.45.3" +version = "0.45.4" dependencies = [ "arrayvec", "async-std", diff --git a/Cargo.toml b/Cargo.toml index 578458a233d..ece7fa2034b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,7 @@ libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.46.1", path = "protocols/gossipsub" } libp2p-identify = { version = "0.44.2", path = "protocols/identify" } libp2p-identity = { version = "0.2.8" } -libp2p-kad = { version = "0.45.3", path = "protocols/kad" } +libp2p-kad = { version = "0.45.4", path = "protocols/kad" } libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" } libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.14.1", path = "misc/metrics" } diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index b27a6943659..8534250e4fd 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.45.4 + +- Compute `jobs_query_capacity` accurately. + See [PR 5148](https://github.com/libp2p/rust-libp2p/pull/5148). + ## 0.45.3 - The progress of the close query iterator shall be decided by ANY of the new peers. diff --git a/protocols/kad/Cargo.toml b/protocols/kad/Cargo.toml index 96f5f10819f..bde0d5f7c84 100644 --- a/protocols/kad/Cargo.toml +++ b/protocols/kad/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-kad" edition = "2021" rust-version = { workspace = true } description = "Kademlia protocol for libp2p" -version = "0.45.3" +version = "0.45.4" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 52d88d3fd007bfb275f887022d87c3a63403e2fa Mon Sep 17 00:00:00 2001 From: June Han Date: Mon, 19 Feb 2024 16:49:50 +0900 Subject: [PATCH 4/4] chore: merge changelogs --- protocols/kad/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index f7baee2d288..25bd0c99cd4 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -3,6 +3,8 @@ - Make it mandatory to provide protocol names when creating a `kad::Config`. Deprecate `kad::Config::default()`, replaced by `kad::Config::new(StreamProtocol)`. See [PR 5122](https://github.com/libp2p/rust-libp2p/pull/5122). +- Compute `jobs_query_capacity` accurately. + See [PR 5148](https://github.com/libp2p/rust-libp2p/pull/5148). ## 0.45.3