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

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: Cleanup Created 9 years, 3 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/message_loop.h"
9 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
10 #include "chrome/browser/sync/glue/data_type_manager.h" 11 #include "base/tracked_objects.h"
11 #include "chrome/browser/sync/internal_api/configure_reason.h" 12 #include "chrome/browser/sync/internal_api/configure_reason.h"
13 #include "chrome/browser/sync/internal_api/read_transaction.h"
12 #include "chrome/browser/sync/profile_sync_service.h" 14 #include "chrome/browser/sync/profile_sync_service.h"
15 #include "chrome/browser/sync/protocol/sync.pb.h"
13 #include "chrome/browser/sync/sessions/session_state.h" 16 #include "chrome/browser/sync/sessions/session_state.h"
17 #include "chrome/browser/sync/syncable/directory_manager.h"
14 #include "chrome/common/chrome_notification_types.h" 18 #include "chrome/common/chrome_notification_types.h"
15 #include "content/browser/browser_thread.h"
16 #include "content/common/notification_details.h" 19 #include "content/common/notification_details.h"
17 #include "content/common/notification_source.h" 20 #include "content/common/notification_source.h"
18 21
19 using syncable::ModelTypeSet; 22 using syncable::ModelTypeSet;
20 23
21 namespace browser_sync { 24 namespace browser_sync {
22 25
23 using sessions::SyncSessionSnapshot; 26 using sessions::SyncSessionSnapshot;
27 using syncable::ModelTypeToString;
24 28
25 BackendMigrator::BackendMigrator(ProfileSyncService* service, 29 MigrationObserver::~MigrationObserver() {}
30
31 BackendMigrator::BackendMigrator(const std::string& name,
32 sync_api::UserShare* user_share,
33 ProfileSyncService* service,
26 DataTypeManager* manager) 34 DataTypeManager* manager)
27 : state_(IDLE), service_(service), manager_(manager), 35 : name_(name), user_share_(user_share), service_(service),
28 restart_migration_(false), 36 manager_(manager), state_(IDLE),
29 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 37 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
30 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, 38 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
31 Source<DataTypeManager>(manager_)); 39 Source<DataTypeManager>(manager_));
32 service_->AddObserver(this);
33 } 40 }
34 41
35 BackendMigrator::~BackendMigrator() { 42 BackendMigrator::~BackendMigrator() {
36 service_->RemoveObserver(this);
37 } 43 }
38 44
39 bool BackendMigrator::HasStartedMigrating() const { 45 // Helper macros to log with the syncer thread name; useful when there
40 return state_ >= DISABLING_TYPES; 46 // are multiple syncer threads involved.
41 } 47
48 #define SLOG(severity) LOG(severity) << name_ << ": "
49
50 #define SVLOG(verbose_level) VLOG(verbose_level) << name_ << ": "
42 51
43 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { 52 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) {
53 const ModelTypeSet old_to_migrate = to_migrate_;
44 { 54 {
45 ModelTypeSet temp; 55 ModelTypeSet temp;
46 std::set_union(to_migrate_.begin(), to_migrate_.end(), 56 std::set_union(to_migrate_.begin(), to_migrate_.end(),
47 types.begin(), types.end(), 57 types.begin(), types.end(),
48 std::inserter(temp, temp.end())); 58 std::inserter(temp, temp.end()));
49 to_migrate_ = temp; 59 std::swap(temp, to_migrate_);
50 } 60 }
51 61 SVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types)
52 if (HasStartedMigrating()) { 62 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate)
53 VLOG(1) << "BackendMigrator::MigrateTypes: STARTED_MIGRATING early-out."; 63 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_);
54 restart_migration_ = true; 64 if (old_to_migrate == to_migrate_) {
65 SVLOG(1) << "MigrateTypes called with no new types; ignoring";
55 return; 66 return;
56 } 67 }
57 68
58 if (manager_->state() != DataTypeManager::CONFIGURED) { 69 if (state_ == IDLE)
59 VLOG(1) << "BackendMigrator::MigrateTypes: manager CONFIGURED early-out."; 70 ChangeState(WAITING_TO_START);
60 state_ = WAITING_TO_START; 71
72 if (state_ == WAITING_TO_START) {
73 if (!TryStart())
74 SVLOG(1) << "Manager not configured; waiting";
61 return; 75 return;
62 } 76 }
63 77
78 DCHECK_GT(state_, WAITING_TO_START);
79 // If we're already migrating, interrupt the current migration.
80 RestartMigration();
81 }
82
83 void BackendMigrator::AddMigrationObserver(MigrationObserver* observer) {
84 migration_observers_.AddObserver(observer);
85 }
86
87 void BackendMigrator::RemoveMigrationObserver(MigrationObserver* observer) {
88 migration_observers_.RemoveObserver(observer);
89 }
90
91 void BackendMigrator::ChangeState(State new_state) {
92 state_ = new_state;
93 FOR_EACH_OBSERVER(MigrationObserver, migration_observers_,
94 OnMigrationStateChange());
95 }
96
97 bool BackendMigrator::TryStart() {
98 DCHECK_EQ(state_, WAITING_TO_START);
99 if (manager_->state() == DataTypeManager::CONFIGURED) {
100 RestartMigration();
101 return true;
102 }
103 return false;
104 }
105
106 void BackendMigrator::RestartMigration() {
64 // We'll now disable any running types that need to be migrated. 107 // We'll now disable any running types that need to be migrated.
65 state_ = DISABLING_TYPES; 108 ChangeState(DISABLING_TYPES);
66 ModelTypeSet full_set; 109 ModelTypeSet full_set;
67 service_->GetPreferredDataTypes(&full_set); 110 service_->GetPreferredDataTypes(&full_set);
68 ModelTypeSet difference; 111 ModelTypeSet difference;
69 std::set_difference(full_set.begin(), full_set.end(), 112 std::set_difference(full_set.begin(), full_set.end(),
70 to_migrate_.begin(), to_migrate_.end(), 113 to_migrate_.begin(), to_migrate_.end(),
71 std::inserter(difference, difference.end())); 114 std::inserter(difference, difference.end()));
72 VLOG(1) << "BackendMigrator disabling types; calling Configure."; 115 bool configure_with_nigori = to_migrate_.count(syncable::NIGORI) == 0;
116 SVLOG(1) << "BackendMigrator disabling types "
117 << ModelTypeSetToString(to_migrate_) << "; configuring "
118 << ModelTypeSetToString(difference)
119 << (configure_with_nigori ? " with nigori" : " without nigori");
73 120
74 // Add nigori for config or not based upon if the server told us to migrate 121 // Add nigori for config or not based upon if the server told us to migrate
75 // nigori or not. 122 // nigori or not.
76 if (to_migrate_.count(syncable::NIGORI) == 0) { 123 if (configure_with_nigori) {
77 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); 124 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION);
78 } else { 125 } else {
79 manager_->ConfigureWithoutNigori(difference, 126 manager_->ConfigureWithoutNigori(difference,
80 sync_api::CONFIGURE_REASON_MIGRATION); 127 sync_api::CONFIGURE_REASON_MIGRATION);
81 } 128 }
82 } 129 }
83 130
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, 131 void BackendMigrator::Observe(int type,
118 const NotificationSource& source, 132 const NotificationSource& source,
119 const NotificationDetails& details) { 133 const NotificationDetails& details) {
120 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); 134 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type);
121 if (state_ == IDLE) 135 if (state_ == IDLE)
122 return; 136 return;
123 137
124 const DataTypeManager::ConfigureResult* result = 138 // |manager_|'s methods aren't re-entrant, and we're notified from
125 Details<DataTypeManager::ConfigureResult>(details).ptr(); 139 // them, so post a task to avoid problems.
140 SVLOG(1) << "Posting OnConfigureDone from Observer";
141 MessageLoop::current()->PostTask(
142 FROM_HERE,
143 base::Bind(&BackendMigrator::OnConfigureDone,
144 weak_ptr_factory_.GetWeakPtr(),
145 *Details<DataTypeManager::ConfigureResult>(details).ptr()));
146 }
126 147
127 ModelTypeSet intersection; 148 namespace {
128 std::set_intersection(result->requested_types.begin(),
129 result->requested_types.end(), to_migrate_.begin(), to_migrate_.end(),
130 std::inserter(intersection, intersection.end()));
131 149
132 // The intersection check is to determine if our disable request was 150 syncable::ModelTypeSet GetUnsyncedDataTypes(sync_api::UserShare* user_share) {
tim (not reviewing) 2011/09/01 01:50:56 Ugh :( It's pretty unfortunate that we have to ho
akalin 2011/09/01 02:19:40 Okay, so should I put this code back into SyncBack
akalin 2011/09/01 03:39:50 Added a TODO in the header file to remove dep on u
133 // interrupted by a user changing preferred types. May still need to purge. 151 sync_api::ReadTransaction trans(FROM_HERE, user_share);
134 // It's pretty wild if we're in WAITING_FOR_PURGE here, because it would mean 152 syncable::ModelTypeSet unsynced_data_types;
135 // that after our disable-config finished but before the purge, another config 153 for (int i = syncable::FIRST_REAL_MODEL_TYPE;
136 // was posted externally _and completed_, which means somehow the nudge to 154 i < syncable::MODEL_TYPE_COUNT; ++i) {
137 // purge was dropped, yet nudges are reliable. 155 syncable::ModelType type = syncable::ModelTypeFromInt(i);
138 if (state_ == WAITING_TO_START || state_ == WAITING_FOR_PURGE || 156 sync_pb::DataTypeProgressMarker progress_marker;
139 (state_ == DISABLING_TYPES && !intersection.empty())) { 157 trans.GetLookup()->GetDownloadProgress(type, &progress_marker);
140 state_ = WAITING_TO_START; 158 if (progress_marker.token().empty()) {
141 restart_migration_ = false; 159 unsynced_data_types.insert(type);
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 } 160 }
161 }
162 return unsynced_data_types;
163 }
164
165 } // namespace
166
167 void BackendMigrator::OnConfigureDone(
168 const DataTypeManager::ConfigureResult& result) {
169 SVLOG(1) << "OnConfigureDone with requested types "
170 << ModelTypeSetToString(result.requested_types)
171 << ", status " << result.status
172 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_);
173 if (state_ == WAITING_TO_START) {
174 if (!TryStart())
175 SVLOG(1) << "Manager still not configured; still waiting";
150 return; 176 return;
151 } 177 }
152 178
153 if (result->status != DataTypeManager::OK) { 179 DCHECK_GT(state_, WAITING_TO_START);
180
181 ModelTypeSet intersection;
182 std::set_intersection(
183 result.requested_types.begin(), result.requested_types.end(),
184 to_migrate_.begin(), to_migrate_.end(),
185 std::inserter(intersection, intersection.end()));
186 // This intersection check is to determine if our disable request
187 // was interrupted by a user changing preferred types.
188 if (state_ == DISABLING_TYPES && !intersection.empty()) {
189 SVLOG(1) << "Disable request interrupted by user changing types";
190 RestartMigration();
191 return;
192 }
193
194 if (result.status != DataTypeManager::OK) {
154 // If this fails, and we're disabling types, a type may or may not be 195 // 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, 196 // 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 197 // 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 198 // 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. 199 // 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 200 // The server will send down MIGRATION_DONE again for types needing
160 // migration as the type will still be enabled on restart. 201 // migration as the type will still be enabled on restart.
161 LOG(WARNING) << "Unable to migrate, configuration failed!"; 202 SLOG(WARNING) << "Unable to migrate, configuration failed!";
162 state_ = IDLE; 203 ChangeState(IDLE);
163 to_migrate_.clear(); 204 to_migrate_.clear();
164 return; 205 return;
165 } 206 }
166 207
167 if (state_ == DISABLING_TYPES) { 208 if (state_ == DISABLING_TYPES) {
168 state_ = WAITING_FOR_PURGE; 209 const syncable::ModelTypeSet& unsynced_types =
169 VLOG(1) << "BackendMigrator waiting for purge."; 210 GetUnsyncedDataTypes(user_share_);
211 if (!std::includes(unsynced_types.begin(), unsynced_types.end(),
212 to_migrate_.begin(), to_migrate_.end())) {
213 SLOG(WARNING) << "Set of unsynced types: "
214 << syncable::ModelTypeSetToString(unsynced_types)
215 << " does not contain types to migrate: "
216 << syncable::ModelTypeSetToString(to_migrate_)
217 << "; not re-enabling yet";
218 return;
219 }
220
221 ChangeState(REENABLING_TYPES);
222 // Don't use |to_migrate_| for the re-enabling because the user
223 // may have chosen to disable types during the migration.
224 ModelTypeSet full_set;
225 service_->GetPreferredDataTypes(&full_set);
226 SVLOG(1) << "BackendMigrator re-enabling types: "
227 << syncable::ModelTypeSetToString(full_set);
228 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION);
170 } else if (state_ == REENABLING_TYPES) { 229 } else if (state_ == REENABLING_TYPES) {
171 // We're done! 230 // We're done!
172 state_ = IDLE; 231 ChangeState(IDLE);
173 232
174 std::stringstream ss; 233 SVLOG(1) << "BackendMigrator: Migration complete for: "
175 std::copy(to_migrate_.begin(), to_migrate_.end(), 234 << syncable::ModelTypeSetToString(to_migrate_);
176 std::ostream_iterator<syncable::ModelType>(ss, ","));
177 VLOG(1) << "BackendMigrator: Migration complete for: " << ss.str();
178 to_migrate_.clear(); 235 to_migrate_.clear();
179 } 236 }
180 } 237 }
181 238
182 BackendMigrator::State BackendMigrator::state() const { 239 BackendMigrator::State BackendMigrator::state() const {
183 return state_; 240 return state_;
184 } 241 }
185 242
243 syncable::ModelTypeSet
244 BackendMigrator::GetPendingMigrationTypesForTest() const {
245 return to_migrate_;
246 }
247
248 #undef SVLOG
249
250 #undef SLOG
251
186 }; // namespace browser_sync 252 }; // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698