use std::future::{Future, IntoFuture};
use std::marker::PhantomData;
use std::sync::Arc;
use crate::BoxError;
use crate::Serdes;
use crate::context::DurableContext;
use crate::engine::OperationId;
use crate::error::OperationError;
use crate::future::DurableFuture;
use crate::map_parallel::ItemEncoding;
use crate::serdes::{JsonSerdes, PassthroughSerdes};
pub use crate::map_parallel::{
BatchError, BatchItem, BatchItemStatus, BatchResult, BatchStats, BatchStatus,
CompletionDecision, CompletionItem, CompletionOutcome, CompletionReason, NestingMode,
};
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct MapBuilder<I, O, F, Fut, IS = JsonSerdes, RS = PassthroughSerdes> {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
max_concurrency: Option<usize>,
completion: Option<CompletionConfig>,
serdes: IS,
encoding: ItemEncoding,
result_serdes: RS,
nesting: NestingMode,
item_namer: Option<crate::map_parallel::ItemNamer<I>>,
summary: Option<crate::map_parallel::BatchSummary<O>>,
items: Vec<I>,
closure: F,
_marker: PhantomData<fn() -> (O, Fut)>,
}
impl<I, O, F, Fut, IS, RS> std::fmt::Debug for MapBuilder<I, O, F, Fut, IS, RS> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MapBuilder")
.field("name", &self.name)
.field("max_concurrency", &self.max_concurrency)
.finish_non_exhaustive()
}
}
impl<I, O, F, Fut> MapBuilder<I, O, F, Fut>
where
I: Send + 'static,
O: Send + 'static,
F: Fn(DurableContext, I, usize) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
pub(crate) fn new_internal(
ctx: DurableContext,
op_id: OperationId,
items: Vec<I>,
closure: F,
) -> Self {
Self {
ctx,
op_id,
name: None,
max_concurrency: None,
completion: None,
serdes: JsonSerdes,
encoding: ItemEncoding::AllJson,
result_serdes: PassthroughSerdes,
nesting: NestingMode::Normal,
item_namer: None,
summary: None,
items,
closure,
_marker: PhantomData,
}
}
}
impl<I, O, F, Fut, IS, RS> MapBuilder<I, O, F, Fut, IS, RS>
where
I: Send + 'static,
O: Send + 'static,
F: Fn(DurableContext, I, usize) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
{
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn max_concurrency(mut self, max: usize) -> Self {
self.max_concurrency = Some(max);
self
}
pub fn completion(mut self, config: CompletionConfig) -> Self {
self.completion = Some(config);
self
}
pub fn serdes<IS2>(self, serdes: IS2) -> MapBuilder<I, O, F, Fut, IS2, RS>
where
IS2: Serdes<O>,
{
MapBuilder {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes,
encoding: ItemEncoding::AllSerdes,
result_serdes: self.result_serdes,
nesting: self.nesting,
item_namer: self.item_namer,
summary: self.summary,
items: self.items,
closure: self.closure,
_marker: PhantomData,
}
}
pub fn result_serdes<RS2>(self, serdes: RS2) -> MapBuilder<I, O, F, Fut, IS, RS2>
where
RS2: Serdes<String>,
{
MapBuilder {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: serdes,
nesting: self.nesting,
item_namer: self.item_namer,
summary: self.summary,
items: self.items,
closure: self.closure,
_marker: PhantomData,
}
}
pub fn nesting(mut self, mode: NestingMode) -> Self {
self.nesting = mode;
self
}
pub fn item_namer(
mut self,
namer: impl Fn(&I, usize) -> String + Send + Sync + 'static,
) -> Self {
self.item_namer = Some(Box::new(namer));
self
}
pub fn summary(
mut self,
summary: impl Fn(&BatchResult<O>) -> String + Send + Sync + 'static,
) -> Self {
self.summary = Some(Arc::new(summary));
self
}
pub async fn await_batch(self) -> Result<BatchResult<O>, OperationError>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future().await
}
pub fn spawn_batch(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
spawn_terminal!(self, started_batch)
}
fn batch_future(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future_with(false)
}
fn started_batch_future(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future_with(true)
}
fn batch_future_with(self, started: bool) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
use crate::map_parallel::MapExecution;
preflight_identity!(self, "Context", crate::map_parallel::MAP_SUB_TYPE);
let execution = MapExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: self.result_serdes,
nesting: self.nesting,
item_namer: self.item_namer,
summary: self.summary,
items: self.items,
closure: Arc::new(self.closure),
_marker: PhantomData,
};
if started {
DurableFuture::for_started_operation(
&execution.ctx.clone(),
execution.execute_batch_result_started(),
)
} else {
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
execution.execute_batch_result_started()
})
}
}
pub fn future(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.into_future()
}
pub fn spawn(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
spawn_terminal!(self, started)
}
fn started_future(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.started_batch_future()
.map_output(|outcome| crate::map_parallel::collect_successful(outcome?))
}
}
impl<I, O, F, Fut, IS, RS> IntoFuture for MapBuilder<I, O, F, Fut, IS, RS>
where
I: Send + 'static,
O: Send + 'static,
F: Fn(DurableContext, I, usize) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
IS: Serdes<O>,
RS: Serdes<String>,
{
type Output = Result<Vec<O>, OperationError>;
type IntoFuture = DurableFuture<Vec<O>>;
fn into_future(self) -> Self::IntoFuture {
use crate::map_parallel::MapExecution;
preflight_identity!(self, "Context", crate::map_parallel::MAP_SUB_TYPE);
let execution = MapExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: self.result_serdes,
nesting: self.nesting,
item_namer: self.item_namer,
summary: self.summary,
items: self.items,
closure: Arc::new(self.closure),
_marker: PhantomData,
};
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
let batch = execution.execute_batch_result_started();
async move { crate::map_parallel::collect_successful(batch.await?) }
})
}
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct ParallelBuilder<O, IS = JsonSerdes, RS = PassthroughSerdes> {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
max_concurrency: Option<usize>,
completion: Option<CompletionConfig>,
serdes: IS,
encoding: ItemEncoding,
result_serdes: RS,
nesting: NestingMode,
branches: Vec<(Option<String>, crate::future::BranchBody<O>)>,
summary: Option<crate::map_parallel::BatchSummary<O>>,
_marker: PhantomData<O>,
}
impl<O, IS, RS> std::fmt::Debug for ParallelBuilder<O, IS, RS> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParallelBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl<O: Send + 'static> ParallelBuilder<O> {
pub(crate) fn new_internal(
ctx: DurableContext,
op_id: OperationId,
branches: Vec<(Option<String>, crate::future::BranchBody<O>)>,
) -> Self {
Self {
ctx,
op_id,
name: None,
max_concurrency: None,
completion: None,
serdes: JsonSerdes,
encoding: ItemEncoding::AllJson,
result_serdes: PassthroughSerdes,
nesting: NestingMode::Normal,
branches,
summary: None,
_marker: PhantomData,
}
}
}
impl<O: Send + 'static, IS, RS> ParallelBuilder<O, IS, RS> {
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn max_concurrency(mut self, max: usize) -> Self {
self.max_concurrency = Some(max);
self
}
pub fn completion(mut self, config: CompletionConfig) -> Self {
self.completion = Some(config);
self
}
pub fn serdes<IS2>(self, serdes: IS2) -> ParallelBuilder<O, IS2, RS>
where
IS2: Serdes<O>,
{
ParallelBuilder {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes,
encoding: ItemEncoding::AllSerdes,
result_serdes: self.result_serdes,
nesting: self.nesting,
branches: self.branches,
summary: self.summary,
_marker: PhantomData,
}
}
pub fn result_serdes<RS2>(self, serdes: RS2) -> ParallelBuilder<O, IS, RS2>
where
RS2: Serdes<String>,
{
ParallelBuilder {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: serdes,
nesting: self.nesting,
branches: self.branches,
summary: self.summary,
_marker: PhantomData,
}
}
pub fn nesting(mut self, mode: NestingMode) -> Self {
self.nesting = mode;
self
}
pub fn summary(
mut self,
summary: impl Fn(&BatchResult<O>) -> String + Send + Sync + 'static,
) -> Self {
self.summary = Some(Arc::new(summary));
self
}
pub async fn await_batch(self) -> Result<BatchResult<O>, OperationError>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future().await
}
pub fn spawn_batch(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
spawn_terminal!(self, started_batch)
}
fn batch_future(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future_with(false)
}
fn started_batch_future(self) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.batch_future_with(true)
}
fn batch_future_with(self, started: bool) -> DurableFuture<BatchResult<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
use crate::map_parallel::{ItemIds, ParallelExecution};
preflight_identity!(self, "Context", crate::map_parallel::PARALLEL_SUB_TYPE);
let item_ids = ItemIds::new(&self.op_id, self.branches.len());
let execution = ParallelExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: self.result_serdes,
nesting: self.nesting,
branches: self.branches,
summary: self.summary,
item_ids,
};
if started {
DurableFuture::for_started_operation(
&execution.ctx.clone(),
execution.execute_batch_result_started(),
)
} else {
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
execution.execute_batch_result_started()
})
}
}
pub fn future(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.into_future()
}
pub fn spawn(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
spawn_terminal!(self, started)
}
fn started_future(self) -> DurableFuture<Vec<O>>
where
IS: Serdes<O>,
RS: Serdes<String>,
{
self.started_batch_future()
.map_output(|outcome| crate::map_parallel::collect_successful(outcome?))
}
}
impl<O, IS, RS> IntoFuture for ParallelBuilder<O, IS, RS>
where
O: Send + 'static,
IS: Serdes<O>,
RS: Serdes<String>,
{
type Output = Result<Vec<O>, OperationError>;
type IntoFuture = DurableFuture<Vec<O>>;
fn into_future(self) -> Self::IntoFuture {
use crate::map_parallel::{ItemIds, ParallelExecution};
preflight_identity!(self, "Context", crate::map_parallel::PARALLEL_SUB_TYPE);
let item_ids = ItemIds::new(&self.op_id, self.branches.len());
let execution = ParallelExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes: self.serdes,
encoding: self.encoding,
result_serdes: self.result_serdes,
nesting: self.nesting,
branches: self.branches,
summary: self.summary,
item_ids,
};
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
let batch = execution.execute_batch_result_started();
async move { crate::map_parallel::collect_successful(batch.await?) }
})
}
}
#[must_use = "builders do nothing unless awaited or spawned"]
#[non_exhaustive]
pub struct ParallelMixedBuilder<RS = PassthroughSerdes> {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
max_concurrency: Option<usize>,
completion: Option<CompletionConfig>,
result_serdes: RS,
nesting: NestingMode,
branches: Vec<(
Option<String>,
crate::future::BranchBody<crate::future::BranchValue>,
)>,
decoders: Arc<Vec<Arc<dyn crate::future::BranchDecoder>>>,
json: Arc<[bool]>,
summary: Option<crate::map_parallel::BatchSummary<crate::future::BranchValue>>,
}
impl<RS> std::fmt::Debug for ParallelMixedBuilder<RS> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParallelMixedBuilder")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl ParallelMixedBuilder {
pub(crate) fn new_internal(
ctx: DurableContext,
op_id: OperationId,
branches: crate::future::BranchSet,
) -> Self {
let (branches, decoders, json) = branches.into_parts();
Self {
ctx,
op_id,
name: None,
max_concurrency: None,
completion: None,
result_serdes: PassthroughSerdes,
nesting: NestingMode::Normal,
branches,
decoders,
json,
summary: None,
}
}
}
impl<RS> ParallelMixedBuilder<RS> {
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn max_concurrency(mut self, max: usize) -> Self {
self.max_concurrency = Some(max);
self
}
pub fn completion(mut self, config: CompletionConfig) -> Self {
self.completion = Some(config);
self
}
pub fn result_serdes<RS2>(self, serdes: RS2) -> ParallelMixedBuilder<RS2>
where
RS2: Serdes<String>,
{
ParallelMixedBuilder {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
result_serdes: serdes,
nesting: self.nesting,
branches: self.branches,
decoders: self.decoders,
json: self.json,
summary: self.summary,
}
}
pub fn nesting(mut self, mode: NestingMode) -> Self {
self.nesting = mode;
self
}
pub fn summary(
mut self,
summary: impl Fn(&BatchResult<crate::future::BranchValue>) -> String + Send + Sync + 'static,
) -> Self {
self.summary = Some(Arc::new(summary));
self
}
pub fn spawn(self) -> DurableFuture<CompletionReason>
where
RS: Serdes<String>,
{
use crate::map_parallel::publish_outcome;
let decoders = Arc::clone(&self.decoders);
let spawned = spawn_terminal!(self, started_batch);
spawned.map_output(move |outcome| publish_outcome(outcome, &decoders))
}
fn batch_future(self) -> DurableFuture<BatchResult<crate::future::BranchValue>>
where
RS: Serdes<String>,
{
self.batch_future_with(false)
}
fn started_batch_future(self) -> DurableFuture<BatchResult<crate::future::BranchValue>>
where
RS: Serdes<String>,
{
self.batch_future_with(true)
}
fn batch_future_with(
self,
started: bool,
) -> DurableFuture<BatchResult<crate::future::BranchValue>>
where
RS: Serdes<String>,
{
use crate::map_parallel::{ItemIds, MixedItemSerdes, PARALLEL_SUB_TYPE, ParallelExecution};
preflight_identity!(self, "Context", PARALLEL_SUB_TYPE);
let item_ids = ItemIds::new(&self.op_id, self.branches.len());
let serdes = MixedItemSerdes::new(&item_ids, self.decoders);
let execution = ParallelExecution {
ctx: self.ctx,
op_id: self.op_id,
name: self.name,
max_concurrency: self.max_concurrency,
completion: self.completion,
serdes,
encoding: ItemEncoding::PerItem(self.json),
result_serdes: self.result_serdes,
nesting: self.nesting,
branches: self.branches,
summary: self.summary,
item_ids,
};
if started {
DurableFuture::for_started_operation(
&execution.ctx.clone(),
execution.execute_batch_result_started(),
)
} else {
DurableFuture::for_start_step(&execution.ctx.clone(), move || {
execution.execute_batch_result_started()
})
}
}
}
impl<RS> IntoFuture for ParallelMixedBuilder<RS>
where
RS: Serdes<String>,
{
type Output = Result<CompletionReason, OperationError>;
type IntoFuture = DurableFuture<CompletionReason>;
fn into_future(self) -> Self::IntoFuture {
use crate::map_parallel::publish_outcome;
let decoders = Arc::clone(&self.decoders);
let batch = self.batch_future();
batch.map_output(move |outcome| publish_outcome(outcome, &decoders))
}
}
pub(crate) type CompletionPredicate =
Arc<dyn Fn(&BatchStats<'_>) -> CompletionDecision + Send + Sync>;
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct CompletionConfig {
min_successful: Option<usize>,
tolerated_failure_count: Option<usize>,
tolerated_failure_percentage: Option<f64>,
completion_predicate: Option<CompletionPredicate>,
}
impl std::fmt::Debug for CompletionConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompletionConfig")
.field("min_successful", &self.min_successful)
.field("tolerated_failure_count", &self.tolerated_failure_count)
.field(
"tolerated_failure_percentage",
&self.tolerated_failure_percentage,
)
.field(
"completion_predicate",
&self.completion_predicate.as_ref().map(|_| "<closure>"),
)
.finish()
}
}
impl CompletionConfig {
pub fn builder() -> CompletionConfigBuilder {
CompletionConfigBuilder::default()
}
#[must_use]
pub fn with_min_successful(min: usize) -> Self {
Self {
min_successful: Some(min),
..Self::default()
}
}
#[must_use]
pub fn with_tolerated_failure_count(count: usize) -> Self {
Self {
tolerated_failure_count: Some(count),
..Self::default()
}
}
pub fn with_tolerated_failure_percentage(
pct: f64,
) -> Result<Self, CompletionConfigValidationError> {
check_percentage(Some(pct))?;
Ok(Self {
tolerated_failure_percentage: Some(pct),
..Self::default()
})
}
#[must_use]
pub fn with_completion_predicate<F>(predicate: F) -> Self
where
F: Fn(&BatchStats<'_>) -> CompletionDecision + Send + Sync + 'static,
{
Self {
completion_predicate: Some(Arc::new(predicate)),
..Self::default()
}
}
pub fn min_successful(&self) -> Option<usize> {
self.min_successful
}
pub fn tolerated_failure_count(&self) -> Option<usize> {
self.tolerated_failure_count
}
pub fn tolerated_failure_percentage(&self) -> Option<f64> {
self.tolerated_failure_percentage
}
pub fn has_completion_predicate(&self) -> bool {
self.completion_predicate.is_some()
}
pub(crate) fn has_criteria(&self) -> bool {
self.min_successful.is_some()
|| self.tolerated_failure_count.is_some()
|| self.tolerated_failure_percentage.is_some()
|| self.completion_predicate.is_some()
}
pub(crate) fn predicate_decision(&self, stats: &BatchStats<'_>) -> CompletionDecision {
self.completion_predicate
.as_ref()
.map_or(CompletionDecision::Continue, |predicate| predicate(stats))
}
}
fn check_percentage(pct: Option<f64>) -> Result<(), CompletionConfigValidationError> {
match pct {
Some(pct) if !(pct.is_finite() && (0.0..=100.0).contains(&pct)) => {
Err(CompletionConfigValidationError {
message: format!("tolerated_failure_percentage must be 0-100, got {pct}"),
})
}
_ => Ok(()),
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CompletionConfigValidationError {
message: String,
}
impl std::fmt::Display for CompletionConfigValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid CompletionConfig: {}", self.message)
}
}
impl std::error::Error for CompletionConfigValidationError {}
#[derive(Clone, Default)]
#[must_use = "builders do nothing unless .build() is called"]
#[non_exhaustive]
pub struct CompletionConfigBuilder {
min_successful: Option<usize>,
tolerated_failure_count: Option<usize>,
tolerated_failure_percentage: Option<f64>,
completion_predicate: Option<CompletionPredicate>,
}
impl std::fmt::Debug for CompletionConfigBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompletionConfigBuilder")
.field("min_successful", &self.min_successful)
.field("tolerated_failure_count", &self.tolerated_failure_count)
.field(
"tolerated_failure_percentage",
&self.tolerated_failure_percentage,
)
.field(
"completion_predicate",
&self.completion_predicate.as_ref().map(|_| "<closure>"),
)
.finish()
}
}
impl CompletionConfigBuilder {
pub fn min_successful(mut self, min: usize) -> Self {
self.min_successful = Some(min);
self
}
pub fn tolerated_failure_count(mut self, count: usize) -> Self {
self.tolerated_failure_count = Some(count);
self
}
pub fn tolerated_failure_percentage(mut self, pct: f64) -> Self {
self.tolerated_failure_percentage = Some(pct);
self
}
pub fn completion_predicate<F>(mut self, predicate: F) -> Self
where
F: Fn(&BatchStats<'_>) -> CompletionDecision + Send + Sync + 'static,
{
self.completion_predicate = Some(Arc::new(predicate));
self
}
pub fn build(self) -> Result<CompletionConfig, CompletionConfigValidationError> {
check_percentage(self.tolerated_failure_percentage)?;
if self.completion_predicate.is_some()
&& (self.min_successful.is_some()
|| self.tolerated_failure_count.is_some()
|| self.tolerated_failure_percentage.is_some())
{
return Err(CompletionConfigValidationError {
message: "completion_predicate is mutually exclusive with min_successful, \
tolerated_failure_count and tolerated_failure_percentage"
.to_owned(),
});
}
Ok(CompletionConfig {
min_successful: self.min_successful,
tolerated_failure_count: self.tolerated_failure_count,
tolerated_failure_percentage: self.tolerated_failure_percentage,
completion_predicate: self.completion_predicate,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn completion_config_builder_combines_thresholds() {
let combined = CompletionConfig::builder()
.min_successful(2)
.tolerated_failure_count(1)
.tolerated_failure_percentage(25.0)
.build()
.expect("valid config");
assert_eq!(combined.min_successful(), Some(2));
assert_eq!(combined.tolerated_failure_count(), Some(1));
assert_eq!(combined.tolerated_failure_percentage(), Some(25.0));
let only_min = CompletionConfig::builder()
.min_successful(3)
.build()
.expect("valid config");
assert_eq!(only_min.min_successful(), Some(3));
assert_eq!(only_min.tolerated_failure_count(), None);
assert_eq!(only_min.tolerated_failure_percentage(), None);
assert!(!only_min.has_completion_predicate());
assert_eq!(
CompletionConfig::builder()
.tolerated_failure_count(0)
.build()
.expect("valid config")
.tolerated_failure_count(),
CompletionConfig::with_tolerated_failure_count(0).tolerated_failure_count(),
);
let default = CompletionConfig::default();
assert_eq!(default.min_successful(), None);
assert_eq!(default.tolerated_failure_count(), None);
assert_eq!(default.tolerated_failure_percentage(), None);
assert!(!default.has_completion_predicate());
}
#[test]
fn percentage_is_checked_when_built() {
for pct in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1.0, 150.0] {
let expected = format!(
"invalid CompletionConfig: \
tolerated_failure_percentage must be 0-100, got {pct}"
);
let direct = CompletionConfig::with_tolerated_failure_percentage(pct)
.expect_err("the constructor rejects an invalid percentage");
assert_eq!(direct.to_string(), expected);
let built = CompletionConfig::builder()
.tolerated_failure_percentage(pct)
.build()
.expect_err("the builder rejects an invalid percentage");
assert_eq!(built.to_string(), expected);
}
for pct in [0.0, 12.5, 100.0] {
let direct = CompletionConfig::with_tolerated_failure_percentage(pct)
.expect("the constructor accepts a valid percentage");
assert_eq!(direct.tolerated_failure_percentage(), Some(pct));
let built = CompletionConfig::builder()
.tolerated_failure_percentage(pct)
.build()
.expect("the builder accepts a valid percentage");
assert_eq!(built.tolerated_failure_percentage(), Some(pct));
}
}
#[test]
fn completion_config_predicate_clone_and_debug() {
let config = CompletionConfig::builder()
.completion_predicate(|stats| {
if stats.settled() >= 3 {
CompletionDecision::complete(CompletionOutcome::Succeeded)
} else {
CompletionDecision::Continue
}
})
.build()
.expect("valid config");
assert!(config.has_completion_predicate());
let cloned = config.clone();
assert!(cloned.has_completion_predicate());
assert_eq!(cloned.min_successful(), None);
let debugged = format!("{config:?}");
assert!(
debugged.contains("completion_predicate") && debugged.contains("<closure>"),
"Debug should report predicate presence, got: {debugged}"
);
let only_predicate = CompletionConfig::with_completion_predicate(|stats| {
if stats.failed() > stats.succeeded() {
CompletionDecision::complete(CompletionOutcome::Succeeded)
} else {
CompletionDecision::Continue
}
});
assert!(only_predicate.has_completion_predicate());
assert_eq!(only_predicate.min_successful(), None);
let more_failed = [
CompletionItem::new(0, None, Some(BatchItemStatus::Failed)),
CompletionItem::new(1, None, Some(BatchItemStatus::Failed)),
CompletionItem::new(2, None, Some(BatchItemStatus::Succeeded)),
];
let stats = BatchStats::new(&more_failed);
assert_eq!(
only_predicate.predicate_decision(&stats),
CompletionDecision::complete(CompletionOutcome::Succeeded)
);
let even = [
CompletionItem::new(0, None, Some(BatchItemStatus::Failed)),
CompletionItem::new(1, None, Some(BatchItemStatus::Succeeded)),
];
assert_eq!(
only_predicate.predicate_decision(&BatchStats::new(&even)),
CompletionDecision::Continue
);
assert_eq!(
CompletionConfig::default().predicate_decision(&stats),
CompletionDecision::Continue
);
}
fn complete_on_first_settle(stats: &BatchStats<'_>) -> CompletionDecision {
if stats.settled() >= 1 {
CompletionDecision::complete(CompletionOutcome::Succeeded)
} else {
CompletionDecision::Continue
}
}
#[test]
fn build_rejects_predicate_with_min_successful() {
let err = CompletionConfig::builder()
.min_successful(1)
.completion_predicate(complete_on_first_settle)
.build()
.expect_err("a predicate with min_successful must be rejected");
assert!(
err.to_string().contains("mutually exclusive"),
"unexpected message: {err}"
);
assert_eq!(
err.to_string(),
"invalid CompletionConfig: completion_predicate is mutually exclusive with \
min_successful, tolerated_failure_count and tolerated_failure_percentage"
);
}
#[test]
fn build_rejects_predicate_with_failure_count() {
let result = CompletionConfig::builder()
.tolerated_failure_count(0)
.completion_predicate(complete_on_first_settle)
.build();
assert!(result.is_err());
}
#[test]
fn build_rejects_predicate_with_failure_percentage() {
let result = CompletionConfig::builder()
.tolerated_failure_percentage(10.0)
.completion_predicate(complete_on_first_settle)
.build();
assert!(result.is_err());
}
#[test]
fn build_accepts_predicate_alone() {
let config = CompletionConfig::builder()
.completion_predicate(complete_on_first_settle)
.build()
.expect("a predicate alone is valid");
assert!(config.has_completion_predicate());
}
#[test]
fn build_checks_percentage_before_predicate_exclusion() {
let err = CompletionConfig::builder()
.tolerated_failure_percentage(150.0)
.completion_predicate(complete_on_first_settle)
.build()
.expect_err("both rules are broken");
assert_eq!(
err.to_string(),
"invalid CompletionConfig: tolerated_failure_percentage must be 0-100, got 150"
);
}
fn ctx_with_step_record_at_1() -> DurableContext {
use crate::engine::{CheckpointLog, CheckpointRecord, CheckpointStatus};
let wire = crate::engine::compute_wire_id_public("1");
let record = CheckpointRecord {
id: wire.clone(),
status: CheckpointStatus::Succeeded,
result: Some("0".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:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::from_records(vec![(wire, record)])),
)
}
#[tokio::test]
async fn map_batch_future_checks_replay_identity_before_first_poll() {
let ctx = ctx_with_step_record_at_1();
let future = ctx
.map(
vec![1_i32],
|_child, item: i32, _idx| async move { Ok(item) },
)
.batch_future();
let fatal = ctx
.coordinator()
.fatal_error()
.expect("the mismatch is recorded before the first poll");
assert_eq!(fatal.error_type, "NonDeterministicExecutionError");
drop(future);
}
#[tokio::test]
async fn parallel_batch_future_checks_replay_identity_before_first_poll() {
let ctx = ctx_with_step_record_at_1();
let branches = vec![crate::future::Branch::new("b", |_child| async {
Ok(1_i32)
})];
let future = ctx.parallel(branches).batch_future();
let fatal = ctx
.coordinator()
.fatal_error()
.expect("the mismatch is recorded before the first poll");
assert_eq!(fatal.error_type, "NonDeterministicExecutionError");
drop(future);
}
fn one_branch_set() -> crate::BranchSet {
let mut set = crate::BranchSet::new();
let _handle = set.add("b", |_child| async { Ok(1_i32) });
set
}
#[tokio::test]
async fn parallel_mixed_into_future_checks_replay_identity_before_first_poll() {
let ctx = ctx_with_step_record_at_1();
let future = ctx.parallel_mixed(one_branch_set()).into_future();
let fatal = ctx
.coordinator()
.fatal_error()
.expect("the mismatch is recorded before the first poll");
assert_eq!(fatal.error_type, "NonDeterministicExecutionError");
drop(future);
}
#[tokio::test]
async fn parallel_mixed_spawn_checks_replay_identity_before_first_poll() {
let ctx = ctx_with_step_record_at_1();
let future = ctx.parallel_mixed(one_branch_set()).spawn();
let fatal = ctx
.coordinator()
.fatal_error()
.expect("the mismatch is recorded before the first poll");
assert_eq!(fatal.error_type, "NonDeterministicExecutionError");
drop(future);
}
#[test]
fn batch_summary_setter_replaces_and_survives_serdes() {
let result = BatchResult::<u32>::new(Vec::new(), CompletionReason::AllCompleted);
let ctx = DurableContext::test_context();
let map = ctx
.map(vec![1_u32], |_child, item, _idx| async move { Ok(item) })
.summary(|r| format!("first {}", r.total_count()))
.summary(|r| format!("second {}", r.total_count()))
.serdes(Arc::new(JsonSerdes))
.result_serdes(JsonSerdes);
let summary = map.summary.as_ref().expect("the map keeps the summary");
assert_eq!(summary(&result), "second 0");
let parallel = ctx
.parallel(vec![crate::Branch::new("a", |_child| async { Ok(1_u32) })])
.summary(|r| format!("first {}", r.total_count()))
.summary(|r| format!("second {}", r.total_count()))
.serdes(Arc::new(JsonSerdes))
.result_serdes(JsonSerdes);
let summary = parallel
.summary
.as_ref()
.expect("the parallel keeps the summary");
assert_eq!(summary(&result), "second 0");
let mixed_result =
BatchResult::<crate::BranchValue>::new(Vec::new(), CompletionReason::AllCompleted);
let mut branches = crate::BranchSet::new();
let _a = branches.add("a", |_child| async { Ok(1_u32) });
let parallel_mixed = ctx
.parallel_mixed(branches)
.summary(|r| format!("first {}", r.total_count()))
.summary(|r| format!("second {}", r.total_count()))
.result_serdes(JsonSerdes);
let summary = parallel_mixed
.summary
.as_ref()
.expect("the parallel_mixed keeps the summary");
assert_eq!(summary(&mixed_result), "second 0");
}
}