1use std::time::Duration;
2
3use nopace_core::{
4 AdmissionRequest, Completion, CompletionResult, LeaseRenewal, LeaseRenewalResult, PartitionKey,
5 PolicyId, ResetReport, Retention, ScopeKey, ScopeState, Store, StoreDecision, StorePeek,
6 StoreSnapshot, collect_at, decide_at, finish_at, peek_at, renew_at, reset_policy, snapshot_at,
7};
8use redis::AsyncCommands;
9use thiserror::Error;
10
11#[derive(Clone)]
12pub struct RedisStore {
13 client: redis::Client,
14 prefix: String,
15 retention: Retention,
16}
17
18impl RedisStore {
19 pub fn connect(url: &str) -> Result<Self, RedisError> {
20 Ok(Self {
21 client: redis::Client::open(url)?,
22 prefix: "nopace:v1".into(),
23 retention: Retention::default(),
24 })
25 }
26
27 pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
28 self.prefix = prefix.into();
29 self
30 }
31 pub fn with_retention(mut self, retention: Retention) -> Self {
32 self.retention = retention;
33 self
34 }
35
36 pub async fn collect(&self, scope: &ScopeKey) -> Result<(), RedisError> {
37 let id = uuid::Uuid::now_v7();
38 self.apply(scope, id, |state, now| {
39 collect_at(state, now, self.retention)
40 })
41 .await
42 }
43
44 pub async fn snapshot(&self, scope: &ScopeKey) -> Result<Option<StoreSnapshot>, RedisError> {
45 let mut connection = self.client.get_multiplexed_async_connection().await?;
46 let key = self.key(scope);
47 let bytes: Option<Vec<u8>> = connection.get(key).await?;
48 bytes
49 .map(|bytes| {
50 bincode::deserialize::<ScopeState>(&bytes).map(|state| snapshot_at(&state))
51 })
52 .transpose()
53 .map_err(RedisError::from)
54 }
55
56 pub async fn reset_policy(
57 &self,
58 scope: &ScopeKey,
59 policy: &PolicyId,
60 partition: Option<&PartitionKey>,
61 ) -> Result<ResetReport, RedisError> {
62 self.apply(scope, uuid::Uuid::now_v7(), |state, _| {
63 reset_policy(state, policy, partition)
64 })
65 .await
66 }
67
68 async fn apply<T: serde::Serialize + serde::de::DeserializeOwned>(
69 &self,
70 scope: &ScopeKey,
71 operation_id: uuid::Uuid,
72 operation: impl FnOnce(&mut ScopeState, u64) -> T,
73 ) -> Result<T, RedisError> {
74 let mut connection = self.client.get_multiplexed_async_connection().await?;
75 let key = self.key(scope);
76 let result_key = format!("{key}:result:{operation_id}");
77 let lock_key = format!("{key}:lock");
78 loop {
79 let acquired: Option<String> = redis::cmd("SET")
80 .arg(&lock_key)
81 .arg(operation_id.to_string())
82 .arg("NX")
83 .arg("PX")
84 .arg(5_000)
85 .query_async(&mut connection)
86 .await?;
87 if acquired.is_some() {
88 break;
89 }
90 if let Some(bytes) = connection.get::<_, Option<Vec<u8>>>(&result_key).await? {
91 return bincode::deserialize(&bytes).map_err(RedisError::from);
92 }
93 tokio::time::sleep(Duration::from_millis(5)).await;
94 }
95 let now = redis_time_millis(&mut connection).await?;
96 let bytes: Option<Vec<u8>> = connection.get(&key).await?;
97 let mut state = bytes
98 .map(|bytes| bincode::deserialize(&bytes))
99 .transpose()?
100 .unwrap_or_default();
101 let result = operation(&mut state, now);
102 let state = bincode::serialize(&state)?;
103 let result_bytes = bincode::serialize(&result)?;
104 let ttl = duration_millis(self.retention.replay);
105 let script = redis::Script::new(
106 r#"
107 if redis.call('GET', KEYS[3]) ~= ARGV[3] then return 0 end
108 redis.call('SET', KEYS[1], ARGV[1])
109 redis.call('SET', KEYS[2], ARGV[2], 'PX', ARGV[4])
110 redis.call('DEL', KEYS[3])
111 return 1
112 "#,
113 );
114 let committed: i32 = script
115 .key(&key)
116 .key(&result_key)
117 .key(&lock_key)
118 .arg(state)
119 .arg(result_bytes)
120 .arg(operation_id.to_string())
121 .arg(ttl)
122 .invoke_async(&mut connection)
123 .await?;
124 if committed != 1 {
125 return Err(RedisError::LostLock);
126 }
127 Ok(result)
128 }
129
130 fn key(&self, scope: &ScopeKey) -> String {
131 format!("{}:{}", self.prefix, scope.as_str())
132 }
133}
134
135impl Store for RedisStore {
136 type Error = RedisError;
137 async fn decide(&self, request: AdmissionRequest) -> Result<StoreDecision, Self::Error> {
138 let scope = request.scope.clone();
139 self.apply(&scope, request.id, |state, now| {
140 decide_at(state, &request, now, self.retention)
141 })
142 .await
143 }
144 async fn peek(&self, request: AdmissionRequest) -> Result<StorePeek, Self::Error> {
145 let mut connection = self.client.get_multiplexed_async_connection().await?;
146 let now = redis_time_millis(&mut connection).await?;
147 let bytes: Option<Vec<u8>> = connection.get(self.key(&request.scope)).await?;
148 let state = bytes
149 .map(|bytes| bincode::deserialize(&bytes))
150 .transpose()?
151 .unwrap_or_default();
152 Ok(peek_at(&state, &request, now))
153 }
154 async fn renew(&self, renewal: LeaseRenewal) -> Result<LeaseRenewalResult, Self::Error> {
155 let scope = renewal.scope.clone();
156 self.apply(&scope, renewal.id, |state, now| {
157 renew_at(state, &renewal, now, self.retention)
158 })
159 .await
160 }
161 async fn finish(&self, completion: Completion) -> Result<CompletionResult, Self::Error> {
162 let scope = completion.scope.clone();
163 self.apply(&scope, completion.id, |state, now| {
164 finish_at(state, &completion, now, self.retention)
165 })
166 .await
167 }
168}
169
170async fn redis_time_millis(
171 connection: &mut redis::aio::MultiplexedConnection,
172) -> Result<u64, redis::RedisError> {
173 let (seconds, micros): (u64, u64) = redis::cmd("TIME").query_async(connection).await?;
174 Ok(seconds.saturating_mul(1_000).saturating_add(micros / 1_000))
175}
176fn duration_millis(duration: Duration) -> u64 {
177 duration.as_millis().min(u64::MAX as u128) as u64
178}
179
180#[derive(Debug, Error)]
181pub enum RedisError {
182 #[error("redis operation failed: {0}")]
183 Redis(#[from] redis::RedisError),
184 #[error("stored nopace state is invalid: {0}")]
185 Serialization(#[from] bincode::Error),
186 #[error("redis operation lost its fencing lock")]
187 LostLock,
188}