decrease scanner frequency

Signed-off-by: junxiang Mu <1948535941@qq.com>
This commit is contained in:
junxiang Mu
2025-05-29 03:17:47 +00:00
committed by houseme
parent eec086c1ec
commit 2f487be832
4 changed files with 49 additions and 78 deletions
+6 -22
View File
@@ -171,22 +171,14 @@ pub async fn init_data_scanner() {
// Use random factor (0.0 to 1.0) multiplied by the scanner cycle duration // Use random factor (0.0 to 1.0) multiplied by the scanner cycle duration
let random_factor: f64 = { let random_factor: f64 = {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
rng.gen_range(0.0..1.0) rng.gen_range(1.0..10.0)
}; };
let base_cycle_duration = SCANNER_CYCLE.load(Ordering::SeqCst) as f64; let base_cycle_duration = SCANNER_CYCLE.load(Ordering::SeqCst) as f64;
let sleep_duration_secs = random_factor * base_cycle_duration; let sleep_duration_secs = random_factor * base_cycle_duration;
// Ensure minimum sleep duration of 1 second to avoid high CPU usage let sleep_duration = Duration::from_secs_f64(sleep_duration_secs);
let sleep_duration = if sleep_duration_secs < 1.0 {
Duration::from_secs(1)
} else {
Duration::from_secs_f64(sleep_duration_secs)
};
info!( info!(duration_secs = sleep_duration.as_secs(), "Data scanner sleeping before next cycle");
duration_secs = sleep_duration.as_secs(),
"Data scanner sleeping before next cycle"
);
// Sleep with the calculated duration // Sleep with the calculated duration
sleep(sleep_duration).await; sleep(sleep_duration).await;
@@ -245,12 +237,8 @@ async fn run_data_scanner() {
// Read background healing information and determine scan mode // Read background healing information and determine scan mode
let bg_heal_info = read_background_heal_info(store.clone()).await; let bg_heal_info = read_background_heal_info(store.clone()).await;
let scan_mode = get_cycle_scan_mode( let scan_mode =
cycle_info.current, get_cycle_scan_mode(cycle_info.current, bg_heal_info.bitrot_start_cycle, bg_heal_info.bitrot_start_time).await;
bg_heal_info.bitrot_start_cycle,
bg_heal_info.bitrot_start_time,
)
.await;
// Update healing info if scan mode changed // Update healing info if scan mode changed
if bg_heal_info.current_scan_mode != scan_mode { if bg_heal_info.current_scan_mode != scan_mode {
@@ -280,11 +268,7 @@ async fn run_data_scanner() {
); );
// Run the namespace scanner // Run the namespace scanner
match store match store.clone().ns_scanner(tx, cycle_info.current as usize, scan_mode).await {
.clone()
.ns_scanner(tx, cycle_info.current as usize, scan_mode)
.await
{
Ok(_) => { Ok(_) => {
info!(cycle = cycle_info.current, "Namespace scanner completed successfully"); info!(cycle = cycle_info.current, "Namespace scanner completed successfully");
+5 -18
View File
@@ -4,12 +4,12 @@ use lazy_static::lazy_static;
use madmin::metrics::ScannerMetrics as M_ScannerMetrics; use madmin::metrics::ScannerMetrics as M_ScannerMetrics;
use std::{ use std::{
collections::HashMap, collections::HashMap,
pin::Pin,
sync::{ sync::{
atomic::{AtomicU64, Ordering}, atomic::{AtomicU64, Ordering},
Arc, Arc,
}, },
time::{Duration, SystemTime}, time::{Duration, SystemTime},
pin::Pin,
}; };
use tokio::sync::{Mutex, RwLock}; use tokio::sync::{Mutex, RwLock};
use tracing::debug; use tracing::debug;
@@ -206,9 +206,7 @@ pub struct ScannerMetrics {
impl ScannerMetrics { impl ScannerMetrics {
pub fn new() -> Self { pub fn new() -> Self {
let operations = (0..ScannerMetric::Last as usize) let operations = (0..ScannerMetric::Last as usize).map(|_| AtomicU64::new(0)).collect();
.map(|_| AtomicU64::new(0))
.collect();
let latency = (0..ScannerMetric::LastRealtime as usize) let latency = (0..ScannerMetric::LastRealtime as usize)
.map(|_| LockedLastMinuteLatency::new()) .map(|_| LockedLastMinuteLatency::new())
@@ -241,11 +239,7 @@ impl ScannerMetrics {
// Log trace metrics // Log trace metrics
if metric as u8 > ScannerMetric::StartTrace as u8 { if metric as u8 > ScannerMetric::StartTrace as u8 {
debug!( debug!(metric = metric.as_str(), duration_ms = duration.as_millis(), "Scanner trace metric");
metric = metric.as_str(),
duration_ms = duration.as_millis(),
"Scanner trace metric"
);
} }
} }
} }
@@ -408,10 +402,7 @@ pub type UpdateCurrentPathFn = Arc<dyn Fn(&str) -> Pin<Box<dyn std::future::Futu
pub type CloseDiskFn = Arc<dyn Fn() -> Pin<Box<dyn std::future::Future<Output = ()> + Send>> + Send + Sync>; pub type CloseDiskFn = Arc<dyn Fn() -> Pin<Box<dyn std::future::Future<Output = ()> + Send>> + Send + Sync>;
/// Create a current path updater for tracking scan progress /// Create a current path updater for tracking scan progress
pub fn current_path_updater( pub fn current_path_updater(disk: &str, initial: &str) -> (UpdateCurrentPathFn, CloseDiskFn) {
disk: &str,
initial: &str
) -> (UpdateCurrentPathFn, CloseDiskFn) {
let tracker = Arc::new(CurrentPathTracker::new(initial.to_string())); let tracker = Arc::new(CurrentPathTracker::new(initial.to_string()));
let disk_name = disk.to_string(); let disk_name = disk.to_string();
@@ -442,11 +433,7 @@ pub fn current_path_updater(
Arc::new(move || -> Pin<Box<dyn std::future::Future<Output = ()> + Send>> { Arc::new(move || -> Pin<Box<dyn std::future::Future<Output = ()> + Send>> {
let disk_name = disk_name.clone(); let disk_name = disk_name.clone();
Box::pin(async move { Box::pin(async move {
globalScannerMetrics globalScannerMetrics.current_paths.write().await.remove(&disk_name);
.current_paths
.write()
.await
.remove(&disk_name);
}) })
}) })
}; };
+2 -3
View File
@@ -3002,7 +3002,7 @@ impl SetDisks {
let self_clone = Arc::clone(&self); let self_clone = Arc::clone(&self);
let bucket_rx_clone = bucket_rx.clone(); let bucket_rx_clone = bucket_rx.clone();
let buckets_results_tx_clone = buckets_results_tx.clone(); let buckets_results_tx_clone = buckets_results_tx.clone();
futures.push(tokio::spawn(async move { futures.push(async move {
loop { loop {
match bucket_rx_clone.write().await.try_recv() { match bucket_rx_clone.write().await.try_recv() {
Err(_) => return, Err(_) => return,
@@ -3083,12 +3083,11 @@ impl SetDisks {
} }
info!("continue scanner"); info!("continue scanner");
} }
})); });
} }
info!("ns_scanner start"); info!("ns_scanner start");
let _ = join_all(futures).await; let _ = join_all(futures).await;
drop(buckets_results_tx);
let _ = task.await; let _ = task.await;
info!("ns_scanner completed"); info!("ns_scanner completed");
Ok(()) Ok(())
+2 -1
View File
@@ -827,7 +827,8 @@ impl ECStore {
} }
} }
}); });
if let Err(err) = set.clone() if let Err(err) = set
.clone()
.ns_scanner(&all_buckets_clone, want_cycle as u32, tx, heal_scan_mode) .ns_scanner(&all_buckets_clone, want_cycle as u32, tx, heal_scan_mode)
.await .await
{ {