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

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: Fix CR feedback and all the unittests. 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
60 TimeDelta SyncerThread::DelayProvider::GetDelay( 29 TimeDelta SyncerThread::DelayProvider::GetDelay(
61 const base::TimeDelta& last_delay) { 30 const base::TimeDelta& last_delay) {
62 return SyncerThread::GetRecommendedDelay(last_delay); 31 return SyncerThread::GetRecommendedDelay(last_delay);
63 } 32 }
64 33
34 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource(
35 NudgeSource source) {
36 switch (source) {
37 case NUDGE_SOURCE_NOTIFICATION:
38 return GetUpdatesCallerInfo::NOTIFICATION;
39 case NUDGE_SOURCE_LOCAL:
40 return GetUpdatesCallerInfo::LOCAL;
41 case NUDGE_SOURCE_CONTINUATION:
42 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION;
43 case NUDGE_SOURCE_UNKNOWN:
44 return GetUpdatesCallerInfo::UNKNOWN;
45 default:
46 NOTREACHED();
47 return GetUpdatesCallerInfo::UNKNOWN;
48 }
49 }
50
65 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length) 51 SyncerThread::WaitInterval::WaitInterval(Mode mode, TimeDelta length)
66 : mode(mode), had_nudge(false), length(length) { } 52 : mode(mode), had_nudge(false), length(length) { }
67 53
68 SyncerThread::SyncerThread(sessions::SyncSessionContext* context, 54 SyncerThread::SyncerThread(sessions::SyncSessionContext* context,
69 Syncer* syncer) 55 Syncer* syncer)
70 : thread_("SyncEngine_SyncerThread"), 56 : thread_("SyncEngine_SyncerThread"),
71 syncer_short_poll_interval_seconds_( 57 syncer_short_poll_interval_seconds_(
72 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), 58 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)),
73 syncer_long_poll_interval_seconds_( 59 syncer_long_poll_interval_seconds_(
74 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), 60 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)),
(...skipping 14 matching lines...) Expand all
89 // thinks there is no valid connection as determined by this method, it 75 // 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 76 // 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 77 // will queue up Talk notifications but not actually call SyncShare) until
92 // some external action causes a ServerConnectionManager to broadcast that 78 // some external action causes a ServerConnectionManager to broadcast that
93 // a valid connection has been re-established. 79 // a valid connection has been re-established.
94 if (HttpResponse::CONNECTION_UNAVAILABLE == code || 80 if (HttpResponse::CONNECTION_UNAVAILABLE == code ||
95 HttpResponse::SYNC_AUTH_ERROR == code) { 81 HttpResponse::SYNC_AUTH_ERROR == code) {
96 server_connection_ok_ = false; 82 server_connection_ok_ = false;
97 } else if (HttpResponse::SERVER_CONNECTION_OK == code) { 83 } else if (HttpResponse::SERVER_CONNECTION_OK == code) {
98 server_connection_ok_ = true; 84 server_connection_ok_ = true;
85 ExecutePendingJob();
99 } 86 }
100 } 87 }
101 88
102 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) { 89 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) {
103 if (!thread_.IsRunning()) { 90 if (!thread_.IsRunning()) {
104 if (!thread_.Start()) { 91 if (!thread_.Start()) {
105 NOTREACHED() << "Unable to start SyncerThread."; 92 NOTREACHED() << "Unable to start SyncerThread.";
106 return; 93 return;
107 } 94 }
108 WatchConnectionManager(); 95 WatchConnectionManager();
(...skipping 24 matching lines...) Expand all
133 120
134 void SyncerThread::StartImpl(Mode mode, 121 void SyncerThread::StartImpl(Mode mode,
135 linked_ptr<ModeChangeCallback> callback) { 122 linked_ptr<ModeChangeCallback> callback) {
136 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 123 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
137 DCHECK(!session_context_->account_name().empty()); 124 DCHECK(!session_context_->account_name().empty());
138 DCHECK(syncer_.get()); 125 DCHECK(syncer_.get());
139 mode_ = mode; 126 mode_ = mode;
140 AdjustPolling(NULL); // Will kick start poll timer if needed. 127 AdjustPolling(NULL); // Will kick start poll timer if needed.
141 if (callback.get()) 128 if (callback.get())
142 callback->Run(); 129 callback->Run();
130
131 ExecutePendingJob();
tim (not reviewing) 2011/04/09 23:52:58 this kind of looks out of place. at the very leas
lipalani1 2011/04/12 02:33:00 Done.
143 } 132 }
144 133
145 bool SyncerThread::ShouldRunJob(SyncSessionJobPurpose purpose, 134 SyncerThread::JobProcessDecision SyncerThread::DecideWhileInWaitInterval(
146 const TimeTicks& scheduled_start) { 135 const SyncSessionJob& job) {
147 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
148 136
149 // Check wait interval. 137 DCHECK(wait_interval_.get());
150 if (wait_interval_.get()) { 138 DCHECK(job.purpose != CLEAR_USER_DATA);
tim (not reviewing) 2011/04/09 23:52:58 DCHECK_NE
lipalani1 2011/04/12 02:33:00 Done.
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 139
156 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); 140 if (job.purpose == POLL)
157 if ((purpose != NUDGE) || wait_interval_->had_nudge) 141 return DROP;
158 return false;
159 }
160 142
161 // Mode / purpose contract (See 'Mode' enum in header). Don't run jobs that 143 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
162 // were intended for a normal sync if we are in configuration mode, and vice 144 if (wait_interval_->mode == WaitInterval::THROTTLED)
163 // versa. 145 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 }
177 146
178 // Continuation NUDGE tasks have priority over POLLs because they are the 147 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF);
179 // only tasks that trigger exponential backoff, so this prevents them from 148 if (job.purpose == NUDGE) {
180 // being starved from running (e.g. due to a very, very low poll interval, 149 if (mode_ == CONFIGURATION_MODE)
181 // such as 0ms). It's rare that this would ever matter in practice. 150 return SAVE;
182 if (purpose == POLL && (pending_nudge_.get() &&
183 pending_nudge_->session->source().updates_source ==
184 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION)) {
185 return false;
186 }
187 151
188 // Freshness condition. 152 // If we already had one nudge then just drop this nudge. We will retry
189 if (purpose == NUDGE && 153 // later when the timer runs out.
190 (scheduled_start < last_sync_session_end_time_)) { 154 return wait_interval_->had_nudge ? DROP : CONTINUE;
191 return false; 155 } else {
192 } 156 // This is a config job. If our timer ran out then continue else save.
tim (not reviewing) 2011/04/09 23:52:58 Saying it's a config job is fine but I'd avoid ref
lipalani1 2011/04/12 02:33:00 Done.
193 157 return job.is_canary_job ? CONTINUE : SAVE;
194 return server_connection_ok_;
195 }
196
197 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource(
198 NudgeSource source) {
199 switch (source) {
200 case NUDGE_SOURCE_NOTIFICATION:
201 return GetUpdatesCallerInfo::NOTIFICATION;
202 case NUDGE_SOURCE_LOCAL:
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 } 158 }
212 } 159 }
213 160
161 SyncerThread::JobProcessDecision SyncerThread::DecideOnJob(
162 const SyncSessionJob& job) {
163 if (job.purpose == CLEAR_USER_DATA)
164 return CONTINUE;
165
166 if (wait_interval_.get())
167 return DecideWhileInWaitInterval(job);
168
169 if (mode_ == CONFIGURATION_MODE) {
170 if (job.purpose == NUDGE) {
tim (not reviewing) 2011/04/09 23:52:58 remove the { }s from this inner if as they're 1-li
lipalani1 2011/04/12 02:33:00 Done.
171 return SAVE;
172 } else if (job.purpose == CONFIGURATION) {
173 return CONTINUE;
174 } else {
175 return DROP;
176 }
177 }
178
179 // We are in normal mode.
180 DCHECK(mode_ == NORMAL_MODE);
tim (not reviewing) 2011/04/09 23:52:58 DCHECK_EQ
lipalani1 2011/04/12 02:33:00 Done.
181 DCHECK(job.purpose != CONFIGURATION);
tim (not reviewing) 2011/04/09 23:52:58 I don't think this should be a dcheck. The mode c
lipalani1 2011/04/12 02:33:00 Not sure I follow this. Let me talk to you today w
182
183 // Freshness condition
184 if (job.scheduled_start < last_sync_session_end_time_)
185 return DROP;
186
187 if (server_connection_ok_)
188 return CONTINUE;
189
190 return job.purpose == NUDGE ? SAVE : DROP;
191 }
192
193 void SyncerThread::UpdatePendingJob(const SyncSessionJob& job) {
tim (not reviewing) 2011/04/09 23:52:58 I've looked at the callsites of this function and
lipalani1 2011/04/12 02:33:00 There are 3 callsites. 2 of them(from savejob and
194 DCHECK(job.purpose != CONFIGURATION);
195 if (pending_nudge_.get() == NULL) {
196 SyncSession* s = job.session.get();
197 scoped_ptr<SyncSession> session(new SyncSession(s->context(),
198 s->delegate(), s->source(), s->routing_info(), s->workers()));
199
200 SyncSessionJob new_job = {NUDGE, job.scheduled_start,
201 make_linked_ptr(session.release()), false, job.nudge_location};
202 pending_nudge_.reset(new SyncSessionJob(new_job));
203
204 return;
205 }
206
207 pending_nudge_->session->Coalesce(*(job.session.get()));
208 pending_nudge_->scheduled_start = job.scheduled_start;
209
210 // Unfortunately the nudge location cannot be modified. So it stores the
211 // location of the first caller.
212 }
213
214 bool SyncerThread::ShouldRunJob(const SyncSessionJob& job) {
215 JobProcessDecision decision = DecideOnJob(job);
216 if (decision != SAVE)
217 return decision == CONTINUE ? true : false;
tim (not reviewing) 2011/04/09 23:52:58 remove the ternary here, it is redundant to decisi
lipalani1 2011/04/12 02:33:00 ha ha ! may be you got confused as I mentioned!!(o
218
219 DCHECK(job.purpose == NUDGE || job.purpose == CONFIGURATION);
220
221 SaveJob(job);
222 return false;
223 }
224
225 void SyncerThread::SaveJob(const SyncSessionJob& job) {
226 DCHECK(job.purpose != CLEAR_USER_DATA);
227 if (job.purpose == NUDGE || job.purpose == POLL) {
tim (not reviewing) 2011/04/09 23:52:58 We should never want to save a POLL.
lipalani1 2011/04/12 02:33:00 The case here is when in config but a poll comes i
tim (not reviewing) 2011/04/12 06:09:29 In that case I claim we want to drop the poll. We
lipalani1 2011/04/13 00:07:29 Actually the code was already dropping it!! On 201
228 UpdatePendingJob(job);
229 } else {
230 DCHECK(wait_interval_.get());
231 DCHECK(mode_ == CONFIGURATION_MODE);
232
233 SyncSession* old = job.session.get();
234 SyncSession* s(new SyncSession(session_context_.get(), this,
235 old->source(), old->routing_info(), old->workers()));
236 SyncSessionJob new_job = {job.purpose, TimeTicks::Now(),
237 make_linked_ptr(s), false, job.nudge_location};
238 wait_interval_->pending_configure_job.reset(new SyncSessionJob(new_job));
239 }
240 }
241
214 // Functor for std::find_if to search by ModelSafeGroup. 242 // Functor for std::find_if to search by ModelSafeGroup.
215 struct ModelSafeWorkerGroupIs { 243 struct ModelSafeWorkerGroupIs {
216 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} 244 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {}
217 bool operator()(ModelSafeWorker* w) { 245 bool operator()(ModelSafeWorker* w) {
218 return group == w->GetModelSafeGroup(); 246 return group == w->GetModelSafeGroup();
219 } 247 }
220 ModelSafeGroup group; 248 ModelSafeGroup group;
221 }; 249 };
222 250
223 void SyncerThread::ScheduleClearUserData() { 251 void SyncerThread::ScheduleClearUserData() {
224 if (!thread_.IsRunning()) { 252 if (!thread_.IsRunning()) {
225 NOTREACHED(); 253 NOTREACHED();
226 return; 254 return;
227 } 255 }
228 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 256 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
229 this, &SyncerThread::ScheduleClearUserDataImpl)); 257 this, &SyncerThread::ScheduleClearUserDataImpl));
230 } 258 }
231 259
232 void SyncerThread::ScheduleNudge(const TimeDelta& delay, 260 void SyncerThread::ScheduleNudge(const TimeDelta& delay,
233 NudgeSource source, const ModelTypeBitSet& types, 261 NudgeSource source, const ModelTypeBitSet& types,
234 const tracked_objects::Location& nudge_location) { 262 const tracked_objects::Location& nudge_location) {
235 if (!thread_.IsRunning()) { 263 if (!thread_.IsRunning()) {
236 NOTREACHED(); 264 NOTREACHED();
237 return; 265 return;
238 } 266 }
239 267
240 ModelTypePayloadMap types_with_payloads = 268 ModelTypePayloadMap types_with_payloads =
241 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); 269 syncable::ModelTypePayloadMapFromBitSet(types, std::string());
242 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 270 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
243 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 271 this, &SyncerThread::ScheduleNudgeImpl, delay,
244 types_with_payloads, nudge_location)); 272 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
273 nudge_location));
245 } 274 }
246 275
247 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, 276 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay,
248 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 277 NudgeSource source, const ModelTypePayloadMap& types_with_payloads,
249 const tracked_objects::Location& nudge_location) { 278 const tracked_objects::Location& nudge_location) {
250 if (!thread_.IsRunning()) { 279 if (!thread_.IsRunning()) {
251 NOTREACHED(); 280 NOTREACHED();
252 return; 281 return;
253 } 282 }
254 283
255 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 284 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
256 this, &SyncerThread::ScheduleNudgeImpl, delay, source, 285 this, &SyncerThread::ScheduleNudgeImpl, delay,
257 types_with_payloads, nudge_location)); 286 GetUpdatesFromNudgeSource(source), types_with_payloads, false,
287 nudge_location));
258 } 288 }
259 289
260 void SyncerThread::ScheduleClearUserDataImpl() { 290 void SyncerThread::ScheduleClearUserDataImpl() {
261 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 291 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
262 SyncSession* session = new SyncSession(session_context_.get(), this, 292 SyncSession* session = new SyncSession(session_context_.get(), this,
263 SyncSourceInfo(), ModelSafeRoutingInfo(), 293 SyncSourceInfo(), ModelSafeRoutingInfo(),
264 std::vector<ModelSafeWorker*>()); 294 std::vector<ModelSafeWorker*>());
265 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session, 295 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session,
266 FROM_HERE); 296 FROM_HERE);
267 } 297 }
268 298
269 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, 299 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay,
270 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, 300 GetUpdatesCallerInfo::GetUpdatesSource source,
271 const tracked_objects::Location& nudge_location) { 301 const ModelTypePayloadMap& types_with_payloads,
302 bool is_canary_job, const tracked_objects::Location& nudge_location) {
272 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 303 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 304
280 // Note we currently nudge for all types regardless of the ones incurring 305 // Note we currently nudge for all types regardless of the ones incurring
281 // the nudge. Doing different would throw off some syncer commands like 306 // the nudge. Doing different would throw off some syncer commands like
282 // CleanupDisabledTypes. We may want to change this in the future. 307 // CleanupDisabledTypes. We may want to change this in the future.
283 ModelSafeRoutingInfo routes; 308 ModelSafeRoutingInfo routes;
284 std::vector<ModelSafeWorker*> workers; 309 std::vector<ModelSafeWorker*> workers;
285 session_context_->registrar()->GetModelSafeRoutingInfo(&routes); 310 session_context_->registrar()->GetModelSafeRoutingInfo(&routes);
286 session_context_->registrar()->GetWorkers(&workers); 311 session_context_->registrar()->GetWorkers(&workers);
287 SyncSourceInfo info(GetUpdatesFromNudgeSource(source), 312 SyncSourceInfo info(source, types_with_payloads);
288 types_with_payloads);
289 313
290 scoped_ptr<SyncSession> session(new SyncSession( 314 SyncSession* session(new SyncSession(
291 session_context_.get(), this, info, routes, workers)); 315 session_context_.get(), this, info, routes, workers));
292 316
317 SyncSessionJob job = {NUDGE, TimeTicks::Now() + delay,
318 make_linked_ptr(session), is_canary_job,
319 nudge_location};
320
321 session = NULL;
322 if (!ShouldRunJob(job))
323 return;
324
293 if (pending_nudge_.get()) { 325 if (pending_nudge_.get()) {
294 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) 326 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1))
295 return; 327 return;
296 328
297 pending_nudge_->session->Coalesce(*session.get()); 329 UpdatePendingJob(job);
tim (not reviewing) 2011/04/09 23:52:58 I think this was more clear when we just did the c
lipalani1 2011/04/12 02:33:00 Done.
298 330
299 if (!IsBackingOff()) { 331 if (!IsBackingOff()) {
300 return; 332 return;
301 } else { 333 } else {
302 // Re-schedule the current pending nudge. 334 // Re-schedule the current pending nudge.
303 SyncSession* s = pending_nudge_->session.get(); 335 SyncSession* s = pending_nudge_->session.get();
304 session.reset(new SyncSession(s->context(), s->delegate(), s->source(), 336 job.session.reset(new SyncSession(s->context(), s->delegate(),
305 s->routing_info(), s->workers())); 337 s->source(), s->routing_info(), s->workers()));
306 pending_nudge_.reset(); 338 pending_nudge_.reset();
307 } 339 }
308 } 340 }
309 ScheduleSyncSessionJob(delay, NUDGE, session.release(), nudge_location); 341
342 // TODO(lipalani) - pass the job itself to ScheduleSyncSessionJob.
343 ScheduleSyncSessionJob(delay, NUDGE, job.session.release(), nudge_location);
310 } 344 }
311 345
312 // Helper to extract the routing info and workers corresponding to types in 346 // Helper to extract the routing info and workers corresponding to types in
313 // |types| from |registrar|. 347 // |types| from |registrar|.
314 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, 348 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types,
315 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, 349 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes,
316 std::vector<ModelSafeWorker*>* workers) { 350 std::vector<ModelSafeWorker*>* workers) {
317 ModelSafeRoutingInfo r_tmp; 351 ModelSafeRoutingInfo r_tmp;
318 std::vector<ModelSafeWorker*> w_tmp; 352 std::vector<ModelSafeWorker*> w_tmp;
319 registrar->GetModelSafeRoutingInfo(&r_tmp); 353 registrar->GetModelSafeRoutingInfo(&r_tmp);
(...skipping 27 matching lines...) Expand all
347 NOTREACHED(); 381 NOTREACHED();
348 return; 382 return;
349 } 383 }
350 384
351 ModelSafeRoutingInfo routes; 385 ModelSafeRoutingInfo routes;
352 std::vector<ModelSafeWorker*> workers; 386 std::vector<ModelSafeWorker*> workers;
353 GetModelSafeParamsForTypes(types, session_context_->registrar(), 387 GetModelSafeParamsForTypes(types, session_context_->registrar(),
354 &routes, &workers); 388 &routes, &workers);
355 389
356 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( 390 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
357 this, &SyncerThread::ScheduleConfigImpl, routes, workers)); 391 this, &SyncerThread::ScheduleConfigImpl, routes, workers,
392 GetUpdatesCallerInfo::FIRST_UPDATE));
358 } 393 }
359 394
360 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, 395 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info,
361 const std::vector<ModelSafeWorker*>& workers) { 396 const std::vector<ModelSafeWorker*>& workers,
397 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
362 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 398 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
363 399
364 // TODO(tim): config-specific GetUpdatesCallerInfo value? 400 // TODO(tim): config-specific GetUpdatesCallerInfo value?
365 SyncSession* session = new SyncSession(session_context_.get(), this, 401 SyncSession* session = new SyncSession(session_context_.get(), this,
366 SyncSourceInfo(GetUpdatesCallerInfo::FIRST_UPDATE, 402 SyncSourceInfo(source,
367 syncable::ModelTypePayloadMapFromRoutingInfo( 403 syncable::ModelTypePayloadMapFromRoutingInfo(
368 routing_info, std::string())), 404 routing_info, std::string())),
369 routing_info, workers); 405 routing_info, workers);
370 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session, 406 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session,
371 FROM_HERE); 407 FROM_HERE);
372 } 408 }
373 409
374 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, 410 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay,
375 SyncSessionJobPurpose purpose, sessions::SyncSession* session, 411 SyncSessionJobPurpose purpose, sessions::SyncSession* session,
376 const tracked_objects::Location& nudge_location) { 412 const tracked_objects::Location& nudge_location) {
377 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 413 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
378 414
379 SyncSessionJob job = {purpose, TimeTicks::Now() + delay, 415 SyncSessionJob job = {purpose, TimeTicks::Now() + delay,
380 make_linked_ptr(session), nudge_location}; 416 make_linked_ptr(session), false, nudge_location};
381 if (purpose == NUDGE) { 417 if (purpose == NUDGE) {
382 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); 418 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session);
383 pending_nudge_.reset(new SyncSessionJob(job)); 419 pending_nudge_.reset(new SyncSessionJob(job));
384 } 420 }
385 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, 421 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this,
386 &SyncerThread::DoSyncSessionJob, job), 422 &SyncerThread::DoSyncSessionJob, job),
387 delay.InMilliseconds()); 423 delay.InMilliseconds());
388 } 424 }
389 425
390 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, 426 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose,
(...skipping 11 matching lines...) Expand all
402 case POLL: 438 case POLL:
403 *start = SYNCER_BEGIN; 439 *start = SYNCER_BEGIN;
404 return; 440 return;
405 default: 441 default:
406 NOTREACHED(); 442 NOTREACHED();
407 } 443 }
408 } 444 }
409 445
410 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { 446 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) {
411 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 447 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
412 if (!ShouldRunJob(job.purpose, job.scheduled_start)) { 448 if (!ShouldRunJob(job))
413 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = "
414 << job.session->source().updates_source;
415 return; 449 return;
416 }
417 450
418 if (job.purpose == NUDGE) { 451 if (job.purpose == NUDGE) {
419 DCHECK(pending_nudge_.get()); 452 DCHECK(pending_nudge_.get());
420 if (pending_nudge_->session != job.session) 453 if (pending_nudge_->session != job.session)
421 return; // Another nudge must have been scheduled in in the meantime. 454 return; // Another nudge must have been scheduled in in the meantime.
422 pending_nudge_.reset(); 455 pending_nudge_.reset();
423 } 456 }
424 457
425 SyncerStep begin(SYNCER_BEGIN); 458 SyncerStep begin(SYNCER_BEGIN);
426 SyncerStep end(SYNCER_END); 459 SyncerStep end(SYNCER_END);
427 SetSyncerStepsForPurpose(job.purpose, &begin, &end); 460 SetSyncerStepsForPurpose(job.purpose, &begin, &end);
428 461
429 bool has_more_to_sync = true; 462 bool has_more_to_sync = true;
430 while (ShouldRunJob(job.purpose, job.scheduled_start) && has_more_to_sync) { 463 while (ShouldRunJob(job) && has_more_to_sync) {
431 VLOG(1) << "SyncerThread: Calling SyncShare."; 464 VLOG(1) << "SyncerThread: Calling SyncShare.";
432 // Synchronously perform the sync session from this thread. 465 // Synchronously perform the sync session from this thread.
433 syncer_->SyncShare(job.session.get(), begin, end); 466 syncer_->SyncShare(job.session.get(), begin, end);
434 has_more_to_sync = job.session->HasMoreToSync(); 467 has_more_to_sync = job.session->HasMoreToSync();
435 if (has_more_to_sync) 468 if (has_more_to_sync)
436 job.session->ResetTransientState(); 469 job.session->ResetTransientState();
470 if (IsSyncingCurrentlySilenced()) {
471 DCHECK_NE(job.purpose, CLEAR_USER_DATA);
472 SaveJob(job);
tim (not reviewing) 2011/04/09 23:52:58 This if-block seems out of place...we should be do
lipalani1 2011/04/12 02:33:00 Good point. Done. On 2011/04/09 23:52:58, timsteel
473 }
437 } 474 }
438 VLOG(1) << "SyncerThread: Done SyncShare looping."; 475 VLOG(1) << "SyncerThread: Done SyncShare looping.";
439 FinishSyncSessionJob(job); 476 FinishSyncSessionJob(job);
440 } 477 }
441 478
442 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) { 479 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) {
443 if (old_job.purpose == CONFIGURATION) { 480 if (old_job.purpose == CONFIGURATION) {
444 // Whatever types were part of a configuration task will have had updates 481 // 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 482 // downloaded. For that reason, we make sure they get recorded in the
446 // event that they get disabled at a later time. 483 // 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); 549 HandleConsecutiveContinuationError(old_job);
513 } else if (IsBackingOff()) { 550 } else if (IsBackingOff()) {
514 // We weren't continuing but we're in backoff; must have been a nudge. 551 // We weren't continuing but we're in backoff; must have been a nudge.
515 DCHECK_EQ(NUDGE, old_job.purpose); 552 DCHECK_EQ(NUDGE, old_job.purpose);
516 DCHECK(!wait_interval_->had_nudge); 553 DCHECK(!wait_interval_->had_nudge);
517 wait_interval_->had_nudge = true; 554 wait_interval_->had_nudge = true;
518 wait_interval_->timer.Reset(); 555 wait_interval_->timer.Reset();
519 } else { 556 } else {
520 // We weren't continuing and we aren't in backoff. Schedule a normal 557 // We weren't continuing and we aren't in backoff. Schedule a normal
521 // continuation. 558 // continuation.
522 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), NUDGE_SOURCE_CONTINUATION, 559 if (old_job.purpose == CONFIGURATION) {
523 old_job.session->source().types, FROM_HERE); 560 ScheduleConfigImpl(old_job.session->routing_info(),
561 old_job.session->workers(),
562 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION));
563 } else {
564 // For all other purposes(nudge and poll) we schedule a retry nudge.
565 ScheduleNudgeImpl(TimeDelta::FromSeconds(0),
566 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION),
567 old_job.session->source().types, false, FROM_HERE);
568 }
524 } 569 }
525 } 570 }
526 571
527 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { 572 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) {
528 DCHECK(thread_.IsRunning()); 573 DCHECK(thread_.IsRunning());
529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 574 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
530 575
531 TimeDelta poll = (!session_context_->notifications_enabled()) ? 576 TimeDelta poll = (!session_context_->notifications_enabled()) ?
532 syncer_short_poll_interval_seconds_ : 577 syncer_short_poll_interval_seconds_ :
533 syncer_long_poll_interval_seconds_; 578 syncer_long_poll_interval_seconds_;
(...skipping 15 matching lines...) Expand all
549 const SyncSessionJob& old_job) { 594 const SyncSessionJob& old_job) {
550 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 595 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
551 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning()); 596 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning());
552 SyncSession* old = old_job.session.get(); 597 SyncSession* old = old_job.session.get();
553 SyncSession* s(new SyncSession(session_context_.get(), this, 598 SyncSession* s(new SyncSession(session_context_.get(), this,
554 old->source(), old->routing_info(), old->workers())); 599 old->source(), old->routing_info(), old->workers()));
555 TimeDelta length = delay_provider_->GetDelay( 600 TimeDelta length = delay_provider_->GetDelay(
556 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); 601 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1));
557 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, 602 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF,
558 length)); 603 length));
559 SyncSessionJob job = {NUDGE, TimeTicks::Now() + length, 604 if (old_job.purpose == CONFIGURATION) {
560 make_linked_ptr(s), FROM_HERE}; 605 SyncSessionJob job = {old_job.purpose, TimeTicks::Now() + length,
561 pending_nudge_.reset(new SyncSessionJob(job)); 606 make_linked_ptr(s), false, FROM_HERE};
607 wait_interval_->pending_configure_job.reset(new SyncSessionJob(job));
608 } else {
609 // We are not in configuration mode. So wait_interval's pending job
tim (not reviewing) 2011/04/09 23:52:58 I'm rather concerned about the apparent connection
lipalani1 2011/04/12 02:33:00 Hmm.. not sure I follow. let me sync up with you.
610 // should be null.
611 DCHECK(wait_interval_->pending_configure_job.get() == NULL);
612
613 // No matter what type of job it is.(nudge or poll, it cannot be config)
614 // We are going to treat it as nudge when doing exponential back off
615 // retries.
616 // TODO(lipalani) - handle clear user data.
617 UpdatePendingJob(old_job);
618 }
562 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); 619 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob);
563 } 620 }
564 621
565 // static 622 // static
566 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { 623 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) {
567 if (last_delay.InSeconds() >= kMaxBackoffSeconds) 624 if (last_delay.InSeconds() >= kMaxBackoffSeconds)
568 return TimeDelta::FromSeconds(kMaxBackoffSeconds); 625 return TimeDelta::FromSeconds(kMaxBackoffSeconds);
569 626
570 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 627 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2
571 int64 backoff_s = 628 int64 backoff_s =
(...skipping 14 matching lines...) Expand all
586 return TimeDelta::FromSeconds(backoff_s); 643 return TimeDelta::FromSeconds(backoff_s);
587 } 644 }
588 645
589 void SyncerThread::Stop() { 646 void SyncerThread::Stop() {
590 syncer_->RequestEarlyExit(); // Safe to call from any thread. 647 syncer_->RequestEarlyExit(); // Safe to call from any thread.
591 session_context_->connection_manager()->RemoveListener(this); 648 session_context_->connection_manager()->RemoveListener(this);
592 thread_.Stop(); 649 thread_.Stop();
593 } 650 }
594 651
595 void SyncerThread::DoCanaryJob() { 652 void SyncerThread::DoCanaryJob() {
596 DCHECK(pending_nudge_.get()); 653 // We should not be here unless wait interval was initialized due to
654 // throttling or backing off.
655 DCHECK(wait_interval_.get());
656
597 wait_interval_->had_nudge = false; 657 wait_interval_->had_nudge = false;
598 SyncSessionJob copy = *pending_nudge_; 658
659 // We should have one of 2 things. Otherwise we shouldnt be running the timer.
660 DCHECK(wait_interval_->pending_configure_job.get() || pending_nudge_.get());
661 ExecutePendingJob();
662 }
663
664 void SyncerThread::ExecutePendingJob() {
665 if (mode_ == CONFIGURATION_MODE) {
666 if (wait_interval_.get() && wait_interval_->pending_configure_job.get()) {
667 ExecuteJobByMakingACopy(wait_interval_->pending_configure_job.get());
668 }
669 } else {
670 if (pending_nudge_.get()) {
671 // Pending jobs mostly have time from the past. Reset it so this job
672 // will get executed.
673 if (pending_nudge_->scheduled_start < TimeTicks::Now())
674 pending_nudge_->scheduled_start = TimeTicks::Now();
675 // The pending nudge would be cleared in the DoSyncSessionJob function.
676 ExecuteJobByMakingACopy(pending_nudge_.get());
677 }
678 }
679 }
680
681 void SyncerThread::ExecuteJobByMakingACopy(SyncSessionJob* job) {
682 DCHECK(job);
683 SyncSessionJob copy = *job;
684 copy.is_canary_job = true;
599 DoSyncSessionJob(copy); 685 DoSyncSessionJob(copy);
600 } 686 }
601 687
602 void SyncerThread::PollTimerCallback() { 688 void SyncerThread::PollTimerCallback() {
603 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 689 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
604 ModelSafeRoutingInfo r; 690 ModelSafeRoutingInfo r;
605 std::vector<ModelSafeWorker*> w; 691 std::vector<ModelSafeWorker*> w;
606 session_context_->registrar()->GetModelSafeRoutingInfo(&r); 692 session_context_->registrar()->GetModelSafeRoutingInfo(&r);
607 session_context_->registrar()->GetWorkers(&w); 693 session_context_->registrar()->GetWorkers(&w);
608 ModelTypePayloadMap types_with_payloads = 694 ModelTypePayloadMap types_with_payloads =
609 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); 695 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string());
610 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); 696 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads);
611 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w); 697 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w);
612 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE); 698 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE);
613 } 699 }
614 700
615 void SyncerThread::Unthrottle() { 701 void SyncerThread::Unthrottle() {
616 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); 702 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode);
703 DoCanaryJob();
617 wait_interval_.reset(); 704 wait_interval_.reset();
618 } 705 }
619 706
620 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { 707 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) {
621 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); 708 DCHECK_EQ(MessageLoop::current(), thread_.message_loop());
622 session_context_->NotifyListeners(SyncEngineEvent(cause)); 709 session_context_->NotifyListeners(SyncEngineEvent(cause));
623 } 710 }
624 711
625 bool SyncerThread::IsBackingOff() const { 712 bool SyncerThread::IsBackingOff() const {
626 return wait_interval_.get() && wait_interval_->mode == 713 return wait_interval_.get() && wait_interval_->mode ==
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 &SyncerThread::CheckServerConnectionManagerStatus, 749 &SyncerThread::CheckServerConnectionManagerStatus,
663 event.connection_code)); 750 event.connection_code));
664 } 751 }
665 752
666 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { 753 void SyncerThread::set_notifications_enabled(bool notifications_enabled) {
667 session_context_->set_notifications_enabled(notifications_enabled); 754 session_context_->set_notifications_enabled(notifications_enabled);
668 } 755 }
669 756
670 } // s3 757 } // s3
671 } // browser_sync 758 } // browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698