ferrokinesis 0.1.0

A local AWS Kinesis mock server for testing, written in Rust
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::constants;
use crate::error::KinesisErrorResponse;
use crate::types::{Consumer, StoredRecord, Stream};
use redb::backends::InMemoryBackend;
use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
use serde_json::Value;
use std::collections::BTreeMap;
use std::sync::Arc;

const STREAMS: TableDefinition<&str, &[u8]> = TableDefinition::new("streams");
const RECORDS: TableDefinition<&str, &[u8]> = TableDefinition::new("records");
const CONSUMERS: TableDefinition<&str, &[u8]> = TableDefinition::new("consumers");
const POLICIES: TableDefinition<&str, &str> = TableDefinition::new("policies");
const RESOURCE_TAGS: TableDefinition<&str, &[u8]> = TableDefinition::new("resource_tags");
const ACCOUNT_SETTINGS: TableDefinition<&str, &[u8]> = TableDefinition::new("account_settings");

#[derive(Debug, Clone)]
pub struct StoreOptions {
    pub create_stream_ms: u64,
    pub delete_stream_ms: u64,
    pub update_stream_ms: u64,
    pub shard_limit: u32,
}

impl Default for StoreOptions {
    fn default() -> Self {
        Self {
            create_stream_ms: 500,
            delete_stream_ms: 500,
            update_stream_ms: 500,
            shard_limit: 10,
        }
    }
}

#[derive(Clone)]
pub struct Store {
    pub options: StoreOptions,
    pub aws_account_id: String,
    pub aws_region: String,
    db: Arc<Database>,
}

/// Serialize a Stream for storage, including hidden fields (seq_ix, tags)
fn serialize_stream(stream: &Stream) -> Vec<u8> {
    let mut val = serde_json::to_value(stream).unwrap();
    let obj = val.as_object_mut().unwrap();
    obj.insert(
        "_seq_ix".to_string(),
        serde_json::to_value(&stream.seq_ix).unwrap(),
    );
    obj.insert(
        "_tags".to_string(),
        serde_json::to_value(&stream.tags).unwrap(),
    );
    if let Some(ref key_id) = stream.key_id {
        obj.insert("_key_id".to_string(), serde_json::to_value(key_id).unwrap());
    }
    obj.insert(
        "_warm_throughput_mibps".to_string(),
        serde_json::to_value(stream.warm_throughput_mibps).unwrap(),
    );
    obj.insert(
        "_max_record_size_kib".to_string(),
        serde_json::to_value(stream.max_record_size_kib).unwrap(),
    );
    serde_json::to_vec(&val).unwrap()
}

/// Deserialize a Stream from storage, restoring hidden fields
fn deserialize_stream(bytes: &[u8]) -> Stream {
    let val: Value = serde_json::from_slice(bytes).unwrap();
    let mut stream: Stream = serde_json::from_value(val.clone()).unwrap();
    if let Some(arr) = val.get("_seq_ix") {
        stream.seq_ix = serde_json::from_value(arr.clone()).unwrap_or_default();
    }
    if let Some(obj) = val.get("_tags") {
        stream.tags = serde_json::from_value(obj.clone()).unwrap_or_default();
    }
    if let Some(key_id) = val.get("_key_id") {
        stream.key_id = serde_json::from_value(key_id.clone()).ok();
    }
    if let Some(v) = val.get("_warm_throughput_mibps") {
        stream.warm_throughput_mibps = serde_json::from_value(v.clone()).unwrap_or(0);
    }
    if let Some(v) = val.get("_max_record_size_kib") {
        stream.max_record_size_kib = serde_json::from_value(v.clone()).unwrap_or(1024);
    }
    stream
}

/// Composite key for records: "{stream_name}\0{shard_hex}/{seq_num}"
fn record_key(stream_name: &str, shard_key: &str) -> String {
    format!("{stream_name}\0{shard_key}")
}

/// Range bounds for all records in a stream's shard range
fn record_range(stream_name: &str, start: &str, end: &str) -> (String, String) {
    (
        format!("{stream_name}\0{start}"),
        format!("{stream_name}\0{end}"),
    )
}

impl Store {
    pub fn new(options: StoreOptions) -> Self {
        let aws_account_id = std::env::var("AWS_ACCOUNT_ID")
            .unwrap_or_else(|_| "0000-0000-0000".to_string())
            .chars()
            .filter(|c| c.is_ascii_digit())
            .collect();

        let aws_region = std::env::var("AWS_REGION")
            .or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
            .unwrap_or_else(|_| "us-east-1".to_string());

        let db = Database::builder()
            .create_with_backend(InMemoryBackend::new())
            .expect("Failed to create in-memory redb database");

        // Create tables
        let write_txn = db.begin_write().unwrap();
        write_txn.open_table(STREAMS).unwrap();
        write_txn.open_table(RECORDS).unwrap();
        write_txn.open_table(CONSUMERS).unwrap();
        write_txn.open_table(POLICIES).unwrap();
        write_txn.open_table(RESOURCE_TAGS).unwrap();
        write_txn.open_table(ACCOUNT_SETTINGS).unwrap();
        write_txn.commit().unwrap();

        Self {
            options,
            aws_account_id,
            aws_region,
            db: Arc::new(db),
        }
    }

    pub async fn get_stream(&self, name: &str) -> Result<Stream, KinesisErrorResponse> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(STREAMS).unwrap();
        match table.get(name).unwrap() {
            Some(guard) => Ok(deserialize_stream(guard.value())),
            None => Err(KinesisErrorResponse::client_error(
                constants::RESOURCE_NOT_FOUND,
                Some(&format!(
                    "Stream {} under account {} not found.",
                    name, self.aws_account_id
                )),
            )),
        }
    }

    pub async fn put_stream(&self, name: &str, stream: Stream) {
        let bytes = serialize_stream(&stream);
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(STREAMS).unwrap();
            table.insert(name, bytes.as_slice()).unwrap();
        }
        write_txn.commit().unwrap();
    }

    pub async fn delete_stream(&self, name: &str) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut streams = write_txn.open_table(STREAMS).unwrap();
            streams.remove(name).unwrap();

            // Delete all records for this stream
            let mut records = write_txn.open_table(RECORDS).unwrap();
            let prefix = format!("{name}\0");
            let prefix_end = format!("{name}\x01");
            let keys_to_remove: Vec<String> = records
                .range(prefix.as_str()..prefix_end.as_str())
                .unwrap()
                .map(|r| {
                    let (k, _) = r.unwrap();
                    k.value().to_string()
                })
                .collect();
            for key in keys_to_remove {
                records.remove(key.as_str()).unwrap();
            }
        }
        write_txn.commit().unwrap();
    }

    /// Check if a stream exists
    pub async fn contains_stream(&self, name: &str) -> bool {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(STREAMS).unwrap();
        table.get(name).unwrap().is_some()
    }

    /// List all stream names (sorted)
    pub async fn list_stream_names(&self) -> Vec<String> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(STREAMS).unwrap();
        table
            .iter()
            .unwrap()
            .map(|r| {
                let (k, _) = r.unwrap();
                k.value().to_string()
            })
            .collect()
    }

    /// Update a stream in a read-modify-write transaction.
    /// Returns the result of the closure.
    pub async fn update_stream<F, R>(&self, name: &str, f: F) -> Result<R, KinesisErrorResponse>
    where
        F: FnOnce(&mut Stream) -> Result<R, KinesisErrorResponse>,
    {
        let write_txn = self.db.begin_write().unwrap();
        let result;
        {
            let mut table = write_txn.open_table(STREAMS).unwrap();
            let bytes = table.get(name).unwrap().ok_or_else(|| {
                KinesisErrorResponse::client_error(
                    "ResourceNotFoundException",
                    Some(&format!(
                        "Stream {} under account {} not found.",
                        name, self.aws_account_id
                    )),
                )
            })?;
            let mut stream = deserialize_stream(bytes.value());
            drop(bytes);
            result = f(&mut stream)?;
            let new_bytes = serialize_stream(&stream);
            table.insert(name, new_bytes.as_slice()).unwrap();
        }
        write_txn.commit().unwrap();
        Ok(result)
    }

    /// Execute a closure with write access to all streams.
    /// The closure receives a mutable BTreeMap of all streams.
    /// Changes are written back to the database.
    pub async fn with_streams_write<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BTreeMap<String, Stream>, &StoreOptions, &str, &str) -> R,
    {
        let write_txn = self.db.begin_write().unwrap();
        let result;
        {
            let mut table = write_txn.open_table(STREAMS).unwrap();
            let mut streams: BTreeMap<String, Stream> = table
                .iter()
                .unwrap()
                .map(|r| {
                    let (k, v) = r.unwrap();
                    (k.value().to_string(), deserialize_stream(v.value()))
                })
                .collect();

            result = f(
                &mut streams,
                &self.options,
                &self.aws_account_id,
                &self.aws_region,
            );

            // Write all streams back
            // First, collect existing keys to detect deletions
            let existing_keys: Vec<String> = table
                .iter()
                .unwrap()
                .map(|r| r.unwrap().0.value().to_string())
                .collect();

            for key in &existing_keys {
                if !streams.contains_key(key) {
                    table.remove(key.as_str()).unwrap();
                }
            }
            for (name, stream) in &streams {
                let bytes = serialize_stream(stream);
                table.insert(name.as_str(), bytes.as_slice()).unwrap();
            }
        }
        write_txn.commit().unwrap();
        result
    }

    pub async fn get_record_store(&self, stream_name: &str) -> BTreeMap<String, StoredRecord> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(RECORDS).unwrap();
        let prefix = format!("{stream_name}\0");
        let prefix_end = format!("{stream_name}\x01");
        table
            .range(prefix.as_str()..prefix_end.as_str())
            .unwrap()
            .map(|r| {
                let (k, v) = r.unwrap();
                let full_key = k.value().to_string();
                // Strip the stream_name\0 prefix to get the shard_hex/seq_num key
                let shard_key = full_key
                    .strip_prefix(&prefix)
                    .unwrap_or(&full_key)
                    .to_string();
                let record: StoredRecord = serde_json::from_slice(v.value()).unwrap();
                (shard_key, record)
            })
            .collect()
    }

    pub async fn put_record(&self, stream_name: &str, key: &str, record: StoredRecord) {
        let composite_key = record_key(stream_name, key);
        let bytes = serde_json::to_vec(&record).unwrap();
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(RECORDS).unwrap();
            table
                .insert(composite_key.as_str(), bytes.as_slice())
                .unwrap();
        }
        write_txn.commit().unwrap();
    }

    pub async fn put_records_batch(&self, stream_name: &str, batch: Vec<(String, StoredRecord)>) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(RECORDS).unwrap();
            for (key, record) in &batch {
                let composite_key = record_key(stream_name, key);
                let bytes = serde_json::to_vec(record).unwrap();
                table
                    .insert(composite_key.as_str(), bytes.as_slice())
                    .unwrap();
            }
        }
        write_txn.commit().unwrap();
    }

    pub async fn delete_record_keys(&self, stream_name: &str, keys: &[String]) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(RECORDS).unwrap();
            for key in keys {
                let composite_key = record_key(stream_name, key);
                table.remove(composite_key.as_str()).unwrap();
            }
        }
        write_txn.commit().unwrap();
    }

    pub async fn sum_open_shards(&self) -> u32 {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(STREAMS).unwrap();
        table
            .iter()
            .unwrap()
            .map(|r| {
                let (_, v) = r.unwrap();
                let stream = deserialize_stream(v.value());
                stream
                    .shards
                    .iter()
                    .filter(|s| s.sequence_number_range.ending_sequence_number.is_none())
                    .count() as u32
            })
            .sum()
    }

    /// Get records in a specific range for a stream
    pub async fn get_records_range(
        &self,
        stream_name: &str,
        range_start: &str,
        range_end: &str,
    ) -> Vec<(String, StoredRecord)> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(RECORDS).unwrap();
        let (start, end) = record_range(stream_name, range_start, range_end);
        table
            .range(start.as_str()..end.as_str())
            .unwrap()
            .map(|r| {
                let (k, v) = r.unwrap();
                let full_key = k.value().to_string();
                let prefix = format!("{stream_name}\0");
                let shard_key = full_key
                    .strip_prefix(&prefix)
                    .unwrap_or(&full_key)
                    .to_string();
                let record: StoredRecord = serde_json::from_slice(v.value()).unwrap();
                (shard_key, record)
            })
            .collect()
    }

    // --- Consumer operations ---

    pub async fn put_consumer(&self, consumer_arn: &str, consumer: Consumer) {
        let bytes = serde_json::to_vec(&consumer).unwrap();
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(CONSUMERS).unwrap();
            table.insert(consumer_arn, bytes.as_slice()).unwrap();
        }
        write_txn.commit().unwrap();
    }

    pub async fn get_consumer(&self, consumer_arn: &str) -> Option<Consumer> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(CONSUMERS).unwrap();
        table
            .get(consumer_arn)
            .unwrap()
            .map(|guard| serde_json::from_slice(guard.value()).unwrap())
    }

    pub async fn delete_consumer(&self, consumer_arn: &str) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(CONSUMERS).unwrap();
            table.remove(consumer_arn).unwrap();
        }
        write_txn.commit().unwrap();
    }

    pub async fn list_consumers_for_stream(&self, stream_arn: &str) -> Vec<Consumer> {
        let prefix = format!("{stream_arn}/consumer/");
        let prefix_end = format!("{stream_arn}/consumer0"); // '0' > '/'
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(CONSUMERS).unwrap();
        table
            .range(prefix.as_str()..prefix_end.as_str())
            .unwrap()
            .map(|r| {
                let (_, v) = r.unwrap();
                serde_json::from_slice(v.value()).unwrap()
            })
            .collect()
    }

    /// Find a consumer by stream ARN + consumer name
    pub async fn find_consumer(&self, stream_arn: &str, consumer_name: &str) -> Option<Consumer> {
        let consumers = self.list_consumers_for_stream(stream_arn).await;
        consumers
            .into_iter()
            .find(|c| c.consumer_name == consumer_name)
    }

    // --- Resource policy operations ---

    pub async fn put_policy(&self, resource_arn: &str, policy: &str) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(POLICIES).unwrap();
            table.insert(resource_arn, policy).unwrap();
        }
        write_txn.commit().unwrap();
    }

    pub async fn get_policy(&self, resource_arn: &str) -> Option<String> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(POLICIES).unwrap();
        table
            .get(resource_arn)
            .unwrap()
            .map(|guard| guard.value().to_string())
    }

    pub async fn delete_policy(&self, resource_arn: &str) {
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(POLICIES).unwrap();
            table.remove(resource_arn).unwrap();
        }
        write_txn.commit().unwrap();
    }

    /// Resolve a stream name from a stream ARN
    pub fn stream_name_from_arn(&self, arn: &str) -> Option<String> {
        // Format: arn:aws:kinesis:{region}:{account}:stream/{name}
        arn.split("/").nth(1).map(|s| s.to_string())
    }

    // --- Resource tag operations (for non-stream resources like consumers) ---

    pub async fn get_resource_tags(&self, resource_arn: &str) -> BTreeMap<String, String> {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(RESOURCE_TAGS).unwrap();
        table
            .get(resource_arn)
            .unwrap()
            .map(|guard| serde_json::from_slice(guard.value()).unwrap_or_default())
            .unwrap_or_default()
    }

    pub async fn put_resource_tags(&self, resource_arn: &str, tags: &BTreeMap<String, String>) {
        let bytes = serde_json::to_vec(tags).unwrap();
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(RESOURCE_TAGS).unwrap();
            table.insert(resource_arn, bytes.as_slice()).unwrap();
        }
        write_txn.commit().unwrap();
    }

    // --- Account settings operations ---

    pub async fn get_account_settings(&self) -> Value {
        let read_txn = self.db.begin_read().unwrap();
        let table = read_txn.open_table(ACCOUNT_SETTINGS).unwrap();
        table
            .get("account_settings")
            .unwrap()
            .map(|guard| serde_json::from_slice(guard.value()).unwrap())
            .unwrap_or(Value::Object(Default::default()))
    }

    pub async fn put_account_settings(&self, settings: &Value) {
        let bytes = serde_json::to_vec(settings).unwrap();
        let write_txn = self.db.begin_write().unwrap();
        {
            let mut table = write_txn.open_table(ACCOUNT_SETTINGS).unwrap();
            table.insert("account_settings", bytes.as_slice()).unwrap();
        }
        write_txn.commit().unwrap();
    }
}