#![allow(dead_code)]
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::future::Future;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
use std::sync::{Arc, Mutex};
use tokio::runtime::Handle as RuntimeHandle;
use tokio::sync::Notify;
use tokio::task::{AbortHandle, JoinError, JoinHandle};
const MODE_RUNNING: u8 = 0;
const MODE_GRACEFUL_STOP: u8 = 1;
const MODE_FORCE_STOP: u8 = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LifecycleMode {
Running,
GracefulStop,
ForceStop,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ChildTaskKind {
Worker,
Observer,
Timer,
Business,
Cleanup,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ChildTaskId(u64);
impl ChildTaskId {
pub(crate) fn get(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RegisterError {
IdExhausted,
AuthorityFailed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SupervisorStartError {
PreviousNotJoined,
AuthorityFailed,
RuntimeUnavailable,
}
#[derive(Debug)]
pub(crate) enum SupervisorWaitError {
Join(JoinError),
AuthorityLost,
}
struct ChildHandleEntry {
kind: ChildTaskKind,
handle: Option<JoinHandle<()>>,
abort: AbortHandle,
}
pub(crate) struct LifecycleCore {
generation: u64,
mode: AtomicU8,
authority_failed: AtomicBool,
supervisor_active: AtomicBool,
supervisor_epoch: AtomicU64,
revision: AtomicU64,
next_child_id: AtomicU64,
children: Mutex<BTreeMap<ChildTaskId, ChildHandleEntry>>,
wake: Notify,
}
impl LifecycleCore {
pub(crate) fn new(generation: u64) -> Arc<Self> {
Arc::new(Self {
generation,
mode: AtomicU8::new(MODE_RUNNING),
authority_failed: AtomicBool::new(false),
supervisor_active: AtomicBool::new(false),
supervisor_epoch: AtomicU64::new(0),
revision: AtomicU64::new(0),
next_child_id: AtomicU64::new(0),
children: Mutex::new(BTreeMap::new()),
wake: Notify::new(),
})
}
pub(crate) fn generation(&self) -> u64 {
self.generation
}
pub(crate) fn mode(&self) -> LifecycleMode {
match self.mode.load(Ordering::Acquire) {
MODE_RUNNING => LifecycleMode::Running,
MODE_GRACEFUL_STOP => LifecycleMode::GracefulStop,
_ => LifecycleMode::ForceStop,
}
}
pub(crate) fn request_graceful_stop(&self) -> bool {
let changed = self
.mode
.compare_exchange(
MODE_RUNNING,
MODE_GRACEFUL_STOP,
Ordering::AcqRel,
Ordering::Acquire,
)
.is_ok();
if changed {
self.signal_change();
}
changed
}
pub(crate) fn request_force_stop(&self) -> bool {
let previous = self.mode.swap(MODE_FORCE_STOP, Ordering::AcqRel);
let changed = previous != MODE_FORCE_STOP;
if changed {
self.signal_change();
}
changed
}
pub(crate) fn fail_authority(&self) -> bool {
let first = !self.authority_failed.swap(true, Ordering::AcqRel);
self.request_force_stop();
first
}
pub(crate) fn authority_failed(&self) -> bool {
self.authority_failed.load(Ordering::Acquire)
}
pub(crate) fn claim_supervisor(self: &Arc<Self>) -> Option<SupervisorGuard> {
if self
.supervisor_active
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_err()
{
return None;
}
let epoch =
match self
.supervisor_epoch
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
value.checked_add(1)
}) {
Ok(previous) => previous + 1,
Err(_) => {
self.supervisor_active.store(false, Ordering::Release);
self.fail_authority();
return None;
}
};
Some(SupervisorGuard {
core: self.clone(),
epoch,
active: true,
})
}
pub(crate) fn supervisor_epoch(&self) -> u64 {
self.supervisor_epoch.load(Ordering::Acquire)
}
pub(crate) fn register_child(
&self,
kind: ChildTaskKind,
handle: JoinHandle<()>,
) -> Result<ChildTaskId, RegisterError> {
if self.authority_failed() {
handle.abort();
return Err(RegisterError::AuthorityFailed);
}
let id =
match self
.next_child_id
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
value.checked_add(1)
}) {
Ok(value) => ChildTaskId(value),
Err(_) => {
handle.abort();
self.fail_authority();
return Err(RegisterError::IdExhausted);
}
};
let abort = handle.abort_handle();
let mut children = self.children.lock().unwrap_or_else(|e| e.into_inner());
match children.entry(id) {
Entry::Vacant(slot) => {
slot.insert(ChildHandleEntry {
kind,
handle: Some(handle),
abort,
});
}
Entry::Occupied(_) => {
drop(children);
handle.abort();
self.fail_authority();
return Err(RegisterError::AuthorityFailed);
}
}
drop(children);
self.signal_change();
Ok(id)
}
pub(crate) fn lease_child(self: &Arc<Self>, id: ChildTaskId) -> Option<HandleLease> {
let mut children = self.children.lock().unwrap_or_else(|e| e.into_inner());
let entry = children.get_mut(&id)?;
let handle = entry.handle.take()?;
Some(HandleLease {
core: self.clone(),
id,
kind: entry.kind,
handle: Some(handle),
})
}
pub(crate) fn abort_all(&self) -> usize {
let handles = self
.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.values()
.map(|entry| entry.abort.clone())
.collect::<Vec<_>>();
for handle in &handles {
handle.abort();
}
handles.len()
}
pub(crate) fn abort_kind(&self, kind: ChildTaskKind) -> usize {
let handles = self
.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.values()
.filter(|entry| entry.kind == kind)
.map(|entry| entry.abort.clone())
.collect::<Vec<_>>();
for handle in &handles {
handle.abort();
}
handles.len()
}
pub(crate) fn child_count_by_kind(&self, kind: ChildTaskKind) -> usize {
self.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.values()
.filter(|entry| entry.kind == kind)
.count()
}
pub(crate) fn child_count(&self) -> usize {
self.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.len()
}
pub(crate) fn leased_count(&self) -> usize {
self.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.values()
.filter(|entry| entry.handle.is_none())
.count()
}
pub(crate) fn child_ids(&self) -> Vec<ChildTaskId> {
self.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.keys()
.copied()
.collect()
}
pub(crate) fn finished_child_ids(&self) -> Vec<ChildTaskId> {
self.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.iter()
.filter_map(|(id, entry)| {
entry
.handle
.as_ref()
.is_some_and(JoinHandle::is_finished)
.then_some(*id)
})
.collect()
}
pub(crate) fn notify_progress(&self) {
self.signal_change();
}
pub(crate) fn revision(&self) -> u64 {
self.revision.load(Ordering::Acquire)
}
pub(crate) async fn changed_since(&self, observed: u64) {
loop {
let notified = self.wake.notified();
if self.revision() != observed {
return;
}
crate::shield_future(notified).await;
}
}
fn signal_change(&self) {
if self
.revision
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |value| {
value.checked_add(1)
})
.is_err()
{
self.authority_failed.store(true, Ordering::Release);
self.mode.store(MODE_FORCE_STOP, Ordering::Release);
}
self.wake.notify_waiters();
self.wake.notify_one();
}
}
struct SupervisorSlot {
epoch: Option<u64>,
handle: Option<JoinHandle<()>>,
abort: Option<AbortHandle>,
joined_epoch: u64,
}
pub(crate) struct GenerationControl {
generation: u64,
lifecycle: Arc<LifecycleCore>,
supervisor: Mutex<SupervisorSlot>,
}
impl GenerationControl {
pub(crate) fn new(generation: u64) -> Arc<Self> {
Arc::new(Self {
generation,
lifecycle: LifecycleCore::new(generation),
supervisor: Mutex::new(SupervisorSlot {
epoch: None,
handle: None,
abort: None,
joined_epoch: 0,
}),
})
}
pub(crate) fn generation(&self) -> u64 {
self.generation
}
pub(crate) fn lifecycle(&self) -> Arc<LifecycleCore> {
self.lifecycle.clone()
}
pub(crate) fn spawn_supervisor(
self: &Arc<Self>,
future: impl Future<Output = ()> + Send + 'static,
) -> Result<u64, SupervisorStartError> {
let runtime =
RuntimeHandle::try_current().map_err(|_| SupervisorStartError::RuntimeUnavailable)?;
let mut slot = self.supervisor.lock().unwrap_or_else(|e| e.into_inner());
if slot.epoch.is_some() || slot.handle.is_some() || slot.abort.is_some() {
return Err(SupervisorStartError::PreviousNotJoined);
}
let guard = self
.lifecycle
.claim_supervisor()
.ok_or(SupervisorStartError::AuthorityFailed)?;
let epoch = guard.epoch();
let handle = runtime.spawn(async move {
let _authority = guard;
future.await;
});
let abort = handle.abort_handle();
slot.epoch = Some(epoch);
slot.handle = Some(handle);
slot.abort = Some(abort);
drop(slot);
self.lifecycle.signal_change();
Ok(epoch)
}
pub(crate) fn lease_supervisor(self: &Arc<Self>) -> Option<SupervisorLease> {
let mut slot = self.supervisor.lock().unwrap_or_else(|e| e.into_inner());
let epoch = slot.epoch?;
let handle = slot.handle.take()?;
Some(SupervisorLease {
control: self.clone(),
epoch,
handle: Some(handle),
})
}
pub(crate) fn abort_supervisor(&self) -> bool {
let abort = self
.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner())
.abort
.clone();
if let Some(abort) = abort {
abort.abort();
true
} else {
false
}
}
pub(crate) fn supervisor_epoch(&self) -> Option<u64> {
self.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner())
.epoch
}
pub(crate) fn supervisor_finished(&self) -> bool {
self.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner())
.handle
.as_ref()
.is_some_and(JoinHandle::is_finished)
}
pub(crate) fn joined_supervisor_epoch(&self) -> u64 {
self.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner())
.joined_epoch
}
}
impl Drop for GenerationControl {
fn drop(&mut self) {
self.lifecycle.request_force_stop();
let slot = self.supervisor.get_mut().unwrap_or_else(|e| e.into_inner());
if let Some(abort) = &slot.abort {
abort.abort();
}
self.lifecycle.abort_all();
}
}
pub(crate) struct SupervisorLease {
control: Arc<GenerationControl>,
epoch: u64,
handle: Option<JoinHandle<()>>,
}
impl SupervisorLease {
pub(crate) fn epoch(&self) -> u64 {
self.epoch
}
pub(crate) async fn join(mut self) -> Result<(), SupervisorWaitError> {
let result = match self.handle.as_mut() {
Some(handle) => handle.await,
None => {
self.control.lifecycle.fail_authority();
return Err(SupervisorWaitError::AuthorityLost);
}
};
self.handle.take();
let mut slot = self
.control
.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner());
if slot.epoch != Some(self.epoch) || slot.handle.is_some() || slot.abort.is_none() {
drop(slot);
self.control.lifecycle.fail_authority();
return Err(SupervisorWaitError::AuthorityLost);
}
slot.epoch = None;
slot.abort = None;
slot.joined_epoch = self.epoch;
drop(slot);
self.control.lifecycle.signal_change();
result.map_err(SupervisorWaitError::Join)
}
}
impl Drop for SupervisorLease {
fn drop(&mut self) {
let Some(handle) = self.handle.take() else {
return;
};
let mut slot = self
.control
.supervisor
.lock()
.unwrap_or_else(|e| e.into_inner());
if slot.epoch != Some(self.epoch) || slot.handle.is_some() || slot.abort.is_none() {
drop(slot);
handle.abort();
self.control.lifecycle.fail_authority();
return;
}
slot.handle = Some(handle);
drop(slot);
self.control.lifecycle.signal_change();
}
}
impl Drop for LifecycleCore {
fn drop(&mut self) {
let children = self.children.get_mut().unwrap_or_else(|e| e.into_inner());
for entry in children.values() {
entry.abort.abort();
}
}
}
pub(crate) struct SupervisorGuard {
core: Arc<LifecycleCore>,
epoch: u64,
active: bool,
}
impl SupervisorGuard {
pub(crate) fn epoch(&self) -> u64 {
self.epoch
}
pub(crate) fn is_current(&self) -> bool {
self.active
&& self.core.supervisor_active.load(Ordering::Acquire)
&& self.core.supervisor_epoch() == self.epoch
}
}
impl Drop for SupervisorGuard {
fn drop(&mut self) {
if !self.active {
return;
}
if self.core.supervisor_epoch() == self.epoch {
self.core.supervisor_active.store(false, Ordering::Release);
self.core.signal_change();
}
self.active = false;
}
}
pub(crate) struct HandleLease {
core: Arc<LifecycleCore>,
id: ChildTaskId,
kind: ChildTaskKind,
handle: Option<JoinHandle<()>>,
}
impl HandleLease {
pub(crate) fn kind(&self) -> ChildTaskKind {
self.kind
}
pub(crate) async fn join(mut self) -> Result<(), JoinError> {
let result = match self.handle.as_mut() {
Some(handle) => handle.await,
None => {
self.core.fail_authority();
return Ok(());
}
};
self.handle.take();
let removed = self
.core
.children
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&self.id);
if removed.is_none() {
self.core.fail_authority();
}
self.core.signal_change();
result
}
}
impl Drop for HandleLease {
fn drop(&mut self) {
let Some(handle) = self.handle.take() else {
return;
};
let mut children = self.core.children.lock().unwrap_or_else(|e| e.into_inner());
let Some(entry) = children.get_mut(&self.id) else {
drop(children);
handle.abort();
self.core.fail_authority();
return;
};
if entry.handle.is_some() {
drop(children);
handle.abort();
self.core.fail_authority();
return;
}
entry.handle = Some(handle);
drop(children);
self.core.signal_change();
}
}