use std::collections::VecDeque;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use std::time::Instant;
use tokio::sync::{Notify, oneshot};
use tracing::debug;
use crate::error::Result;
use super::cancelled_error;
use super::tasks::{CheckIntegrityTask, IntegrityOutcome};
#[derive(Debug, Clone)]
struct PickedMeta {
gid: u64,
total_length: u64,
cancelled: Arc<AtomicBool>,
progress: Arc<std::sync::atomic::AtomicU64>,
}
pub struct CheckIntegrityEntry {
pub gid: u64,
pub task: Box<dyn CheckIntegrityTask>,
pub created_at: Instant,
cancelled: Arc<AtomicBool>,
progress: Arc<std::sync::atomic::AtomicU64>,
done_tx: Option<oneshot::Sender<Result<IntegrityOutcome>>>,
}
impl CheckIntegrityEntry {
pub(super) fn new(
gid: u64,
task: Box<dyn CheckIntegrityTask>,
done_tx: oneshot::Sender<Result<IntegrityOutcome>>,
) -> Self {
Self {
gid,
task,
created_at: Instant::now(),
cancelled: Arc::new(AtomicBool::new(false)),
progress: Arc::new(std::sync::atomic::AtomicU64::new(0)),
done_tx: Some(done_tx),
}
}
fn mark_cancelled(&self) {
self.cancelled.store(true, Ordering::Relaxed);
}
pub(super) fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Relaxed)
}
pub(super) fn progress(&self) -> &std::sync::atomic::AtomicU64 {
&self.progress
}
pub(super) fn take_done_sender(&mut self) -> Option<oneshot::Sender<Result<IntegrityOutcome>>> {
self.done_tx.take()
}
}
pub struct CheckIntegrityMan {
queue: VecDeque<CheckIntegrityEntry>,
picked: Option<PickedMeta>,
max_concurrent: usize,
active_count: usize,
wake: Arc<Notify>,
}
impl CheckIntegrityMan {
pub fn new() -> Self {
Self {
queue: VecDeque::new(),
picked: None,
max_concurrent: 1,
active_count: 0,
wake: Arc::new(Notify::new()),
}
}
pub fn with_concurrency(max_concurrent: usize) -> Self {
Self {
queue: VecDeque::new(),
picked: None,
max_concurrent: max_concurrent.max(1),
active_count: 0,
wake: Arc::new(Notify::new()),
}
}
pub fn push_entry(&mut self, entry: CheckIntegrityEntry) {
debug!(
gid = entry.gid,
queue_len = self.queue.len(),
"Integrity check entry queued"
);
self.queue.push_back(entry);
self.wake.notify_one();
}
pub fn take_next_owned(&mut self) -> Option<CheckIntegrityEntry> {
if self.active_count >= self.max_concurrent {
return None;
}
let mut entry = self.queue.pop_front()?;
self.active_count += 1;
debug!(gid = entry.gid, "Integrity check entry picked by worker");
let total = entry.task.total_length();
self.picked = Some(PickedMeta {
gid: entry.gid,
total_length: total,
cancelled: Arc::clone(&entry.cancelled),
progress: Arc::new(std::sync::atomic::AtomicU64::new(0)),
});
entry.progress = Arc::clone(&self.picked.as_ref().expect("picked metadata").progress);
Some(entry)
}
pub fn drop_picked(&mut self) {
if self.picked.take().is_some() {
self.active_count = self.active_count.saturating_sub(1);
self.wake.notify_one();
}
}
pub fn is_picked(&self) -> bool {
self.picked.is_some()
}
pub fn has_next(&self) -> bool {
!self.queue.is_empty()
}
pub fn count_in_queue(&self) -> usize {
self.queue.len()
}
pub fn active_count(&self) -> usize {
self.active_count
}
pub fn current_progress(&self) -> Option<(u64, u64)> {
self.picked.as_ref().map(|m| {
(
m.progress
.load(std::sync::atomic::Ordering::Relaxed)
.min(m.total_length),
m.total_length,
)
})
}
pub fn status_for_gid(&self, gid: u64) -> (Option<u64>, bool) {
let verified_length = self
.picked
.as_ref()
.filter(|entry| entry.gid == gid)
.map(|entry| {
entry
.progress
.load(std::sync::atomic::Ordering::Relaxed)
.min(entry.total_length)
});
let queued = self.queue.iter().any(|entry| entry.gid == gid);
(verified_length, queued)
}
pub fn cancel_all(&mut self) {
for entry in self.queue.drain(..) {
entry.mark_cancelled();
if let Some(tx) = entry.done_tx {
let _ = tx.send(Err(cancelled_error()));
}
}
if let Some(meta) = self.picked.as_ref() {
meta.cancelled.store(true, Ordering::Relaxed);
}
self.wake.notify_one();
}
pub fn cancel_gid(&mut self, gid: u64) -> bool {
let mut cancelled = false;
let mut retained = VecDeque::with_capacity(self.queue.len());
while let Some(entry) = self.queue.pop_front() {
if entry.gid == gid {
entry.mark_cancelled();
if let Some(tx) = entry.done_tx {
let _ = tx.send(Err(cancelled_error()));
}
cancelled = true;
} else {
retained.push_back(entry);
}
}
self.queue = retained;
if let Some(meta) = self.picked.as_ref()
&& meta.gid == gid
{
meta.cancelled.store(true, Ordering::Relaxed);
cancelled = true;
}
if cancelled {
self.wake.notify_one();
}
cancelled
}
pub(super) fn wake_notifier(&self) -> Arc<Notify> {
Arc::clone(&self.wake)
}
}
impl Default for CheckIntegrityMan {
fn default() -> Self {
Self::new()
}
}