use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_IDENTITY: AtomicU64 = AtomicU64::new(1);
fn next_identity() -> NonZeroU64 {
let raw = NEXT_IDENTITY.fetch_add(1, Ordering::Relaxed);
NonZeroU64::new(raw).expect("identity counter starts at 1 and only increases")
}
macro_rules! identity_newtype {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name(NonZeroU64);
impl $name {
pub fn fresh() -> Self {
Self(next_identity())
}
}
};
}
identity_newtype!(
BindingIdentity
);
identity_newtype!(
ObserverToken
);
identity_newtype!(
CandidateIdentity
);
identity_newtype!(
GenerationIdentity
);
identity_newtype!(
PublicationIdentity
);
identity_newtype!(
SnapshotIdentity
);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MutationEpoch(u64);
impl MutationEpoch {
pub const fn initial() -> Self {
Self(0)
}
fn advanced(self) -> Self {
Self(self.0 + 1)
}
pub const fn get(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone)]
pub struct BindingAuthority {
identity: BindingIdentity,
physical_root: crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity,
revoked: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl PartialEq for BindingAuthority {
fn eq(&self, other: &Self) -> bool {
self.identity == other.identity && self.physical_root == other.physical_root
}
}
impl Eq for BindingAuthority {}
impl BindingAuthority {
pub fn bind(
physical_root: crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity,
) -> Self {
Self {
identity: BindingIdentity::fresh(),
physical_root,
revoked: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
}
}
pub fn revoke(&self) {
self.revoked
.store(true, std::sync::atomic::Ordering::Release);
}
pub fn is_live(&self) -> bool {
!self.revoked.load(std::sync::atomic::Ordering::Acquire)
}
pub fn identity(&self) -> BindingIdentity {
self.identity
}
pub fn physical_root(
&self,
) -> crate::live_index::index_lifecycle::physical_root::PhysicalRootIdentity {
self.physical_root
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CandidateAuthority {
identity: CandidateIdentity,
binding: BindingAuthority,
observer_cut: ObserverToken,
}
impl CandidateAuthority {
pub fn open(binding: BindingAuthority, observer_cut: ObserverToken) -> Self {
Self {
identity: CandidateIdentity::fresh(),
binding,
observer_cut,
}
}
pub fn identity(&self) -> CandidateIdentity {
self.identity
}
pub fn binding(&self) -> &BindingAuthority {
&self.binding
}
pub fn observer_cut(&self) -> ObserverToken {
self.observer_cut
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct MutationAuthority {
generation: GenerationIdentity,
binding: BindingAuthority,
epoch: MutationEpoch,
publication: PublicationIdentity,
}
impl MutationAuthority {
pub fn generation(&self) -> GenerationIdentity {
self.generation
}
pub fn binding(&self) -> &BindingAuthority {
&self.binding
}
pub fn epoch(&self) -> MutationEpoch {
self.epoch
}
pub fn publication(&self) -> PublicationIdentity {
self.publication
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct NonCurrentPublicationProof {
publication: PublicationIdentity,
epoch: MutationEpoch,
}
impl NonCurrentPublicationProof {
pub fn publication(&self) -> PublicationIdentity {
self.publication
}
pub fn epoch(&self) -> MutationEpoch {
self.epoch
}
}
#[derive(Debug)]
pub struct CurrentMutationGrantAuthority {
authority: MutationAuthority,
published_non_current: NonCurrentPublicationProof,
}
impl CurrentMutationGrantAuthority {
pub fn authority(&self) -> &MutationAuthority {
&self.authority
}
pub fn published_non_current(&self) -> &NonCurrentPublicationProof {
&self.published_non_current
}
pub(crate) fn into_parts(self) -> (MutationAuthority, NonCurrentPublicationProof) {
(self.authority, self.published_non_current)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CurrentPublication {
publication: PublicationIdentity,
generation: GenerationIdentity,
binding: BindingAuthority,
observer_cut: ObserverToken,
}
impl CurrentPublication {
pub fn promote(binding: BindingAuthority, observer_cut: ObserverToken) -> Self {
Self {
publication: PublicationIdentity::fresh(),
generation: GenerationIdentity::fresh(),
binding,
observer_cut,
}
}
pub fn publication(&self) -> PublicationIdentity {
self.publication
}
pub fn generation(&self) -> GenerationIdentity {
self.generation
}
pub fn binding(&self) -> &BindingAuthority {
&self.binding
}
pub fn observer_cut(&self) -> ObserverToken {
self.observer_cut
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MutationGrantInput {
LiveCurrent(PublicationIdentity),
Candidate(CandidateIdentity),
Snapshot(SnapshotIdentity),
RetainedGeneration(GenerationIdentity),
StalePublication(PublicationIdentity),
}
impl MutationGrantInput {
fn provenance(&self) -> Provenance {
match self {
Self::LiveCurrent(_) => Provenance::LiveCurrent,
Self::Candidate(_) => Provenance::Candidate,
Self::Snapshot(_) => Provenance::Snapshot,
Self::RetainedGeneration(_) => Provenance::RetainedGeneration,
Self::StalePublication(_) => Provenance::StalePublication,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Provenance {
LiveCurrent,
Candidate,
Snapshot,
RetainedGeneration,
StalePublication,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PhaseName {
Loading,
Current,
Refreshing,
Blocked,
Stopping,
}
#[derive(Debug)]
enum SourcePhase {
Loading {
binding: BindingAuthority,
observer_phase: ObserverPhase,
work: NonCurrentWork,
},
Current(CurrentPublication),
Refreshing {
retained: GenerationIdentity,
binding: BindingAuthority,
publication: PublicationIdentity,
observer_phase: ObserverPhase,
active_permits: ActivePermits,
work: NonCurrentWork,
},
Blocked {
binding: BindingAuthority,
observer_phase: ObserverPhase,
retained: Option<GenerationIdentity>,
cause: BlockedCause,
},
Stopping {
retained: Option<GenerationIdentity>,
committed_source_revocation_residency: Option<RevocationResidency>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObserverPhase {
Absent,
Active {
token: ObserverToken,
},
Draining {
token: ObserverToken,
},
ObserverFree {
handoff: ObserverToken,
},
}
impl ObserverPhase {
pub fn token(&self) -> Option<ObserverToken> {
match self {
Self::Active { token } | Self::Draining { token } => Some(*token),
Self::Absent | Self::ObserverFree { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NonCurrentWork {
Dirty,
WaitingForCapacity,
Building {
candidate: CandidateIdentity,
},
Verifying {
candidate: CandidateIdentity,
},
RetryWait {
attempt: u32,
},
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ActivePermits {
outstanding: Vec<PublicationIdentity>,
ever_recorded: bool,
}
impl ActivePermits {
pub fn none() -> Self {
Self::default()
}
pub fn record(&mut self, grant: PublicationIdentity) {
self.ever_recorded = true;
if !self.outstanding.contains(&grant) {
self.outstanding.push(grant);
}
}
pub fn ever_recorded(&self) -> bool {
self.ever_recorded
}
pub fn retire(&mut self, grant: PublicationIdentity) -> bool {
let before = self.outstanding.len();
self.outstanding.retain(|entry| *entry != grant);
self.outstanding.len() != before
}
pub fn is_drained(&self) -> bool {
self.outstanding.is_empty()
}
pub fn outstanding(&self) -> impl Iterator<Item = PublicationIdentity> + '_ {
self.outstanding.iter().copied()
}
pub fn len(&self) -> usize {
self.outstanding.len()
}
pub fn is_empty(&self) -> bool {
self.outstanding.is_empty()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockedCause {
VerificationFailed,
ObserverUnavailable,
CapacityRefused,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RevocationResidency {
charge: PublicationIdentity,
}
impl RevocationResidency {
pub fn committed() -> Self {
Self {
charge: PublicationIdentity::fresh(),
}
}
pub fn charge(self) -> PublicationIdentity {
self.charge
}
}
impl SourcePhase {
fn name(&self) -> PhaseName {
match self {
Self::Loading { .. } => PhaseName::Loading,
Self::Current(_) => PhaseName::Current,
Self::Refreshing { .. } => PhaseName::Refreshing,
Self::Blocked { .. } => PhaseName::Blocked,
Self::Stopping { .. } => PhaseName::Stopping,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthorityRefusal {
PhaseNotCurrent {
phase: PhaseName,
},
ProvenanceNotLiveCurrent {
provenance: Provenance,
},
PublicationIdentityMismatch {
presented: PublicationIdentity,
live: PublicationIdentity,
},
PhysicalRootReplaced,
PermitAlreadyTerminal,
SideEffectAlreadyInFlight,
WholeAuthorityMismatch,
BindingRevoked {
binding: BindingIdentity,
},
OutstandingPermit,
}
#[derive(Debug)]
pub struct SourceRuntime {
phase: SourcePhase,
mutation_epoch: MutationEpoch,
grants_issued: u64,
}
impl SourceRuntime {
pub fn loading(binding: BindingAuthority) -> Self {
Self {
phase: SourcePhase::Loading {
binding,
observer_phase: ObserverPhase::Absent,
work: NonCurrentWork::Dirty,
},
mutation_epoch: MutationEpoch::initial(),
grants_issued: 0,
}
}
pub fn current(publication: CurrentPublication) -> Self {
Self {
phase: SourcePhase::Current(publication),
mutation_epoch: MutationEpoch::initial(),
grants_issued: 0,
}
}
pub fn refreshing(binding: BindingAuthority, retained: GenerationIdentity) -> Self {
Self {
phase: SourcePhase::Refreshing {
retained,
binding,
publication: PublicationIdentity::fresh(),
observer_phase: ObserverPhase::Absent,
active_permits: ActivePermits::none(),
work: NonCurrentWork::Dirty,
},
mutation_epoch: MutationEpoch::initial(),
grants_issued: 0,
}
}
pub fn blocked(binding: BindingAuthority, retained: Option<GenerationIdentity>) -> Self {
Self {
phase: SourcePhase::Blocked {
binding,
observer_phase: ObserverPhase::Absent,
retained,
cause: BlockedCause::VerificationFailed,
},
mutation_epoch: MutationEpoch::initial(),
grants_issued: 0,
}
}
pub fn stopping(
retained: Option<GenerationIdentity>,
committed_source_revocation_residency: Option<RevocationResidency>,
) -> Self {
Self {
phase: SourcePhase::Stopping {
retained,
committed_source_revocation_residency,
},
mutation_epoch: MutationEpoch::initial(),
grants_issued: 0,
}
}
pub fn observer_phase(&self) -> ObserverPhase {
match &self.phase {
SourcePhase::Current(publication) => ObserverPhase::Active {
token: publication.observer_cut(),
},
SourcePhase::Loading { observer_phase, .. }
| SourcePhase::Refreshing { observer_phase, .. }
| SourcePhase::Blocked { observer_phase, .. } => observer_phase.clone(),
SourcePhase::Stopping { .. } => ObserverPhase::Absent,
}
}
pub fn work(&self) -> Option<NonCurrentWork> {
match &self.phase {
SourcePhase::Loading { work, .. } | SourcePhase::Refreshing { work, .. } => {
Some(work.clone())
}
SourcePhase::Current(_)
| SourcePhase::Blocked { .. }
| SourcePhase::Stopping { .. } => None,
}
}
pub fn active_permits(&self) -> ActivePermits {
match &self.phase {
SourcePhase::Refreshing { active_permits, .. } => active_permits.clone(),
_ => ActivePermits::none(),
}
}
pub fn committed_revocation_residency(&self) -> Option<RevocationResidency> {
match &self.phase {
SourcePhase::Stopping {
committed_source_revocation_residency,
..
} => *committed_source_revocation_residency,
_ => None,
}
}
pub fn phase(&self) -> PhaseName {
self.phase.name()
}
pub fn mutation_epoch(&self) -> MutationEpoch {
self.mutation_epoch
}
pub fn grants_issued(&self) -> u64 {
self.grants_issued
}
pub fn retained_generation(&self) -> Option<GenerationIdentity> {
match &self.phase {
SourcePhase::Loading { .. } => None,
SourcePhase::Current(publication) => Some(publication.generation()),
SourcePhase::Refreshing { retained, .. } => Some(*retained),
SourcePhase::Blocked { retained, .. } | SourcePhase::Stopping { retained, .. } => {
*retained
}
}
}
pub fn queryable_generation(&self) -> Option<GenerationIdentity> {
match &self.phase {
SourcePhase::Current(publication) => Some(publication.generation()),
SourcePhase::Refreshing {
retained,
active_permits,
..
} => (!active_permits.ever_recorded()).then_some(*retained),
SourcePhase::Loading { .. }
| SourcePhase::Blocked { .. }
| SourcePhase::Stopping { .. } => None,
}
}
pub fn is_queryable(&self) -> bool {
self.queryable_generation().is_some()
}
pub fn retained_binding(&self) -> Option<&BindingAuthority> {
match &self.phase {
SourcePhase::Current(publication) => Some(publication.binding()),
SourcePhase::Refreshing { binding, .. }
| SourcePhase::Loading { binding, .. }
| SourcePhase::Blocked { binding, .. } => Some(binding),
SourcePhase::Stopping { .. } => None,
}
}
pub fn blocked_cause(&self) -> Option<BlockedCause> {
match &self.phase {
SourcePhase::Blocked { cause, .. } => Some(cause.clone()),
_ => None,
}
}
pub fn live_publication(&self) -> Option<&CurrentPublication> {
match &self.phase {
SourcePhase::Current(publication) => Some(publication),
_ => None,
}
}
pub fn published_identity(&self) -> Option<PublicationIdentity> {
match &self.phase {
SourcePhase::Current(publication) => Some(publication.publication()),
SourcePhase::Refreshing { publication, .. } => Some(*publication),
SourcePhase::Loading { .. }
| SourcePhase::Blocked { .. }
| SourcePhase::Stopping { .. } => None,
}
}
pub fn freeze(&mut self) -> Option<PublicationIdentity> {
let (retained, binding, observer_phase, active_permits, work) = match &self.phase {
SourcePhase::Current(current) => (
current.generation(),
current.binding().clone(),
ObserverPhase::Active {
token: current.observer_cut(),
},
ActivePermits::none(),
NonCurrentWork::Dirty,
),
SourcePhase::Refreshing {
retained,
binding,
observer_phase,
active_permits,
work,
..
} => (
*retained,
binding.clone(),
observer_phase.clone(),
active_permits.clone(),
work.clone(),
),
SourcePhase::Loading { .. }
| SourcePhase::Blocked { .. }
| SourcePhase::Stopping { .. } => {
return None;
}
};
let publication = PublicationIdentity::fresh();
self.mutation_epoch = self.mutation_epoch.advanced();
self.phase = SourcePhase::Refreshing {
retained,
binding,
publication,
observer_phase,
active_permits,
work,
};
Some(publication)
}
pub fn record_permit(&mut self, grant: PublicationIdentity) -> bool {
match &mut self.phase {
SourcePhase::Refreshing { active_permits, .. } => {
active_permits.record(grant);
true
}
_ => false,
}
}
pub fn retire_permit(&mut self, grant: PublicationIdentity) -> bool {
match &mut self.phase {
SourcePhase::Refreshing { active_permits, .. } => active_permits.retire(grant),
_ => false,
}
}
pub fn set_work(&mut self, next: NonCurrentWork) -> bool {
match &mut self.phase {
SourcePhase::Loading { work, .. } | SourcePhase::Refreshing { work, .. } => {
*work = next;
true
}
_ => false,
}
}
pub fn set_observer_phase(&mut self, next: ObserverPhase) -> bool {
match &mut self.phase {
SourcePhase::Loading { observer_phase, .. }
| SourcePhase::Refreshing { observer_phase, .. }
| SourcePhase::Blocked { observer_phase, .. } => {
*observer_phase = next;
true
}
_ => false,
}
}
pub(crate) fn install(&mut self, publication: CurrentPublication) {
self.phase = SourcePhase::Current(publication);
}
pub fn request_mutation_grant(
&mut self,
input: MutationGrantInput,
) -> Result<CurrentMutationGrantAuthority, AuthorityRefusal> {
let live = match &self.phase {
SourcePhase::Current(publication) => publication.clone(),
other => {
return Err(AuthorityRefusal::PhaseNotCurrent {
phase: other.name(),
});
}
};
let presented = match input {
MutationGrantInput::LiveCurrent(presented) => presented,
other => {
return Err(AuthorityRefusal::ProvenanceNotLiveCurrent {
provenance: other.provenance(),
});
}
};
if presented != live.publication() {
return Err(AuthorityRefusal::PublicationIdentityMismatch {
presented,
live: live.publication(),
});
}
let publication = self
.freeze()
.expect("a Current source always has a publication to freeze");
self.record_permit(publication);
let epoch = self.mutation_epoch;
self.grants_issued += 1;
Ok(CurrentMutationGrantAuthority {
authority: MutationAuthority {
generation: live.generation(),
binding: live.binding().clone(),
epoch,
publication: live.publication(),
},
published_non_current: NonCurrentPublicationProof { publication, epoch },
})
}
}