From 19eac361d1512747a76497cfa57c557588b2e923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Wed, 28 Jun 2023 00:17:16 -0600 Subject: [PATCH] add fixes to mempool from catalyst-core Original found at github.com/input-output-hk/catalyst-core commit b2ac65058dcc2768d5dbbbf7c3074f44e21da524 Author: Alex Pozhylenkov Date: Mon Jan 23 15:49:34 2023 +0200 Update node metrics (#228) # Description Replace `mempool_total_size` which shows current memory usage of the mempool with the `mempool_tx_count` which shows actual transactions amount inside the mempool ## Type of change - Refactor --- jormungandr/src/fragment/pool.rs | 24 ++----------------- .../src/jormungandr/mempool.rs | 6 ++--- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/jormungandr/src/fragment/pool.rs b/jormungandr/src/fragment/pool.rs index 05ecdad84c..bf7ff22f38 100644 --- a/jormungandr/src/fragment/pool.rs +++ b/jormungandr/src/fragment/pool.rs @@ -320,8 +320,7 @@ impl Pool { n => self.pool.len() as f64 / n as f64, }; self.metrics.set_mempool_usage_ratio(mempool_usage_ratio); - self.metrics - .set_mempool_total_size(self.pool.total_size_bytes()); + self.metrics.set_mempool_total_size(self.pool.len()); } } @@ -562,7 +561,6 @@ pub(super) mod internal { entries: IndexedDeqeue, timeout_queue: BTreeSet, max_entries: usize, - total_size_bytes: usize, } impl Pool { @@ -573,7 +571,6 @@ pub(super) mod internal { // out of their order in a queue. BinaryHeap does not allow that. timeout_queue: BTreeSet::new(), max_entries, - total_size_bytes: 0, } } @@ -589,7 +586,6 @@ pub(super) mod internal { if self.entries.contains(id) { false } else { - self.total_size_bytes += fragment.serialized_size(); self.timeout_queue_insert(fragment, *id); self.entries.push_front(*id, fragment.clone()); true @@ -604,7 +600,6 @@ pub(super) mod internal { let maybe_fragment = self.entries.remove(fragment_id); if let Some(fragment) = maybe_fragment { self.timeout_queue_remove(&fragment, *fragment_id); - self.total_size_bytes -= fragment.serialized_size(); } } } @@ -612,7 +607,6 @@ pub(super) mod internal { pub fn remove_oldest(&mut self) -> Option<(Fragment, FragmentId)> { let (id, fragment) = self.entries.pop_back().map(|(id, value)| (id, value))?; self.timeout_queue_remove(&fragment, id); - self.total_size_bytes -= fragment.serialized_size(); Some((fragment, id)) } @@ -622,7 +616,6 @@ pub(super) mod internal { ) { for (fragment, id) in fragments.into_iter() { self.timeout_queue_insert(&fragment, id); - self.total_size_bytes += fragment.serialized_size(); self.entries.push_back(id, fragment); } } @@ -650,9 +643,7 @@ pub(super) mod internal { .collect(); for item in &to_remove { self.timeout_queue.remove(item); - if let Some(fragment) = self.entries.remove(&item.id) { - self.total_size_bytes -= fragment.serialized_size(); - } + self.entries.remove(&item.id); } to_remove.into_iter().map(|x| x.id).collect() // TODO convert to something like this when .first() and .pop_first() are stabilized. This does not have unnecessary clones. @@ -675,10 +666,6 @@ pub(super) mod internal { self.entries.len() } - pub fn total_size_bytes(&self) -> usize { - self.total_size_bytes - } - pub fn max_entries(&self) -> usize { self.max_entries } @@ -729,13 +716,6 @@ pub(super) mod internal { ]; let mut pool = Pool::new(4); assert_eq!(fragments1, pool.insert_all(fragments1.clone())); - assert_eq!( - pool.total_size_bytes, - fragments1 - .iter() - .map(|(f, _)| f.serialized_size()) - .sum::() - ); assert_eq!(fragments2_expected, pool.insert_all(fragments2)); for expected in final_expected.into_iter() { diff --git a/testing/jormungandr-integration-tests/src/jormungandr/mempool.rs b/testing/jormungandr-integration-tests/src/jormungandr/mempool.rs index a5d79dd4fc..eb776dc5b9 100644 --- a/testing/jormungandr-integration-tests/src/jormungandr/mempool.rs +++ b/testing/jormungandr-integration-tests/src/jormungandr/mempool.rs @@ -2,7 +2,7 @@ use assert_fs::{ fixture::{PathChild, PathCreateDir}, TempDir, }; -use chain_core::property::{FromStr, Serialize}; +use chain_core::property::FromStr; use chain_impl_mockchain::{ block::BlockDate, chaintypes::ConsensusVersion, @@ -544,7 +544,6 @@ fn pending_transaction_stats() { }, ); - let mut pending_size = 0; let mut pending_cnt = 0; for i in 0..10 { @@ -552,7 +551,6 @@ fn pending_transaction_stats() { .transaction(&alice, bob.address(), i.into()) .unwrap(); - pending_size += transaction.serialized_size(); pending_cnt += 1; let status = @@ -566,7 +564,7 @@ fn pending_transaction_stats() { pending_cnt as f64 / mempool_max_entries as f64, stats.mempool_usage_ratio ); - assert_eq!(pending_size, stats.mempool_total_size as usize); + assert_eq!(pending_cnt, stats.mempool_total_size); } }