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