use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use super::authority::{
AuthorityRefusal, CurrentMutationGrantAuthority, MutationAuthority, NonCurrentPublicationProof,
};
use super::physical_root::{PhysicalRootLease, WriteReceipt};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Termination {
Committed,
NoSideEffect,
Drained,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PermitState {
Granted,
InFlight,
Terminal(Termination),
}
#[derive(Debug, Default)]
pub struct PermitDrainSignal {
armed: AtomicBool,
ended: AtomicBool,
termination: std::sync::Mutex<Option<Termination>>,
}
impl PermitDrainSignal {
pub fn new() -> Self {
Self::default()
}
pub fn has_ended(&self) -> bool {
!self.armed.load(Ordering::Acquire) || self.ended.load(Ordering::Acquire)
}
fn arm(&self) {
self.armed.store(true, Ordering::Release);
}
pub fn termination(&self) -> Option<Termination> {
*self
.termination
.lock()
.expect("permit drain signal mutex is never held across a panic")
}
fn record(&self, termination: Termination) {
let mut slot = self
.termination
.lock()
.expect("permit drain signal mutex is never held across a panic");
if slot.is_none() {
*slot = Some(termination);
self.ended.store(true, Ordering::Release);
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct RefreshTicket {
epoch: super::authority::MutationEpoch,
termination: Termination,
}
impl RefreshTicket {
pub fn epoch(&self) -> super::authority::MutationEpoch {
self.epoch
}
pub fn termination(&self) -> Termination {
self.termination
}
}
#[derive(Debug)]
pub struct SourceMutationPermit {
authority: MutationAuthority,
published_non_current: NonCurrentPublicationProof,
lease: Arc<PhysicalRootLease>,
state: PermitState,
drain: Arc<PermitDrainSignal>,
}
impl SourceMutationPermit {
pub fn grant(
grant: CurrentMutationGrantAuthority,
lease: Arc<PhysicalRootLease>,
drain: Arc<PermitDrainSignal>,
) -> Result<Self, AuthorityRefusal> {
let (authority, published_non_current) = grant.into_parts();
if authority.binding().physical_root() != lease.identity() {
return Err(AuthorityRefusal::WholeAuthorityMismatch);
}
if !authority.binding().is_live() {
return Err(AuthorityRefusal::BindingRevoked {
binding: authority.binding().identity(),
});
}
if !lease.is_live() {
return Err(AuthorityRefusal::PhysicalRootReplaced);
}
drain.arm();
Ok(Self {
authority,
published_non_current,
lease,
state: PermitState::Granted,
drain,
})
}
pub fn replace_beneath(
&mut self,
relative: &std::path::Path,
contents: &[u8],
) -> Result<WriteReceipt, AuthorityRefusal> {
if self.state != PermitState::InFlight {
return Err(AuthorityRefusal::PermitAlreadyTerminal);
}
super::physical_root::replace_beneath(&self.lease, relative, contents)
.map_err(AuthorityRefusal::from)
}
pub fn attest_delegated_beneath(
&mut self,
relative: &std::path::Path,
expected: &[u8],
) -> Result<Option<WriteReceipt>, AuthorityRefusal> {
if self.state != PermitState::InFlight {
return Err(AuthorityRefusal::PermitAlreadyTerminal);
}
super::physical_root::verify_replacement_beneath(&self.lease, relative, expected)
.map_err(AuthorityRefusal::from)
}
pub fn authority(&self) -> &MutationAuthority {
&self.authority
}
pub fn lease(&self) -> &Arc<PhysicalRootLease> {
&self.lease
}
pub fn published_non_current(&self) -> &NonCurrentPublicationProof {
&self.published_non_current
}
pub fn is_terminal(&self) -> bool {
matches!(self.state, PermitState::Terminal(_))
}
pub fn start_side_effect(&mut self) -> Result<(), AuthorityRefusal> {
match self.state {
PermitState::Terminal(_) => return Err(AuthorityRefusal::PermitAlreadyTerminal),
PermitState::InFlight => return Err(AuthorityRefusal::SideEffectAlreadyInFlight),
PermitState::Granted => {}
}
if !self.lease.is_live() {
return Err(AuthorityRefusal::PhysicalRootReplaced);
}
self.state = PermitState::InFlight;
Ok(())
}
pub fn commit(&mut self, receipt: WriteReceipt) -> Result<RefreshTicket, AuthorityRefusal> {
if self.state != PermitState::InFlight {
return Err(AuthorityRefusal::PermitAlreadyTerminal);
}
if receipt.lease() != self.lease.identity() {
return Err(AuthorityRefusal::WholeAuthorityMismatch);
}
self.finish(Termination::Committed)
}
pub fn no_side_effect(&mut self) -> Result<RefreshTicket, AuthorityRefusal> {
match self.state {
PermitState::Terminal(_) => return Err(AuthorityRefusal::PermitAlreadyTerminal),
PermitState::InFlight => return Err(AuthorityRefusal::SideEffectAlreadyInFlight),
PermitState::Granted => {}
}
self.finish(Termination::NoSideEffect)
}
fn finish(&mut self, termination: Termination) -> Result<RefreshTicket, AuthorityRefusal> {
self.state = PermitState::Terminal(termination);
self.drain.record(termination);
Ok(RefreshTicket {
epoch: self.authority.epoch(),
termination,
})
}
}
impl Drop for SourceMutationPermit {
fn drop(&mut self) {
if !matches!(self.state, PermitState::Terminal(_)) {
self.drain.record(Termination::Drained);
}
}
}