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

added elapsed sync times #125

Merged
merged 6 commits into from
May 22, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- feat(metrics): Added sync time metrics
- refactor: using const and OnceCell instead of lazy_static
- refactor: remove crate mp-storage
- feat(infra): corrected dockerfile + docker-compose
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN apt-get -y update && \
build-essential

# Set the working directory
WORKDIR /usr/local/bin
WORKDIR /usr/src/

# Copy the source code into the container
COPY . .
Expand All @@ -30,7 +30,7 @@ RUN apt-get -y update && \
WORKDIR /usr/local/bin

# Copy the compiled binary from the builder stage
COPY --from=builder /usr/local/bin/target/release/deoxys .
COPY --from=builder /usr/src/target/release/deoxys .

# Set the entrypoint
ENTRYPOINT ["./deoxys"]
ENTRYPOINT ["./deoxys"]
2 changes: 1 addition & 1 deletion crates/client/sync/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn l2_fetch_task(
}
};

log::info!("🥳🥳🥳 The sync process caught up with the tip of the chain.");
log::info!("🥳 The sync process caught up with the tip of the chain.");

if let Some(sync_polling_interval) = sync_polling_interval {
// Polling
Expand Down
49 changes: 46 additions & 3 deletions crates/client/sync/src/l2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Contains the code required to sync data from the feeder efficiently.
use std::pin::pin;
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use std::sync::{Arc, Mutex, RwLock};
use std::time::Instant;

use anyhow::{bail, Context};
use futures::{stream, StreamExt, TryStreamExt};
Expand Down Expand Up @@ -29,6 +30,7 @@ use crate::convert::convert_block;
use crate::fetch::fetchers::L2BlockAndUpdates;
use crate::fetch::l2_fetch_task;
use crate::l1::ETHEREUM_STATE_UPDATE;
use crate::metrics::block_metrics::BlockMetrics;
use crate::utils::PerfStopwatch;
use crate::{stopwatch_end, CommandSink};

Expand Down Expand Up @@ -126,6 +128,8 @@ async fn l2_verify_and_apply_task(
mut command_sink: CommandSink,
verify: bool,
backup_every_n_blocks: Option<usize>,
block_metrics: Option<BlockMetrics>,
sync_timer: Arc<Mutex<Option<Instant>>>,
) -> anyhow::Result<()> {
let block_sender = Arc::new(block_sender);

Expand Down Expand Up @@ -197,7 +201,15 @@ async fn l2_verify_and_apply_task(
},
async {
let sw = PerfStopwatch::new();
create_block(&mut command_sink, &mut last_block_hash).await.expect("creating block");
create_block(
&mut command_sink,
&mut last_block_hash,
block_n,
block_metrics.clone(),
sync_timer.clone(),
)
.await
.expect("creating block");
stopwatch_end!(sw, "end create_block {}: {:?}", block_n);
}
);
Expand Down Expand Up @@ -272,13 +284,15 @@ pub async fn sync<C>(
provider: SequencerGatewayProvider,
client: Arc<C>,
config: L2SyncConfig,
block_metrics: Option<BlockMetrics>,
) -> anyhow::Result<()>
where
C: HeaderBackend<DBlockT> + 'static,
{
let (fetch_stream_sender, fetch_stream_receiver) = mpsc::channel(10);
let (block_conv_sender, block_conv_receiver) = mpsc::channel(10);
let provider = Arc::new(provider);
let sync_timer = Arc::new(Mutex::new(None));

// [Fetch task] ==new blocks and updates=> [Block conversion task] ======> [Verification and apply
// task]
Expand All @@ -304,6 +318,8 @@ where
command_sink,
config.verify,
config.backup_every_n_blocks,
block_metrics.clone(),
Arc::clone(&sync_timer),
));

tokio::select!(
Expand Down Expand Up @@ -334,7 +350,13 @@ where
}

/// Notifies the consensus engine that a new block should be created.
async fn create_block(cmds: &mut CommandSink, parent_hash: &mut Option<H256>) -> Result<(), String> {
async fn create_block(
cmds: &mut CommandSink,
parent_hash: &mut Option<H256>,
block_number: u64,
block_metrics: Option<BlockMetrics>,
sync_timer: Arc<Mutex<Option<Instant>>>,
) -> Result<(), String> {
let (sender, receiver) = futures::channel::oneshot::channel();

cmds.try_send(sc_consensus_manual_seal::rpc::EngineCommand::SealNewBlock {
Expand All @@ -350,6 +372,27 @@ async fn create_block(cmds: &mut CommandSink, parent_hash: &mut Option<H256>) ->
.map_err(|err| format!("failed to seal block: {err}"))?
.map_err(|err| format!("failed to seal block: {err}"))?;

// Update Block sync time metrics
if let Some(block_metrics) = block_metrics {
let elapsed_time;
{
let mut timer_guard = sync_timer.lock().unwrap();
if let Some(start_time) = *timer_guard {
elapsed_time = start_time.elapsed().as_secs_f64();
*timer_guard = Some(Instant::now());
} else {
// For the first block, there is no previous timer set
elapsed_time = 0.0;
*timer_guard = Some(Instant::now());
}
}

let sync_time = block_metrics.l2_sync_time.get() + elapsed_time;
block_metrics.l2_sync_time.set(sync_time);
block_metrics.l2_latest_sync_time.set(elapsed_time);
block_metrics.l2_avg_sync_time.set(block_metrics.l2_sync_time.get() / block_number as f64);
}

*parent_hash = Some(create_block_info.hash);
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion crates/client/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod starknet_sync_worker {
}

tokio::select!(
res = l1::sync(l1_url.clone(), block_metrics) => res.context("syncing L1 state")?,
res = l1::sync(l1_url.clone(), block_metrics.clone()) => res.context("syncing L1 state")?,
res = l2::sync(
block_sender,
command_sink,
Expand All @@ -89,6 +89,7 @@ pub mod starknet_sync_worker {
sync_polling_interval: fetch_config.sync_polling_interval,
backup_every_n_blocks,
},
block_metrics,
) => res.context("syncing L2 state")?
);

Expand Down
16 changes: 15 additions & 1 deletion crates/client/sync/src/metrics/block_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ use prometheus_endpoint::{register, PrometheusError, Registry};

#[derive(Clone, Debug)]
pub struct BlockMetrics {
// L2 network metrics
pub l2_block_number: Gauge,
pub l1_block_number: Gauge,
pub l2_sync_time: Gauge,
pub l2_avg_sync_time: Gauge,
pub l2_latest_sync_time: Gauge,
pub transaction_count: Gauge,
pub event_count: Gauge,
// L1 network metrics
pub l1_block_number: Gauge,
pub l1_gas_price_wei: Gauge,
pub l1_gas_price_strk: Gauge,
}
Expand All @@ -18,6 +23,15 @@ impl BlockMetrics {
Gauge::new("deoxys_l2_block_number", "Gauge for deoxys L2 block number")?,
registry,
)?,
l2_sync_time: register(Gauge::new("deoxys_l2_sync_time", "Gauge for deoxys L2 sync time")?, registry)?,
l2_avg_sync_time: register(
Gauge::new("deoxys_l2_avg_sync_time", "Gauge for deoxys L2 average sync time")?,
registry,
)?,
l2_latest_sync_time: register(
Gauge::new("deoxys_l2_latest_sync_time", "Gauge for deoxys L2 latest sync time")?,
registry,
)?,
l1_block_number: register(
Gauge::new("deoxys_l1_block_number", "Gauge for deoxys L1 block number")?,
registry,
Expand Down