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

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: Sync to head 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;
tim (not reviewing) 2011/08/22 15:08:49 nit - I'd just specify std::swap inline since you
akalin 2011/08/24 22:50:17 Done.
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) {
tim (not reviewing) 2011/08/22 15:08:49 nit - file uses no-braces-on-single-line-if
akalin 2011/08/24 22:50:17 Done.
59 VLOG(1) << "BackendMigrator::MigrateTypes: manager CONFIGURED early-out.";
60 state_ = WAITING_TO_START; 56 state_ = WAITING_TO_START;
57 }
58
59 if (state_ == WAITING_TO_START) {
60 TryStart();
tim (not reviewing) 2011/08/22 15:08:49 Would probably be simpler (here and below) if TryS
akalin 2011/08/24 22:50:17 Done.
61 if (state_ == WAITING_TO_START) {
62 VLOG(1) << "Manager not configured; waiting";
63 }
61 return; 64 return;
62 } 65 }
63 66
67 DCHECK_GT(state_, WAITING_TO_START);
68 // If we're already migrating, interrupt the current migration.
69 RestartMigration();
70 }
71
72 void BackendMigrator::TryStart() {
73 DCHECK_EQ(state_, WAITING_TO_START);
74 if (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())
tim (not reviewing) 2011/08/22 15:08:49 Is there a way we can still have this check? I kn
akalin 2011/08/24 22:55:01 Done.
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 if (state_ == WAITING_TO_START) {
134 // It's pretty wild if we're in WAITING_FOR_PURGE here, because it would mean 128 TryStart();
135 // that after our disable-config finished but before the purge, another config 129 if (state_ == WAITING_TO_START) {
136 // was posted externally _and completed_, which means somehow the nudge to 130 VLOG(1) << "Manager still not configured; still waiting";
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 } 131 }
150 return; 132 return;
151 } 133 }
152 134
153 if (result->status != DataTypeManager::OK) { 135 DCHECK_GT(state_, WAITING_TO_START);
136
137 ModelTypeSet intersection;
138 std::set_intersection(
139 result.requested_types.begin(), result.requested_types.end(),
140 to_migrate_.begin(), to_migrate_.end(),
141 std::inserter(intersection, intersection.end()));
142 // This intersection check is to determine if our disable request
143 // was interrupted by a user changing preferred types.
144 if (state_ == DISABLING_TYPES && !intersection.empty()) {
145 VLOG(1) << "Disable request interrupted by user changing types";
146 RestartMigration();
147 return;
148 }
149
150 if (result.status != DataTypeManager::OK) {
154 // If this fails, and we're disabling types, a type may or may not be 151 // 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, 152 // 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 153 // 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 154 // 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. 155 // 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 156 // The server will send down MIGRATION_DONE again for types needing
160 // migration as the type will still be enabled on restart. 157 // migration as the type will still be enabled on restart.
161 LOG(WARNING) << "Unable to migrate, configuration failed!"; 158 LOG(WARNING) << "Unable to migrate, configuration failed!";
162 state_ = IDLE; 159 state_ = IDLE;
163 to_migrate_.clear(); 160 to_migrate_.clear();
164 return; 161 return;
165 } 162 }
166 163
167 if (state_ == DISABLING_TYPES) { 164 if (state_ == DISABLING_TYPES) {
168 state_ = WAITING_FOR_PURGE; 165 state_ = REENABLING_TYPES;
169 VLOG(1) << "BackendMigrator waiting for purge."; 166 // Don't use |to_migrate_| for the re-enabling because the user
167 // may have chosen to disable types during the migration.
168 ModelTypeSet full_set;
169 service_->GetPreferredDataTypes(&full_set);
170 VLOG(1) << "BackendMigrator re-enabling types: "
171 << syncable::ModelTypeSetToString(full_set);
172 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION);
170 } else if (state_ == REENABLING_TYPES) { 173 } else if (state_ == REENABLING_TYPES) {
171 // We're done! 174 // We're done!
172 state_ = IDLE; 175 state_ = IDLE;
173 176
174 std::stringstream ss; 177 VLOG(1) << "BackendMigrator: Migration complete for: "
175 std::copy(to_migrate_.begin(), to_migrate_.end(), 178 << syncable::ModelTypeSetToString(to_migrate_);
176 std::ostream_iterator<syncable::ModelType>(ss, ","));
177 VLOG(1) << "BackendMigrator: Migration complete for: " << ss.str();
178 to_migrate_.clear(); 179 to_migrate_.clear();
179 } 180 }
180 } 181 }
181 182
182 BackendMigrator::State BackendMigrator::state() const { 183 BackendMigrator::State BackendMigrator::state() const {
183 return state_; 184 return state_;
184 } 185 }
185 186
186 }; // namespace browser_sync 187 }; // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698