use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use crate::entry::TaskEnvelope;
use crate::queue::{
channel, ConsumerBoundary, ConsumerPoll, ProducerBoundary, Reservation, ReserveError,
SequencedReceiver, SequencedSender,
};
use crate::route::TaskType;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StrictQueue {
Stock,
Incremental,
Staging,
}
struct StrictFifo {
sender: SequencedSender<Arc<TaskEnvelope>>,
receiver: Mutex<SequencedReceiver<Arc<TaskEnvelope>>>,
depth: AtomicUsize,
}
impl StrictFifo {
fn new() -> Self {
let (sender, receiver) = channel();
Self {
sender,
receiver: Mutex::new(receiver),
depth: AtomicUsize::new(0),
}
}
fn reserve(&self) -> Result<Reservation<Arc<TaskEnvelope>>, ReserveError> {
let reservation = self.sender.reserve()?;
self.depth.fetch_add(1, Ordering::AcqRel);
Ok(reservation)
}
fn commit_reserved(&self) -> usize {
self.depth.load(Ordering::Acquire)
}
fn abort_reserved(&self) {
let decremented = self
.depth
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
value.checked_sub(1)
})
.is_ok();
debug_assert!(decremented, "strict FIFO depth authority lost");
self.sender.close();
}
fn poll(&self) -> ConsumerPoll<Arc<TaskEnvelope>> {
let result = self
.receiver
.lock()
.unwrap_or_else(|e| e.into_inner())
.try_recv();
if matches!(
result,
ConsumerPoll::Item { .. } | ConsumerPoll::Poisoned { .. }
) {
let decremented = self
.depth
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
value.checked_sub(1)
})
.is_ok();
debug_assert!(decremented, "strict FIFO depth authority lost");
}
result
}
fn close(&self) {
self.sender.close();
}
fn depth(&self) -> usize {
self.depth.load(Ordering::Acquire)
}
fn producer_inflight(&self) -> usize {
self.sender
.producer_inflight()
.saturating_add(self.sender.outstanding_reservations())
}
}
pub(crate) struct StrictTunnel {
id: u64,
runner_id: u64,
generation: u64,
source: u32,
target: u32,
task_type: TaskType,
accepting: AtomicBool,
target_execution: AtomicBool,
activity_epoch: AtomicU64,
stock: StrictFifo,
incremental: StrictFifo,
staging: StrictFifo,
source_boundary: OnceLock<ProducerBoundary>,
migration_complete: AtomicBool,
failure_reason: OnceLock<&'static str>,
}
impl StrictTunnel {
pub(crate) fn new(
id: u64,
runner_id: u64,
generation: u64,
source: u32,
target: u32,
task_type: TaskType,
) -> Arc<Self> {
Arc::new(Self {
id,
runner_id,
generation,
source,
target,
task_type,
accepting: AtomicBool::new(true),
target_execution: AtomicBool::new(true),
activity_epoch: AtomicU64::new(0),
stock: StrictFifo::new(),
incremental: StrictFifo::new(),
staging: StrictFifo::new(),
source_boundary: OnceLock::new(),
migration_complete: AtomicBool::new(false),
failure_reason: OnceLock::new(),
})
}
pub(crate) fn record_failure(&self, reason: &'static str) -> bool {
self.failure_reason.set(reason).is_ok()
}
pub(crate) fn failure_reason(&self) -> Option<&'static str> {
self.failure_reason.get().copied()
}
pub(crate) fn id(&self) -> u64 {
self.id
}
pub(crate) fn runner_id(&self) -> u64 {
self.runner_id
}
pub(crate) fn generation(&self) -> u64 {
self.generation
}
pub(crate) fn source(&self) -> u32 {
self.source
}
pub(crate) fn target(&self) -> u32 {
self.target
}
pub(crate) fn task_type(&self) -> TaskType {
self.task_type
}
pub(crate) fn reserve(
&self,
queue: StrictQueue,
) -> Result<Reservation<Arc<TaskEnvelope>>, ReserveError> {
if !self.accepting.load(Ordering::Acquire) && queue != StrictQueue::Staging {
return Err(ReserveError::Closed);
}
match queue {
StrictQueue::Stock => self.stock.reserve(),
StrictQueue::Incremental => self.incremental.reserve(),
StrictQueue::Staging => self.staging.reserve(),
}
}
pub(crate) fn commit_reserved(&self, queue: StrictQueue) -> usize {
self.activity_epoch.fetch_add(1, Ordering::AcqRel);
match queue {
StrictQueue::Stock => self.stock.commit_reserved(),
StrictQueue::Incremental => self.incremental.commit_reserved(),
StrictQueue::Staging => self.staging.commit_reserved(),
}
}
pub(crate) fn abort_reserved(&self, queue: StrictQueue) {
match queue {
StrictQueue::Stock => self.stock.abort_reserved(),
StrictQueue::Incremental => self.incremental.abort_reserved(),
StrictQueue::Staging => self.staging.abort_reserved(),
}
self.close_all();
}
pub(crate) fn poll_executable(&self) -> ConsumerPoll<Arc<TaskEnvelope>> {
if !self.target_execution.load(Ordering::Acquire) {
return ConsumerPoll::Empty;
}
match self.stock.poll() {
ConsumerPoll::Empty | ConsumerPoll::Closed
if self.migration_complete.load(Ordering::Acquire) =>
{
self.incremental.poll()
}
ConsumerPoll::Empty | ConsumerPoll::Closed => ConsumerPoll::Empty,
other => other,
}
}
pub(crate) fn install_source_boundary(&self, boundary: ProducerBoundary) -> bool {
match self.source_boundary.set(boundary) {
Ok(()) => true,
Err(current) => current == boundary,
}
}
pub(crate) fn complete_migration(&self, consumer: ConsumerBoundary) -> bool {
let Some(boundary) = self.source_boundary.get().copied() else {
return false;
};
if !consumer.reached(boundary) {
return false;
}
self.migration_complete.store(true, Ordering::Release);
true
}
pub(crate) fn poll_return(&self, queue: StrictQueue) -> ConsumerPoll<Arc<TaskEnvelope>> {
match queue {
StrictQueue::Stock => self.stock.poll(),
StrictQueue::Incremental => self.incremental.poll(),
StrictQueue::Staging => self.staging.poll(),
}
}
pub(crate) fn poll_failure(&self) -> (StrictQueue, ConsumerPoll<Arc<TaskEnvelope>>) {
let stock = self.stock.poll();
match stock {
ConsumerPoll::Empty | ConsumerPoll::Closed => {}
other => return (StrictQueue::Stock, other),
}
let incremental = self.incremental.poll();
match incremental {
ConsumerPoll::Empty | ConsumerPoll::Closed => {}
other => return (StrictQueue::Incremental, other),
}
(StrictQueue::Staging, self.staging.poll())
}
pub(crate) fn begin_return(&self) {
self.accepting.store(false, Ordering::Release);
self.stock.close();
self.incremental.close();
}
pub(crate) fn begin_return_prepare(&self) {
self.target_execution.store(false, Ordering::Release);
}
pub(crate) fn resume_stolen_execution(&self) {
self.target_execution.store(true, Ordering::Release);
}
pub(crate) fn begin_staging_drain(&self) {
self.staging.close();
}
pub(crate) fn close_all(&self) {
self.accepting.store(false, Ordering::Release);
self.stock.close();
self.incremental.close();
self.staging.close();
}
pub(crate) fn depth(&self) -> usize {
self.stock.depth() + self.incremental.depth() + self.staging.depth()
}
pub(crate) fn queue_depths(&self) -> (usize, usize, usize) {
(
self.stock.depth(),
self.incremental.depth(),
self.staging.depth(),
)
}
pub(crate) fn producer_inflight(&self) -> usize {
self.stock
.producer_inflight()
.saturating_add(self.incremental.producer_inflight())
.saturating_add(self.staging.producer_inflight())
}
pub(crate) fn executable_empty(&self) -> bool {
self.stock.depth() == 0 && self.incremental.depth() == 0
}
pub(crate) fn direct_producers_stopped(&self) -> bool {
self.stock.producer_inflight() == 0 && self.incremental.producer_inflight() == 0
}
pub(crate) fn staging_producers_stopped(&self) -> bool {
self.staging.producer_inflight() == 0
}
pub(crate) fn staging_depth(&self) -> usize {
self.staging.depth()
}
pub(crate) fn activity_epoch(&self) -> u64 {
self.activity_epoch.load(Ordering::Acquire)
}
}