Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/sync/backend_migrator.cc

Issue 7655055: [Sync] Make BackendMigrator not wait for full sync cycles (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Actually re-enable tests Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/sync/backend_migrator.h" 5 #include "chrome/browser/sync/backend_migrator.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h"
10 #include "base/message_loop.h"
9 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
10 #include "chrome/browser/sync/profile_sync_service.h" 12 #include "chrome/browser/sync/profile_sync_service.h"
11 #include "chrome/browser/sync/engine/configure_reason.h" 13 #include "chrome/browser/sync/engine/configure_reason.h"
12 #include "chrome/browser/sync/glue/data_type_manager.h"
13 #include "chrome/browser/sync/sessions/session_state.h" 14 #include "chrome/browser/sync/sessions/session_state.h"
14 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
15 #include "content/browser/browser_thread.h"
16 #include "content/common/notification_details.h" 16 #include "content/common/notification_details.h"
17 #include "content/common/notification_source.h" 17 #include "content/common/notification_source.h"
18 18
19 using syncable::ModelTypeSet; 19 using syncable::ModelTypeSet;
20 20
21 namespace browser_sync { 21 namespace browser_sync {
22 22
23 using sessions::SyncSessionSnapshot; 23 using sessions::SyncSessionSnapshot;
24 using syncable::ModelTypeToString;
24 25
25 BackendMigrator::BackendMigrator(ProfileSyncService* service, 26 BackendMigrator::BackendMigrator(ProfileSyncService* service,
26 DataTypeManager* manager) 27 DataTypeManager* manager)
27 : state_(IDLE), service_(service), manager_(manager), 28 : state_(IDLE), service_(service), manager_(manager),
28 restart_migration_(false), 29 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
29 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
30 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, 30 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
31 Source<DataTypeManager>(manager_)); 31 Source<DataTypeManager>(manager_));
32 service_->AddObserver(this);
33 } 32 }
34 33
35 BackendMigrator::~BackendMigrator() { 34 BackendMigrator::~BackendMigrator() {
36 service_->RemoveObserver(this);
37 }
38
39 bool BackendMigrator::HasStartedMigrating() const {
40 return state_ >= DISABLING_TYPES;
41 } 35 }
42 36
43 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { 37 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) {
38 const ModelTypeSet old_to_migrate = to_migrate_;
44 { 39 {
40 using std::swap;
45 ModelTypeSet temp; 41 ModelTypeSet temp;
46 std::set_union(to_migrate_.begin(), to_migrate_.end(), 42 std::set_union(to_migrate_.begin(), to_migrate_.end(),
47 types.begin(), types.end(), 43 types.begin(), types.end(),
48 std::inserter(temp, temp.end())); 44 std::inserter(temp, temp.end()));
49 to_migrate_ = temp; 45 swap(temp, to_migrate_);
50 } 46 }
51 47 VLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types)
52 if (HasStartedMigrating()) { 48 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate)
53 VLOG(1) << "BackendMigrator::MigrateTypes: STARTED_MIGRATING early-out."; 49 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_);
54 restart_migration_ = true; 50 if (old_to_migrate == to_migrate_) {
51 VLOG(1) << "MigrateTypes called with no new types; ignoring";
55 return; 52 return;
56 } 53 }
57 54
58 if (manager_->state() != DataTypeManager::CONFIGURED) { 55 if (state_ == IDLE) {
59 VLOG(1) << "BackendMigrator::MigrateTypes: manager CONFIGURED early-out.";
60 state_ = WAITING_TO_START; 56 state_ = WAITING_TO_START;
57 }
58
59 TryStartIfWaiting();
60 // If we're still waiting, we'll pick up where we left off in
61 // OnConfigureDone().
62 if (state_ == WAITING_TO_START) {
63 VLOG(1) << "Manager not configured; waiting";
61 return; 64 return;
62 } 65 }
63 66
67 // If we're already migrating, interrupt the current migration.
68 DCHECK_GT(state_, WAITING_TO_START);
69 RestartMigration();
70 }
71
72 void BackendMigrator::TryStartIfWaiting() {
73 if (state_ == WAITING_TO_START &&
74 manager_->state() == DataTypeManager::CONFIGURED) {
75 RestartMigration();
76 }
77 }
78
79 void BackendMigrator::RestartMigration() {
64 // We'll now disable any running types that need to be migrated. 80 // We'll now disable any running types that need to be migrated.
65 state_ = DISABLING_TYPES; 81 state_ = DISABLING_TYPES;
66 ModelTypeSet full_set; 82 ModelTypeSet full_set;
67 service_->GetPreferredDataTypes(&full_set); 83 service_->GetPreferredDataTypes(&full_set);
68 ModelTypeSet difference; 84 ModelTypeSet difference;
69 std::set_difference(full_set.begin(), full_set.end(), 85 std::set_difference(full_set.begin(), full_set.end(),
70 to_migrate_.begin(), to_migrate_.end(), 86 to_migrate_.begin(), to_migrate_.end(),
71 std::inserter(difference, difference.end())); 87 std::inserter(difference, difference.end()));
72 VLOG(1) << "BackendMigrator disabling types; calling Configure."; 88 bool configure_with_nigori = to_migrate_.count(syncable::NIGORI) == 0;
89 VLOG(1) << "BackendMigrator disabling types "
90 << ModelTypeSetToString(to_migrate_) << "; configuring "
91 << ModelTypeSetToString(difference)
92 << (configure_with_nigori ? " with nigori" : " without nigori");
73 93
74 // Add nigori for config or not based upon if the server told us to migrate 94 // Add nigori for config or not based upon if the server told us to migrate
75 // nigori or not. 95 // nigori or not.
76 if (to_migrate_.count(syncable::NIGORI) == 0) { 96 if (configure_with_nigori) {
77 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); 97 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION);
78 } else { 98 } else {
79 manager_->ConfigureWithoutNigori(difference, 99 manager_->ConfigureWithoutNigori(difference,
80 sync_api::CONFIGURE_REASON_MIGRATION); 100 sync_api::CONFIGURE_REASON_MIGRATION);
81 } 101 }
82 } 102 }
83 103
84 void BackendMigrator::OnStateChanged() {
85 if (restart_migration_ == true) {
86 VLOG(1) << "BackendMigrator restarting migration in OnStateChanged.";
87 state_ = WAITING_TO_START;
88 restart_migration_ = false;
89 MigrateTypes(to_migrate_);
90 return;
91 }
92
93 if (state_ != WAITING_FOR_PURGE)
94 return;
95
96 size_t num_empty_migrated_markers = 0;
97 const SyncSessionSnapshot* snap = service_->GetLastSessionSnapshot();
98 for (ModelTypeSet::const_iterator it = to_migrate_.begin();
99 it != to_migrate_.end(); ++it) {
100 if (snap->download_progress_markers[*it].empty())
101 num_empty_migrated_markers++;
102 }
103
104 if (num_empty_migrated_markers < to_migrate_.size())
105 return;
106
107 state_ = REENABLING_TYPES;
108 ModelTypeSet full_set;
109 service_->GetPreferredDataTypes(&full_set);
110 VLOG(1) << "BackendMigrator re-enabling types.";
111
112 // Don't use |to_migrate_| for the re-enabling because the user may have
113 // chosen to disable types during the migration.
114 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION);
115 }
116
117 void BackendMigrator::Observe(int type, 104 void BackendMigrator::Observe(int type,
118 const NotificationSource& source, 105 const NotificationSource& source,
119 const NotificationDetails& details) { 106 const NotificationDetails& details) {
120 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); 107 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type);
121 if (state_ == IDLE) 108 if (state_ == IDLE)
122 return; 109 return;
123 110
124 const DataTypeManager::ConfigureResult* result = 111 // |manager_|'s methods aren't re-entrant, and we're notified from
125 Details<DataTypeManager::ConfigureResult>(details).ptr(); 112 // them, so post a task to avoid problems.
113 VLOG(1) << "Posting OnConfigureDone from Observer";
114 MessageLoop::current()->PostTask(
115 FROM_HERE,
116 base::Bind(&BackendMigrator::OnConfigureDone,
117 weak_ptr_factory_.GetWeakPtr(),
118 *Details<DataTypeManager::ConfigureResult>(details).ptr()));
119 }
126 120
127 ModelTypeSet intersection; 121 void BackendMigrator::OnConfigureDone(
128 std::set_intersection(result->requested_types.begin(), 122 const DataTypeManager::ConfigureResult& result) {
129 result->requested_types.end(), to_migrate_.begin(), to_migrate_.end(), 123 VLOG(1) << "OnConfigureDone with types "
130 std::inserter(intersection, intersection.end())); 124 << ModelTypeSetToString(result.requested_types)
131 125 << ", status " << result.status
132 // The intersection check is to determine if our disable request was 126 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_);
133 // interrupted by a user changing preferred types. May still need to purge. 127 TryStartIfWaiting();
134 // It's pretty wild if we're in WAITING_FOR_PURGE here, because it would mean 128 if (state_ == WAITING_TO_START) {
135 // that after our disable-config finished but before the purge, another config 129 VLOG(1) << "Manager still not configured; still waiting";
136 // was posted externally _and completed_, which means somehow the nudge to
137 // purge was dropped, yet nudges are reliable.
138 if (state_ == WAITING_TO_START || state_ == WAITING_FOR_PURGE ||
139 (state_ == DISABLING_TYPES && !intersection.empty())) {
140 state_ = WAITING_TO_START;
141 restart_migration_ = false;
142 VLOG(1) << "BackendMigrator::Observe posting MigrateTypes.";
143 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
144 method_factory_.NewRunnableMethod(&BackendMigrator::MigrateTypes,
145 to_migrate_))) {
146 // Unittests need this.
147 // TODO(tim): Clean this up.
148 MigrateTypes(to_migrate_);
149 }
150 return; 130 return;
151 } 131 }
152 132
153 if (result->status != DataTypeManager::OK) { 133 DCHECK_GT(state_, WAITING_TO_START);
134
135 ModelTypeSet intersection;
136 std::set_intersection(
137 result.requested_types.begin(), result.requested_types.end(),
138 to_migrate_.begin(), to_migrate_.end(),
139 std::inserter(intersection, intersection.end()));
140 // This intersection check is to determine if our disable request
141 // was interrupted by a user changing preferred types.
142 if (state_ == DISABLING_TYPES && !intersection.empty()) {
143 VLOG(1) << "Disable request interrupted by user changing types";
144 RestartMigration();
145 return;
146 }
147
148 if (result.status != DataTypeManager::OK) {
154 // If this fails, and we're disabling types, a type may or may not be 149 // If this fails, and we're disabling types, a type may or may not be
155 // disabled until the user restarts the browser. If this wasn't an abort, 150 // disabled until the user restarts the browser. If this wasn't an abort,
156 // any failure will be reported as an unrecoverable error to the UI. If it 151 // any failure will be reported as an unrecoverable error to the UI. If it
157 // was an abort, then typically things are shutting down anyway. There isn't 152 // was an abort, then typically things are shutting down anyway. There isn't
158 // much we can do in any case besides wait until a restart to try again. 153 // much we can do in any case besides wait until a restart to try again.
159 // The server will send down MIGRATION_DONE again for types needing 154 // The server will send down MIGRATION_DONE again for types needing
160 // migration as the type will still be enabled on restart. 155 // migration as the type will still be enabled on restart.
161 LOG(WARNING) << "Unable to migrate, configuration failed!"; 156 LOG(WARNING) << "Unable to migrate, configuration failed!";
162 state_ = IDLE; 157 state_ = IDLE;
163 to_migrate_.clear(); 158 to_migrate_.clear();
164 return; 159 return;
165 } 160 }
166 161
167 if (state_ == DISABLING_TYPES) { 162 if (state_ == DISABLING_TYPES) {
168 state_ = WAITING_FOR_PURGE; 163 state_ = REENABLING_TYPES;
169 VLOG(1) << "BackendMigrator waiting for purge."; 164 // Don't use |to_migrate_| for the re-enabling because the user
165 // may have chosen to disable types during the migration.
166 ModelTypeSet full_set;
167 service_->GetPreferredDataTypes(&full_set);
168 VLOG(1) << "BackendMigrator re-enabling types: "
169 << syncable::ModelTypeSetToString(full_set);
170 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION);
170 } else if (state_ == REENABLING_TYPES) { 171 } else if (state_ == REENABLING_TYPES) {
171 // We're done! 172 // We're done!
172 state_ = IDLE; 173 state_ = IDLE;
173 174
174 std::stringstream ss; 175 VLOG(1) << "BackendMigrator: Migration complete for: "
175 std::copy(to_migrate_.begin(), to_migrate_.end(), 176 << syncable::ModelTypeSetToString(to_migrate_);
176 std::ostream_iterator<syncable::ModelType>(ss, ","));
177 VLOG(1) << "BackendMigrator: Migration complete for: " << ss.str();
178 to_migrate_.clear(); 177 to_migrate_.clear();
179 } 178 }
180 } 179 }
181 180
182 BackendMigrator::State BackendMigrator::state() const { 181 BackendMigrator::State BackendMigrator::state() const {
183 return state_; 182 return state_;
184 } 183 }
185 184
186 }; // namespace browser_sync 185 }; // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698