use super::Envelope;
use futures::{future::BoxFuture, FutureExt};
use serde::de::DeserializeOwned;
use std::{any::Any, collections::HashMap, hash::Hash, sync::Arc};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordIdentity {
pub stream: Arc<str>,
pub id: Arc<str>,
}
#[derive(Debug)]
pub struct PartitionRecord<T> {
pub identity: RecordIdentity,
pub topic: Arc<str>,
pub event: Arc<str>,
pub data: T,
pub passthrough: Option<Arc<serde_json::Map<String, serde_json::Value>>>,
}
pub(crate) type Payload = Box<dyn Any + Send>;
pub(crate) type PlanMap = HashMap<(String, String), Arc<ConsumerPlan>>;
pub(crate) type Decode =
dyn Fn(RecordIdentity, Envelope) -> Result<PreparedRecord, DecodeFailure> + Send + Sync;
pub(crate) type Invoke =
dyn Fn(Vec<(String, Payload)>) -> BoxFuture<'static, Vec<String>> + Send + Sync;
#[derive(Debug, Clone, Copy)]
pub(crate) enum DecodeFailure {
Malformed,
Internal,
}
pub(crate) struct PreparedRecord {
pub payload: Payload,
pub route: Option<napart::RouteHash>,
pub weight: usize,
}
pub(crate) struct ConsumerPlan {
pub id: u32,
pub legacy: bool,
pub decode: Arc<Decode>,
pub invoke: Arc<Invoke>,
}
impl ConsumerPlan {
pub(crate) fn spec(&self, ordered: bool) -> napart::TaskSpec {
if ordered && !self.legacy {
napart::TaskSpec::strict(napart::TaskType(self.id * 2))
} else {
napart::TaskSpec::relaxed(napart::TaskType(self.id * 2 + 1))
}
}
pub(crate) fn typed<T, K, KF, WF, H, Fut>(id: u32, key: KF, weight: WF, handler: H) -> Arc<Self>
where
T: DeserializeOwned + Send + 'static,
K: Hash,
KF: Fn(&T) -> Option<K> + Send + Sync + 'static,
WF: Fn(&T) -> usize + Send + Sync + 'static,
H: Fn(PartitionRecord<T>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<(), String>> + Send + 'static,
{
let handler = Arc::new(handler);
Arc::new(Self {
id,
legacy: false,
decode: Arc::new(move |identity, env| {
let data: T =
serde_json::from_value(env.data).map_err(|_| DecodeFailure::Malformed)?;
let (route, weight) =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
(
key(&data).map(|key| napart::RouteHash::from_key(&key)),
weight(&data),
)
}))
.map_err(|_| DecodeFailure::Internal)?;
Ok(PreparedRecord {
payload: Box::new(PartitionRecord {
identity,
topic: Arc::from(env.topic),
event: Arc::from(env.event),
data,
passthrough: env.passthrough.map(Arc::new),
}),
route,
weight: weight.saturating_add(std::mem::size_of::<T>()),
})
}),
invoke: Arc::new(move |items| {
let handler = handler.clone();
Box::pin(async move {
let mut failed = Vec::new();
for (id, payload) in items {
let result = std::panic::AssertUnwindSafe(async {
match payload.downcast::<PartitionRecord<T>>() {
Ok(record) => handler(*record).await,
Err(_) => Err("consumer plan payload type mismatch".into()),
}
})
.catch_unwind()
.await;
if !matches!(result, Ok(Ok(()))) {
failed.push(id);
}
}
failed
})
}),
})
}
pub(crate) fn legacy<T, F, Fut>(id: u32, handler: F) -> Arc<Self>
where
T: DeserializeOwned + Send + 'static,
F: Fn(Vec<T>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = Result<(), String>> + Send + 'static,
{
let handler = Arc::new(handler);
Arc::new(Self {
id,
legacy: true,
decode: Arc::new(|_, env| {
let data: T =
serde_json::from_value(env.data).map_err(|_| DecodeFailure::Malformed)?;
Ok(PreparedRecord {
payload: Box::new(data),
route: None,
weight: std::mem::size_of::<T>(),
})
}),
invoke: Arc::new(move |items| {
let handler = handler.clone();
Box::pin(async move {
let mut values = Vec::with_capacity(items.len());
let mut failed = Vec::new();
let mut good = Vec::new();
for (id, payload) in items {
match payload.downcast::<T>() {
Ok(value) => {
good.push(id);
values.push(*value);
}
Err(_) => failed.push(id),
}
}
if !values.is_empty()
&& !matches!(
std::panic::AssertUnwindSafe(async move { handler(values).await })
.catch_unwind()
.await,
Ok(Ok(()))
)
{
failed.extend(good);
}
failed
})
}),
})
}
}