pub struct Transactor {
cads_token: u64,
shard_id: ShardId,
client: PersistClient,
since: SinceHandle<MaelstromKey, MaelstromVal, u64, i64, u64>,
write: WriteHandle<MaelstromKey, MaelstromVal, u64, i64>,
read_ts: u64,
long_lived_updates: Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>,
long_lived_listen: Listen<MaelstromKey, MaelstromVal, u64, i64>,
}
Expand description
An implementation of read-write transactions on top of persist
This executes Maelstrom txn-list-append transactions. The timestamp selection is modeled after Materialize’s SQL implementation.
A persist shard is maintained that directly represents a key-value map as it evolves over time. (Our real SQL implementation would likely instead maintain a WAL of updates to multiple tables.) Each transaction has associated timestamps:
read_ts
: at which the contents of the map are read (inclusive)write_ts
: at which txn mutations (if any) are writtenexpected_upper
: the upper bound of the previous txnnew_upper
: the upper bound after this transaction commits, if it does
To keep things simple, write_ts
is always read_ts+1
, expected_upper
is
antichain[write_ts]
, and new_upper
is antichain[write_ts+1]
.
Transactions are “committed” by using [WriteHandle::compare_and_append]
,
which atomically:
- verifies that
expected_upper
matches - writes the updates
- advances the upper to
new_upper
.
This guarantees that all transactions are linearized with each txn’s
read_ts
equal to the previous txn’s write_ts
. If two transactions race
by reading at the same read_ts
and writing at the same write_ts
, the
compare_and_append
guarantees that only one of them succeeds and the other
must retry with new timestamps.
Upon first use, the persist shard is initialized by advancing the upper to
antichain[T::minimum() + 1]
. This allows the first txn to use 0 as the
read_ts
and 1 as the write_ts
.
Similarly modeling Materialize, the since of the shard is kept following the upper. To keep things simple, this is done by a fixed offset. This exercises persist compaction.
To ensure that both mz_persist_client::read::ReadHandle::snapshot and
mz_persist_client::read::ReadHandle::listen are exercised, when a txn
reads the state at read_ts
, it artificially picks an as_of
timestamp in
[since, read_ts]
and splits up the read data between snapshot and listen
along this timestamp.
Fields§
§cads_token: u64
§shard_id: ShardId
§client: PersistClient
§since: SinceHandle<MaelstromKey, MaelstromVal, u64, i64, u64>
§write: WriteHandle<MaelstromKey, MaelstromVal, u64, i64>
§read_ts: u64
§long_lived_updates: Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>
§long_lived_listen: Listen<MaelstromKey, MaelstromVal, u64, i64>
Implementations§
Source§impl Transactor
impl Transactor
pub async fn new( client: &PersistClient, node_id: NodeId, shard_id: ShardId, ) -> Result<Self, MaelstromError>
Sourceasync fn maybe_init_shard(
write: &mut WriteHandle<MaelstromKey, MaelstromVal, u64, i64>,
) -> Result<u64, MaelstromError>
async fn maybe_init_shard( write: &mut WriteHandle<MaelstromKey, MaelstromVal, u64, i64>, ) -> Result<u64, MaelstromError>
Initializes the shard, if it hasn’t been already, and returns the read timestamp.
pub async fn transact( &mut self, req_ops: &[ReqTxnOp], ) -> Result<Vec<ResTxnOp>, MaelstromError>
async fn read_short_lived( &mut self, ) -> Result<(Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>, Antichain<u64>), MaelstromError>
async fn read( &mut self, ) -> Result<BTreeMap<MaelstromKey, MaelstromVal>, MaelstromError>
async fn listen_through( listen: Listen<MaelstromKey, MaelstromVal, u64, i64>, frontier: &Antichain<u64>, ) -> Result<Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>, ExternalError>
async fn read_long_lived( &mut self, as_of: &Antichain<u64>, ) -> Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>
fn extract_state_map( read_ts: u64, updates: Vec<((Result<MaelstromKey, String>, Result<MaelstromVal, String>), u64, i64)>, ) -> Result<BTreeMap<MaelstromKey, MaelstromVal>, MaelstromError>
fn eval_txn( state: &BTreeMap<MaelstromKey, MaelstromVal>, req_ops: &[ReqTxnOp], ) -> (Vec<(MaelstromKey, MaelstromVal, i64)>, Vec<ResTxnOp>)
async fn advance_since(&mut self) -> Result<(), MaelstromError>
fn extract_ts<T: TotalOrder + Copy>( frontier: &Antichain<T>, ) -> Result<T, MaelstromError>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Transactor
impl !RefUnwindSafe for Transactor
impl Send for Transactor
impl Sync for Transactor
impl Unpin for Transactor
impl !UnwindSafe for Transactor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T, U> OverrideFrom<Option<&T>> for Uwhere
U: OverrideFrom<T>,
impl<T, U> OverrideFrom<Option<&T>> for Uwhere
U: OverrideFrom<T>,
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
impl<P, R> ProtoType<R> for Pwhere
R: RustType<P>,
Source§fn into_rust(self) -> Result<R, TryFromProtoError>
fn into_rust(self) -> Result<R, TryFromProtoError>
RustType::from_proto
.Source§fn from_rust(rust: &R) -> P
fn from_rust(rust: &R) -> P
RustType::into_proto
.Source§impl<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
impl<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
Source§fn plus_equals(&mut self, rhs: &&'a S)
fn plus_equals(&mut self, rhs: &&'a S)
std::ops::AddAssign
, for types that do not implement AddAssign
.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.