Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 DoPendingJobIfPossible(); | |
| 99 } | 102 } |
| 100 } | 103 } |
| 101 | 104 |
| 102 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) { | 105 void SyncerThread::Start(Mode mode, ModeChangeCallback* callback) { |
| 103 if (!thread_.IsRunning()) { | 106 if (!thread_.IsRunning()) { |
| 104 if (!thread_.Start()) { | 107 if (!thread_.Start()) { |
| 105 NOTREACHED() << "Unable to start SyncerThread."; | 108 NOTREACHED() << "Unable to start SyncerThread."; |
| 106 return; | 109 return; |
| 107 } | 110 } |
| 108 WatchConnectionManager(); | 111 WatchConnectionManager(); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 133 | 136 |
| 134 void SyncerThread::StartImpl(Mode mode, | 137 void SyncerThread::StartImpl(Mode mode, |
| 135 linked_ptr<ModeChangeCallback> callback) { | 138 linked_ptr<ModeChangeCallback> callback) { |
| 136 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 139 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 137 DCHECK(!session_context_->account_name().empty()); | 140 DCHECK(!session_context_->account_name().empty()); |
| 138 DCHECK(syncer_.get()); | 141 DCHECK(syncer_.get()); |
| 139 mode_ = mode; | 142 mode_ = mode; |
| 140 AdjustPolling(NULL); // Will kick start poll timer if needed. | 143 AdjustPolling(NULL); // Will kick start poll timer if needed. |
| 141 if (callback.get()) | 144 if (callback.get()) |
| 142 callback->Run(); | 145 callback->Run(); |
| 146 | |
| 147 // We just changed our mode. See if there are any pending jobs that we could | |
| 148 // execute in the new mode. | |
| 149 DoPendingJobIfPossible(); | |
| 143 } | 150 } |
| 144 | 151 |
| 145 bool SyncerThread::ShouldRunJob(SyncSessionJobPurpose purpose, | 152 SyncerThread::JobProcessDecision SyncerThread::DecideWhileInWaitInterval( |
| 146 const TimeTicks& scheduled_start) { | 153 const SyncSessionJob& job) { |
| 147 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | |
| 148 | 154 |
| 149 // Check wait interval. | 155 DCHECK(wait_interval_.get()); |
| 150 if (wait_interval_.get()) { | 156 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 | 157 |
| 156 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); | 158 if (job.purpose == SyncSessionJob::POLL) |
| 157 if ((purpose != NUDGE) || wait_interval_->had_nudge) | 159 return DROP; |
| 158 return false; | 160 |
| 161 DCHECK(job.purpose == SyncSessionJob::NUDGE || | |
| 162 job.purpose == SyncSessionJob::CONFIGURATION); | |
| 163 if (wait_interval_->mode == WaitInterval::THROTTLED) | |
| 164 return SAVE; | |
| 165 | |
| 166 DCHECK_EQ(wait_interval_->mode, WaitInterval::EXPONENTIAL_BACKOFF); | |
| 167 if (job.purpose == SyncSessionJob::NUDGE) { | |
| 168 if (mode_ == CONFIGURATION_MODE) | |
| 169 return SAVE; | |
| 170 | |
| 171 // If we already had one nudge then just drop this nudge. We will retry | |
| 172 // later when the timer runs out. | |
| 173 return wait_interval_->had_nudge ? DROP : CONTINUE; | |
| 174 } | |
| 175 // This is a config job. | |
| 176 return job.is_canary_job ? CONTINUE : SAVE; | |
| 177 | |
| 178 } | |
| 179 | |
| 180 SyncerThread::JobProcessDecision SyncerThread::DecideOnJob( | |
| 181 const SyncSessionJob& job) { | |
| 182 if (job.purpose == SyncSessionJob::CLEAR_USER_DATA) | |
| 183 return CONTINUE; | |
| 184 | |
| 185 if (wait_interval_.get()) | |
| 186 return DecideWhileInWaitInterval(job); | |
| 187 | |
| 188 if (mode_ == CONFIGURATION_MODE) { | |
| 189 if (job.purpose == SyncSessionJob::NUDGE) | |
| 190 return SAVE; | |
| 191 else if (job.purpose == SyncSessionJob::CONFIGURATION) | |
| 192 return CONTINUE; | |
| 193 else | |
| 194 return DROP; | |
| 159 } | 195 } |
| 160 | 196 |
| 161 // Mode / purpose contract (See 'Mode' enum in header). Don't run jobs that | 197 // We are in normal mode. |
| 162 // were intended for a normal sync if we are in configuration mode, and vice | 198 DCHECK_EQ(mode_, NORMAL_MODE); |
| 163 // versa. | 199 DCHECK_NE(job.purpose, SyncSessionJob::CONFIGURATION); |
| 164 switch (mode_) { | 200 |
| 165 case CONFIGURATION_MODE: | 201 // Freshness condition |
| 166 if (purpose != CONFIGURATION) | 202 if (job.scheduled_start < last_sync_session_end_time_) |
| 167 return false; | 203 return DROP; |
| 168 break; | 204 |
| 169 case NORMAL_MODE: | 205 if (server_connection_ok_) |
| 170 if (purpose == CONFIGURATION) | 206 return CONTINUE; |
| 171 return false; | 207 |
| 172 break; | 208 return job.purpose == SyncSessionJob::NUDGE ? SAVE : DROP; |
| 173 default: | 209 } |
| 174 NOTREACHED() << "Unknown SyncerThread Mode: " << mode_; | 210 |
| 175 return false; | 211 void SyncerThread::UpdatePendingJob(const SyncSessionJob& job) { |
| 212 DCHECK(job.purpose != SyncSessionJob::CONFIGURATION); | |
| 213 if (pending_nudge_.get() == NULL) { | |
| 214 SyncSession* s = job.session.get(); | |
| 215 scoped_ptr<SyncSession> session(new SyncSession(s->context(), | |
| 216 s->delegate(), s->source(), s->routing_info(), s->workers())); | |
| 217 | |
| 218 SyncSessionJob new_job(SyncSessionJob::NUDGE, job.scheduled_start, | |
| 219 make_linked_ptr(session.release()), false, job.nudge_location); | |
| 220 pending_nudge_.reset(new SyncSessionJob(new_job)); | |
| 221 | |
| 222 return; | |
| 176 } | 223 } |
| 177 | 224 |
| 178 // Continuation NUDGE tasks have priority over POLLs because they are the | 225 pending_nudge_->session->Coalesce(*(job.session.get())); |
|
tim (not reviewing)
2011/04/13 19:49:06
still here.
| |
| 179 // only tasks that trigger exponential backoff, so this prevents them from | 226 pending_nudge_->scheduled_start = job.scheduled_start; |
| 180 // being starved from running (e.g. due to a very, very low poll interval, | |
| 181 // such as 0ms). It's rare that this would ever matter in practice. | |
| 182 if (purpose == POLL && (pending_nudge_.get() && | |
| 183 pending_nudge_->session->source().updates_source == | |
| 184 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION)) { | |
| 185 return false; | |
| 186 } | |
| 187 | 227 |
| 188 // Freshness condition. | 228 // Unfortunately the nudge location cannot be modified. So it stores the |
| 189 if (purpose == NUDGE && | 229 // location of the first caller. |
| 190 (scheduled_start < last_sync_session_end_time_)) { | |
| 191 return false; | |
| 192 } | |
| 193 | |
| 194 return server_connection_ok_; | |
| 195 } | 230 } |
| 196 | 231 |
| 197 GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource( | 232 bool SyncerThread::ShouldRunJob(const SyncSessionJob& job) { |
| 198 NudgeSource source) { | 233 JobProcessDecision decision = DecideOnJob(job); |
| 199 switch (source) { | 234 if (decision != SAVE) |
| 200 case NUDGE_SOURCE_NOTIFICATION: | 235 return decision == CONTINUE; |
| 201 return GetUpdatesCallerInfo::NOTIFICATION; | 236 |
| 202 case NUDGE_SOURCE_LOCAL: | 237 DCHECK(job.purpose == SyncSessionJob::NUDGE || job.purpose == |
| 203 return GetUpdatesCallerInfo::LOCAL; | 238 SyncSessionJob::CONFIGURATION); |
| 204 case NUDGE_SOURCE_CONTINUATION: | 239 |
| 205 return GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; | 240 SaveJob(job); |
| 206 case NUDGE_SOURCE_UNKNOWN: | 241 return false; |
| 207 return GetUpdatesCallerInfo::UNKNOWN; | 242 } |
| 208 default: | 243 |
| 209 NOTREACHED(); | 244 void SyncerThread::SaveJob(const SyncSessionJob& job) { |
| 210 return GetUpdatesCallerInfo::UNKNOWN; | 245 DCHECK(job.purpose != SyncSessionJob::CLEAR_USER_DATA); |
| 246 if (job.purpose == SyncSessionJob::NUDGE) { | |
| 247 UpdatePendingJob(job); | |
| 248 } else { | |
| 249 DCHECK(wait_interval_.get()); | |
| 250 DCHECK(mode_ == CONFIGURATION_MODE); | |
| 251 | |
| 252 SyncSession* old = job.session.get(); | |
| 253 SyncSession* s(new SyncSession(session_context_.get(), this, | |
| 254 old->source(), old->routing_info(), old->workers())); | |
| 255 SyncSessionJob new_job(job.purpose, TimeTicks::Now(), | |
| 256 make_linked_ptr(s), false, job.nudge_location); | |
| 257 wait_interval_->pending_configure_job.reset(new SyncSessionJob(new_job)); | |
| 211 } | 258 } |
| 212 } | 259 } |
| 213 | 260 |
| 214 // Functor for std::find_if to search by ModelSafeGroup. | 261 // Functor for std::find_if to search by ModelSafeGroup. |
| 215 struct ModelSafeWorkerGroupIs { | 262 struct ModelSafeWorkerGroupIs { |
| 216 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} | 263 explicit ModelSafeWorkerGroupIs(ModelSafeGroup group) : group(group) {} |
| 217 bool operator()(ModelSafeWorker* w) { | 264 bool operator()(ModelSafeWorker* w) { |
| 218 return group == w->GetModelSafeGroup(); | 265 return group == w->GetModelSafeGroup(); |
| 219 } | 266 } |
| 220 ModelSafeGroup group; | 267 ModelSafeGroup group; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 233 NudgeSource source, const ModelTypeBitSet& types, | 280 NudgeSource source, const ModelTypeBitSet& types, |
| 234 const tracked_objects::Location& nudge_location) { | 281 const tracked_objects::Location& nudge_location) { |
| 235 if (!thread_.IsRunning()) { | 282 if (!thread_.IsRunning()) { |
| 236 NOTREACHED(); | 283 NOTREACHED(); |
| 237 return; | 284 return; |
| 238 } | 285 } |
| 239 | 286 |
| 240 ModelTypePayloadMap types_with_payloads = | 287 ModelTypePayloadMap types_with_payloads = |
| 241 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); | 288 syncable::ModelTypePayloadMapFromBitSet(types, std::string()); |
| 242 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 289 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 243 this, &SyncerThread::ScheduleNudgeImpl, delay, source, | 290 this, &SyncerThread::ScheduleNudgeImpl, delay, |
| 244 types_with_payloads, nudge_location)); | 291 GetUpdatesFromNudgeSource(source), types_with_payloads, false, |
| 292 nudge_location)); | |
| 245 } | 293 } |
| 246 | 294 |
| 247 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, | 295 void SyncerThread::ScheduleNudgeWithPayloads(const TimeDelta& delay, |
| 248 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, | 296 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, |
| 249 const tracked_objects::Location& nudge_location) { | 297 const tracked_objects::Location& nudge_location) { |
| 250 if (!thread_.IsRunning()) { | 298 if (!thread_.IsRunning()) { |
| 251 NOTREACHED(); | 299 NOTREACHED(); |
| 252 return; | 300 return; |
| 253 } | 301 } |
| 254 | 302 |
| 255 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 303 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 256 this, &SyncerThread::ScheduleNudgeImpl, delay, source, | 304 this, &SyncerThread::ScheduleNudgeImpl, delay, |
| 257 types_with_payloads, nudge_location)); | 305 GetUpdatesFromNudgeSource(source), types_with_payloads, false, |
| 306 nudge_location)); | |
| 258 } | 307 } |
| 259 | 308 |
| 260 void SyncerThread::ScheduleClearUserDataImpl() { | 309 void SyncerThread::ScheduleClearUserDataImpl() { |
| 261 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 310 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 262 SyncSession* session = new SyncSession(session_context_.get(), this, | 311 SyncSession* session = new SyncSession(session_context_.get(), this, |
| 263 SyncSourceInfo(), ModelSafeRoutingInfo(), | 312 SyncSourceInfo(), ModelSafeRoutingInfo(), |
| 264 std::vector<ModelSafeWorker*>()); | 313 std::vector<ModelSafeWorker*>()); |
| 265 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CLEAR_USER_DATA, session, | 314 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), |
| 266 FROM_HERE); | 315 SyncSessionJob::CLEAR_USER_DATA, session, FROM_HERE); |
| 267 } | 316 } |
| 268 | 317 |
| 269 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, | 318 void SyncerThread::ScheduleNudgeImpl(const TimeDelta& delay, |
| 270 NudgeSource source, const ModelTypePayloadMap& types_with_payloads, | 319 GetUpdatesCallerInfo::GetUpdatesSource source, |
| 271 const tracked_objects::Location& nudge_location) { | 320 const ModelTypePayloadMap& types_with_payloads, |
| 321 bool is_canary_job, const tracked_objects::Location& nudge_location) { | |
| 272 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 322 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 | 323 |
| 280 // Note we currently nudge for all types regardless of the ones incurring | 324 // Note we currently nudge for all types regardless of the ones incurring |
| 281 // the nudge. Doing different would throw off some syncer commands like | 325 // the nudge. Doing different would throw off some syncer commands like |
| 282 // CleanupDisabledTypes. We may want to change this in the future. | 326 // CleanupDisabledTypes. We may want to change this in the future. |
| 283 ModelSafeRoutingInfo routes; | 327 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 | 328 |
| 290 scoped_ptr<SyncSession> session(new SyncSession( | 329 SyncSession* session(CreateSyncSession(info)); |
| 291 session_context_.get(), this, info, routes, workers)); | 330 |
| 331 SyncSessionJob job(SyncSessionJob::NUDGE, TimeTicks::Now() + delay, | |
| 332 make_linked_ptr(session), is_canary_job, | |
| 333 nudge_location); | |
| 334 | |
| 335 session = NULL; | |
| 336 if (!ShouldRunJob(job)) | |
| 337 return; | |
| 292 | 338 |
| 293 if (pending_nudge_.get()) { | 339 if (pending_nudge_.get()) { |
| 294 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) | 340 if (IsBackingOff() && delay > TimeDelta::FromSeconds(1)) |
| 295 return; | 341 return; |
| 296 | 342 |
| 297 pending_nudge_->session->Coalesce(*session.get()); | 343 pending_nudge_->session->Coalesce(*(job.session.get())); |
| 298 | 344 |
| 299 if (!IsBackingOff()) { | 345 if (!IsBackingOff()) { |
| 300 return; | 346 return; |
| 301 } else { | 347 } else { |
| 302 // Re-schedule the current pending nudge. | 348 // Re-schedule the current pending nudge. |
| 303 SyncSession* s = pending_nudge_->session.get(); | 349 SyncSession* s = pending_nudge_->session.get(); |
| 304 session.reset(new SyncSession(s->context(), s->delegate(), s->source(), | 350 job.session.reset(new SyncSession(s->context(), s->delegate(), |
| 305 s->routing_info(), s->workers())); | 351 s->source(), s->routing_info(), s->workers())); |
| 306 pending_nudge_.reset(); | 352 pending_nudge_.reset(); |
| 307 } | 353 } |
| 308 } | 354 } |
| 309 ScheduleSyncSessionJob(delay, NUDGE, session.release(), nudge_location); | 355 |
| 356 // TODO(lipalani) - pass the job itself to ScheduleSyncSessionJob. | |
| 357 ScheduleSyncSessionJob(delay, SyncSessionJob::NUDGE, job.session.release(), | |
| 358 nudge_location); | |
| 310 } | 359 } |
| 311 | 360 |
| 312 // Helper to extract the routing info and workers corresponding to types in | 361 // Helper to extract the routing info and workers corresponding to types in |
| 313 // |types| from |registrar|. | 362 // |types| from |registrar|. |
| 314 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, | 363 void GetModelSafeParamsForTypes(const ModelTypeBitSet& types, |
| 315 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, | 364 ModelSafeWorkerRegistrar* registrar, ModelSafeRoutingInfo* routes, |
| 316 std::vector<ModelSafeWorker*>* workers) { | 365 std::vector<ModelSafeWorker*>* workers) { |
| 317 ModelSafeRoutingInfo r_tmp; | 366 ModelSafeRoutingInfo r_tmp; |
| 318 std::vector<ModelSafeWorker*> w_tmp; | 367 std::vector<ModelSafeWorker*> w_tmp; |
| 319 registrar->GetModelSafeRoutingInfo(&r_tmp); | 368 registrar->GetModelSafeRoutingInfo(&r_tmp); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 347 NOTREACHED(); | 396 NOTREACHED(); |
| 348 return; | 397 return; |
| 349 } | 398 } |
| 350 | 399 |
| 351 ModelSafeRoutingInfo routes; | 400 ModelSafeRoutingInfo routes; |
| 352 std::vector<ModelSafeWorker*> workers; | 401 std::vector<ModelSafeWorker*> workers; |
| 353 GetModelSafeParamsForTypes(types, session_context_->registrar(), | 402 GetModelSafeParamsForTypes(types, session_context_->registrar(), |
| 354 &routes, &workers); | 403 &routes, &workers); |
| 355 | 404 |
| 356 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 405 thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 357 this, &SyncerThread::ScheduleConfigImpl, routes, workers)); | 406 this, &SyncerThread::ScheduleConfigImpl, routes, workers, |
| 407 GetUpdatesCallerInfo::FIRST_UPDATE)); | |
| 358 } | 408 } |
| 359 | 409 |
| 360 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, | 410 void SyncerThread::ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, |
| 361 const std::vector<ModelSafeWorker*>& workers) { | 411 const std::vector<ModelSafeWorker*>& workers, |
| 412 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { | |
| 362 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 413 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 363 | 414 |
| 364 // TODO(tim): config-specific GetUpdatesCallerInfo value? | 415 // TODO(tim): config-specific GetUpdatesCallerInfo value? |
| 365 SyncSession* session = new SyncSession(session_context_.get(), this, | 416 SyncSession* session = new SyncSession(session_context_.get(), this, |
| 366 SyncSourceInfo(GetUpdatesCallerInfo::FIRST_UPDATE, | 417 SyncSourceInfo(source, |
| 367 syncable::ModelTypePayloadMapFromRoutingInfo( | 418 syncable::ModelTypePayloadMapFromRoutingInfo( |
| 368 routing_info, std::string())), | 419 routing_info, std::string())), |
| 369 routing_info, workers); | 420 routing_info, workers); |
| 370 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), CONFIGURATION, session, | 421 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), |
| 371 FROM_HERE); | 422 SyncSessionJob::CONFIGURATION, session, FROM_HERE); |
| 372 } | 423 } |
| 373 | 424 |
| 374 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, | 425 void SyncerThread::ScheduleSyncSessionJob(const base::TimeDelta& delay, |
| 375 SyncSessionJobPurpose purpose, sessions::SyncSession* session, | 426 SyncSessionJob::SyncSessionJobPurpose purpose, |
| 427 sessions::SyncSession* session, | |
| 376 const tracked_objects::Location& nudge_location) { | 428 const tracked_objects::Location& nudge_location) { |
| 377 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 429 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 378 | 430 |
| 379 SyncSessionJob job = {purpose, TimeTicks::Now() + delay, | 431 SyncSessionJob job(purpose, TimeTicks::Now() + delay, |
| 380 make_linked_ptr(session), nudge_location}; | 432 make_linked_ptr(session), false, nudge_location); |
| 381 if (purpose == NUDGE) { | 433 if (purpose == SyncSessionJob::NUDGE) { |
| 382 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); | 434 DCHECK(!pending_nudge_.get() || pending_nudge_->session.get() == session); |
| 383 pending_nudge_.reset(new SyncSessionJob(job)); | 435 pending_nudge_.reset(new SyncSessionJob(job)); |
| 384 } | 436 } |
| 385 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, | 437 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(this, |
| 386 &SyncerThread::DoSyncSessionJob, job), | 438 &SyncerThread::DoSyncSessionJob, job), |
| 387 delay.InMilliseconds()); | 439 delay.InMilliseconds()); |
| 388 } | 440 } |
| 389 | 441 |
| 390 void SyncerThread::SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, | 442 void SyncerThread::SetSyncerStepsForPurpose( |
| 443 SyncSessionJob::SyncSessionJobPurpose purpose, | |
| 391 SyncerStep* start, SyncerStep* end) { | 444 SyncerStep* start, SyncerStep* end) { |
| 392 *end = SYNCER_END; | 445 *end = SYNCER_END; |
| 393 switch (purpose) { | 446 switch (purpose) { |
| 394 case CONFIGURATION: | 447 case SyncSessionJob::CONFIGURATION: |
| 395 *start = DOWNLOAD_UPDATES; | 448 *start = DOWNLOAD_UPDATES; |
| 396 *end = APPLY_UPDATES; | 449 *end = APPLY_UPDATES; |
| 397 return; | 450 return; |
| 398 case CLEAR_USER_DATA: | 451 case SyncSessionJob::CLEAR_USER_DATA: |
| 399 *start = CLEAR_PRIVATE_DATA; | 452 *start = CLEAR_PRIVATE_DATA; |
| 400 return; | 453 return; |
| 401 case NUDGE: | 454 case SyncSessionJob::NUDGE: |
| 402 case POLL: | 455 case SyncSessionJob::POLL: |
| 403 *start = SYNCER_BEGIN; | 456 *start = SYNCER_BEGIN; |
| 404 return; | 457 return; |
| 405 default: | 458 default: |
| 406 NOTREACHED(); | 459 NOTREACHED(); |
| 407 } | 460 } |
| 408 } | 461 } |
| 409 | 462 |
| 410 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { | 463 void SyncerThread::DoSyncSessionJob(const SyncSessionJob& job) { |
| 411 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 464 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 412 if (!ShouldRunJob(job.purpose, job.scheduled_start)) { | 465 if (!ShouldRunJob(job)) |
| 413 LOG(WARNING) << "Dropping nudge at DoSyncSessionJob, source = " | |
| 414 << job.session->source().updates_source; | |
| 415 return; | 466 return; |
| 416 } | |
| 417 | 467 |
| 418 if (job.purpose == NUDGE) { | 468 if (job.purpose == SyncSessionJob::NUDGE) { |
| 419 DCHECK(pending_nudge_.get()); | 469 DCHECK(pending_nudge_.get()); |
| 420 if (pending_nudge_->session != job.session) | 470 if (pending_nudge_->session != job.session) |
| 421 return; // Another nudge must have been scheduled in in the meantime. | 471 return; // Another nudge must have been scheduled in in the meantime. |
| 422 pending_nudge_.reset(); | 472 pending_nudge_.reset(); |
| 423 } | 473 } |
| 424 | 474 |
| 425 SyncerStep begin(SYNCER_BEGIN); | 475 SyncerStep begin(SYNCER_BEGIN); |
| 426 SyncerStep end(SYNCER_END); | 476 SyncerStep end(SYNCER_END); |
| 427 SetSyncerStepsForPurpose(job.purpose, &begin, &end); | 477 SetSyncerStepsForPurpose(job.purpose, &begin, &end); |
| 428 | 478 |
| 429 bool has_more_to_sync = true; | 479 bool has_more_to_sync = true; |
| 430 while (ShouldRunJob(job.purpose, job.scheduled_start) && has_more_to_sync) { | 480 while (ShouldRunJob(job) && has_more_to_sync) { |
| 431 VLOG(1) << "SyncerThread: Calling SyncShare."; | 481 VLOG(1) << "SyncerThread: Calling SyncShare."; |
| 432 // Synchronously perform the sync session from this thread. | 482 // Synchronously perform the sync session from this thread. |
| 433 syncer_->SyncShare(job.session.get(), begin, end); | 483 syncer_->SyncShare(job.session.get(), begin, end); |
| 434 has_more_to_sync = job.session->HasMoreToSync(); | 484 has_more_to_sync = job.session->HasMoreToSync(); |
| 435 if (has_more_to_sync) | 485 if (has_more_to_sync) |
| 436 job.session->ResetTransientState(); | 486 job.session->ResetTransientState(); |
| 437 } | 487 } |
| 438 VLOG(1) << "SyncerThread: Done SyncShare looping."; | 488 VLOG(1) << "SyncerThread: Done SyncShare looping."; |
| 439 FinishSyncSessionJob(job); | 489 FinishSyncSessionJob(job); |
| 440 } | 490 } |
| 441 | 491 |
| 442 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) { | 492 void SyncerThread::UpdateCarryoverSessionState(const SyncSessionJob& old_job) { |
| 443 if (old_job.purpose == CONFIGURATION) { | 493 if (old_job.purpose == SyncSessionJob::CONFIGURATION) { |
| 444 // Whatever types were part of a configuration task will have had updates | 494 // 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 | 495 // downloaded. For that reason, we make sure they get recorded in the |
| 446 // event that they get disabled at a later time. | 496 // event that they get disabled at a later time. |
| 447 ModelSafeRoutingInfo r(session_context_->previous_session_routing_info()); | 497 ModelSafeRoutingInfo r(session_context_->previous_session_routing_info()); |
| 448 if (!r.empty()) { | 498 if (!r.empty()) { |
| 449 ModelSafeRoutingInfo temp_r; | 499 ModelSafeRoutingInfo temp_r; |
| 450 ModelSafeRoutingInfo old_info(old_job.session->routing_info()); | 500 ModelSafeRoutingInfo old_info(old_job.session->routing_info()); |
| 451 std::set_union(r.begin(), r.end(), old_info.begin(), old_info.end(), | 501 std::set_union(r.begin(), r.end(), old_info.begin(), old_info.end(), |
| 452 std::insert_iterator<ModelSafeRoutingInfo>(temp_r, temp_r.begin())); | 502 std::insert_iterator<ModelSafeRoutingInfo>(temp_r, temp_r.begin())); |
| 453 session_context_->set_previous_session_routing_info(temp_r); | 503 session_context_->set_previous_session_routing_info(temp_r); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 466 ModelTypePayloadMap::const_iterator iter; | 516 ModelTypePayloadMap::const_iterator iter; |
| 467 for (iter = job.session->source().types.begin(); | 517 for (iter = job.session->source().types.begin(); |
| 468 iter != job.session->source().types.end(); | 518 iter != job.session->source().types.end(); |
| 469 ++iter) { | 519 ++iter) { |
| 470 syncable::PostTimeToTypeHistogram(iter->first, | 520 syncable::PostTimeToTypeHistogram(iter->first, |
| 471 now - last_sync_session_end_time_); | 521 now - last_sync_session_end_time_); |
| 472 } | 522 } |
| 473 } | 523 } |
| 474 last_sync_session_end_time_ = now; | 524 last_sync_session_end_time_ = now; |
| 475 UpdateCarryoverSessionState(job); | 525 UpdateCarryoverSessionState(job); |
| 476 if (IsSyncingCurrentlySilenced()) | 526 if (IsSyncingCurrentlySilenced()) { |
| 527 // Let us save the job. So when we are unthrottled we can execute it. | |
| 528 SaveJob(job); | |
| 477 return; // Nothing to do. | 529 return; // Nothing to do. |
| 530 } | |
| 478 | 531 |
| 479 VLOG(1) << "Updating the next polling time after SyncMain"; | 532 VLOG(1) << "Updating the next polling time after SyncMain"; |
| 480 ScheduleNextSync(job); | 533 ScheduleNextSync(job); |
| 481 } | 534 } |
| 482 | 535 |
| 483 void SyncerThread::ScheduleNextSync(const SyncSessionJob& old_job) { | 536 void SyncerThread::ScheduleNextSync(const SyncSessionJob& old_job) { |
| 484 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 537 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 485 DCHECK(!old_job.session->HasMoreToSync()); | 538 DCHECK(!old_job.session->HasMoreToSync()); |
| 486 // Note: |num_server_changes_remaining| > 0 here implies that we received a | 539 // Note: |num_server_changes_remaining| > 0 here implies that we received a |
| 487 // broken response while trying to download all updates, because the Syncer | 540 // broken response while trying to download all updates, because the Syncer |
| 488 // will loop until this value is exhausted. Also, if unsynced_handles exist | 541 // will loop until this value is exhausted. Also, if unsynced_handles exist |
| 489 // but HasMoreToSync is false, this implies that the Syncer determined no | 542 // 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 | 543 // forward progress was possible at this time (an error, such as an HTTP |
| 491 // 500, is likely to have occurred during commit). | 544 // 500, is likely to have occurred during commit). |
| 492 const bool work_to_do = | 545 const bool work_to_do = |
| 493 old_job.session->status_controller()->num_server_changes_remaining() > 0 | 546 old_job.session->status_controller()->num_server_changes_remaining() > 0 |
| 494 || old_job.session->status_controller()->unsynced_handles().size() > 0; | 547 || old_job.session->status_controller()->unsynced_handles().size() > 0; |
| 495 VLOG(1) << "syncer has work to do: " << work_to_do; | 548 VLOG(1) << "syncer has work to do: " << work_to_do; |
| 496 | 549 |
| 497 AdjustPolling(&old_job); | 550 AdjustPolling(&old_job); |
| 498 | 551 |
| 499 // TODO(tim): Old impl had special code if notifications disabled. Needed? | 552 // TODO(tim): Old impl had special code if notifications disabled. Needed? |
| 500 if (!work_to_do) { | 553 if (!work_to_do) { |
| 501 // Success implies backoff relief. Note that if this was a "one-off" job | 554 // 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 | 555 // (i.e. purpose == SyncSessionJob::CLEAR_USER_DATA), |
| 503 // ran this wont have changed, as jobs like this don't run a full sync | 556 // if there was work_to_do before it ran this wont have changed, |
| 504 // cycle. So we don't need special code here. | 557 // as jobs like this don't run a full sync cycle. So we don't need |
| 558 // special code here. | |
| 505 wait_interval_.reset(); | 559 wait_interval_.reset(); |
| 506 return; | 560 return; |
| 507 } | 561 } |
| 508 | 562 |
| 509 if (old_job.session->source().updates_source == | 563 if (old_job.session->source().updates_source == |
| 510 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION) { | 564 GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION) { |
| 511 // We don't seem to have made forward progress. Start or extend backoff. | 565 // We don't seem to have made forward progress. Start or extend backoff. |
| 512 HandleConsecutiveContinuationError(old_job); | 566 HandleConsecutiveContinuationError(old_job); |
| 513 } else if (IsBackingOff()) { | 567 } else if (IsBackingOff()) { |
| 514 // We weren't continuing but we're in backoff; must have been a nudge. | 568 // We weren't continuing but we're in backoff; must have been a nudge. |
| 515 DCHECK_EQ(NUDGE, old_job.purpose); | 569 DCHECK_EQ(SyncSessionJob::NUDGE, old_job.purpose); |
| 516 DCHECK(!wait_interval_->had_nudge); | 570 DCHECK(!wait_interval_->had_nudge); |
| 517 wait_interval_->had_nudge = true; | 571 wait_interval_->had_nudge = true; |
| 518 wait_interval_->timer.Reset(); | 572 wait_interval_->timer.Reset(); |
| 519 } else { | 573 } else { |
| 520 // We weren't continuing and we aren't in backoff. Schedule a normal | 574 // We weren't continuing and we aren't in backoff. Schedule a normal |
| 521 // continuation. | 575 // continuation. |
| 522 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), NUDGE_SOURCE_CONTINUATION, | 576 if (old_job.purpose == SyncSessionJob::CONFIGURATION) { |
| 523 old_job.session->source().types, FROM_HERE); | 577 ScheduleConfigImpl(old_job.session->routing_info(), |
| 578 old_job.session->workers(), | |
| 579 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION)); | |
| 580 } else { | |
| 581 // For all other purposes(nudge and poll) we schedule a retry nudge. | |
| 582 ScheduleNudgeImpl(TimeDelta::FromSeconds(0), | |
| 583 GetUpdatesFromNudgeSource(NUDGE_SOURCE_CONTINUATION), | |
| 584 old_job.session->source().types, false, FROM_HERE); | |
| 585 } | |
| 524 } | 586 } |
| 525 } | 587 } |
| 526 | 588 |
| 527 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { | 589 void SyncerThread::AdjustPolling(const SyncSessionJob* old_job) { |
| 528 DCHECK(thread_.IsRunning()); | 590 DCHECK(thread_.IsRunning()); |
| 529 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 591 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 530 | 592 |
| 531 TimeDelta poll = (!session_context_->notifications_enabled()) ? | 593 TimeDelta poll = (!session_context_->notifications_enabled()) ? |
| 532 syncer_short_poll_interval_seconds_ : | 594 syncer_short_poll_interval_seconds_ : |
| 533 syncer_long_poll_interval_seconds_; | 595 syncer_long_poll_interval_seconds_; |
| 534 bool rate_changed = !poll_timer_.IsRunning() || | 596 bool rate_changed = !poll_timer_.IsRunning() || |
| 535 poll != poll_timer_.GetCurrentDelay(); | 597 poll != poll_timer_.GetCurrentDelay(); |
| 536 | 598 |
| 537 if (old_job && old_job->purpose != POLL && !rate_changed) | 599 if (old_job && old_job->purpose != SyncSessionJob::POLL && !rate_changed) |
| 538 poll_timer_.Reset(); | 600 poll_timer_.Reset(); |
| 539 | 601 |
| 540 if (!rate_changed) | 602 if (!rate_changed) |
| 541 return; | 603 return; |
| 542 | 604 |
| 543 // Adjust poll rate. | 605 // Adjust poll rate. |
| 544 poll_timer_.Stop(); | 606 poll_timer_.Stop(); |
| 545 poll_timer_.Start(poll, this, &SyncerThread::PollTimerCallback); | 607 poll_timer_.Start(poll, this, &SyncerThread::PollTimerCallback); |
| 546 } | 608 } |
| 547 | 609 |
| 548 void SyncerThread::HandleConsecutiveContinuationError( | 610 void SyncerThread::HandleConsecutiveContinuationError( |
| 549 const SyncSessionJob& old_job) { | 611 const SyncSessionJob& old_job) { |
| 550 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 612 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 551 DCHECK(!IsBackingOff() || !wait_interval_->timer.IsRunning()); | 613 // This if conditions should be compiled out in retail builds. |
| 614 if (IsBackingOff()) { | |
| 615 DCHECK(wait_interval_->timer.IsRunning() || old_job.is_canary_job); | |
| 616 } | |
| 552 SyncSession* old = old_job.session.get(); | 617 SyncSession* old = old_job.session.get(); |
| 553 SyncSession* s(new SyncSession(session_context_.get(), this, | 618 SyncSession* s(new SyncSession(session_context_.get(), this, |
| 554 old->source(), old->routing_info(), old->workers())); | 619 old->source(), old->routing_info(), old->workers())); |
| 555 TimeDelta length = delay_provider_->GetDelay( | 620 TimeDelta length = delay_provider_->GetDelay( |
| 556 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); | 621 IsBackingOff() ? wait_interval_->length : TimeDelta::FromSeconds(1)); |
| 557 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, | 622 wait_interval_.reset(new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, |
| 558 length)); | 623 length)); |
| 559 SyncSessionJob job = {NUDGE, TimeTicks::Now() + length, | 624 if (old_job.purpose == SyncSessionJob::CONFIGURATION) { |
| 560 make_linked_ptr(s), FROM_HERE}; | 625 SyncSessionJob job(old_job.purpose, TimeTicks::Now() + length, |
| 561 pending_nudge_.reset(new SyncSessionJob(job)); | 626 make_linked_ptr(s), false, FROM_HERE); |
| 627 wait_interval_->pending_configure_job.reset(new SyncSessionJob(job)); | |
| 628 } else { | |
| 629 // We are not in configuration mode. So wait_interval's pending job | |
| 630 // should be null. | |
| 631 DCHECK(wait_interval_->pending_configure_job.get() == NULL); | |
| 632 | |
| 633 // No matter what type of job it is.(nudge or poll, it cannot be config) | |
| 634 // We are going to treat it as nudge when doing exponential back off | |
| 635 // retries. | |
| 636 // TODO(lipalani) - handle clear user data. | |
| 637 UpdatePendingJob(old_job); | |
| 638 } | |
| 562 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); | 639 wait_interval_->timer.Start(length, this, &SyncerThread::DoCanaryJob); |
| 563 } | 640 } |
| 564 | 641 |
| 565 // static | 642 // static |
| 566 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { | 643 TimeDelta SyncerThread::GetRecommendedDelay(const TimeDelta& last_delay) { |
| 567 if (last_delay.InSeconds() >= kMaxBackoffSeconds) | 644 if (last_delay.InSeconds() >= kMaxBackoffSeconds) |
| 568 return TimeDelta::FromSeconds(kMaxBackoffSeconds); | 645 return TimeDelta::FromSeconds(kMaxBackoffSeconds); |
| 569 | 646 |
| 570 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 | 647 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 |
| 571 int64 backoff_s = | 648 int64 backoff_s = |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 586 return TimeDelta::FromSeconds(backoff_s); | 663 return TimeDelta::FromSeconds(backoff_s); |
| 587 } | 664 } |
| 588 | 665 |
| 589 void SyncerThread::Stop() { | 666 void SyncerThread::Stop() { |
| 590 syncer_->RequestEarlyExit(); // Safe to call from any thread. | 667 syncer_->RequestEarlyExit(); // Safe to call from any thread. |
| 591 session_context_->connection_manager()->RemoveListener(this); | 668 session_context_->connection_manager()->RemoveListener(this); |
| 592 thread_.Stop(); | 669 thread_.Stop(); |
| 593 } | 670 } |
| 594 | 671 |
| 595 void SyncerThread::DoCanaryJob() { | 672 void SyncerThread::DoCanaryJob() { |
| 596 DCHECK(pending_nudge_.get()); | 673 // We should not be here unless wait interval was initialized due to |
| 674 // throttling or backing off. | |
| 675 DCHECK(wait_interval_.get()); | |
| 676 | |
| 597 wait_interval_->had_nudge = false; | 677 wait_interval_->had_nudge = false; |
|
tim (not reviewing)
2011/04/13 19:49:06
hmm..
| |
| 598 SyncSessionJob copy = *pending_nudge_; | 678 |
| 599 DoSyncSessionJob(copy); | 679 // We should have one of 2 things. Otherwise we shouldnt be running the timer. |
| 680 DCHECK(wait_interval_->pending_configure_job.get() || pending_nudge_.get()); | |
| 681 DoPendingJobIfPossible(); | |
| 682 } | |
| 683 | |
| 684 void SyncerThread::DoPendingJobIfPossible() { | |
| 685 SyncSessionJob* job_to_execute = NULL; | |
| 686 if (mode_ == CONFIGURATION_MODE && wait_interval_.get() | |
| 687 && wait_interval_->pending_configure_job.get()) { | |
| 688 job_to_execute = wait_interval_->pending_configure_job.get(); | |
| 689 } else if (pending_nudge_.get()) { | |
| 690 // Pending jobs mostly have time from the past. Reset it so this job | |
| 691 // will get executed. | |
| 692 if (pending_nudge_->scheduled_start < TimeTicks::Now()) | |
| 693 pending_nudge_->scheduled_start = TimeTicks::Now(); | |
| 694 | |
| 695 scoped_ptr<SyncSession> session(CreateSyncSession( | |
| 696 pending_nudge_->session->source())); | |
| 697 | |
| 698 // Also the routing info might have been changed since we cached the | |
| 699 // pending nudge. Update it by coalescing to the latest. | |
| 700 pending_nudge_->session->Coalesce(*(session.get())); | |
| 701 // The pending nudge would be cleared in the DoSyncSessionJob function. | |
| 702 job_to_execute = pending_nudge_.get(); | |
| 703 } | |
| 704 | |
| 705 if (job_to_execute != NULL) { | |
| 706 SyncSessionJob copy = *job_to_execute; | |
| 707 copy.is_canary_job = true; | |
| 708 DoSyncSessionJob(copy); | |
| 709 } | |
| 710 } | |
| 711 | |
| 712 SyncSession* SyncerThread::CreateSyncSession(const SyncSourceInfo& source) { | |
| 713 ModelSafeRoutingInfo routes; | |
| 714 std::vector<ModelSafeWorker*> workers; | |
| 715 session_context_->registrar()->GetModelSafeRoutingInfo(&routes); | |
| 716 session_context_->registrar()->GetWorkers(&workers); | |
| 717 SyncSourceInfo info(source); | |
| 718 | |
| 719 SyncSession* session(new SyncSession(session_context_.get(), this, info, | |
| 720 routes, workers)); | |
| 721 | |
| 722 return session; | |
| 600 } | 723 } |
| 601 | 724 |
| 602 void SyncerThread::PollTimerCallback() { | 725 void SyncerThread::PollTimerCallback() { |
| 603 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 726 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 604 ModelSafeRoutingInfo r; | 727 ModelSafeRoutingInfo r; |
| 605 std::vector<ModelSafeWorker*> w; | |
| 606 session_context_->registrar()->GetModelSafeRoutingInfo(&r); | |
| 607 session_context_->registrar()->GetWorkers(&w); | |
| 608 ModelTypePayloadMap types_with_payloads = | 728 ModelTypePayloadMap types_with_payloads = |
| 609 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); | 729 syncable::ModelTypePayloadMapFromRoutingInfo(r, std::string()); |
| 610 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); | 730 SyncSourceInfo info(GetUpdatesCallerInfo::PERIODIC, types_with_payloads); |
| 611 SyncSession* s = new SyncSession(session_context_.get(), this, info, r, w); | 731 SyncSession* s = CreateSyncSession(info); |
| 612 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), POLL, s, FROM_HERE); | 732 ScheduleSyncSessionJob(TimeDelta::FromSeconds(0), SyncSessionJob::POLL, s, |
| 733 FROM_HERE); | |
| 613 } | 734 } |
| 614 | 735 |
| 615 void SyncerThread::Unthrottle() { | 736 void SyncerThread::Unthrottle() { |
| 616 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); | 737 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); |
| 738 DoCanaryJob(); | |
| 617 wait_interval_.reset(); | 739 wait_interval_.reset(); |
| 618 } | 740 } |
| 619 | 741 |
| 620 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { | 742 void SyncerThread::Notify(SyncEngineEvent::EventCause cause) { |
| 621 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); | 743 DCHECK_EQ(MessageLoop::current(), thread_.message_loop()); |
| 622 session_context_->NotifyListeners(SyncEngineEvent(cause)); | 744 session_context_->NotifyListeners(SyncEngineEvent(cause)); |
| 623 } | 745 } |
| 624 | 746 |
| 625 bool SyncerThread::IsBackingOff() const { | 747 bool SyncerThread::IsBackingOff() const { |
| 626 return wait_interval_.get() && wait_interval_->mode == | 748 return wait_interval_.get() && wait_interval_->mode == |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 &SyncerThread::CheckServerConnectionManagerStatus, | 784 &SyncerThread::CheckServerConnectionManagerStatus, |
| 663 event.connection_code)); | 785 event.connection_code)); |
| 664 } | 786 } |
| 665 | 787 |
| 666 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { | 788 void SyncerThread::set_notifications_enabled(bool notifications_enabled) { |
| 667 session_context_->set_notifications_enabled(notifications_enabled); | 789 session_context_->set_notifications_enabled(notifications_enabled); |
| 668 } | 790 } |
| 669 | 791 |
| 670 } // s3 | 792 } // s3 |
| 671 } // browser_sync | 793 } // browser_sync |
| OLD | NEW |