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

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

Powered by Google App Engine
This is Rietveld 408576698