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
//! State management for stream processing
//!
//! This module provides state backend abstractions and implementations for maintaining
//! window state across the distributed stream processing system. It supports:
//!
//! - **Multiple Backends**: In-memory (with TTL), persistent (Sled), and distributed (Redis) storage
//! - **Checkpointing**: Periodic state snapshots for fault tolerance
//! - **Distributed Locking**: Coordinate state access across multiple processor instances
//! - **Async Operations**: All state operations are async-first
//! - **Type Safety**: Generic state serialization/deserialization
//!
//! ## Architecture
//!
//! The state module is organized around four core concepts:
//!
//! 1. **StateBackend Trait**: Defines the interface for all state backends
//! 2. **Backend Implementations**: Memory, Sled-based persistent, and Redis distributed storage
//! 3. **Checkpoint Coordination**: Manages periodic snapshots and recovery
//! 4. **Distributed Locking**: Redis and PostgreSQL-based distributed locks
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use processor::state::{MemoryStateBackend, StateBackend};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Create an in-memory state backend with TTL
//! let backend = MemoryStateBackend::new(Some(Duration::from_secs(3600)));
//!
//! // Store state
//! backend.put(b"window:123", b"state_data").await?;
//!
//! // Retrieve state
//! if let Some(data) = backend.get(b"window:123").await? {
//! println!("Retrieved: {:?}", data);
//! }
//!
//! // List all windows
//! let keys = backend.list_keys(b"window:").await?;
//! println!("Found {} windows", keys.len());
//!
//! Ok(())
//! }
//! ```
//!
//! ## Checkpointing
//!
//! The checkpoint coordinator automatically creates state snapshots:
//!
//! ```rust,no_run
//! use processor::state::{CheckpointCoordinator, MemoryStateBackend};
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let backend = Arc::new(MemoryStateBackend::new(None));
//!
//! // Create coordinator with 5-minute intervals
//! let coordinator = CheckpointCoordinator::new(
//! backend.clone(),
//! Duration::from_secs(300),
//! "/tmp/checkpoints".into(),
//! );
//!
//! // Start periodic checkpointing
//! coordinator.start().await?;
//!
//! // Work with state...
//!
//! // Checkpoint is created automatically
//! coordinator.shutdown().await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Distributed Locking
//!
//! Coordinate critical operations across multiple processor instances:
//!
//! ```rust,no_run
//! use processor::state::distributed_lock::{RedisDistributedLock, DistributedLock};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let lock = RedisDistributedLock::new("redis://localhost:6379").await?;
//!
//! // Acquire lock for checkpoint coordination
//! let guard = lock.acquire("checkpoint:state1", Duration::from_secs(30)).await?;
//!
//! // Perform checkpoint - only one instance will execute this
//! println!("Creating checkpoint...");
//!
//! // Lock automatically released when guard is dropped
//! drop(guard);
//!
//! Ok(())
//! }
//! ```
// Re-export main types
pub use StateBackend;
pub use ;
pub use ;
pub use ;
pub use ;
pub use MemoryStateBackend;
pub use ;
pub use ;
pub use PostgresDistributedLock;
pub use ;
pub use ;
pub use ;
pub use ;
/// Type alias for state operation results
pub type StateResult<T> = ;