mssf-util 0.7.0

mssf utilites and extensions for tokio and more
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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

use std::{
    collections::{BTreeMap, HashMap},
    sync::{Arc, Mutex},
};

use mssf_core::{
    GUID, WString,
    runtime::IStatefulServicePartition,
    sync::SimpleCancelToken,
    types::{Epoch, ServicePartitionInformation, Uri},
};

#[derive(Clone)]
pub struct StatefulServicePartitionMock {
    info: mssf_core::types::ServicePartitionInformation,
    read_status: Arc<Mutex<mssf_core::types::ServicePartitionAccessStatus>>,
    write_status: Arc<Mutex<mssf_core::types::ServicePartitionAccessStatus>>,
}

impl StatefulServicePartitionMock {
    pub fn new(info: mssf_core::types::ServicePartitionInformation) -> Self {
        Self {
            info,
            read_status: Arc::new(Mutex::new(
                mssf_core::types::ServicePartitionAccessStatus::ReconfigurationPending,
            )),
            write_status: Arc::new(Mutex::new(
                mssf_core::types::ServicePartitionAccessStatus::ReconfigurationPending,
            )),
        }
    }
    pub fn new_boxed(
        info: mssf_core::types::ServicePartitionInformation,
    ) -> Box<dyn IStatefulServicePartition> {
        Box::new(Self::new(info))
    }
    pub fn set_read_status(&self, status: mssf_core::types::ServicePartitionAccessStatus) {
        *self.read_status.lock().unwrap() = status;
    }
    pub fn set_write_status(&self, status: mssf_core::types::ServicePartitionAccessStatus) {
        *self.write_status.lock().unwrap() = status;
    }
}

impl IStatefulServicePartition for StatefulServicePartitionMock {
    fn create_replicator(
        &self,
    ) -> mssf_core::Result<Box<dyn mssf_core::runtime::IPrimaryReplicator>> {
        unimplemented!("Not implemented")
    }

    fn get_partition_information(
        &self,
    ) -> mssf_core::Result<mssf_core::types::ServicePartitionInformation> {
        Ok(self.info.clone())
    }

    fn get_read_status(&self) -> mssf_core::Result<mssf_core::types::ServicePartitionAccessStatus> {
        Ok(*self.read_status.lock().unwrap())
    }

    fn get_write_status(
        &self,
    ) -> mssf_core::Result<mssf_core::types::ServicePartitionAccessStatus> {
        Ok(*self.write_status.lock().unwrap())
    }

    fn report_load(&self, _metrics: &[mssf_core::types::LoadMetric]) -> mssf_core::Result<()> {
        Ok(())
    }

    fn report_fault(&self, _fault_type: mssf_core::types::FaultType) -> mssf_core::Result<()> {
        Ok(())
    }

    fn report_move_cost(&self, _move_cost: mssf_core::types::MoveCost) -> mssf_core::Result<()> {
        Ok(())
    }

    fn report_partition_health(
        &self,
        _healthinfo: &mssf_core::types::HealthInformation,
    ) -> mssf_core::Result<()> {
        Ok(())
    }

    fn report_replica_health(
        &self,
        _healthinfo: &mssf_core::types::HealthInformation,
    ) -> mssf_core::Result<()> {
        Ok(())
    }

    fn try_get_com(
        &self,
    ) -> mssf_core::Result<&mssf_com::FabricRuntime::IFabricStatefulServicePartition> {
        Err(mssf_core::ErrorCode::FABRIC_E_OPERATION_NOT_SUPPORTED.into())
    }
}

#[derive(Clone)]
pub struct CreateStatefulServicePartitionArg {
    pub partition_id: GUID,
    pub replica_count: usize,
    pub init_data: Vec<u8>,
    pub service_name: Uri,
    pub service_type_name: WString,
}

/// Test driver for a single stateful service replica.
pub struct StatefulServicePartitionDriver {
    /// This keeps track of which factory to use next.
    factory_index: i64,
    service_factory: Vec<Box<dyn mssf_core::runtime::IStatefulServiceFactory>>,
    replica_index: i64,
    epoch_index: Epoch, // Used to generate new epoch.
    partition_state: PartitionState,
}

struct PartitionState {
    // states for all replicas.
    pub replica_states: HashMap<i64, StatefulServiceReplicaState>,
    pub primary_index: i64,
    pub epoch: Epoch,
    pub static_info: Option<CreateStatefulServicePartitionArg>, // Filled when created.
    // Write quorum and secondary replica list.
    pub current_configuration: mssf_core::types::ReplicaSetConfig,
}

struct StatefulServiceReplicaState {
    pub replica: Box<dyn mssf_core::runtime::IStatefulServiceReplica>,
    pub replicator: Box<dyn mssf_core::runtime::IPrimaryReplicator>,
    pub partition: StatefulServicePartitionMock,
    pub factory_index: i64, // The index of the factory that created the replica
    pub _replica_address: WString,
    pub _replicator_address: WString,
}

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

impl StatefulServicePartitionDriver {
    pub fn new() -> Self {
        Self {
            service_factory: Vec::new(),
            factory_index: 0,
            replica_index: 1, // replica id starting from 1
            epoch_index: Epoch {
                data_loss_number: 0,
                configuration_number: 1,
            },
            partition_state: PartitionState {
                replica_states: HashMap::new(),
                primary_index: 1, // First replica is the primary.
                epoch: Epoch {
                    data_loss_number: 0,
                    configuration_number: 1,
                },
                static_info: None,
                current_configuration: mssf_core::types::ReplicaSetConfig {
                    replicas: vec![],
                    write_quorum: 0,
                },
            },
        }
    }

    /// Register a service factory to be used to create replicas.
    /// One should register multiple factories to simulate multi node scenarios.
    /// Replicas are created in round robin fashion from the registered factories.
    pub fn register_service_factory(
        &mut self,
        factory: Box<dyn mssf_core::runtime::IStatefulServiceFactory>,
    ) {
        self.service_factory.push(factory);
    }

    /// Get the next service factory in round robin fashion.
    /// This ensures that multiple factories can be tested, to simulate
    /// multi node scenarios.
    /// Returns the current index and the factory.
    fn get_round_robin_factory(
        &mut self,
    ) -> (i64, &dyn mssf_core::runtime::IStatefulServiceFactory) {
        assert!(!self.service_factory.is_empty());
        let idx = self.factory_index as usize % self.service_factory.len();
        self.factory_index += 1;
        (idx as i64, &*self.service_factory[idx])
    }

    fn next_replica_index(&mut self) -> i64 {
        let idx = self.replica_index;
        self.replica_index += 1;
        idx
    }

    fn next_epoch_index(&mut self) -> Epoch {
        let idx = self.epoch_index.clone();
        self.epoch_index.configuration_number += 1;
        idx
    }

    fn get_primary_state(&self) -> mssf_core::Result<&StatefulServiceReplicaState> {
        let state = self
            .partition_state
            .replica_states
            .get(&self.partition_state.primary_index)
            .ok_or_else(|| {
                mssf_core::Error::from(mssf_core::ErrorCode::FABRIC_E_REPLICA_DOES_NOT_EXIST)
            })?;
        Ok(state)
    }

    /// Check the invariants of the partition state.
    /// Panics if any invariant is violated.
    fn check_partition_state(&self) {
        if self.partition_state.replica_states.is_empty() {
            assert!(self.partition_state.static_info.is_none());
            assert_eq!(self.partition_state.current_configuration.replicas.len(), 0);
            assert_eq!(self.partition_state.current_configuration.write_quorum, 0);
            return;
        }
        // check primary exists
        self.get_primary_state().unwrap();
        // check quorum size matches
        let expected_quorum = (self.partition_state.replica_states.len() as u32) / 2 + 1;
        assert_eq!(
            self.partition_state.current_configuration.write_quorum,
            expected_quorum
        );
    }
}

// Public Accessors
impl StatefulServicePartitionDriver {
    /// Get the current primary replica id.
    pub fn get_primary_replica_id(&self) -> i64 {
        self.partition_state.primary_index
    }
    /// Get a replica by id.
    pub fn get_replica(
        &self,
        replica_id: i64,
    ) -> Option<&dyn mssf_core::runtime::IStatefulServiceReplica> {
        let state = self.partition_state.replica_states.get(&replica_id);
        state.map(|s| s.replica.as_ref())
    }
    /// Get a replicator by replica id.
    pub fn get_replicator(
        &self,
        replica_id: i64,
    ) -> Option<&dyn mssf_core::runtime::IPrimaryReplicator> {
        let state = self.partition_state.replica_states.get(&replica_id);
        state.map(|s| s.replicator.as_ref())
    }
    /// List all replica ids.
    pub fn list_replica_ids(&self) -> Vec<i64> {
        self.partition_state
            .replica_states
            .keys()
            .cloned()
            .collect()
    }
}

// Workflow implementations.
impl StatefulServicePartitionDriver {
    /// Create a stateful service partition with the specified number of replicas.
    /// The first replica is the primary.
    /// Runs the replica build steps.
    pub async fn create_service_partition(
        &mut self,
        desc: &CreateStatefulServicePartitionArg,
    ) -> mssf_core::Result<()> {
        assert!(desc.replica_count > 0);
        assert!(self.partition_state.replica_states.is_empty());

        let mut replicas = BTreeMap::new();
        let mut replicators = BTreeMap::new();
        let mut replica_addresses = BTreeMap::new();
        let mut replicator_addresses = BTreeMap::new();
        let mut replica_infos = BTreeMap::new();
        let mut partitions = BTreeMap::new();

        for _ in 0..desc.replica_count {
            let id = self.next_replica_index();
            let (factory_index, factory) = self.get_round_robin_factory();
            let replica = factory
                .create_replica(
                    desc.service_type_name.clone(),
                    desc.service_name.clone(),
                    &desc.init_data,
                    desc.partition_id,
                    id,
                )
                .inspect_err(|e| {
                    tracing::error!("Failed to create stateful service replica: {:?}", e)
                })?;
            let prev = replicas.insert(id, (factory_index, replica));
            assert!(prev.is_none(), "Service replica already exists");
        }

        // open all replicas
        for (id, (_, replica)) in &replicas {
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            // TODO: support other partition schemes.
            let partition =
                StatefulServicePartitionMock::new(ServicePartitionInformation::Singleton(
                    mssf_core::types::SingletonPartitionInformation {
                        id: desc.partition_id,
                    },
                ));
            let replctr = replica
                .open(
                    mssf_core::types::OpenMode::New,
                    Arc::new(partition.clone()),
                    cancellation_token,
                )
                .await?;
            replicators.insert(*id, replctr);
            partitions.insert(*id, partition);
        }

        // open all replicators
        for (id, replctr) in &replicators {
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();

            let replctr_addr = replctr.open(cancellation_token).await?;
            replicator_addresses.insert(*id, replctr_addr);
        }

        // assign roles to replicators. for simplicity, we assume the first replica is the primary.
        let primary_index = 1;
        let epoch = self.next_epoch_index();
        for (id, rplctr) in &replicators {
            let epoch_cp = epoch.clone();
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            if *id == primary_index {
                rplctr
                    .change_role(
                        epoch_cp,
                        mssf_core::types::ReplicaRole::Primary,
                        cancellation_token,
                    )
                    .await?;
                self.partition_state.primary_index = primary_index;
            } else {
                rplctr
                    .change_role(
                        epoch_cp,
                        mssf_core::types::ReplicaRole::IdleSecondary,
                        cancellation_token,
                    )
                    .await?;
            }
        }

        // assign roles to replicas. First one is primary.
        for (id, (_, replica)) in &replicas {
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            let replica_addr = if *id == self.partition_state.primary_index {
                replica
                    .change_role(mssf_core::types::ReplicaRole::Primary, cancellation_token)
                    .await?
            } else {
                replica
                    .change_role(
                        mssf_core::types::ReplicaRole::IdleSecondary,
                        cancellation_token,
                    )
                    .await?
            };
            replica_addresses.insert(*id, replica_addr);
        }

        // build secondaries.
        let primary = replicators
            .get(&self.partition_state.primary_index)
            .unwrap();
        for (id, (_, replica)) in &replicas {
            if *id == self.partition_state.primary_index {
                let replica_info = mssf_core::types::ReplicaInformation {
                    replicator_address: replicator_addresses.get(id).unwrap().clone(),
                    id: *id,
                    role: mssf_core::types::ReplicaRole::Primary,
                    status: mssf_core::types::ReplicaStatus::Up,
                    current_progress: -1, // -1 for invalid. observed in sf logs.
                    catch_up_capability: -1,
                    must_catch_up: false,
                };
                replica_infos.insert(*id, replica_info);
                continue;
            }
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            let replica_info = mssf_core::types::ReplicaInformation {
                replicator_address: replicator_addresses.get(id).unwrap().clone(),
                id: *id,
                role: mssf_core::types::ReplicaRole::IdleSecondary,
                status: mssf_core::types::ReplicaStatus::Up,
                current_progress: -1,
                catch_up_capability: -1,
                must_catch_up: false,
            };
            replica_infos.insert(*id, replica_info.clone());
            primary
                .build_replica(replica_info, cancellation_token)
                .await?;
            // change replicator role to active secondary after successful build.
            let rplctr = replicators.get(id).unwrap();
            rplctr
                .change_role(
                    epoch.clone(),
                    mssf_core::types::ReplicaRole::ActiveSecondary,
                    SimpleCancelToken::new_boxed(),
                )
                .await?;
            // change replica role to active secondary after successful build.
            replica
                .change_role(
                    mssf_core::types::ReplicaRole::ActiveSecondary,
                    SimpleCancelToken::new_boxed(),
                )
                .await?;
            // update the replica info
            replica_infos.get_mut(id).unwrap().role =
                mssf_core::types::ReplicaRole::ActiveSecondary;
        }

        // Run update catchup workflow for each secondary replica. Exclude primary.
        let mut new_config = mssf_core::types::ReplicaSetConfig {
            replicas: vec![],
            write_quorum: 1, // for primary
        };
        // incase only primary exists, save the current configuration.
        self.partition_state.current_configuration = new_config.clone();
        let mut ready_replicas = 1;
        for id in replicas.keys() {
            if *id == self.partition_state.primary_index {
                continue;
            }
            let prev_config = new_config.clone();
            // construct new config
            let replica_info = replica_infos.get(id).unwrap().clone();
            new_config.replicas.push(replica_info);
            ready_replicas += 1;
            new_config.write_quorum = ready_replicas / 2 + 1_u32;

            primary.update_catch_up_replica_set_configuration(new_config.clone(), prev_config)?;

            // wait for catch up
            primary
                .wait_for_catch_up_quorum(
                    mssf_core::types::ReplicaSetQuorumMode::Write,
                    SimpleCancelToken::new_boxed(),
                )
                .await?;
            // update current configuration
            primary.update_current_replica_set_configuration(new_config.clone())?;
            self.partition_state.current_configuration = new_config.clone();
        }

        // Update read write status.
        // TODO: This might not be accurate.
        // Maybe for primary it is always granted.
        // Since the quorum size is increasing and no replica down during build process.
        for (id, partition) in &partitions {
            if *id == self.partition_state.primary_index {
                partition.set_read_status(mssf_core::types::ServicePartitionAccessStatus::Granted);
                partition.set_write_status(mssf_core::types::ServicePartitionAccessStatus::Granted);
            } else {
                partition
                    .set_read_status(mssf_core::types::ServicePartitionAccessStatus::NotPrimary);
                partition
                    .set_write_status(mssf_core::types::ServicePartitionAccessStatus::NotPrimary);
            }
        }

        // Save the state.
        for (id, (factory_index, replica)) in replicas {
            let state = StatefulServiceReplicaState {
                replica,
                replicator: replicators.remove(&id).unwrap(),
                _replica_address: replica_addresses.remove(&id).unwrap(),
                _replicator_address: replicator_addresses.remove(&id).unwrap(),
                partition: partitions.remove(&id).unwrap(),
                factory_index,
            };
            self.partition_state.replica_states.insert(id, state);
        }
        self.partition_state.epoch = epoch;
        self.partition_state.static_info = Some(desc.clone());

        self.check_partition_state();
        Ok(())
    }

    /// Delete the service partition.
    pub async fn delete_service_partition(&mut self) -> mssf_core::Result<()> {
        // Not sure if the sequence is correct.

        // Change read write status to pending
        for state in self.partition_state.replica_states.values_mut() {
            state.partition.set_read_status(
                mssf_core::types::ServicePartitionAccessStatus::ReconfigurationPending,
            );
            state.partition.set_write_status(
                mssf_core::types::ServicePartitionAccessStatus::ReconfigurationPending,
            );
        }

        // Change primary to active secondary
        let primary = self
            .partition_state
            .replica_states
            .get_mut(&self.partition_state.primary_index)
            .expect("Primary replica not found");

        // Replicator change_role is called before Replica change_role.
        primary
            .replicator
            .change_role(
                self.partition_state.epoch.clone(), // Epoch is unchanged.
                mssf_core::types::ReplicaRole::ActiveSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;
        primary
            .replica
            .change_role(
                mssf_core::types::ReplicaRole::ActiveSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;

        // change role to none for all replicas
        // Replicator change_role is called before Replica change_role.
        for state in self.partition_state.replica_states.values_mut() {
            state
                .replicator
                .change_role(
                    self.partition_state.epoch.clone(), // Epoch is unchanged.
                    mssf_core::types::ReplicaRole::None,
                    SimpleCancelToken::new_boxed(),
                )
                .await?;
            state
                .replica
                .change_role(
                    mssf_core::types::ReplicaRole::None,
                    SimpleCancelToken::new_boxed(),
                )
                .await?;
        }

        // close all replicas and replicators
        for state in self.partition_state.replica_states.values_mut() {
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            state.replica.close(cancellation_token.clone()).await?;
            state.replicator.close(cancellation_token).await?;
        }

        // clear the state
        self.partition_state.replica_states.clear();
        self.partition_state.static_info = None;
        self.partition_state.current_configuration = mssf_core::types::ReplicaSetConfig {
            replicas: vec![],
            write_quorum: 0,
        };
        self.check_partition_state();
        Ok(())
    }

    /// Restart a secondary replica gracefully.
    pub async fn restart_secondary_graceful(&mut self, replica_id: i64) -> mssf_core::Result<()> {
        // check if replica exists
        {
            self.partition_state
                .replica_states
                .get_mut(&replica_id)
                .ok_or_else(|| {
                    mssf_core::Error::from(mssf_core::ErrorCode::FABRIC_E_REPLICA_DOES_NOT_EXIST)
                })?;
            // check if it is not primary
            if replica_id == self.partition_state.primary_index {
                tracing::error!(
                    "Replica {} is primary, cannot restart as secondary",
                    replica_id
                );
                return Err(mssf_core::Error::from(
                    mssf_core::ErrorCode::FABRIC_E_INVALID_OPERATION,
                ));
            }
        }

        // Update primary to remove the replica from the configuration.
        {
            let primary = self.get_primary_state().unwrap();
            let current_config = self.partition_state.current_configuration.clone();
            let replica_count = current_config.replicas.len();
            let new_replicas = current_config
                .replicas
                .iter()
                .filter(|r| r.id != replica_id)
                .cloned()
                .collect::<Vec<_>>();
            let write_quorum = (replica_count as u32) / 2 + 1; // Note that quorum is not changing here during graceful restart.
            let new_config = mssf_core::types::ReplicaSetConfig {
                replicas: new_replicas,
                write_quorum,
            };
            primary
                .replicator
                .update_current_replica_set_configuration(new_config.clone())?;
            self.partition_state.current_configuration = new_config;
        }

        let prev_state = self
            .partition_state
            .replica_states
            .remove(&replica_id)
            .unwrap();
        let factory_index = prev_state.factory_index;
        // Close the Secondary, and cleanup. No change_role(None) since this is a restart (data is preserved).
        {
            let cancellation_token = mssf_core::sync::SimpleCancelToken::new_boxed();
            prev_state
                .replicator
                .close(cancellation_token.clone())
                .await?;
            prev_state.replica.close(cancellation_token).await?;
            drop(prev_state);
        }

        // Create replica existing from the same factory.
        let factory = &*self.service_factory[factory_index as usize];
        let replica = factory
            .create_replica(
                self.partition_state
                    .static_info
                    .as_ref()
                    .unwrap()
                    .service_type_name
                    .clone(),
                self.partition_state
                    .static_info
                    .as_ref()
                    .unwrap()
                    .service_name
                    .clone(),
                &self.partition_state.static_info.as_ref().unwrap().init_data,
                self.partition_state
                    .static_info
                    .as_ref()
                    .unwrap()
                    .partition_id,
                replica_id,
            )
            .inspect_err(|e| {
                tracing::error!("Failed to create stateful service replica: {:?}", e)
            })?;
        // open the replica
        let partition = StatefulServicePartitionMock::new(ServicePartitionInformation::Singleton(
            mssf_core::types::SingletonPartitionInformation {
                id: self
                    .partition_state
                    .static_info
                    .as_ref()
                    .unwrap()
                    .partition_id,
            },
        ));
        // open existing replicator
        let replctr = replica
            .open(
                mssf_core::types::OpenMode::Existing,
                Arc::new(partition.clone()),
                SimpleCancelToken::new_boxed(),
            )
            .await
            .inspect_err(|e| tracing::error!("Fail to open replica {}", e))?;
        // open the replicator
        let replctr_addr = replctr.open(SimpleCancelToken::new_boxed()).await?;
        // change role to idle secondary
        replctr
            .change_role(
                self.partition_state.epoch.clone(),
                mssf_core::types::ReplicaRole::IdleSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;
        let replica_addr = replica
            .change_role(
                mssf_core::types::ReplicaRole::IdleSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;

        // build the replica again using the same id.
        let primary = self.get_primary_state().unwrap();

        let replica_info = mssf_core::types::ReplicaInformation {
            replicator_address: replctr_addr.clone(),
            id: replica_id,
            role: mssf_core::types::ReplicaRole::IdleSecondary,
            status: mssf_core::types::ReplicaStatus::Up,
            current_progress: -1, // Observed value for restart.
            catch_up_capability: -1,
            must_catch_up: false,
        };
        primary
            .replicator
            .build_replica(replica_info.clone(), SimpleCancelToken::new_boxed())
            .await?;

        // change role to active secondary after successful build.
        // Replicator change_role is called before Replica change_role.
        replctr
            .change_role(
                self.partition_state.epoch.clone(),
                mssf_core::types::ReplicaRole::ActiveSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;
        replica
            .change_role(
                mssf_core::types::ReplicaRole::ActiveSecondary,
                SimpleCancelToken::new_boxed(),
            )
            .await?;
        // update the replica info
        let mut updated_replica_info = replica_info.clone();
        updated_replica_info.role = mssf_core::types::ReplicaRole::ActiveSecondary;
        // update catch up config again.
        let prev_config = self.partition_state.current_configuration.clone();
        let mut new_config_replicas = prev_config.replicas.clone();
        new_config_replicas.push(updated_replica_info.clone());

        let total_replica_count = new_config_replicas.len() + 1; // including primary
        let write_quorum = (total_replica_count as u32) / 2 + 1;
        let new_config = mssf_core::types::ReplicaSetConfig {
            replicas: new_config_replicas,
            write_quorum,
        };
        primary
            .replicator
            .update_catch_up_replica_set_configuration(new_config.clone(), prev_config)?;
        // wait for catch up
        primary
            .replicator
            .wait_for_catch_up_quorum(
                mssf_core::types::ReplicaSetQuorumMode::Write,
                SimpleCancelToken::new_boxed(),
            )
            .await?;
        // update current configuration again.
        primary
            .replicator
            .update_current_replica_set_configuration(new_config.clone())?;
        self.partition_state.current_configuration = new_config;
        // save the state
        let state = StatefulServiceReplicaState {
            replica,
            replicator: replctr,
            _replica_address: replica_addr,
            _replicator_address: replctr_addr,
            partition,
            factory_index,
        };
        let prev = self
            .partition_state
            .replica_states
            .insert(replica_id, state);
        assert!(prev.is_none(), "Service replica already exists");
        // done.
        self.check_partition_state();
        Ok(())
    }
}