@@ -1234,10 +1234,7 @@ void ServerFamily::Shutdown() {
12341234 DebugCmd::Shutdown ();
12351235 });
12361236
1237- service_.proactor_pool ().AwaitFiberOnAll ([](auto index, auto * cntx) {
1238- tl_replica = nullptr ;
1239- tl_cluster_replicas.clear ();
1240- });
1237+ UpdateReplicationThreadLocals (nullptr );
12411238}
12421239
12431240bool ServerFamily::HasPrivilegedInterface () {
@@ -3457,10 +3454,9 @@ void ServerFamily::ReplicaOfNoOne(SinkReplyBuilder* builder) {
34573454 SetMasterFlagOnAllThreads (true );
34583455 // TODO we should not allow partial sync after NO-ONE. Only after Takeover.
34593456 last_master_data_ = replica_->Stop ();
3460- // TODO set thread locals to nullptr
34613457 replica_.reset ();
34623458 StopAllClusterReplicas ();
3463- service_. proactor_pool (). AwaitFiberOnAll ([]( auto index, auto * cntx) { tl_replica = nullptr ; } );
3459+ UpdateReplicationThreadLocals ( nullptr );
34643460 }
34653461
34663462 // May not switch to ACTIVE if the process is, for example, shutting down at the same time.
@@ -3523,6 +3519,11 @@ void ServerFamily::ReplicaOfInternal(CmdArgList args, Transaction* tx, SinkReply
35233519 return builder->SendError (ec ? ec.Format () : " replication cancelled" );
35243520 }
35253521
3522+ // Critical section.
3523+ // 1. Stop the old replica_ if it exists
3524+ // 2. Update all the pointers to the new replica and update master flag
3525+ // 3. Start the main replication fiber
3526+ // 4. Send OK
35263527 util::fb2::LockGuard lk (replicaof_mu_);
35273528 if (replica_)
35283529 last_master_data = replica_->Stop ();
@@ -3534,10 +3535,7 @@ void ServerFamily::ReplicaOfInternal(CmdArgList args, Transaction* tx, SinkReply
35343535
35353536 // Update thread locals. That way INFO never blocks
35363537 replica_ = new_replica;
3537- service_.proactor_pool ().AwaitFiberOnAll ([new_replica](auto index, auto * context) {
3538- tl_replica = new_replica;
3539- tl_cluster_replicas.clear ();
3540- });
3538+ UpdateReplicationThreadLocals (new_replica);
35413539 SetMasterFlagOnAllThreads (false );
35423540
35433541 if (on_err == ActionOnConnectionFail::kReturnOnError ) {
@@ -3622,7 +3620,7 @@ void ServerFamily::ReplTakeOver(CmdArgList args, const CommandContext& cmd_cntx)
36223620 last_master_data_ = replica_->Stop ();
36233621 replica_.reset ();
36243622
3625- service_. proactor_pool (). AwaitFiberOnAll ([]( auto index, auto * context) { tl_replica = nullptr ; } );
3623+ UpdateReplicationThreadLocals ( nullptr );
36263624
36273625 return builder->SendOk ();
36283626}
@@ -3916,6 +3914,13 @@ void ServerFamily::ClientPauseCmd(CmdArgList args, SinkReplyBuilder* builder,
39163914 }
39173915}
39183916
3917+ void ServerFamily::UpdateReplicationThreadLocals (std::shared_ptr<Replica> repl) {
3918+ service_.proactor_pool ().AwaitFiberOnAll ([repl](auto index, auto * context) {
3919+ tl_replica = nullptr ;
3920+ tl_cluster_replicas.clear ();
3921+ });
3922+ }
3923+
39193924#define HFUNC (x ) SetHandler(HandlerFunc(this , &ServerFamily::x))
39203925
39213926namespace acl {
0 commit comments