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

add diskcheck #3598

Merged
merged 12 commits into from
Aug 8, 2022
16 changes: 16 additions & 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 sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ starcoin-service-registry = { path = "../commons/service-registry" }
starcoin-chain-service = { path = "../chain/service" }
starcoin-chain-api = { path = "../chain/api" }
network-rpc-core = { path = "../network-rpc/core" }
sysinfo = "0.25.1"

[dev-dependencies]
tokio = {version = "^1", features = ["full"] }
Expand Down
83 changes: 78 additions & 5 deletions sync/src/block_connector/block_connector_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,30 @@ use starcoin_sync_api::PeerNewBlock;
use starcoin_types::block::ExecutedBlock;
use starcoin_types::sync_status::SyncStatus;
use starcoin_types::system_events::{MinedBlock, SyncStatusChangeEvent};
use std::sync::Arc;
use std::{sync::Arc, time::Instant};
use sysinfo::{DiskExt, System, SystemExt};
use txpool::TxPoolService;

const DISKCHECKPOINTFORPAINC: u64 = 1024 * 1024 * 1024 * 3;
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
const DISKCHECKPOINTFORWARN: u64 = 1024 * 1024 * 1024 * 5;

pub struct BlockConnectorService {
chain_service: WriteBlockChainService<TxPoolService>,
sync_status: Option<SyncStatus>,
config: Arc<NodeConfig>,
disk_space_check_time: Instant,
}

impl BlockConnectorService {
pub fn new(chain_service: WriteBlockChainService<TxPoolService>) -> Self {
pub fn new(
chain_service: WriteBlockChainService<TxPoolService>,
config: Arc<NodeConfig>,
) -> Self {
Self {
chain_service,
sync_status: None,
disk_space_check_time: Instant::now(),
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
config,
}
}

Expand All @@ -41,6 +52,52 @@ impl BlockConnectorService {
None => false,
}
}

pub fn check_disk_space(&mut self) -> Option<Result<u64>> {
let t = std::time::Duration::from_secs(3);
let check = self.disk_space_check_time;
self.disk_space_check_time = Instant::now();

if check.elapsed() >= t && System::IS_SUPPORTED {
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
let mut sys = System::new_all();
if sys.disks().len() == 1 {
let disk = &sys.disks()[0];
if DISKCHECKPOINTFORPAINC > disk.available_space() {
return Some(Err(anyhow::anyhow!(
"Disk space is less than 3GB, please add disk space."
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
)));
} else if DISKCHECKPOINTFORWARN > disk.available_space() {
return Some(Ok(disk.available_space() / 1024 / 1024));
}
} else {
sys.sort_disks_by(|a, b| {
if a.mount_point()
.starts_with(b.mount_point().to_str().unwrap())
{
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
});

let base_data_dir = self.config.base().base_data_dir.path();
for disk in sys.disks() {
if base_data_dir.starts_with(disk.mount_point()) {
if DISKCHECKPOINTFORPAINC > disk.available_space() {
return Some(Err(anyhow::anyhow!(
"Disk space is less than 3GB, please add disk space."
)));
} else if DISKCHECKPOINTFORWARN > disk.available_space() {
return Some(Ok(disk.available_space()));
}
break;
}
}
}
}

None
}
}

impl ServiceFactory<Self> for BlockConnectorService {
Expand All @@ -53,10 +110,16 @@ impl ServiceFactory<Self> for BlockConnectorService {
.get_startup_info()?
.ok_or_else(|| format_err!("Startup info should exist."))?;
let vm_metrics = ctx.get_shared_opt::<VMMetrics>()?;
let chain_service =
WriteBlockChainService::new(config, startup_info, storage, txpool, bus, vm_metrics)?;
let chain_service = WriteBlockChainService::new(
config.clone(),
startup_info,
storage,
txpool,
bus,
vm_metrics,
)?;

Ok(Self::new(chain_service))
Ok(Self::new(chain_service, config))
}
}

Expand Down Expand Up @@ -84,6 +147,16 @@ impl EventHandler<Self, BlockConnectedEvent> for BlockConnectorService {
) {
//because this block has execute at sync task, so just try connect to select head chain.
//TODO refactor connect and execute

YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
if let Some(res) = self.check_disk_space() {
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
match res {
Ok(available_space) => {
warn!("Available diskspace only {}/GB left ", available_space)
}
Err(e) => panic!("{}", e),
YouNeedWork marked this conversation as resolved.
Show resolved Hide resolved
}
}

let block = msg.block;
if let Err(e) = self.chain_service.try_connect(block) {
error!("Process connected block error: {:?}", e);
Expand Down