use std::collections::BTreeMap;
use hatchet_proto::root::AssignedAction;
use serde::Deserialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::TaskError;
pub type BatchMemberId = String;
pub(crate) type BatchReport = BTreeMap<BatchMemberId, Result<Value, TaskError>>;
#[derive(Deserialize)]
struct WireItem {
#[serde(default)]
workflow_run_id: String,
#[serde(default)]
payload: WirePayload,
}
#[derive(Default, Deserialize)]
struct WirePayload {
#[serde(default)]
input: Value,
}
#[derive(Clone, Debug)]
pub struct BatchItem<I> {
input: I,
workflow_run_id: String,
}
impl<I> BatchItem<I> {
#[must_use]
pub fn input(&self) -> &I {
&self.input
}
#[must_use]
pub fn workflow_run_id(&self) -> &str {
&self.workflow_run_id
}
#[must_use]
pub fn into_input(self) -> I {
self.input
}
}
#[derive(Clone, Debug)]
pub struct BatchInput<I> {
batch_id: Option<String>,
batch_key: Option<String>,
batch_index: Option<i32>,
batch_size: Option<i32>,
trigger_reason: Option<String>,
expected_size: Option<i32>,
items: BTreeMap<BatchMemberId, BatchItem<I>>,
}
impl<I: DeserializeOwned> BatchInput<I> {
pub(crate) fn decode(action: &AssignedAction) -> Result<(Self, BatchReport), TaskError> {
let start = action.batch_start_payload.as_ref();
let mut decoded = Self {
batch_id: action.batch_id.clone(),
batch_key: action.batch_key.clone(),
batch_index: action.batch_index,
batch_size: action.batch_size,
trigger_reason: start
.map(|start| start.trigger_reason.clone())
.filter(|reason| !reason.is_empty()),
expected_size: start.map(|start| start.expected_size),
items: BTreeMap::new(),
};
let mut rejected = BatchReport::new();
if action.action_payload.trim().is_empty() {
return Ok((decoded, rejected));
}
let wire: BTreeMap<BatchMemberId, WireItem> = serde_json::from_str(&action.action_payload)
.map_err(|e| {
TaskError::non_retryable(format!(
"the batch action payload is not a JSON object of items keyed by task run \
id: {e}"
))
})?;
for (id, item) in wire {
match serde_json::from_value::<I>(item.payload.input) {
Ok(input) => {
decoded.items.insert(
id,
BatchItem {
input,
workflow_run_id: item.workflow_run_id,
},
);
}
Err(e) => {
rejected.insert(
id,
Err(TaskError::non_retryable(format!(
"the input does not match the task's input type: {e}"
))),
);
}
}
}
Ok((decoded, rejected))
}
}
impl<I> BatchInput<I> {
#[must_use]
pub fn len(&self) -> usize {
self.items.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&BatchMemberId, &I)> {
self.items.iter().map(|(id, item)| (id, &item.input))
}
pub fn items(&self) -> impl Iterator<Item = (&BatchMemberId, &BatchItem<I>)> {
self.items.iter()
}
pub fn ids(&self) -> impl Iterator<Item = &BatchMemberId> {
self.items.keys()
}
#[must_use]
pub fn get(&self, id: &str) -> Option<&I> {
self.items.get(id).map(|item| &item.input)
}
#[must_use]
pub fn batch_id(&self) -> Option<&str> {
self.batch_id.as_deref()
}
#[must_use]
pub fn batch_key(&self) -> Option<&str> {
self.batch_key.as_deref()
}
#[must_use]
pub fn trigger_reason(&self) -> Option<&str> {
self.trigger_reason.as_deref()
}
#[must_use]
pub fn expected_size(&self) -> Option<i32> {
self.expected_size
}
#[must_use]
pub fn batch_index(&self) -> Option<i32> {
self.batch_index
}
#[must_use]
pub fn batch_size(&self) -> Option<i32> {
self.batch_size
}
}
#[must_use = "a batch output is the handler's answer; return it"]
#[derive(Clone, Debug)]
pub struct BatchOutput<O> {
items: BTreeMap<BatchMemberId, Result<O, TaskError>>,
}
impl<O> BatchOutput<O> {
pub fn new() -> Self {
Self {
items: BTreeMap::new(),
}
}
pub fn ok(mut self, id: impl Into<BatchMemberId>, value: O) -> Self {
self.items.insert(id.into(), Ok(value));
self
}
pub fn fail(mut self, id: impl Into<BatchMemberId>, error: TaskError) -> Self {
self.items.insert(id.into(), Err(error));
self
}
#[must_use]
pub fn len(&self) -> usize {
self.items.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub(crate) fn into_items(self) -> BTreeMap<BatchMemberId, Result<O, TaskError>> {
self.items
}
}
impl<O> Default for BatchOutput<O> {
fn default() -> Self {
Self::new()
}
}
impl<O> FromIterator<(BatchMemberId, O)> for BatchOutput<O> {
fn from_iter<T: IntoIterator<Item = (BatchMemberId, O)>>(iter: T) -> Self {
Self {
items: iter
.into_iter()
.map(|(id, value)| (id, Ok(value)))
.collect(),
}
}
}
pub(crate) fn reconcile<O: serde::Serialize>(
task: &str,
members: &[BatchMemberId],
answered: BTreeMap<BatchMemberId, Result<O, TaskError>>,
report: &mut BatchReport,
) {
for (id, result) in answered {
if !members.contains(&id) && !report.contains_key(&id) {
tracing::warn!(
task,
member = %id,
"a batch handler answered for an id that was not in the batch; dropping it",
);
continue;
}
let value = result.and_then(|value| {
serde_json::to_value(value).map_err(|e| {
TaskError::non_retryable(format!("the output could not be serialized to JSON: {e}"))
})
});
report.insert(id, value);
}
for id in members {
if report.contains_key(id) {
continue;
}
tracing::warn!(
task,
member = %id,
"a batch handler returned no output for one of its members; failing it",
);
report.insert(
id.clone(),
Err(TaskError::non_retryable(format!(
"the batch handler of `{task}` returned no output for this member of the batch; \
a member that is never reported is left running by the engine until it times \
out, so it is failed here instead"
))),
);
}
}
pub(crate) fn member_ids(action: &AssignedAction) -> Vec<BatchMemberId> {
let raw = action.action_payload.trim();
if raw.is_empty() {
return Vec::new();
}
match serde_json::from_str::<BTreeMap<BatchMemberId, serde::de::IgnoredAny>>(raw) {
Ok(items) => items.into_keys().collect(),
Err(_) => Vec::new(),
}
}
pub(crate) fn fail_all(members: &[BatchMemberId], error: &TaskError) -> BatchReport {
members
.iter()
.map(|id| (id.clone(), Err(error.clone())))
.collect()
}
pub(crate) fn run_key(action: &AssignedAction) -> String {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(1);
match action.batch_id.as_deref() {
Some(id) if !id.is_empty() => format!("batch:{id}"),
_ => format!("batch:anonymous:{}", NEXT.fetch_add(1, Ordering::Relaxed)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use hatchet_proto::root::BatchStartPayload;
use serde_json::json;
fn action(items: &[(&str, Value)]) -> AssignedAction {
let payload: serde_json::Map<String, Value> = items
.iter()
.map(|(id, input)| {
(
(*id).to_string(),
json!({
"workflow_run_id": format!("{id}-run"),
"payload": {
"input": input,
"parents": {},
"triggers": {"filter_payload": null},
"overrides": null,
"user_data": null,
"triggered_by": "manual",
},
}),
)
})
.collect();
AssignedAction {
action_id: "dev_t:t".to_string(),
action_payload: Value::Object(payload).to_string(),
batch_id: Some("b-1".to_string()),
batch_key: Some("acme".to_string()),
batch_start_payload: Some(BatchStartPayload {
trigger_reason: "batch size threshold 2 reached".to_string(),
trigger_time: None,
expected_size: i32::try_from(items.len()).unwrap(),
}),
..AssignedAction::default()
}
}
#[test]
fn decoding_types_every_item_and_keeps_the_ids() {
let (input, rejected) =
BatchInput::<u32>::decode(&action(&[("a", json!(1)), ("b", json!(2))])).unwrap();
assert!(rejected.is_empty());
assert_eq!(input.len(), 2);
assert!(!input.is_empty());
assert_eq!(input.get("a"), Some(&1));
assert_eq!(input.get("missing"), None);
assert_eq!(
input
.iter()
.map(|(id, value)| (id.as_str(), *value))
.collect::<Vec<_>>(),
vec![("a", 1), ("b", 2)],
);
assert_eq!(
input.ids().map(String::as_str).collect::<Vec<_>>(),
vec!["a", "b"],
);
assert_eq!(
input
.items()
.map(|(_, item)| item.workflow_run_id())
.collect::<Vec<_>>(),
vec!["a-run", "b-run"],
);
}
#[test]
fn decoding_carries_the_batch_wide_facts_and_says_what_the_engine_never_sends() {
let (input, _) = BatchInput::<u32>::decode(&action(&[("a", json!(1))])).unwrap();
assert_eq!(input.batch_id(), Some("b-1"));
assert_eq!(input.batch_key(), Some("acme"));
assert_eq!(
input.trigger_reason(),
Some("batch size threshold 2 reached")
);
assert_eq!(input.expected_size(), Some(1));
assert_eq!(input.batch_index(), None);
assert_eq!(input.batch_size(), None);
}
#[test]
fn a_nested_input_survives_the_round_trip() {
let (input, _) = BatchInput::<Value>::decode(&action(&[(
"a",
json!({"n": 0, "nested": {"k": [1, 2]}}),
)]))
.unwrap();
assert_eq!(
input.get("a"),
Some(&json!({"n": 0, "nested": {"k": [1, 2]}}))
);
}
#[test]
fn an_item_that_does_not_match_the_input_type_fails_alone() {
let (input, rejected) =
BatchInput::<u32>::decode(&action(&[("a", json!(1)), ("b", json!("not a number"))]))
.unwrap();
assert_eq!(input.len(), 1);
assert!(input.get("b").is_none());
let error = rejected
.get("b")
.expect("b was rejected")
.as_ref()
.unwrap_err();
assert!(!error.is_retryable(), "{error}");
assert!(error.message().contains("input type"), "{error}");
}
#[test]
fn an_empty_payload_is_an_empty_batch_and_a_malformed_one_is_an_error() {
let empty = AssignedAction {
action_payload: String::new(),
..AssignedAction::default()
};
let (input, rejected) = BatchInput::<u32>::decode(&empty).unwrap();
assert!(input.is_empty());
assert!(rejected.is_empty());
assert_eq!(input.batch_id(), None);
assert_eq!(input.trigger_reason(), None);
assert_eq!(input.expected_size(), None);
let malformed = AssignedAction {
action_payload: "[1, 2]".to_string(),
..AssignedAction::default()
};
let error = BatchInput::<u32>::decode(&malformed).unwrap_err();
assert!(!error.is_retryable(), "{error}");
assert!(error.message().contains("JSON object of items"), "{error}");
}
#[test]
fn an_output_is_collected_or_built() {
let built = BatchOutput::new()
.ok("a", 1)
.fail("b", TaskError::new("no"));
assert_eq!(built.len(), 2);
assert!(!built.is_empty());
assert!(BatchOutput::<u32>::default().is_empty());
let collected: BatchOutput<u32> = [("a".to_string(), 1), ("b".to_string(), 2)]
.into_iter()
.collect();
assert_eq!(collected.len(), 2);
assert_eq!(
collected.into_items().get("a").unwrap().as_ref().unwrap(),
&1
);
}
#[test]
fn answering_for_a_member_twice_keeps_the_last_answer() {
let output = BatchOutput::new().ok("a", 1).ok("a", 2);
assert_eq!(output.len(), 1);
assert_eq!(output.into_items().get("a").unwrap().as_ref().unwrap(), &2);
}
#[test]
fn reconciling_fails_the_members_the_handler_left_out_and_drops_strangers() {
let members = vec!["a".to_string(), "b".to_string()];
let mut report = BatchReport::new();
let answered = BTreeMap::from([
("a".to_string(), Ok(1_u32)),
("stranger".to_string(), Ok(9)),
]);
reconcile("t", &members, answered, &mut report);
assert_eq!(report.len(), 2, "the stranger is not reported: {report:?}");
assert_eq!(report["a"].as_ref().unwrap(), &json!(1));
let omitted = report["b"].as_ref().unwrap_err();
assert!(!omitted.is_retryable(), "{omitted}");
assert!(omitted.message().contains("no output"), "{omitted}");
}
#[test]
fn reconciling_keeps_an_already_rejected_member_and_its_own_answer() {
let members = vec!["a".to_string()];
let mut report = BatchReport::from([(
"bad".to_string(),
Err(TaskError::non_retryable("the input does not match")),
)]);
let answered = BTreeMap::from([
("a".to_string(), Err(TaskError::new("retry me"))),
("bad".to_string(), Ok(1_u32)),
]);
reconcile("t", &members, answered, &mut report);
assert!(report["a"].as_ref().unwrap_err().is_retryable());
assert_eq!(report["bad"].as_ref().unwrap(), &json!(1));
}
#[test]
fn the_members_are_named_even_when_their_items_cannot_be_read() {
assert_eq!(
member_ids(&action(&[("b", json!(1)), ("a", json!(2))])),
vec!["a".to_string(), "b".to_string()],
"in id order, as the handler sees them",
);
let unreadable = AssignedAction {
action_payload: json!({"a": 1, "b": "not an item"}).to_string(),
..AssignedAction::default()
};
assert_eq!(
member_ids(&unreadable),
vec!["a".to_string(), "b".to_string()],
);
for nobody in ["", " ", "[1, 2]", "not json at all", "null"] {
let action = AssignedAction {
action_payload: nobody.to_string(),
..AssignedAction::default()
};
assert!(member_ids(&action).is_empty(), "{nobody:?}");
}
}
#[test]
fn failing_the_whole_batch_gives_every_member_the_same_error() {
let members = vec!["a".to_string(), "b".to_string()];
let report = fail_all(&members, &TaskError::non_retryable("the model is down"));
assert_eq!(report.len(), 2);
for id in &members {
let error = report[id].as_ref().unwrap_err();
assert_eq!(error.message(), "the model is down");
assert!(!error.is_retryable());
}
let retryable = fail_all(&members, &TaskError::new("try again"));
assert!(retryable["a"].as_ref().unwrap_err().is_retryable());
}
#[test]
fn a_batch_is_booked_under_its_batch_id() {
assert_eq!(run_key(&action(&[("a", json!(1))])), "batch:b-1");
let anonymous = AssignedAction::default();
let first = run_key(&anonymous);
assert!(first.starts_with("batch:anonymous:"), "{first}");
assert_ne!(first, run_key(&anonymous));
}
#[test]
fn an_output_that_cannot_be_serialized_fails_only_its_own_member() {
use std::collections::HashMap;
let members = vec!["a".to_string(), "b".to_string()];
let mut report = BatchReport::new();
let answered = BTreeMap::from([
("a".to_string(), Ok(HashMap::from([((1, 2), 3)]))),
("b".to_string(), Ok(HashMap::new())),
]);
reconcile("t", &members, answered, &mut report);
let error = report["a"].as_ref().unwrap_err();
assert!(!error.is_retryable(), "{error}");
assert!(error.message().contains("output"), "{error}");
assert!(report["b"].is_ok());
}
}