use std::sync::Arc;
use serde::{Deserialize, Serialize};
use super::KeyLayout;
use crate::client::RedisClient;
use crate::error::{NasaRedisError, Result};
pub fn marker_key(layout: &KeyLayout, p: u32, op_id: &str) -> String {
format!("{}:retryop:{}:{}", layout.prefix, p, op_id)
}
pub fn operation_id(layout: &KeyLayout, consumer: &str, claim_term: u64, ids: &[String]) -> String {
let mut sorted: Vec<&str> = ids.iter().map(|s| s.as_str()).collect();
sorted.sort_unstable();
let payload = format!(
"{}|{}|{}|{}",
sorted.join(","),
layout.group(),
consumer,
claim_term
);
crate::keytag::fnv1a64_hex(payload.as_bytes())
}
fn parse_record_id(id: &str) -> Option<(u64, u64)> {
let (millis, sequence) = id.split_once('-')?;
if millis.is_empty()
|| sequence.is_empty()
|| !millis.bytes().all(|byte| byte.is_ascii_digit())
|| !sequence.bytes().all(|byte| byte.is_ascii_digit())
{
return None;
}
Some((millis.parse().ok()?, sequence.parse().ok()?))
}
const RETRYOP_MARKER_TTL: std::time::Duration = std::time::Duration::from_secs(600);
const MAX_RETRY_COUNT: u64 = 9_007_199_254_740_991;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryIntent {
pub id: String,
pub desired: u64,
}
#[derive(Debug)]
pub enum RetryOutcome {
Claimed(Vec<u8>),
Have(Vec<u8>),
Resolved,
OwnershipChanged,
Superseded,
Corrupt,
}
pub struct FenceArgs<'a> {
pub holder: &'a str,
pub lock_key: &'a str,
pub fence_key: &'a str,
pub round: u64,
pub nonce: &'a str,
pub epoch: u64,
pub counter: u64,
}
const RETRY_CAS_LUA: &str = r#"
-- owner/fence 前缀:KEYS[2]=lock KEYS[3]=fence;ARGV[5]=holder ARGV[6]=has_fence
-- ARGV[7]=round、ARGV[8]=nonce、ARGV[9]=p、ARGV[10]=counter、ARGV[11]=epoch。
-- 修改投递次数同样需要完整权威,不能只依赖仍相同的 PEL consumer。
if redis.call('hexists', KEYS[2], ARGV[5]) == 0 then
return {'OWNER'}
end
if ARGV[6] == '1' then
if redis.call('HGET', KEYS[3], 'state') ~= 'Ready' then
return {'OWNER'}
end
if redis.call('HGET', KEYS[3], 'round') ~= ARGV[7] then
return {'OWNER'}
end
if redis.call('HGET', KEYS[3], 'nonce') ~= ARGV[8] then
return {'OWNER'}
end
if redis.call('HGET', KEYS[3], 'c:' .. ARGV[9]) ~= ARGV[10] then
return {'OWNER'}
end
local epoch = redis.call('HGET', KEYS[3], 'epoch')
if epoch == false or string.match(epoch, '^%d+$') == nil then
return {'OWNER'}
end
local normalized_epoch = string.gsub(epoch, '^0+', '')
if normalized_epoch == '' then
normalized_epoch = '0'
end
if normalized_epoch ~= ARGV[11] then
return {'OWNER'}
end
end
-- XPENDING 的整数进入 Lua 后已经转为双精度数,超出精确范围的目标不能参与相等和前驱判定。
if string.match(ARGV[4], '^%d+$') == nil or string.sub(ARGV[4], 1, 1) == '0' or
string.len(ARGV[4]) > 16 or (string.len(ARGV[4]) == 16 and ARGV[4] > '9007199254740991') then
return {'CORRUPT'}
end
local pend = redis.call('XPENDING', KEYS[1], ARGV[1], ARGV[3], ARGV[3], 1)
if #pend == 0 then
return {'RESOLVED'}
end
-- XPENDING 详式返回 {id, consumer, idle, delivery-count}
local owner = pend[1][2]
local count = pend[1][4]
local desired = tonumber(ARGV[4])
if owner ~= ARGV[2] then
return {'OWNER'}
end
if type(count) ~= 'number' or count < 0 or count % 1 ~= 0 then
return {'CORRUPT'}
end
-- 目标严格低于双精度舍入边界,即使更大实际次数已被舍入,仍能可靠判为被后继替代。
if count > desired then
return {'SUPERSEDED'}
end
if count < desired - 1 then
return {'CORRUPT'}
end
-- 业务作用:读取同一原子步骤内的精确记录正文,避免用其它坐标代替重投目标。
-- 参数说明:无。
-- 返回:存在 data 字段时返回原始字节,否则返回 nil,由调用方继续区分墓碑与缺字段记录。
local function payload_of()
local rows = redis.call('XRANGE', KEYS[1], ARGV[3], ARGV[3])
if #rows == 0 then
return nil
end
local fields = rows[1][2]
for i = 1, #fields, 2 do
if fields[i] == 'data' then
return fields[i + 1]
end
end
return nil
end
if count == desired then
local pl = payload_of()
if pl == nil then
return {'RESOLVED'}
end
return {'HAVE', pl}
end
-- 仅前驱次数才能递增;发送原十进制参数,避免 Lua 数字转文本再次改变目标值。
local claimed = redis.call('XCLAIM', KEYS[1], ARGV[1], ARGV[2], 0, ARGV[3], 'RETRYCOUNT', ARGV[4])
if #claimed == 0 then
return {'RESOLVED'}
end
local pl = payload_of()
if pl == nil then
return {'RESOLVED'}
end
return {'CLAIMED', pl}
"#;
pub async fn ensure_pending(
client: &Arc<RedisClient>,
layout: &KeyLayout,
p: u32,
op_id: &str,
ids: &[String],
) -> Result<Vec<RetryIntent>> {
let mut requested = std::collections::BTreeSet::new();
for id in ids {
let coordinate = parse_record_id(id).ok_or_else(|| {
NasaRedisError::ProtocolMarker("retry-op 请求 ID 不完整或越界".into())
})?;
if !requested.insert(coordinate) {
return Err(NasaRedisError::ProtocolMarker(
"retry-op 请求包含重复坐标".into(),
));
}
}
let identities: Vec<_> = requested
.iter()
.map(|(millis, sequence)| format!("{millis}-{sequence}"))
.collect();
let request_json = serde_json::to_string(&identities)
.map_err(|_| NasaRedisError::Codec("retry-op 请求编码失败".into()))?;
let mkey = marker_key(layout, p, op_id);
let script = redis::Script::new(ENSURE_PENDING_LUA);
let reply = script
.key(&mkey)
.arg("load")
.arg("")
.arg(&request_json)
.arg(RETRYOP_MARKER_TTL.as_millis() as u64)
.invoke_async(&mut client.conn())
.await?;
if let Some(intents) = parse_pending_reply(reply, &requested)? {
return Ok(intents);
}
let mut intents = Vec::with_capacity(ids.len());
for id in ids {
if let Some(count) = actual_count(client, layout, p, id).await? {
let desired = count
.checked_add(1)
.filter(|desired| *desired <= MAX_RETRY_COUNT)
.ok_or_else(|| {
NasaRedisError::ProtocolMarker("retry-op 目标次数超过精确比较范围".into())
})?;
intents.push(RetryIntent {
id: id.clone(),
desired,
});
}
}
let json = serde_json::to_string(&intents)
.map_err(|_| NasaRedisError::Codec("retry-op 意图编码失败".into()))?;
let reply = script
.key(&mkey)
.arg("create")
.arg(&json)
.arg(&request_json)
.arg(RETRYOP_MARKER_TTL.as_millis() as u64)
.invoke_async(&mut client.conn())
.await?;
parse_pending_reply(reply, &requested)?
.ok_or_else(|| NasaRedisError::ProtocolMarker("retry-op 登记未返回持久意图".into()))
}
const ENSURE_PENDING_LUA: &str = r#"
-- 残缺或无期限的已有标记不能覆盖,否则迟到登记可能改变已经执行过的目标次数。
if redis.call('EXISTS', KEYS[1]) ~= 0 then
local state = redis.call('HGET', KEYS[1], 'state')
local intents = redis.call('HGET', KEYS[1], 'ids')
if state ~= 'Pending' or intents == false or redis.call('PTTL', KEYS[1]) <= 0 then
return {'CORRUPT'}
end
return {'FOUND', intents, redis.call('HGET', KEYS[1], 'request_ids')}
end
if ARGV[1] == 'load' then
return {'MISSING'}
end
if ARGV[1] ~= 'create' then
return {'CORRUPT'}
end
-- 全部已出 PEL 时没有需要持久化的意图,但仍须先检查是否存在并发登记的原操作。
if ARGV[2] == '[]' then
return {'CREATED', ARGV[2], ARGV[3]}
end
redis.call('HSET', KEYS[1], 'ids', ARGV[2], 'request_ids', ARGV[3], 'state', 'Pending')
redis.call('PEXPIRE', KEYS[1], ARGV[4])
return {'CREATED', ARGV[2], ARGV[3]}
"#;
fn parse_pending_reply(
reply: redis::Value,
requested: &std::collections::BTreeSet<(u64, u64)>,
) -> Result<Option<Vec<RetryIntent>>> {
let invalid = || NasaRedisError::ProtocolMarker("retry-op 标记响应或持久意图非法".into());
let redis::Value::Array(fields) = reply else {
return Err(invalid());
};
if let [redis::Value::BulkString(tag)] = fields.as_slice() {
return if tag == b"MISSING" {
Ok(None)
} else {
Err(invalid())
};
}
let [redis::Value::BulkString(tag), redis::Value::BulkString(json), request_json] =
fields.as_slice()
else {
return Err(invalid());
};
if tag != b"FOUND" && tag != b"CREATED" {
return Err(invalid());
}
let intents: Vec<RetryIntent> = serde_json::from_slice(json).map_err(|_| invalid())?;
if tag == b"FOUND" && intents.is_empty() {
return Err(invalid());
}
let mut stored = std::collections::BTreeSet::new();
for intent in &intents {
let id = parse_record_id(&intent.id).ok_or_else(invalid)?;
if !stored.insert(id) || intent.desired == 0 || intent.desired > MAX_RETRY_COUNT {
return Err(invalid());
}
}
match request_json {
redis::Value::BulkString(json) => {
let identities: Vec<String> = serde_json::from_slice(json).map_err(|_| invalid())?;
let mut declared = std::collections::BTreeSet::new();
for id in identities {
if !declared.insert(parse_record_id(&id).ok_or_else(invalid)?) {
return Err(invalid());
}
}
if &declared != requested || !stored.is_subset(&declared) {
return Err(invalid());
}
}
redis::Value::Nil if tag == b"FOUND" && &stored == requested => {}
_ => return Err(invalid()),
}
Ok(Some(intents))
}
pub async fn execute(
client: &Arc<RedisClient>,
layout: &KeyLayout,
p: u32,
consumer: &str,
intents: &[RetryIntent],
fence: &FenceArgs<'_>,
) -> Result<Vec<(String, RetryOutcome)>> {
let mut identities = std::collections::BTreeSet::new();
for intent in intents {
let id = parse_record_id(&intent.id).ok_or_else(|| {
NasaRedisError::ProtocolMarker("retry-op 意图 ID 不完整或越界".into())
})?;
if !identities.insert(id) || intent.desired == 0 || intent.desired > MAX_RETRY_COUNT {
return Err(NasaRedisError::ProtocolMarker(
"retry-op 意图重复或目标次数不可精确比较".into(),
));
}
}
if intents.is_empty() {
return Ok(Vec::new());
}
if fence.holder.is_empty()
|| fence.lock_key.is_empty()
|| (!fence.fence_key.is_empty()
&& (fence.round == 0
|| fence.nonce.is_empty()
|| fence.counter == 0
|| fence.counter > i64::MAX as u64))
{
return Err(NasaRedisError::ProtocolMarker(
"retry-op 来源持有权凭据不完整".into(),
));
}
let stream = layout.stream(p);
let lua = redis::Script::new(RETRY_CAS_LUA);
let has_fence = if fence.fence_key.is_empty() {
"0"
} else {
"1"
};
let mut out = Vec::with_capacity(intents.len());
for it in intents {
let v: redis::Value = lua
.key(&stream)
.key(fence.lock_key)
.key(if fence.fence_key.is_empty() {
stream.as_str()
} else {
fence.fence_key
})
.arg(layout.group())
.arg(consumer)
.arg(&it.id)
.arg(it.desired)
.arg(fence.holder)
.arg(has_fence)
.arg(fence.round.to_string())
.arg(fence.nonce)
.arg(p.to_string())
.arg(fence.counter.to_string())
.arg(fence.epoch)
.invoke_async(&mut client.conn())
.await?;
let outcome = parse_outcome(v);
if matches!(outcome, RetryOutcome::Corrupt) {
tracing::error!(p, id = %it.id, "retry-op 响应或计数状态异常,停止本条自动修改");
}
out.push((it.id.clone(), outcome));
}
Ok(out)
}
pub async fn actual_count(
client: &Arc<RedisClient>,
layout: &KeyLayout,
p: u32,
id: &str,
) -> Result<Option<u64>> {
let expected = parse_record_id(id)
.ok_or_else(|| NasaRedisError::ProtocolMarker("retry-op 记录 ID 不完整或越界".into()))?;
let v: redis::Value = redis::cmd("XPENDING")
.arg(layout.stream(p))
.arg(layout.group())
.arg(id)
.arg(id)
.arg(1)
.query_async(&mut client.conn())
.await?;
let invalid = || NasaRedisError::ProtocolMarker("retry-op XPENDING 响应形态或字段非法".into());
let redis::Value::Array(rows) = v else {
return Err(invalid());
};
if rows.is_empty() {
return Ok(None);
}
let [redis::Value::Array(row)] = rows.as_slice() else {
return Err(invalid());
};
let [redis::Value::BulkString(returned), redis::Value::BulkString(consumer), redis::Value::Int(idle), redis::Value::Int(count)] =
row.as_slice()
else {
return Err(invalid());
};
if std::str::from_utf8(returned).ok().and_then(parse_record_id) != Some(expected)
|| std::str::from_utf8(consumer).is_err()
|| *idle < 0
{
return Err(invalid());
}
u64::try_from(*count).map(Some).map_err(|_| invalid())
}
pub async fn finish(
client: &Arc<RedisClient>,
layout: &KeyLayout,
p: u32,
op_id: &str,
) -> Result<()> {
client.del(&[&marker_key(layout, p, op_id)]).await?;
Ok(())
}
fn parse_outcome(v: redis::Value) -> RetryOutcome {
let redis::Value::Array(mut arr) = v else {
return RetryOutcome::Corrupt;
};
let tag = match arr.first() {
Some(redis::Value::BulkString(bytes)) => std::str::from_utf8(bytes).ok(),
Some(redis::Value::SimpleString(value)) => Some(value.as_str()),
_ => None,
};
match tag {
Some("CLAIMED" | "HAVE") if arr.len() == 2 => {
let claimed = tag == Some("CLAIMED");
let payload = match arr.pop() {
Some(redis::Value::BulkString(bytes)) => bytes,
_ => return RetryOutcome::Corrupt,
};
if claimed {
RetryOutcome::Claimed(payload)
} else {
RetryOutcome::Have(payload)
}
}
Some("RESOLVED") if arr.len() == 1 => RetryOutcome::Resolved,
Some("OWNER") if arr.len() == 1 => RetryOutcome::OwnershipChanged,
Some("SUPERSEDED") if arr.len() == 1 => RetryOutcome::Superseded,
_ => RetryOutcome::Corrupt,
}
}