use super::{PartitionLimits, DATA_FIELD};
use crate::{
client::RedisClient,
error::{NasaRedisError, Result},
};
use serde::Serialize;
use std::{
collections::HashMap,
io::Write,
sync::{Arc, Mutex},
};
use tokio::sync::{oneshot, Notify};
#[derive(Debug, Clone, Default)]
pub struct PublisherSnapshot {
pub inflight: usize,
pub payload_bytes: usize,
pub succeeded: u64,
pub failed_before_send: u64,
pub outcome_unknown: u64,
pub closed: bool,
pub unjoined_tasks: usize,
}
#[derive(Default)]
struct State {
next: u64,
tickets: HashMap<u64, usize>,
snapshot: PublisherSnapshot,
tasks: HashMap<u64, tokio::task::JoinHandle<()>>,
forced: bool,
}
struct PublishCompletion {
owner: Arc<PublisherCoordinator>,
ticket: u64,
kind: u8,
}
impl Drop for PublishCompletion {
fn drop(&mut self) {
self.owner.finish(self.ticket, self.kind);
}
}
pub(super) struct PublisherCoordinator {
state: Mutex<State>,
limits: PartitionLimits,
changed: Notify,
}
struct BoundedWriter {
bytes: Vec<u8>,
limit: usize,
}
impl Write for BoundedWriter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
if self
.bytes
.len()
.checked_add(bytes.len())
.is_none_or(|n| n > self.limit)
{
return Err(std::io::Error::other(
"partition publish exceeds max_record_bytes",
));
}
self.bytes.extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl PublisherCoordinator {
pub(super) fn new(limits: PartitionLimits) -> Arc<Self> {
Arc::new(Self {
state: Mutex::new(State {
next: 1,
..Default::default()
}),
limits,
changed: Notify::new(),
})
}
pub(super) async fn publish<T: Serialize>(
self: &Arc<Self>,
client: Arc<RedisClient>,
stream: String,
topic: &str,
event: &str,
data: &T,
passthrough: Option<serde_json::Map<String, serde_json::Value>>,
) -> Result<String> {
super::activation::validate_route(topic, event)?;
self.reap_finished().await;
let ticket = {
let mut state = self.state.lock().expect("publish registry");
if state.snapshot.closed
|| state.tickets.len()
+ state
.tasks
.keys()
.filter(|id| !state.tickets.contains_key(id))
.count()
>= self.limits.max_inflight_publishes
|| state
.snapshot
.payload_bytes
.checked_add(self.limits.max_record_bytes)
.is_none_or(|n| n > self.limits.max_inflight_publish_bytes)
{
return Err(NasaRedisError::NotExecuted(
"partition publish admission closed or exhausted".into(),
));
}
let id = state.next;
state.next = id.checked_add(1).ok_or_else(|| {
NasaRedisError::NotExecuted("publisher identity exhausted".into())
})?;
state.tickets.insert(id, self.limits.max_record_bytes);
state.snapshot.payload_bytes += self.limits.max_record_bytes;
state.snapshot.inflight += 1;
id
};
#[derive(Serialize)]
struct WireEnvelope<'a, T> {
topic: &'a str,
event: &'a str,
data: &'a T,
#[serde(skip_serializing_if = "Option::is_none")]
passthrough: Option<serde_json::Map<String, serde_json::Value>>,
}
let serialized = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut writer = BoundedWriter {
bytes: Vec::with_capacity(self.limits.max_record_bytes),
limit: self.limits.max_record_bytes,
};
serde_json::to_writer(
&mut writer,
&WireEnvelope {
topic,
event,
data,
passthrough,
},
)
.map(|_| writer.bytes)
}));
let mut body = match serialized {
Ok(Ok(body)) => body,
result => {
self.finish(ticket, 1);
return Err(NasaRedisError::Codec(match result {
Ok(Err(error)) => error.to_string(),
_ => "partition serializer panic".into(),
}));
}
};
body.shrink_to_fit();
let (sender, receiver) = oneshot::channel();
{
let mut state = self.state.lock().expect("publish registry");
if state.forced {
drop(state);
self.finish(ticket, 1);
return Err(NasaRedisError::NotExecuted(
"publisher force stop before send".into(),
));
}
let previous = state
.tickets
.insert(ticket, body.len())
.expect("registered publish");
state.snapshot.payload_bytes -= previous - body.len();
let mut completion = PublishCompletion {
owner: self.clone(),
ticket,
kind: 2,
};
let handle = tokio::spawn(async move {
let result = redis::cmd("XADD")
.arg(stream)
.arg("*")
.arg(DATA_FIELD)
.arg(&body)
.query_async::<String>(&mut client.conn())
.await;
drop(body);
let result = match result {
Ok(id) => {
completion.kind = 0;
Ok(id)
}
Err(error) => Err(NasaRedisError::PublishOutcomeUnknown(error.to_string())),
};
let _ = sender.send(result);
drop(completion);
});
state.tasks.insert(ticket, handle);
}
receiver.await.unwrap_or_else(|_| {
Err(NasaRedisError::PublishOutcomeUnknown(
"publisher supervisor unavailable".into(),
))
})
}
fn finish(&self, ticket: u64, kind: u8) {
let mut state = self.state.lock().expect("publish registry");
if let Some(bytes) = state.tickets.remove(&ticket) {
state.snapshot.payload_bytes -= bytes;
state.snapshot.inflight -= 1;
match kind {
0 => state.snapshot.succeeded += 1,
1 => state.snapshot.failed_before_send += 1,
_ => state.snapshot.outcome_unknown += 1,
}
}
self.changed.notify_waiters();
}
pub(super) fn close(&self) {
self.state.lock().expect("publish registry").snapshot.closed = true;
}
pub(super) fn force(&self) {
let mut state = self.state.lock().expect("publish registry");
state.snapshot.closed = true;
state.forced = true;
for task in state.tasks.values() {
task.abort();
}
}
pub(super) async fn drain(&self) {
loop {
self.reap_finished().await;
let changed = self.changed.notified();
let snapshot = self.snapshot();
if snapshot.inflight == 0 && snapshot.unjoined_tasks == 0 {
return;
}
tokio::select! {
_ = changed => {
}
_ = tokio::time::sleep(std::time::Duration::from_millis(5)) => {
}
}
}
}
async fn reap_finished(&self) {
let handles = {
let mut state = self.state.lock().expect("publish registry");
let ids: Vec<_> = state
.tasks
.iter()
.filter(|(_, task)| task.is_finished())
.map(|(id, _)| *id)
.collect();
ids.into_iter()
.filter_map(|id| state.tasks.remove(&id))
.collect::<Vec<_>>()
};
for handle in handles {
let _ = handle.await;
}
}
pub(super) fn snapshot(&self) -> PublisherSnapshot {
let state = self.state.lock().expect("publish registry");
let mut snapshot = state.snapshot.clone();
snapshot.unjoined_tasks = state.tasks.len();
snapshot
}
}