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

Side by Side Diff: chrome/browser/sync/engine/syncer_thread2.cc

Issue 6812004: sync: Make nudge + config jobs reliable in SyncerThread2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed a comment(it is the only change in this patch) Created 9 years, 8 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/engine/syncer_thread2.h" 5 #include "chrome/browser/sync/engine/syncer_thread2.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/rand_util.h" 9 #include "base/rand_util.h"
10 #include "chrome/browser/sync/engine/syncer.h" 10 #include "chrome/browser/sync/engine/syncer.h"
11 11
12 using base::TimeDelta; 12 using base::TimeDelta;
13 using base::TimeTicks; 13 using base::TimeTicks;
14 14
15 namespace browser_sync { 15 namespace browser_sync {
16 16
17 using sessions::SyncSession; 17 using sessions::SyncSession;
18 using sessions::SyncSessionSnapshot; 18 using sessions::SyncSessionSnapshot;
19 using sessions::SyncSourceInfo; 19 using sessions::SyncSourceInfo;
20 using syncable::ModelTypePayloadMap; 20 using syncable::ModelTypePayloadMap;
21 using syncable::ModelTypeBitSet; 21 using syncable::ModelTypeBitSet;
22 using sync_pb::GetUpdatesCallerInfo; 22 using sync_pb::GetUpdatesCallerInfo;
23 23
24 namespace s3 { 24 namespace s3 {
25 25
26 struct SyncerThread::WaitInterval {
27 enum Mode {
28 // A wait interval whose duration has been affected by exponential
29 // backoff.
30 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval.
31 EXPONENTIAL_BACKOFF,
32 // A server-initiated throttled interval. We do not allow any syncing
33 // during such an interval.
34 THROTTLED,
35 };
36 Mode mode;
37
38 // This bool is set to true if we have observed a nudge during this
39 // interval and mode == EXPONENTIAL_BACKOFF.
40 bool had_nudge;
41 base::TimeDelta length;
42 base::OneShotTimer<SyncerThread> timer;
43 WaitInterval(Mode mode, base::TimeDelta length);
44 };
45
46 struct SyncerThread::SyncSessionJob {
47 SyncSessionJobPurpose purpose;
48 base::TimeTicks scheduled_start;
49 linked_ptr<sessions::SyncSession> session;
50
51 // This is the location the nudge came from. used for debugging purpose.
52 // In case of multiple nudges getting coalesced this stores the first nudge
53 // that came in.
54 tracked_objects::Location nudge_location;
55 };
56
57 SyncerThread::DelayProvider::DelayProvider() {} 26 SyncerThread::DelayProvider::DelayProvider() {}
58 SyncerThread::DelayProvider::~DelayProvider() {} 27 SyncerThread::DelayProvider::~DelayProvider() {}
59 28
29 SyncerThread::WaitInterval::WaitInterval() {}
30 SyncerThread::WaitInterval::~WaitInterval() {}
31
32 SyncerThread::SyncSessionJob::SyncSessionJob() {}
33 SyncerThread::SyncSessionJob::~SyncSessionJob() {}
34
35 SyncerThread::SyncSessionJob::SyncSessionJob(SyncSessionJobPurpose purpose,
36 base::TimeTicks start,
37 linked_ptr<sessions::SyncSession> session, bool is_canary_job,
38 const tracked_objects::Location& nudge_location) : purpose(purpose),
39 scheduled_start(start),
40 session(session),
41 is_canary_job(is_canary_job),
42 nudge_location(nudge_location) {
43 }
44
60 TimeDelta SyncerThread::DelayProvider::GetDelay( 45 TimeDelta SyncerThread::DelayProvider::GetDelay(
61 const base::TimeDelta& last_delay) { 46 const base::TimeDelta& last_delay) {
62 return SyncerThread::GetRecommendedDelay(last_delay); 47 return SyncerThread::GetRecommendedDelay(last_delay);
63 } 48 }
64 49
50 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource(
51 NudgeSource source) {
52 switch (source) {
53 case NUDGE_SOURCE_NOTIFICATION:
54 return GetUpdatesCallerInfo::NOTIFICATION;
55 case NUDGE_SOURCE_LOCAL:
56 return GetUpdatesCallerInfo::LOCAL;
57 case NUDGE_SOURCE_CONTINUATION:
58 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION;
59 case NUDGE_SOURCE_UNKNOWN:
60 return GetUpdatesCallerInfo::UNKNOWN;
61 default:
62 NOTREACHED();
63 return GetUpdatesCallerInfo::UNKNOWN;
64 }
65 }
66
65 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length) 67 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length)
66 : mode(mode), had_nudge(false), length(length) { } 68 : mode(mode), had_nudge(false), length(length) { }
67 69
68 SyncerThread::SyncerThread(sessions::SyncSessionContext* context, 70 SyncerThread::SyncerThread(sessions::SyncSessionContext* context,
69 Syncer* syncer) 71 Syncer* syncer)
70 : thread_("SyncEngine_SyncerThread"), 72 : thread_("SyncEngine_SyncerThread"),
71 syncer_short_poll_interval_seconds_( 73 syncer_short_poll_interval_seconds_(
72 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), 74 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)),
73 syncer_long_poll_interval_seconds_( 75 syncer_long_poll_interval_seconds_(
74 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), 76 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)),
75 mode_(NORMAL_MODE), 77 mode_(NORMAL_MODE),
76 server_connection_ok_(false), 78 server_connection_ok_(false),
77 delay_provider_(new DelayProvider()), 79 delay_provider_(new DelayProvider()),
78 syncer_(syncer), 80 syncer_(syncer),
79 session_context_(context) { 81 session_context_(context) {
80 } 82 }
81 83
82 SyncerThread::~SyncerThread() { 84 SyncerThread::~SyncerThread() {
83 DCHECK(!thread_.IsRunning()); 85 DCHECK(!thread_.IsRunning());
84 } 86 }
85 87
86 void SyncerThread::CheckServerConnectionManagerStatus( 88 void SyncerThread::CheckServerConnectionManagerStatus(
87 HttpResponse::ServerConnectionCode code) { 89 HttpResponse::ServerConnectionCode code) {
90
88 // Note, be careful when adding cases here because if the SyncerThread 91 // Note, be careful when adding cases here because if the SyncerThread
89 // thinks there is no valid connection as determined by this method, it 92 // thinks there is no valid connection as determined by this method, it
90 // will drop out of *all* forward progress sync loops (it won't poll and it 93 // will drop out of *all* forward progress sync loops (it won't poll and it
91 // will queue up Talk notifications but not actually call SyncShare) until 94 // will queue up Talk notifications but not actually call SyncShare) until
92 // some external action causes a ServerConnectionManager to broadcast that 95 // some external action causes a ServerConnectionManager to broadcast that
93 // a valid connection has been re-established. 96 // a valid connection has been re-established.
94 if (HttpResponse::CONNECTION_UNAVAILABLE == code || 97 if (HttpResponse::CONNECTION_UNAVAILABLE == code ||
95 HttpResponse::SYNC_AUTH_ERROR == code) { 98 HttpResponse::SYNC_AUTH_ERROR == code) {
96 server_connection_ok_ = false; 99 server_connection_ok_ = false;
97 } else if (HttpResponse::SERVER_CONNECTION_OK == code) { 100 } else if (HttpResponse::SERVER_CONNECTION_OK == code) {
98 server_connection_ok_ = true; 101 server_connection_ok_ = true;
102 DoCanaryJob();
99 } 103 }
100 } 104 }
101 105
102 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) { 106 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) {
103 if (!thread_.IsRunning()) { 107 if (!thread_.IsRunning()) {
108 VLOG(2) << "Starting thread with mode " << mode;
104 if (!thread_.Start()) { 109 if (!thread_.Start()) {
105 NOTREACHED() << "Unable to start SyncerThread."; 110 NOTREACHED() << "Unable to start SyncerThread.";
106 return; 111 return;
107 } 112 }
108 WatchConnectionManager(); 113 WatchConnectionManager();
109 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 114 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
110 this, &SyncerThread::SendInitialSnapshot)); 115 this, &SyncerThread::SendInitialSnapshot));
111 } 116 }
112 117
118 VLOG(2) << " Entering start with mode = " << mode;
119
113 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 120 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
114 this, &SyncerThread::StartImpl, mode, make_linked_ptr(callback))); 121 this, &SyncerThread::StartImpl, mode, make_linked_ptr(callback)));
115 } 122 }
116 123
117 void SyncerThread::SendInitialSnapshot() { 124 void SyncerThread::SendInitialSnapshot() {
118 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 125 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
119 scoped_ptr<SyncSession> dummy(new SyncSession(session_context_.get(), this, 126 scoped_ptr<SyncSession> dummy(new SyncSession(session_context_.get(), this,
120 SyncSourceInfo(), ModelSafeRoutingInfo(), 127 SyncSourceInfo(), ModelSafeRoutingInfo(),
121 std::vector<ModelSafeWorker*>())); 128 std::vector<ModelSafeWorker*>()));
122 SyncEngineEvent event(SyncEngineEvent::STATUS_CHANGED); 129 SyncEngineEvent event(SyncEngineEvent::STATUS_CHANGED);
123 sessions::SyncSessionSnapshot snapshot(dummy->TakeSnapshot()); 130 sessions::SyncSessionSnapshot snapshot(dummy->TakeSnapshot());
124 event.snapshot = &snapshot; 131 event.snapshot = &snapshot;
125 session_context_->NotifyListeners(event); 132 session_context_->NotifyListeners(event);
126 } 133 }
127 134
128 void SyncerThread::WatchConnectionManager() { 135 void SyncerThread::WatchConnectionManager() {
129 ServerConnectionManager* scm = session_context_->connection_manager(); 136 ServerConnectionManager* scm = session_context_->connection_manager();
130 CheckServerConnectionManagerStatus(scm->server_status()); 137 CheckServerConnectionManagerStatus(scm->server_status());
131 scm->AddListener(this); 138 scm->AddListener(this);
132 } 139 }
133 140
134 void SyncerThread::StartImpl(Mode mode, 141 void SyncerThread::StartImpl(Mode mode,
135 linked_ptr<ModeChangeCallback> callback) { 142 linked_ptr<ModeChangeCallback> callback) {
143 VLOG(2) << "Doing StartImpl with mode " << mode;
136 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 144 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
137 DCHECK(!session_context_->account_name().empty()); 145 DCHECK(!session_context_->account_name().empty());
138 DCHECK(syncer_.get()); 146 DCHECK(syncer_.get());
139 mode_ = mode; 147 mode_ = mode;
140 AdjustPolling(NULL); // Will kick start poll timer if needed. 148 AdjustPolling(NULL); // Will kick start poll timer if needed.
141 if (callback.get()) 149 if (callback.get())
142 callback->Run(); 150 callback->Run();
151
152 // We just changed our mode. See if there are any pending jobs that we could
153 // execute in the new mode.
154 DoPendingJobIfPossible(false);
143 } 155 }
144 156
145 bool SyncerThread::ShouldRunJob(SyncSessionJobPurpose purpose, 157 SyncerThread::JobProcessDecision SyncerThread::DecideWhileInWaitInterval(
146 const TimeTicks& scheduled_start) { 158 const SyncSessionJob& job) {
147 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
148 159
149 // Check wait interval. 160 DCHECK(wait_interval_.get());
150 if (wait_interval_.get()) { 161 DCHECK_NE(job.purpose, SyncSessionJob::CLEAR_USER_DATA);
151 // TODO(tim): Consider different handling for CLEAR_USER_DATA (i.e. permit
152 // when throttled).
153 if (wait_interval_->mode == WaitInterval::THROTTLED)
154 return false;
155 162
156 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); 163 VLOG(2) << "Wait interval mode : " << wait_interval_->mode
157 if ((purpose != NUDGE) || wait_interval_->had_nudge) 164 << "Wait interval had nudge : " << wait_interval_->had_nudge
158 return false; 165 << "is canary job : " << job.is_canary_job;
166
167 if (job.purpose == SyncSessionJob::POLL)
168 return DROP;
169
170 DCHECK(job.purpose == SyncSessionJob::NUDGE ||
171 job.purpose == SyncSessionJob::CONFIGURATION);
172 if (wait_interval_->mode == WaitInterval::THROTTLED)
173 return SAVE;
174
175 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF);
176 if (job.purpose == SyncSessionJob::NUDGE) {
177 if (mode_ == CONFIGURATION_MODE)
178 return SAVE;
179
180 // If we already had one nudge then just drop this nudge. We will retry
181 // later when the timer runs out.
182 return wait_interval_->had_nudge ? DROP : CONTINUE;
183 }
184 // This is a config job.
185 return job.is_canary_job ? CONTINUE : SAVE;
186 }
187
188 SyncerThread::JobProcessDecision SyncerThread::DecideOnJob(
189 const SyncSessionJob& job) {
190 if (job.purpose == SyncSessionJob::CLEAR_USER_DATA)
191 return CONTINUE;
192
193 if (wait_interval_.get())
194 return DecideWhileInWaitInterval(job);
195
196 if (mode_ == CONFIGURATION_MODE) {
197 if (job.purpose == SyncSessionJob::NUDGE)
198 return SAVE;
199 else if (job.purpose == SyncSessionJob::CONFIGURATION)
200 return CONTINUE;
201 else
202 return DROP;
159 } 203 }
160 204
161 // Mode / purpose contract (See 'Mode' enum in header). Don't run jobs that 205 // We are in normal mode.
162 // were intended for a normal sync if we are in configuration mode, and vice 206 DCHECK_EQ(mode_, NORMAL_MODE);
163 // versa. 207 DCHECK_NE(job.purpose, SyncSessionJob::CONFIGURATION);
164 switch (mode_) { 208
165 case CONFIGURATION_MODE: 209 // Freshness condition
166 if (purpose != CONFIGURATION) 210 if (job.scheduled_start < last_sync_session_end_time_) {
167 return false; 211 VLOG(2) << "Dropping job because of freshness";
168 break; 212 return DROP;
169 case NORMAL_MODE:
170 if (purpose == CONFIGURATION)
171 return false;
172 break;
173 default:
174 NOTREACHED() << "Unknown SyncerThread Mode: " << mode_;
175 return false;
176 } 213 }
177 214
178 // Continuation NUDGE tasks have priority over POLLs because they are the 215 if (server_connection_ok_)
179 // only tasks that trigger exponential backoff, so this prevents them from 216 return CONTINUE;
180 // being starved from running (e.g. due to a very, very low poll interval, 217
181 // such as 0ms). It's rare that this would ever matter in practice. 218 VLOG(2) << " Bad server connection. Using that to decide on job.";
182 if (purpose == POLL && (pending_nudge_.get() && 219 return job.purpose == SyncSessionJob::NUDGE ? SAVE : DROP;
183 pending_nudge_->session->source().updates_source == 220 }
184 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION)) { 221
185 return false; 222 void SyncerThread::InitOrCoalescePendingJob(const SyncSessionJob& job) {
223 DCHECK(job.purpose != SyncSessionJob::CONFIGURATION);
224 if (pending_nudge_.get() == NULL) {
225 VLOG(2) << "Creating a pending nudge job";
226 SyncSession* s = job.session.get();
227 scoped_ptr<SyncSession> session(new SyncSession(s->context(),
228 s->delegate(), s->source(), s->routing_info(), s->workers()));
229
230 SyncSessionJob new_job(SyncSessionJob::NUDGE, job.scheduled_start,
231 make_linked_ptr(session.release()), false, job.nudge_location);
232 pending_nudge_.reset(new SyncSessionJob(new_job));
233
234 return;
186 } 235 }
187 236
188 // Freshness condition. 237 VLOG(2) << "Coalescing a pending nudge";
189 if (purpose == NUDGE && 238 pending_nudge_->session->Coalesce(*(job.session.get()));
190 (scheduled_start < last_sync_session_end_time_)) { 239 pending_nudge_->scheduled_start = job.scheduled_start;
191 return false;
192 }
193 240
194 return server_connection_ok_; 241 // Unfortunately the nudge location cannot be modified. So it stores the
242 // location of the first caller.
195 } 243 }
196 244
197 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource( 245 bool SyncerThread::ShouldRunJob(const SyncSessionJob& job) {
198 NudgeSource source) { 246 JobProcessDecision decision = DecideOnJob(job);
199 switch (source) { 247 VLOG(2) << "Should run job, decision: " << decision
200 case NUDGE_SOURCE_NOTIFICATION: 248 << " Job purpose " << job.purpose << "mode " << mode_;
201 return GetUpdatesCallerInfo::NOTIFICATION; 249 if (decision != SAVE)
202 case NUDGE_SOURCE_LOCAL: 250 return decision == CONTINUE;
203 return GetUpdatesCallerInfo::LOCAL; 251
204 case NUDGE_SOURCE_CONTINUATION: 252 DCHECK(job.purpose == SyncSessionJob::NUDGE || job.purpose ==
205 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; 253 SyncSessionJob::CONFIGURATION);
206 case NUDGE_SOURCE_UNKNOWN: 254
207 return GetUpdatesCallerInfo::UNKNOWN; 255 SaveJob(job);
208 default: 256 return false;
209 NOTREACHED(); 257 }
210 return GetUpdatesCallerInfo::UNKNOWN; 258
211 } 259 void SyncerThread::SaveJob(const SyncSessionJob& job) {
260 DCHECK(job.purpose != SyncSessionJob::CLEAR_USER_DATA);
261 if (job.purpose == SyncSessionJob::NUDGE) {
262 VLOG(2) << "Saving a nudge job";
263 InitOrCoalescePendingJob(job);
264 } else if (job.purpose == SyncSessionJob::CONFIGURATION){
265 VLOG(2) << "Saving a configuration job";
266 DCHECK(wait_interval_.get());
267 DCHECK(mode_ == CONFIGURATION_MODE);
268
269 SyncSession* old = job.session.get();
270 SyncSession* s(new SyncSession(session_context_.get(), this,
271 old->source(), old->routing_info(), old->workers()));
272 SyncSessionJob new_job(job.purpose, TimeTicks::Now(),
273 make_linked_ptr(s), false, job.nudge_location);
274 wait_interval_->pending_configure_job.reset(new SyncSessionJob(new_job));
275 } // drop the rest.
212 } 276 }
213 277
214 // Functor for std::find_if to search by ModelSafeGroup. 278 // Functor for std::find_if to search by ModelSafeGroup.
215 struct ModelSafeWorkerGroupIs { 279 struct ModelSafeWorkerGroupIs {
216 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} 280 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {}
217 bool operator()(ModelSafeWorker* w) { 281 bool operator()(ModelSafeWorker* w) {
218 return group == w->GetModelSafeGroup(); 282 return group == w->GetModelSafeGroup();
219 } 283 }
220 ModelSafeGroup group; 284 ModelSafeGroup group;
221 }; 285 };
222 286
223 void SyncerThread::ScheduleClearUserData() { 287 void SyncerThread::ScheduleClearUserData() {
224 if (!thread_.IsRunning()) { 288 if (!thread_.IsRunning()) {
225 NOTREACHED(); 289 NOTREACHED();
226 return; 290 return;
227 } 291 }
228 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 292 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
229 this, &SyncerThread::ScheduleClearUserDataImpl)); 293 this, &SyncerThread::ScheduleClearUserDataImpl));
230 } 294 }
231 295
232 void SyncerThread::ScheduleNudge(const TimeDelta& delay, 296 void SyncerThread::ScheduleNudge(const TimeDelta& delay,
233 NudgeSource source, const ModelTypeBitSet& types, 297 NudgeSource source, const ModelTypeBitSet& types,
234 const tracked_objects::Location& nudge_location) { 298 const tracked_objects::Location& nudge_location) {
235 if (!thread_.IsRunning()) { 299 if (!thread_.IsRunning()) {
236 NOTREACHED(); 300 NOTREACHED();
237 return; 301 return;
238 } 302 }
239 303
304 VLOG(2) << "Nudge scheduled";
305
240 ModelTypePayloadMap types_with_payloads = 306 ModelTypePayloadMap types_with_payloads =
241 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); 307 syncable::ModelTypePayloadMapFromBitSet(types, std::string());
242 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 308 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
243 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 309 this, &SyncerThread::ScheduleNudgeImpl, delay,
244 types_with_payloads, nudge_location)); 310 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
311 nudge_location));
245 } 312 }
246 313
247 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, 314 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay,
248 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 315 NudgeSource source, const ModelTypePayloadMap& types_with_payloads,
249 const tracked_objects::Location& nudge_location) { 316 const tracked_objects::Location& nudge_location) {
250 if (!thread_.IsRunning()) { 317 if (!thread_.IsRunning()) {
251 NOTREACHED(); 318 NOTREACHED();
252 return; 319 return;
253 } 320 }
254 321
322 VLOG(2) << "Nudge scheduled with payloads";
323
255 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 324 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
256 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 325 this, &SyncerThread::ScheduleNudgeImpl, delay,
257 types_with_payloads, nudge_location)); 326 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
327 nudge_location));
258 } 328 }
259 329
260 void SyncerThread::ScheduleClearUserDataImpl() { 330 void SyncerThread::ScheduleClearUserDataImpl() {
261 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 331 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
262 SyncSession* session = new SyncSession(session_context_.get(), this, 332 SyncSession* session = new SyncSession(session_context_.get(), this,
263 SyncSourceInfo(), ModelSafeRoutingInfo(), 333 SyncSourceInfo(), ModelSafeRoutingInfo(),
264 std::vector<ModelSafeWorker*>()); 334 std::vector<ModelSafeWorker*>());
265 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session, 335 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0),
266 FROM_HERE); 336 SyncSessionJob::CLEAR_USER_DATA, session, FROM_HERE);
267 } 337 }
268 338
269 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, 339 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay,
270 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 340 GetUpdatesCallerInfo::GetUpdatesSource source,
271 const tracked_objects::Location& nudge_location) { 341 const ModelTypePayloadMap& types_with_payloads,
342 bool is_canary_job, const tracked_objects::Location& nudge_location) {
272 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 343 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
273 TimeTicks rough_start = TimeTicks::Now() + delay;
274 if (!ShouldRunJob(NUDGE, rough_start)) {
275 LOG(WARNING) << "Dropping nudge at scheduling time, source = "
276 << source;
277 return;
278 }
279 344
345 VLOG(2) << "Running Schedule nudge impl";
280 // Note we currently nudge for all types regardless of the ones incurring 346 // Note we currently nudge for all types regardless of the ones incurring
281 // the nudge. Doing different would throw off some syncer commands like 347 // the nudge. Doing different would throw off some syncer commands like
282 // CleanupDisabledTypes. We may want to change this in the future. 348 // CleanupDisabledTypes. We may want to change this in the future.
283 ModelSafeRoutingInfo routes; 349 SyncSourceInfo info(source, types_with_payloads);
284 std::vector<ModelSafeWorker*> workers;
285 session_context_->registrar()->GetModelSafeRoutingInfo(&routes);
286 session_context_->registrar()->GetWorkers(&workers);
287 SyncSourceInfo info(GetUpdatesFromNudgeSource(source),
288 types_with_payloads);
289 350
290 scoped_ptr<SyncSession> session(new SyncSession( 351 SyncSession* session(CreateSyncSession(info));
291 session_context_.get(), this, info, routes, workers)); 352 SyncSessionJob job(SyncSessionJob::NUDGE, TimeTicks::Now() + delay,
353 make_linked_ptr(session), is_canary_job,
354 nudge_location);
355
356 session = NULL;
357 if (!ShouldRunJob(job))
358 return;
292 359
293 if (pending_nudge_.get()) { 360 if (pending_nudge_.get()) {
294 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) 361 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) {
362 VLOG(2) << "Dropping the nudge because we are in backoff";
295 return; 363 return;
364 }
296 365
297 pending_nudge_->session->Coalesce(*session.get()); 366 VLOG(2) << "Coalescing pending nudge";
367 pending_nudge_->session->Coalesce(*(job.session.get()));
298 368
299 if (!IsBackingOff()) { 369 if (!IsBackingOff()) {
370 VLOG(2) << "Dropping a nudge because we are not in backoff and the job"
371 << " was coalesced";
300 return; 372 return;
301 } else { 373 } else {
302 // Re-schedule the current pending nudge. 374 VLOG(2) << "Rescheduling pending nudge";
303 SyncSession* s = pending_nudge_->session.get(); 375 SyncSession* s = pending_nudge_->session.get();
304 session.reset(new SyncSession(s->context(), s->delegate(), s->source(), 376 job.session.reset(new SyncSession(s->context(), s->delegate(),
305 s->routing_info(), s->workers())); 377 s->source(), s->routing_info(), s->workers()));
306 pending_nudge_.reset(); 378 pending_nudge_.reset();
307 } 379 }
308 } 380 }
309 ScheduleSyncSessionJob(delay, NUDGE, session.release(), nudge_location); 381
382 // TODO(lipalani) - pass the job itself to ScheduleSyncSessionJob.
383 ScheduleSyncSessionJob(delay, SyncSessionJob::NUDGE, job.session.release(),
384 nudge_location);
310 } 385 }
311 386
312 // Helper to extract the routing info and workers corresponding to types in 387 // Helper to extract the routing info and workers corresponding to types in
313 // |types| from |registrar|. 388 // |types| from |registrar|.
314 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, 389 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types,
315 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, 390 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes,
316 std::vector<ModelSafeWorker*>* workers) { 391 std::vector<ModelSafeWorker*>* workers) {
317 ModelSafeRoutingInfo r_tmp; 392 ModelSafeRoutingInfo r_tmp;
318 std::vector<ModelSafeWorker*> w_tmp; 393 std::vector<ModelSafeWorker*> w_tmp;
319 registrar->GetModelSafeRoutingInfo(&r_tmp); 394 registrar->GetModelSafeRoutingInfo(&r_tmp);
(...skipping 21 matching lines...) Expand all
341 else 416 else
342 NOTREACHED(); 417 NOTREACHED();
343 } 418 }
344 419
345 void SyncerThread::ScheduleConfig(const ModelTypeBitSet& types) { 420 void SyncerThread::ScheduleConfig(const ModelTypeBitSet& types) {
346 if (!thread_.IsRunning()) { 421 if (!thread_.IsRunning()) {
347 NOTREACHED(); 422 NOTREACHED();
348 return; 423 return;
349 } 424 }
350 425
426 VLOG(2) << "Scheduling a config";
351 ModelSafeRoutingInfo routes; 427 ModelSafeRoutingInfo routes;
352 std::vector<ModelSafeWorker*> workers; 428 std::vector<ModelSafeWorker*> workers;
353 GetModelSafeParamsForTypes(types, session_context_->registrar(), 429 GetModelSafeParamsForTypes(types, session_context_->registrar(),
354 &routes, &workers); 430 &routes, &workers);
355 431
356 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 432 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
357 this, &SyncerThread::ScheduleConfigImpl, routes, workers)); 433 this, &SyncerThread::ScheduleConfigImpl, routes, workers,
434 GetUpdatesCallerInfo::FIRST_UPDATE));
358 } 435 }
359 436
360 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, 437 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info,
361 const std::vector<ModelSafeWorker*>& workers) { 438 const std::vector<ModelSafeWorker*>& workers,
439 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
362 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 440 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
363 441
442 VLOG(2) << "ScheduleConfigImpl...";
364 // TODO(tim): config-specific GetUpdatesCallerInfo value? 443 // TODO(tim): config-specific GetUpdatesCallerInfo value?
365 SyncSession* session = new SyncSession(session_context_.get(), this, 444 SyncSession* session = new SyncSession(session_context_.get(), this,
366 SyncSourceInfo(GetUpdatesCallerInfo::FIRST_UPDATE, 445 SyncSourceInfo(source,
367 syncable::ModelTypePayloadMapFromRoutingInfo( 446 syncable::ModelTypePayloadMapFromRoutingInfo(
368 routing_info, std::string())), 447 routing_info, std::string())),
369 routing_info, workers); 448 routing_info, workers);
370 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session, 449 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0),
371 FROM_HERE); 450 SyncSessionJob::CONFIGURATION, session, FROM_HERE);
372 } 451 }
373 452
374 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, 453 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay,
375 SyncSessionJobPurpose purpose, sessions::SyncSession* session, 454 SyncSessionJob::SyncSessionJobPurpose purpose,
455 sessions::SyncSession* session,
376 const tracked_objects::Location& nudge_location) { 456 const tracked_objects::Location& nudge_location) {
377 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 457 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
378 458
379 SyncSessionJob job = {purpose, TimeTicks::Now() + delay, 459 SyncSessionJob job(purpose, TimeTicks::Now() + delay,
380 make_linked_ptr(session), nudge_location}; 460 make_linked_ptr(session), false, nudge_location);
381 if (purpose == NUDGE) { 461 if (purpose == SyncSessionJob::NUDGE) {
462 VLOG(2) << "Resetting pending_nudge in ScheduleSyncSessionJob";
382 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); 463 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session);
383 pending_nudge_.reset(new SyncSessionJob(job)); 464 pending_nudge_.reset(new SyncSessionJob(job));
384 } 465 }
466 VLOG(2) << "Posting job to execute in DoSyncSessionJob. Job purpose "
467 << job.purpose;
385 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, 468 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this,
386 &SyncerThread::DoSyncSessionJob, job), 469 &SyncerThread::DoSyncSessionJob, job),
387 delay.InMilliseconds()); 470 delay.InMilliseconds());
388 } 471 }
389 472
390 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, 473 void SyncerThread::SetSyncerStepsForPurpose(
474 SyncSessionJob::SyncSessionJobPurpose purpose,
391 SyncerStep* start, SyncerStep* end) { 475 SyncerStep* start, SyncerStep* end) {
392 *end = SYNCER_END; 476 *end = SYNCER_END;
393 switch (purpose) { 477 switch (purpose) {
394 case CONFIGURATION: 478 case SyncSessionJob::CONFIGURATION:
395 *start = DOWNLOAD_UPDATES; 479 *start = DOWNLOAD_UPDATES;
396 *end = APPLY_UPDATES; 480 *end = APPLY_UPDATES;
397 return; 481 return;
398 case CLEAR_USER_DATA: 482 case SyncSessionJob::CLEAR_USER_DATA:
399 *start = CLEAR_PRIVATE_DATA; 483 *start = CLEAR_PRIVATE_DATA;
400 return; 484 return;
401 case NUDGE: 485 case SyncSessionJob::NUDGE:
402 case POLL: 486 case SyncSessionJob::POLL:
403 *start = SYNCER_BEGIN; 487 *start = SYNCER_BEGIN;
404 return; 488 return;
405 default: 489 default:
406 NOTREACHED(); 490 NOTREACHED();
407 } 491 }
408 } 492 }
409 493
410 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { 494 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) {
411 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 495 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
412 if (!ShouldRunJob(job.purpose, job.scheduled_start)) { 496 if (!ShouldRunJob(job))
413 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = "
414 << job.session->source().updates_source;
415 return; 497 return;
416 }
417 498
418 if (job.purpose == NUDGE) { 499 if (job.purpose == SyncSessionJob::NUDGE) {
419 DCHECK(pending_nudge_.get()); 500 DCHECK(pending_nudge_.get());
420 if (pending_nudge_->session != job.session) 501 if (pending_nudge_->session != job.session)
421 return; // Another nudge must have been scheduled in in the meantime. 502 return; // Another nudge must have been scheduled in in the meantime.
422 pending_nudge_.reset(); 503 pending_nudge_.reset();
423 } 504 }
505 VLOG(2) << "DoSyncSessionJob. job purpose " << job.purpose;
424 506
425 SyncerStep begin(SYNCER_BEGIN); 507 SyncerStep begin(SYNCER_BEGIN);
426 SyncerStep end(SYNCER_END); 508 SyncerStep end(SYNCER_END);
427 SetSyncerStepsForPurpose(job.purpose, &begin, &end); 509 SetSyncerStepsForPurpose(job.purpose, &begin, &end);
428 510
429 bool has_more_to_sync = true; 511 bool has_more_to_sync = true;
430 while (ShouldRunJob(job.purpose, job.scheduled_start) && has_more_to_sync) { 512 while (ShouldRunJob(job) && has_more_to_sync) {
431 VLOG(1) << "SyncerThread: Calling SyncShare."; 513 VLOG(2) << "SyncerThread: Calling SyncShare.";
432 // Synchronously perform the sync session from this thread. 514 // Synchronously perform the sync session from this thread.
433 syncer_->SyncShare(job.session.get(), begin, end); 515 syncer_->SyncShare(job.session.get(), begin, end);
434 has_more_to_sync = job.session->HasMoreToSync(); 516 has_more_to_sync = job.session->HasMoreToSync();
435 if (has_more_to_sync) 517 if (has_more_to_sync)
436 job.session->ResetTransientState(); 518 job.session->ResetTransientState();
437 } 519 }
438 VLOG(1) << "SyncerThread: Done SyncShare looping."; 520 VLOG(2) << "SyncerThread: Done SyncShare looping.";
439 FinishSyncSessionJob(job); 521 FinishSyncSessionJob(job);
440 } 522 }
441 523
442 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) { 524 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) {
443 if (old_job.purpose == CONFIGURATION) { 525 if (old_job.purpose == SyncSessionJob::CONFIGURATION) {
444 // Whatever types were part of a configuration task will have had updates 526 // Whatever types were part of a configuration task will have had updates
445 // downloaded. For that reason, we make sure they get recorded in the 527 // downloaded. For that reason, we make sure they get recorded in the
446 // event that they get disabled at a later time. 528 // event that they get disabled at a later time.
447 ModelSafeRoutingInfo r(session_context_->previous_session_routing_info()); 529 ModelSafeRoutingInfo r(session_context_->previous_session_routing_info());
448 if (!r.empty()) { 530 if (!r.empty()) {
449 ModelSafeRoutingInfo temp_r; 531 ModelSafeRoutingInfo temp_r;
450 ModelSafeRoutingInfo old_info(old_job.session->routing_info()); 532 ModelSafeRoutingInfo old_info(old_job.session->routing_info());
451 std::set_union(r.begin(), r.end(), old_info.begin(), old_info.end(), 533 std::set_union(r.begin(), r.end(), old_info.begin(), old_info.end(),
452 std::insert_iterator<ModelSafeRoutingInfo>(temp_r, temp_r.begin())); 534 std::insert_iterator<ModelSafeRoutingInfo>(temp_r, temp_r.begin()));
453 session_context_->set_previous_session_routing_info(temp_r); 535 session_context_->set_previous_session_routing_info(temp_r);
(...skipping 12 matching lines...) Expand all
466 ModelTypePayloadMap::const_iterator iter; 548 ModelTypePayloadMap::const_iterator iter;
467 for (iter = job.session->source().types.begin(); 549 for (iter = job.session->source().types.begin();
468 iter != job.session->source().types.end(); 550 iter != job.session->source().types.end();
469 ++iter) { 551 ++iter) {
470 syncable::PostTimeToTypeHistogram(iter->first, 552 syncable::PostTimeToTypeHistogram(iter->first,
471 now - last_sync_session_end_time_); 553 now - last_sync_session_end_time_);
472 } 554 }
473 } 555 }
474 last_sync_session_end_time_ = now; 556 last_sync_session_end_time_ = now;
475 UpdateCarryoverSessionState(job); 557 UpdateCarryoverSessionState(job);
476 if (IsSyncingCurrentlySilenced()) 558 if (IsSyncingCurrentlySilenced()) {
559 VLOG(2) << "We are currently throttled. So not scheduling the next sync";
560 SaveJob(job);
477 return; // Nothing to do. 561 return; // Nothing to do.
562 }
478 563
479 VLOG(1) << "Updating the next polling time after SyncMain"; 564 VLOG(2) << "Updating the next polling time after SyncMain";
480 ScheduleNextSync(job); 565 ScheduleNextSync(job);
481 } 566 }
482 567
483 void SyncerThread::ScheduleNextSync(const SyncSessionJob& old_job) { 568 void SyncerThread::ScheduleNextSync(const SyncSessionJob& old_job) {
484 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 569 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
485 DCHECK(!old_job.session->HasMoreToSync()); 570 DCHECK(!old_job.session->HasMoreToSync());
486 // Note: |num_server_changes_remaining| > 0 here implies that we received a 571 // Note: |num_server_changes_remaining| > 0 here implies that we received a
487 // broken response while trying to download all updates, because the Syncer 572 // broken response while trying to download all updates, because the Syncer
488 // will loop until this value is exhausted. Also, if unsynced_handles exist 573 // will loop until this value is exhausted. Also, if unsynced_handles exist
489 // but HasMoreToSync is false, this implies that the Syncer determined no 574 // but HasMoreToSync is false, this implies that the Syncer determined no
490 // forward progress was possible at this time (an error, such as an HTTP 575 // forward progress was possible at this time (an error, such as an HTTP
491 // 500, is likely to have occurred during commit). 576 // 500, is likely to have occurred during commit).
492 const bool work_to_do = 577 const bool work_to_do =
493 old_job.session->status_controller()->num_server_changes_remaining() > 0 578 old_job.session->status_controller()->num_server_changes_remaining() > 0
494 || old_job.session->status_controller()->unsynced_handles().size() > 0; 579 || old_job.session->status_controller()->unsynced_handles().size() > 0;
495 VLOG(1) << "syncer has work to do: " << work_to_do; 580 VLOG(2) << "syncer has work to do: " << work_to_do;
496 581
497 AdjustPolling(&old_job); 582 AdjustPolling(&old_job);
498 583
499 // TODO(tim): Old impl had special code if notifications disabled. Needed? 584 // TODO(tim): Old impl had special code if notifications disabled. Needed?
500 if (!work_to_do) { 585 if (!work_to_do) {
501 // Success implies backoff relief. Note that if this was a "one-off" job 586 // Success implies backoff relief. Note that if this was a "one-off" job
502 // (i.e. purpose == CLEAR_USER_DATA), if there was work_to_do before it 587 // (i.e. purpose == SyncSessionJob::CLEAR_USER_DATA), if there was
503 // ran this wont have changed, as jobs like this don't run a full sync 588 // work_to_do before it ran this wont have changed, as jobs like this don't
504 // cycle. So we don't need special code here. 589 // run a full sync cycle. So we don't need special code here.
505 wait_interval_.reset(); 590 wait_interval_.reset();
591 VLOG(2) << "Job suceeded so not scheduling more jobs";
506 return; 592 return;
507 } 593 }
508 594
509 if (old_job.session->source().updates_source == 595 if (old_job.session->source().updates_source ==
510 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION) { 596 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION) {
597 VLOG(2) << "Job failed with source continuation";
511 // We don't seem to have made forward progress. Start or extend backoff. 598 // We don't seem to have made forward progress. Start or extend backoff.
512 HandleConsecutiveContinuationError(old_job); 599 HandleConsecutiveContinuationError(old_job);
513 } else if (IsBackingOff()) { 600 } else if (IsBackingOff()) {
601 VLOG(2) << "A nudge during backoff failed";
514 // We weren't continuing but we're in backoff; must have been a nudge. 602 // We weren't continuing but we're in backoff; must have been a nudge.
515 DCHECK_EQ(NUDGE, old_job.purpose); 603 DCHECK_EQ(SyncSessionJob::NUDGE, old_job.purpose);
516 DCHECK(!wait_interval_->had_nudge); 604 DCHECK(!wait_interval_->had_nudge);
517 wait_interval_->had_nudge = true; 605 wait_interval_->had_nudge = true;
518 wait_interval_->timer.Reset(); 606 wait_interval_->timer.Reset();
519 } else { 607 } else {
608 VLOG(2) << "Failed. Schedule a job with continuation as source";
520 // We weren't continuing and we aren't in backoff. Schedule a normal 609 // We weren't continuing and we aren't in backoff. Schedule a normal
521 // continuation. 610 // continuation.
522 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), NUDGE_SOURCE_CONTINUATION, 611 if (old_job.purpose == SyncSessionJob::CONFIGURATION) {
523 old_job.session->source().types, FROM_HERE); 612 ScheduleConfigImpl(old_job.session->routing_info(),
613 old_job.session->workers(),
614 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION));
615 } else {
616 // For all other purposes(nudge and poll) we schedule a retry nudge.
617 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
618 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION),
619 old_job.session->source().types, false, FROM_HERE);
620 }
524 } 621 }
525 } 622 }
526 623
527 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { 624 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) {
528 DCHECK(thread_.IsRunning()); 625 DCHECK(thread_.IsRunning());
529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 626 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
530 627
531 TimeDelta poll = (!session_context_->notifications_enabled()) ? 628 TimeDelta poll = (!session_context_->notifications_enabled()) ?
532 syncer_short_poll_interval_seconds_ : 629 syncer_short_poll_interval_seconds_ :
533 syncer_long_poll_interval_seconds_; 630 syncer_long_poll_interval_seconds_;
534 bool rate_changed = !poll_timer_.IsRunning() || 631 bool rate_changed = !poll_timer_.IsRunning() ||
535 poll != poll_timer_.GetCurrentDelay(); 632 poll != poll_timer_.GetCurrentDelay();
536 633
537 if (old_job && old_job->purpose != POLL && !rate_changed) 634 if (old_job && old_job->purpose != SyncSessionJob::POLL && !rate_changed)
538 poll_timer_.Reset(); 635 poll_timer_.Reset();
539 636
540 if (!rate_changed) 637 if (!rate_changed)
541 return; 638 return;
542 639
543 // Adjust poll rate. 640 // Adjust poll rate.
544 poll_timer_.Stop(); 641 poll_timer_.Stop();
545 poll_timer_.Start(poll, this, &SyncerThread::PollTimerCallback); 642 poll_timer_.Start(poll, this, &SyncerThread::PollTimerCallback);
546 } 643 }
547 644
548 void SyncerThread::HandleConsecutiveContinuationError( 645 void SyncerThread::HandleConsecutiveContinuationError(
549 const SyncSessionJob& old_job) { 646 const SyncSessionJob& old_job) {
550 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 647 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
551 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning()); 648 // This if conditions should be compiled out in retail builds.
649 if (IsBackingOff()) {
650 DCHECK(wait_interval_->timer.IsRunning() || old_job.is_canary_job);
651 }
552 SyncSession* old = old_job.session.get(); 652 SyncSession* old = old_job.session.get();
553 SyncSession* s(new SyncSession(session_context_.get(), this, 653 SyncSession* s(new SyncSession(session_context_.get(), this,
554 old->source(), old->routing_info(), old->workers())); 654 old->source(), old->routing_info(), old->workers()));
555 TimeDelta length = delay_provider_->GetDelay( 655 TimeDelta length = delay_provider_->GetDelay(
556 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); 656 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1));
657
658 VLOG(2) << "In handle continuation error. Old job purpose is "
659 << old_job.purpose;
660 VLOG(2) << "In Handle continuation error. The time delta(ms) calculated is "
661 << length.InMilliseconds();
662
663 // This will reset the had_nudge variable as well.
557 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, 664 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF,
558 length)); 665 length));
559 SyncSessionJob job = {NUDGE, TimeTicks::Now() + length, 666 if (old_job.purpose == SyncSessionJob::CONFIGURATION) {
560 make_linked_ptr(s), FROM_HERE}; 667 SyncSessionJob job(old_job.purpose, TimeTicks::Now() + length,
561 pending_nudge_.reset(new SyncSessionJob(job)); 668 make_linked_ptr(s), false, FROM_HERE);
669 wait_interval_->pending_configure_job.reset(new SyncSessionJob(job));
670 } else {
671 // We are not in configuration mode. So wait_interval's pending job
672 // should be null.
673 DCHECK(wait_interval_->pending_configure_job.get() == NULL);
674
675 // TODO(lipalani) - handle clear user data.
676 InitOrCoalescePendingJob(old_job);
677 }
562 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); 678 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob);
563 } 679 }
564 680
565 // static 681 // static
566 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { 682 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) {
567 if (last_delay.InSeconds() >= kMaxBackoffSeconds) 683 if (last_delay.InSeconds() >= kMaxBackoffSeconds)
568 return TimeDelta::FromSeconds(kMaxBackoffSeconds); 684 return TimeDelta::FromSeconds(kMaxBackoffSeconds);
569 685
570 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 686 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2
571 int64 backoff_s = 687 int64 backoff_s =
(...skipping 14 matching lines...) Expand all
586 return TimeDelta::FromSeconds(backoff_s); 702 return TimeDelta::FromSeconds(backoff_s);
587 } 703 }
588 704
589 void SyncerThread::Stop() { 705 void SyncerThread::Stop() {
590 syncer_->RequestEarlyExit(); // Safe to call from any thread. 706 syncer_->RequestEarlyExit(); // Safe to call from any thread.
591 session_context_->connection_manager()->RemoveListener(this); 707 session_context_->connection_manager()->RemoveListener(this);
592 thread_.Stop(); 708 thread_.Stop();
593 } 709 }
594 710
595 void SyncerThread::DoCanaryJob() { 711 void SyncerThread::DoCanaryJob() {
596 DCHECK(pending_nudge_.get()); 712 VLOG(2) << "Do canary job";
597 wait_interval_->had_nudge = false; 713 DoPendingJobIfPossible(true);
598 SyncSessionJob copy = *pending_nudge_; 714 }
599 DoSyncSessionJob(copy); 715
716 void SyncerThread::DoPendingJobIfPossible(bool is_canary_job) {
717 SyncSessionJob* job_to_execute = NULL;
718 if (mode_ == CONFIGURATION_MODE && wait_interval_.get()
719 && wait_interval_->pending_configure_job.get()) {
720 VLOG(2) << "Found pending configure job";
721 job_to_execute = wait_interval_->pending_configure_job.get();
722 } else if (pending_nudge_.get()) {
723 VLOG(2) << "Found pending nudge job";
724 // Pending jobs mostly have time from the past. Reset it so this job
725 // will get executed.
726 if (pending_nudge_->scheduled_start < TimeTicks::Now())
727 pending_nudge_->scheduled_start = TimeTicks::Now();
728
729 scoped_ptr<SyncSession> session(CreateSyncSession(
730 pending_nudge_->session->source()));
731
732 // Also the routing info might have been changed since we cached the
733 // pending nudge. Update it by coalescing to the latest.
734 pending_nudge_->session->Coalesce(*(session.get()));
735 // The pending nudge would be cleared in the DoSyncSessionJob function.
736 job_to_execute = pending_nudge_.get();
737 }
738
739 if (job_to_execute != NULL) {
740 VLOG(2) << "Executing pending job";
741 SyncSessionJob copy = *job_to_execute;
742 copy.is_canary_job = is_canary_job;
743 DoSyncSessionJob(copy);
744 }
745 }
746
747 SyncSession* SyncerThread::CreateSyncSession(const SyncSourceInfo& source) {
748 ModelSafeRoutingInfo routes;
749 std::vector<ModelSafeWorker*> workers;
750 session_context_->registrar()->GetModelSafeRoutingInfo(&routes);
751 session_context_->registrar()->GetWorkers(&workers);
752 SyncSourceInfo info(source);
753
754 SyncSession* session(new SyncSession(session_context_.get(), this, info,
755 routes, workers));
756
757 return session;
600 } 758 }
601 759
602 void SyncerThread::PollTimerCallback() { 760 void SyncerThread::PollTimerCallback() {
603 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 761 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
604 ModelSafeRoutingInfo r; 762 ModelSafeRoutingInfo r;
605 std::vector<ModelSafeWorker*> w;
606 session_context_->registrar()->GetModelSafeRoutingInfo(&r);
607 session_context_->registrar()->GetWorkers(&w);
608 ModelTypePayloadMap types_with_payloads = 763 ModelTypePayloadMap types_with_payloads =
609 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); 764 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string());
610 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); 765 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads);
611 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w); 766 SyncSession* s = CreateSyncSession(info);
612 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE); 767 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), SyncSessionJob::POLL, s,
768 FROM_HERE);
613 } 769 }
614 770
615 void SyncerThread::Unthrottle() { 771 void SyncerThread::Unthrottle() {
616 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); 772 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode);
773 VLOG(2) << "Unthrottled..";
774 DoCanaryJob();
617 wait_interval_.reset(); 775 wait_interval_.reset();
618 } 776 }
619 777
620 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { 778 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) {
621 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 779 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
622 session_context_->NotifyListeners(SyncEngineEvent(cause)); 780 session_context_->NotifyListeners(SyncEngineEvent(cause));
623 } 781 }
624 782
625 bool SyncerThread::IsBackingOff() const { 783 bool SyncerThread::IsBackingOff() const {
626 return wait_interval_.get() && wait_interval_->mode == 784 return wait_interval_.get() && wait_interval_->mode ==
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 &SyncerThread::CheckServerConnectionManagerStatus, 820 &SyncerThread::CheckServerConnectionManagerStatus,
663 event.connection_code)); 821 event.connection_code));
664 } 822 }
665 823
666 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { 824 void SyncerThread::set_notifications_enabled(bool notifications_enabled) {
667 session_context_->set_notifications_enabled(notifications_enabled); 825 session_context_->set_notifications_enabled(notifications_enabled);
668 } 826 }
669 827
670 } // s3 828 } // s3
671 } // browser_sync 829 } // browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698