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

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 a typo. 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
« no previous file with comments | « chrome/browser/sync/engine/syncer_thread2.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 scoped_ptr<SyncSessionJob> pending_job;
43 WaitInterval(Mode mode, base::TimeDelta length); 56 WaitInterval(Mode mode, base::TimeDelta length);
44 }; 57 };
45 58
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() {} 59 SyncerThread::DelayProvider::DelayProvider() {}
58 SyncerThread::DelayProvider::~DelayProvider() {} 60 SyncerThread::DelayProvider::~DelayProvider() {}
59 61
60 TimeDelta SyncerThread::DelayProvider::GetDelay( 62 TimeDelta SyncerThread::DelayProvider::GetDelay(
61 const base::TimeDelta& last_delay) { 63 const base::TimeDelta& last_delay) {
62 return SyncerThread::GetRecommendedDelay(last_delay); 64 return SyncerThread::GetRecommendedDelay(last_delay);
63 } 65 }
64 66
67 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource(
68 NudgeSource source) {
69 switch (source) {
70 case NUDGE_SOURCE_NOTIFICATION:
71 return GetUpdatesCallerInfo::NOTIFICATION;
72 case NUDGE_SOURCE_LOCAL:
73 return GetUpdatesCallerInfo::LOCAL;
74 case NUDGE_SOURCE_CONTINUATION:
75 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION;
76 case NUDGE_SOURCE_UNKNOWN:
77 return GetUpdatesCallerInfo::UNKNOWN;
78 default:
79 NOTREACHED();
80 return GetUpdatesCallerInfo::UNKNOWN;
81 }
82 }
83
65 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length) 84 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length)
66 : mode(mode), had_nudge(false), length(length) { } 85 : mode(mode), had_nudge(false), length(length) { }
67 86
68 SyncerThread::SyncerThread(sessions::SyncSessionContext* context, 87 SyncerThread::SyncerThread(sessions::SyncSessionContext* context,
69 Syncer* syncer) 88 Syncer* syncer)
70 : thread_("SyncEngine_SyncerThread"), 89 : thread_("SyncEngine_SyncerThread"),
71 syncer_short_poll_interval_seconds_( 90 syncer_short_poll_interval_seconds_(
72 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), 91 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)),
73 syncer_long_poll_interval_seconds_( 92 syncer_long_poll_interval_seconds_(
74 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), 93 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)),
75 mode_(NORMAL_MODE), 94 mode_(NORMAL_MODE),
76 server_connection_ok_(false), 95 server_connection_ok_(false),
77 delay_provider_(new DelayProvider()), 96 delay_provider_(new DelayProvider()),
78 syncer_(syncer), 97 syncer_(syncer),
79 session_context_(context) { 98 session_context_(context),
99 saved_nudge_(false),
100 saved_source_(GetUpdatesCallerInfo::UNKNOWN) {
80 } 101 }
81 102
82 SyncerThread::~SyncerThread() { 103 SyncerThread::~SyncerThread() {
83 DCHECK(!thread_.IsRunning()); 104 DCHECK(!thread_.IsRunning());
84 } 105 }
85 106
86 void SyncerThread::CheckServerConnectionManagerStatus( 107 void SyncerThread::CheckServerConnectionManagerStatus(
87 HttpResponse::ServerConnectionCode code) { 108 HttpResponse::ServerConnectionCode code) {
88 // Note, be careful when adding cases here because if the SyncerThread 109 // Note, be careful when adding cases here because if the SyncerThread
89 // thinks there is no valid connection as determined by this method, it 110 // thinks there is no valid connection as determined by this method, it
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 154
134 void SyncerThread::StartImpl(Mode mode, 155 void SyncerThread::StartImpl(Mode mode,
135 linked_ptr<ModeChangeCallback> callback) { 156 linked_ptr<ModeChangeCallback> callback) {
136 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 157 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
137 DCHECK(!session_context_->account_name().empty()); 158 DCHECK(!session_context_->account_name().empty());
138 DCHECK(syncer_.get()); 159 DCHECK(syncer_.get());
139 mode_ = mode; 160 mode_ = mode;
140 AdjustPolling(NULL); // Will kick start poll timer if needed. 161 AdjustPolling(NULL); // Will kick start poll timer if needed.
141 if (callback.get()) 162 if (callback.get())
142 callback->Run(); 163 callback->Run();
164
165 if (mode_ == NORMAL_MODE && saved_nudge_ == true) {
166 syncable::ModelTypePayloadMap map;
167 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
168 saved_source_, map, false,
169 FROM_HERE);
170 }
143 } 171 }
144 172
145 bool SyncerThread::ShouldRunJob(SyncSessionJobPurpose purpose, 173 SyncerThread::JobProcessDecision SyncerThread::DecideOnJobWhileBackingOff(
146 const TimeTicks& scheduled_start) { 174 const SyncSessionJob& job) {
147 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
148 175
149 // Check wait interval. 176 DCHECK(wait_interval_.get());
150 if (wait_interval_.get()) { 177 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 178
156 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); 179 if (job.purpose == POLL) {
157 if ((purpose != NUDGE) || wait_interval_->had_nudge) 180 return DROP;
158 return false;
159 } 181 }
160 182
161 // Mode / purpose contract (See 'Mode' enum in header). Don't run jobs that 183 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
162 // were intended for a normal sync if we are in configuration mode, and vice 184 if (wait_interval_->mode == WaitInterval::THROTTLED) {
163 // versa. 185 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 } 186 }
177 187
178 // Continuation NUDGE tasks have priority over POLLs because they are the 188 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF);
179 // only tasks that trigger exponential backoff, so this prevents them from 189 if (job.purpose == NUDGE) {
180 // being starved from running (e.g. due to a very, very low poll interval, 190 if (mode_ == CONFIGURATION) {
181 // such as 0ms). It's rare that this would ever matter in practice. 191 return SAVE;
182 if (purpose == POLL && (pending_nudge_.get() && 192 }
183 pending_nudge_->session->source().updates_source == 193
184 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION)) { 194 // If we already had one nudge then just drop this nudge. We will retry
195 // later when the timer runs out.
196 if (wait_interval_->had_nudge) {
197 return DROP;
198 } else {
199 // If this is our first nudge during backoff or if our timer ran out
200 // let us retry once more.
201 return CONTINUE;
202 }
203 } else {
204 // This is a config job.
205 // If our timer ran out then continue.
tim (not reviewing) 2011/04/08 03:37:46 this can go on above line.
lipalani1 2011/04/08 18:40:23 Done.
206 if (job.is_canary_job) {
207 return CONTINUE;
208 } else {
209 return SAVE;
210 }
211 }
212 }
213
214 SyncerThread::JobProcessDecision SyncerThread::ProcessJob(
tim (not reviewing) 2011/04/08 03:37:46 Perhaps MakeJobDecision or DecideOnJob Process is
lipalani1 2011/04/08 18:40:23 Done.
215 const SyncSessionJob& job) {
216 if (job.purpose == CLEAR_USER_DATA) {
tim (not reviewing) 2011/04/08 03:37:46 this file in general follows the "no { } on single
lipalani1 2011/04/08 18:40:23 Left as such if the if had an else clause. Otherwi
217 return CONTINUE;
218 }
219
220 if (wait_interval_.get()) {
221 return DecideOnJobWhileBackingOff(job);
tim (not reviewing) 2011/04/08 03:37:46 this method could be named better e.g. not 'Backin
lipalani1 2011/04/08 18:40:23 Done.
222 }
223
224 if (mode_ == CONFIGURATION_MODE) {
225 if (job.purpose == NUDGE) {
226 return SAVE;
227 } else if (job.purpose == CONFIGURATION) {
228 return CONTINUE;
229 } else {
230 return DROP;
231 }
232 }
233
234 // We are in normal mode.
235 DCHECK(mode_ == NORMAL_MODE);
236 DCHECK(job.purpose != CONFIGURATION);
237
238 // Freshness condition
239 if (job.scheduled_start < last_sync_session_end_time_) {
240 return DROP;
241 }
242
243 if (server_connection_ok_) {
244 return CONTINUE;
245 }
246
247 job.purpose == NUDGE ? SAVE : DROP;
248 }
249
250 bool SyncerThread::ShouldRunJob(const SyncSessionJob& job) {
251 JobProcessDecision decision = ProcessJob(job);
252 if (decision == DROP) {
tim (not reviewing) 2011/04/08 03:37:46 I'd collapse these 2 if statements into if (decis
lipalani1 2011/04/08 18:40:23 I am OK with changing it. But readability wise thi
185 return false; 253 return false;
186 } 254 }
187 255
188 // Freshness condition. 256 if (decision == CONTINUE) {
189 if (purpose == NUDGE && 257 return true;
190 (scheduled_start < last_sync_session_end_time_)) {
191 return false;
192 } 258 }
193 259
194 return server_connection_ok_; 260 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
195 } 261 DCHECK_EQ(decision, SAVE);
262 if (job.purpose == NUDGE) {
263 saved_nudge_ = true;
264 saved_source_ = job.session->source().updates_source;
265 } else {
266 DCHECK(wait_interval_.get());
267 DCHECK(mode_ == CONFIGURATION_MODE);
tim (not reviewing) 2011/04/08 03:37:46 how do we know this??
lipalani1 2011/04/08 18:40:23 If the purpose is config then we should be in conf
196 268
197 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource( 269 SyncSession* old = job.session.get();
198 NudgeSource source) { 270 SyncSession* s(new SyncSession(session_context_.get(), this,
199 switch (source) { 271 old->source(), old->routing_info(), old->workers()));
200 case NUDGE_SOURCE_NOTIFICATION: 272 SyncSessionJob new_job = {job.purpose, TimeTicks::Now(),
201 return GetUpdatesCallerInfo::NOTIFICATION; 273 make_linked_ptr(s), false, job.nudge_location};
202 case NUDGE_SOURCE_LOCAL: 274 wait_interval_->pending_job.reset(new SyncSessionJob(new_job));
203 return GetUpdatesCallerInfo::LOCAL;
204 case NUDGE_SOURCE_CONTINUATION:
205 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION;
206 case NUDGE_SOURCE_UNKNOWN:
207 return GetUpdatesCallerInfo::UNKNOWN;
208 default:
209 NOTREACHED();
210 return GetUpdatesCallerInfo::UNKNOWN;
211 } 275 }
276 return false;
212 } 277 }
213 278
214 // Functor for std::find_if to search by ModelSafeGroup. 279 // Functor for std::find_if to search by ModelSafeGroup.
215 struct ModelSafeWorkerGroupIs { 280 struct ModelSafeWorkerGroupIs {
216 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} 281 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {}
217 bool operator()(ModelSafeWorker* w) { 282 bool operator()(ModelSafeWorker* w) {
218 return group == w->GetModelSafeGroup(); 283 return group == w->GetModelSafeGroup();
219 } 284 }
220 ModelSafeGroup group; 285 ModelSafeGroup group;
221 }; 286 };
(...skipping 11 matching lines...) Expand all
233 NudgeSource source, const ModelTypeBitSet& types, 298 NudgeSource source, const ModelTypeBitSet& types,
234 const tracked_objects::Location& nudge_location) { 299 const tracked_objects::Location& nudge_location) {
235 if (!thread_.IsRunning()) { 300 if (!thread_.IsRunning()) {
236 NOTREACHED(); 301 NOTREACHED();
237 return; 302 return;
238 } 303 }
239 304
240 ModelTypePayloadMap types_with_payloads = 305 ModelTypePayloadMap types_with_payloads =
241 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); 306 syncable::ModelTypePayloadMapFromBitSet(types, std::string());
242 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 307 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
243 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 308 this, &SyncerThread::ScheduleNudgeImpl, delay,
244 types_with_payloads, nudge_location)); 309 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
310 nudge_location));
245 } 311 }
246 312
247 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, 313 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay,
248 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 314 NudgeSource source, const ModelTypePayloadMap& types_with_payloads,
249 const tracked_objects::Location& nudge_location) { 315 const tracked_objects::Location& nudge_location) {
250 if (!thread_.IsRunning()) { 316 if (!thread_.IsRunning()) {
251 NOTREACHED(); 317 NOTREACHED();
252 return; 318 return;
253 } 319 }
254 320
255 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 321 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
256 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 322 this, &SyncerThread::ScheduleNudgeImpl, delay,
257 types_with_payloads, nudge_location)); 323 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
324 nudge_location));
258 } 325 }
259 326
260 void SyncerThread::ScheduleClearUserDataImpl() { 327 void SyncerThread::ScheduleClearUserDataImpl() {
261 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 328 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
262 SyncSession* session = new SyncSession(session_context_.get(), this, 329 SyncSession* session = new SyncSession(session_context_.get(), this,
263 SyncSourceInfo(), ModelSafeRoutingInfo(), 330 SyncSourceInfo(), ModelSafeRoutingInfo(),
264 std::vector<ModelSafeWorker*>()); 331 std::vector<ModelSafeWorker*>());
265 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session, 332 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session,
266 FROM_HERE); 333 FROM_HERE);
267 } 334 }
268 335
269 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, 336 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay,
270 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 337 GetUpdatesCallerInfo::GetUpdatesSource source,
271 const tracked_objects::Location& nudge_location) { 338 const ModelTypePayloadMap& types_with_payloads,
339 bool is_canary_job, const tracked_objects::Location& nudge_location) {
272 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 340 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 341
280 // Note we currently nudge for all types regardless of the ones incurring 342 // Note we currently nudge for all types regardless of the ones incurring
281 // the nudge. Doing different would throw off some syncer commands like 343 // the nudge. Doing different would throw off some syncer commands like
282 // CleanupDisabledTypes. We may want to change this in the future. 344 // CleanupDisabledTypes. We may want to change this in the future.
283 ModelSafeRoutingInfo routes; 345 ModelSafeRoutingInfo routes;
284 std::vector<ModelSafeWorker*> workers; 346 std::vector<ModelSafeWorker*> workers;
285 session_context_->registrar()->GetModelSafeRoutingInfo(&routes); 347 session_context_->registrar()->GetModelSafeRoutingInfo(&routes);
286 session_context_->registrar()->GetWorkers(&workers); 348 session_context_->registrar()->GetWorkers(&workers);
287 SyncSourceInfo info(GetUpdatesFromNudgeSource(source), 349 SyncSourceInfo info(source, types_with_payloads);
288 types_with_payloads);
289 350
290 scoped_ptr<SyncSession> session(new SyncSession( 351 SyncSession* session(new SyncSession(
291 session_context_.get(), this, info, routes, workers)); 352 session_context_.get(), this, info, routes, workers));
292 353
354 SyncSessionJob job = {NUDGE, TimeTicks::Now() + delay,
355 make_linked_ptr(session), is_canary_job,
356 nudge_location};
357
358 session = NULL;
tim (not reviewing) 2011/04/08 03:37:46 what's this for?
lipalani1 2011/04/08 18:40:23 So that nobody reuses session. I can put it in a s
359 if (!ShouldRunJob(job)) {
360 return;
361 }
362
293 if (pending_nudge_.get()) { 363 if (pending_nudge_.get()) {
294 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) 364 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1))
295 return; 365 return;
296 366
297 pending_nudge_->session->Coalesce(*session.get()); 367 pending_nudge_->session->Coalesce(*(job.session.get()));
298 368
299 if (!IsBackingOff()) { 369 if (!IsBackingOff()) {
300 return; 370 return;
301 } else { 371 } else {
302 // Re-schedule the current pending nudge. 372 // Re-schedule the current pending nudge.
303 SyncSession* s = pending_nudge_->session.get(); 373 SyncSession* s = pending_nudge_->session.get();
304 session.reset(new SyncSession(s->context(), s->delegate(), s->source(), 374 job.session.reset(new SyncSession(s->context(), s->delegate(),
305 s->routing_info(), s->workers())); 375 s->source(), s->routing_info(), s->workers()));
306 pending_nudge_.reset(); 376 pending_nudge_.reset();
307 } 377 }
308 } 378 }
309 ScheduleSyncSessionJob(delay, NUDGE, session.release(), nudge_location); 379
380 // TODO(lipalani) - pass the job itself to ScheduleSyncSessionJob.
381 ScheduleSyncSessionJob(delay, NUDGE, job.session.release(), nudge_location);
310 } 382 }
311 383
312 // Helper to extract the routing info and workers corresponding to types in 384 // Helper to extract the routing info and workers corresponding to types in
313 // |types| from |registrar|. 385 // |types| from |registrar|.
314 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, 386 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types,
315 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, 387 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes,
316 std::vector<ModelSafeWorker*>* workers) { 388 std::vector<ModelSafeWorker*>* workers) {
317 ModelSafeRoutingInfo r_tmp; 389 ModelSafeRoutingInfo r_tmp;
318 std::vector<ModelSafeWorker*> w_tmp; 390 std::vector<ModelSafeWorker*> w_tmp;
319 registrar->GetModelSafeRoutingInfo(&r_tmp); 391 registrar->GetModelSafeRoutingInfo(&r_tmp);
(...skipping 27 matching lines...) Expand all
347 NOTREACHED(); 419 NOTREACHED();
348 return; 420 return;
349 } 421 }
350 422
351 ModelSafeRoutingInfo routes; 423 ModelSafeRoutingInfo routes;
352 std::vector<ModelSafeWorker*> workers; 424 std::vector<ModelSafeWorker*> workers;
353 GetModelSafeParamsForTypes(types, session_context_->registrar(), 425 GetModelSafeParamsForTypes(types, session_context_->registrar(),
354 &routes, &workers); 426 &routes, &workers);
355 427
356 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 428 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
357 this, &SyncerThread::ScheduleConfigImpl, routes, workers)); 429 this, &SyncerThread::ScheduleConfigImpl, routes, workers,
430 GetUpdatesCallerInfo::FIRST_UPDATE));
358 } 431 }
359 432
360 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, 433 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info,
361 const std::vector<ModelSafeWorker*>& workers) { 434 const std::vector<ModelSafeWorker*>& workers,
435 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
362 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 436 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
363 437
364 // TODO(tim): config-specific GetUpdatesCallerInfo value? 438 // TODO(tim): config-specific GetUpdatesCallerInfo value?
365 SyncSession* session = new SyncSession(session_context_.get(), this, 439 SyncSession* session = new SyncSession(session_context_.get(), this,
366 SyncSourceInfo(GetUpdatesCallerInfo::FIRST_UPDATE, 440 SyncSourceInfo(source,
367 syncable::ModelTypePayloadMapFromRoutingInfo( 441 syncable::ModelTypePayloadMapFromRoutingInfo(
368 routing_info, std::string())), 442 routing_info, std::string())),
369 routing_info, workers); 443 routing_info, workers);
370 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session, 444 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session,
371 FROM_HERE); 445 FROM_HERE);
372 } 446 }
373 447
374 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, 448 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay,
375 SyncSessionJobPurpose purpose, sessions::SyncSession* session, 449 SyncSessionJobPurpose purpose, sessions::SyncSession* session,
376 const tracked_objects::Location& nudge_location) { 450 const tracked_objects::Location& nudge_location) {
377 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 451 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
378 452
379 SyncSessionJob job = {purpose, TimeTicks::Now() + delay, 453 SyncSessionJob job = {purpose, TimeTicks::Now() + delay,
380 make_linked_ptr(session), nudge_location}; 454 make_linked_ptr(session), false, nudge_location};
381 if (purpose == NUDGE) { 455 if (purpose == NUDGE) {
382 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); 456 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session);
383 pending_nudge_.reset(new SyncSessionJob(job)); 457 pending_nudge_.reset(new SyncSessionJob(job));
384 } 458 }
385 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, 459 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this,
386 &SyncerThread::DoSyncSessionJob, job), 460 &SyncerThread::DoSyncSessionJob, job),
387 delay.InMilliseconds()); 461 delay.InMilliseconds());
388 } 462 }
389 463
390 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, 464 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose,
(...skipping 11 matching lines...) Expand all
402 case POLL: 476 case POLL:
403 *start = SYNCER_BEGIN; 477 *start = SYNCER_BEGIN;
404 return; 478 return;
405 default: 479 default:
406 NOTREACHED(); 480 NOTREACHED();
407 } 481 }
408 } 482 }
409 483
410 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { 484 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) {
411 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 485 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
412 if (!ShouldRunJob(job.purpose, job.scheduled_start)) { 486 if (!ShouldRunJob(job)) {
413 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = " 487 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = "
414 << job.session->source().updates_source; 488 << job.session->source().updates_source;
415 return; 489 return;
416 } 490 }
417 491
418 if (job.purpose == NUDGE) { 492 if (job.purpose == NUDGE) {
419 DCHECK(pending_nudge_.get()); 493 DCHECK(pending_nudge_.get());
420 if (pending_nudge_->session != job.session) 494 if (pending_nudge_->session != job.session)
421 return; // Another nudge must have been scheduled in in the meantime. 495 return; // Another nudge must have been scheduled in in the meantime.
422 pending_nudge_.reset(); 496 pending_nudge_.reset();
497 saved_nudge_ = false;
423 } 498 }
424 499
425 SyncerStep begin(SYNCER_BEGIN); 500 SyncerStep begin(SYNCER_BEGIN);
426 SyncerStep end(SYNCER_END); 501 SyncerStep end(SYNCER_END);
427 SetSyncerStepsForPurpose(job.purpose, &begin, &end); 502 SetSyncerStepsForPurpose(job.purpose, &begin, &end);
428 503
429 bool has_more_to_sync = true; 504 bool has_more_to_sync = true;
430 while (ShouldRunJob(job.purpose, job.scheduled_start) && has_more_to_sync) { 505 while (ShouldRunJob(job) && has_more_to_sync) {
431 VLOG(1) << "SyncerThread: Calling SyncShare."; 506 VLOG(1) << "SyncerThread: Calling SyncShare.";
432 // Synchronously perform the sync session from this thread. 507 // Synchronously perform the sync session from this thread.
433 syncer_->SyncShare(job.session.get(), begin, end); 508 syncer_->SyncShare(job.session.get(), begin, end);
434 has_more_to_sync = job.session->HasMoreToSync(); 509 has_more_to_sync = job.session->HasMoreToSync();
435 if (has_more_to_sync) 510 if (has_more_to_sync)
436 job.session->ResetTransientState(); 511 job.session->ResetTransientState();
437 } 512 }
438 VLOG(1) << "SyncerThread: Done SyncShare looping."; 513 VLOG(1) << "SyncerThread: Done SyncShare looping.";
439 FinishSyncSessionJob(job); 514 FinishSyncSessionJob(job);
440 } 515 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 HandleConsecutiveContinuationError(old_job); 587 HandleConsecutiveContinuationError(old_job);
513 } else if (IsBackingOff()) { 588 } else if (IsBackingOff()) {
514 // We weren't continuing but we're in backoff; must have been a nudge. 589 // We weren't continuing but we're in backoff; must have been a nudge.
515 DCHECK_EQ(NUDGE, old_job.purpose); 590 DCHECK_EQ(NUDGE, old_job.purpose);
516 DCHECK(!wait_interval_->had_nudge); 591 DCHECK(!wait_interval_->had_nudge);
517 wait_interval_->had_nudge = true; 592 wait_interval_->had_nudge = true;
518 wait_interval_->timer.Reset(); 593 wait_interval_->timer.Reset();
519 } else { 594 } else {
520 // We weren't continuing and we aren't in backoff. Schedule a normal 595 // We weren't continuing and we aren't in backoff. Schedule a normal
521 // continuation. 596 // continuation.
522 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), NUDGE_SOURCE_CONTINUATION, 597 if (old_job.purpose == NUDGE) {
523 old_job.session->source().types, FROM_HERE); 598 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
599 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION),
600 old_job.session->source().types, false, FROM_HERE);
601 } else if (old_job.purpose == CONFIGURATION) {
602 ScheduleConfigImpl(old_job.session->routing_info(),
603 old_job.session->workers(),
604 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION));
605 } // Drop the rest.
524 } 606 }
525 } 607 }
526 608
527 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { 609 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) {
528 DCHECK(thread_.IsRunning()); 610 DCHECK(thread_.IsRunning());
529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 611 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
530 612
531 TimeDelta poll = (!session_context_->notifications_enabled()) ? 613 TimeDelta poll = (!session_context_->notifications_enabled()) ?
532 syncer_short_poll_interval_seconds_ : 614 syncer_short_poll_interval_seconds_ :
533 syncer_long_poll_interval_seconds_; 615 syncer_long_poll_interval_seconds_;
(...skipping 15 matching lines...) Expand all
549 const SyncSessionJob& old_job) { 631 const SyncSessionJob& old_job) {
550 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 632 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
551 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning()); 633 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning());
552 SyncSession* old = old_job.session.get(); 634 SyncSession* old = old_job.session.get();
553 SyncSession* s(new SyncSession(session_context_.get(), this, 635 SyncSession* s(new SyncSession(session_context_.get(), this,
554 old->source(), old->routing_info(), old->workers())); 636 old->source(), old->routing_info(), old->workers()));
555 TimeDelta length = delay_provider_->GetDelay( 637 TimeDelta length = delay_provider_->GetDelay(
556 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); 638 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1));
557 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, 639 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF,
558 length)); 640 length));
559 SyncSessionJob job = {NUDGE, TimeTicks::Now() + length, 641 if (old_job.purpose == CONFIGURATION) {
560 make_linked_ptr(s), FROM_HERE}; 642 SyncSessionJob job = {old_job.purpose, TimeTicks::Now() + length,
561 pending_nudge_.reset(new SyncSessionJob(job)); 643 make_linked_ptr(s), false, FROM_HERE};
644 wait_interval_->pending_job.reset(new SyncSessionJob(job));
645 } else {
646 // No matter what type of job it is.(nudge or poll), we are going to treat
647 // it as nudge when doing exponential back off retries.
648 saved_nudge_ = true;
649 saved_source_ = old_job.session->source().updates_source;
650 wait_interval_->pending_job.reset();
651 }
562 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); 652 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob);
563 } 653 }
564 654
565 // static 655 // static
566 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { 656 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) {
567 if (last_delay.InSeconds() >= kMaxBackoffSeconds) 657 if (last_delay.InSeconds() >= kMaxBackoffSeconds)
568 return TimeDelta::FromSeconds(kMaxBackoffSeconds); 658 return TimeDelta::FromSeconds(kMaxBackoffSeconds);
569 659
570 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 660 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2
571 int64 backoff_s = 661 int64 backoff_s =
(...skipping 14 matching lines...) Expand all
586 return TimeDelta::FromSeconds(backoff_s); 676 return TimeDelta::FromSeconds(backoff_s);
587 } 677 }
588 678
589 void SyncerThread::Stop() { 679 void SyncerThread::Stop() {
590 syncer_->RequestEarlyExit(); // Safe to call from any thread. 680 syncer_->RequestEarlyExit(); // Safe to call from any thread.
591 session_context_->connection_manager()->RemoveListener(this); 681 session_context_->connection_manager()->RemoveListener(this);
592 thread_.Stop(); 682 thread_.Stop();
593 } 683 }
594 684
595 void SyncerThread::DoCanaryJob() { 685 void SyncerThread::DoCanaryJob() {
596 DCHECK(pending_nudge_.get());
597 wait_interval_->had_nudge = false; 686 wait_interval_->had_nudge = false;
598 SyncSessionJob copy = *pending_nudge_; 687
599 DoSyncSessionJob(copy); 688 // We should have one of 2 things. Otherwise we shouldnt be running the timer.
689 DCHECK(wait_interval_->pending_job.get() || saved_nudge_);
690 if (mode_ == CONFIGURATION_MODE) {
691 if (wait_interval_->pending_job.get()) {
692 SyncSessionJob copy = *(wait_interval_->pending_job);
693 copy.is_canary_job = true;
694 DoSyncSessionJob(copy);
695 }
696 } else {
697 if (saved_nudge_) {
698 syncable::ModelTypePayloadMap map;
699 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), saved_source_, map, true,
700 FROM_HERE);
701 }
702 }
600 } 703 }
601 704
602 void SyncerThread::PollTimerCallback() { 705 void SyncerThread::PollTimerCallback() {
603 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 706 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
604 ModelSafeRoutingInfo r; 707 ModelSafeRoutingInfo r;
605 std::vector<ModelSafeWorker*> w; 708 std::vector<ModelSafeWorker*> w;
606 session_context_->registrar()->GetModelSafeRoutingInfo(&r); 709 session_context_->registrar()->GetModelSafeRoutingInfo(&r);
607 session_context_->registrar()->GetWorkers(&w); 710 session_context_->registrar()->GetWorkers(&w);
608 ModelTypePayloadMap types_with_payloads = 711 ModelTypePayloadMap types_with_payloads =
609 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); 712 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string());
610 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); 713 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads);
611 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w); 714 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w);
612 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE); 715 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE);
613 } 716 }
614 717
615 void SyncerThread::Unthrottle() { 718 void SyncerThread::Unthrottle() {
616 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); 719 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode);
720 DoCanaryJob();
617 wait_interval_.reset(); 721 wait_interval_.reset();
618 } 722 }
619 723
620 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { 724 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) {
621 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 725 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
622 session_context_->NotifyListeners(SyncEngineEvent(cause)); 726 session_context_->NotifyListeners(SyncEngineEvent(cause));
623 } 727 }
624 728
625 bool SyncerThread::IsBackingOff() const { 729 bool SyncerThread::IsBackingOff() const {
626 return wait_interval_.get() && wait_interval_->mode == 730 return wait_interval_.get() && wait_interval_->mode ==
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 &SyncerThread::CheckServerConnectionManagerStatus, 766 &SyncerThread::CheckServerConnectionManagerStatus,
663 event.connection_code)); 767 event.connection_code));
664 } 768 }
665 769
666 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { 770 void SyncerThread::set_notifications_enabled(bool notifications_enabled) {
667 session_context_->set_notifications_enabled(notifications_enabled); 771 session_context_->set_notifications_enabled(notifications_enabled);
668 } 772 }
669 773
670 } // s3 774 } // s3
671 } // browser_sync 775 } // browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/syncer_thread2.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698