go_zoom_kinesis/store/
dynamodb.rs

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
//! DynamoDB implementation of checkpoint storage with configurable retry behavior
//!
//! By default, the DynamoDB checkpoint store will retry indefinitely to ensure no checkpoints are lost.
//! This behavior can be modified using the builder pattern.
//!
//! # Examples
//!
//! ```rust,no_run
//! use go_zoom_kinesis::store::{DynamoDbCheckpointStore};
//! use std::time::Duration;
//! #
//! use go_zoom_kinesis::retry::RetryConfig;
//!
//! async fn example() -> anyhow::Result<()> {
//! # let dynamo_client = aws_sdk_dynamodb::Client::new(&aws_config::load_from_env().await);
//! // Default configuration - will retry forever
//! let store = DynamoDbCheckpointStore::builder()
//!     .with_client(dynamo_client.clone())
//!     .with_table_name("checkpoints".to_string())
//!     .build()?;
//!
//! // Custom configuration with limited retries
//! let store = DynamoDbCheckpointStore::builder()
//!     .with_client(dynamo_client)
//!     .with_table_name("checkpoints".to_string())
//!     .with_retry_config(RetryConfig {
//!         max_retries: Some(3),  // Try 3 times then warn and continue
//!         initial_backoff: Duration::from_millis(100),
//!         max_backoff: Duration::from_secs(5),
//!         jitter_factor: 0.1,
//!     })
//!     .build()?;
//! # Ok(())
//! # }
//! ```

use crate::retry::backoff::Backoff;
use crate::retry::RetryConfig;
use crate::retry::RetryHandle;
use crate::ExponentialBackoff;
use anyhow::Context;
use std::time::Duration;
use tracing::warn;

use {
    crate::store::CheckpointStore,
    async_trait::async_trait,
    aws_sdk_dynamodb::{types::AttributeValue, Client as DynamoClient},
    tracing::{debug, instrument, trace},
};

#[derive(Debug, Clone)]
pub struct DynamoDbCheckpointStore {
    client: DynamoClient,
    table_name: String,
    key_prefix: String,
    retry_config: RetryConfig,
    backoff: ExponentialBackoff,
}

impl DynamoDbCheckpointStore {
    pub fn builder() -> DynamoDbCheckpointStoreBuilder {
        DynamoDbCheckpointStoreBuilder::new()
    }
    pub fn new(client: DynamoClient, table_name: String, key_prefix: String) -> Self {
        Self::builder()
            .with_client(client)
            .with_table_name(table_name)
            .with_key_prefix(key_prefix)
            .build()
            .expect("Failed to create DynamoDbCheckpointStore with default configuration")
    }

    fn prefixed_key(&self, shard_id: &str) -> String {
        format!("{}{}", self.key_prefix, shard_id)
    }
}

#[async_trait]
impl CheckpointStore for DynamoDbCheckpointStore {
    #[instrument(skip(self), fields(table = %self.table_name, prefix = %self.key_prefix))]
    async fn get_checkpoint(&self, shard_id: &str) -> anyhow::Result<Option<String>> {
        let key = self.prefixed_key(shard_id);
        let mut retry = RetryHandle::new(self.retry_config.clone(), self.backoff.clone());

        trace!(
            shard_id = %shard_id,
            key = %key,
            "Getting checkpoint from DynamoDB"
        );

        let checkpoint = retry
            .retry(
                || async {
                    let response = self
                        .client
                        .get_item()
                        .table_name(&self.table_name)
                        .key("shard_id", AttributeValue::S(key.clone()))
                        .send()
                        .await
                        .context("Failed to get checkpoint from DynamoDB")?;

                    let checkpoint = response
                        .item
                        .and_then(|item| item.get("sequence_number").cloned())
                        .and_then(|attr| attr.as_s().ok().map(|s| s.to_string()));

                    Ok::<Option<String>, anyhow::Error>(checkpoint)
                },
                &mut tokio::sync::watch::channel(false).1,
            )
            .await?;

        debug!(
            shard_id = %shard_id,
            key = %key,
            checkpoint = ?checkpoint,
            "Retrieved checkpoint from DynamoDB"
        );

        Ok(checkpoint)
    }

    async fn save_checkpoint(&self, shard_id: &str, sequence_number: &str) -> anyhow::Result<()> {
        let key = self.prefixed_key(shard_id);
        let mut retry = RetryHandle::new(self.retry_config.clone(), self.backoff.clone());
        let mut attempt = 0;

        debug!(
            shard_id = %shard_id,
            key = %key,
            sequence_number = %sequence_number,
            "Saving checkpoint to DynamoDB"
        );

        match self.retry_config.max_retries {
            None => {
                // Infinite retries - keep trying until success
                loop {
                    attempt += 1;
                    match self.try_save_checkpoint(&key, sequence_number).await {
                        Ok(_) => {
                            debug!(
                                shard_id = %shard_id,
                                sequence_number = %sequence_number,
                                "Successfully saved checkpoint"
                            );
                            return Ok(());
                        }
                        Err(e) => {
                            debug!(
                                shard_id = %shard_id,
                                error = %e,
                                attempt = attempt,
                                "Checkpoint failed, retrying indefinitely"
                            );
                            let delay = self.backoff.next_delay(attempt);
                            tokio::time::sleep(delay).await;
                        }
                    }
                }
            }
            Some(max_retries) => {
                // Limited retries - attempt up to max_retries times
                let result = retry
                    .retry(
                        || async { self.try_save_checkpoint(&key, sequence_number).await },
                        &mut tokio::sync::watch::channel(false).1,
                    )
                    .await;

                if let Err(e) = result {
                    warn!(
                        shard_id = %shard_id,
                        error = %e,
                        max_retries = max_retries,
                        "Failed to save checkpoint after max retries"
                    );
                    // Return error but allow processing to continue
                    return Err(e.into());
                }

                Ok(())
            }
        }
    }
}

impl DynamoDbCheckpointStore {
    // Helper method for actual save operation
    async fn try_save_checkpoint(&self, key: &str, sequence_number: &str) -> anyhow::Result<()> {
        self.client
            .put_item()
            .table_name(&self.table_name)
            .item("shard_id", AttributeValue::S(key.to_string()))
            .item(
                "sequence_number",
                AttributeValue::S(sequence_number.to_string()),
            )
            .send()
            .await
            .context("Failed to save checkpoint to DynamoDB")?;

        trace!(
            key = %key,
            sequence_number = %sequence_number,
            "Checkpoint saved to DynamoDB"
        );

        Ok(())
    }
}

#[derive(Debug)]
pub struct DynamoDbCheckpointStoreBuilder {
    client: Option<DynamoClient>,
    table_name: Option<String>,
    key_prefix: Option<String>,
    retry_config: RetryConfig,
    backoff: ExponentialBackoff,
}

impl Default for DynamoDbCheckpointStoreBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl DynamoDbCheckpointStoreBuilder {
    pub fn new() -> Self {
        Self {
            client: None,
            table_name: None,
            key_prefix: None,
            retry_config: RetryConfig::default(),
            backoff: ExponentialBackoff::builder()
                .initial_delay(Duration::from_millis(100))
                .max_delay(Duration::from_secs(30))
                .build(),
        }
    }

    pub fn with_client(mut self, client: DynamoClient) -> Self {
        self.client = Some(client);
        self
    }

    pub fn with_table_name(mut self, table_name: String) -> Self {
        self.table_name = Some(table_name);
        self
    }

    pub fn with_key_prefix(mut self, key_prefix: String) -> Self {
        self.key_prefix = Some(key_prefix);
        self
    }

    pub fn with_retry_config(mut self, config: RetryConfig) -> Self {
        self.retry_config = config;
        self
    }

    pub fn with_backoff(mut self, backoff: ExponentialBackoff) -> Self {
        self.backoff = backoff;
        self
    }

    pub fn build(self) -> anyhow::Result<DynamoDbCheckpointStore> {
        Ok(DynamoDbCheckpointStore {
            client: self
                .client
                .ok_or_else(|| anyhow::anyhow!("DynamoDB client is required"))?,
            table_name: self
                .table_name
                .ok_or_else(|| anyhow::anyhow!("Table name is required"))?,
            key_prefix: self.key_prefix.unwrap_or_default(),
            retry_config: self.retry_config,
            backoff: self.backoff,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use aws_credential_types::Credentials;
    use aws_sdk_dynamodb::config::Builder;
    use std::time::Duration;
    use tokio::time::timeout;

    // Helper function to create a test DynamoDB client
    async fn create_test_client() -> DynamoClient {
        let creds = Credentials::new("test", "test", None, None, "test");
        let config = Builder::new()
            .credentials_provider(creds)
            .region(aws_config::Region::new("us-east-1"))
            .build();
        DynamoClient::from_conf(config)
    }

    // Helper to create a store with custom retry config
    async fn create_test_store(max_retries: Option<u32>) -> DynamoDbCheckpointStore {
        let retry_config = RetryConfig {
            max_retries,
            initial_backoff: Duration::from_millis(10),
            max_backoff: Duration::from_millis(100),
            jitter_factor: 0.1,
        };

        DynamoDbCheckpointStore::builder()
            .with_client(create_test_client().await)
            .with_table_name("test-table".to_string())
            .with_key_prefix("test-".to_string())
            .with_retry_config(retry_config)
            .build()
            .expect("Failed to create test store")
    }

    #[tokio::test]
    async fn test_builder_configuration() -> anyhow::Result<()> {
        // Test required fields
        let result = DynamoDbCheckpointStore::builder().build();
        assert!(result.is_err(), "Build should fail without required fields");

        // Test successful build
        let store = DynamoDbCheckpointStore::builder()
            .with_client(create_test_client().await)
            .with_table_name("test-table".to_string())
            .with_key_prefix("test-".to_string())
            .build()?;

        assert_eq!(store.table_name, "test-table");
        assert_eq!(store.key_prefix, "test-");

        Ok(())
    }

    #[tokio::test]
    async fn test_retry_configuration() -> anyhow::Result<()> {
        let custom_retry = RetryConfig {
            max_retries: Some(5),
            initial_backoff: Duration::from_millis(50),
            max_backoff: Duration::from_secs(1),
            jitter_factor: 0.2,
        };

        let store = DynamoDbCheckpointStore::builder()
            .with_client(create_test_client().await)
            .with_table_name("test-table".to_string())
            .with_retry_config(custom_retry.clone())
            .build()?;

        assert_eq!(
            store.retry_config.max_retries, custom_retry.max_retries,
            "Retry configuration should be preserved"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_save_checkpoint_retries() -> anyhow::Result<()> {
        let store = create_test_store(Some(3)).await;
        let shard_id = "test-shard";
        let sequence = "test-sequence";

        // Mock client behavior could be implemented here
        // For now, we'll just test the basic operation
        let result = store.save_checkpoint(shard_id, sequence).await;

        // In a real test environment, we'd verify retry behavior
        assert!(
            result.is_err(),
            "Should fail without proper DynamoDB access"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_get_checkpoint_retries() -> anyhow::Result<()> {
        let store = create_test_store(Some(3)).await;
        let shard_id = "test-shard";

        // Test basic operation
        let result = store.get_checkpoint(shard_id).await;

        // In a real test environment, we'd verify retry behavior
        assert!(
            result.is_err(),
            "Should fail without proper DynamoDB access"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_retry_timeout() -> anyhow::Result<()> {
        let store = create_test_store(Some(3)).await;
        let shard_id = "test-shard";
        let sequence = "test-sequence";

        // Set a short timeout for the test
        let timeout_duration = Duration::from_millis(500);

        // Attempt operation with timeout
        let result = timeout(timeout_duration, store.save_checkpoint(shard_id, sequence)).await;

        // Should timeout or fail, but not hang
        assert!(result.is_err() || result.unwrap().is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_prefixed_key_generation() -> anyhow::Result<()> {
        let store = create_test_store(None).await;

        assert_eq!(
            store.prefixed_key("shard-1"),
            "test-shard-1",
            "Prefixed key should combine prefix and shard ID"
        );

        Ok(())
    }
}