use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use redis::aio::ConnectionManager;
use redis::AsyncCommands;
#[cfg(feature = "job")]
use redis::IntoConnectionInfo;
use crate::config::{CompatibilityProfile, RedisConfig};
use crate::error::{NasaRedisError, Result};
#[derive(Clone)]
pub enum Conn {
Single(ConnectionManager),
Cluster(redis::cluster_async::ClusterConnection),
}
impl redis::aio::ConnectionLike for Conn {
fn req_packed_command<'a>(
&'a mut self,
cmd: &'a redis::Cmd,
) -> redis::RedisFuture<'a, redis::Value> {
match self {
Conn::Single(c) => c.req_packed_command(cmd),
Conn::Cluster(c) => c.req_packed_command(cmd),
}
}
fn req_packed_commands<'a>(
&'a mut self,
cmd: &'a redis::Pipeline,
offset: usize,
count: usize,
) -> redis::RedisFuture<'a, Vec<redis::Value>> {
match self {
Conn::Single(c) => c.req_packed_commands(cmd, offset, count),
Conn::Cluster(c) => c.req_packed_commands(cmd, offset, count),
}
}
fn get_db(&self) -> i64 {
match self {
Conn::Single(c) => c.get_db(),
Conn::Cluster(c) => c.get_db(),
}
}
}
fn marker_key(namespace: &str) -> String {
format!("nasa:protocol:{namespace}")
}
fn naming_config_canonical(cfg: &RedisConfig) -> String {
let base = format!(
"v1|profile={}|lock.prefix={}|partition.default_group={}|partition.count={}",
cfg.profile.wire_name(),
cfg.lock.prefix,
cfg.partition.default_group,
cfg.partition.count
);
if cfg.partition.groups.is_empty() {
base
} else {
format!("{base}|partition.groups={}", canonical_groups(cfg))
}
}
fn canonical_groups(cfg: &RedisConfig) -> String {
let p = &cfg.partition;
let s = &cfg.stream;
#[derive(serde::Serialize)]
struct CanonRuntime {
count: u32,
rebalance_ms: u64,
min_idle_ms: u64,
holds_check_interval_ms: u64,
drain_timeout_ms: u64,
handler_timeout_ms: u64,
max_redeliver: u32,
poison_policy: crate::config::PoisonPolicy,
batch_size: usize,
poll_timeout_ms: u64,
inflight_max: usize,
}
#[derive(serde::Serialize)]
struct CanonGroup<'a> {
topics: Vec<&'a str>,
#[serde(flatten)]
rt: CanonRuntime,
}
#[derive(serde::Serialize)]
struct CanonPartition<'a> {
default: CanonRuntime,
groups: std::collections::BTreeMap<&'a str, CanonGroup<'a>>,
}
let default = CanonRuntime {
count: p.count,
rebalance_ms: p.rebalance_ms,
min_idle_ms: p.min_idle_ms,
holds_check_interval_ms: p.holds_check_interval_ms,
drain_timeout_ms: p.drain_timeout_ms,
handler_timeout_ms: p.handler_timeout_ms,
max_redeliver: p.max_redeliver,
poison_policy: p.poison_policy,
batch_size: s.batch_size,
poll_timeout_ms: s.poll_timeout_ms.max(1),
inflight_max: s.inflight_max,
};
let mut groups: std::collections::BTreeMap<&str, CanonGroup> =
std::collections::BTreeMap::new();
for (logical, g) in &p.groups {
let mut topics: Vec<&str> = if g.topics.is_empty() {
vec![logical.as_str()] } else {
g.topics.iter().map(|s| s.as_str()).collect()
};
topics.sort_unstable();
groups.insert(
logical.as_str(),
CanonGroup {
topics,
rt: CanonRuntime {
count: if g.count > 0 {
g.count
} else {
p.count
},
rebalance_ms: g.rebalance_ms.unwrap_or(p.rebalance_ms),
min_idle_ms: g.min_idle_ms.unwrap_or(p.min_idle_ms),
holds_check_interval_ms: g
.holds_check_interval_ms
.unwrap_or(p.holds_check_interval_ms),
drain_timeout_ms: g.drain_timeout_ms.unwrap_or(p.drain_timeout_ms),
handler_timeout_ms: g.handler_timeout_ms.unwrap_or(p.handler_timeout_ms),
max_redeliver: g.max_redeliver.unwrap_or(p.max_redeliver),
poison_policy: g.poison_policy.unwrap_or(p.poison_policy),
batch_size: g.batch_size.unwrap_or(s.batch_size),
poll_timeout_ms: g.poll_timeout_ms.unwrap_or(s.poll_timeout_ms).max(1),
inflight_max: g.inflight_max.unwrap_or(s.inflight_max),
},
},
);
}
serde_json::to_string(&CanonPartition {
default,
groups,
})
.expect("canonical partition json 序列化不应失败")
}
fn legacy_marker_matches(existing: &str, cfg: &RedisConfig) -> bool {
let Ok(v) = serde_json::from_str::<serde_json::Value>(existing) else {
return false;
};
match (
v.get("profile").and_then(|x| x.as_str()),
v.get("naming_config_hash").and_then(|x| x.as_str()),
) {
(Some(prof), Some(hash)) => {
if prof != cfg.profile.wire_name() {
return false;
}
let canon = naming_config_canonical(cfg);
let crc = |s: &str| format!("{:04x}", crate::keytag::crc16(s.as_bytes()));
hash == crc(&canon)
}
_ => false,
}
}
fn marker_payload(cfg: &RedisConfig) -> String {
serde_json::json!({
"profile": cfg.profile.wire_name(),
"naming_config": naming_config_canonical(cfg),
})
.to_string()
}
fn marker_semantically_equal(existing: &str, payload: &str) -> bool {
match (
serde_json::from_str::<serde_json::Value>(existing),
serde_json::from_str::<serde_json::Value>(payload),
) {
(Ok(a), Ok(b)) => a == b,
_ => existing == payload,
}
}
pub struct RedisClient {
cfg: RedisConfig,
conn: Conn,
raw: redis::Client,
is_cluster: bool,
pipe_cell: tokio::sync::OnceCell<Conn>,
#[cfg(feature = "job")]
job_control_cell: tokio::sync::OnceCell<Conn>,
#[cfg(feature = "job")]
job_heartbeat_cell: tokio::sync::OnceCell<Conn>,
#[cfg(feature = "job")]
job_metrics: RwLock<Option<std::sync::Weak<crate::job::metrics::JobMetrics>>>,
pub(crate) idempotent:
std::sync::OnceLock<crate::idempotent::runtime::IdempotentCounterRuntime>,
}
async fn await_with_deadline<F, T>(
fut: F,
ms: u64,
what: &str,
) -> std::result::Result<T, redis::RedisError>
where
F: std::future::Future<Output = std::result::Result<T, redis::RedisError>>,
{
if ms == 0 {
return fut.await;
}
match tokio::time::timeout(Duration::from_millis(ms), fut).await {
Ok(r) => r,
Err(_) => Err(redis::RedisError::from((
redis::ErrorKind::Io,
"建连超时",
format!("{what} 超过 {ms}ms 未就绪(Redis 不可达?)"),
))),
}
}
fn parse_cluster_mode(info: &str) -> std::result::Result<bool, redis::RedisError> {
let mut modes = info
.lines()
.filter_map(|line| line.strip_prefix("cluster_enabled:"));
match (modes.next(), modes.next()) {
(Some("0"), None) => Ok(false),
(Some("1"), None) => Ok(true),
_ => Err(redis::RedisError::from((
redis::ErrorKind::Parse,
"INFO cluster requires exactly one cluster_enabled field with value 0 or 1",
))),
}
}
fn connect_probe(
stage: &'static str,
cfg: &RedisConfig,
source: redis::RedisError,
) -> NasaRedisError {
NasaRedisError::ConnectProbe {
stage,
endpoint: cfg.safe_endpoint(),
source,
}
}
impl RedisClient {
pub async fn connect(cfg: RedisConfig) -> Result<Arc<RedisClient>> {
cfg.validate()?;
let raw =
redis::Client::open(cfg.url.as_str()).map_err(|e| connect_probe("connect", &cfg, e))?;
let resp_timeout = cfg.command.response_timeout_ms;
let mgr_cfg = {
let mut c = redis::aio::ConnectionManagerConfig::new();
if resp_timeout > 0 {
let d = Duration::from_millis(resp_timeout);
c = c
.set_response_timeout(Some(d))
.set_connection_timeout(Some(d));
}
c
};
let mut bootstrap = await_with_deadline(
ConnectionManager::new_with_config(raw.clone(), mgr_cfg),
resp_timeout,
"ConnectionManager 建连",
)
.await
.map_err(|e| connect_probe("connect", &cfg, e))?;
let is_cluster = {
let mut last_err = None;
let mut detected = None;
for attempt in 0..3 {
match redis::cmd("INFO")
.arg("cluster")
.query_async::<String>(&mut bootstrap)
.await
{
Ok(info) => {
detected = Some(
parse_cluster_mode(&info)
.map_err(|error| connect_probe("topology", &cfg, error))?,
);
break;
}
Err(e) => {
tracing::warn!(attempt, err = %e, "`INFO cluster` 失败(连接故障),重试");
last_err = Some(e);
if resp_timeout > 0 {
tokio::time::sleep(Duration::from_millis(
(resp_timeout / 3).clamp(50, 500),
))
.await;
}
}
}
}
match detected {
Some(b) => b,
None => {
return Err(match last_err {
Some(e) => connect_probe("topology", &cfg, e),
None => NasaRedisError::Config(
"`INFO cluster` 拓扑探测失败且无底层错误可用(不应发生)".into(),
),
});
}
}
};
let mut conn = if is_cluster {
let mut builder = redis::cluster::ClusterClient::builder(vec![cfg.url.as_str()]);
if resp_timeout > 0 {
let d = Duration::from_millis(resp_timeout);
builder = builder.response_timeout(d).connection_timeout(d);
}
let cc = builder
.build()
.map_err(|e| connect_probe("cluster-discovery", &cfg, e))?;
tracing::info!(namespace = %cfg.namespace, "目标 Redis 为 Cluster 模式,启用 cluster_async transport(跟随 MOVED/ASK)");
Conn::Cluster(
await_with_deadline(cc.get_async_connection(), resp_timeout, "cluster 建连")
.await
.map_err(|e| connect_probe("cluster-discovery", &cfg, e))?,
)
} else {
Conn::Single(bootstrap)
};
let key = marker_key(&cfg.namespace);
let payload = marker_payload(&cfg);
let created: bool = redis::cmd("SET")
.arg(&key)
.arg(&payload)
.arg("NX")
.query_async::<Option<String>>(&mut conn)
.await
.map_err(|e| connect_probe("protocol-marker", &cfg, e))?
.is_some();
if !created {
let existing: String = conn
.get(&key)
.await
.map_err(|e| connect_probe("protocol-marker", &cfg, e))?;
if !marker_semantically_equal(&existing, &payload)
&& !legacy_marker_matches(&existing, &cfg)
{
return Err(NasaRedisError::ProtocolMarker(format!(
"namespace={} 既有标记 {existing} 与本节点 {payload} 不一致——profile 或命名配置不符,拒绝启动",
cfg.namespace
)));
}
} else {
tracing::info!(namespace = %cfg.namespace, profile = ?cfg.profile, "协议标记已创建");
}
Ok(Arc::new(RedisClient {
cfg,
conn,
raw,
is_cluster,
pipe_cell: tokio::sync::OnceCell::new(),
#[cfg(feature = "job")]
job_control_cell: tokio::sync::OnceCell::new(),
#[cfg(feature = "job")]
job_heartbeat_cell: tokio::sync::OnceCell::new(),
#[cfg(feature = "job")]
job_metrics: RwLock::new(None),
idempotent: std::sync::OnceLock::new(),
}))
}
pub fn is_cluster(&self) -> bool {
self.is_cluster
}
pub fn profile(&self) -> CompatibilityProfile {
self.cfg.profile
}
pub fn qualifier(&self) -> &str {
&self.cfg.qualifier
}
pub fn config(&self) -> &RedisConfig {
&self.cfg
}
pub fn conn(&self) -> Conn {
self.conn.clone()
}
pub(crate) async fn pipe_conn(&self) -> Result<Conn> {
if !self.cfg.pipeline.dedicated_conn {
return Ok(self.conn());
}
let c = self
.pipe_cell
.get_or_try_init(|| self.build_transport("pipeline 专用"))
.await?;
Ok(c.clone())
}
#[cfg(feature = "job")]
pub(crate) async fn job_pubsub_conn(
&self,
push_sender: Arc<dyn redis::aio::AsyncPushSender>,
) -> Result<Conn> {
let timeout_ms = self.cfg.command.response_timeout_ms;
let conn = if self.is_cluster {
let mut builder = redis::cluster::ClusterClient::builder(vec![self.cfg.url.as_str()])
.use_protocol(redis::ProtocolVersion::RESP3)
.push_sender(push_sender.clone());
if timeout_ms > 0 {
let timeout = Duration::from_millis(timeout_ms);
builder = builder
.response_timeout(timeout)
.connection_timeout(timeout);
}
let client = builder.build().map_err(NasaRedisError::Redis)?;
Conn::Cluster(
await_with_deadline(
client.get_async_connection(),
timeout_ms,
"Job RESP3 Cluster lane 建连",
)
.await
.map_err(NasaRedisError::Redis)?,
)
} else {
let mut info = self
.cfg
.url
.as_str()
.into_connection_info()
.map_err(NasaRedisError::Redis)?;
let redis_settings = info
.redis_settings()
.clone()
.set_protocol(redis::ProtocolVersion::RESP3);
info = info.set_redis_settings(redis_settings);
let client = redis::Client::open(info).map_err(NasaRedisError::Redis)?;
let mut manager = redis::aio::ConnectionManagerConfig::new()
.set_push_sender(push_sender)
.set_automatic_resubscription();
if timeout_ms > 0 {
let timeout = Duration::from_millis(timeout_ms);
manager = manager
.set_response_timeout(Some(timeout))
.set_connection_timeout(Some(timeout));
}
Conn::Single(
await_with_deadline(
ConnectionManager::new_with_config(client, manager),
timeout_ms,
"Job RESP3 lane 建连",
)
.await
.map_err(NasaRedisError::Redis)?,
)
};
Ok(conn)
}
#[cfg(feature = "job")]
pub(crate) async fn job_control_conn(&self) -> Result<Conn> {
let conn = self
.job_control_cell
.get_or_try_init(|| self.build_transport("Job control lane"))
.await?;
Ok(conn.clone())
}
#[cfg(feature = "job")]
pub(crate) async fn job_heartbeat_conn(&self) -> Result<Conn> {
let conn = self
.job_heartbeat_cell
.get_or_try_init(|| self.build_transport("Job heartbeat lane"))
.await?;
Ok(conn.clone())
}
async fn build_transport(&self, what: &str) -> Result<Conn> {
let resp_timeout = self.cfg.command.response_timeout_ms;
if self.is_cluster {
let mut builder = redis::cluster::ClusterClient::builder(vec![self.cfg.url.as_str()]);
if resp_timeout > 0 {
let d = Duration::from_millis(resp_timeout);
builder = builder.response_timeout(d).connection_timeout(d);
}
let cc = builder.build()?;
let conn = await_with_deadline(
cc.get_async_connection(),
resp_timeout,
&format!("{what} cluster 建连"),
)
.await?;
Ok(Conn::Cluster(conn))
} else {
let mut mgr_cfg = redis::aio::ConnectionManagerConfig::new();
if resp_timeout > 0 {
let d = Duration::from_millis(resp_timeout);
mgr_cfg = mgr_cfg
.set_response_timeout(Some(d))
.set_connection_timeout(Some(d));
}
let conn = await_with_deadline(
ConnectionManager::new_with_config(self.raw.clone(), mgr_cfg),
resp_timeout,
&format!("{what} 建连"),
)
.await?;
Ok(Conn::Single(conn))
}
}
pub(crate) fn raw_client(&self) -> &redis::Client {
&self.raw
}
pub(crate) async fn dedicated_conn(&self, what: &str) -> Result<Conn> {
self.build_transport(what).await
}
#[cfg(feature = "job")]
pub(crate) fn attach_job_metrics(&self, metrics: &Arc<crate::job::metrics::JobMetrics>) {
*self
.job_metrics
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::downgrade(metrics));
}
#[cfg(feature = "job")]
pub(crate) fn record_job_script_reload(&self) {
let metrics = self
.job_metrics
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_ref()
.and_then(std::sync::Weak::upgrade);
if let Some(metrics) = metrics {
metrics.add("redis_job_script_reload_total", 1);
}
}
pub async fn execute_raw<T: redis::FromRedisValue>(&self, cmd: &redis::Cmd) -> Result<T> {
let mut c = self.conn();
self.timed(cmd.query_async(&mut c)).await
}
pub async fn ping(&self) -> Result<()> {
self.execute_raw::<redis::Value>(&redis::cmd("PING"))
.await
.map(|_| ())
}
pub async fn shutdown(&self) {
tracing::info!(namespace = %self.cfg.namespace, "RedisClient shutdown");
}
}
#[derive(Default)]
pub struct RedisRegistry {
map: RwLock<HashMap<String, Arc<RedisClient>>>,
}
impl RedisRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register(&self, qualifier: &str, client: Arc<RedisClient>) -> Result<()> {
if qualifier != client.qualifier() {
return Err(NasaRedisError::Config(format!(
"注册表 key {qualifier} 与 RedisClient qualifier {} 不一致",
client.qualifier()
)));
}
let mut m = self.map.write().unwrap_or_else(|e| e.into_inner());
if m.contains_key(qualifier) {
return Err(NasaRedisError::Config(format!(
"qualifier 重复注册: {qualifier}"
)));
}
m.insert(qualifier.to_string(), client);
Ok(())
}
pub fn get(&self, qualifier: &str) -> Option<Arc<RedisClient>> {
self.map
.read()
.unwrap_or_else(|e| e.into_inner())
.get(qualifier)
.cloned()
}
pub fn deregister(&self, qualifier: &str) -> Option<Arc<RedisClient>> {
self.map
.write()
.unwrap_or_else(|e| e.into_inner())
.remove(qualifier)
}
}