use super::{
command_mismatch, decode_wire_avro_value, encode_typed_envelope, invalid_recorded_history,
normalize_avro_arguments, AvroValue, Error, HistoryEvent, RecordedCommand, ReplayFailure,
Result, Value, WorkflowContext,
};
use serde::Serialize;
use std::{
future::Future,
pin::Pin,
task::{Context as TaskContext, Poll},
};
pub(super) const COMMAND: &str = "signal_external_workflow";
pub(super) const CAPABILITY: &str = "signal_external_workflow";
pub(super) const REQUESTED: &str = "ExternalSignalRequested";
pub(super) const DELIVERED: &str = "ExternalSignalDelivered";
pub(super) const FAILED: &str = "ExternalSignalFailed";
const MAX_WORKFLOW_ID_CHARS: usize = 191;
const MAX_SIGNAL_NAME_CHARS: usize = 191;
const MAX_SIGNAL_NAME_BYTES: usize = 255;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ExternalSignalOutcome {
Delivered,
TargetNotFound,
TargetClosed,
Rejected,
}
impl ExternalSignalOutcome {
pub fn as_str(self) -> &'static str {
match self {
Self::Delivered => "delivered",
Self::TargetNotFound => "target_not_found",
Self::TargetClosed => "target_closed",
Self::Rejected => "rejected",
}
}
fn parse(value: &str) -> Option<Self> {
Some(match value {
"delivered" => Self::Delivered,
"target_not_found" => Self::TargetNotFound,
"target_closed" => Self::TargetClosed,
"rejected" => Self::Rejected,
_ => return None,
})
}
}
impl std::fmt::Display for ExternalSignalOutcome {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExternalSignalResult {
pub outcome: ExternalSignalOutcome,
pub reason: Option<String>,
pub message: Option<String>,
pub target_workflow_id: String,
pub target_run_id: Option<String>,
pub signal_name: String,
pub request_id: Option<String>,
pub target_command_id: Option<String>,
pub target_signal_id: Option<String>,
pub retained: bool,
}
impl ExternalSignalResult {
pub fn is_delivered(&self) -> bool {
self.outcome == ExternalSignalOutcome::Delivered
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct RecordedRequest {
target_workflow_id: String,
target_run_id: Option<String>,
signal_name: String,
arguments: Option<AvroValue>,
}
pub(super) fn runtime_supports(capabilities: Option<&Value>) -> bool {
let Some(capabilities) = capabilities.and_then(Value::as_object) else {
return false;
};
let flag = match capabilities.get(CAPABILITY) {
Some(Value::Bool(supported)) => *supported,
Some(Value::Object(capability)) => capability.get("supported") == Some(&Value::Bool(true)),
_ => false,
};
let command_advertised = capabilities
.get("supported_workflow_task_commands")
.and_then(Value::as_array)
.is_some_and(|commands| commands.iter().any(|command| command == COMMAND));
flag && command_advertised
}
pub(super) fn is_event(event: &HistoryEvent) -> bool {
matches!(event.event_type.as_str(), REQUESTED | DELIVERED | FAILED)
}
fn optional_string(event: &HistoryEvent, field: &str) -> Option<String> {
event
.payload
.get(field)
.and_then(Value::as_str)
.filter(|value| !value.is_empty())
.map(str::to_string)
}
fn required_string(event: &HistoryEvent, field: &str, sequence: u64) -> Result<String> {
optional_string(event, field).ok_or_else(|| {
invalid_recorded_history(
"external_signal_field_missing",
sequence,
&format!("non-empty {field}"),
&format!("missing or invalid {field} on {}", event.event_type),
"external-signal history is missing a required field",
)
})
}
pub(super) fn recorded(
events: &[&HistoryEvent],
sequence: u64,
fallback_codec: &str,
) -> Result<RecordedCommand> {
let requested: Vec<_> = events
.iter()
.copied()
.filter(|event| event.event_type == REQUESTED)
.collect();
if requested.len() != 1 {
return Err(invalid_recorded_history(
"external_signal_request_count",
sequence,
&format!("one {REQUESTED} event"),
&format!("{} {REQUESTED} events", requested.len()),
"external-signal history must record exactly one request per workflow sequence",
));
}
let requested = requested[0];
let resolutions: Vec<_> = events
.iter()
.copied()
.filter(|event| event.event_type != REQUESTED)
.collect();
if resolutions.len() > 1 {
return Err(invalid_recorded_history(
"external_signal_resolution_count",
sequence,
"at most one ExternalSignalDelivered or ExternalSignalFailed event",
&format!("{} resolution events", resolutions.len()),
"external-signal history resolves one request more than once",
));
}
let target_workflow_id = required_string(requested, "target_workflow_instance_id", sequence)?;
let signal_name = required_string(requested, "signal_name", sequence)?;
let codec = requested
.payload
.get("payload_codec")
.and_then(Value::as_str)
.unwrap_or(fallback_codec);
let arguments = match requested
.payload
.get("arguments")
.filter(|value| !value.is_null())
{
Some(envelope) => Some(normalize_avro_arguments(
decode_wire_avro_value(envelope, codec).map_err(|error| {
if error.to_string().contains("unsupported_payload_codec") {
return error;
}
invalid_recorded_history(
"external_signal_arguments_incompatible",
sequence,
&format!("valid {codec} payload envelope"),
&error.to_string(),
"external-signal history arguments cannot be decoded with their recorded codec",
)
})?,
)),
None => None,
};
let request = RecordedRequest {
target_workflow_id,
target_run_id: optional_string(requested, "target_workflow_run_id"),
signal_name,
arguments,
};
let outcome = match resolutions.first() {
None => None,
Some(resolution) => Some(resolution_result(
resolution, &request, requested, sequence,
)?),
};
Ok(RecordedCommand::ExternalSignal {
sequence,
request,
outcome,
})
}
fn resolution_result(
event: &HistoryEvent,
request: &RecordedRequest,
requested: &HistoryEvent,
sequence: u64,
) -> Result<ExternalSignalResult> {
for (field, expected) in [
(
"target_workflow_instance_id",
Some(request.target_workflow_id.as_str()),
),
("signal_name", Some(request.signal_name.as_str())),
] {
let actual = event.payload.get(field).and_then(Value::as_str);
if actual.is_some() && actual != expected {
return Err(invalid_recorded_history(
"external_signal_identity_mismatch",
sequence,
expected.unwrap_or_default(),
actual.unwrap_or_default(),
"external-signal resolution disagrees with its recorded request",
));
}
}
let request_id = optional_string(event, "request_id");
if let (Some(resolved), Some(recorded)) = (
request_id.as_deref(),
optional_string(requested, "request_id"),
) {
if resolved != recorded {
return Err(invalid_recorded_history(
"external_signal_identity_mismatch",
sequence,
&recorded,
resolved,
"external-signal resolution disagrees with its recorded command key",
));
}
}
let raw_outcome = event
.payload
.get("outcome")
.and_then(Value::as_str)
.unwrap_or(if event.event_type == DELIVERED {
"delivered"
} else {
""
});
let outcome = ExternalSignalOutcome::parse(raw_outcome)
.filter(|outcome| {
(*outcome == ExternalSignalOutcome::Delivered) == (event.event_type == DELIVERED)
})
.ok_or_else(|| {
invalid_recorded_history(
"external_signal_outcome_invalid",
sequence,
if event.event_type == DELIVERED {
"delivered"
} else {
"target_not_found, target_closed or rejected"
},
raw_outcome,
"external-signal resolution records an unknown outcome",
)
})?;
let reason = optional_string(event, "reason");
if outcome != ExternalSignalOutcome::Delivered && reason.is_none() {
return Err(invalid_recorded_history(
"external_signal_field_missing",
sequence,
"non-empty reason",
&format!("missing reason on {}", event.event_type),
"a failed external signal must record its reason",
));
}
Ok(ExternalSignalResult {
outcome,
reason,
message: optional_string(event, "message"),
target_workflow_id: request.target_workflow_id.clone(),
target_run_id: optional_string(event, "target_workflow_run_id"),
signal_name: request.signal_name.clone(),
request_id,
target_command_id: optional_string(event, "target_command_id"),
target_signal_id: optional_string(event, "target_signal_id"),
retained: event.payload.get("retained").and_then(Value::as_bool) == Some(true),
})
}
fn validate_signal_name(signal_name: &str) -> Result<()> {
let refusal = if signal_name.trim().is_empty() {
Some("must not be empty or blank")
} else if signal_name.bytes().any(|byte| byte < 0x20 || byte == 0x7f) {
Some("must not contain control characters")
} else if signal_name.len() > MAX_SIGNAL_NAME_BYTES {
Some("must be at most 255 bytes")
} else if signal_name.chars().count() > MAX_SIGNAL_NAME_CHARS {
Some("must be at most 191 characters")
} else {
None
};
match refusal {
Some(refusal) => Err(Error::InvalidExternalSignal(format!(
"signal_name {refusal}"
))),
None => Ok(()),
}
}
fn validate(workflow_id: &str, signal_name: &str, run_id: Option<&str>) -> Result<()> {
if workflow_id.is_empty() || workflow_id.chars().count() > MAX_WORKFLOW_ID_CHARS {
return Err(Error::InvalidExternalSignal(format!(
"workflow_id must be 1 to {MAX_WORKFLOW_ID_CHARS} characters"
)));
}
validate_signal_name(signal_name)?;
if run_id.is_some_and(str::is_empty) {
return Err(Error::InvalidExternalSignal(
"run_id must not be empty; omit it to signal the current run".to_string(),
));
}
Ok(())
}
impl WorkflowContext {
pub fn signal_external_workflow<T: Serialize>(
&self,
workflow_id: impl Into<String>,
signal_name: impl Into<String>,
args: T,
) -> ExternalSignalCall {
ExternalSignalCall {
ctx: self.clone(),
workflow_id: workflow_id.into(),
signal_name: signal_name.into(),
run_id: None,
args: Some(AvroValue::from_serialize(&args)),
submitted: false,
matched_pending: false,
}
}
}
pub struct ExternalSignalCall {
ctx: WorkflowContext,
workflow_id: String,
signal_name: String,
run_id: Option<String>,
args: Option<Result<AvroValue>>,
submitted: bool,
matched_pending: bool,
}
impl ExternalSignalCall {
pub fn run_id(mut self, run_id: impl Into<String>) -> Self {
self.run_id = Some(run_id.into());
self
}
fn mismatch(
&self,
sequence: u64,
field: &str,
recorded: String,
current: String,
) -> Poll<Result<ExternalSignalResult>> {
Poll::Ready(Err(Error::NonDeterministicReplay(ReplayFailure::new(
"recorded_command_detail_mismatch",
Some(sequence),
Some(format!("external signal {field}:{recorded}")),
Some(format!("external signal {field}:{current}")),
format!("recorded external signal {field} differs from the current workflow command"),
))))
}
}
impl Future for ExternalSignalCall {
type Output = Result<ExternalSignalResult>;
fn poll(mut self: Pin<&mut Self>, _cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
if self.matched_pending || self.submitted {
return Poll::Pending;
}
let args = match self.args.take() {
Some(Ok(args)) => normalize_avro_arguments(args),
Some(Err(error)) => return Poll::Ready(Err(error)),
None => AvroValue::Array(Vec::new()),
};
let ctx = self.ctx.clone();
let mut state = match ctx.state.lock() {
Ok(state) => state,
Err(_) => return Poll::Ready(Err(Error::WorkflowStatePoisoned)),
};
if let Some(recorded) = state.recorded_commands.get(state.command_cursor).cloned() {
let RecordedCommand::ExternalSignal {
sequence,
request,
outcome,
} = recorded
else {
return Poll::Ready(Err(command_mismatch(
&recorded,
format!("external signal:{}", self.signal_name),
)));
};
if request.target_workflow_id != self.workflow_id {
return self.mismatch(
sequence,
"target",
request.target_workflow_id,
self.workflow_id.clone(),
);
}
if request.target_run_id != self.run_id {
return self.mismatch(
sequence,
"run",
request.target_run_id.unwrap_or_default(),
self.run_id.clone().unwrap_or_default(),
);
}
if request.signal_name != self.signal_name {
return self.mismatch(
sequence,
"name",
request.signal_name,
self.signal_name.clone(),
);
}
if let Some(recorded_args) = request.arguments {
if recorded_args != args {
return self.mismatch(
sequence,
"arguments",
format!("{recorded_args:?}"),
format!("{args:?}"),
);
}
}
state.command_cursor += 1;
state.replay_clock.unavailable();
if let Some(outcome) = outcome {
return Poll::Ready(Ok(outcome));
}
state.matched_recorded_pending = true;
state.observation_pending = true;
self.matched_pending = true;
return Poll::Pending;
}
if let Err(error) = validate(&self.workflow_id, &self.signal_name, self.run_id.as_deref()) {
return Poll::Ready(Err(error));
}
if !state.signal_external_workflow_supported {
return Poll::Ready(Err(Error::SignalExternalWorkflowUnavailable));
}
let arguments = match encode_typed_envelope(&args, &state.payload_codec) {
Ok(arguments) => arguments,
Err(error) => return Poll::Ready(Err(error)),
};
state.replay_clock.unavailable();
state.commands.push(serde_json::json!({
"type": COMMAND,
"workflow_id": self.workflow_id,
"run_id": self.run_id,
"signal_name": self.signal_name,
"arguments": arguments,
}));
drop(state);
self.submitted = true;
Poll::Pending
}
}