llm-optimizer-processor 0.1.1

Data processing and transformation pipeline
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
//! Redis-based distributed state backend implementation
//!
//! This module provides a production-ready Redis state backend with comprehensive
//! features for distributed state management including connection pooling, cluster
//! support, TTL management, batch operations, and metrics tracking.
//!
//! ## Features
//!
//! - **Connection Pooling**: Automatic connection management with configurable pool size
//! - **Cluster Support**: Works with both standalone and clustered Redis deployments
//! - **TTL Management**: Per-key expiration with automatic cleanup
//! - **Batch Operations**: Efficient pipeline support for bulk operations
//! - **Retry Logic**: Configurable retry strategy for transient failures
//! - **Metrics**: Comprehensive operation tracking and statistics
//! - **Atomic Operations**: MULTI/EXEC transactions for consistency
//! - **Health Checks**: Connection monitoring and auto-reconnection
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use processor::state::{RedisStateBackend, RedisConfig, StateBackend};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     // Configure Redis backend
//!     let config = RedisConfig::builder()
//!         .url("redis://localhost:6379")
//!         .key_prefix("myapp:")
//!         .default_ttl(Duration::from_secs(3600))
//!         .pool_size(10, 50)
//!         .build()?;
//!
//!     // Create backend
//!     let backend = RedisStateBackend::new(config).await?;
//!
//!     // Store state with TTL
//!     backend.put_with_ttl(b"session:123", b"user_data", Duration::from_secs(1800)).await?;
//!
//!     // Retrieve state
//!     if let Some(data) = backend.get(b"session:123").await? {
//!         println!("Session data: {:?}", data);
//!     }
//!
//!     // Batch operations
//!     let keys = vec![b"key1", b"key2", b"key3"];
//!     let values = backend.batch_get(&keys).await?;
//!
//!     // Get statistics
//!     let stats = backend.stats().await;
//!     println!("Hit rate: {:.2}%", stats.hit_rate() * 100.0);
//!
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use redis::{
    aio::ConnectionManager, AsyncCommands, Client, RedisResult, Script,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, error, trace, warn};

use super::backend::StateBackend;
use crate::error::{StateError, StateResult};

/// Redis connection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisConfig {
    /// Redis connection URL(s) - supports cluster mode with multiple URLs
    pub urls: Vec<String>,
    /// Key prefix for namespace isolation
    pub key_prefix: String,
    /// Default TTL for keys (None = no expiration)
    pub default_ttl: Option<Duration>,
    /// Minimum connections in pool
    pub min_connections: u32,
    /// Maximum connections in pool
    pub max_connections: u32,
    /// Connection timeout
    pub connect_timeout: Duration,
    /// Read timeout
    pub read_timeout: Duration,
    /// Write timeout
    pub write_timeout: Duration,
    /// Maximum retry attempts for failed operations
    pub max_retries: u32,
    /// Base delay for exponential backoff
    pub retry_base_delay: Duration,
    /// Maximum delay between retries
    pub retry_max_delay: Duration,
    /// Enable cluster mode
    pub cluster_mode: bool,
    /// Enable TLS/SSL
    pub tls_enabled: bool,
    /// Pipeline batch size for bulk operations
    pub pipeline_batch_size: usize,
}

impl Default for RedisConfig {
    fn default() -> Self {
        Self {
            urls: vec!["redis://localhost:6379".to_string()],
            key_prefix: "state:".to_string(),
            default_ttl: None,
            min_connections: 5,
            max_connections: 50,
            connect_timeout: Duration::from_secs(5),
            read_timeout: Duration::from_secs(3),
            write_timeout: Duration::from_secs(3),
            max_retries: 3,
            retry_base_delay: Duration::from_millis(100),
            retry_max_delay: Duration::from_secs(5),
            cluster_mode: false,
            tls_enabled: false,
            pipeline_batch_size: 100,
        }
    }
}

impl RedisConfig {
    /// Create a new configuration builder
    pub fn builder() -> RedisConfigBuilder {
        RedisConfigBuilder::default()
    }

    /// Get primary Redis URL
    pub fn primary_url(&self) -> &str {
        self.urls.first().map(|s| s.as_str()).unwrap_or("redis://localhost:6379")
    }
}

/// Builder for RedisConfig
#[derive(Debug, Default)]
pub struct RedisConfigBuilder {
    config: RedisConfig,
}

impl RedisConfigBuilder {
    /// Set Redis URL (single instance)
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.config.urls = vec![url.into()];
        self
    }

    /// Set Redis URLs (cluster mode)
    pub fn urls(mut self, urls: Vec<String>) -> Self {
        self.config.cluster_mode = urls.len() > 1;
        self.config.urls = urls;
        self
    }

    /// Set key prefix for namespace isolation
    pub fn key_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.config.key_prefix = prefix.into();
        self
    }

    /// Set default TTL for all keys
    pub fn default_ttl(mut self, ttl: Duration) -> Self {
        self.config.default_ttl = Some(ttl);
        self
    }

    /// Set connection pool size (min, max)
    pub fn pool_size(mut self, min: u32, max: u32) -> Self {
        self.config.min_connections = min;
        self.config.max_connections = max;
        self
    }

    /// Set timeouts (connect, read, write)
    pub fn timeouts(mut self, connect: Duration, read: Duration, write: Duration) -> Self {
        self.config.connect_timeout = connect;
        self.config.read_timeout = read;
        self.config.write_timeout = write;
        self
    }

    /// Set retry configuration (max attempts, base delay, max delay)
    pub fn retries(mut self, max: u32, base_delay: Duration, max_delay: Duration) -> Self {
        self.config.max_retries = max;
        self.config.retry_base_delay = base_delay;
        self.config.retry_max_delay = max_delay;
        self
    }

    /// Enable cluster mode
    pub fn cluster_mode(mut self, enabled: bool) -> Self {
        self.config.cluster_mode = enabled;
        self
    }

    /// Enable TLS/SSL
    pub fn tls(mut self, enabled: bool) -> Self {
        self.config.tls_enabled = enabled;
        self
    }

    /// Set pipeline batch size
    pub fn pipeline_batch_size(mut self, size: usize) -> Self {
        self.config.pipeline_batch_size = size;
        self
    }

    /// Build the configuration
    pub fn build(self) -> StateResult<RedisConfig> {
        if self.config.urls.is_empty() {
            return Err(StateError::StorageError {
                backend_type: "redis".to_string(),
                details: "No Redis URLs configured".to_string(),
            });
        }

        if self.config.max_connections < self.config.min_connections {
            return Err(StateError::StorageError {
                backend_type: "redis".to_string(),
                details: "max_connections must be >= min_connections".to_string(),
            });
        }

        Ok(self.config)
    }
}

/// Statistics for Redis backend operations
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RedisStats {
    /// Total GET operations
    pub get_count: u64,
    /// Total PUT operations
    pub put_count: u64,
    /// Total DELETE operations
    pub delete_count: u64,
    /// Total cache hits
    pub hit_count: u64,
    /// Total cache misses
    pub miss_count: u64,
    /// Total pipeline operations
    pub pipeline_count: u64,
    /// Total retry attempts
    pub retry_count: u64,
    /// Total failed operations
    pub error_count: u64,
    /// Total bytes read
    pub bytes_read: u64,
    /// Total bytes written
    pub bytes_written: u64,
    /// Average operation latency (microseconds)
    pub avg_latency_us: u64,
    /// Number of active connections
    pub active_connections: u32,
}

impl RedisStats {
    /// Calculate hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let total = self.hit_count + self.miss_count;
        if total == 0 {
            0.0
        } else {
            self.hit_count as f64 / total as f64
        }
    }

    /// Calculate error rate (0.0 to 1.0)
    pub fn error_rate(&self) -> f64 {
        let total = self.get_count + self.put_count + self.delete_count;
        if total == 0 {
            0.0
        } else {
            self.error_count as f64 / total as f64
        }
    }

    /// Get total operations count
    pub fn total_operations(&self) -> u64 {
        self.get_count + self.put_count + self.delete_count
    }
}

/// Redis state backend with connection pooling and advanced features
pub struct RedisStateBackend {
    /// Redis connection manager
    connection: Arc<ConnectionManager>,
    /// Configuration
    config: RedisConfig,
    /// Statistics tracking
    stats: Arc<RwLock<RedisStats>>,
    /// LUA script for atomic operations
    scripts: RedisScripts,
}

/// Pre-compiled LUA scripts for atomic operations
struct RedisScripts {
    /// Atomic get-and-delete operation
    get_delete: Script,
    /// Atomic increment with TTL
    incr_with_ttl: Script,
    /// Conditional set (only if not exists)
    set_nx_with_ttl: Script,
}

impl RedisScripts {
    fn new() -> Self {
        Self {
            // GET and DELETE atomically
            get_delete: Script::new(
                r"
                local value = redis.call('GET', KEYS[1])
                if value then
                    redis.call('DEL', KEYS[1])
                end
                return value
                "
            ),
            // INCREMENT with TTL
            incr_with_ttl: Script::new(
                r"
                local value = redis.call('INCR', KEYS[1])
                redis.call('EXPIRE', KEYS[1], ARGV[1])
                return value
                "
            ),
            // SET NX (only if not exists) with TTL
            set_nx_with_ttl: Script::new(
                r"
                local exists = redis.call('EXISTS', KEYS[1])
                if exists == 0 then
                    redis.call('SET', KEYS[1], ARGV[1])
                    redis.call('EXPIRE', KEYS[1], ARGV[2])
                    return 1
                end
                return 0
                "
            ),
        }
    }
}

impl RedisStateBackend {
    /// Create a new Redis state backend
    ///
    /// # Arguments
    ///
    /// * `config` - Redis configuration
    ///
    /// # Returns
    ///
    /// A new `RedisStateBackend` instance
    ///
    /// # Errors
    ///
    /// Returns error if connection to Redis fails
    pub async fn new(config: RedisConfig) -> StateResult<Self> {
        let client = Self::create_client(&config)?;
        let connection = Self::create_connection_manager(client, &config).await?;

        Ok(Self {
            connection: Arc::new(connection),
            config,
            stats: Arc::new(RwLock::new(RedisStats::default())),
            scripts: RedisScripts::new(),
        })
    }

    /// Create Redis client from configuration
    fn create_client(config: &RedisConfig) -> StateResult<Client> {
        let url = config.primary_url();
        Client::open(url).map_err(|e| StateError::StorageError {
            backend_type: "redis".to_string(),
            details: format!("Failed to create client: {}", e),
        })
    }

    /// Create connection manager with retry logic
    async fn create_connection_manager(
        client: Client,
        config: &RedisConfig,
    ) -> StateResult<ConnectionManager> {
        let mut retries = 0;
        let mut delay = config.retry_base_delay;

        loop {
            match ConnectionManager::new(client.clone()).await {
                Ok(conn) => {
                    debug!("Redis connection established");
                    return Ok(conn);
                }
                Err(e) => {
                    retries += 1;
                    if retries >= config.max_retries {
                        error!("Failed to connect to Redis after {} retries: {}", retries, e);
                        return Err(StateError::StorageError {
                            backend_type: "redis".to_string(),
                            details: format!("Connection failed after {} retries: {}", retries, e),
                        });
                    }

                    warn!("Redis connection attempt {} failed: {}, retrying in {:?}",
                          retries, e, delay);
                    tokio::time::sleep(delay).await;
                    delay = std::cmp::min(delay * 2, config.retry_max_delay);
                }
            }
        }
    }

    /// Build full key with prefix
    fn build_key(&self, key: &[u8]) -> Vec<u8> {
        let mut full_key = self.config.key_prefix.as_bytes().to_vec();
        full_key.extend_from_slice(key);
        full_key
    }

    /// Strip prefix from key
    fn strip_prefix(&self, key: &[u8]) -> Vec<u8> {
        let prefix = self.config.key_prefix.as_bytes();
        if key.starts_with(prefix) {
            key[prefix.len()..].to_vec()
        } else {
            key.to_vec()
        }
    }

    /// Execute operation with retry logic
    async fn with_retry<F, Fut, T>(&self, mut operation: F) -> StateResult<T>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = RedisResult<T>>,
    {
        let mut retries = 0;
        let mut delay = self.config.retry_base_delay;

        loop {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    retries += 1;

                    // Update error stats
                    let mut stats = self.stats.write().await;
                    stats.error_count += 1;
                    stats.retry_count += 1;
                    drop(stats);

                    if retries >= self.config.max_retries {
                        error!("Operation failed after {} retries: {}", retries, e);
                        return Err(StateError::StorageError {
                            backend_type: "redis".to_string(),
                            details: format!("Operation failed after {} retries: {}", retries, e),
                        });
                    }

                    warn!("Operation attempt {} failed: {}, retrying in {:?}",
                          retries, e, delay);
                    tokio::time::sleep(delay).await;
                    delay = std::cmp::min(delay * 2, self.config.retry_max_delay);
                }
            }
        }
    }

    /// Get current statistics
    pub async fn stats(&self) -> RedisStats {
        self.stats.read().await.clone()
    }

    /// Check connection health
    pub async fn health_check(&self) -> StateResult<bool> {
        let mut conn = self.connection.as_ref().clone();
        let result: RedisResult<String> = redis::cmd("PING").query_async(&mut conn).await;

        match result {
            Ok(response) => Ok(response == "PONG"),
            Err(e) => {
                error!("Health check failed: {}", e);
                Err(StateError::StorageError {
                    backend_type: "redis".to_string(),
                    details: format!("Health check failed: {}", e),
                })
            }
        }
    }

    /// Get Redis server info
    pub async fn server_info(&self) -> StateResult<String> {
        let mut conn = self.connection.as_ref().clone();
        redis::cmd("INFO")
            .query_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "redis".to_string(),
                details: format!("Failed to get server info: {}", e),
            })
    }

    /// Put with custom TTL
    pub async fn put_with_ttl(&self, key: &[u8], value: &[u8], ttl: Duration) -> StateResult<()> {
        let full_key = self.build_key(key);
        let ttl_secs = ttl.as_secs();

        let conn = self.connection.as_ref().clone();
        let full_key_clone = full_key.clone();
        let value_clone = value.to_vec();

        let result = self.with_retry(move || {
            let mut conn = conn.clone();
            let full_key = full_key_clone.clone();
            let value = value_clone.clone();
            async move {
                conn.set_ex(&full_key, &value, ttl_secs).await
            }
        }).await;

        if result.is_ok() {
            let mut stats = self.stats.write().await;
            stats.put_count += 1;
            stats.bytes_written += value.len() as u64;
        }

        result
    }

    /// Batch get multiple keys
    pub async fn batch_get(&self, keys: &[&[u8]]) -> StateResult<Vec<Option<Vec<u8>>>> {
        if keys.is_empty() {
            return Ok(Vec::new());
        }

        let full_keys: Vec<Vec<u8>> = keys.iter().map(|k| self.build_key(k)).collect();
        let conn = self.connection.as_ref().clone();

        let full_keys_clone = full_keys.clone();
        let result: Vec<Option<Vec<u8>>> = self.with_retry(move || {
            let mut conn = conn.clone();
            let full_keys = full_keys_clone.clone();
            async move {
                redis::cmd("MGET")
                    .arg(&full_keys)
                    .query_async(&mut conn)
                    .await
            }
        }).await?;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.get_count += keys.len() as u64;
        for value in &result {
            if let Some(v) = value {
                stats.hit_count += 1;
                stats.bytes_read += v.len() as u64;
            } else {
                stats.miss_count += 1;
            }
        }

        Ok(result)
    }

    /// Batch put multiple key-value pairs
    pub async fn batch_put(&self, pairs: &[(&[u8], &[u8])]) -> StateResult<()> {
        if pairs.is_empty() {
            return Ok(());
        }

        // Process in chunks based on pipeline batch size
        for chunk in pairs.chunks(self.config.pipeline_batch_size) {
            let mut conn = self.connection.as_ref().clone();

            // Build commands for this chunk
            let commands: Vec<(Vec<u8>, Vec<u8>, Option<u64>)> = chunk
                .iter()
                .map(|(key, value)| {
                    let full_key = self.build_key(key);
                    let ttl = self.config.default_ttl.map(|d| d.as_secs());
                    (full_key, value.to_vec(), ttl)
                })
                .collect();

            self.with_retry(move || {
                let mut conn = conn.clone();
                let commands = commands.clone();
                async move {
                    let mut pipe = redis::pipe();
                    for (full_key, value, ttl) in &commands {
                        if let Some(ttl_secs) = ttl {
                            pipe.set_ex(full_key, value, *ttl_secs);
                        } else {
                            pipe.set(full_key, value);
                        }
                    }
                    pipe.query_async(&mut conn).await
                }
            }).await?;

            // Update stats
            let mut stats = self.stats.write().await;
            stats.put_count += chunk.len() as u64;
            stats.pipeline_count += 1;
            stats.bytes_written += chunk.iter().map(|(_, v)| v.len() as u64).sum::<u64>();
        }

        Ok(())
    }

    /// Batch delete multiple keys
    pub async fn batch_delete(&self, keys: &[&[u8]]) -> StateResult<usize> {
        if keys.is_empty() {
            return Ok(0);
        }

        let full_keys: Vec<Vec<u8>> = keys.iter().map(|k| self.build_key(k)).collect();
        let conn = self.connection.as_ref().clone();
        let full_keys_clone = full_keys.clone();

        let deleted: usize = self.with_retry(move || {
            let mut conn = conn.clone();
            let full_keys = full_keys_clone.clone();
            async move {
                redis::cmd("DEL")
                    .arg(&full_keys)
                    .query_async(&mut conn)
                    .await
            }
        }).await?;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.delete_count += keys.len() as u64;

        Ok(deleted)
    }

    /// Scan keys with cursor (for large datasets)
    async fn scan_keys_internal(&self, pattern: Vec<u8>, count: usize) -> StateResult<Vec<Vec<u8>>> {
        let mut conn = self.connection.as_ref().clone();
        let mut cursor = 0u64;
        let mut all_keys = Vec::new();

        loop {
            let (new_cursor, keys): (u64, Vec<Vec<u8>>) = redis::cmd("SCAN")
                .arg(cursor)
                .arg("MATCH")
                .arg(&pattern)
                .arg("COUNT")
                .arg(count)
                .query_async(&mut conn)
                .await
                .map_err(|e| StateError::StorageError {
                    backend_type: "redis".to_string(),
                    details: format!("SCAN failed: {}", e),
                })?;

            all_keys.extend(keys);
            cursor = new_cursor;

            if cursor == 0 {
                break;
            }
        }

        Ok(all_keys)
    }

    /// Atomic get and delete operation
    pub async fn get_and_delete(&self, key: &[u8]) -> StateResult<Option<Vec<u8>>> {
        let full_key = self.build_key(key);
        let mut conn = self.connection.as_ref().clone();

        let result: Option<Vec<u8>> = self.scripts
            .get_delete
            .key(&full_key)
            .invoke_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "redis".to_string(),
                details: format!("GET_DELETE script failed: {}", e),
            })?;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.get_count += 1;
        stats.delete_count += 1;

        if result.is_some() {
            stats.hit_count += 1;
        } else {
            stats.miss_count += 1;
        }

        Ok(result)
    }

    /// Set key only if it doesn't exist (with TTL)
    pub async fn set_if_not_exists(&self, key: &[u8], value: &[u8], ttl: Duration) -> StateResult<bool> {
        let full_key = self.build_key(key);
        let ttl_secs = ttl.as_secs();
        let mut conn = self.connection.as_ref().clone();

        let result: i32 = self.scripts
            .set_nx_with_ttl
            .key(&full_key)
            .arg(value)
            .arg(ttl_secs)
            .invoke_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "redis".to_string(),
                details: format!("SET_NX_WITH_TTL script failed: {}", e),
            })?;

        if result == 1 {
            let mut stats = self.stats.write().await;
            stats.put_count += 1;
            stats.bytes_written += value.len() as u64;
        }

        Ok(result == 1)
    }

    /// Get remaining TTL for a key
    pub async fn ttl(&self, key: &[u8]) -> StateResult<Option<Duration>> {
        let full_key = self.build_key(key);
        let mut conn = self.connection.as_ref().clone();

        let ttl_secs: i64 = redis::cmd("TTL")
            .arg(&full_key)
            .query_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "redis".to_string(),
                details: format!("TTL query failed: {}", e),
            })?;

        match ttl_secs {
            -2 => Ok(None), // Key doesn't exist
            -1 => Ok(Some(Duration::MAX)), // No expiration
            secs if secs >= 0 => Ok(Some(Duration::from_secs(secs as u64))),
            _ => Ok(None),
        }
    }

    /// Create a checkpoint (snapshot) of all state
    pub async fn checkpoint(&self) -> StateResult<Vec<(Vec<u8>, Vec<u8>)>> {
        let pattern = format!("{}*", self.config.key_prefix).into_bytes();
        let keys = self.scan_keys_internal(pattern, 1000).await?;

        if keys.is_empty() {
            return Ok(Vec::new());
        }

        // Get all values
        let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
        let values = self.batch_get(&key_refs).await?;

        // Combine keys and values, stripping prefix
        let checkpoint: Vec<(Vec<u8>, Vec<u8>)> = keys
            .into_iter()
            .zip(values)
            .filter_map(|(k, v)| v.map(|val| (self.strip_prefix(&k), val)))
            .collect();

        Ok(checkpoint)
    }

    /// Restore state from a checkpoint
    pub async fn restore(&self, checkpoint: Vec<(Vec<u8>, Vec<u8>)>) -> StateResult<()> {
        if checkpoint.is_empty() {
            return Ok(());
        }

        let pairs: Vec<(&[u8], &[u8])> = checkpoint
            .iter()
            .map(|(k, v)| (k.as_slice(), v.as_slice()))
            .collect();

        self.batch_put(&pairs).await
    }

    /// Get memory usage statistics from Redis
    pub async fn memory_usage(&self) -> StateResult<u64> {
        let mut conn = self.connection.as_ref().clone();

        let info: String = redis::cmd("INFO")
            .arg("memory")
            .query_async(&mut conn)
            .await
            .map_err(|e| StateError::StorageError {
                backend_type: "redis".to_string(),
                details: format!("Failed to get memory info: {}", e),
            })?;

        // Parse used_memory from INFO response
        for line in info.lines() {
            if line.starts_with("used_memory:") {
                if let Some(bytes_str) = line.split(':').nth(1) {
                    if let Ok(bytes) = bytes_str.trim().parse::<u64>() {
                        return Ok(bytes);
                    }
                }
            }
        }

        Ok(0)
    }
}

#[async_trait]
impl StateBackend for RedisStateBackend {
    async fn get(&self, key: &[u8]) -> StateResult<Option<Vec<u8>>> {
        let start = std::time::Instant::now();
        let full_key = self.build_key(key);
        trace!("Getting key: {:?}", full_key);

        let conn = self.connection.as_ref().clone();
        let full_key_clone = full_key.clone();

        let result: Option<Vec<u8>> = self.with_retry(move || {
            let mut conn = conn.clone();
            let full_key = full_key_clone.clone();
            async move {
                conn.get(&full_key).await
            }
        }).await?;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.get_count += 1;

        if let Some(ref value) = result {
            stats.hit_count += 1;
            stats.bytes_read += value.len() as u64;
        } else {
            stats.miss_count += 1;
        }

        // Update latency (simple moving average)
        let latency_us = start.elapsed().as_micros() as u64;
        stats.avg_latency_us = if stats.total_operations() == 1 {
            latency_us
        } else {
            (stats.avg_latency_us * 9 + latency_us) / 10
        };

        Ok(result)
    }

    async fn put(&self, key: &[u8], value: &[u8]) -> StateResult<()> {
        let full_key = self.build_key(key);
        trace!("Putting key: {:?}, value size: {} bytes", full_key, value.len());

        let conn = self.connection.as_ref().clone();
        let full_key_clone = full_key.clone();
        let value_clone = value.to_vec();

        if let Some(ttl) = self.config.default_ttl {
            let ttl_secs = ttl.as_secs();
            self.with_retry(move || {
                let mut conn = conn.clone();
                let full_key = full_key_clone.clone();
                let value = value_clone.clone();
                async move {
                    conn.set_ex(&full_key, &value, ttl_secs).await
                }
            }).await?;
        } else {
            self.with_retry(move || {
                let mut conn = conn.clone();
                let full_key = full_key_clone.clone();
                let value = value_clone.clone();
                async move {
                    conn.set(&full_key, &value).await
                }
            }).await?;
        }

        // Update stats
        let mut stats = self.stats.write().await;
        stats.put_count += 1;
        stats.bytes_written += value.len() as u64;

        Ok(())
    }

    async fn delete(&self, key: &[u8]) -> StateResult<()> {
        let full_key = self.build_key(key);
        trace!("Deleting key: {:?}", full_key);

        let conn = self.connection.as_ref().clone();
        let full_key_clone = full_key.clone();

        self.with_retry(move || {
            let mut conn = conn.clone();
            let full_key = full_key_clone.clone();
            async move {
                conn.del(&full_key).await
            }
        }).await?;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.delete_count += 1;

        Ok(())
    }

    async fn list_keys(&self, prefix: &[u8]) -> StateResult<Vec<Vec<u8>>> {
        trace!("Listing keys with prefix: {:?}", prefix);

        let pattern = if prefix.is_empty() {
            format!("{}*", self.config.key_prefix).into_bytes()
        } else {
            let mut p = self.config.key_prefix.as_bytes().to_vec();
            p.extend_from_slice(prefix);
            p.push(b'*');
            p
        };

        let keys = self.scan_keys_internal(pattern, 1000).await?;

        // Strip prefix from all keys
        let stripped_keys = keys
            .into_iter()
            .map(|k| self.strip_prefix(&k))
            .collect();

        Ok(stripped_keys)
    }

    async fn clear(&self) -> StateResult<()> {
        debug!("Clearing all state");

        let pattern = format!("{}*", self.config.key_prefix).into_bytes();
        let keys = self.scan_keys_internal(pattern, 1000).await?;

        if keys.is_empty() {
            return Ok(());
        }

        // Delete in batches
        let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
        for chunk in key_refs.chunks(self.config.pipeline_batch_size) {
            let chunk_vec: Vec<Vec<u8>> = chunk.iter().map(|k| k.to_vec()).collect();

            // Perform deletion directly without retry wrapper to avoid closure lifetime issues
            let mut retries = 0;
            let mut delay = self.config.retry_base_delay;

            loop {
                let mut conn = self.connection.as_ref().clone();

                let result: RedisResult<()> = redis::cmd("DEL")
                    .arg(&chunk_vec)
                    .query_async(&mut conn)
                    .await;

                match result {
                    Ok(_) => break,
                    Err(e) => {
                        retries += 1;

                        if retries >= self.config.max_retries {
                            return Err(StateError::StorageError {
                                backend_type: "redis".to_string(),
                                details: format!("Delete batch failed after {} retries: {}", retries, e),
                            });
                        }

                        warn!("Delete batch attempt {} failed: {}, retrying in {:?}", retries, e, delay);
                        tokio::time::sleep(delay).await;
                        delay = std::cmp::min(delay * 2, self.config.retry_max_delay);
                    }
                }
            }
        }

        Ok(())
    }

    async fn count(&self) -> StateResult<usize> {
        let pattern = format!("{}*", self.config.key_prefix).into_bytes();
        let keys = self.scan_keys_internal(pattern, 1000).await?;
        Ok(keys.len())
    }

    async fn contains(&self, key: &[u8]) -> StateResult<bool> {
        let full_key = self.build_key(key);
        let conn = self.connection.as_ref().clone();
        let full_key_clone = full_key.clone();

        let exists: bool = self.with_retry(move || {
            let mut conn = conn.clone();
            let full_key = full_key_clone.clone();
            async move {
                conn.exists(&full_key).await
            }
        }).await?;

        Ok(exists)
    }
}

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

    // Helper to create test backend (requires Redis running)
    async fn create_test_backend() -> Option<RedisStateBackend> {
        let config = RedisConfig::builder()
            .url("redis://localhost:6379")
            .key_prefix("test:")
            .build()
            .ok()?;

        match RedisStateBackend::new(config).await {
            Ok(backend) => {
                // Clear any existing test data
                let _ = backend.clear().await;
                Some(backend)
            }
            Err(_) => None,
        }
    }

    #[tokio::test]
    async fn test_redis_backend_basic() {
        if let Some(backend) = create_test_backend().await {
            test_backend_basic_ops(backend).await;
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_backend_list_keys() {
        if let Some(backend) = create_test_backend().await {
            test_backend_list_keys(backend).await;
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_backend_clear() {
        if let Some(backend) = create_test_backend().await {
            test_backend_clear(backend).await;
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_backend_contains() {
        if let Some(backend) = create_test_backend().await {
            test_backend_contains(backend).await;
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_ttl() {
        if let Some(backend) = create_test_backend().await {
            backend
                .put_with_ttl(b"ttl_key", b"value", Duration::from_secs(2))
                .await
                .unwrap();

            assert!(backend.get(b"ttl_key").await.unwrap().is_some());

            // Check TTL
            let ttl = backend.ttl(b"ttl_key").await.unwrap();
            assert!(ttl.is_some());
            assert!(ttl.unwrap().as_secs() <= 2);

            // Wait for expiration
            tokio::time::sleep(Duration::from_secs(3)).await;
            assert!(backend.get(b"ttl_key").await.unwrap().is_none());
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_batch_operations() {
        if let Some(backend) = create_test_backend().await {
            // Batch put
            let pairs = vec![
                (&b"batch1"[..], &b"value1"[..]),
                (&b"batch2"[..], &b"value2"[..]),
                (&b"batch3"[..], &b"value3"[..]),
            ];
            backend.batch_put(&pairs).await.unwrap();

            // Batch get
            let keys = vec![b"batch1", b"batch2", b"batch3"];
            let values = backend.batch_get(&keys).await.unwrap();
            assert_eq!(values.len(), 3);
            assert_eq!(values[0], Some(b"value1".to_vec()));
            assert_eq!(values[1], Some(b"value2".to_vec()));
            assert_eq!(values[2], Some(b"value3".to_vec()));

            // Batch delete
            let deleted = backend.batch_delete(&keys).await.unwrap();
            assert_eq!(deleted, 3);
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_atomic_operations() {
        if let Some(backend) = create_test_backend().await {
            backend.put(b"atomic_key", b"value").await.unwrap();

            // Get and delete atomically
            let value = backend.get_and_delete(b"atomic_key").await.unwrap();
            assert_eq!(value, Some(b"value".to_vec()));

            // Key should be gone
            assert!(backend.get(b"atomic_key").await.unwrap().is_none());

            // Set if not exists
            let set1 = backend
                .set_if_not_exists(b"nx_key", b"value1", Duration::from_secs(60))
                .await
                .unwrap();
            assert!(set1);

            let set2 = backend
                .set_if_not_exists(b"nx_key", b"value2", Duration::from_secs(60))
                .await
                .unwrap();
            assert!(!set2);

            // First value should still be there
            assert_eq!(
                backend.get(b"nx_key").await.unwrap(),
                Some(b"value1".to_vec())
            );
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_checkpoint_restore() {
        if let Some(backend) = create_test_backend().await {
            backend.put(b"key1", b"value1").await.unwrap();
            backend.put(b"key2", b"value2").await.unwrap();

            // Create checkpoint
            let checkpoint = backend.checkpoint().await.unwrap();
            assert_eq!(checkpoint.len(), 2);

            // Clear and restore
            backend.clear().await.unwrap();
            assert_eq!(backend.count().await.unwrap(), 0);

            backend.restore(checkpoint).await.unwrap();
            assert_eq!(backend.count().await.unwrap(), 2);

            assert_eq!(
                backend.get(b"key1").await.unwrap(),
                Some(b"value1".to_vec())
            );
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_stats() {
        if let Some(backend) = create_test_backend().await {
            backend.put(b"key1", b"value1").await.unwrap();
            backend.put(b"key2", b"value2").await.unwrap();

            backend.get(b"key1").await.unwrap(); // Hit
            backend.get(b"key1").await.unwrap(); // Hit
            backend.get(b"nonexistent").await.unwrap(); // Miss

            backend.delete(b"key1").await.unwrap();

            let stats = backend.stats().await;
            assert_eq!(stats.put_count, 2);
            assert_eq!(stats.get_count, 3);
            assert_eq!(stats.delete_count, 1);
            assert_eq!(stats.hit_count, 2);
            assert_eq!(stats.miss_count, 1);
            assert!(stats.hit_rate() > 0.0);
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_redis_health_check() {
        if let Some(backend) = create_test_backend().await {
            let healthy = backend.health_check().await.unwrap();
            assert!(healthy);
        } else {
            eprintln!("Skipping test: Redis not available");
        }
    }

    #[tokio::test]
    async fn test_config_builder() {
        let config = RedisConfig::builder()
            .url("redis://localhost:6379")
            .key_prefix("myapp:")
            .default_ttl(Duration::from_secs(3600))
            .pool_size(10, 50)
            .cluster_mode(false)
            .build()
            .unwrap();

        assert_eq!(config.urls.len(), 1);
        assert_eq!(config.key_prefix, "myapp:");
        assert_eq!(config.default_ttl, Some(Duration::from_secs(3600)));
        assert_eq!(config.min_connections, 10);
        assert_eq!(config.max_connections, 50);
        assert!(!config.cluster_mode);
    }
}