rusmes-storage 0.1.2

Storage abstraction layer for RusMES — trait-based mailbox and message store with filesystem (maildir), PostgreSQL, and AmateRS distributed backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! AmateRS client — dispatches between mock (in-memory) and real SDK variants.

use super::circuit_breaker::CircuitBreaker;
use super::config::AmatersConfig;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

// Real AmateRS SDK — only compiled when the `amaters-backend` feature is enabled.
// The SDK has C transitive deps (aws-lc-rs, ring) via tonic/rustls; keep opt-in.
#[cfg(feature = "amaters-backend")]
use amaters_sdk_rust::{
    AmateRSClient, CipherBlob as SdkCipherBlob, ClientConfig as SdkClientConfig, Key as SdkKey,
    RetryConfig as SdkRetryConfig,
};

// ---------------------------------------------------------------------------
// Scheme helper (real client only)
// ---------------------------------------------------------------------------

/// Prepend `http://` if no scheme is present in the endpoint string.
#[cfg(feature = "amaters-backend")]
fn ensure_scheme(endpoint: &str) -> String {
    if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
        endpoint.to_owned()
    } else {
        format!("http://{endpoint}")
    }
}

// ---------------------------------------------------------------------------
// Prefix upper-bound helper (real client only)
// ---------------------------------------------------------------------------

/// Compute an exclusive upper bound for a lexicographic prefix range scan.
///
/// Increments the last non-`0xFF` byte of `prefix`.  Returns `None` when all
/// bytes are `0xFF` (very unlikely for human-readable keys).
#[cfg(feature = "amaters-backend")]
fn prefix_upper_bound(prefix: &str) -> Option<Vec<u8>> {
    let mut upper = prefix.as_bytes().to_vec();
    for byte in upper.iter_mut().rev() {
        if *byte < 0xFF {
            *byte += 1;
            return Some(upper);
        }
        *byte = 0x00;
    }
    None
}

// ---------------------------------------------------------------------------
// Inner state for the mock path
// ---------------------------------------------------------------------------

/// State held by the in-memory mock variant of `AmatersClient`.
pub(super) struct MockState {
    pub(super) config: AmatersConfig,
    pub(super) metadata: Arc<RwLock<HashMap<String, Vec<u8>>>>,
    pub(super) blobs: Arc<RwLock<HashMap<String, Vec<u8>>>>,
    pub(super) circuit_breaker: CircuitBreaker,
}

// ---------------------------------------------------------------------------
// AmatersClient — enum dispatching between mock and real SDK client
// ---------------------------------------------------------------------------

/// AmateRS client.
///
/// - `Mock` variant: in-memory HashMap-backed implementation used for unit tests
///   and development builds without the `amaters-backend` feature.
/// - `Real` variant (requires `amaters-backend` feature): wraps the real
///   `amaters-sdk-rust v0.2` gRPC client connected to a live AmateRS cluster.
pub(super) enum AmatersClient {
    Mock(MockState),
    #[cfg(feature = "amaters-backend")]
    Real {
        sdk: Arc<AmateRSClient>,
        metadata_collection: String,
        blob_collection: String,
    },
}

impl AmatersClient {
    /// Create an in-memory mock client (no network, suitable for tests and dev builds).
    pub(super) fn new(config: AmatersConfig) -> Self {
        let circuit_breaker = CircuitBreaker::new(
            config.circuit_breaker_threshold,
            config.circuit_breaker_timeout_ms,
        );
        Self::Mock(MockState {
            config,
            metadata: Arc::new(RwLock::new(HashMap::new())),
            blobs: Arc::new(RwLock::new(HashMap::new())),
            circuit_breaker,
        })
    }

    /// Create a real AmateRS SDK client connected to the cluster.
    ///
    /// Requires the `amaters-backend` feature.  Uses the first endpoint from
    /// `config.cluster_endpoints` as the primary gRPC target; the SDK's own
    /// connection pool and retry logic handle failover for subsequent ops.
    ///
    /// Retry parameters (`max_retries`, `initial_backoff_ms`, `max_backoff_ms`)
    /// are wired directly into the SDK's [`SdkRetryConfig`].
    ///
    /// `replication_factor` and `read/write_consistency` are checked and, when
    /// non-default, emit a `tracing::warn!` because amaters-sdk-rust v0.2.0
    /// does not expose these settings at connect time.
    #[cfg(feature = "amaters-backend")]
    pub(super) async fn new_real(config: &AmatersConfig) -> anyhow::Result<Self> {
        use super::config::ConsistencyLevel;
        use std::time::Duration;

        // Build the SDK retry configuration from our settings.
        let retry_config = SdkRetryConfig {
            max_retries: config.max_retries,
            initial_backoff: Duration::from_millis(config.initial_backoff_ms),
            max_backoff: Duration::from_millis(config.max_backoff_ms),
            // Preserve SDK defaults for parameters not yet surfaced in AmatersConfig.
            backoff_multiplier: 2.0,
            jitter: true,
        };

        // Warn when forward-compat fields are set to non-default values —
        // amaters-sdk-rust v0.2.0 silently ignores them otherwise.
        if config.replication_factor != AmatersConfig::DEFAULT_REPLICATION_FACTOR {
            tracing::warn!(
                target: "rusmes::storage::amaters",
                configured = config.replication_factor,
                default = AmatersConfig::DEFAULT_REPLICATION_FACTOR,
                "amaters: replication_factor config field is set but will be ignored — \
                 amaters-sdk-rust v0.2.0 does not expose replication_factor via its connect API. \
                 The cluster-side replication factor governs replication for now."
            );
        }
        if config.read_consistency != ConsistencyLevel::default()
            || config.write_consistency != ConsistencyLevel::default()
        {
            tracing::warn!(
                target: "rusmes::storage::amaters",
                read_consistency = ?config.read_consistency,
                write_consistency = ?config.write_consistency,
                "amaters: read_consistency/write_consistency config fields are set but will be \
                 ignored — amaters-sdk-rust v0.2.0 has no per-call or per-connection consistency \
                 knob. All operations use the SDK's default consistency."
            );
        }

        if config.cluster_endpoints.is_empty() {
            return Err(anyhow::anyhow!("AmatersConfig has no cluster endpoints"));
        }

        let mut last_err: Option<anyhow::Error> = None;
        let mut connected_sdk: Option<AmateRSClient> = None;
        for endpoint in &config.cluster_endpoints {
            let addr = ensure_scheme(endpoint);
            let sdk_config = SdkClientConfig::new(addr)
                .with_connect_timeout(Duration::from_millis(config.timeout_ms))
                .with_request_timeout(Duration::from_millis(config.timeout_ms.saturating_mul(3)))
                .with_retry_config(retry_config.clone());
            match AmateRSClient::connect_with_config(sdk_config).await {
                Ok(client) => {
                    tracing::info!(
                        target: "rusmes::storage::amaters",
                        "amaters: connected via endpoint {}", endpoint
                    );
                    connected_sdk = Some(client);
                    break;
                }
                Err(e) => {
                    tracing::warn!(
                        target: "rusmes::storage::amaters",
                        endpoint = %endpoint,
                        error = %e,
                        "amaters: connect attempt failed; cycling to next endpoint"
                    );
                    last_err = Some(anyhow::anyhow!("connect to {endpoint} failed: {e}"));
                }
            }
        }
        let sdk = connected_sdk.ok_or_else(|| {
            last_err.unwrap_or_else(|| {
                anyhow::anyhow!(
                    "amaters: all {} cluster endpoints unreachable",
                    config.cluster_endpoints.len()
                )
            })
        })?;

        Ok(Self::Real {
            sdk: Arc::new(sdk),
            metadata_collection: config.metadata_keyspace.clone(),
            blob_collection: config.blob_keyspace.clone(),
        })
    }

    // -----------------------------------------------------------------------
    // Shared interface — dispatches to Mock or Real variant
    // -----------------------------------------------------------------------

    pub(super) async fn connect(&self) -> anyhow::Result<()> {
        match self {
            Self::Mock(state) => {
                tracing::info!(
                    "Connecting to AmateRS cluster (mock) at {:?}",
                    state.config.cluster_endpoints
                );
                if state.circuit_breaker.is_open().await {
                    state.circuit_breaker.attempt_reset().await;
                    if state.circuit_breaker.is_open().await {
                        return Err(anyhow::anyhow!("Circuit breaker is open"));
                    }
                }
                Ok(())
            }
            #[cfg(feature = "amaters-backend")]
            Self::Real { .. } => {
                // The SDK already verified the connection in `new_real`.
                Ok(())
            }
        }
    }

    pub(super) async fn init_keyspaces(&self) -> anyhow::Result<()> {
        match self {
            Self::Mock(state) => {
                tracing::info!(
                    "Initializing keyspaces (mock): {} and {}",
                    state.config.metadata_keyspace,
                    state.config.blob_keyspace
                );
                Ok(())
            }
            #[cfg(feature = "amaters-backend")]
            Self::Real { .. } => {
                // Keyspace management is server-side in AmateRS.
                Ok(())
            }
        }
    }

    pub(super) async fn put(
        &self,
        keyspace: &str,
        key: String,
        value: Vec<u8>,
    ) -> anyhow::Result<()> {
        match self {
            Self::Mock(state) => {
                if state.circuit_breaker.is_open().await {
                    state.circuit_breaker.attempt_reset().await;
                    if state.circuit_breaker.is_open().await {
                        return Err(anyhow::anyhow!(
                            "Circuit breaker is open, rejecting request"
                        ));
                    }
                }

                let store = if keyspace.contains("blob") {
                    &state.blobs
                } else {
                    &state.metadata
                };

                // Retry logic with exponential backoff.
                // (The mock HashMap insert is infallible; the loop structure mirrors
                //  the real retry pattern for future parity.)
                let mut last_error: Option<anyhow::Error> = None;
                for attempt in 0..state.config.max_retries {
                    let insert_result: anyhow::Result<()> = {
                        let mut map = store.write().await;
                        map.insert(key.clone(), value.clone());
                        Ok(())
                    };
                    match insert_result {
                        Ok(()) => {
                            state.circuit_breaker.record_success().await;
                            return Ok(());
                        }
                        Err(e) => {
                            tracing::warn!("Mock put failed (attempt {}): {}", attempt + 1, e);
                            last_error = Some(e);
                            if attempt < state.config.max_retries - 1 {
                                let backoff = 100 * 2_u64.pow(attempt as u32);
                                tokio::time::sleep(tokio::time::Duration::from_millis(backoff))
                                    .await;
                            }
                        }
                    }
                }

                state.circuit_breaker.record_failure().await;
                Err(last_error.unwrap_or_else(|| anyhow::anyhow!("Mock put operation failed")))
            }

            #[cfg(feature = "amaters-backend")]
            Self::Real {
                sdk,
                metadata_collection,
                blob_collection,
            } => {
                let collection = if keyspace.contains("blob") {
                    blob_collection.as_str()
                } else {
                    metadata_collection.as_str()
                };
                let sdk_key = SdkKey::from_str(&key);
                let sdk_value = SdkCipherBlob::new(value);
                sdk.set(collection, &sdk_key, &sdk_value)
                    .await
                    .map_err(|e| anyhow::anyhow!("AmateRS set error: {e}"))
            }
        }
    }

    pub(super) async fn get(&self, keyspace: &str, key: &str) -> anyhow::Result<Option<Vec<u8>>> {
        match self {
            Self::Mock(state) => {
                let store = if keyspace.contains("blob") {
                    &state.blobs
                } else {
                    &state.metadata
                };
                let map = store.read().await;
                Ok(map.get(key).cloned())
            }

            #[cfg(feature = "amaters-backend")]
            Self::Real {
                sdk,
                metadata_collection,
                blob_collection,
            } => {
                let collection = if keyspace.contains("blob") {
                    blob_collection.as_str()
                } else {
                    metadata_collection.as_str()
                };
                let sdk_key = SdkKey::from_str(key);
                let result = sdk
                    .get(collection, &sdk_key)
                    .await
                    .map_err(|e| anyhow::anyhow!("AmateRS get error: {e}"))?;
                Ok(result.map(|blob| blob.as_bytes().to_vec()))
            }
        }
    }

    pub(super) async fn delete(&self, keyspace: &str, key: &str) -> anyhow::Result<()> {
        match self {
            Self::Mock(state) => {
                let store = if keyspace.contains("blob") {
                    &state.blobs
                } else {
                    &state.metadata
                };
                let mut map = store.write().await;
                map.remove(key);
                Ok(())
            }

            #[cfg(feature = "amaters-backend")]
            Self::Real {
                sdk,
                metadata_collection,
                blob_collection,
            } => {
                let collection = if keyspace.contains("blob") {
                    blob_collection.as_str()
                } else {
                    metadata_collection.as_str()
                };
                let sdk_key = SdkKey::from_str(key);
                sdk.delete(collection, &sdk_key)
                    .await
                    .map_err(|e| anyhow::anyhow!("AmateRS delete error: {e}"))
            }
        }
    }

    pub(super) async fn list_prefix(
        &self,
        keyspace: &str,
        prefix: &str,
    ) -> anyhow::Result<Vec<String>> {
        match self {
            Self::Mock(state) => {
                let store = if keyspace.contains("blob") {
                    &state.blobs
                } else {
                    &state.metadata
                };
                let map = store.read().await;
                Ok(map
                    .keys()
                    .filter(|k| k.starts_with(prefix))
                    .cloned()
                    .collect())
            }

            #[cfg(feature = "amaters-backend")]
            Self::Real {
                sdk,
                metadata_collection,
                blob_collection,
            } => {
                let collection = if keyspace.contains("blob") {
                    blob_collection.as_str()
                } else {
                    metadata_collection.as_str()
                };

                let start = SdkKey::from_str(prefix);
                // Compute an exclusive upper bound for the prefix range scan.
                // The SDK `range` uses lexicographic ordering; incrementing the last
                // non-0xFF byte gives a tight upper bound.
                let upper_bytes = prefix_upper_bound(prefix).unwrap_or_else(|| vec![0xFF; 32]);
                let end = SdkKey::from_slice(&upper_bytes);

                let pairs = sdk
                    .range(collection, &start, &end)
                    .await
                    .map_err(|e| anyhow::anyhow!("AmateRS range error: {e}"))?;

                // Convert key bytes back to strings; filter defensively.
                let keys = pairs
                    .into_iter()
                    .map(|(k, _v)| k.to_string_lossy())
                    .filter(|s| s.starts_with(prefix))
                    .collect();

                Ok(keys)
            }
        }
    }
}