use std::future::IntoFuture;
use crate::context::DurableContext;
use crate::engine::OperationId;
use crate::error::OperationError;
use crate::future::{DurableFuture, PendingClaim};
struct Claimed {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct TryJoinAllBuilder<O> {
ctx: DurableContext,
name: Option<String>,
futures: Vec<DurableFuture<O>>,
}
impl<O> std::fmt::Debug for TryJoinAllBuilder<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TryJoinAllBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> TryJoinAllBuilder<O> {
pub(crate) fn new_internal(ctx: DurableContext, futures: Vec<DurableFuture<O>>) -> Self {
Self {
ctx,
name: None,
futures,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn future(self) -> DurableFuture<Vec<O>> {
self.into_future()
}
pub fn spawn(self) -> DurableFuture<Vec<O>> {
spawn_terminal!(self)
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> IntoFuture
for TryJoinAllBuilder<O>
{
type Output = Result<Vec<O>, OperationError>;
type IntoFuture = DurableFuture<Vec<O>>;
fn into_future(self) -> Self::IntoFuture {
use crate::combinator::TryJoinAllExecution;
let Self { ctx, name, futures } = self;
let context = crate::context::display_prefix(ctx.prefix()).to_owned();
DurableFuture::deferred(PendingClaim::new("try_join_all", context, move || {
let claimed = Claimed {
op_id: ctx.claim_operation_id("try_join_all"),
ctx,
name,
};
preflight_identity!(claimed, "Context", crate::combinator::COMBINATOR_SUB_TYPE);
let gate = claimed.ctx.clone();
let execution = TryJoinAllExecution {
ctx: claimed.ctx,
op_id: claimed.op_id,
name: claimed.name,
futures,
};
DurableFuture::for_operation(&gate, execution.execute())
}))
}
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct JoinAllBuilder<O> {
ctx: DurableContext,
name: Option<String>,
futures: Vec<DurableFuture<O>>,
}
impl<O> std::fmt::Debug for JoinAllBuilder<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JoinAllBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> JoinAllBuilder<O> {
pub(crate) fn new_internal(ctx: DurableContext, futures: Vec<DurableFuture<O>>) -> Self {
Self {
ctx,
name: None,
futures,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn future(self) -> DurableFuture<Vec<Result<O, OperationError>>> {
self.into_future()
}
pub fn spawn(self) -> DurableFuture<Vec<Result<O, OperationError>>> {
spawn_terminal!(self)
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> IntoFuture
for JoinAllBuilder<O>
{
type Output = Result<Vec<Result<O, OperationError>>, OperationError>;
type IntoFuture = DurableFuture<Vec<Result<O, OperationError>>>;
fn into_future(self) -> Self::IntoFuture {
use crate::combinator::JoinAllExecution;
let Self { ctx, name, futures } = self;
let context = crate::context::display_prefix(ctx.prefix()).to_owned();
DurableFuture::deferred(PendingClaim::new("join_all", context, move || {
let claimed = Claimed {
op_id: ctx.claim_operation_id("join_all"),
ctx,
name,
};
preflight_identity!(claimed, "Context", crate::combinator::COMBINATOR_SUB_TYPE);
let gate = claimed.ctx.clone();
let execution = JoinAllExecution {
ctx: claimed.ctx,
op_id: claimed.op_id,
name: claimed.name,
futures,
};
DurableFuture::for_operation(&gate, execution.execute())
}))
}
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct SelectOkBuilder<O> {
ctx: DurableContext,
name: Option<String>,
futures: Vec<DurableFuture<O>>,
}
impl<O> std::fmt::Debug for SelectOkBuilder<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SelectOkBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> SelectOkBuilder<O> {
pub(crate) fn new_internal(ctx: DurableContext, futures: Vec<DurableFuture<O>>) -> Self {
Self {
ctx,
name: None,
futures,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn future(self) -> DurableFuture<O> {
self.into_future()
}
pub fn spawn(self) -> DurableFuture<O> {
spawn_terminal!(self)
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> IntoFuture
for SelectOkBuilder<O>
{
type Output = Result<O, OperationError>;
type IntoFuture = DurableFuture<O>;
fn into_future(self) -> Self::IntoFuture {
use crate::combinator::SelectOkExecution;
let Self { ctx, name, futures } = self;
let context = crate::context::display_prefix(ctx.prefix()).to_owned();
DurableFuture::deferred(PendingClaim::new("select_ok", context, move || {
let claimed = Claimed {
op_id: ctx.claim_operation_id("select_ok"),
ctx,
name,
};
preflight_identity!(claimed, "Context", crate::combinator::COMBINATOR_SUB_TYPE);
let gate = claimed.ctx.clone();
let execution = SelectOkExecution {
ctx: claimed.ctx,
op_id: claimed.op_id,
name: claimed.name,
futures,
};
DurableFuture::for_operation(&gate, execution.execute())
}))
}
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct RaceBuilder<O> {
ctx: DurableContext,
name: Option<String>,
futures: Vec<DurableFuture<O>>,
}
impl<O> std::fmt::Debug for RaceBuilder<O> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RaceBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> RaceBuilder<O> {
pub(crate) fn new_internal(ctx: DurableContext, futures: Vec<DurableFuture<O>>) -> Self {
Self {
ctx,
name: None,
futures,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn future(self) -> DurableFuture<O> {
self.into_future()
}
pub fn spawn(self) -> DurableFuture<O> {
spawn_terminal!(self)
}
}
impl<O: serde::Serialize + serde::de::DeserializeOwned + Send + 'static> IntoFuture
for RaceBuilder<O>
{
type Output = Result<O, OperationError>;
type IntoFuture = DurableFuture<O>;
fn into_future(self) -> Self::IntoFuture {
use crate::combinator::RaceExecution;
let Self { ctx, name, futures } = self;
let context = crate::context::display_prefix(ctx.prefix()).to_owned();
DurableFuture::deferred(PendingClaim::new("race", context, move || {
let claimed = Claimed {
op_id: ctx.claim_operation_id("race"),
ctx,
name,
};
preflight_identity!(claimed, "Context", crate::combinator::COMBINATOR_SUB_TYPE);
let gate = claimed.ctx.clone();
let execution = RaceExecution {
ctx: claimed.ctx,
op_id: claimed.op_id,
name: claimed.name,
futures,
};
DurableFuture::for_operation(&gate, execution.execute())
}))
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::DurableFuture;
use crate::context::DurableContext;
use crate::engine::{CheckpointLog, CheckpointRecord, CheckpointStatus};
#[test]
fn dropped_combinator_builder_claims_nothing() {
let ctx = DurableContext::new_root(
"arn:aws:lambda:us-east-1:1:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
);
drop(ctx.try_join_all(Vec::<DurableFuture<i32>>::new()));
drop(ctx.join_all(Vec::<DurableFuture<i32>>::new()).future());
drop(
ctx.select_ok(Vec::<DurableFuture<i32>>::new())
.name("s")
.future(),
);
drop(ctx.race(Vec::<DurableFuture<i32>>::new()));
assert_eq!(ctx.mint_id().positional(), "1", "the counter is unchanged");
}
fn ctx_with_step_at_1() -> DurableContext {
let wire_id = crate::engine::compute_wire_id_public("1");
let record = CheckpointRecord {
id: wire_id.clone(),
status: CheckpointStatus::Succeeded,
result: Some("1".to_owned()),
error_type: None,
error_message: None,
error_data: None,
stack_trace: None,
attempt: 0,
invoke_result: None,
invoke_error_type: None,
invoke_error_message: None,
invoke_error_data: None,
invoke_stack_trace: None,
replay_children: false,
callback_id: None,
op_type: Some("Step".to_owned()),
sub_type: Some("Step".to_owned()),
due_at: None,
op_name: None,
parent_id: None,
start_timestamp: None,
end_timestamp: None,
};
DurableContext::new_root(
"arn:aws:lambda:us-east-1:1:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::from_records(vec![(wire_id, record)])),
)
}
fn assert_mismatch_recorded(method: &str, ctx: &DurableContext) {
assert!(
ctx.coordinator().fatal_error().is_some(),
"{method}: the claim records the mismatch before any poll"
);
}
#[test]
fn claim_checks_identity_before_poll() {
let empty = Vec::<DurableFuture<i32>>::new;
let ctx = ctx_with_step_at_1();
ctx.try_join_all(empty()).future().claim_now();
assert_mismatch_recorded("try_join_all", &ctx);
let ctx = ctx_with_step_at_1();
ctx.join_all(empty()).future().claim_now();
assert_mismatch_recorded("join_all", &ctx);
let ctx = ctx_with_step_at_1();
ctx.select_ok(empty()).future().claim_now();
assert_mismatch_recorded("select_ok", &ctx);
let ctx = ctx_with_step_at_1();
ctx.race(empty()).future().claim_now();
assert_mismatch_recorded("race", &ctx);
}
}