Skip to content
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

refactor(metrics): add gc metrics #158

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading