use std::collections::BTreeSet;
use std::sync::Arc;
use base64::Engine;
use redis::cluster_routing::{MultipleNodeRoutingInfo, RoutingInfo, SingleNodeRoutingInfo};
use redis::streams::StreamRangeReply;
use crate::client::{Conn, RedisClient};
use crate::error::{NasaRedisError, Result};
use crate::job::config::JobConfig;
use crate::job::definition::JobDefinition;
use crate::job::identifiers::{manual_run_id, scheduled_run_id};
use crate::job::keyspace::JobKeyspace;
use crate::job::model::{JobDefinitionState, JobResultCode, JobState};
use crate::job::names::require_name;
use crate::job::payload::JobPayload;
use crate::job::run::JobRun;
use crate::job::script::{eval, value_to_string, JobScript};
use crate::partition::compat_string_hash;
static JOB_REGISTER: JobScript = JobScript::new(include_str!("lua/job_register.lua"));
static JOB_LAYOUT: JobScript = JobScript::new(include_str!("lua/job_layout.lua"));
static MANUAL_FIRE: JobScript = JobScript::new(include_str!("lua/manual_fire.lua"));
static READ_RUN: JobScript = JobScript::new(include_str!("lua/read_run.lua"));
static SCAN_DUE: JobScript = JobScript::new(include_str!("lua/scan_due.lua"));
static FIRE_DUE: JobScript = JobScript::new(include_str!("lua/fire_due.lua"));
static FIRE_DUE_BATCH: JobScript = JobScript::new(include_str!("lua/fire_due_batch.lua"));
static RENEW_BATCH: JobScript = JobScript::new(include_str!("lua/renew_batch.lua"));
static START_RUN: JobScript = JobScript::new(include_str!("lua/start_run.lua"));
static RENEW_RUN: JobScript = JobScript::new(include_str!("lua/renew_run.lua"));
static FINISH_RUN: JobScript = JobScript::new(include_str!("lua/finish_run.lua"));
static RECOVER_EXPIRED: JobScript = JobScript::new(include_str!("lua/recover_expired.lua"));
static DEFER_RUN: JobScript = JobScript::new(include_str!("lua/defer_run.lua"));
static PROMOTE_VISIBLE: JobScript = JobScript::new(include_str!("lua/promote_visible.lua"));
static REQUEST_CANCEL: JobScript = JobScript::new(include_str!("lua/request_cancel.lua"));
static FAIL_WAITING_CREATION: JobScript =
JobScript::new(include_str!("lua/fail_waiting_creation.lua"));
static JOB_PAUSE: JobScript = JobScript::new(include_str!("lua/job_pause.lua"));
static JOB_RESUME: JobScript = JobScript::new(include_str!("lua/job_resume.lua"));
static JOB_DELETE: JobScript = JobScript::new(include_str!("lua/job_delete.lua"));
static JOB_RESOLVE_CONFLICT: JobScript =
JobScript::new(include_str!("lua/job_resolve_conflict.lua"));
static NAMESPACE_SET_STATE: JobScript = JobScript::new(include_str!("lua/namespace_set_state.lua"));
static JOB_CLEANUP_TOMBSTONES: JobScript =
JobScript::new(include_str!("lua/job_cleanup_tombstones.lua"));
static JOB_REAP: JobScript = JobScript::new(include_str!("lua/job_reap.lua"));
static PREPARE_FANOUT_ROOT: JobScript = JobScript::new(include_str!("lua/prepare_fanout_root.lua"));
static FINISH_FANOUT_ROOT: JobScript = JobScript::new(include_str!("lua/finish_fanout_root.lua"));
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ManualFireOutcome {
Fired {
run_id: String,
message_id: String,
},
Adopted {
run_id: String,
},
NotFound,
StateMismatch,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrepareFanoutRootOutcome {
Prepared {
redis_now: i64,
deadline: i64,
},
Adopted {
deadline: i64,
},
StateMismatch,
StaleOwner,
JobDeleted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FinishFanoutRootOutcome {
Ok {
state: JobState,
redis_now_or_deadline: i64,
wake_delay_ms: Option<i64>,
},
Adopted,
Stale,
AlreadyCompleted {
state: JobState,
},
StateMismatch,
JobDeleted {
state: JobState,
deadline: i64,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefinitionControlOutcome {
Ok,
NotFound,
StateMismatch,
Stale,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeleteOutcome {
Ok {
redis_now: i64,
},
NotFound,
Stale {
revision: i64,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamespaceShardSnapshot {
pub shard: u32,
pub state: Option<String>,
pub updated_at_ms: Option<i64>,
pub updated_by: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NamespaceStateOutcome {
Ok {
redis_now: i64,
},
Invalid,
}
#[derive(Debug, Clone)]
pub struct FireDueBatchItem<'a> {
pub definition: &'a JobDefinition,
pub logical_fire_at: i64,
pub proposed_next_fire_at: i64,
pub skip_run: bool,
pub misfire: bool,
pub must_end_in_future: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FireDueBatchResult {
pub run_id: String,
pub code: String,
pub message_id: String,
}
#[derive(Debug, Clone)]
pub struct RenewBatchItem {
pub record_key: String,
pub leases_key: String,
pub waiting_key: String,
pub member: String,
pub attempt_token: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenewBatchResult {
pub code: String,
pub deadline: i64,
pub cancel_requested: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DueEntry {
pub job_name: String,
pub logical_fire_at: i64,
pub definition_revision: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScheduleDueScan {
pub redis_now: i64,
pub next_score: Option<i64>,
pub namespace_enabled: bool,
pub entries: Vec<DueEntry>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DueIndexEntry {
pub member: String,
pub score: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DueIndexScan {
pub redis_now: i64,
pub next_score: Option<i64>,
pub entries: Vec<DueIndexEntry>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FireDueOutcome {
Fired {
run_id: String,
redis_now: i64,
message_id: String,
},
Adopted {
run_id: String,
},
Skipped {
redis_now: i64,
next_fire_at: i64,
},
NotDue {
redis_now: i64,
},
NeedRecompute {
redis_now: i64,
},
StateMismatch,
Stale,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StartOutcome {
Started {
attempt: i64,
attempt_token: i64,
redis_now: i64,
lease_until: i64,
},
Adopted {
attempt: i64,
attempt_token: i64,
redis_now: i64,
lease_until: i64,
},
LeaseExpired,
JobDeleted,
NotCommitted,
StaleAssignment,
NotFound,
Deferred,
ProtocolUnsupported,
SourceMismatch,
IdentityMismatch,
ContractMismatch,
Conflict,
Stale,
Cancelled,
Skipped,
Blocked,
FencingRegression,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RenewOutcome {
Ok {
redis_now: i64,
deadline: i64,
cancel_requested: bool,
},
StateMismatch,
StaleOwner,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FinishOutcome {
Ok {
next_state: JobState,
redis_now: i64,
wake_delay_ms: Option<i64>,
},
StateMismatch,
StaleOwner,
StaleAssignment,
IdentityMismatch,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoverOutcome {
Ok {
next_state: JobState,
redis_now: i64,
wake_delay_ms: Option<i64>,
},
NotDue {
lease_until: i64,
},
Stale,
RootNotCancelling,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Promotion {
pub promoted: i64,
pub next_score: Option<i64>,
pub redis_now: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CancelOutcome {
Ok {
wake_delay_ms: Option<i64>,
},
NotFound,
AlreadyCompleted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FailWaitingOutcome {
Ok {
next_state: JobState,
redis_now: i64,
wake_delay_ms: Option<i64>,
},
StateMismatch,
NotDue {
deadline: i64,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeferOutcome {
Deferred {
delay_ms: i64,
},
Stale,
JobDeleted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobRegisterOutcome {
Registered {
revision: i64,
},
Adopted {
revision: i64,
},
Stale {
revision: i64,
},
Conflict {
revision: i64,
},
Deleted {
revision: i64,
},
SourceMismatch {
current_source: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompletionTrimOutcome {
pub length_trimmed: i64,
pub retention_trimmed: i64,
pub oldest_id: Option<String>,
pub redis_now: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobReapOutcome {
pub reaped: i64,
pub active: bool,
pub(crate) fanout_requests: Vec<JobReapFanoutRequest>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct JobReapFanoutRequest {
pub(crate) job_name: String,
pub(crate) run_id: String,
pub(crate) fanout_id: String,
pub(crate) root_attempt: i64,
}
#[derive(Clone)]
pub struct JobRepository {
client: Arc<RedisClient>,
keyspace: JobKeyspace,
config: Arc<JobConfig>,
}
impl JobRepository {
pub(crate) fn shard_count(&self) -> u32 {
self.keyspace.shard_count()
}
pub(crate) fn schedule_shard(&self, job_name: &str) -> u32 {
self.keyspace.schedule_shard(job_name)
}
pub fn new(client: Arc<RedisClient>, keyspace: JobKeyspace, config: Arc<JobConfig>) -> Self {
Self {
client,
keyspace,
config,
}
}
pub async fn trim_completion(&self, shard: u32) -> Result<CompletionTrimOutcome> {
self.trim_completion_key(&self.keyspace.completion(shard))
.await
}
pub async fn trim_fanout_completion(&self, bucket: u32) -> Result<CompletionTrimOutcome> {
self.trim_completion_key(&self.keyspace.fanout_completion_at(bucket))
.await
}
async fn trim_completion_key(&self, key: &str) -> Result<CompletionTrimOutcome> {
let mut connection = self.client.conn();
let (seconds, micros): (i64, i64) = redis::cmd("TIME")
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)?;
let redis_now = seconds.saturating_mul(1_000).saturating_add(micros / 1_000);
let length_trimmed: i64 = redis::cmd("XTRIM")
.arg(key)
.arg("MAXLEN")
.arg("~")
.arg(self.config.completion_max_len)
.arg("LIMIT")
.arg(self.config.completion_trim_batch_size)
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)?;
let retention_ms = i64::try_from(self.config.completion_retention_ms).unwrap_or(i64::MAX);
let cutoff = redis_now.saturating_sub(retention_ms).max(0);
let cutoff_id = format!("{cutoff}-0");
let expired: StreamRangeReply = redis::cmd("XRANGE")
.arg(key)
.arg("-")
.arg(format!("({cutoff_id}"))
.arg("COUNT")
.arg(self.config.completion_trim_batch_size.saturating_add(1))
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)?;
let batch = self.config.completion_trim_batch_size as usize;
let retention_trimmed = if expired.ids.is_empty() {
0
} else {
let boundary = expired
.ids
.get(batch)
.map(|entry| entry.id.clone())
.unwrap_or(cutoff_id);
redis::cmd("XTRIM")
.arg(key)
.arg("MINID")
.arg("=")
.arg(boundary)
.query_async::<i64>(&mut connection)
.await
.map_err(NasaRedisError::Redis)?
};
let oldest: StreamRangeReply = redis::cmd("XRANGE")
.arg(key)
.arg("-")
.arg("+")
.arg("COUNT")
.arg(1)
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)?;
Ok(CompletionTrimOutcome {
length_trimmed,
retention_trimmed,
oldest_id: oldest.ids.first().map(|entry| entry.id.clone()),
redis_now,
})
}
pub async fn confirm_layout(&self) -> Result<()> {
let fingerprint = format!(
"{}|{}|{}|{}|{}|{}|{}",
self.config.protocol_version,
self.keyspace.qualifier(),
self.keyspace.namespace(),
self.keyspace.shard_count(),
self.keyspace.fanout_bucket_count(),
self.config.dispatch_group,
self.config.pubsub_mode.wire_name(),
);
let raw = eval(
&self.client,
&JOB_LAYOUT,
&[self.keyspace.layout_marker().into_bytes()],
&[fingerprint.as_bytes().to_vec()],
)
.await?;
match raw
.first()
.map(value_to_string)
.unwrap_or_default()
.as_str()
{
"OK" => {}
"MISMATCH" => Err(crate::job::JobError::Protocol(format!(
"Job layout 与既有 marker 不一致: qualifier={} namespace={}",
self.keyspace.qualifier(),
self.keyspace.namespace()
)))?,
_ => return Err(protocol("job_layout 返回未知码")),
}
let runtime_fingerprint = format!(
"{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}",
self.config.protocol_version,
self.config.lease_ms,
self.config.visibility_timeout_ms,
self.config.max_run_duration_ms,
self.config.max_result_summary_bytes,
self.config.max_dispatch_attempts,
self.config.max_serial_backlog,
self.config.serial_overflow_policy.wire_name(),
self.config.run_retention_ms,
self.config.completion_retention_ms,
self.config.completion_max_len,
self.config.completion_trim_batch_size,
self.config.tombstone_retention_ms,
self.config.max_parameter_bytes,
self.config.max_catch_up_runs,
self.config.max_catch_up_window_ms,
self.config.executor_expire_ms,
self.config.registry_gc_grace_ms,
self.config.ready_max_wakeups,
self.config.fanout_capacity_wait_ms,
self.config.fanout_max_assignments,
self.config.node_unready_evidence_count,
self.config.fanout_max_members,
self.config.fanout_delivery_batch_size,
self.config.fanout_create_timeout_ms,
self.config.fanout_max_wait_ms,
self.config.fanout_retention_ms,
self.config.fanout_cleanup_batch_size,
self.config.fanout_max_total_parameter_bytes,
self.config.json_default_typing,
self.config.dispatch_group,
self.config.pubsub_mode.wire_name(),
self.keyspace.qualifier(),
crate::job::trigger::CRON_SEMANTICS_ID,
crate::job::trigger::CRON_TZDB_VERSION,
);
let runtime = eval(
&self.client,
&JOB_LAYOUT,
&[self.keyspace.runtime_layout_marker().into_bytes()],
&[runtime_fingerprint.into_bytes()],
)
.await?;
match runtime
.first()
.map(value_to_string)
.unwrap_or_default()
.as_str()
{
"OK" => Ok(()),
"MISMATCH" => Err(crate::job::JobError::Protocol(format!(
"Job Rust runtime layout 与既有 marker 不一致: qualifier={} namespace={}",
self.keyspace.qualifier(),
self.keyspace.namespace()
))
.into()),
_ => Err(protocol("job_layout companion 返回未知码")),
}
}
pub async fn probe_capabilities(&self) -> Result<()> {
let mut names = vec![
"TIME",
"EVALSHA",
"EVAL",
"XAUTOCLAIM",
"XREADGROUP",
"XGROUP",
"HSET",
"ZADD",
"SADD",
];
match self.config.pubsub_mode {
crate::job::model::JobPubSubMode::Sharded => {
names.extend(["SPUBLISH", "SSUBSCRIBE", "SUNSUBSCRIBE"]);
}
crate::job::model::JobPubSubMode::Broadcast => {
names.extend(["PUBLISH", "SUBSCRIBE", "UNSUBSCRIBE"]);
}
}
let supported = match self.client.conn() {
Conn::Single(mut connection) => {
let command = command_info(&names);
command
.query_async::<redis::Value>(&mut connection)
.await
.is_ok_and(|value| command_info_supported(&value, names.len()))
}
Conn::Cluster(mut connection) => {
let mut complete = false;
for _ in 0..3 {
let Some(before) = cluster_primary_addresses(&mut connection).await else {
break;
};
let responses = connection
.route_command(
command_info(&names),
RoutingInfo::MultiNode((MultipleNodeRoutingInfo::AllMasters, None)),
)
.await;
let Some(after) = cluster_primary_addresses(&mut connection).await else {
break;
};
if before != after {
tokio::task::yield_now().await;
continue;
}
complete = responses.ok().is_some_and(|value| {
all_master_command_info_supported(&value, &before, names.len())
});
break;
}
complete
}
};
if supported {
Ok(())
} else {
Err(crate::job::JobError::Config(format!(
"RedisJob source {} 的 master 命令能力或 ACL 不完整",
self.keyspace.qualifier()
))
.into())
}
}
pub async fn manual_fire(
&self,
definition: &JobDefinition,
request_id: &str,
payload: &JobPayload,
) -> Result<ManualFireOutcome> {
let request_id = require_name(request_id, "requestId")?;
if payload.bytes().len() > self.config.max_parameter_bytes as usize {
return Err(cfg("manual fire 参数超过 max_parameter_bytes"));
}
if !definition.codecs().contains(&payload.codec()) {
return Err(cfg("manual fire 载荷线编码不在定义声明的 codecs 中"));
}
if payload.schema_id() != definition.schema_id() {
return Err(cfg("manual fire 载荷 schema_id 与定义不符"));
}
let shard = self.keyspace.schedule_shard(definition.name());
let run_id = manual_run_id(
self.keyspace.qualifier(),
self.keyspace.namespace(),
definition.name(),
&request_id,
);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.run(shard, &run_id).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
self.keyspace.completion(shard).into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let encoded = base64::engine::general_purpose::STANDARD.encode(payload.bytes());
let argv: Vec<Vec<u8>> = vec![
run_id.clone().into_bytes(),
request_id.into_bytes(),
encoded.into_bytes(),
payload.schema_id().as_bytes().to_vec(),
payload.codec().wire_name().as_bytes().to_vec(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.config.protocol_version.to_string().into_bytes(),
shard.to_string().into_bytes(),
definition.name().as_bytes().to_vec(),
];
let raw = eval(&self.client, &MANUAL_FIRE, &keys, &argv).await?;
interpret_manual_fire(&raw, run_id)
}
pub async fn read_run(&self, job_name: &str, run_id: &str) -> Result<Option<JobRun>> {
let shard = self.keyspace.schedule_shard(job_name);
let key = self.keyspace.run(shard, run_id).into_bytes();
let raw = eval(&self.client, &READ_RUN, &[key], &[]).await?;
if raw.is_empty() {
return Ok(None);
}
Ok(Some(JobRun::from_fields(&raw)?))
}
pub async fn scan_schedule_due(&self, shard: u32, limit: u32) -> Result<ScheduleDueScan> {
let keys = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
limit.to_string().into_bytes(),
b"SCHEDULE".to_vec(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &SCAN_DUE, &keys, &argv).await?;
interpret_schedule_scan(&raw)
}
pub async fn scan_leases_due(&self, shard: u32) -> Result<DueIndexScan> {
self.scan_index_due(self.keyspace.leases(shard)).await
}
pub async fn scan_waiting_due(&self, shard: u32) -> Result<DueIndexScan> {
self.scan_index_due(self.keyspace.waiting(shard)).await
}
pub async fn read_run_at_shard(&self, shard: u32, run_id: &str) -> Result<Option<JobRun>> {
let raw = eval(
&self.client,
&READ_RUN,
&[self.keyspace.run(shard, run_id).into_bytes()],
&[],
)
.await?;
if raw.is_empty() {
return Ok(None);
}
Ok(Some(JobRun::from_fields(&raw)?))
}
async fn scan_index_due(&self, key: String) -> Result<DueIndexScan> {
let argv = vec![
self.config.scan_batch_size.to_string().into_bytes(),
Vec::new(),
Vec::new(),
];
let raw = eval(&self.client, &SCAN_DUE, &[key.into_bytes()], &argv).await?;
interpret_index_scan(&raw)
}
pub async fn fire_due(
&self,
definition: &JobDefinition,
logical_fire_at: i64,
proposed_next_fire_at: i64,
skip_run: bool,
misfire: bool,
) -> Result<FireDueOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let schedule_type = definition.schedule_type().wire_name();
let trigger_type = if misfire {
"MISFIRE"
} else {
schedule_type
};
let run_id = scheduled_run_id(
self.keyspace.qualifier(),
self.keyspace.namespace(),
definition.name(),
logical_fire_at,
trigger_type,
);
let codec = definition
.codecs()
.first()
.ok_or_else(|| cfg("fire due 定义缺少线编码"))?
.wire_name();
let keys: Vec<Vec<u8>> = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.run(shard, &run_id).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
definition.definition_revision().to_string().into_bytes(),
logical_fire_at.to_string().into_bytes(),
proposed_next_fire_at.to_string().into_bytes(),
run_id.clone().into_bytes(),
schedule_type.as_bytes().to_vec(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.config.protocol_version.to_string().into_bytes(),
definition.name().as_bytes().to_vec(),
shard.to_string().into_bytes(),
codec.as_bytes().to_vec(),
schedule_type.as_bytes().to_vec(),
if skip_run {
b"SKIP".to_vec()
} else {
b"RUN".to_vec()
},
trigger_type.as_bytes().to_vec(),
];
let raw = eval(&self.client, &FIRE_DUE, &keys, &argv).await?;
interpret_fire_due(&raw, run_id)
}
pub async fn fire_due_batch(
&self,
shard: u32,
items: &[FireDueBatchItem<'_>],
) -> Result<Vec<FireDueBatchResult>> {
if items.is_empty() {
return Ok(Vec::new());
}
let mut keys: Vec<Vec<u8>> = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let mut argv: Vec<Vec<u8>> = vec![items.len().to_string().into_bytes()];
let mut run_ids: Vec<String> = Vec::with_capacity(items.len());
for item in items {
let definition = item.definition;
let schedule_type = definition.schedule_type().wire_name();
let trigger_type = if item.misfire {
"MISFIRE"
} else {
schedule_type
};
let run_id = scheduled_run_id(
self.keyspace.qualifier(),
self.keyspace.namespace(),
definition.name(),
item.logical_fire_at,
trigger_type,
);
let codec = definition
.codecs()
.first()
.ok_or_else(|| cfg("fire due batch 定义缺少线编码"))?
.wire_name();
keys.push(self.keyspace.job(shard, definition.name()).into_bytes());
keys.push(self.keyspace.run(shard, &run_id).into_bytes());
keys.push(
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
);
argv.extend([
definition.definition_revision().to_string().into_bytes(),
item.logical_fire_at.to_string().into_bytes(),
item.proposed_next_fire_at.to_string().into_bytes(),
run_id.clone().into_bytes(),
schedule_type.as_bytes().to_vec(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.config.protocol_version.to_string().into_bytes(),
definition.name().as_bytes().to_vec(),
shard.to_string().into_bytes(),
codec.as_bytes().to_vec(),
if item.skip_run {
b"SKIP".to_vec()
} else {
b"RUN".to_vec()
},
trigger_type.as_bytes().to_vec(),
if item.must_end_in_future {
b"1".to_vec()
} else {
b"0".to_vec()
},
]);
run_ids.push(run_id);
}
let raw = eval(&self.client, &FIRE_DUE_BATCH, &keys, &argv).await?;
let mut results = Vec::with_capacity(items.len());
for (index, run_id) in run_ids.into_iter().enumerate() {
let code_at = 1 + index * 2;
let code = raw
.get(code_at)
.map(value_to_string)
.ok_or_else(|| protocol("fire_due_batch 返回项数不足"))?;
let message_id = raw
.get(code_at + 1)
.map(value_to_string)
.unwrap_or_default();
results.push(FireDueBatchResult {
run_id,
code,
message_id,
});
}
Ok(results)
}
pub async fn renew_batch(
&self,
owner: &str,
items: &[RenewBatchItem],
) -> Result<Vec<RenewBatchResult>> {
if items.is_empty() {
return Ok(Vec::new());
}
let mut keys: Vec<Vec<u8>> = Vec::with_capacity(items.len() * 3);
let mut argv: Vec<Vec<u8>> = vec![items.len().to_string().into_bytes()];
for item in items {
keys.push(item.record_key.clone().into_bytes());
keys.push(item.leases_key.clone().into_bytes());
keys.push(item.waiting_key.clone().into_bytes());
argv.extend([
item.member.clone().into_bytes(),
owner.as_bytes().to_vec(),
item.attempt_token.to_string().into_bytes(),
self.config.lease_ms.to_string().into_bytes(),
]);
}
let raw = eval(&self.client, &RENEW_BATCH, &keys, &argv).await?;
let mut results = Vec::with_capacity(items.len());
for index in 0..items.len() {
let base = 1 + index * 3;
let code = raw
.get(base)
.map(value_to_string)
.ok_or_else(|| protocol("renew_batch 返回项数不足"))?;
let deadline = raw
.get(base + 1)
.map(value_to_string)
.and_then(|text| text.parse::<i64>().ok())
.unwrap_or(0);
let cancel_requested = !raw
.get(base + 2)
.map(value_to_string)
.unwrap_or_default()
.is_empty();
results.push(RenewBatchResult {
code,
deadline,
cancel_requested,
});
}
Ok(results)
}
pub async fn start_run(
&self,
definition: &JobDefinition,
run_id: &str,
executor_id: &str,
message_id: &str,
) -> Result<StartOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.waitq(shard, definition.name()).into_bytes(),
self.keyspace.fences(shard).into_bytes(),
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
self.keyspace.completion(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
definition.name().as_bytes().to_vec(),
run_id.as_bytes().to_vec(),
executor_id.as_bytes().to_vec(),
message_id.as_bytes().to_vec(),
self.config.lease_ms.to_string().into_bytes(),
definition.concurrency().wire_name().as_bytes().to_vec(),
self.config.max_serial_backlog.to_string().into_bytes(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.config.dispatch_group.as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
b"NORMAL".to_vec(),
self.config
.serial_overflow_policy
.wire_name()
.as_bytes()
.to_vec(),
self.config.protocol_version.to_string().into_bytes(),
definition.definition_revision().to_string().into_bytes(),
definition.contract_revision().to_string().into_bytes(),
definition.schema_id().as_bytes().to_vec(),
definition.wire_codecs().as_bytes().to_vec(),
self.keyspace.shard_prefix(shard).into_bytes(),
self.keyspace.qualifier().as_bytes().to_vec(),
self.config.max_scan_interval_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &START_RUN, &keys, &argv).await?;
interpret_start(&raw)
}
pub async fn renew_run(
&self,
definition: &JobDefinition,
run_id: &str,
executor_id: &str,
attempt_token: i64,
) -> Result<RenewOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
executor_id.as_bytes().to_vec(),
attempt_token.to_string().into_bytes(),
self.config.lease_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &RENEW_RUN, &keys, &argv).await?;
interpret_renew(&raw)
}
pub async fn finish_run(
&self,
definition: &JobDefinition,
run_id: &str,
executor_id: &str,
attempt_token: i64,
result_code: JobResultCode,
summary: &str,
) -> Result<FinishOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.waitq(shard, definition.name()).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
];
let retry_delay = self.retry_delay(definition.retry_delay_ms(), run_id);
let bounded_summary = limit_summary(summary, self.config.max_result_summary_bytes as usize);
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
executor_id.as_bytes().to_vec(),
attempt_token.to_string().into_bytes(),
definition.name().as_bytes().to_vec(),
result_code.wire_name().as_bytes().to_vec(),
bounded_summary.into_bytes(),
definition.max_attempts().to_string().into_bytes(),
retry_delay.to_string().into_bytes(),
self.config.run_retention_ms.to_string().into_bytes(),
b"NORMAL".to_vec(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
b"0".to_vec(),
b"0".to_vec(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &FINISH_RUN, &keys, &argv).await?;
interpret_finish(&raw)
}
pub async fn recover_expired(
&self,
definition: &JobDefinition,
run_id: &str,
) -> Result<RecoverOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.waitq(shard, definition.name()).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
];
let retry_delay = self.retry_delay(definition.retry_delay_ms(), run_id);
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
definition.name().as_bytes().to_vec(),
definition.max_attempts().to_string().into_bytes(),
retry_delay.to_string().into_bytes(),
self.config.run_retention_ms.to_string().into_bytes(),
b"NORMAL".to_vec(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
b"0".to_vec(),
b"0".to_vec(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &RECOVER_EXPIRED, &keys, &argv).await?;
interpret_recover(&raw)
}
pub async fn defer_run(
&self,
definition: &JobDefinition,
run_id: &str,
message_id: &str,
) -> Result<DeferOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace
.dispatch(shard, definition.worker_name())
.into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
message_id.as_bytes().to_vec(),
self.config.min_scan_interval_ms.to_string().into_bytes(),
self.config.dispatch_group.as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &DEFER_RUN, &keys, &argv).await?;
interpret_defer(&raw)
}
pub(crate) async fn defer_stream(
&self,
job_name: &str,
run_id: &str,
message_id: &str,
stream: &str,
) -> Result<DeferOutcome> {
let shard = self.keyspace.schedule_shard(job_name);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
stream.as_bytes().to_vec(),
self.keyspace.job(shard, job_name).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
message_id.as_bytes().to_vec(),
self.config.min_scan_interval_ms.to_string().into_bytes(),
self.config.dispatch_group.as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &DEFER_RUN, &keys, &argv).await?;
interpret_defer(&raw)
}
pub async fn promote_visible(&self, shard: u32) -> Result<Promotion> {
let keys = vec![
self.keyspace.visible(shard).into_bytes(),
self.keyspace.control(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
self.config.scan_batch_size.to_string().into_bytes(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.config.max_dispatch_attempts.to_string().into_bytes(),
self.config.max_scan_interval_ms.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
self.config.run_retention_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &PROMOTE_VISIBLE, &keys, &argv).await?;
interpret_promotion(&raw)
}
pub async fn visible_size(&self, shard: u32) -> Result<i64> {
let mut connection = self.client.conn();
redis::cmd("ZCARD")
.arg(self.keyspace.visible(shard))
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)
}
pub(crate) async fn serial_wait_depth(&self, job_name: &str) -> Result<i64> {
let shard = self.keyspace.schedule_shard(job_name);
let mut connection = self.client.conn();
redis::cmd("ZCARD")
.arg(self.keyspace.waitq(shard, job_name))
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)
}
pub async fn request_cancel(
&self,
definition: &JobDefinition,
run_id: &str,
) -> Result<CancelOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.waitq(shard, definition.name()).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
definition.name().as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &REQUEST_CANCEL, &keys, &argv).await?;
interpret_cancel(&raw)
}
pub async fn fail_waiting_creation(
&self,
definition: &JobDefinition,
run_id: &str,
) -> Result<FailWaitingOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.waitq(shard, definition.name()).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
definition.name().as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &FAIL_WAITING_CREATION, &keys, &argv).await?;
interpret_fail_waiting(&raw)
}
pub async fn pause(&self, job_name: &str) -> Result<DefinitionControlOutcome> {
let shard = self.keyspace.schedule_shard(job_name);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, job_name).into_bytes(),
];
let raw = eval(
&self.client,
&JOB_PAUSE,
&keys,
&[job_name.as_bytes().to_vec()],
)
.await?;
interpret_control(&raw)
}
pub async fn resume(
&self,
job_name: &str,
next_fire_at: i64,
) -> Result<DefinitionControlOutcome> {
let shard = self.keyspace.schedule_shard(job_name);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, job_name).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
job_name.as_bytes().to_vec(),
next_fire_at.to_string().into_bytes(),
];
let raw = eval(&self.client, &JOB_RESUME, &keys, &argv).await?;
interpret_control(&raw)
}
pub async fn delete(&self, job_name: &str, revision: i64) -> Result<DeleteOutcome> {
let shard = self.keyspace.schedule_shard(job_name);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.jobs(shard).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, job_name).into_bytes(),
self.keyspace.reaping(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
job_name.as_bytes().to_vec(),
revision.to_string().into_bytes(),
self.config.tombstone_retention_ms.to_string().into_bytes(),
];
let raw = eval(&self.client, &JOB_DELETE, &keys, &argv).await?;
interpret_delete(&raw)
}
pub async fn resolve_conflict(
&self,
definition: &JobDefinition,
next_fire_at: i64,
) -> Result<DefinitionControlOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
definition.name().as_bytes().to_vec(),
definition.definition_revision().to_string().into_bytes(),
definition.definition_digest().as_bytes().to_vec(),
JobDefinitionState::Enabled.wire_name().as_bytes().to_vec(),
next_fire_at.to_string().into_bytes(),
];
let raw = eval(&self.client, &JOB_RESOLVE_CONFLICT, &keys, &argv).await?;
interpret_control(&raw)
}
pub async fn set_namespace_state(
&self,
shard: u32,
state: JobDefinitionState,
actor: &str,
) -> Result<NamespaceStateOutcome> {
let actor = require_name(actor, "actor")?;
let key = self.keyspace.control(shard).into_bytes();
let argv: Vec<Vec<u8>> = vec![state.wire_name().as_bytes().to_vec(), actor.into_bytes()];
let raw = eval(&self.client, &NAMESPACE_SET_STATE, &[key], &argv).await?;
interpret_namespace_state(&raw)
}
pub async fn namespace_state_snapshot(&self, shard: u32) -> Result<NamespaceShardSnapshot> {
let key = self.keyspace.control(shard);
let mut connection = self.client.conn();
let (state, updated_at_ms, updated_by): (Option<String>, Option<i64>, Option<String>) =
redis::cmd("HMGET")
.arg(&key)
.arg("namespaceState")
.arg("updatedAt")
.arg("updatedBy")
.query_async(&mut connection)
.await
.map_err(NasaRedisError::Redis)?;
Ok(NamespaceShardSnapshot {
shard,
state,
updated_at_ms,
updated_by,
})
}
pub async fn cleanup_tombstones(&self, shard: u32) -> Result<i64> {
let keys: Vec<Vec<u8>> = vec![
self.keyspace.jobs(shard).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.fences(shard).into_bytes(),
self.keyspace.reaping(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
self.config.scan_batch_size.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &JOB_CLEANUP_TOMBSTONES, &keys, &argv).await?;
raw.first()
.map(value_to_string)
.ok_or_else(|| protocol("job_cleanup_tombstones 返回结构非法"))?
.parse::<i64>()
.map_err(|_| protocol("job_cleanup_tombstones 数量非法"))
}
pub async fn reap_deleted(&self, shard: u32) -> Result<JobReapOutcome> {
let keys: Vec<Vec<u8>> = vec![
self.keyspace.reaping(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
self.config.scan_batch_size.to_string().into_bytes(),
self.config.run_retention_ms.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &JOB_REAP, &keys, &argv).await?;
if raw.len() < 3 || (raw.len() - 3) % 3 != 0 {
return Err(protocol("job_reap 返回结构非法"));
}
let job_name = value_to_string(&raw[0]);
let reaped = value_to_string(&raw[1])
.parse::<i64>()
.map_err(|_| protocol("job_reap reapedCount 非法"))?;
let active = match value_to_string(&raw[2]).as_str() {
"0" => false,
"1" => true,
_ => return Err(protocol("job_reap active 非法")),
};
let mut fanout_requests = Vec::with_capacity((raw.len() - 3) / 3);
for fields in raw[3..].as_chunks::<3>().0 {
let run_id = value_to_string(&fields[0]);
let fanout_id = value_to_string(&fields[1]);
let root_attempt = value_to_string(&fields[2])
.parse::<i64>()
.map_err(|_| protocol("job_reap rootAttempt 非法"))?;
if job_name.is_empty() || run_id.is_empty() || fanout_id.is_empty() {
return Err(protocol("job_reap Fanout 定位字段为空"));
}
fanout_requests.push(JobReapFanoutRequest {
job_name: job_name.clone(),
run_id,
fanout_id,
root_attempt,
});
}
Ok(JobReapOutcome {
reaped,
active,
fanout_requests,
})
}
#[allow(clippy::too_many_arguments)]
pub async fn prepare_fanout_root(
&self,
definition: &JobDefinition,
run_id: &str,
owner: &str,
attempt_token: i64,
fanout_id: &str,
snapshot_id: &str,
shard_total: i64,
) -> Result<PrepareFanoutRootOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.leases(shard).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
owner.as_bytes().to_vec(),
attempt_token.to_string().into_bytes(),
fanout_id.as_bytes().to_vec(),
snapshot_id.as_bytes().to_vec(),
shard_total.to_string().into_bytes(),
self.config
.fanout_create_timeout_ms
.to_string()
.into_bytes(),
];
let raw = eval(&self.client, &PREPARE_FANOUT_ROOT, &keys, &argv).await?;
let code = raw.first().map(value_to_string).unwrap_or_default();
Ok(match code.as_str() {
"STATE_MISMATCH" => PrepareFanoutRootOutcome::StateMismatch,
"STALE_OWNER" => PrepareFanoutRootOutcome::StaleOwner,
"JOB_DELETED" => PrepareFanoutRootOutcome::JobDeleted,
"ADOPTED" => PrepareFanoutRootOutcome::Adopted {
deadline: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("prepare_fanout_root 缺少 deadline"))?
.parse::<i64>()
.map_err(|_| protocol("prepare_fanout_root deadline 非法"))?,
},
"PREPARED" => PrepareFanoutRootOutcome::Prepared {
redis_now: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("prepare_fanout_root 缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("prepare_fanout_root redisNow 非法"))?,
deadline: raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("prepare_fanout_root 缺少 deadline"))?
.parse::<i64>()
.map_err(|_| protocol("prepare_fanout_root deadline 非法"))?,
},
_ => return Err(protocol("prepare_fanout_root 返回未知码")),
})
}
#[allow(clippy::too_many_arguments)]
pub async fn finish_fanout_root(
&self,
definition: &JobDefinition,
run_id: &str,
fanout_id: &str,
root_attempt: i64,
target_state: &str,
error_type: &str,
result_summary: &str,
) -> Result<FinishFanoutRootOutcome> {
self.finish_fanout_root_for_job(
definition.name(),
run_id,
fanout_id,
root_attempt,
target_state,
error_type,
result_summary,
)
.await
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn finish_fanout_root_for_job(
&self,
job_name: &str,
run_id: &str,
fanout_id: &str,
root_attempt: i64,
target_state: &str,
error_type: &str,
result_summary: &str,
) -> Result<FinishFanoutRootOutcome> {
let shard = self.keyspace.schedule_shard(job_name);
let keys: Vec<Vec<u8>> = vec![
self.keyspace.run(shard, run_id).into_bytes(),
self.keyspace.waiting(shard).into_bytes(),
self.keyspace.running(shard).into_bytes(),
self.keyspace.visible(shard).into_bytes(),
self.keyspace.waitq(shard, job_name).into_bytes(),
self.keyspace.completion(shard).into_bytes(),
];
let argv: Vec<Vec<u8>> = vec![
run_id.as_bytes().to_vec(),
fanout_id.as_bytes().to_vec(),
root_attempt.to_string().into_bytes(),
target_state.as_bytes().to_vec(),
error_type.as_bytes().to_vec(),
self.config.fanout_max_wait_ms.to_string().into_bytes(),
result_summary.as_bytes().to_vec(),
job_name.as_bytes().to_vec(),
self.config.run_retention_ms.to_string().into_bytes(),
self.config.visibility_timeout_ms.to_string().into_bytes(),
self.keyspace.shard_prefix(shard).into_bytes(),
];
let raw = eval(&self.client, &FINISH_FANOUT_ROOT, &keys, &argv).await?;
interpret_finish_fanout_root(&raw)
}
fn retry_delay(&self, base: u64, run_id: &str) -> i64 {
let base = base as i64;
let span = (base / 5).max(1);
let jitter = (compat_string_hash(run_id) as i64).rem_euclid(span);
(base + jitter).min(self.config.max_run_duration_ms as i64)
}
pub async fn register(
&self,
definition: &JobDefinition,
next_fire_at: i64,
) -> Result<JobRegisterOutcome> {
let shard = self.keyspace.schedule_shard(definition.name());
let keys: Vec<Vec<u8>> = vec![
self.keyspace.jobs(shard).into_bytes(),
self.keyspace.schedule(shard).into_bytes(),
self.keyspace.job(shard, definition.name()).into_bytes(),
];
let codec_field = definition.wire_codecs().to_owned();
let argv: Vec<Vec<u8>> = vec![
definition.name().as_bytes().to_vec(),
definition.definition_revision().to_string().into_bytes(),
definition.definition_digest().as_bytes().to_vec(),
JobDefinitionState::Enabled.wire_name().as_bytes().to_vec(),
shard.to_string().into_bytes(),
definition.worker_name().as_bytes().to_vec(),
definition.worker_key().as_bytes().to_vec(),
definition.trigger().wire_name().as_bytes().to_vec(),
definition.schedule_type().wire_name().as_bytes().to_vec(),
definition.cron().as_bytes().to_vec(),
definition.zone().as_bytes().to_vec(),
definition.interval_ms().to_string().into_bytes(),
definition.concurrency().wire_name().as_bytes().to_vec(),
definition.misfire().wire_name().as_bytes().to_vec(),
definition.timeout_ms().to_string().into_bytes(),
definition.max_attempts().to_string().into_bytes(),
definition.retry_delay_ms().to_string().into_bytes(),
definition.contract_revision().to_string().into_bytes(),
definition.schema_id().as_bytes().to_vec(),
codec_field.into_bytes(),
definition
.fanout_receipt_timeout_ms()
.to_string()
.into_bytes(),
definition
.fanout_receipt_max_retries()
.to_string()
.into_bytes(),
definition
.fanout_failure_policy()
.wire_name()
.as_bytes()
.to_vec(),
next_fire_at.to_string().into_bytes(),
self.keyspace.qualifier().as_bytes().to_vec(),
];
let raw = eval(&self.client, &JOB_REGISTER, &keys, &argv).await?;
interpret_register(&raw)
}
pub async fn definition_record(&self, job_name: &str) -> Result<Option<JobDefinitionRecord>> {
let shard = self.keyspace.schedule_shard(job_name);
let key = self.keyspace.job(shard, job_name);
let mut conn = self.client.conn();
let raw: Vec<Option<Vec<u8>>> = redis::cmd("HMGET")
.arg(key.as_bytes())
.arg("state")
.arg("definitionRevision")
.arg("definitionDigest")
.arg("nextFireAt")
.query_async(&mut conn)
.await
.map_err(NasaRedisError::Redis)?;
if raw.iter().all(Option::is_none) {
return Ok(None);
}
let state_text = utf8(raw.first().cloned().flatten())?;
let state = JobDefinitionState::parse(&state_text)
.ok_or_else(|| protocol("job 定义 state 字段非法"))?;
let revision = parse_i64(raw.get(1).cloned().flatten())?;
let digest = utf8(raw.get(2).cloned().flatten())?;
let next_fire_at = parse_i64(raw.get(3).cloned().flatten())?;
Ok(Some(JobDefinitionRecord {
state,
revision,
digest,
next_fire_at,
}))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobDefinitionRecord {
pub state: JobDefinitionState,
pub revision: i64,
pub digest: String,
pub next_fire_at: i64,
}
fn interpret_register(raw: &[redis::Value]) -> Result<JobRegisterOutcome> {
if raw.len() != 2 {
return Err(protocol("job_register 返回结构非法"));
}
let code = value_to_string(&raw[0]);
if code == "SOURCE_MISMATCH" {
return Ok(JobRegisterOutcome::SourceMismatch {
current_source: value_to_string(&raw[1]),
});
}
let revision = value_to_string(&raw[1])
.parse::<i64>()
.map_err(|_| protocol("job_register 修订号非法"))?;
Ok(match code.as_str() {
"OK" => JobRegisterOutcome::Registered { revision },
"ADOPTED" => JobRegisterOutcome::Adopted { revision },
"STALE" => JobRegisterOutcome::Stale { revision },
"CONFLICT" => JobRegisterOutcome::Conflict { revision },
"DELETED" => JobRegisterOutcome::Deleted { revision },
_ => return Err(protocol("job_register 返回未知码")),
})
}
fn interpret_manual_fire(raw: &[redis::Value], run_id: String) -> Result<ManualFireOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"NOT_FOUND" => Ok(ManualFireOutcome::NotFound),
"STATE_MISMATCH" => Ok(ManualFireOutcome::StateMismatch),
"ADOPTED" => Ok(ManualFireOutcome::Adopted { run_id }),
"OK" => {
if raw.len() != 4 {
return Err(protocol("manual_fire OK 返回结构非法"));
}
Ok(ManualFireOutcome::Fired {
run_id,
message_id: value_to_string(&raw[3]),
})
}
_ => Err(protocol("manual_fire 返回未知码")),
}
}
fn interpret_schedule_scan(raw: &[redis::Value]) -> Result<ScheduleDueScan> {
if raw.len() < 3 {
return Err(protocol("scan_due 返回缺少调度头"));
}
let redis_now = value_to_string(&raw[0])
.parse::<i64>()
.map_err(|_| protocol("scan_due redisNow 非法"))?;
let next_text = value_to_string(&raw[1]);
let next_score = if next_text.is_empty() {
None
} else {
Some(
next_text
.parse::<i64>()
.map_err(|_| protocol("scan_due nextScore 非法"))?,
)
};
let namespace_enabled = value_to_string(&raw[2]) == JobDefinitionState::Enabled.wire_name();
let body = &raw[3..];
if !body.len().is_multiple_of(3) {
return Err(protocol("scan_due 成员三元组不完整"));
}
let mut entries = Vec::with_capacity(body.len() / 3);
for chunk in body.as_chunks::<3>().0 {
entries.push(DueEntry {
job_name: value_to_string(&chunk[0]),
logical_fire_at: value_to_string(&chunk[1])
.parse::<i64>()
.map_err(|_| protocol("scan_due score 非法"))?,
definition_revision: value_to_string(&chunk[2])
.parse::<i64>()
.map_err(|_| protocol("scan_due definitionRevision 非法"))?,
});
}
Ok(ScheduleDueScan {
redis_now,
next_score,
namespace_enabled,
entries,
})
}
fn interpret_index_scan(raw: &[redis::Value]) -> Result<DueIndexScan> {
if raw.len() < 2 {
return Err(protocol("scan_due 通用模式缺少时间头"));
}
let redis_now = value_to_string(&raw[0])
.parse::<i64>()
.map_err(|_| protocol("scan_due 通用模式 redisNow 非法"))?;
let next = value_to_string(&raw[1]);
let next_score = if next.is_empty() {
None
} else {
Some(
next.parse::<i64>()
.map_err(|_| protocol("scan_due 通用模式 nextScore 非法"))?,
)
};
let body = &raw[2..];
if !body.len().is_multiple_of(2) {
return Err(protocol("scan_due 通用模式成员二元组不完整"));
}
let mut entries = Vec::with_capacity(body.len() / 2);
for pair in body.as_chunks::<2>().0 {
entries.push(DueIndexEntry {
member: value_to_string(&pair[0]),
score: value_to_string(&pair[1])
.parse::<i64>()
.map_err(|_| protocol("scan_due 通用模式 score 非法"))?,
});
}
Ok(DueIndexScan {
redis_now,
next_score,
entries,
})
}
fn interpret_fire_due(raw: &[redis::Value], run_id: String) -> Result<FireDueOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
let now_at = |index: usize| -> Result<i64> {
raw.get(index)
.map(value_to_string)
.ok_or_else(|| protocol("fire_due 缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("fire_due redisNow 非法"))
};
Ok(match code.as_str() {
"STATE_MISMATCH" => FireDueOutcome::StateMismatch,
"STALE" => FireDueOutcome::Stale,
"NOT_DUE" => FireDueOutcome::NotDue {
redis_now: now_at(1)?,
},
"NEED_RECOMPUTE" => FireDueOutcome::NeedRecompute {
redis_now: now_at(1)?,
},
"SKIPPED" => FireDueOutcome::Skipped {
redis_now: now_at(1)?,
next_fire_at: raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("fire_due SKIPPED 缺少 nextFireAt"))?
.parse::<i64>()
.map_err(|_| protocol("fire_due nextFireAt 非法"))?,
},
"ADOPTED" => FireDueOutcome::Adopted { run_id },
"OK" => FireDueOutcome::Fired {
run_id,
redis_now: now_at(1)?,
message_id: raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("fire_due OK 缺少 messageId"))?,
},
_ => return Err(protocol("fire_due 返回未知码")),
})
}
pub(crate) fn interpret_start(raw: &[redis::Value]) -> Result<StartOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
let number = |index: usize, field: &str| -> Result<i64> {
raw.get(index)
.map(value_to_string)
.ok_or_else(|| protocol("start_run 成功返回字段缺失"))?
.parse::<i64>()
.map_err(|_| protocol(&format!("start_run {field} 字段非法")))
};
let started_fields = || -> Result<(i64, i64, i64, i64)> {
Ok((
number(1, "attempt")?,
number(2, "attemptToken")?,
number(3, "redisNow")?,
number(4, "leaseUntil")?,
))
};
Ok(match code.as_str() {
"STARTED" => {
let (attempt, attempt_token, redis_now, lease_until) = started_fields()?;
StartOutcome::Started {
attempt,
attempt_token,
redis_now,
lease_until,
}
}
"ADOPTED" => {
let (attempt, attempt_token, redis_now, lease_until) = started_fields()?;
StartOutcome::Adopted {
attempt,
attempt_token,
redis_now,
lease_until,
}
}
"NOT_COMMITTED" => StartOutcome::NotCommitted,
"STALE_ASSIGNMENT" => StartOutcome::StaleAssignment,
"NOT_FOUND" => StartOutcome::NotFound,
"DEFERRED" => StartOutcome::Deferred,
"LEASE_EXPIRED" => StartOutcome::LeaseExpired,
"JOB_DELETED" => StartOutcome::JobDeleted,
"PROTOCOL_UNSUPPORTED" => StartOutcome::ProtocolUnsupported,
"SOURCE_MISMATCH" => StartOutcome::SourceMismatch,
"IDENTITY_MISMATCH" => StartOutcome::IdentityMismatch,
"CONTRACT_MISMATCH" => StartOutcome::ContractMismatch,
"CONFLICT" => StartOutcome::Conflict,
"STALE" | "STATE_MISMATCH" => StartOutcome::Stale,
"CANCELLED" => StartOutcome::Cancelled,
"SKIPPED" => StartOutcome::Skipped,
"BLOCKED" => StartOutcome::Blocked,
"FENCING_REGRESSION" => StartOutcome::FencingRegression,
_ => return Err(protocol("start_run 返回未知码")),
})
}
pub(crate) fn interpret_renew(raw: &[redis::Value]) -> Result<RenewOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STATE_MISMATCH" => Ok(RenewOutcome::StateMismatch),
"STALE_OWNER" => Ok(RenewOutcome::StaleOwner),
"OK" => {
let redis_now = raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("renew_run 缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("renew_run redisNow 非法"))?;
let deadline = raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("renew_run 缺少 deadline"))?
.parse::<i64>()
.map_err(|_| protocol("renew_run deadline 非法"))?;
let cancel_requested = !raw
.get(3)
.map(value_to_string)
.unwrap_or_default()
.is_empty();
Ok(RenewOutcome::Ok {
redis_now,
deadline,
cancel_requested,
})
}
_ => Err(protocol("renew_run 返回未知码")),
}
}
pub(crate) fn interpret_finish(raw: &[redis::Value]) -> Result<FinishOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STATE_MISMATCH" => Ok(FinishOutcome::StateMismatch),
"STALE_OWNER" => Ok(FinishOutcome::StaleOwner),
"STALE_ASSIGNMENT" => Ok(FinishOutcome::StaleAssignment),
"IDENTITY_MISMATCH" => Ok(FinishOutcome::IdentityMismatch),
"OK" => {
let (next_state, redis_now, wake_delay_ms) = parse_completion(raw)?;
Ok(FinishOutcome::Ok {
next_state,
redis_now,
wake_delay_ms,
})
}
_ => Err(protocol("finish_run 返回未知码")),
}
}
pub(crate) fn interpret_recover(raw: &[redis::Value]) -> Result<RecoverOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STALE" => Ok(RecoverOutcome::Stale),
"ROOT_NOT_CANCELLING" => Ok(RecoverOutcome::RootNotCancelling),
"NOT_DUE" => Ok(RecoverOutcome::NotDue {
lease_until: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("recover_expired 缺少 leaseUntil"))?
.parse::<i64>()
.map_err(|_| protocol("recover_expired leaseUntil 非法"))?,
}),
"OK" => {
let (next_state, redis_now, wake_delay_ms) = parse_completion(raw)?;
Ok(RecoverOutcome::Ok {
next_state,
redis_now,
wake_delay_ms,
})
}
_ => Err(protocol("recover_expired 返回未知码")),
}
}
fn parse_completion(raw: &[redis::Value]) -> Result<(JobState, i64, Option<i64>)> {
let next_state = JobState::parse(&raw.get(1).map(value_to_string).unwrap_or_default())
.ok_or_else(|| protocol("完成返回 nextState 非法"))?;
let redis_now = raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("完成返回缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("完成返回 redisNow 非法"))?;
let wake_delay_ms = match raw.get(3).map(value_to_string) {
Some(text) if !text.is_empty() => Some(
text.parse::<i64>()
.map_err(|_| protocol("完成返回 wakeDelay 非法"))?,
),
_ => None,
};
Ok((next_state, redis_now, wake_delay_ms))
}
fn interpret_defer(raw: &[redis::Value]) -> Result<DeferOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STALE" => Ok(DeferOutcome::Stale),
"JOB_DELETED" => Ok(DeferOutcome::JobDeleted),
"DEFERRED" => Ok(DeferOutcome::Deferred {
delay_ms: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("defer_run 缺少 delayMs"))?
.parse::<i64>()
.map_err(|_| protocol("defer_run delayMs 非法"))?,
}),
_ => Err(protocol("defer_run 返回未知码")),
}
}
fn interpret_promotion(raw: &[redis::Value]) -> Result<Promotion> {
if raw.len() < 3 {
return Err(protocol("promote_visible 返回结构非法"));
}
let promoted = value_to_string(&raw[0])
.parse::<i64>()
.map_err(|_| protocol("promote_visible promoted 非法"))?;
let next_text = value_to_string(&raw[1]);
let next_score = if next_text.is_empty() {
None
} else {
Some(
next_text
.parse::<i64>()
.map_err(|_| protocol("promote_visible nextScore 非法"))?,
)
};
let redis_now = value_to_string(&raw[2])
.parse::<i64>()
.map_err(|_| protocol("promote_visible redisNow 非法"))?;
Ok(Promotion {
promoted,
next_score,
redis_now,
})
}
fn interpret_cancel(raw: &[redis::Value]) -> Result<CancelOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"NOT_FOUND" => Ok(CancelOutcome::NotFound),
"ALREADY_COMPLETED" => Ok(CancelOutcome::AlreadyCompleted),
"OK" => {
let wake_delay_ms = match raw.get(1).map(value_to_string) {
Some(text) if !text.is_empty() => Some(
text.parse::<i64>()
.map_err(|_| protocol("request_cancel wakeDelay 非法"))?,
),
_ => None,
};
Ok(CancelOutcome::Ok { wake_delay_ms })
}
_ => Err(protocol("request_cancel 返回未知码")),
}
}
fn interpret_fail_waiting(raw: &[redis::Value]) -> Result<FailWaitingOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STATE_MISMATCH" => Ok(FailWaitingOutcome::StateMismatch),
"NOT_DUE" => Ok(FailWaitingOutcome::NotDue {
deadline: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("fail_waiting_creation 缺少 deadline"))?
.parse::<i64>()
.map_err(|_| protocol("fail_waiting_creation deadline 非法"))?,
}),
"OK" => {
let (next_state, redis_now, wake_delay_ms) = parse_completion(raw)?;
Ok(FailWaitingOutcome::Ok {
next_state,
redis_now,
wake_delay_ms,
})
}
_ => Err(protocol("fail_waiting_creation 返回未知码")),
}
}
fn interpret_control(raw: &[redis::Value]) -> Result<DefinitionControlOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
Ok(match code.as_str() {
"OK" => DefinitionControlOutcome::Ok,
"NOT_FOUND" => DefinitionControlOutcome::NotFound,
"STATE_MISMATCH" => DefinitionControlOutcome::StateMismatch,
"STALE" => DefinitionControlOutcome::Stale,
_ => return Err(protocol("定义控制脚本返回未知码")),
})
}
fn interpret_finish_fanout_root(raw: &[redis::Value]) -> Result<FinishFanoutRootOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"STALE" => Ok(FinishFanoutRootOutcome::Stale),
"ADOPTED" => Ok(FinishFanoutRootOutcome::Adopted),
"STATE_MISMATCH" => Ok(FinishFanoutRootOutcome::StateMismatch),
"JOB_DELETED" => Ok(FinishFanoutRootOutcome::JobDeleted {
state: JobState::parse(&raw.get(1).map(value_to_string).unwrap_or_default())
.ok_or_else(|| protocol("finish_fanout_root 删除状态字段非法"))?,
deadline: raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("finish_fanout_root 删除截止字段缺失"))?
.parse::<i64>()
.map_err(|_| protocol("finish_fanout_root 删除截止字段非法"))?,
}),
"ALREADY_COMPLETED" => Ok(FinishFanoutRootOutcome::AlreadyCompleted {
state: JobState::parse(&raw.get(1).map(value_to_string).unwrap_or_default())
.ok_or_else(|| protocol("finish_fanout_root 终态字段非法"))?,
}),
"OK" => {
let state = JobState::parse(&raw.get(1).map(value_to_string).unwrap_or_default())
.ok_or_else(|| protocol("finish_fanout_root state 字段非法"))?;
let redis_now_or_deadline = raw
.get(2)
.map(value_to_string)
.ok_or_else(|| protocol("finish_fanout_root 缺少时刻"))?
.parse::<i64>()
.map_err(|_| protocol("finish_fanout_root 时刻非法"))?;
let wake_delay_ms = match raw.get(3).map(value_to_string) {
Some(text) if !text.is_empty() => Some(
text.parse::<i64>()
.map_err(|_| protocol("finish_fanout_root wakeDelay 非法"))?,
),
_ => None,
};
Ok(FinishFanoutRootOutcome::Ok {
state,
redis_now_or_deadline,
wake_delay_ms,
})
}
_ => Err(protocol("finish_fanout_root 返回未知码")),
}
}
fn interpret_delete(raw: &[redis::Value]) -> Result<DeleteOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"NOT_FOUND" => Ok(DeleteOutcome::NotFound),
"STALE" => Ok(DeleteOutcome::Stale {
revision: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("job_delete 缺少当前修订号"))?
.parse::<i64>()
.map_err(|_| protocol("job_delete 修订号非法"))?,
}),
"OK" => Ok(DeleteOutcome::Ok {
redis_now: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("job_delete 缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("job_delete redisNow 非法"))?,
}),
_ => Err(protocol("job_delete 返回未知码")),
}
}
fn interpret_namespace_state(raw: &[redis::Value]) -> Result<NamespaceStateOutcome> {
let code = raw.first().map(value_to_string).unwrap_or_default();
match code.as_str() {
"INVALID" => Ok(NamespaceStateOutcome::Invalid),
"OK" => Ok(NamespaceStateOutcome::Ok {
redis_now: raw
.get(1)
.map(value_to_string)
.ok_or_else(|| protocol("namespace_set_state 缺少 redisNow"))?
.parse::<i64>()
.map_err(|_| protocol("namespace_set_state redisNow 非法"))?,
}),
_ => Err(protocol("namespace_set_state 返回未知码")),
}
}
fn limit_summary(value: &str, max_bytes: usize) -> String {
if value.is_empty() || value.len() <= max_bytes {
return value.to_owned();
}
let mut length = max_bytes;
while length > 0 && !value.is_char_boundary(length) {
length -= 1;
}
value[..length].to_owned()
}
fn command_info(names: &[&str]) -> redis::Cmd {
let mut command = redis::cmd("COMMAND");
command.arg("INFO");
for name in names {
command.arg(name);
}
command
}
fn command_info_supported(value: &redis::Value, expected: usize) -> bool {
matches!(
value,
redis::Value::Array(items)
if items.len() == expected
&& items.iter().all(|item| !matches!(item, redis::Value::Nil | redis::Value::ServerError(_)))
)
}
fn all_master_command_info_supported(
value: &redis::Value,
expected_addresses: &BTreeSet<String>,
command_count: usize,
) -> bool {
let redis::Value::Map(entries) = value else {
return false;
};
let mut answered = BTreeSet::new();
for (address, response) in entries {
let Some(address) = redis_text(address) else {
return false;
};
if !answered.insert(address) || !command_info_supported(response, command_count) {
return false;
}
}
answered == *expected_addresses
}
async fn cluster_primary_addresses(
connection: &mut redis::cluster_async::ClusterConnection,
) -> Option<BTreeSet<String>> {
let mut command = redis::cmd("CLUSTER");
command.arg("SLOTS");
let value = connection
.route_command(
command,
RoutingInfo::SingleNode(SingleNodeRoutingInfo::RandomPrimary),
)
.await
.ok()?;
let redis::Value::Array(rows) = value else {
return None;
};
let mut addresses = BTreeSet::new();
for row in rows {
let redis::Value::Array(fields) = row else {
return None;
};
let Some(redis::Value::Array(primary)) = fields.get(2) else {
return None;
};
let host = redis_text(primary.first()?)?;
let port = match primary.get(1)? {
redis::Value::Int(port) if (1..=u16::MAX as i64).contains(port) => *port as u16,
_ => return None,
};
if host.is_empty() || host == "?" {
return None;
}
addresses.insert(format!("{host}:{port}"));
}
(!addresses.is_empty()).then_some(addresses)
}
fn redis_text(value: &redis::Value) -> Option<String> {
match value {
redis::Value::BulkString(bytes) => String::from_utf8(bytes.clone()).ok(),
redis::Value::SimpleString(text) => Some(text.clone()),
_ => None,
}
}
fn cfg(message: &str) -> NasaRedisError {
crate::job::JobError::Config(message.to_owned()).into()
}
fn utf8(value: Option<Vec<u8>>) -> Result<String> {
match value {
Some(bytes) => String::from_utf8(bytes).map_err(|_| protocol("job 字段不是合法 UTF-8")),
None => Ok(String::new()),
}
}
fn parse_i64(value: Option<Vec<u8>>) -> Result<i64> {
match value {
None => Ok(0),
Some(bytes) => String::from_utf8(bytes)
.ok()
.and_then(|text| text.parse::<i64>().ok())
.ok_or_else(|| protocol("job 数值字段非法")),
}
}
fn protocol(message: &str) -> NasaRedisError {
crate::job::JobError::Protocol(message.to_owned()).into()
}