Skip to main content

DynamoLeaseStore

Struct DynamoLeaseStore 

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

A DynamoDB-backed lease store.

The table needs a string partition key named lease_key and nothing else; create it with on-demand billing. Instances race leases with conditional writes and steal only expired ones. One row holds one shard of one stream, under the key stream:shard, so one table serves every stream a service consumes.

§Rows from earlier versions

Earlier versions of this crate keyed a row by the shard id alone (shardId-000000000000), and a table written by them still holds those rows. A row keyed by a bare shard id does not say which stream it belongs to, so the store reads one only for the stream named by legacy_stream: when a shard has no checkpoint under its own key, the bare row’s checkpoint is used and copied under the new key. For any other stream the bare rows are ignored. With no stream named, a shard whose bare row holds a checkpoint is refused with an error naming the table, the shard and the stream that asked, and it is not read until the operator names the stream or deletes the row.

The two versions lease different rows, so an instance of each can read the same shard at once: stop the old instances before starting the new ones, or accept the duplicates.

Implementations§

Source§

impl DynamoLeaseStore

Source

pub fn new(config: &SdkConfig, table: impl Into<String>) -> Self

Uses table in the account and region of config.

§Examples
use std::error::Error;
use std::sync::Arc;

use aws_config::BehaviorVersion;
use ruststream_kinesis::prelude::*;

pub async fn run() -> Result<(), Box<dyn Error>> {
    let config = aws_config::defaults(BehaviorVersion::latest()).load().await;
    let leases = Arc::new(DynamoLeaseStore::new(&config, "orders-leases"));
    let broker = KinesisBroker::from_config(config).lease_store(leases);

    RustStream::new(AppInfo::new("orders", "0.1.0"))
        .with_broker(broker, |b| {
            b.include(handle);
        })
        .run()
        .await?;
    Ok(())
}
Source

pub fn legacy_stream(self, stream: impl Into<String>) -> Self

Names the stream the table’s rows keyed by a bare shard id belong to: the rows an earlier version of this crate wrote, before a row was kept per stream and shard.

A shard of stream with no checkpoint under its own key resumes from its bare row, and the first read copies that checkpoint under the new key. Other streams ignore the bare rows. Once the upgraded service has started a subscription on every stream it consumes, every bare row that held progress has its copy, and the bare rows and this setting can go.

A table that served more than one stream before the upgrade holds, in each bare row, whichever stream checkpointed last: no stream can resume from it safely. Delete those rows and open each stream at a chosen position instead.

§Examples
use std::error::Error;
use std::sync::Arc;

use aws_config::BehaviorVersion;
use ruststream_kinesis::prelude::*;

pub async fn run() -> Result<(), Box<dyn Error>> {
    let config = aws_config::defaults(BehaviorVersion::latest()).load().await;
    // The table was written by an earlier version for the `orders` stream only.
    let leases =
        Arc::new(DynamoLeaseStore::new(&config, "orders-leases").legacy_stream("orders"));
    let broker = KinesisBroker::from_config(config).lease_store(leases);

    RustStream::new(AppInfo::new("orders", "0.1.0"))
        .with_broker(broker, |b| {
            b.include(handle);
        })
        .run()
        .await?;
    Ok(())
}

Trait Implementations§

Source§

impl Clone for DynamoLeaseStore

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for DynamoLeaseStore

Source§

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

Formats the value using the given formatter. Read more
Source§

impl LeaseStore for DynamoLeaseStore

Source§

fn acquire<'a>( &'a self, key: &'a LeaseKey, owner: &'a str, ttl: Duration, ) -> BoxFuture<'a, Result<bool, LeaseError>>

Attempts to take the shard’s lease for owner, valid for ttl. Returns false when another live owner holds it.
Source§

fn renew<'a>( &'a self, key: &'a LeaseKey, owner: &'a str, ttl: Duration, ) -> BoxFuture<'a, Result<bool, LeaseError>>

Heartbeats the lease. Returns false when the lease is no longer held (fenced).
Source§

fn checkpoint<'a>( &'a self, key: &'a LeaseKey, owner: &'a str, sequence: &'a str, ) -> BoxFuture<'a, Result<bool, LeaseError>>

Records sequence as the shard’s checkpoint, conditional on owner holding the lease. Returns false when fenced.
Source§

fn read<'a>( &'a self, key: &'a LeaseKey, ) -> BoxFuture<'a, Result<LeaseState, LeaseError>>

Reads the shard’s persisted state (regardless of ownership).
Source§

fn release<'a>( &'a self, key: &'a LeaseKey, owner: &'a str, ) -> BoxFuture<'a, Result<(), LeaseError>>

Releases the lease so another owner can take it without waiting for expiry.

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, dest: *mut u8)

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

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> PublishedThrough<DefaultSlot> for T

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more