oximedia-distributed 0.2.1

Distributed encoding coordinator for OxiMedia
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
//! S3 / object-storage integration for distributed segment I/O.
//!
//! Provides the [`SegmentStore`] trait and two implementations:
//!
//! - [`S3SegmentStore`] — uploads/downloads segments via HTTP PUT/GET using
//!   the `reqwest` HTTP client (compatible with any S3-API endpoint).
//! - [`InMemorySegmentStore`] — stores segments in a `HashMap`; useful for
//!   unit tests and local development without a real S3 bucket.
//!
//! # Feature gate
//!
//! This module is gated behind the `s3` Cargo feature:
//!
//! ```toml
//! oximedia-distributed = { version = "…", features = ["s3"] }
//! ```

use bytes::Bytes;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;

/// Configuration for an S3-compatible endpoint.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct S3Config {
    /// S3 bucket name.
    pub bucket: String,
    /// AWS region string (e.g. `"us-east-1"`).
    pub region: String,
    /// Optional custom endpoint URL for S3-compatible services (MinIO, Wasabi,
    /// Cloudflare R2, …).  Defaults to the standard AWS endpoint when `None`.
    pub endpoint_url: Option<String>,
    /// Optional AWS access key ID.
    pub access_key_id: Option<String>,
    /// Optional AWS secret access key.
    pub secret_access_key: Option<String>,
}

impl S3Config {
    /// Build the base URL for object operations.
    ///
    /// Format: `{endpoint}/{bucket}/` or the standard AWS URL.
    #[must_use]
    pub fn base_url(&self) -> String {
        if let Some(ref endpoint) = self.endpoint_url {
            format!("{}/{}", endpoint.trim_end_matches('/'), self.bucket)
        } else {
            format!("https://{}.s3.{}.amazonaws.com", self.bucket, self.region)
        }
    }
}

impl Default for S3Config {
    fn default() -> Self {
        Self {
            bucket: "oximedia-segments".to_string(),
            region: "us-east-1".to_string(),
            endpoint_url: None,
            access_key_id: None,
            secret_access_key: None,
        }
    }
}

/// Result type used throughout this module.
pub type Result<T> = std::result::Result<T, SegmentStoreError>;

/// Errors produced by segment store operations.
#[derive(Debug, thiserror::Error)]
pub enum SegmentStoreError {
    #[error("HTTP error during segment upload/download: {0}")]
    Http(String),

    #[error("Segment not found: {key}")]
    NotFound { key: String },

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Configuration error: {0}")]
    Config(String),
}

/// Async trait for segment storage backends.
#[async_trait::async_trait]
pub trait SegmentStore: Send + Sync {
    /// Upload `data` and return the storage key under which it was stored.
    async fn upload_segment(&self, id: Uuid, data: Bytes) -> Result<String>;

    /// Download a segment by its storage key.
    async fn download_segment(&self, key: &str) -> Result<Bytes>;

    /// Delete a segment by its storage key.
    async fn delete_segment(&self, key: &str) -> Result<()>;

    /// Check whether a segment exists.
    async fn segment_exists(&self, key: &str) -> Result<bool>;
}

/// Derives the canonical S3 object key for a segment UUID.
///
/// Format: `segments/{uuid}` — keeps all segment objects under a common prefix
/// for lifecycle policies and bulk operations.
#[must_use]
pub fn segment_key(id: Uuid) -> String {
    format!("segments/{id}")
}

/// S3-backed segment store that uses HTTP PUT/GET to interact with any
/// S3-compatible object storage API.
pub struct S3SegmentStore {
    config: S3Config,
    client: reqwest::Client,
}

impl S3SegmentStore {
    /// Create a new S3 segment store with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns [`SegmentStoreError::Config`] if the HTTP client cannot be built.
    pub fn new(config: S3Config) -> Result<Self> {
        let client = reqwest::Client::builder()
            .build()
            .map_err(|e| SegmentStoreError::Config(e.to_string()))?;
        Ok(Self { config, client })
    }

    fn object_url(&self, key: &str) -> String {
        format!("{}/{}", self.config.base_url(), key)
    }
}

#[async_trait::async_trait]
impl SegmentStore for S3SegmentStore {
    async fn upload_segment(&self, id: Uuid, data: Bytes) -> Result<String> {
        let key = segment_key(id);
        let url = self.object_url(&key);

        let mut req = self.client.put(&url).body(data);

        if let (Some(ref aki), Some(ref sak)) = (
            self.config.access_key_id.as_ref(),
            self.config.secret_access_key.as_ref(),
        ) {
            req = req.basic_auth(aki, Some(sak));
        }

        let resp = req
            .send()
            .await
            .map_err(|e| SegmentStoreError::Http(e.to_string()))?;

        if !resp.status().is_success() {
            return Err(SegmentStoreError::Http(format!(
                "PUT {} returned HTTP {}",
                url,
                resp.status()
            )));
        }

        Ok(key)
    }

    async fn download_segment(&self, key: &str) -> Result<Bytes> {
        let url = self.object_url(key);

        let mut req = self.client.get(&url);

        if let (Some(ref aki), Some(ref sak)) = (
            self.config.access_key_id.as_ref(),
            self.config.secret_access_key.as_ref(),
        ) {
            req = req.basic_auth(aki, Some(sak));
        }

        let resp = req
            .send()
            .await
            .map_err(|e| SegmentStoreError::Http(e.to_string()))?;

        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SegmentStoreError::NotFound {
                key: key.to_string(),
            });
        }

        if !resp.status().is_success() {
            return Err(SegmentStoreError::Http(format!(
                "GET {} returned HTTP {}",
                url,
                resp.status()
            )));
        }

        resp.bytes()
            .await
            .map_err(|e| SegmentStoreError::Http(e.to_string()))
    }

    async fn delete_segment(&self, key: &str) -> Result<()> {
        let url = self.object_url(key);

        let mut req = self.client.delete(&url);

        if let (Some(ref aki), Some(ref sak)) = (
            self.config.access_key_id.as_ref(),
            self.config.secret_access_key.as_ref(),
        ) {
            req = req.basic_auth(aki, Some(sak));
        }

        let resp = req
            .send()
            .await
            .map_err(|e| SegmentStoreError::Http(e.to_string()))?;

        if !resp.status().is_success() && resp.status() != reqwest::StatusCode::NOT_FOUND {
            return Err(SegmentStoreError::Http(format!(
                "DELETE {} returned HTTP {}",
                url,
                resp.status()
            )));
        }

        Ok(())
    }

    async fn segment_exists(&self, key: &str) -> Result<bool> {
        let url = self.object_url(key);

        let mut req = self.client.head(&url);

        if let (Some(ref aki), Some(ref sak)) = (
            self.config.access_key_id.as_ref(),
            self.config.secret_access_key.as_ref(),
        ) {
            req = req.basic_auth(aki, Some(sak));
        }

        let resp = req
            .send()
            .await
            .map_err(|e| SegmentStoreError::Http(e.to_string()))?;

        Ok(resp.status().is_success())
    }
}

/// In-memory segment store for unit tests and local development.
///
/// Thread-safe via an `Arc<RwLock<HashMap>>`.
#[derive(Debug, Clone, Default)]
pub struct InMemorySegmentStore {
    store: Arc<RwLock<HashMap<String, Bytes>>>,
}

impl InMemorySegmentStore {
    /// Create a new, empty in-memory store.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of segments currently in the store.
    pub async fn len(&self) -> usize {
        self.store.read().await.len()
    }

    /// Returns `true` if the store contains no segments.
    pub async fn is_empty(&self) -> bool {
        self.store.read().await.is_empty()
    }
}

#[async_trait::async_trait]
impl SegmentStore for InMemorySegmentStore {
    async fn upload_segment(&self, id: Uuid, data: Bytes) -> Result<String> {
        let key = segment_key(id);
        self.store.write().await.insert(key.clone(), data);
        Ok(key)
    }

    async fn download_segment(&self, key: &str) -> Result<Bytes> {
        self.store
            .read()
            .await
            .get(key)
            .cloned()
            .ok_or_else(|| SegmentStoreError::NotFound {
                key: key.to_string(),
            })
    }

    async fn delete_segment(&self, key: &str) -> Result<()> {
        self.store.write().await.remove(key);
        Ok(())
    }

    async fn segment_exists(&self, key: &str) -> Result<bool> {
        Ok(self.store.read().await.contains_key(key))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_in_memory_store_upload_download_roundtrip() {
        let store = InMemorySegmentStore::new();
        let id = Uuid::new_v4();
        let data = Bytes::from_static(b"hello segment data");

        let key = store
            .upload_segment(id, data.clone())
            .await
            .expect("upload should succeed");

        let downloaded = store
            .download_segment(&key)
            .await
            .expect("download should succeed");

        assert_eq!(downloaded, data);
    }

    #[tokio::test]
    async fn test_s3_store_key_format() {
        let store = InMemorySegmentStore::new();
        let id = Uuid::new_v4();
        let key = store
            .upload_segment(id, Bytes::from("test"))
            .await
            .expect("upload");

        assert!(
            key.starts_with("segments/"),
            "key should start with 'segments/': {key}"
        );
        assert!(key.contains(&id.to_string()), "key should contain UUID");
    }

    #[tokio::test]
    async fn test_in_memory_download_missing_key_fails() {
        let store = InMemorySegmentStore::new();
        let result = store.download_segment("segments/nonexistent").await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SegmentStoreError::NotFound { .. }
        ));
    }

    #[tokio::test]
    async fn test_in_memory_delete_removes_segment() {
        let store = InMemorySegmentStore::new();
        let id = Uuid::new_v4();
        let key = store
            .upload_segment(id, Bytes::from("data"))
            .await
            .expect("upload");

        assert!(store.segment_exists(&key).await.unwrap());
        store.delete_segment(&key).await.expect("delete");
        assert!(!store.segment_exists(&key).await.unwrap());
    }

    #[tokio::test]
    async fn test_in_memory_store_len() {
        let store = InMemorySegmentStore::new();
        assert!(store.is_empty().await);

        store
            .upload_segment(Uuid::new_v4(), Bytes::from("a"))
            .await
            .unwrap();
        store
            .upload_segment(Uuid::new_v4(), Bytes::from("b"))
            .await
            .unwrap();

        assert_eq!(store.len().await, 2);
    }

    #[tokio::test]
    async fn test_in_memory_concurrent_access() {
        use std::sync::Arc;
        let store = Arc::new(InMemorySegmentStore::new());
        let mut handles = Vec::new();

        for i in 0_u8..8 {
            let s = Arc::clone(&store);
            handles.push(tokio::spawn(async move {
                let id = Uuid::new_v4();
                let data = Bytes::copy_from_slice(&[i; 64]);
                let key = s.upload_segment(id, data.clone()).await.unwrap();
                let downloaded = s.download_segment(&key).await.unwrap();
                assert_eq!(downloaded, data);
            }));
        }

        for h in handles {
            h.await.expect("task should not panic");
        }

        assert_eq!(store.len().await, 8);
    }

    #[test]
    fn test_s3_config_base_url_default() {
        let config = S3Config::default();
        let url = config.base_url();
        assert!(url.contains("amazonaws.com"));
        assert!(url.contains("oximedia-segments"));
    }

    #[test]
    fn test_s3_config_custom_endpoint() {
        let config = S3Config {
            bucket: "my-bucket".to_string(),
            region: "us-east-1".to_string(),
            endpoint_url: Some("http://localhost:9000".to_string()),
            ..Default::default()
        };
        let url = config.base_url();
        assert!(url.starts_with("http://localhost:9000"));
        assert!(url.contains("my-bucket"));
    }

    #[test]
    fn test_segment_key_format() {
        let id = Uuid::parse_str("12345678-1234-1234-1234-123456789012").unwrap();
        let key = segment_key(id);
        assert_eq!(key, "segments/12345678-1234-1234-1234-123456789012");
    }
}