Skip to content

Commit

Permalink
refactor(metrics): add gc metrics
Browse files Browse the repository at this point in the history
- split collator metrics to several rows
- fix several clippy lints
  • Loading branch information
0xdeafbeef committed Jul 8, 2024
1 parent c179d38 commit a127f8b
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 76 deletions.
2 changes: 1 addition & 1 deletion cli/src/tools/gen_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct MultisigCmd {
#[clap(short, long)]
lifetime: Option<u32>,

/// Use SetcodeMultisig instead of SafeMultisig
/// Use `SetcodeMultisig` instead of `SafeMultisig`
#[clap(short, long)]
updatable: bool,
}
Expand Down
11 changes: 6 additions & 5 deletions cli/src/tools/storage_cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::print_stdout)] // it's a CLI tool
use std::path::PathBuf;

use anyhow::{Context, Result};
Expand Down Expand Up @@ -126,8 +127,8 @@ impl BlockCmd {

println!("Found block full {}\n", &self.block_id);
println!("Block is link: {}\n", is_link);
println!("Block hex {}\n", hex::encode(&block));
println!("Block proof {}\n", hex::encode(&proof));
println!("Block hex {}\n", hex::encode(block));
println!("Block proof {}\n", hex::encode(proof));
}
_ => {
println!("Found block empty {}\n", &self.block_id);
Expand Down Expand Up @@ -168,11 +169,11 @@ impl BlockCmd {

println!("Found block full {}\n", &self.block_id);
println!("Block is link: {}\n", is_link);
println!("Block hex {}\n", hex::encode(&block));
println!("Block proof {}\n", hex::encode(&proof));
println!("Block hex {}\n", hex::encode(block));
println!("Block proof {}\n", hex::encode(proof));
}
_ => {
println!("Found block empty {}\n", &self.block_id)
println!("Found block empty {}\n", &self.block_id);
}
};
Ok::<(), anyhow::Error>(())
Expand Down
45 changes: 26 additions & 19 deletions core/src/block_strider/subscriber/gc_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use anyhow::Result;
use everscale_types::models::BlockId;
use rand::Rng;
use scopeguard::defer;
use tokio::select;
use tokio::sync::watch;
use tokio::task::AbortHandle;
use tycho_block_util::block::BlockStuff;
use tycho_storage::{BlocksGcType, Storage};
use tycho_util::metrics::HistogramGuard;

use crate::block_strider::{
BlockSubscriber, BlockSubscriberContext, StateSubscriber, StateSubscriberContext,
Expand Down Expand Up @@ -206,6 +208,7 @@ impl GcSubscriber {

#[tracing::instrument(skip_all)]
async fn states_gc(mut trigger_rx: TriggerRx, storage: Storage) {
use tokio::time;
let Some(config) = storage.config().states_gc else {
tracing::warn!("manager disabled");
return;
Expand All @@ -215,41 +218,45 @@ impl GcSubscriber {
tracing::info!("manager stopped");
}

let mut last_tiggered_at = None::<Instant>;
let mut interval = time::interval(config.interval);
let mut last_triggered_at = None;

while trigger_rx.changed().await.is_ok() {
match last_tiggered_at {
// Wait for an offset before the first GC but after the first trigger
None => {
let offset = if config.random_offset {
rand::thread_rng().gen_range(Duration::ZERO..config.interval)
} else {
config.interval
};
tokio::time::sleep(offset).await
}
// Wait to maintaint the interval between GCs
Some(last) => {
if last.elapsed() < config.interval {
tokio::time::sleep_until((last + config.interval).into()).await;
}
loop {
// either the interval has ticked or a new trigger has arrived
select! {
_ = interval.tick() => {},
Ok(_) = trigger_rx.changed() => {},
else => break,
}

let now = Instant::now();

if let Some(last) = last_triggered_at {
let next_gc: Instant = last + config.interval;
if next_gc > now {
time::sleep_until(next_gc.into()).await;
}
} else if config.random_offset {
let offset = rand::thread_rng().gen_range(Duration::ZERO..config.interval);
time::sleep(offset).await;
}
last_tiggered_at = Some(Instant::now());

// Get the most recent trigger
last_triggered_at = Some(Instant::now());

let Some(trigger) = trigger_rx.borrow_and_update().clone() else {
continue;
};
tracing::debug!(?trigger);

let _hist = HistogramGuard::begin("tycho_gc_states_time");
if let Err(e) = storage
.shard_state_storage()
.remove_outdated_states(trigger.mc_block_id.seqno)
.await
{
tracing::error!("failed to remove outdated states: {e:?}");
}
metrics::gauge!("tycho_gc_states_seqno").set(trigger.mc_block_id.seqno as f64);
}
}
}
Expand Down
Loading

0 comments on commit a127f8b

Please sign in to comment.