use std::collections::HashMap;
use async_trait::async_trait;
use fred::prelude::{
Builder, Client, ClientLike as _, Config, HashesInterface as _, LuaInterface as _,
};
use fred::types::Value;
use crate::error::{KernelError, Result};
use crate::partition::PartitionId;
use crate::proc::{
LedgerEntry, LedgerQuery, LedgerStore, Observation, ObservationQuery, PassState,
order_observations,
};
const LAYOUT: i64 = 1;
const CHUNK: usize = 512;
const MIGRATE_SCRIPT: &str = r"
local stored = tonumber(redis.call('GET', KEYS[1]) or '0')
local mine = tonumber(ARGV[1])
if stored > mine then return stored end
if stored < mine then redis.call('SET', KEYS[1], ARGV[1]) end
return mine
";
const APPEND_SCRIPT: &str = r"
local seq = redis.call('INCR', KEYS[1])
redis.call('HSET', KEYS[2], seq, ARGV[1])
redis.call('ZADD', KEYS[3], seq, seq)
redis.call('ZADD', KEYS[4], ARGV[2], seq)
return seq
";
const PAGE_SCRIPT: &str = r"
local seqs = redis.call('ZRANGEBYSCORE', KEYS[1], '(' .. ARGV[1], '+inf', 'LIMIT', 0, tonumber(ARGV[2]))
local out = {}
for _, seq in ipairs(seqs) do
table.insert(out, seq)
table.insert(out, redis.call('HGET', KEYS[2], seq) or '')
end
return out
";
const WINDOW_SCRIPT: &str = r"
return redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1], ARGV[2])
";
const ROWS_SCRIPT: &str = r"
local out = {}
for _, seq in ipairs(ARGV) do
table.insert(out, redis.call('HGET', KEYS[1], seq) or '')
end
return out
";
#[derive(Debug, Clone)]
pub struct RedisLedgerStore {
client: Client,
namespace: String,
}
impl RedisLedgerStore {
pub const DEFAULT_NAMESPACE: &'static str = "adk:kernel";
pub fn from_client(client: Client) -> Self {
Self { client, namespace: Self::DEFAULT_NAMESPACE.to_string() }
}
pub async fn connect(url: &str) -> Result<Self> {
let config =
Config::from_url(url).map_err(|error| store_error("parse the Redis URL", &error))?;
let client = Builder::from_config(config)
.build()
.map_err(|error| store_error("build the Redis client", &error))?;
client.init().await.map_err(|error| store_error("connect to Redis", &error))?;
Ok(Self::from_client(client))
}
#[must_use]
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = namespace.into();
self
}
pub fn client(&self) -> &Client {
&self.client
}
pub fn namespace(&self) -> &str {
&self.namespace
}
pub async fn migrate(&self) -> Result<()> {
let stored: i64 = self
.client
.eval(MIGRATE_SCRIPT, vec![self.key("ledger:layout")], vec![LAYOUT.to_string()])
.await
.map_err(|error| store_error("record the ledger layout version", &error))?;
if stored > LAYOUT {
return Err(KernelError::Store {
backend: "redis",
reason: format!(
"the ledger keys under {} have layout v{stored}, but this release knows up \
to v{LAYOUT}; upgrade ADK-Rust",
self.namespace
),
});
}
Ok(())
}
fn key(&self, suffix: &str) -> String {
let namespace = self.namespace.replace('{', "%7B").replace('}', "%7D");
format!("{{{namespace}}}:{suffix}")
}
async fn script(&self, script: &str, keys: Vec<String>, args: Vec<String>) -> Result<Value> {
self.client
.eval(script, keys, args)
.await
.map_err(|error| store_error("read the ledger", &error))
}
async fn rows(&self, seqs: &[u64]) -> Result<Vec<LedgerEntry>> {
let mut entries = Vec::with_capacity(seqs.len());
for chunk in seqs.chunks(CHUNK) {
let args = chunk.iter().map(u64::to_string).collect();
let reply = self.script(ROWS_SCRIPT, vec![self.key("ledger:rows")], args).await?;
for (seq, row) in chunk.iter().zip(reply.into_array()) {
entries.push(decode(*seq, row)?);
}
}
Ok(entries)
}
}
fn store_error(action: &str, error: &dyn std::fmt::Display) -> KernelError {
KernelError::Store { backend: "redis", reason: format!("could not {action}: {error}") }
}
fn decode(seq: u64, row: Value) -> Result<LedgerEntry> {
let text = row.into_string().unwrap_or_default();
let mut entry: LedgerEntry = serde_json::from_str(&text)
.map_err(|_| store_error("read the ledger", &format!("row {seq} is corrupt")))?;
entry.seq = seq;
Ok(entry)
}
fn json<T: serde::de::DeserializeOwned>(what: &str, text: &str) -> Result<T> {
serde_json::from_str(text).map_err(|error| store_error(&format!("read {what}"), &error))
}
#[async_trait]
impl LedgerStore for RedisLedgerStore {
async fn append(&self, mut entry: LedgerEntry) -> Result<LedgerEntry> {
entry.seq = 0;
let keys = vec![
self.key("ledger:seq"),
self.key("ledger:rows"),
self.key("ledger:by_seq"),
self.key("ledger:by_ts"),
];
let args = vec![serde_json::to_string(&entry)?, entry.ts.timestamp_millis().to_string()];
let seq: i64 = self
.client
.eval(APPEND_SCRIPT, keys, args)
.await
.map_err(|error| store_error("append to the ledger", &error))?;
entry.seq = u64::try_from(seq).unwrap_or_default();
Ok(entry)
}
async fn list(&self, query: &LedgerQuery) -> Result<Vec<LedgerEntry>> {
let limit = query.limit.unwrap_or(usize::MAX);
let mut found = Vec::new();
if limit == 0 {
return Ok(found);
}
if query.from.is_some() || query.to.is_some() {
let min =
query.from.map_or("-inf".to_string(), |from| from.timestamp_millis().to_string());
let max =
query.to.map_or("+inf".to_string(), |to| format!("({}", to.timestamp_millis()));
let reply =
self.script(WINDOW_SCRIPT, vec![self.key("ledger:by_ts")], vec![min, max]).await?;
let mut seqs: Vec<u64> = reply
.into_array()
.into_iter()
.filter_map(|seq| seq.into_string().and_then(|seq| seq.parse().ok()))
.filter(|seq| query.after.is_none_or(|after| *seq > after))
.collect();
seqs.sort_unstable();
for chunk in seqs.chunks(CHUNK) {
for entry in self.rows(chunk).await? {
if query.matches(&entry) {
found.push(entry);
if found.len() == limit {
return Ok(found);
}
}
}
}
return Ok(found);
}
let mut cursor = query.after.unwrap_or(0);
loop {
let keys = vec![self.key("ledger:by_seq"), self.key("ledger:rows")];
let reply =
self.script(PAGE_SCRIPT, keys, vec![cursor.to_string(), CHUNK.to_string()]).await?;
let items = reply.into_array();
if items.is_empty() {
return Ok(found);
}
let mut items = items.into_iter();
while let (Some(seq), Some(row)) = (items.next(), items.next()) {
let seq: u64 =
seq.into_string().and_then(|seq| seq.parse().ok()).ok_or_else(|| {
store_error("read the ledger", &"a sequence number is corrupt")
})?;
cursor = seq;
let entry = decode(seq, row)?;
if query.matches(&entry) {
found.push(entry);
if found.len() == limit {
return Ok(found);
}
}
}
}
}
async fn put_observation(&self, observation: Observation) -> Result<()> {
let body = serde_json::to_string(&observation)?;
self.client
.hset::<(), _, _>(self.key("observations"), (observation.id.as_str(), body))
.await
.map_err(|error| store_error("write an observation", &error))
}
async fn observations(&self, query: &ObservationQuery) -> Result<Vec<Observation>> {
let rows: HashMap<String, String> = self
.client
.hgetall(self.key("observations"))
.await
.map_err(|error| store_error("read the observations", &error))?;
let mut observations = rows
.values()
.map(|body| json::<Observation>("an observation", body))
.collect::<Result<Vec<_>>>()?;
observations.retain(|observation| query.matches(observation));
order_observations(&mut observations);
observations.truncate(query.limit.unwrap_or(usize::MAX));
Ok(observations)
}
async fn pass_state(&self, partition: &PartitionId) -> Result<Option<PassState>> {
let body: Option<String> = self
.client
.hget(self.key("proc_state"), partition.as_str())
.await
.map_err(|error| store_error("read the pass state", &error))?;
body.map(|body| json("the pass state", &body)).transpose()
}
async fn pass_states(&self) -> Result<Vec<PassState>> {
let rows: HashMap<String, String> = self
.client
.hgetall(self.key("proc_state"))
.await
.map_err(|error| store_error("read the pass states", &error))?;
let mut states = rows
.values()
.map(|body| json::<PassState>("a pass state", body))
.collect::<Result<Vec<_>>>()?;
states.sort_by(|a, b| a.partition.cmp(&b.partition));
Ok(states)
}
async fn put_pass_state(&self, state: PassState) -> Result<()> {
let body = serde_json::to_string(&state)?;
self.client
.hset::<(), _, _>(self.key("proc_state"), (state.partition.as_str(), body))
.await
.map_err(|error| store_error("write the pass state", &error))
}
}