1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/sync_driver/data_type_manager_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/debug/trace_event.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/stringprintf.h"
18 #include "components/sync_driver/data_type_controller.h"
19 #include "components/sync_driver/data_type_encryption_handler.h"
20 #include "components/sync_driver/data_type_manager_observer.h"
21 #include "components/sync_driver/data_type_status_table.h"
22 #include "sync/internal_api/public/data_type_debug_info_listener.h"
24 namespace sync_driver {
28 DataTypeStatusTable::TypeErrorMap
29 GenerateCryptoErrorsForTypes(syncer::ModelTypeSet encrypted_types) {
30 DataTypeStatusTable::TypeErrorMap crypto_errors;
31 for (syncer::ModelTypeSet::Iterator iter = encrypted_types.First();
32 iter.Good(); iter.Inc()) {
33 crypto_errors[iter.Get()] = syncer::SyncError(
35 syncer::SyncError::CRYPTO_ERROR,
44 DataTypeManagerImpl::AssociationTypesInfo::AssociationTypesInfo() {}
45 DataTypeManagerImpl::AssociationTypesInfo::~AssociationTypesInfo() {}
47 DataTypeManagerImpl::DataTypeManagerImpl(
48 const base::Closure& unrecoverable_error_method,
49 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
51 const DataTypeController::TypeMap* controllers,
52 const DataTypeEncryptionHandler* encryption_handler,
53 BackendDataTypeConfigurer* configurer,
54 DataTypeManagerObserver* observer)
55 : configurer_(configurer),
56 controllers_(controllers),
57 state_(DataTypeManager::STOPPED),
58 needs_reconfigure_(false),
59 last_configure_reason_(syncer::CONFIGURE_REASON_UNKNOWN),
60 debug_info_listener_(debug_info_listener),
61 model_association_manager_(controllers, this),
63 encryption_handler_(encryption_handler),
64 unrecoverable_error_method_(unrecoverable_error_method),
65 weak_ptr_factory_(this) {
70 DataTypeManagerImpl::~DataTypeManagerImpl() {}
72 void DataTypeManagerImpl::Configure(syncer::ModelTypeSet desired_types,
73 syncer::ConfigureReason reason) {
74 if (reason == syncer::CONFIGURE_REASON_BACKUP_ROLLBACK)
75 desired_types.PutAll(syncer::ControlTypes());
77 desired_types.PutAll(syncer::CoreTypes());
79 // Only allow control types and types that have controllers.
80 syncer::ModelTypeSet filtered_desired_types;
81 for (syncer::ModelTypeSet::Iterator type = desired_types.First();
82 type.Good(); type.Inc()) {
83 DataTypeController::TypeMap::const_iterator iter =
84 controllers_->find(type.Get());
85 if (syncer::IsControlType(type.Get()) ||
86 iter != controllers_->end()) {
87 if (iter != controllers_->end()) {
88 if (!iter->second->ReadyForStart() &&
89 !data_type_status_table_.GetUnreadyErrorTypes().Has(
91 // Add the type to the unready types set to prevent purging it. It's
92 // up to the datatype controller to, if necessary, explicitly
93 // mark the type as broken to trigger a purge.
94 syncer::SyncError error(FROM_HERE,
95 syncer::SyncError::UNREADY_ERROR,
96 "Datatype not ready at config time.",
98 std::map<syncer::ModelType, syncer::SyncError> errors;
99 errors[type.Get()] = error;
100 data_type_status_table_.UpdateFailedDataTypes(errors);
101 } else if (iter->second->ReadyForStart()) {
102 data_type_status_table_.ResetUnreadyErrorFor(type.Get());
105 filtered_desired_types.Put(type.Get());
108 ConfigureImpl(filtered_desired_types, reason);
111 void DataTypeManagerImpl::ReenableType(syncer::ModelType type) {
112 // TODO(zea): move the "should we reconfigure" logic into the datatype handler
114 // Only reconfigure if the type actually had a data type or unready error.
115 if (!data_type_status_table_.ResetDataTypeErrorFor(type) &&
116 !data_type_status_table_.ResetUnreadyErrorFor(type)) {
120 // Only reconfigure if the type is actually desired.
121 if (!last_requested_types_.Has(type))
124 DVLOG(1) << "Reenabling " << syncer::ModelTypeToString(type);
125 needs_reconfigure_ = true;
126 last_configure_reason_ = syncer::CONFIGURE_REASON_PROGRAMMATIC;
127 ProcessReconfigure();
130 void DataTypeManagerImpl::ResetDataTypeErrors() {
131 data_type_status_table_.Reset();
134 void DataTypeManagerImpl::PurgeForMigration(
135 syncer::ModelTypeSet undesired_types,
136 syncer::ConfigureReason reason) {
137 syncer::ModelTypeSet remainder = Difference(last_requested_types_,
139 ConfigureImpl(remainder, reason);
142 void DataTypeManagerImpl::ConfigureImpl(
143 syncer::ModelTypeSet desired_types,
144 syncer::ConfigureReason reason) {
145 DCHECK_NE(reason, syncer::CONFIGURE_REASON_UNKNOWN);
146 DVLOG(1) << "Configuring for " << syncer::ModelTypeSetToString(desired_types)
147 << " with reason " << reason;
148 if (state_ == STOPPING) {
149 // You can not set a configuration while stopping.
150 LOG(ERROR) << "Configuration set while stopping.";
154 // TODO(zea): consider not performing a full configuration once there's a
155 // reliable way to determine if the requested set of enabled types matches the
158 last_requested_types_ = desired_types;
159 last_configure_reason_ = reason;
160 // Only proceed if we're in a steady state or retrying.
161 if (state_ != STOPPED && state_ != CONFIGURED && state_ != RETRYING) {
162 DVLOG(1) << "Received configure request while configuration in flight. "
163 << "Postponing until current configuration complete.";
164 needs_reconfigure_ = true;
171 BackendDataTypeConfigurer::DataTypeConfigStateMap
172 DataTypeManagerImpl::BuildDataTypeConfigStateMap(
173 const syncer::ModelTypeSet& types_being_configured) const {
174 // 1. Get the failed types (due to fatal, crypto, and unready errors).
175 // 2. Add the difference between last_requested_types_ and the failed types
176 // as CONFIGURE_INACTIVE.
177 // 3. Flip |types_being_configured| to CONFIGURE_ACTIVE.
178 // 4. Set non-enabled user types as DISABLED.
179 // 5. Set the fatal, crypto, and unready types to their respective states.
180 syncer::ModelTypeSet error_types =
181 data_type_status_table_.GetFailedTypes();
182 syncer::ModelTypeSet fatal_types =
183 data_type_status_table_.GetFatalErrorTypes();
184 syncer::ModelTypeSet crypto_types =
185 data_type_status_table_.GetCryptoErrorTypes();
186 syncer::ModelTypeSet unready_types=
187 data_type_status_table_.GetUnreadyErrorTypes();
189 // Types with persistence errors are only purged/resynced when they're
190 // actively being configured.
191 syncer::ModelTypeSet persistence_types =
192 data_type_status_table_.GetPersistenceErrorTypes();
193 persistence_types.RetainAll(types_being_configured);
195 // Types with unready errors do not count as unready if they've been disabled.
196 unready_types.RetainAll(last_requested_types_);
198 syncer::ModelTypeSet enabled_types = last_requested_types_;
199 enabled_types.RemoveAll(error_types);
200 syncer::ModelTypeSet disabled_types =
202 syncer::Union(syncer::UserTypes(), syncer::ControlTypes()),
204 syncer::ModelTypeSet to_configure = syncer::Intersection(
205 enabled_types, types_being_configured);
206 DVLOG(1) << "Enabling: " << syncer::ModelTypeSetToString(enabled_types);
207 DVLOG(1) << "Configuring: " << syncer::ModelTypeSetToString(to_configure);
208 DVLOG(1) << "Disabling: " << syncer::ModelTypeSetToString(disabled_types);
210 BackendDataTypeConfigurer::DataTypeConfigStateMap config_state_map;
211 BackendDataTypeConfigurer::SetDataTypesState(
212 BackendDataTypeConfigurer::CONFIGURE_INACTIVE, enabled_types,
214 BackendDataTypeConfigurer::SetDataTypesState(
215 BackendDataTypeConfigurer::CONFIGURE_ACTIVE, to_configure,
217 BackendDataTypeConfigurer::SetDataTypesState(
218 BackendDataTypeConfigurer::CONFIGURE_CLEAN, persistence_types,
220 BackendDataTypeConfigurer::SetDataTypesState(
221 BackendDataTypeConfigurer::DISABLED, disabled_types,
223 BackendDataTypeConfigurer::SetDataTypesState(
224 BackendDataTypeConfigurer::FATAL, fatal_types,
226 BackendDataTypeConfigurer::SetDataTypesState(
227 BackendDataTypeConfigurer::CRYPTO, crypto_types,
229 BackendDataTypeConfigurer::SetDataTypesState(
230 BackendDataTypeConfigurer::UNREADY, unready_types,
232 return config_state_map;
235 void DataTypeManagerImpl::Restart(syncer::ConfigureReason reason) {
236 DVLOG(1) << "Restarting...";
238 // Check for new or resolved data type crypto errors.
239 if (encryption_handler_->IsPassphraseRequired()) {
240 syncer::ModelTypeSet encrypted_types =
241 encryption_handler_->GetEncryptedDataTypes();
242 encrypted_types.RetainAll(last_requested_types_);
243 encrypted_types.RemoveAll(
244 data_type_status_table_.GetCryptoErrorTypes());
245 DataTypeStatusTable::TypeErrorMap crypto_errors =
246 GenerateCryptoErrorsForTypes(encrypted_types);
247 data_type_status_table_.UpdateFailedDataTypes(crypto_errors);
249 data_type_status_table_.ResetCryptoErrors();
252 syncer::ModelTypeSet failed_types =
253 data_type_status_table_.GetFailedTypes();
254 syncer::ModelTypeSet enabled_types =
255 syncer::Difference(last_requested_types_, failed_types);
257 last_restart_time_ = base::Time::Now();
258 configuration_stats_.clear();
260 DCHECK(state_ == STOPPED || state_ == CONFIGURED || state_ == RETRYING);
262 // Starting from a "steady state" (stopped or configured) state
263 // should send a start notification.
264 if (state_ == STOPPED || state_ == CONFIGURED)
267 state_ = DOWNLOAD_PENDING;
268 download_types_queue_ = PrioritizeTypes(enabled_types);
269 association_types_queue_ = std::queue<AssociationTypesInfo>();
271 model_association_manager_.Initialize(enabled_types);
273 // Tell the backend about the new set of data types we wish to sync.
274 // The task will be invoked when updates are downloaded.
275 configurer_->ConfigureDataTypes(
277 BuildDataTypeConfigStateMap(download_types_queue_.front()),
278 base::Bind(&DataTypeManagerImpl::DownloadReady,
279 weak_ptr_factory_.GetWeakPtr(),
281 download_types_queue_.front(),
282 syncer::ModelTypeSet()),
283 base::Bind(&DataTypeManagerImpl::OnDownloadRetry,
284 weak_ptr_factory_.GetWeakPtr()));
287 syncer::ModelTypeSet DataTypeManagerImpl::GetPriorityTypes() const {
288 syncer::ModelTypeSet high_priority_types;
289 high_priority_types.PutAll(syncer::PriorityCoreTypes());
290 high_priority_types.PutAll(syncer::PriorityUserTypes());
291 return high_priority_types;
294 TypeSetPriorityList DataTypeManagerImpl::PrioritizeTypes(
295 const syncer::ModelTypeSet& types) {
296 syncer::ModelTypeSet high_priority_types = GetPriorityTypes();
297 high_priority_types.RetainAll(types);
299 syncer::ModelTypeSet low_priority_types =
300 syncer::Difference(types, high_priority_types);
302 TypeSetPriorityList result;
303 if (!high_priority_types.Empty())
304 result.push(high_priority_types);
305 if (!low_priority_types.Empty())
306 result.push(low_priority_types);
308 // Could be empty in case of purging for migration, sync nothing, etc.
309 // Configure empty set to purge data from backend.
311 result.push(syncer::ModelTypeSet());
316 void DataTypeManagerImpl::ProcessReconfigure() {
317 DCHECK(needs_reconfigure_);
319 // Wait for current download and association to finish.
320 if (!(download_types_queue_.empty() && association_types_queue_.empty()))
323 // An attempt was made to reconfigure while we were already configuring.
324 // This can be because a passphrase was accepted or the user changed the
325 // set of desired types. Either way, |last_requested_types_| will contain
326 // the most recent set of desired types, so we just call configure.
327 // Note: we do this whether or not GetControllersNeedingStart is true,
328 // because we may need to stop datatypes.
329 DVLOG(1) << "Reconfiguring due to previous configure attempt occuring while"
332 // Note: ConfigureImpl is called directly, rather than posted, in order to
333 // ensure that any purging/unapplying/journaling happens while the set of
334 // failed types is still up to date. If stack unwinding were to be done
335 // via PostTask, the failed data types may be reset before the purging was
338 needs_reconfigure_ = false;
339 ConfigureImpl(last_requested_types_, last_configure_reason_);
342 void DataTypeManagerImpl::OnDownloadRetry() {
343 DCHECK(state_ == DOWNLOAD_PENDING || state_ == CONFIGURING);
344 observer_->OnConfigureRetry();
347 void DataTypeManagerImpl::DownloadReady(
348 base::Time download_start_time,
349 syncer::ModelTypeSet types_to_download,
350 syncer::ModelTypeSet high_priority_types_before,
351 syncer::ModelTypeSet first_sync_types,
352 syncer::ModelTypeSet failed_configuration_types) {
353 DCHECK(state_ == DOWNLOAD_PENDING || state_ == CONFIGURING);
355 // Persistence errors are reset after each backend configuration attempt
356 // during which they would have been purged.
357 data_type_status_table_.ResetPersistenceErrorsFrom(types_to_download);
359 // Ignore |failed_configuration_types| if we need to reconfigure
361 if (needs_reconfigure_) {
362 download_types_queue_ = TypeSetPriorityList();
363 ProcessReconfigure();
367 if (!failed_configuration_types.Empty()) {
368 if (!unrecoverable_error_method_.is_null())
369 unrecoverable_error_method_.Run();
370 DataTypeStatusTable::TypeErrorMap errors;
371 for (syncer::ModelTypeSet::Iterator iter =
372 failed_configuration_types.First(); iter.Good(); iter.Inc()) {
373 syncer::SyncError error(
375 syncer::SyncError::UNRECOVERABLE_ERROR,
376 "Backend failed to download type.",
378 errors[iter.Get()] = error;
380 data_type_status_table_.UpdateFailedDataTypes(errors);
381 Abort(UNRECOVERABLE_ERROR);
385 state_ = CONFIGURING;
387 // Pop and associate download-ready types.
388 syncer::ModelTypeSet ready_types = types_to_download;
389 download_types_queue_.pop();
390 syncer::ModelTypeSet new_types_to_download;
391 if (!download_types_queue_.empty())
392 new_types_to_download = download_types_queue_.front();
394 AssociationTypesInfo association_info;
395 association_info.types = ready_types;
396 association_info.first_sync_types = first_sync_types;
397 association_info.download_start_time = download_start_time;
398 association_info.download_ready_time = base::Time::Now();
399 association_info.high_priority_types_before = high_priority_types_before;
400 association_types_queue_.push(association_info);
401 if (association_types_queue_.size() == 1u)
402 StartNextAssociation();
404 // Download types of low priority while configuring types of high priority.
405 if (!new_types_to_download.Empty()) {
406 configurer_->ConfigureDataTypes(
407 last_configure_reason_,
408 BuildDataTypeConfigStateMap(new_types_to_download),
409 base::Bind(&DataTypeManagerImpl::DownloadReady,
410 weak_ptr_factory_.GetWeakPtr(),
412 new_types_to_download,
413 syncer::Union(ready_types, high_priority_types_before)),
414 base::Bind(&DataTypeManagerImpl::OnDownloadRetry,
415 weak_ptr_factory_.GetWeakPtr()));
419 void DataTypeManagerImpl::StartNextAssociation() {
420 CHECK(!association_types_queue_.empty());
422 association_types_queue_.front().association_request_time =
424 model_association_manager_.StartAssociationAsync(
425 association_types_queue_.front().types);
428 void DataTypeManagerImpl::OnSingleDataTypeWillStop(
429 syncer::ModelType type,
430 const syncer::SyncError& error) {
431 configurer_->DeactivateDataType(type);
433 DataTypeStatusTable::TypeErrorMap failed_types;
434 failed_types[type] = error;
435 data_type_status_table_.UpdateFailedDataTypes(
438 // Unrecoverable errors will shut down the entire backend, so no need to
440 if (error.error_type() != syncer::SyncError::UNRECOVERABLE_ERROR) {
441 needs_reconfigure_ = true;
442 last_configure_reason_ = syncer::CONFIGURE_REASON_PROGRAMMATIC;
443 ProcessReconfigure();
448 void DataTypeManagerImpl::OnSingleDataTypeAssociationDone(
449 syncer::ModelType type,
450 const syncer::DataTypeAssociationStats& association_stats) {
451 DCHECK(!association_types_queue_.empty());
452 DataTypeController::TypeMap::const_iterator c_it = controllers_->find(type);
453 DCHECK(c_it != controllers_->end());
454 if (c_it->second->state() == DataTypeController::RUNNING) {
455 // Tell the backend about the change processor for this type so it can
456 // begin routing changes to it.
457 configurer_->ActivateDataType(type, c_it->second->model_safe_group(),
458 c_it->second->GetChangeProcessor());
461 if (!debug_info_listener_.IsInitialized())
464 AssociationTypesInfo& info = association_types_queue_.front();
465 configuration_stats_.push_back(syncer::DataTypeConfigurationStats());
466 configuration_stats_.back().model_type = type;
467 configuration_stats_.back().association_stats = association_stats;
468 if (info.types.Has(type)) {
469 // Times in |info| only apply to non-slow types.
470 configuration_stats_.back().download_wait_time =
471 info.download_start_time - last_restart_time_;
472 if (info.first_sync_types.Has(type)) {
473 configuration_stats_.back().download_time =
474 info.download_ready_time - info.download_start_time;
476 configuration_stats_.back().association_wait_time_for_high_priority =
477 info.association_request_time - info.download_ready_time;
478 configuration_stats_.back().high_priority_types_configured_before =
479 info.high_priority_types_before;
480 configuration_stats_.back().same_priority_types_configured_before =
481 info.configured_types;
482 info.configured_types.Put(type);
486 void DataTypeManagerImpl::OnModelAssociationDone(
487 const DataTypeManager::ConfigureResult& result) {
488 DCHECK(state_ == STOPPING || state_ == CONFIGURING);
490 if (state_ == STOPPING)
493 // Ignore abort/unrecoverable error if we need to reconfigure anyways.
494 if (needs_reconfigure_) {
495 association_types_queue_ = std::queue<AssociationTypesInfo>();
496 ProcessReconfigure();
500 if (result.status == ABORTED || result.status == UNRECOVERABLE_ERROR) {
501 Abort(result.status);
505 DCHECK(result.status == OK);
507 association_types_queue_.pop();
508 if (!association_types_queue_.empty()) {
509 StartNextAssociation();
510 } else if (download_types_queue_.empty()) {
516 void DataTypeManagerImpl::Stop() {
517 if (state_ == STOPPED)
520 bool need_to_notify =
521 state_ == DOWNLOAD_PENDING || state_ == CONFIGURING;
524 if (need_to_notify) {
525 ConfigureResult result(ABORTED,
526 last_requested_types_);
531 void DataTypeManagerImpl::Abort(ConfigureStatus status) {
532 DCHECK(state_ == DOWNLOAD_PENDING || state_ == CONFIGURING);
536 DCHECK_NE(OK, status);
537 ConfigureResult result(status,
538 last_requested_types_);
542 void DataTypeManagerImpl::StopImpl() {
545 // Invalidate weak pointer to drop download callbacks.
546 weak_ptr_factory_.InvalidateWeakPtrs();
548 // Stop all data types. This may trigger association callback but the
549 // callback will do nothing because state is set to STOPPING above.
550 model_association_manager_.Stop();
555 void DataTypeManagerImpl::NotifyStart() {
556 observer_->OnConfigureStart();
559 void DataTypeManagerImpl::NotifyDone(const ConfigureResult& raw_result) {
560 AddToConfigureTime();
562 ConfigureResult result = raw_result;
563 result.data_type_status_table = data_type_status_table_;
565 DVLOG(1) << "Total time spent configuring: "
566 << configure_time_delta_.InSecondsF() << "s";
567 switch (result.status) {
568 case DataTypeManager::OK:
569 DVLOG(1) << "NotifyDone called with result: OK";
570 UMA_HISTOGRAM_LONG_TIMES("Sync.ConfigureTime_Long.OK",
571 configure_time_delta_);
572 if (debug_info_listener_.IsInitialized() &&
573 !configuration_stats_.empty()) {
574 debug_info_listener_.Call(
576 &syncer::DataTypeDebugInfoListener::OnDataTypeConfigureComplete,
577 configuration_stats_);
579 configuration_stats_.clear();
581 case DataTypeManager::ABORTED:
582 DVLOG(1) << "NotifyDone called with result: ABORTED";
583 UMA_HISTOGRAM_LONG_TIMES("Sync.ConfigureTime_Long.ABORTED",
584 configure_time_delta_);
586 case DataTypeManager::UNRECOVERABLE_ERROR:
587 DVLOG(1) << "NotifyDone called with result: UNRECOVERABLE_ERROR";
588 UMA_HISTOGRAM_LONG_TIMES("Sync.ConfigureTime_Long.UNRECOVERABLE_ERROR",
589 configure_time_delta_);
591 case DataTypeManager::UNKNOWN:
595 observer_->OnConfigureDone(result);
598 DataTypeManager::State DataTypeManagerImpl::state() const {
602 void DataTypeManagerImpl::AddToConfigureTime() {
603 DCHECK(!last_restart_time_.is_null());
604 configure_time_delta_ += (base::Time::Now() - last_restart_time_);
607 } // namespace sync_driver