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

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: Fixing CR feedback. 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::SyncSessionJob {
27 SyncSessionJobPurpose purpose;
28 base::TimeTicks scheduled_start;
29 linked_ptr<sessions::SyncSession> session;
30 bool is_canary_job;
31
32 // This is the location the nudge came from. used for debugging purpose.
33 // In case of multiple nudges getting coalesced this stores the first nudge
34 // that came in.
35 tracked_objects::Location nudge_location;
36 };
37
26 struct SyncerThread::WaitInterval { 38 struct SyncerThread::WaitInterval {
27 enum Mode { 39 enum Mode {
28 // A wait interval whose duration has been affected by exponential 40 // A wait interval whose duration has been affected by exponential
29 // backoff. 41 // backoff.
30 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval. 42 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval.
31 EXPONENTIAL_BACKOFF, 43 EXPONENTIAL_BACKOFF,
32 // A server-initiated throttled interval. We do not allow any syncing 44 // A server-initiated throttled interval. We do not allow any syncing
33 // during such an interval. 45 // during such an interval.
34 THROTTLED, 46 THROTTLED,
35 }; 47 };
36 Mode mode; 48 Mode mode;
37 49
38 // This bool is set to true if we have observed a nudge during this 50 // This bool is set to true if we have observed a nudge during this
39 // interval and mode == EXPONENTIAL_BACKOFF. 51 // interval and mode == EXPONENTIAL_BACKOFF.
40 bool had_nudge; 52 bool had_nudge;
41 base::TimeDelta length; 53 base::TimeDelta length;
42 base::OneShotTimer<SyncerThread> timer; 54 base::OneShotTimer<SyncerThread> timer;
55
56 // Configure jobs are saved only when backing off or throttling. So we
57 // expose the pointer here.
58 scoped_ptr<SyncSessionJob> pending_configure_job;
43 WaitInterval(Mode mode, base::TimeDelta length); 59 WaitInterval(Mode mode, base::TimeDelta length);
44 }; 60 };
45 61
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() {} 62 SyncerThread::DelayProvider::DelayProvider() {}
58 SyncerThread::DelayProvider::~DelayProvider() {} 63 SyncerThread::DelayProvider::~DelayProvider() {}
59 64
60 TimeDelta SyncerThread::DelayProvider::GetDelay( 65 TimeDelta SyncerThread::DelayProvider::GetDelay(
61 const base::TimeDelta& last_delay) { 66 const base::TimeDelta& last_delay) {
62 return SyncerThread::GetRecommendedDelay(last_delay); 67 return SyncerThread::GetRecommendedDelay(last_delay);
63 } 68 }
64 69
70 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource(
71 NudgeSource source) {
72 switch (source) {
73 case NUDGE_SOURCE_NOTIFICATION:
74 return GetUpdatesCallerInfo::NOTIFICATION;
75 case NUDGE_SOURCE_LOCAL:
76 return GetUpdatesCallerInfo::LOCAL;
77 case NUDGE_SOURCE_CONTINUATION:
78 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION;
79 case NUDGE_SOURCE_UNKNOWN:
80 return GetUpdatesCallerInfo::UNKNOWN;
81 default:
82 NOTREACHED();
83 return GetUpdatesCallerInfo::UNKNOWN;
84 }
85 }
86
65 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length) 87 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length)
66 : mode(mode), had_nudge(false), length(length) { } 88 : mode(mode), had_nudge(false), length(length) { }
67 89
68 SyncerThread::SyncerThread(sessions::SyncSessionContext* context, 90 SyncerThread::SyncerThread(sessions::SyncSessionContext* context,
69 Syncer* syncer) 91 Syncer* syncer)
70 : thread_("SyncEngine_SyncerThread"), 92 : thread_("SyncEngine_SyncerThread"),
71 syncer_short_poll_interval_seconds_( 93 syncer_short_poll_interval_seconds_(
72 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), 94 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)),
73 syncer_long_poll_interval_seconds_( 95 syncer_long_poll_interval_seconds_(
74 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), 96 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)),
(...skipping 14 matching lines...) Expand all
89 // thinks there is no valid connection as determined by this method, it 111 // 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 112 // 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 113 // will queue up Talk notifications but not actually call SyncShare) until
92 // some external action causes a ServerConnectionManager to broadcast that 114 // some external action causes a ServerConnectionManager to broadcast that
93 // a valid connection has been re-established. 115 // a valid connection has been re-established.
94 if (HttpResponse::CONNECTION_UNAVAILABLE == code || 116 if (HttpResponse::CONNECTION_UNAVAILABLE == code ||
95 HttpResponse::SYNC_AUTH_ERROR == code) { 117 HttpResponse::SYNC_AUTH_ERROR == code) {
96 server_connection_ok_ = false; 118 server_connection_ok_ = false;
97 } else if (HttpResponse::SERVER_CONNECTION_OK == code) { 119 } else if (HttpResponse::SERVER_CONNECTION_OK == code) {
98 server_connection_ok_ = true; 120 server_connection_ok_ = true;
121
122 // run the pending nudges if any.
123 if (mode_ == NORMAL_MODE && pending_nudge_.get()) {
124 pending_nudge_.reset();
125 syncable::ModelTypePayloadMap map;
126 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
127 saved_source_, map, false,
128 FROM_HERE);
129 } else if (wait_interval_.get() &&
130 wait_interval_->pending_configure_job.get()) {
131 DoCanaryJob();
132
133 // Note this means we will restart the wait interval if this job fails.
134 // Which is probably ok since we just got connection.
135 wait_interval_.reset();
136 }
137
99 } 138 }
100 } 139 }
101 140
102 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) { 141 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) {
103 if (!thread_.IsRunning()) { 142 if (!thread_.IsRunning()) {
104 if (!thread_.Start()) { 143 if (!thread_.Start()) {
105 NOTREACHED() << "Unable to start SyncerThread."; 144 NOTREACHED() << "Unable to start SyncerThread.";
106 return; 145 return;
107 } 146 }
108 WatchConnectionManager(); 147 WatchConnectionManager();
(...skipping 24 matching lines...) Expand all
133 172
134 void SyncerThread::StartImpl(Mode mode, 173 void SyncerThread::StartImpl(Mode mode,
135 linked_ptr<ModeChangeCallback> callback) { 174 linked_ptr<ModeChangeCallback> callback) {
136 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 175 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
137 DCHECK(!session_context_->account_name().empty()); 176 DCHECK(!session_context_->account_name().empty());
138 DCHECK(syncer_.get()); 177 DCHECK(syncer_.get());
139 mode_ = mode; 178 mode_ = mode;
140 AdjustPolling(NULL); // Will kick start poll timer if needed. 179 AdjustPolling(NULL); // Will kick start poll timer if needed.
141 if (callback.get()) 180 if (callback.get())
142 callback->Run(); 181 callback->Run();
182
183 if (mode_ == NORMAL_MODE && pending_nudge_.get()) {
184 pending_nudge_.reset();
185 syncable::ModelTypePayloadMap map;
186 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
187 saved_source_, map, false,
188 FROM_HERE);
189 }
143 } 190 }
144 191
145 bool SyncerThread::ShouldRunJob(SyncSessionJobPurpose purpose, 192 SyncerThread::JobProcessDecision SyncerThread::DecideOnJobWhileBackingOff(
146 const TimeTicks& scheduled_start) { 193 const SyncSessionJob& job) {
147 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
148 194
149 // Check wait interval. 195 DCHECK(wait_interval_.get());
150 if (wait_interval_.get()) { 196 DCHECK(job.purpose != 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 197
156 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); 198 if (job.purpose == POLL) {
157 if ((purpose != NUDGE) || wait_interval_->had_nudge) 199 return DROP;
158 return false;
159 } 200 }
160 201
161 // Mode / purpose contract (See 'Mode' enum in header). Don't run jobs that 202 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
162 // were intended for a normal sync if we are in configuration mode, and vice 203 if (wait_interval_->mode == WaitInterval::THROTTLED) {
163 // versa. 204 return SAVE;
164 switch (mode_) {
165 case CONFIGURATION_MODE:
166 if (purpose != CONFIGURATION)
167 return false;
168 break;
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 } 205 }
177 206
178 // Continuation NUDGE tasks have priority over POLLs because they are the 207 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF);
179 // only tasks that trigger exponential backoff, so this prevents them from 208 if (job.purpose == NUDGE) {
180 // being starved from running (e.g. due to a very, very low poll interval, 209 if (mode_ == CONFIGURATION) {
181 // such as 0ms). It's rare that this would ever matter in practice. 210 return SAVE;
182 if (purpose == POLL && (pending_nudge_.get() && 211 }
183 pending_nudge_->session->source().updates_source == 212
184 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION)) { 213 // If we already had one nudge then just drop this nudge. We will retry
214 // later when the timer runs out.
215 if (wait_interval_->had_nudge) {
216 return DROP;
217 } else {
218 // If this is our first nudge during backoff or if our timer ran out
219 // let us retry once more.
tim (not reviewing) 2011/04/08 20:57:41 if the timer ran out.. is that still valid now tha
lipalani1 2011/04/08 23:45:59 Done.
220 return CONTINUE;
221 }
222 } else {
223 // This is a config job.
224 // If our timer ran out then continue.
225 if (job.is_canary_job) {
tim (not reviewing) 2011/04/08 20:57:41 I want to use ternary form here. e.g. return is_ca
lipalani1 2011/04/08 23:45:59 Done.
226 return CONTINUE;
227 } else {
228 return SAVE;
229 }
230 }
231 }
232
233 SyncerThread::JobProcessDecision SyncerThread::ProcessJob(
234 const SyncSessionJob& job) {
235 if (job.purpose == CLEAR_USER_DATA) {
236 return CONTINUE;
237 }
238
239 if (wait_interval_.get()) {
240 return DecideOnJobWhileBackingOff(job);
241 }
242
243 if (mode_ == CONFIGURATION_MODE) {
244 if (job.purpose == NUDGE) {
245 return SAVE;
246 } else if (job.purpose == CONFIGURATION) {
247 return CONTINUE;
248 } else {
249 return DROP;
250 }
251 }
252
253 // We are in normal mode.
254 DCHECK(mode_ == NORMAL_MODE);
255 DCHECK(job.purpose != CONFIGURATION);
256
257 // Freshness condition
258 if (job.scheduled_start < last_sync_session_end_time_) {
259 return DROP;
260 }
261
262 if (server_connection_ok_) {
263 return CONTINUE;
264 }
265
266 return job.purpose == NUDGE ? SAVE : DROP;
267 }
268
269 void SyncerThread::CoalescePendingNudge(const SyncSessionJob& job) {
tim (not reviewing) 2011/04/08 20:57:41 i still see places in the code where Coalesce is c
lipalani1 2011/04/08 23:45:59 Renamed it to updatependingjob. On 2011/04/08 20:5
270 DCHECK(job.purpose != CONFIGURATION);
271 if (pending_nudge_.get() == NULL) {
272 SyncSession* s = job.session.get();
273 scoped_ptr<SyncSession> session(new SyncSession(s->context(),
274 s->delegate(), s->source(), s->routing_info(), s->workers()));
275
276 SyncSessionJob new_job = {NUDGE, job.scheduled_start,
277 make_linked_ptr(session.release()), false, job.nudge_location};
278 pending_nudge_.reset(new SyncSessionJob(new_job));
279
280 return;
281 }
282
283 pending_nudge_->session->Coalesce(*(job.session.get()));
284 pending_nudge_->scheduled_start = job.scheduled_start;
285
286 // Unfortunately the nudge location cannot be modified. So it stores the
287 // location of the first caller.
288 }
289
290 bool SyncerThread::ShouldRunJob(const SyncSessionJob& job) {
291 JobProcessDecision decision = ProcessJob(job);
292 if (decision == DROP) {
185 return false; 293 return false;
186 } 294 }
187 295
188 // Freshness condition. 296 if (decision == CONTINUE) {
189 if (purpose == NUDGE && 297 return true;
tim (not reviewing) 2011/04/08 20:57:41 I still think we should combine these ifs as I sug
lipalani1 2011/04/08 23:45:59 Done.
190 (scheduled_start < last_sync_session_end_time_)) {
191 return false;
192 } 298 }
193 299
194 return server_connection_ok_; 300 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
301 DCHECK_EQ(decision, SAVE);
302 SaveJob(job);
303 return false;
195 } 304 }
196 305
197 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource( 306 void SyncerThread::SaveJob(const SyncSessionJob& job) {
198 NudgeSource source) { 307 DCHECK(job.purpose != CLEAR_USER_DATA);
199 switch (source) { 308 if (job.purpose == NUDGE || job.purpose == POLL) {
200 case NUDGE_SOURCE_NOTIFICATION: 309 CoalescePendingNudge(job);
201 return GetUpdatesCallerInfo::NOTIFICATION; 310 } else {
202 case NUDGE_SOURCE_LOCAL: 311 DCHECK(wait_interval_.get());
203 return GetUpdatesCallerInfo::LOCAL; 312 DCHECK(mode_ == CONFIGURATION_MODE);
204 case NUDGE_SOURCE_CONTINUATION: 313
205 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; 314 SyncSession* old = job.session.get();
206 case NUDGE_SOURCE_UNKNOWN: 315 SyncSession* s(new SyncSession(session_context_.get(), this,
207 return GetUpdatesCallerInfo::UNKNOWN; 316 old->source(), old->routing_info(), old->workers()));
208 default: 317 SyncSessionJob new_job = {job.purpose, TimeTicks::Now(),
209 NOTREACHED(); 318 make_linked_ptr(s), false, job.nudge_location};
210 return GetUpdatesCallerInfo::UNKNOWN; 319 wait_interval_->pending_configure_job.reset(new SyncSessionJob(new_job));
211 } 320 }
212 } 321 }
213 322
214 // Functor for std::find_if to search by ModelSafeGroup. 323 // Functor for std::find_if to search by ModelSafeGroup.
215 struct ModelSafeWorkerGroupIs { 324 struct ModelSafeWorkerGroupIs {
216 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} 325 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {}
217 bool operator()(ModelSafeWorker* w) { 326 bool operator()(ModelSafeWorker* w) {
218 return group == w->GetModelSafeGroup(); 327 return group == w->GetModelSafeGroup();
219 } 328 }
220 ModelSafeGroup group; 329 ModelSafeGroup group;
(...skipping 12 matching lines...) Expand all
233 NudgeSource source, const ModelTypeBitSet& types, 342 NudgeSource source, const ModelTypeBitSet& types,
234 const tracked_objects::Location& nudge_location) { 343 const tracked_objects::Location& nudge_location) {
235 if (!thread_.IsRunning()) { 344 if (!thread_.IsRunning()) {
236 NOTREACHED(); 345 NOTREACHED();
237 return; 346 return;
238 } 347 }
239 348
240 ModelTypePayloadMap types_with_payloads = 349 ModelTypePayloadMap types_with_payloads =
241 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); 350 syncable::ModelTypePayloadMapFromBitSet(types, std::string());
242 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 351 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
243 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 352 this, &SyncerThread::ScheduleNudgeImpl, delay,
244 types_with_payloads, nudge_location)); 353 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
354 nudge_location));
245 } 355 }
246 356
247 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, 357 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay,
248 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 358 NudgeSource source, const ModelTypePayloadMap& types_with_payloads,
249 const tracked_objects::Location& nudge_location) { 359 const tracked_objects::Location& nudge_location) {
250 if (!thread_.IsRunning()) { 360 if (!thread_.IsRunning()) {
251 NOTREACHED(); 361 NOTREACHED();
252 return; 362 return;
253 } 363 }
254 364
255 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 365 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
256 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 366 this, &SyncerThread::ScheduleNudgeImpl, delay,
257 types_with_payloads, nudge_location)); 367 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
368 nudge_location));
258 } 369 }
259 370
260 void SyncerThread::ScheduleClearUserDataImpl() { 371 void SyncerThread::ScheduleClearUserDataImpl() {
261 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 372 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
262 SyncSession* session = new SyncSession(session_context_.get(), this, 373 SyncSession* session = new SyncSession(session_context_.get(), this,
263 SyncSourceInfo(), ModelSafeRoutingInfo(), 374 SyncSourceInfo(), ModelSafeRoutingInfo(),
264 std::vector<ModelSafeWorker*>()); 375 std::vector<ModelSafeWorker*>());
265 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session, 376 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session,
266 FROM_HERE); 377 FROM_HERE);
267 } 378 }
268 379
269 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, 380 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay,
270 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 381 GetUpdatesCallerInfo::GetUpdatesSource source,
271 const tracked_objects::Location& nudge_location) { 382 const ModelTypePayloadMap& types_with_payloads,
383 bool is_canary_job, const tracked_objects::Location& nudge_location) {
272 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 384 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 385
280 // Note we currently nudge for all types regardless of the ones incurring 386 // Note we currently nudge for all types regardless of the ones incurring
281 // the nudge. Doing different would throw off some syncer commands like 387 // the nudge. Doing different would throw off some syncer commands like
282 // CleanupDisabledTypes. We may want to change this in the future. 388 // CleanupDisabledTypes. We may want to change this in the future.
283 ModelSafeRoutingInfo routes; 389 ModelSafeRoutingInfo routes;
284 std::vector<ModelSafeWorker*> workers; 390 std::vector<ModelSafeWorker*> workers;
285 session_context_->registrar()->GetModelSafeRoutingInfo(&routes); 391 session_context_->registrar()->GetModelSafeRoutingInfo(&routes);
286 session_context_->registrar()->GetWorkers(&workers); 392 session_context_->registrar()->GetWorkers(&workers);
287 SyncSourceInfo info(GetUpdatesFromNudgeSource(source), 393 SyncSourceInfo info(source, types_with_payloads);
288 types_with_payloads);
289 394
290 scoped_ptr<SyncSession> session(new SyncSession( 395 SyncSession* session(new SyncSession(
291 session_context_.get(), this, info, routes, workers)); 396 session_context_.get(), this, info, routes, workers));
292 397
398 SyncSessionJob job = {NUDGE, TimeTicks::Now() + delay,
399 make_linked_ptr(session), is_canary_job,
400 nudge_location};
401
402 session = NULL;
403 if (!ShouldRunJob(job)) {
404 return;
405 }
406
293 if (pending_nudge_.get()) { 407 if (pending_nudge_.get()) {
294 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) 408 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1))
295 return; 409 return;
296 410
297 pending_nudge_->session->Coalesce(*session.get()); 411 pending_nudge_->session->Coalesce(*(job.session.get()));
298 412
299 if (!IsBackingOff()) { 413 if (!IsBackingOff()) {
300 return; 414 return;
301 } else { 415 } else {
302 // Re-schedule the current pending nudge. 416 // Re-schedule the current pending nudge.
303 SyncSession* s = pending_nudge_->session.get(); 417 SyncSession* s = pending_nudge_->session.get();
304 session.reset(new SyncSession(s->context(), s->delegate(), s->source(), 418 job.session.reset(new SyncSession(s->context(), s->delegate(),
305 s->routing_info(), s->workers())); 419 s->source(), s->routing_info(), s->workers()));
306 pending_nudge_.reset(); 420 pending_nudge_.reset();
307 } 421 }
308 } 422 }
309 ScheduleSyncSessionJob(delay, NUDGE, session.release(), nudge_location); 423
424 // TODO(lipalani) - pass the job itself to ScheduleSyncSessionJob.
425 ScheduleSyncSessionJob(delay, NUDGE, job.session.release(), nudge_location);
310 } 426 }
311 427
312 // Helper to extract the routing info and workers corresponding to types in 428 // Helper to extract the routing info and workers corresponding to types in
313 // |types| from |registrar|. 429 // |types| from |registrar|.
314 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, 430 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types,
315 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, 431 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes,
316 std::vector<ModelSafeWorker*>* workers) { 432 std::vector<ModelSafeWorker*>* workers) {
317 ModelSafeRoutingInfo r_tmp; 433 ModelSafeRoutingInfo r_tmp;
318 std::vector<ModelSafeWorker*> w_tmp; 434 std::vector<ModelSafeWorker*> w_tmp;
319 registrar->GetModelSafeRoutingInfo(&r_tmp); 435 registrar->GetModelSafeRoutingInfo(&r_tmp);
(...skipping 27 matching lines...) Expand all
347 NOTREACHED(); 463 NOTREACHED();
348 return; 464 return;
349 } 465 }
350 466
351 ModelSafeRoutingInfo routes; 467 ModelSafeRoutingInfo routes;
352 std::vector<ModelSafeWorker*> workers; 468 std::vector<ModelSafeWorker*> workers;
353 GetModelSafeParamsForTypes(types, session_context_->registrar(), 469 GetModelSafeParamsForTypes(types, session_context_->registrar(),
354 &routes, &workers); 470 &routes, &workers);
355 471
356 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 472 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
357 this, &SyncerThread::ScheduleConfigImpl, routes, workers)); 473 this, &SyncerThread::ScheduleConfigImpl, routes, workers,
474 GetUpdatesCallerInfo::FIRST_UPDATE));
358 } 475 }
359 476
360 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, 477 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info,
361 const std::vector<ModelSafeWorker*>& workers) { 478 const std::vector<ModelSafeWorker*>& workers,
479 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
362 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 480 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
363 481
364 // TODO(tim): config-specific GetUpdatesCallerInfo value? 482 // TODO(tim): config-specific GetUpdatesCallerInfo value?
365 SyncSession* session = new SyncSession(session_context_.get(), this, 483 SyncSession* session = new SyncSession(session_context_.get(), this,
366 SyncSourceInfo(GetUpdatesCallerInfo::FIRST_UPDATE, 484 SyncSourceInfo(source,
367 syncable::ModelTypePayloadMapFromRoutingInfo( 485 syncable::ModelTypePayloadMapFromRoutingInfo(
368 routing_info, std::string())), 486 routing_info, std::string())),
369 routing_info, workers); 487 routing_info, workers);
370 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session, 488 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session,
371 FROM_HERE); 489 FROM_HERE);
372 } 490 }
373 491
374 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, 492 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay,
375 SyncSessionJobPurpose purpose, sessions::SyncSession* session, 493 SyncSessionJobPurpose purpose, sessions::SyncSession* session,
376 const tracked_objects::Location& nudge_location) { 494 const tracked_objects::Location& nudge_location) {
377 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 495 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
378 496
379 SyncSessionJob job = {purpose, TimeTicks::Now() + delay, 497 SyncSessionJob job = {purpose, TimeTicks::Now() + delay,
380 make_linked_ptr(session), nudge_location}; 498 make_linked_ptr(session), false, nudge_location};
381 if (purpose == NUDGE) { 499 if (purpose == NUDGE) {
382 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); 500 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session);
383 pending_nudge_.reset(new SyncSessionJob(job)); 501 pending_nudge_.reset(new SyncSessionJob(job));
384 } 502 }
385 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, 503 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this,
386 &SyncerThread::DoSyncSessionJob, job), 504 &SyncerThread::DoSyncSessionJob, job),
387 delay.InMilliseconds()); 505 delay.InMilliseconds());
388 } 506 }
389 507
390 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, 508 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose,
(...skipping 11 matching lines...) Expand all
402 case POLL: 520 case POLL:
403 *start = SYNCER_BEGIN; 521 *start = SYNCER_BEGIN;
404 return; 522 return;
405 default: 523 default:
406 NOTREACHED(); 524 NOTREACHED();
407 } 525 }
408 } 526 }
409 527
410 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { 528 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) {
411 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
412 if (!ShouldRunJob(job.purpose, job.scheduled_start)) { 530 if (!ShouldRunJob(job)) {
413 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = "
414 << job.session->source().updates_source;
415 return; 531 return;
416 } 532 }
417 533
418 if (job.purpose == NUDGE) { 534 if (job.purpose == NUDGE) {
419 DCHECK(pending_nudge_.get()); 535 DCHECK(pending_nudge_.get());
420 if (pending_nudge_->session != job.session) 536 if (pending_nudge_->session != job.session)
421 return; // Another nudge must have been scheduled in in the meantime. 537 return; // Another nudge must have been scheduled in in the meantime.
422 pending_nudge_.reset(); 538 pending_nudge_.reset();
423 } 539 }
424 540
425 SyncerStep begin(SYNCER_BEGIN); 541 SyncerStep begin(SYNCER_BEGIN);
426 SyncerStep end(SYNCER_END); 542 SyncerStep end(SYNCER_END);
427 SetSyncerStepsForPurpose(job.purpose, &begin, &end); 543 SetSyncerStepsForPurpose(job.purpose, &begin, &end);
428 544
429 bool has_more_to_sync = true; 545 bool has_more_to_sync = true;
430 while (ShouldRunJob(job.purpose, job.scheduled_start) && has_more_to_sync) { 546 while (ShouldRunJob(job) && has_more_to_sync) {
431 VLOG(1) << "SyncerThread: Calling SyncShare."; 547 VLOG(1) << "SyncerThread: Calling SyncShare.";
432 // Synchronously perform the sync session from this thread. 548 // Synchronously perform the sync session from this thread.
433 syncer_->SyncShare(job.session.get(), begin, end); 549 syncer_->SyncShare(job.session.get(), begin, end);
434 has_more_to_sync = job.session->HasMoreToSync(); 550 has_more_to_sync = job.session->HasMoreToSync();
435 if (has_more_to_sync) 551 if (has_more_to_sync)
436 job.session->ResetTransientState(); 552 job.session->ResetTransientState();
553 if (IsSyncingCurrentlySilenced()) {
554 DCHECK_NE(job.purpose, CLEAR_USER_DATA);
555 SaveJob(job);
556 }
437 } 557 }
438 VLOG(1) << "SyncerThread: Done SyncShare looping."; 558 VLOG(1) << "SyncerThread: Done SyncShare looping.";
439 FinishSyncSessionJob(job); 559 FinishSyncSessionJob(job);
440 } 560 }
441 561
442 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) { 562 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) {
443 if (old_job.purpose == CONFIGURATION) { 563 if (old_job.purpose == CONFIGURATION) {
444 // Whatever types were part of a configuration task will have had updates 564 // 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 565 // downloaded. For that reason, we make sure they get recorded in the
446 // event that they get disabled at a later time. 566 // event that they get disabled at a later time.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 HandleConsecutiveContinuationError(old_job); 632 HandleConsecutiveContinuationError(old_job);
513 } else if (IsBackingOff()) { 633 } else if (IsBackingOff()) {
514 // We weren't continuing but we're in backoff; must have been a nudge. 634 // We weren't continuing but we're in backoff; must have been a nudge.
515 DCHECK_EQ(NUDGE, old_job.purpose); 635 DCHECK_EQ(NUDGE, old_job.purpose);
516 DCHECK(!wait_interval_->had_nudge); 636 DCHECK(!wait_interval_->had_nudge);
517 wait_interval_->had_nudge = true; 637 wait_interval_->had_nudge = true;
518 wait_interval_->timer.Reset(); 638 wait_interval_->timer.Reset();
519 } else { 639 } else {
520 // We weren't continuing and we aren't in backoff. Schedule a normal 640 // We weren't continuing and we aren't in backoff. Schedule a normal
521 // continuation. 641 // continuation.
522 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), NUDGE_SOURCE_CONTINUATION, 642 if (old_job.purpose == CONFIGURATION) {
523 old_job.session->source().types, FROM_HERE); 643 ScheduleConfigImpl(old_job.session->routing_info(),
644 old_job.session->workers(),
645 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION));
646 } else {
647 // For all other purposes(nudge and poll) we schedule a retry nudge.
648 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
649 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION),
650 old_job.session->source().types, false, FROM_HERE);
651 }
524 } 652 }
525 } 653 }
526 654
527 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { 655 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) {
528 DCHECK(thread_.IsRunning()); 656 DCHECK(thread_.IsRunning());
529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 657 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
530 658
531 TimeDelta poll = (!session_context_->notifications_enabled()) ? 659 TimeDelta poll = (!session_context_->notifications_enabled()) ?
532 syncer_short_poll_interval_seconds_ : 660 syncer_short_poll_interval_seconds_ :
533 syncer_long_poll_interval_seconds_; 661 syncer_long_poll_interval_seconds_;
(...skipping 15 matching lines...) Expand all
549 const SyncSessionJob& old_job) { 677 const SyncSessionJob& old_job) {
550 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 678 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
551 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning()); 679 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning());
552 SyncSession* old = old_job.session.get(); 680 SyncSession* old = old_job.session.get();
553 SyncSession* s(new SyncSession(session_context_.get(), this, 681 SyncSession* s(new SyncSession(session_context_.get(), this,
554 old->source(), old->routing_info(), old->workers())); 682 old->source(), old->routing_info(), old->workers()));
555 TimeDelta length = delay_provider_->GetDelay( 683 TimeDelta length = delay_provider_->GetDelay(
556 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); 684 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1));
557 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, 685 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF,
558 length)); 686 length));
559 SyncSessionJob job = {NUDGE, TimeTicks::Now() + length, 687 if (old_job.purpose == CONFIGURATION) {
560 make_linked_ptr(s), FROM_HERE}; 688 SyncSessionJob job = {old_job.purpose, TimeTicks::Now() + length,
561 pending_nudge_.reset(new SyncSessionJob(job)); 689 make_linked_ptr(s), false, FROM_HERE};
690 wait_interval_->pending_configure_job.reset(new SyncSessionJob(job));
691 } else {
692 // We are not in configuration mode. So wait_interval's pending job
693 // should be null.
694 DCHECK(wait_interval_->pending_configure_job.get() == NULL);
695
696 // No matter what type of job it is.(nudge or poll, it cannot be config or
697 // or clear user data) we are going to treat
tim (not reviewing) 2011/04/08 20:57:41 text formatting not proper here. also why can't it
lipalani1 2011/04/08 23:45:59 added a todo for clearuserdata. On 2011/04/08 20:5
698 // it as nudge when doing exponential back off retries.
699 CoalescePendingNudge(old_job);
700 }
562 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); 701 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob);
563 } 702 }
564 703
565 // static 704 // static
566 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { 705 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) {
567 if (last_delay.InSeconds() >= kMaxBackoffSeconds) 706 if (last_delay.InSeconds() >= kMaxBackoffSeconds)
568 return TimeDelta::FromSeconds(kMaxBackoffSeconds); 707 return TimeDelta::FromSeconds(kMaxBackoffSeconds);
569 708
570 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 709 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2
571 int64 backoff_s = 710 int64 backoff_s =
(...skipping 14 matching lines...) Expand all
586 return TimeDelta::FromSeconds(backoff_s); 725 return TimeDelta::FromSeconds(backoff_s);
587 } 726 }
588 727
589 void SyncerThread::Stop() { 728 void SyncerThread::Stop() {
590 syncer_->RequestEarlyExit(); // Safe to call from any thread. 729 syncer_->RequestEarlyExit(); // Safe to call from any thread.
591 session_context_->connection_manager()->RemoveListener(this); 730 session_context_->connection_manager()->RemoveListener(this);
592 thread_.Stop(); 731 thread_.Stop();
593 } 732 }
594 733
595 void SyncerThread::DoCanaryJob() { 734 void SyncerThread::DoCanaryJob() {
596 DCHECK(pending_nudge_.get());
597 wait_interval_->had_nudge = false; 735 wait_interval_->had_nudge = false;
tim (not reviewing) 2011/04/08 20:57:41 this should not be required anymore.
lipalani1 2011/04/08 23:45:59 This is the only place it gets cleared out. We wan
598 SyncSessionJob copy = *pending_nudge_; 736
737 // We should have one of 2 things. Otherwise we shouldnt be running the timer.
738 DCHECK(wait_interval_->pending_configure_job.get() || pending_nudge_.get());
739 if (mode_ == CONFIGURATION_MODE) {
740 if (wait_interval_->pending_configure_job.get()) {
741 ScheduleCanaryJob(wait_interval_->pending_configure_job.get());
742 wait_interval_->pending_configure_job.reset();
743 }
744 } else {
745 if (pending_nudge_.get()) {
746 ScheduleCanaryJob(pending_nudge_.get());
747 }
748 }
749 }
750
751 void SyncerThread::ScheduleCanaryJob(SyncSessionJob* job) {
tim (not reviewing) 2011/04/08 20:57:41 this function is exactly what DoCanaryJob did befo
lipalani1 2011/04/08 23:45:59 Sorry this was part of the unfinished refactoring
752 DCHECK(job);
753 SyncSessionJob copy = *job;
754 copy.is_canary_job = true;
599 DoSyncSessionJob(copy); 755 DoSyncSessionJob(copy);
600 } 756 }
601 757
602 void SyncerThread::PollTimerCallback() { 758 void SyncerThread::PollTimerCallback() {
603 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 759 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
604 ModelSafeRoutingInfo r; 760 ModelSafeRoutingInfo r;
605 std::vector<ModelSafeWorker*> w; 761 std::vector<ModelSafeWorker*> w;
606 session_context_->registrar()->GetModelSafeRoutingInfo(&r); 762 session_context_->registrar()->GetModelSafeRoutingInfo(&r);
607 session_context_->registrar()->GetWorkers(&w); 763 session_context_->registrar()->GetWorkers(&w);
608 ModelTypePayloadMap types_with_payloads = 764 ModelTypePayloadMap types_with_payloads =
609 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); 765 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string());
610 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); 766 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads);
611 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w); 767 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w);
612 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE); 768 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, 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 DoCanaryJob();
617 wait_interval_.reset(); 774 wait_interval_.reset();
618 } 775 }
619 776
620 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { 777 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) {
621 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 778 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
622 session_context_->NotifyListeners(SyncEngineEvent(cause)); 779 session_context_->NotifyListeners(SyncEngineEvent(cause));
623 } 780 }
624 781
625 bool SyncerThread::IsBackingOff() const { 782 bool SyncerThread::IsBackingOff() const {
626 return wait_interval_.get() && wait_interval_->mode == 783 return wait_interval_.get() && wait_interval_->mode ==
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 &SyncerThread::CheckServerConnectionManagerStatus, 819 &SyncerThread::CheckServerConnectionManagerStatus,
663 event.connection_code)); 820 event.connection_code));
664 } 821 }
665 822
666 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { 823 void SyncerThread::set_notifications_enabled(bool notifications_enabled) {
667 session_context_->set_notifications_enabled(notifications_enabled); 824 session_context_->set_notifications_enabled(notifications_enabled);
668 } 825 }
669 826
670 } // s3 827 } // s3
671 } // browser_sync 828 } // browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698