foundationdb::recipes::leader_election

Struct LeaderElection

Source
pub struct LeaderElection { /* private fields */ }
Expand description

A handle for one independently scoped leader lease.

lease_duration is this caller’s non-zero desired duration. It is persisted on every successful acquisition, takeover, and renewal. Existing foreign records remain governed by their own persisted duration. The handle holds no caller-local state and performs no scheduling or retries.

Implementations§

Source§

impl LeaderElection

Source

pub fn new(subspace: Subspace, lease_duration: Duration) -> Result<Self>

Creates an election handle with the non-zero duration it will persist on successful ownership changes.

Constructing a handle does not read or initialize durable state.

Source

pub fn lease_duration(&self) -> Duration

Returns this handle’s desired duration for its future owner records.

Source

pub async fn poll<T>( &self, txn: &T, participant: &ParticipantId, local_state: &LocalState, attempt_started_at: Duration, ) -> Result<PollResult>
where T: Deref<Target = Transaction>,

Polls the durable lease state in the caller’s transaction.

A released state is acquired immediately. An exact, locally unexpired Leadership token renews ownership with a fresh revision. A first or changed Observation never steals; only an exact unchanged observation may take over, or same-owner reacquire, after the observed record’s persisted duration. An expired or mismatched leadership token becomes observation/reacquisition state and cannot renew directly.

attempt_started_at must be read from the caller’s monotonic clock immediately before this call in each retry attempt. It is deliberately before the durable read, making renewal and takeover decisions conservative relative to read and commit delay. After the enclosing Database::run succeeds, pass a fresh caller-clock reading to PollResult::into_next_state to adopt the returned state.

Renew well before the local deadline represented by Leadership, leaving headroom for scheduling delay, retries, and commit latency. This affects availability only; it is not a durable expiry or safety condition.

Source

pub async fn state<T>(&self, txn: &T) -> Result<ElectionState>
where T: Deref<Target = Transaction>,

Reads durable state without making any liveness or leadership-validity claim.

This is a snapshot only. It does not create a local observation, permit renewal or takeover, or authorize protected work.

Source

pub async fn resign<T>( &self, txn: &T, leadership: &Leadership, ) -> Result<ResignOutcome>
where T: Deref<Target = Transaction>,

Releases ownership only when leadership still exactly matches durable state.

The revision and persisted duration remain, so a later acquisition has a strictly newer fencing rank. A stale delayed resignation is rejected. This conditional operation does not make a liveness claim and may be used to relinquish an otherwise exact durable token after its local renewal window has elapsed.

Trait Implementations§

Source§

impl Clone for LeaderElection

Source§

fn clone(&self) -> LeaderElection

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LeaderElection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.