Skip to content

Commit

Permalink
Metrics (#1656)
Browse files Browse the repository at this point in the history
* [monitor] update metrics collection (#1651)

* block-relayer: Add metrics

* block-relayer: Query txn in txpool without load all

* Fix up

Co-authored-by: suoyuan <suoyuan@gmail.com>
Co-authored-by: Guangyu Zhu <guangyuz@gmail.com>
  • Loading branch information
3 people authored Nov 18, 2020
1 parent 05704d7 commit 762c69d
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions block-relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ starcoin-types = {path = "../types", package = "starcoin-types" }
starcoin-metrics = {path = "../commons/metrics"}
starcoin-network-rpc-api = { path = "../network-rpc/api" }
starcoin-service-registry = { path = "../commons/service-registry" }
once_cell = "1.5.2"

[dev-dependencies]
tokio = { version = "0.2", features = ["full"] }
Expand Down
40 changes: 8 additions & 32 deletions block-relayer/src/block_relayer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::metrics::BLOCK_RELAYER_METRICS;
use anyhow::Result;
use crypto::HashValue;
use futures::FutureExt;
Expand All @@ -18,7 +19,7 @@ use starcoin_types::sync_status::SyncStatus;
use starcoin_types::system_events::SyncStatusChangeEvent;
use starcoin_types::{
block::{Block, BlockBody},
cmpact_block::{CompactBlock, PrefiledTxn, ShortId},
cmpact_block::{CompactBlock, ShortId},
peer_info::PeerId,
system_events::NewHeadBlock,
transaction::{SignedUserTransaction, Transaction},
Expand Down Expand Up @@ -59,22 +60,15 @@ impl BlockRelayer {
compact_block: CompactBlock,
peer_id: PeerId,
) -> Result<Block> {
let txns_pool_vec = txpool.get_pending_txns(None, None);
let txns_pool_map: HashMap<ShortId, &SignedUserTransaction> = {
let pool_id_txn_iter = txns_pool_vec
.iter()
.map(|txn| (Transaction::UserTransaction(txn.clone()).id(), txn))
.map(|(id, txn)| (ShortId(id), txn));
HashMap::from_iter(pool_id_txn_iter)
};
let txns = {
let mut txns: Vec<Option<SignedUserTransaction>> =
vec![None; compact_block.short_ids.len()];
let mut missing_txn_short_ids = HashSet::new();
// Fill the block txns by tx pool
for (index, short_id) in compact_block.short_ids.iter().enumerate() {
if let Some(txn) = txns_pool_map.get(short_id) {
txns[index] = Some((*txn).clone());
if let Some(txn) = txpool.find_txn(&short_id.0) {
BLOCK_RELAYER_METRICS.txns_filled_from_txpool.inc();
txns[index] = Some(txn);
} else {
missing_txn_short_ids.insert(short_id);
}
Expand All @@ -86,6 +80,7 @@ impl BlockRelayer {
continue;
}
txns[prefilled_txn.index as usize] = Some(prefilled_txn.clone().tx);
BLOCK_RELAYER_METRICS.txns_filled_from_prefill.inc();
missing_txn_short_ids.remove(&ShortId(
Transaction::UserTransaction(prefilled_txn.tx).id(),
));
Expand Down Expand Up @@ -114,6 +109,7 @@ impl BlockRelayer {
for (index, short_id) in compact_block.short_ids.iter().enumerate() {
if txns[index].is_none() {
if let Some(&txn) = fetched_missing_txn_map.get(short_id) {
BLOCK_RELAYER_METRICS.txns_filled_from_network.inc();
txns[index] = Some(txn.clone());
}
}
Expand All @@ -129,27 +125,7 @@ impl BlockRelayer {
}

fn block_into_compact(&self, block: Block) -> CompactBlock {
let mut prefilled_txn = Vec::new();
let txns_pool_map: HashMap<HashValue, SignedUserTransaction> = {
let pool_id_txn = self.txpool.get_pending_txns(None, None);
HashMap::from_iter(
pool_id_txn
.iter()
.map(|txn| (Transaction::UserTransaction(txn.clone()).id(), txn.clone())),
)
};
for (index, txn) in block.transactions().iter().enumerate() {
let id = Transaction::UserTransaction(txn.clone()).id();
if !txns_pool_map.contains_key(&id) {
prefilled_txn.push(PrefiledTxn {
index: index as u64,
tx: txn.clone(),
});
}
}
// TODO: prefill txns always equal to block.transactions.
prefilled_txn.clear();
CompactBlock::new(&block, prefilled_txn)
CompactBlock::new(&block, vec![])
}

fn handle_block_event(
Expand Down
2 changes: 1 addition & 1 deletion block-relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0

mod block_relayer;

mod metrics;
pub use block_relayer::BlockRelayer;
37 changes: 37 additions & 0 deletions block-relayer/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use once_cell::sync::Lazy;
use starcoin_metrics::{register_int_gauge, IntGauge, Opts, PrometheusError};

pub static BLOCK_RELAYER_METRICS: Lazy<BlockRelayerMetrics> =
Lazy::new(|| BlockRelayerMetrics::register().unwrap());

#[derive(Clone)]
pub struct BlockRelayerMetrics {
pub txns_filled_from_network: IntGauge,
pub txns_filled_from_txpool: IntGauge,
pub txns_filled_from_prefill: IntGauge,
}

impl BlockRelayerMetrics {
pub fn register() -> Result<Self, PrometheusError> {
let txns_filled_from_network = register_int_gauge!(Opts::new(
"txns_filled_from_network",
"Count of block filled transactions from network"
)
.namespace("starcoin"))?;
let txns_filled_from_txpool = register_int_gauge!(Opts::new(
"txns_filled_from_txpool",
"Count of block filled transactions from txpool"
)
.namespace("starcoin"))?;
let txns_filled_from_prefill = register_int_gauge!(Opts::new(
"txns_filled_from_prefill",
"Count of block filled transactions from prefill"
)
.namespace("starcoin"))?;
Ok(Self {
txns_filled_from_network,
txns_filled_from_txpool,
txns_filled_from_prefill,
})
}
}
5 changes: 4 additions & 1 deletion storage/src/db_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::batch::WriteBatch;
use crate::errors::StorageInitError;
use crate::metrics::record_metrics;
use crate::metrics::{record_metrics, STORAGE_ITER_BYTES};
use crate::storage::{ColumnFamilyName, InnerStore, WriteOp};
use crate::{DEFAULT_PREFIX_NAME, VEC_PREFIX_NAME};
use anyhow::{ensure, format_err, Error, Result};
Expand Down Expand Up @@ -159,6 +159,9 @@ impl InnerStore for DBStorage {
}

fn put(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
STORAGE_ITER_BYTES
.with_label_values(&[prefix_name])
.observe((key.len() + value.len()) as f64);
record_metrics("db", prefix_name, "put").end_with(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
self.db
Expand Down
12 changes: 12 additions & 0 deletions storage/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ pub static STORAGE_COUNTERS: Lazy<IntCounterVec> = Lazy::new(|| {
.unwrap()
});

pub static STORAGE_ITER_BYTES: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
// metric name
"storage_iter_bytes",
// metric description
"storage iter size in bytess",
// metric labels (dimensions)
&["cf_name"]
)
.unwrap()
});

pub static STORAGE_TIMES: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"starcoin_storage_time",
Expand Down
2 changes: 2 additions & 0 deletions txpool/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ pub trait TxPoolSyncService: Clone + Send + Sync + Unpin {

/// Tx Pool status
fn status(&self) -> TxPoolStatus;

fn find_txn(&self, hash: &HashValue) -> Option<SignedUserTransaction>;
}
4 changes: 4 additions & 0 deletions txpool/mock-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl TxPoolSyncService for MockTxPoolService {
fn status(&self) -> TxPoolStatus {
unimplemented!()
}

fn find_txn(&self, _hash: &HashValue) -> Option<SignedUserTransaction> {
unimplemented!()
}
}

#[cfg(test)]
Expand Down
12 changes: 1 addition & 11 deletions txpool/src/counters.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use once_cell::sync::Lazy;
use prometheus::{HistogramOpts, HistogramVec, IntCounterVec, IntGauge, IntGaugeVec, Opts};

/// Counter of txn status in tx_pool
pub static TXN_STATUS_COUNTERS: Lazy<IntCounterVec> = Lazy::new(|| {
let opts = Opts::new(
"txpool_txn_stats",
"Counters of how many txn's stats in tx pool",
)
.namespace("starcoin");
register_int_counter_vec!(opts, &["status"]).unwrap()
});
use prometheus::{HistogramOpts, HistogramVec, IntGauge, IntGaugeVec, Opts};

pub static TXPOOL_TXNS_GAUGE: Lazy<IntGauge> = Lazy::new(|| {
let opts =
Expand Down
7 changes: 7 additions & 0 deletions txpool/src/tx_pool_service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ impl TxPoolSyncService for TxPoolService {
fn status(&self) -> TxPoolStatus {
self.inner.queue.status().into()
}

fn find_txn(&self, hash: &HashValue) -> Option<SignedUserTransaction> {
self.inner
.queue
.find(hash)
.map(move |txn| txn.signed().clone())
}
}

pub(crate) type TxnQueue = TransactionQueue;
Expand Down
8 changes: 8 additions & 0 deletions vm/vm-runtime/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ pub static TXN_EXECUTION_GAS_USAGE: Lazy<Histogram> = Lazy::new(|| {
)
.unwrap()
});

pub static BLOCK_UNCLES: Lazy<Histogram> = Lazy::new(|| {
register_histogram!(
"vm_block_uncles",
"Histogram for the uncles of block handle"
)
.unwrap()
});
3 changes: 2 additions & 1 deletion vm/vm-runtime/src/starcoin_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::data_cache::{RemoteStorage, StateViewCache};
use crate::errors::{
convert_normal_success_epilogue_error, convert_prologue_runtime_error, error_split,
};
use crate::metrics::TXN_EXECUTION_GAS_USAGE;
use crate::metrics::{BLOCK_UNCLES, TXN_EXECUTION_GAS_USAGE};
use anyhow::{format_err, Error, Result};
use crypto::HashValue;
use move_vm_runtime::data_cache::RemoteCache;
Expand Down Expand Up @@ -577,6 +577,7 @@ impl StarcoinVM {
&mut cost_strategy,
)
.or_else(convert_prologue_runtime_error)?;
BLOCK_UNCLES.observe(uncles as f64);
Ok(get_transaction_output(
&mut (),
session,
Expand Down

0 comments on commit 762c69d

Please sign in to comment.