use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use crate::client::{Conn, RedisClient};
use crate::error::{NasaRedisError, Result};
use crate::job::keyspace::JobKeyspace;
use crate::job::model::JobPubSubMode;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum FanoutSignal {
Notification(Vec<u8>),
Receipt(Vec<u8>),
}
pub(crate) struct JobPubSubLane {
_connection: Conn,
inbox: JobPushInbox,
receipt_channels: HashSet<String>,
}
struct JobPushInbox {
receiver: tokio::sync::mpsc::Receiver<redis::PushInfo>,
overflowed: Arc<AtomicBool>,
overflow_notify: Arc<tokio::sync::Notify>,
}
impl JobPushInbox {
fn new(capacity: usize) -> (Arc<dyn redis::aio::AsyncPushSender>, Self) {
let (sender, receiver) = tokio::sync::mpsc::channel(capacity.max(1));
let overflowed = Arc::new(AtomicBool::new(false));
let overflow_notify = Arc::new(tokio::sync::Notify::new());
let sender_overflowed = overflowed.clone();
let sender_notify = overflow_notify.clone();
let push_sender: Arc<dyn redis::aio::AsyncPushSender> = Arc::new(
move |push: redis::PushInfo| -> std::result::Result<(), ()> {
match sender.try_send(push) {
Ok(()) => Ok(()),
Err(_) => {
sender_overflowed.store(true, Ordering::Release);
sender_notify.notify_one();
Err(())
}
}
},
);
(
push_sender,
Self {
receiver,
overflowed,
overflow_notify,
},
)
}
async fn recv(&mut self) -> Result<redis::PushInfo> {
loop {
if self.overflowed.load(Ordering::Acquire) {
return Err(crate::job::JobError::ExecutionUnknown(
"RedisJob RESP3 Push 有界通道容量耗尽".to_owned(),
)
.into());
}
tokio::select! {
push = self.receiver.recv() => {
return push.ok_or_else(|| {
crate::job::JobError::ExecutionUnknown(
"RedisJob RESP3 Push 通道已关闭".to_owned(),
)
.into()
});
}
_ = self.overflow_notify.notified() => {}
}
}
}
}
impl JobPubSubLane {
pub(crate) async fn start(
client: Arc<RedisClient>,
keyspace: &JobKeyspace,
mode: JobPubSubMode,
node_identity: &str,
executor_id: &str,
) -> Result<Self> {
let bucket_count = usize::try_from(keyspace.fanout_bucket_count()).unwrap_or(usize::MAX);
let push_capacity = bucket_count.saturating_mul(4).saturating_add(32).max(64);
let (push_sender, mut inbox) = JobPushInbox::new(push_capacity);
let mut connection = client.job_pubsub_conn(push_sender).await?;
let ack_timeout_ms = client.config().command.response_timeout_ms;
let mut receipt_channels = HashSet::new();
for bucket in 0..keyspace.fanout_bucket_count() {
let notification = keyspace.fanout_notify_channel_at(bucket, node_identity);
let receipt = keyspace.fanout_receipt_channel_at(bucket, executor_id);
subscribe(
&mut connection,
&mut inbox,
mode,
¬ification,
ack_timeout_ms,
)
.await?;
subscribe(&mut connection, &mut inbox, mode, &receipt, ack_timeout_ms).await?;
receipt_channels.insert(receipt);
}
Ok(Self {
_connection: connection,
inbox,
receipt_channels,
})
}
pub(crate) async fn next_signal(&mut self) -> Result<FanoutSignal> {
loop {
let push = self.inbox.recv().await?;
match push.kind {
redis::PushKind::Message | redis::PushKind::SMessage => {
if push.data.len() != 2 {
return Err(crate::job::JobError::Protocol(
"Fanout Push 字段数不符合频道与信封合同".to_owned(),
)
.into());
}
let channel = value_bytes(&push.data[0]);
let payload = value_bytes(&push.data[1]);
let channel = String::from_utf8(channel).map_err(|_| {
NasaRedisError::from(crate::job::JobError::Protocol(
"Fanout Push channel 不是 UTF-8".to_owned(),
))
})?;
return Ok(if self.receipt_channels.contains(&channel) {
FanoutSignal::Receipt(payload)
} else {
FanoutSignal::Notification(payload)
});
}
redis::PushKind::Disconnection => {
return Err(crate::job::JobError::ExecutionUnknown(
"RedisJob RESP3 subscription generation 已断开".to_owned(),
)
.into());
}
redis::PushKind::Subscribe
| redis::PushKind::SSubscribe
| redis::PushKind::Unsubscribe
| redis::PushKind::SUnsubscribe => {}
_ => {}
}
}
}
}
async fn subscribe(
connection: &mut Conn,
inbox: &mut JobPushInbox,
mode: JobPubSubMode,
channel: &str,
timeout_ms: u64,
) -> Result<()> {
let command = match mode {
JobPubSubMode::Sharded => "SSUBSCRIBE",
JobPubSubMode::Broadcast => "SUBSCRIBE",
};
let mut cmd = redis::cmd(command);
cmd.arg(channel);
cmd.exec_async(connection)
.await
.map_err(NasaRedisError::Redis)?;
let expected = match mode {
JobPubSubMode::Sharded => redis::PushKind::SSubscribe,
JobPubSubMode::Broadcast => redis::PushKind::Subscribe,
};
let wait_ack = async {
loop {
let push = inbox.recv().await?;
if push.kind == redis::PushKind::Disconnection {
return Err(crate::job::JobError::ExecutionUnknown(
"RedisJob RESP3 订阅 ACK 前连接断开".to_owned(),
)
.into());
}
if push.kind == expected
&& push
.data
.first()
.is_some_and(|value| value_bytes(value) == channel.as_bytes())
{
return Ok(());
}
}
};
let timeout = if timeout_ms == 0 {
30_000
} else {
timeout_ms
};
tokio::time::timeout(std::time::Duration::from_millis(timeout), wait_ack)
.await
.map_err(|_| {
NasaRedisError::from(crate::job::JobError::ExecutionUnknown(
"RedisJob RESP3 订阅未在命令期限内取得 ACK".to_owned(),
))
})?
}
fn value_bytes(value: &redis::Value) -> Vec<u8> {
match value {
redis::Value::BulkString(bytes) => bytes.clone(),
redis::Value::SimpleString(text) => text.as_bytes().to_vec(),
_ => Vec::new(),
}
}