This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Cache block time in Blockstore #11955
Merged
CriesofCarrots
merged 6 commits into
solana-labs:master
from
CriesofCarrots:cache-block-time
Sep 9, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e663f05
Add blockstore column to cache block times
317eaf4
Add method to cache block time
a3a6131
Add service to cache block time
733e053
Update rpc getBlockTime to use new method, and refactor blockstore sl…
8572ed2
Return block_time with confirmed block, if available
d0a0f50
Add measure and warning to cache-block-time
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use crossbeam_channel::{Receiver, RecvTimeoutError, Sender}; | ||
| use solana_ledger::blockstore::Blockstore; | ||
| use solana_measure::measure::Measure; | ||
| use solana_runtime::bank::Bank; | ||
| use solana_sdk::timing::slot_duration_from_slots_per_year; | ||
| use std::{ | ||
| collections::HashMap, | ||
| sync::{ | ||
| atomic::{AtomicBool, Ordering}, | ||
| Arc, | ||
| }, | ||
| thread::{self, Builder, JoinHandle}, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| pub type CacheBlockTimeReceiver = Receiver<Arc<Bank>>; | ||
| pub type CacheBlockTimeSender = Sender<Arc<Bank>>; | ||
|
|
||
| pub struct CacheBlockTimeService { | ||
| thread_hdl: JoinHandle<()>, | ||
| } | ||
|
|
||
| const CACHE_BLOCK_TIME_WARNING_MS: u64 = 150; | ||
|
|
||
| impl CacheBlockTimeService { | ||
| #[allow(clippy::new_ret_no_self)] | ||
| pub fn new( | ||
| cache_block_time_receiver: CacheBlockTimeReceiver, | ||
| blockstore: Arc<Blockstore>, | ||
| exit: &Arc<AtomicBool>, | ||
| ) -> Self { | ||
| let exit = exit.clone(); | ||
| let thread_hdl = Builder::new() | ||
| .name("solana-cache-block-time".to_string()) | ||
| .spawn(move || loop { | ||
| if exit.load(Ordering::Relaxed) { | ||
| break; | ||
| } | ||
| let recv_result = cache_block_time_receiver.recv_timeout(Duration::from_secs(1)); | ||
| match recv_result { | ||
| Err(RecvTimeoutError::Disconnected) => { | ||
| break; | ||
| } | ||
| Ok(bank) => { | ||
| let mut cache_block_time_timer = Measure::start("cache_block_time_timer"); | ||
| Self::cache_block_time(bank, &blockstore); | ||
| cache_block_time_timer.stop(); | ||
| if cache_block_time_timer.as_ms() > CACHE_BLOCK_TIME_WARNING_MS { | ||
| warn!( | ||
| "cache_block_time operation took: {}ms", | ||
| cache_block_time_timer.as_ms() | ||
| ); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| }) | ||
| .unwrap(); | ||
| Self { thread_hdl } | ||
| } | ||
|
|
||
| fn cache_block_time(bank: Arc<Bank>, blockstore: &Arc<Blockstore>) { | ||
| let slot_duration = slot_duration_from_slots_per_year(bank.slots_per_year()); | ||
| let epoch = bank.epoch_schedule().get_epoch(bank.slot()); | ||
| let stakes = HashMap::new(); | ||
| let stakes = bank.epoch_vote_accounts(epoch).unwrap_or(&stakes); | ||
|
|
||
| if let Err(e) = blockstore.cache_block_time(bank.slot(), slot_duration, stakes) { | ||
| error!("cache_block_time failed: slot {:?} {:?}", bank.slot(), e); | ||
| } | ||
| } | ||
|
|
||
| pub fn join(self) -> thread::Result<()> { | ||
| self.thread_hdl.join() | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
How expensive is this? Maybe wrap a measure around it?
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.
The pieces of this are measured in
get_timestamp_slots()andcache_block_time(). You looking for a sum?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.
I was just more wondering if we run into issues of being able to keep up with the new slots as they come in. But if this operation takes 1-10ms or so then no worries
Uh oh!
There was an error while loading. Please reload this page.
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.
Empirical data suggests this operation will be in the 2-10ms range for current mainnet-beta throughput. However, it does depend on deserializing blocks to find vote transactions, and that deserialization definitely takes longer as TPS increases. (With about 10k TPS, I was seeing this take about 10x as long on my under-powered GCE instance.) One solution would be to index vote transactions/timestamps in blockstore to avoid the deserialization altogether; possibly as part of the transaction-status-service. I think that could be a follow-up optimization. Wdyt? @mvines
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.
Ok cool, that seems fine for now. But how about this:
recv_timeout()out ofcache_block_time()cache_block_time(). Then ifcache_block_time()takes longer than IDK, 100ms or so, emit awarn!orerror!log.Since this is an unbounded channel, if
cache_block_time()ever does get backed up and roots start coming in faster than it can process then we have a memory leak and will probably eventually OOM. It'd be nice to get yelled at from the log if this ever starts happeningThere 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.
Sounds like a plan
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.
How does this look? d0a0f50