-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add missing rpc methods and CORS headers #663
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1298731
feat: add missing rpc methods and CORS headers
bmuddha 1b2df27
Merge branch 'master' into bmuddha/feat/add-missing-rpc-methods
bmuddha f6389ab
fix: correct perf samples deltas
bmuddha 2d803a1
Merge branch 'master' into bmuddha/feat/add-missing-rpc-methods
bmuddha 56fe05b
fix: correct performance samples collection
bmuddha 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
113 changes: 113 additions & 0 deletions
113
magicblock-aperture/src/requests/http/get_recent_performance_samples.rs
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,113 @@ | ||
| use std::{ | ||
| cmp::Reverse, | ||
| sync::{Arc, OnceLock}, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use magicblock_metrics::metrics::TRANSACTION_COUNT; | ||
| use scc::{ebr::Guard, TreeIndex}; | ||
| use solana_rpc_client_api::response::RpcPerfSample; | ||
| use tokio::time; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| use super::prelude::*; | ||
|
|
||
| /// 60 seconds per sample | ||
| const PERIOD_SECS: u64 = 60; | ||
|
|
||
| /// Keep 12 hours of history (720 minutes) | ||
| const MAX_PERF_SAMPLES: usize = 720; | ||
|
|
||
| /// Estimated blocks per minute: | ||
| /// Nominal = 1200 (20 blocks/sec * 60s). | ||
| /// We use 1500 (25% buffer) to ensure the cleanup | ||
| /// logic never accidentally prunes valid history | ||
| const ESTIMATED_SLOTS_PER_SAMPLE: u64 = 1500; | ||
|
|
||
| static PERF_SAMPLES: OnceLock<TreeIndex<Reverse<Slot>, Sample>> = | ||
| OnceLock::new(); | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct Sample { | ||
| transactions: u64, | ||
| slots: u64, | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| impl HttpDispatcher { | ||
| pub(crate) fn get_recent_performance_samples( | ||
| &self, | ||
| request: &mut JsonRequest, | ||
| ) -> HandlerResult { | ||
| let count = parse_params!(request.params()?, usize); | ||
| let mut count: usize = some_or_err!(count); | ||
|
|
||
| // Cap request at max history size (12h) | ||
| count = count.min(MAX_PERF_SAMPLES); | ||
|
|
||
| let index = PERF_SAMPLES.get_or_init(TreeIndex::default); | ||
| let mut samples = Vec::with_capacity(count); | ||
|
|
||
| // Index is keyed by Reverse(Slot), so iter() yields Newest -> Oldest | ||
| for (slot, &sample) in index.iter(&Guard::new()).take(count) { | ||
| samples.push(RpcPerfSample { | ||
| slot: slot.0, | ||
| num_slots: sample.slots, | ||
| num_transactions: sample.transactions, | ||
| num_non_vote_transactions: None, | ||
| sample_period_secs: PERIOD_SECS as u16, | ||
| }); | ||
| } | ||
|
|
||
| Ok(ResponsePayload::encode_no_context(&request.id, samples)) | ||
| } | ||
|
|
||
| pub(crate) async fn run_perf_samples_collector( | ||
| self: Arc<Self>, | ||
| cancel: CancellationToken, | ||
| ) { | ||
| let mut interval = time::interval(Duration::from_secs(PERIOD_SECS)); | ||
|
|
||
| let mut last_slot = self.blocks.block_height(); | ||
| let mut last_tx_count = TRANSACTION_COUNT.get(); | ||
|
|
||
| loop { | ||
| tokio::select! { | ||
| _ = interval.tick() => { | ||
| // Capture current state | ||
| let current_slot = self.blocks.block_height(); | ||
| let current_tx_count = TRANSACTION_COUNT.get(); | ||
|
|
||
| // Calculate Deltas (Activity within the last 60s) | ||
| let slots_delta = current_slot.saturating_sub(last_slot).max(1); | ||
| let tx_delta = current_tx_count.saturating_sub(last_tx_count); | ||
|
|
||
| let index = PERF_SAMPLES.get_or_init(TreeIndex::default); | ||
| let sample = Sample { | ||
| slots: slots_delta, | ||
| transactions: tx_delta, | ||
| }; | ||
| let _ = index.insert_async(Reverse(current_slot), sample).await; | ||
|
|
||
| // Prune old history | ||
| if index.len() > MAX_PERF_SAMPLES { | ||
| // Calculate cutoff: 720 samples * 1500 blocks = ~1.08M blocks history | ||
| let retention_range = MAX_PERF_SAMPLES as u64 * ESTIMATED_SLOTS_PER_SAMPLE; | ||
| let cutoff_slot = current_slot.saturating_sub(retention_range); | ||
|
|
||
| // Remove everything OLDER than the cutoff. | ||
| // In Reverse(), "Older" (Smaller Slot) == "Greater Value". | ||
| // RangeFrom (cutoff..) removes the tail of the tree. | ||
| index.remove_range_async(Reverse(cutoff_slot)..).await; | ||
| } | ||
|
|
||
| // Update baseline for next tick | ||
| last_slot = current_slot; | ||
| last_tx_count = current_tx_count; | ||
| } | ||
| _ = cancel.cancelled() => { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
bmuddha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.