-
Notifications
You must be signed in to change notification settings - Fork 86
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
Spawn GC task from RecentBlockCache constructor #2109
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ use { | |||||
cached::{Cached, SizedCache}, | ||||||
ethcontract::BlockNumber, | ||||||
ethrpc::current_block::CurrentBlockStream, | ||||||
futures::FutureExt, | ||||||
futures::{FutureExt, StreamExt}, | ||||||
itertools::Itertools, | ||||||
prometheus::IntCounterVec, | ||||||
std::{ | ||||||
|
@@ -41,6 +41,7 @@ use { | |||||
sync::{Arc, Mutex}, | ||||||
time::Duration, | ||||||
}, | ||||||
tracing::Instrument, | ||||||
}; | ||||||
|
||||||
/// How many liqudity sources should at most be fetched in a single chunk. | ||||||
|
@@ -87,14 +88,21 @@ impl From<Block> for BlockNumber { | |||||
/// updates the N most recently used entries automatically when a new block | ||||||
/// arrives. | ||||||
pub struct RecentBlockCache<K, V, F> | ||||||
where | ||||||
K: CacheKey<V>, | ||||||
F: CacheFetching<K, V>, | ||||||
{ | ||||||
inner: Arc<Inner<K, V, F>>, | ||||||
} | ||||||
|
||||||
pub struct Inner<K, V, F> | ||||||
where | ||||||
K: CacheKey<V>, | ||||||
F: CacheFetching<K, V>, | ||||||
{ | ||||||
mutexed: Mutex<Mutexed<K, V>>, | ||||||
number_of_blocks_to_cache: NonZeroU64, | ||||||
fetcher: Arc<F>, | ||||||
block_stream: CurrentBlockStream, | ||||||
maximum_retries: u32, | ||||||
delay_between_retries: Duration, | ||||||
metrics: &'static Metrics, | ||||||
|
@@ -133,7 +141,6 @@ struct Metrics { | |||||
#[metric(labels("cache_type"))] | ||||||
recent_block_cache_misses: IntCounterVec, | ||||||
} | ||||||
|
||||||
impl<K, V, F> RecentBlockCache<K, V, F> | ||||||
where | ||||||
K: CacheKey<V>, | ||||||
|
@@ -158,28 +165,55 @@ where | |||||
metrics_label: &'static str, | ||||||
) -> Result<Self> { | ||||||
let block = block_stream.borrow().number; | ||||||
Ok(Self { | ||||||
let inner = Arc::new(Inner { | ||||||
mutexed: Mutex::new(Mutexed::new( | ||||||
config.number_of_entries_to_auto_update, | ||||||
block, | ||||||
config.maximum_recent_block_age, | ||||||
)), | ||||||
number_of_blocks_to_cache: config.number_of_blocks_to_cache, | ||||||
fetcher: Arc::new(fetcher), | ||||||
block_stream, | ||||||
maximum_retries: config.max_retries, | ||||||
delay_between_retries: config.delay_between_retries, | ||||||
metrics: Metrics::instance(observe::metrics::get_storage_registry()).unwrap(), | ||||||
metrics_label, | ||||||
requests: BoxRequestSharing::labelled("liquidity_fetching".into()), | ||||||
}) | ||||||
}); | ||||||
|
||||||
let inner_cloned = Arc::downgrade(&inner); | ||||||
tokio::task::spawn( | ||||||
async move { | ||||||
let mut stream = ethrpc::current_block::into_stream(block_stream); | ||||||
while let Some(block) = stream.next().await { | ||||||
let Some(inner) = inner_cloned.upgrade() else { | ||||||
tracing::debug!("cache no longer in use; terminate GC task"); | ||||||
break; | ||||||
}; | ||||||
if let Err(err) = inner.update_cache_at_block(block.number).await { | ||||||
tracing::warn!(?err, "filed to update cache"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
} | ||||||
.instrument(tracing::info_span!( | ||||||
"cache_maintenance", | ||||||
cache = metrics_label | ||||||
)), | ||||||
); | ||||||
|
||||||
Ok(Self { inner }) | ||||||
} | ||||||
|
||||||
pub async fn update_cache(&self) -> Result<()> { | ||||||
let new_block = self.block_stream.borrow().number; | ||||||
self.update_cache_at_block(new_block).await | ||||||
pub async fn fetch(&self, keys: impl IntoIterator<Item = K>, block: Block) -> Result<Vec<V>> { | ||||||
self.inner.fetch(keys, block).await | ||||||
} | ||||||
} | ||||||
|
||||||
impl<K, V, F> Inner<K, V, F> | ||||||
where | ||||||
K: CacheKey<V>, | ||||||
V: Clone + Send + Sync + 'static, | ||||||
F: CacheFetching<K, V>, | ||||||
{ | ||||||
async fn update_cache_at_block(&self, new_block: u64) -> Result<()> { | ||||||
let keys = self | ||||||
.mutexed | ||||||
|
@@ -239,7 +273,7 @@ where | |||||
fut.await.context("could not fetch liquidity") | ||||||
} | ||||||
|
||||||
pub async fn fetch(&self, keys: impl IntoIterator<Item = K>, block: Block) -> Result<Vec<V>> { | ||||||
async fn fetch(&self, keys: impl IntoIterator<Item = K>, block: Block) -> Result<Vec<V>> { | ||||||
let block = match block { | ||||||
Block::Recent => None, | ||||||
Block::Number(number) => Some(number), | ||||||
|
@@ -509,7 +543,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
|
||||||
let assert_keys_recently_used = |expected_keys: &[usize]| { | ||||||
let cached_keys = cache | ||||||
|
@@ -565,7 +600,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
|
||||||
// Initial state on the block chain. | ||||||
let initial_values = vec![ | ||||||
|
@@ -626,7 +662,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
|
||||||
let value0 = TestValue::new(0, "0"); | ||||||
let value1 = TestValue::new(1, "1"); | ||||||
|
@@ -683,7 +720,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
|
||||||
// cache at block 5 | ||||||
*values.lock().unwrap() = vec![TestValue::new(0, "foo")]; | ||||||
|
@@ -750,7 +788,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
|
||||||
// Fetch 10 keys on block 10; but we only have capacity to update 2 of those in | ||||||
// the background. | ||||||
|
@@ -798,7 +837,8 @@ mod tests { | |||||
block_stream, | ||||||
"", | ||||||
) | ||||||
.unwrap(); | ||||||
.unwrap() | ||||||
.inner; | ||||||
let key = TestKey(0); | ||||||
|
||||||
// cache at block 7, most recent block is 10. | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nanonit: given the size and branch complexity of this nested task could be nice to factor it into a free method and keep
new
more contained.