@@ -170,9 +170,17 @@ std::optional<Replica::LastMasterSyncData> Replica::Stop() {
170
170
sync_fb_.JoinIfNeeded ();
171
171
DVLOG (1 ) << " MainReplicationFb stopped " << this ;
172
172
acks_fb_.JoinIfNeeded ();
173
- for (auto & flow : shard_flows_) {
174
- flow.reset ();
175
- }
173
+
174
+ proactor_->Await ([this ]() {
175
+ // Destructor is blocking, so other fibers can observe partial state
176
+ // of flows during clean up. To avoid this, we move them and clear the
177
+ // member before the preemption point
178
+ auto shard_flows = std::move (shard_flows_);
179
+ shard_flows_.clear ();
180
+ for (auto & flow : shard_flows) {
181
+ flow.reset ();
182
+ }
183
+ });
176
184
177
185
if (last_journal_LSNs_.has_value ()) {
178
186
return LastMasterSyncData{master_context_.master_repl_id , last_journal_LSNs_.value ()};
@@ -501,29 +509,45 @@ error_code Replica::InitiatePSync() {
501
509
return error_code{};
502
510
}
503
511
504
- // Initialize and start sub-replica for each flow.
505
- error_code Replica::InitiateDflySync (std::optional<LastMasterSyncData> last_master_sync_data) {
506
- auto start_time = absl::Now ( );
507
-
508
- // Initialize MultiShardExecution.
509
- multi_shard_exe_. reset ( new MultiShardExecution () );
512
+ void Replica::InitializeShardFlows () {
513
+ decltype (shard_flows_) shard_flows_copy;
514
+ shard_flows_copy. resize (master_context_. num_flows );
515
+ DCHECK (!shard_flows_copy. empty ());
516
+ thread_flow_map_ = Partition (shard_flows_copy. size ());
517
+ const size_t pool_sz = shard_set-> pool ()-> size ( );
510
518
511
- // Initialize shard flows.
512
- shard_flows_.resize (master_context_.num_flows );
513
- DCHECK (!shard_flows_.empty ());
514
- for (unsigned i = 0 ; i < shard_flows_.size (); ++i) {
515
- // Transfer LSN state for partial sync
519
+ for (size_t i = 0 ; i < shard_flows_copy.size (); ++i) {
516
520
uint64_t partial_sync_lsn = 0 ;
517
- if (shard_flows_[i]) {
521
+ if (!shard_flows_. empty () && shard_flows_[i]) {
518
522
partial_sync_lsn = shard_flows_[i]->JournalExecutedCount ();
519
523
}
520
- shard_flows_ [i].reset (
524
+ shard_flows_copy [i].reset (
521
525
new DflyShardReplica (server (), master_context_, i, &service_, multi_shard_exe_));
522
526
if (partial_sync_lsn > 0 ) {
523
- shard_flows_ [i]->SetRecordsExecuted (partial_sync_lsn);
527
+ shard_flows_copy [i]->SetRecordsExecuted (partial_sync_lsn);
524
528
}
525
529
}
526
- thread_flow_map_ = Partition (shard_flows_.size ());
530
+
531
+ shard_set->pool ()->AwaitFiberOnAll ([pool_sz, this , &shard_flows_copy](auto index, auto * ctx) {
532
+ for (unsigned i = index; i < shard_flows_copy.size (); i += pool_sz) {
533
+ shard_flows_copy[i]->SetSocketThread (ProactorBase::me ());
534
+ }
535
+ });
536
+ // now update shard_flows on proactor thread
537
+ shard_flows_ = std::move (shard_flows_copy);
538
+ }
539
+
540
+ // Initialize and start sub-replica for each flow.
541
+ error_code Replica::InitiateDflySync (std::optional<LastMasterSyncData> last_master_sync_data) {
542
+ auto start_time = absl::Now ();
543
+
544
+ // Initialize MultiShardExecution.
545
+ multi_shard_exe_.reset (new MultiShardExecution ());
546
+
547
+ // Initialize shard flows. The update to the shard_flows_ should be done by this thread.
548
+ // Otherwise, there is a race condition between GetSummary() and the shard_flows_[i].reset()
549
+ // below.
550
+ InitializeShardFlows ();
527
551
528
552
// Blocked on until all flows got full sync cut.
529
553
BlockingCounter sync_block{unsigned (shard_flows_.size ())};
@@ -1210,11 +1234,12 @@ error_code Replica::ParseReplicationHeader(base::IoBuf* io_buf, PSyncResponse* d
1210
1234
1211
1235
auto Replica::GetSummary () const -> Summary {
1212
1236
auto f = [this ]() {
1237
+ DCHECK (this );
1213
1238
auto last_io_time = LastIoTime ();
1214
1239
1215
- // Note: we access LastIoTime from foreigh thread in unsafe manner. However, specifically here
1216
- // it's unlikely to cause a real bug.
1217
- for ( const auto & flow : shard_flows_) { // Get last io time from all sub flows.
1240
+ for ( const auto & flow : shard_flows_) {
1241
+ DCHECK ( Proactor () == ProactorBase::me ());
1242
+ DCHECK ( flow);
1218
1243
last_io_time = std::max (last_io_time, flow->LastIoTime ());
1219
1244
}
1220
1245
@@ -1246,25 +1271,14 @@ auto Replica::GetSummary() const -> Summary {
1246
1271
return res;
1247
1272
};
1248
1273
1249
- if (Sock ())
1250
- return Proactor ()->AwaitBrief (f);
1251
-
1252
- /* *
1253
- * when this branch happens: there is a very short grace period
1254
- * where Sock() is not initialized, yet the server can
1255
- * receive ROLE/INFO commands. That period happens when launching
1256
- * an instance with '--replicaof' and then immediately
1257
- * sending a command.
1258
- *
1259
- * In that instance, we have to run f() on the current fiber.
1260
- */
1261
- return f ();
1274
+ return Proactor ()->AwaitBrief (f);
1262
1275
}
1263
1276
1264
1277
std::vector<uint64_t > Replica::GetReplicaOffset () const {
1265
1278
std::vector<uint64_t > flow_rec_count;
1266
1279
flow_rec_count.resize (shard_flows_.size ());
1267
1280
for (const auto & flow : shard_flows_) {
1281
+ DCHECK (flow.get ());
1268
1282
uint32_t flow_id = flow->FlowId ();
1269
1283
uint64_t rec_count = flow->JournalExecutedCount ();
1270
1284
DCHECK_LT (flow_id, shard_flows_.size ());
0 commit comments