@@ -37,7 +37,6 @@ FDv2DataSystem::FDv2DataSystem(
3737 : logger_(logger),
3838 ioc_ (std::move(ioc)),
3939 initializer_factories_(std::move(initializer_factories)),
40- synchronizer_factories_(std::move(synchronizer_factories)),
4140 fallback_condition_factory_(std::move(fallback_condition_factory)),
4241 recovery_condition_factory_(std::move(recovery_condition_factory)),
4342 status_manager_(status_manager),
@@ -47,7 +46,7 @@ FDv2DataSystem::FDv2DataSystem(
4746 closed_(false ),
4847 selector_(),
4948 initializer_index_(0 ),
50- synchronizer_index_( 0 ),
49+ source_manager_(std::move(synchronizer_factories) ),
5150 active_initializer_(nullptr ),
5251 active_synchronizer_(nullptr ),
5352 active_conditions_(nullptr ) {}
@@ -101,7 +100,8 @@ void FDv2DataSystem::Initialize() {
101100 assert (!already_called && " Initialize() must be called at most once" );
102101
103102 LD_LOG (logger_, LogLevel::kInfo ) << Identity () << " : starting" ;
104- if (initializer_factories_.empty () && synchronizer_factories_.empty ()) {
103+ if (initializer_factories_.empty () &&
104+ source_manager_.SynchronizerCount () == 0 ) {
105105 // Offline mode: empty store is the canonical state.
106106 status_manager_->SetState (DataSourceStatus::DataSourceState::kValid );
107107 return ;
@@ -196,30 +196,27 @@ void FDv2DataSystem::OnInitializerResult(
196196
197197void FDv2DataSystem::StartSynchronizers () {
198198 bool exhausted = false ;
199- bool cycled_synchronizers = false ;
199+ // True if at least one synchronizer factory was configured at construction.
200+ bool any_synchronizers_configured = false ;
200201 {
201202 std::lock_guard<std::mutex> lock (mutex_);
202203 if (closed_) {
203204 return ;
204205 }
205- if (synchronizer_index_ >= synchronizer_factories_. size ()) {
206- exhausted = true ;
207- cycled_synchronizers = synchronizer_index_ > 0 ;
206+ active_synchronizer_ = source_manager_. NextSynchronizer ();
207+ if (active_synchronizer_) {
208+ active_conditions_ = BuildActiveConditions () ;
208209 } else {
209- auto & factory = synchronizer_factories_[synchronizer_index_];
210- active_synchronizer_ = factory->Build ();
211- active_conditions_ =
212- BuildConditionsForSynchronizer (synchronizer_index_);
213- ++synchronizer_index_;
210+ exhausted = true ;
211+ any_synchronizers_configured =
212+ source_manager_.SynchronizerCount () > 0 ;
214213 }
215214 }
216215
217216 if (exhausted) {
218- // kOff when we can't continue updating; init-only with data stays
219- // kValid.
220- if (cycled_synchronizers || !store_.Initialized ()) {
217+ if (any_synchronizers_configured || !store_.Initialized ()) {
221218 std::string const message =
222- cycled_synchronizers
219+ any_synchronizers_configured
223220 ? " all data source acquisition methods have been exhausted"
224221 : " all initializers exhausted and no synchronizers "
225222 " configured" ;
@@ -274,7 +271,7 @@ void FDv2DataSystem::OnConditionFired(
274271 if (type == Type::kRecovery ) {
275272 LD_LOG (logger_, LogLevel::kInfo )
276273 << Identity () << " : recovery condition met" ;
277- synchronizer_index_ = 0 ;
274+ source_manager_. ResetSourceIndex () ;
278275 } else {
279276 LD_LOG (logger_, LogLevel::kInfo )
280277 << Identity () << " : fallback condition met" ;
@@ -283,16 +280,18 @@ void FDv2DataSystem::OnConditionFired(
283280 StartSynchronizers ();
284281}
285282
286- std::unique_ptr<Conditions> FDv2DataSystem::BuildConditionsForSynchronizer (
287- std::size_t synchronizer_position) const {
283+ std::unique_ptr<Conditions> FDv2DataSystem::BuildActiveConditions () const {
288284 std::vector<std::unique_ptr<data_interfaces::IFDv2Condition>> conditions;
289- if (synchronizer_factories_.size () <= 1 ) {
285+ // With only one synchronizer available there's nothing to fall back to
286+ // or recover from, so leave the conditions empty.
287+ if (source_manager_.AvailableSynchronizerCount () == 1 ) {
290288 return std::make_unique<Conditions>(std::move (conditions));
291289 }
292290 if (fallback_condition_factory_) {
293291 conditions.push_back (fallback_condition_factory_->Build ());
294292 }
295- if (synchronizer_position > 0 && recovery_condition_factory_) {
293+ // The prime synchronizer has nothing more-preferred to recover to.
294+ if (!source_manager_.IsPrimeSynchronizer () && recovery_condition_factory_) {
296295 conditions.push_back (recovery_condition_factory_->Build ());
297296 }
298297 return std::make_unique<Conditions>(std::move (conditions));
@@ -356,6 +355,7 @@ void FDv2DataSystem::OnSynchronizerResult(
356355 return ;
357356 }
358357 if (advance) {
358+ source_manager_.BlockCurrentSynchronizer ();
359359 active_synchronizer_.reset ();
360360 active_conditions_.reset ();
361361 }
0 commit comments