sabi_redis 0.7.0

The sabi data access library for Redis 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
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
// Copyright (C) 2026 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.

use r2d2::{Builder, Pool, PooledConnection};
use redis::sentinel::{
    LockedSentinelClient, SentinelClient, SentinelClientBuilder, SentinelNodeConnectionInfo,
    SentinelServerType,
};
use redis::{Connection, IntoConnectionInfo};
use sabi::{AsyncGroup, DataConn, DataSrc};

use std::fmt::Debug;
use std::mem;

/// The error type for synchronous Redis Sentinel operations.
#[derive(Debug)]
pub enum RedisSentinelSyncError {
    /// Indicates that the Redis Sentinel data source has not been set up yet.
    NotSetupYet,
    /// Indicates that the Redis Sentinel data source has already been set up.
    AlreadySetup,
    /// Indicates a failure to parse connection addresses.
    FailToParseConnectionAddrs,
    /// Indicates a failure to create a Sentinel client builder.
    FailToCreateSentinelClientBuilder,
    /// Indicates a failure to build a Redis connection pool.
    FailToBuildPool,
    /// Indicates a failure to build a Redis Sentinel client.
    FailToBuildSentinelClient,
    /// Indicates a failure to get a connection from the pool.
    FailToGetConnectionFromPool,
}

#[allow(clippy::type_complexity)]
/// A data connection for Redis Sentinel, providing synchronous operations.
///
/// This structure holds a pooled connection for a Redis Sentinel-managed setup
/// and allows for adding hooks (pre-commit, post-commit, and force-back)
/// that are executed during the lifecycle of a data operation managed by `sabi`.
///
/// # Examples
/// ```
/// use sabi_redis::sentinel::RedisSentinelDataConn;
/// use redis::Commands;
/// use sabi::DataAcc;
///
/// trait MyDataAcc: DataAcc {
///     fn set_value(&mut self, key: &str, val: &str) -> errs::Result<()> {
///         let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;
///         let conn = data_conn.get_connection();
///         conn.set(key, val).map_err(|e| errs::Err::with_source("fail", e))
///     }
/// }
/// ```
pub struct RedisSentinelDataConn {
    conn: PooledConnection<LockedSentinelClient>,
    pre_commit_vec: Vec<Box<dyn FnMut(&mut Connection) -> errs::Result<()>>>,
    post_commit_vec: Vec<Box<dyn FnMut(&mut Connection) -> errs::Result<()>>>,
    force_back_vec: Vec<Box<dyn FnMut(&mut Connection) -> errs::Result<()>>>,
}

impl RedisSentinelDataConn {
    fn new(conn: PooledConnection<LockedSentinelClient>) -> Self {
        Self {
            conn,
            pre_commit_vec: Vec::new(),
            post_commit_vec: Vec::new(),
            force_back_vec: Vec::new(),
        }
    }

    /// Gets a Sentinel-managed connection from the pool.
    ///
    /// # Returns
    /// Returns a mutable reference to a `PooledConnection<LockedSentinelClient>`.
    pub fn get_connection(&mut self) -> &mut PooledConnection<LockedSentinelClient> {
        &mut self.conn
    }

    /// Adds a function to be executed before a commit occurs in the `sabi` lifecycle.
    ///
    /// # Arguments
    /// * `f` - A closure or function that takes a mutable reference to a `Connection` and returns a `Result`.
    pub fn add_pre_commit<F>(&mut self, f: F)
    where
        F: FnMut(&mut Connection) -> errs::Result<()> + 'static,
    {
        self.pre_commit_vec.push(Box::new(f));
    }

    /// Adds a function to be executed after a successful commit in the `sabi` lifecycle.
    ///
    /// # Arguments
    /// * `f` - A closure or function that takes a mutable reference to a `Connection` and returns a `Result`.
    pub fn add_post_commit<F>(&mut self, f: F)
    where
        F: FnMut(&mut Connection) -> errs::Result<()> + 'static,
    {
        self.post_commit_vec.push(Box::new(f));
    }

    /// Adds a function to be executed when a rollback or forced recovery is triggered.
    ///
    /// # Arguments
    /// * `f` - A closure or function that takes a mutable reference to a `Connection` and returns a `Result`.
    pub fn add_force_back<F>(&mut self, f: F)
    where
        F: FnMut(&mut Connection) -> errs::Result<()> + 'static,
    {
        self.force_back_vec.push(Box::new(f));
    }
}

impl DataConn for RedisSentinelDataConn {
    fn pre_commit(&mut self, _ag: &mut AsyncGroup) -> errs::Result<()> {
        for f in self.pre_commit_vec.iter_mut() {
            f(&mut self.conn)?;
        }
        Ok(())
    }

    fn commit(&mut self, _ag: &mut AsyncGroup) -> errs::Result<()> {
        Ok(())
    }

    fn post_commit(&mut self, _ag: &mut AsyncGroup) {
        for f in self.post_commit_vec.iter_mut() {
            // for error notification
            let _ = f(&mut self.conn);
        }
    }

    fn rollback(&mut self, _ag: &mut AsyncGroup) {}

    fn should_force_back(&self) -> bool {
        true
    }

    fn force_back(&mut self, _ag: &mut AsyncGroup) {
        for f in self.force_back_vec.iter_mut().rev() {
            // for error notification
            let _ = f(&mut self.conn);
        }
    }

    fn close(&mut self) {}
}

/// A data source for Redis Sentinel, used to initialize and provide `RedisSentinelDataConn` instances.
///
/// This struct implements the `DataSrc` trait from the `sabi` library.
///
/// # Examples
/// ```
/// use sabi_redis::sentinel::RedisSentinelDataSrc;
/// use sabi::DataHub;
///
/// let mut data = DataHub::new();
/// data.uses("redis", RedisSentinelDataSrc::new(
///     vec![
///         "redis://127.0.0.1:26479",
///         "redis://127.0.0.1:26480",
///         "redis://127.0.0.1:26481",
///     ],
///     "mymaster",
/// ));
/// ```
///
/// # Type Parameters
/// * `T` - A type that can be converted into Redis connection info.
pub struct RedisSentinelDataSrc<T>
where
    T: redis::IntoConnectionInfo,
{
    pool: Option<RedisPool<T>>,
}

struct SentinelConfig<T> {
    addrs: Vec<T>,
    service_name: String,
    node_conn_info: Option<SentinelNodeConnectionInfo>,
    server_type: SentinelServerType,
    pool_builder: Builder<LockedSentinelClient>,
}

struct SentinelBuilderConfig {
    client_builder: SentinelClientBuilder,
    pool_builder: Builder<LockedSentinelClient>,
}

enum RedisPool<T>
where
    T: IntoConnectionInfo,
{
    Object(Pool<LockedSentinelClient>),
    Client(Box<SentinelConfig<T>>),
    Builder(Box<SentinelBuilderConfig>),
}

impl<T> RedisSentinelDataSrc<T>
where
    T: redis::IntoConnectionInfo,
{
    /// Creates a new `RedisSentinelDataSrc` with Sentinel addresses and a service name.
    ///
    /// # Arguments
    /// * `addrs` - A vector of items that can be converted into Redis connection info (Sentinel addresses).
    /// * `service_name` - The name of the Redis service to monitor.
    ///
    /// # Returns
    /// Returns a new instance of `RedisSentinelDataSrc`.
    pub fn new(addrs: Vec<T>, service_name: impl AsRef<str>) -> Self {
        Self {
            pool: Some(RedisPool::Client(Box::new(SentinelConfig {
                addrs,
                service_name: service_name.as_ref().to_string(),
                node_conn_info: None,
                server_type: SentinelServerType::Master,
                pool_builder: Pool::builder(),
            }))),
        }
    }

    /// Creates a new `RedisSentinelDataSrc` with specific Sentinel client parameters.
    ///
    /// # Arguments
    /// * `addrs` - A vector of Sentinel addresses.
    /// * `service_name` - The name of the Redis service.
    /// * `node_conn_info` - Connection information for the Redis nodes.
    /// * `server_type` - The type of server to connect to (e.g., Master or Slave).
    ///
    /// # Returns
    /// Returns a new instance of `RedisSentinelDataSrc`.
    pub fn with_client_params(
        addrs: Vec<T>,
        service_name: impl AsRef<str>,
        node_conn_info: SentinelNodeConnectionInfo,
        server_type: SentinelServerType,
    ) -> Self {
        Self {
            pool: Some(RedisPool::Client(Box::new(SentinelConfig {
                addrs,
                service_name: service_name.as_ref().to_string(),
                node_conn_info: Some(node_conn_info),
                server_type,
                pool_builder: Pool::builder(),
            }))),
        }
    }

    /// Creates a new `RedisSentinelDataSrc` with Sentinel client parameters and a custom pool builder.
    ///
    /// # Arguments
    /// * `addrs` - A vector of Sentinel addresses.
    /// * `service_name` - The name of the Redis service.
    /// * `node_conn_info` - Connection information for the Redis nodes.
    /// * `server_type` - The type of server to connect to.
    /// * `pool_builder` - A `r2d2::Builder` for Configuring the connection pool.
    ///
    /// # Returns
    /// Returns a new instance of `RedisSentinelDataSrc`.
    pub fn with_client_params_and_pool_builder(
        addrs: Vec<T>,
        service_name: impl AsRef<str>,
        node_conn_info: SentinelNodeConnectionInfo,
        server_type: SentinelServerType,
        pool_builder: Builder<LockedSentinelClient>,
    ) -> Self {
        Self {
            pool: Some(RedisPool::Client(Box::new(SentinelConfig {
                addrs,
                service_name: service_name.as_ref().to_string(),
                node_conn_info: Some(node_conn_info),
                server_type,
                pool_builder,
            }))),
        }
    }
}

impl RedisSentinelDataSrc<&'static str> {
    /// Creates a new `RedisSentinelDataSrc` with a pre-configured `SentinelClientBuilder`.
    ///
    /// # Arguments
    /// * `client_builder` - A `SentinelClientBuilder` for the Sentinel setup.
    ///
    /// # Returns
    /// Returns a new instance of `RedisSentinelDataSrc`.
    pub fn with_client_builder(client_builder: SentinelClientBuilder) -> Self {
        Self {
            pool: Some(RedisPool::Builder(Box::new(SentinelBuilderConfig {
                client_builder,
                pool_builder: Pool::builder(),
            }))),
        }
    }

    /// Creates a new `RedisSentinelDataSrc` with a Sentinel client builder and a custom pool builder.
    ///
    /// # Arguments
    /// * `client_builder` - A `SentinelClientBuilder` for the Sentinel setup.
    /// * `pool_builder` - A `r2d2::Builder` for Configuring the connection pool.
    ///
    /// # Returns
    /// Returns a new instance of `RedisSentinelDataSrc`.
    pub fn with_client_builder_and_pool_builder(
        client_builder: SentinelClientBuilder,
        pool_builder: Builder<LockedSentinelClient>,
    ) -> Self {
        Self {
            pool: Some(RedisPool::Builder(Box::new(SentinelBuilderConfig {
                client_builder,
                pool_builder,
            }))),
        }
    }
}

impl<T> DataSrc<RedisSentinelDataConn> for RedisSentinelDataSrc<T>
where
    T: redis::IntoConnectionInfo,
{
    fn setup(&mut self, _ag: &mut AsyncGroup) -> errs::Result<()> {
        let pool_opt = mem::take(&mut self.pool);
        let pool = pool_opt.ok_or_else(|| errs::Err::new(RedisSentinelSyncError::AlreadySetup))?;
        match pool {
            RedisPool::Client(cfg) => {
                let client = SentinelClient::build(
                    cfg.addrs,
                    cfg.service_name,
                    cfg.node_conn_info,
                    cfg.server_type,
                )
                .map_err(|e| {
                    errs::Err::with_source(RedisSentinelSyncError::FailToBuildSentinelClient, e)
                })?;

                let pool = cfg
                    .pool_builder
                    .build(LockedSentinelClient::new(client))
                    .map_err(|e| {
                        errs::Err::with_source(RedisSentinelSyncError::FailToBuildPool, e)
                    })?;

                self.pool = Some(RedisPool::Object(pool));
                Ok(())
            }
            RedisPool::Builder(cfg) => {
                let client = cfg.client_builder.build().map_err(|e| {
                    errs::Err::with_source(RedisSentinelSyncError::FailToBuildSentinelClient, e)
                })?;

                let pool = cfg
                    .pool_builder
                    .build(LockedSentinelClient::new(client))
                    .map_err(|e| {
                        errs::Err::with_source(RedisSentinelSyncError::FailToBuildPool, e)
                    })?;

                self.pool = Some(RedisPool::Object(pool));
                Ok(())
            }

            _ => Err(errs::Err::new(RedisSentinelSyncError::AlreadySetup)),
        }
    }

    fn close(&mut self) {}

    fn create_data_conn(&mut self) -> errs::Result<Box<RedisSentinelDataConn>> {
        let pool = self
            .pool
            .as_mut()
            .ok_or_else(|| errs::Err::new(RedisSentinelSyncError::NotSetupYet))?;
        match pool {
            RedisPool::Object(pool) => match pool.get() {
                Ok(conn) => Ok(Box::new(RedisSentinelDataConn::new(conn))),
                Err(e) => Err(errs::Err::with_source(
                    RedisSentinelSyncError::FailToGetConnectionFromPool,
                    e,
                )),
            },
            _ => Err(errs::Err::new(RedisSentinelSyncError::NotSetupYet)),
        }
    }
}

#[cfg(test)]
mod unit_tests {
    use super::*;
    use override_macro::{overridable, override_with};
    use redis::sentinel::SentinelNodeConnectionInfo;
    use redis::{Commands, RedisConnectionInfo};
    use sabi::{DataAcc, DataHub};
    use std::time;

    #[derive(Debug)]
    enum SampleError {
        FailToGetValue,
        FailToSetValue,
        FailToDelValue,
    }

    #[overridable]
    trait RedisSentinelSampleDataAcc: DataAcc {
        fn get_sample_key(&mut self) -> errs::Result<Option<String>> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;
            let conn = data_conn.get_connection();
            conn.get("sample_sentinel")
                .map_err(|e| errs::Err::with_source(SampleError::FailToGetValue, e))
        }
        fn set_sample_key(&mut self, val: &str) -> errs::Result<()> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;
            let conn = data_conn.get_connection();
            conn.set("sample_sentinel", val)
                .map_err(|e| errs::Err::with_source(SampleError::FailToGetValue, e))
        }
        fn del_sample_key(&mut self) -> errs::Result<()> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;
            let conn = data_conn.get_connection();
            conn.del("sample_sentinel")
                .map_err(|e| errs::Err::with_source(SampleError::FailToDelValue, e))
        }

        fn set_sample_key_with_force_back(&mut self, val: &str) -> errs::Result<()> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;

            {
                let conn = data_conn.get_connection();
                conn.set::<&str, &str, ()>("sample_force_back_sentinel", val)
                    .map_err(|e| errs::Err::with_source(SampleError::FailToSetValue, e))?;
            }

            data_conn.add_force_back(|conn| {
                conn.del("sample_force_back_sentinel")
                    .map_err(|e| errs::Err::with_source("fail to force back", e))
            });

            {
                let conn = data_conn.get_connection();
                conn.set::<&str, &str, ()>("sample_force_back_sentinel_2", val)
                    .map_err(|e| errs::Err::with_source(SampleError::FailToSetValue, e))?;
            }

            data_conn.add_force_back(|conn| {
                conn.del("sample_force_back_sentinel_2")
                    .map_err(|e| errs::Err::with_source("fail to force back", e))
            });

            Ok(())
        }

        fn set_sample_key_with_pre_commit(&mut self, val: &str) -> errs::Result<()> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;

            let val_owned = val.to_string();

            data_conn.add_pre_commit(move |conn| {
                conn.set::<&str, &str, ()>("sample_pre_commit_sentinel", &val_owned)
                    .map_err(|e| errs::Err::with_source(SampleError::FailToSetValue, e))?;
                Ok(())
            });

            Ok(())
        }

        fn set_sample_key_with_post_commit(&mut self, val: &str) -> errs::Result<()> {
            let data_conn = self.get_data_conn::<RedisSentinelDataConn>("redis")?;

            let val_owned = val.to_string();

            data_conn.add_post_commit(move |conn| {
                conn.set::<&str, &str, ()>("sample_post_commit_sentinel", &val_owned)
                    .map_err(|e| errs::Err::with_source(SampleError::FailToSetValue, e))?;
                Ok(())
            });

            Ok(())
        }
    }
    impl RedisSentinelSampleDataAcc for DataHub {}

    #[overridable]
    trait SampleDataSentinel {
        fn get_sample_key(&mut self) -> errs::Result<Option<String>>;
        fn set_sample_key(&mut self, value: &str) -> errs::Result<()>;
        fn del_sample_key(&mut self) -> errs::Result<()>;
        fn set_sample_key_with_force_back(&mut self, val: &str) -> errs::Result<()>;
        fn set_sample_key_with_pre_commit(&mut self, val: &str) -> errs::Result<()>;
        fn set_sample_key_with_post_commit(&mut self, val: &str) -> errs::Result<()>;
    }
    #[override_with(RedisSentinelSampleDataAcc)]
    impl SampleDataSentinel for DataHub {}

    fn sample_logic(data: &mut impl SampleDataSentinel) -> errs::Result<()> {
        data.get_sample_key().expect("Data exists");
        data.set_sample_key("Hello")?;
        data.del_sample_key()?;
        Ok(())
    }

    #[test]
    fn test_new() -> errs::Result<()> {
        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::new(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
            ),
        );
        data.run(sample_logic)?;
        Ok(())
    }

    #[test]
    fn test_with_client_params() -> errs::Result<()> {
        let redis_connection_info = RedisConnectionInfo::default().set_db(1);
        let sentinel_node_connection_info =
            SentinelNodeConnectionInfo::default().set_redis_connection_info(redis_connection_info);

        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::with_client_params(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
                sentinel_node_connection_info,
                SentinelServerType::Master,
            ),
        );
        data.run(sample_logic)?;
        Ok(())
    }

    #[test]
    fn test_with_client_params_and_pool_builder() -> errs::Result<()> {
        let redis_connection_info = RedisConnectionInfo::default().set_db(1);
        let sentinel_node_connection_info =
            SentinelNodeConnectionInfo::default().set_redis_connection_info(redis_connection_info);

        let pool_builder = Pool::<LockedSentinelClient>::builder()
            .max_size(100)
            .min_idle(Some(10))
            .max_lifetime(Some(time::Duration::from_secs(60 * 60)))
            .idle_timeout(Some(time::Duration::from_secs(5 * 60)))
            .connection_timeout(time::Duration::from_secs(30));

        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::with_client_params_and_pool_builder(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
                sentinel_node_connection_info,
                SentinelServerType::Master,
                pool_builder,
            ),
        );
        data.run(sample_logic)?;
        Ok(())
    }

    #[test]
    fn test_with_client_builder() -> errs::Result<()> {
        let builder = SentinelClientBuilder::new(
            vec![
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26479),
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26480),
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26481),
            ],
            "mymaster".to_string(),
            SentinelServerType::Master,
        )
        .unwrap();

        let mut data = DataHub::new();
        data.uses("redis", RedisSentinelDataSrc::with_client_builder(builder));
        data.run(sample_logic)?;
        Ok(())
    }

    #[test]
    fn test_with_client_builder_and_pool_builder() -> errs::Result<()> {
        let builder = SentinelClientBuilder::new(
            vec![
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26479),
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26480),
                redis::ConnectionAddr::Tcp(String::from("127.0.0.1"), 26481),
            ],
            "mymaster".to_string(),
            SentinelServerType::Master,
        )
        .unwrap();

        let pool_builder = Pool::<LockedSentinelClient>::builder()
            .max_size(100)
            .min_idle(Some(10))
            .max_lifetime(Some(time::Duration::from_secs(60 * 60)))
            .idle_timeout(Some(time::Duration::from_secs(5 * 60)))
            .connection_timeout(time::Duration::from_secs(30));

        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::with_client_builder_and_pool_builder(builder, pool_builder),
        );
        data.run(sample_logic)?;
        Ok(())
    }

    #[test]
    fn fail_to_setup() {
        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::new(vec!["xxxxxx"], "mymaster"),
        );

        if let Err(err) = data.run(sample_logic) {
            if let Ok(r) = err.reason::<sabi::DataHubError>() {
                match r {
                    sabi::DataHubError::FailToSetupLocalDataSrcs { errors } => {
                        assert_eq!(errors.len(), 1);
                        assert_eq!(errors[0].0.as_ref(), "redis");
                        if let Ok(r) = errors[0].1.reason::<RedisSentinelSyncError>() {
                            match r {
                                RedisSentinelSyncError::FailToBuildSentinelClient => {}
                                _ => panic!(),
                            }
                        }
                        let e = errors[0]
                            .1
                            .source()
                            .unwrap()
                            .downcast_ref::<redis::RedisError>()
                            .unwrap();
                        assert_eq!(e.kind(), redis::ErrorKind::InvalidClientConfig);
                        assert!(e.detail().is_none());
                        assert!(e.code().is_none());
                        assert_eq!(e.category(), "invalid client config");
                    }
                    _ => panic!("{:?}", err),
                }
            } else {
                panic!("{:?}", err)
            }
        } else {
            panic!();
        }
    }

    fn sample_logic_with_force_back_ok(data: &mut impl SampleDataSentinel) -> errs::Result<()> {
        data.set_sample_key_with_force_back("Good Afternoon")?;
        Ok(())
    }
    fn sample_logic_with_force_back_err(data: &mut impl SampleDataSentinel) -> errs::Result<()> {
        data.set_sample_key_with_force_back("Good Afternoon")?;
        Err(errs::Err::new("XXX"))
    }
    fn sample_logic_with_pre_commit(data: &mut impl SampleDataSentinel) -> errs::Result<()> {
        data.set_sample_key_with_pre_commit("Good Evening")?;
        Ok(())
    }
    fn sample_logic_with_post_commit(data: &mut impl SampleDataSentinel) -> errs::Result<()> {
        data.set_sample_key_with_post_commit("Good Night")?;
        Ok(())
    }

    #[test]
    fn test_with_force_back() -> errs::Result<()> {
        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::new(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
            ),
        );

        let r = data.txn(sample_logic_with_force_back_ok);
        assert!(r.is_ok());

        {
            let mut sentinel = redis::sentinel::Sentinel::build(vec![
                "redis://127.0.0.1:26479",
                "redis://127.0.0.1:26480",
                "redis://127.0.0.1:26481",
            ])
            .unwrap();
            let client = sentinel.master_for("mymaster", None).unwrap();
            let mut conn = client.get_connection().unwrap();

            let r: redis::RedisResult<Option<String>> = conn.get("sample_force_back_sentinel");
            let _: redis::RedisResult<()> = conn.del("sample_force_back_sentinel");
            assert_eq!(r.unwrap().unwrap(), "Good Afternoon");

            let r: redis::RedisResult<Option<String>> = conn.get("sample_force_back_sentinel_2");
            let _: redis::RedisResult<()> = conn.del("sample_force_back_sentinel_2");
            assert_eq!(r.unwrap().unwrap(), "Good Afternoon");
        }

        if let Err(err) = data.txn(sample_logic_with_force_back_err) {
            assert_eq!(err.reason::<&str>().unwrap(), &"XXX");
        } else {
            panic!();
        }
        {
            let mut sentinel = redis::sentinel::Sentinel::build(vec![
                "redis://127.0.0.1:26479",
                "redis://127.0.0.1:26480",
                "redis://127.0.0.1:26481",
            ])
            .unwrap();
            let client = sentinel.master_for("mymaster", None).unwrap();
            let mut conn = client.get_connection().unwrap();

            let r: redis::RedisResult<Option<String>> = conn.get("sample_force_back_sentinel");
            let _: redis::RedisResult<()> = conn.del("sample_force_back_sentinel");
            assert!(r.unwrap().is_none());

            let r: redis::RedisResult<Option<String>> = conn.get("sample_force_back_sentinel_2");
            let _: redis::RedisResult<()> = conn.del("sample_force_back_sentinel_2");
            assert!(r.unwrap().is_none());
        }

        Ok(())
    }

    #[test]
    fn test_txn_and_pre_commit() -> errs::Result<()> {
        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::new(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
            ),
        );
        data.txn(sample_logic_with_pre_commit)?;

        {
            let mut sentinel = redis::sentinel::Sentinel::build(vec![
                "redis://127.0.0.1:26479",
                "redis://127.0.0.1:26480",
                "redis://127.0.0.1:26481",
            ])
            .unwrap();
            let client = sentinel.master_for("mymaster", None).unwrap();
            let mut conn = client.get_connection().unwrap();

            let s: redis::RedisResult<Option<String>> = conn.get("sample_pre_commit_sentinel");
            let _: redis::RedisResult<()> = conn.del("sample_pre_commit_sentinel");
            assert_eq!(s.unwrap().unwrap(), "Good Evening");
        }

        Ok(())
    }

    #[test]
    fn test_txn_and_post_commit() -> errs::Result<()> {
        let mut data = DataHub::new();
        data.uses(
            "redis",
            RedisSentinelDataSrc::new(
                vec![
                    "redis://127.0.0.1:26479",
                    "redis://127.0.0.1:26480",
                    "redis://127.0.0.1:26481",
                ],
                "mymaster",
            ),
        );
        data.txn(sample_logic_with_post_commit)?;

        {
            let mut sentinel = redis::sentinel::Sentinel::build(vec![
                "redis://127.0.0.1:26479",
                "redis://127.0.0.1:26480",
                "redis://127.0.0.1:26481",
            ])
            .unwrap();
            let client = sentinel.master_for("mymaster", None).unwrap();
            let mut conn = client.get_connection().unwrap();

            let s: redis::RedisResult<Option<String>> = conn.get("sample_post_commit_sentinel");
            let _: redis::RedisResult<()> = conn.del("sample_post_commit_sentinel");
            assert_eq!(s.unwrap().unwrap(), "Good Night");
        }

        Ok(())
    }
}