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

Side by Side Diff: components/sync/engine_impl/sync_scheduler_impl.cc

Issue 2258873003: [Sync] Move sessions/ to engine/cycle/ and rename things to match. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/sync/engine_impl/sync_scheduler_impl.h" 5 #include "components/sync/engine_impl/sync_scheduler_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 10 matching lines...) Expand all
21 #include "components/sync/engine_impl/backoff_delay_provider.h" 21 #include "components/sync/engine_impl/backoff_delay_provider.h"
22 #include "components/sync/engine_impl/syncer.h" 22 #include "components/sync/engine_impl/syncer.h"
23 #include "components/sync/protocol/proto_enum_conversions.h" 23 #include "components/sync/protocol/proto_enum_conversions.h"
24 #include "components/sync/protocol/sync.pb.h" 24 #include "components/sync/protocol/sync.pb.h"
25 25
26 using base::TimeDelta; 26 using base::TimeDelta;
27 using base::TimeTicks; 27 using base::TimeTicks;
28 28
29 namespace syncer { 29 namespace syncer {
30 30
31 using sessions::SyncSession;
32 using sessions::SyncSessionSnapshot;
33 using sync_pb::GetUpdatesCallerInfo; 31 using sync_pb::GetUpdatesCallerInfo;
34 32
35 namespace { 33 namespace {
36 34
37 bool IsConfigRelatedUpdateSourceValue( 35 bool IsConfigRelatedUpdateSourceValue(
38 GetUpdatesCallerInfo::GetUpdatesSource source) { 36 GetUpdatesCallerInfo::GetUpdatesSource source) {
39 switch (source) { 37 switch (source) {
40 case GetUpdatesCallerInfo::RECONFIGURATION: 38 case GetUpdatesCallerInfo::RECONFIGURATION:
41 case GetUpdatesCallerInfo::MIGRATION: 39 case GetUpdatesCallerInfo::MIGRATION:
42 case GetUpdatesCallerInfo::NEW_CLIENT: 40 case GetUpdatesCallerInfo::NEW_CLIENT:
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 161
164 #define SLOG(severity) LOG(severity) << name_ << ": " 162 #define SLOG(severity) LOG(severity) << name_ << ": "
165 163
166 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " 164 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": "
167 165
168 #define SDVLOG_LOC(from_here, verbose_level) \ 166 #define SDVLOG_LOC(from_here, verbose_level) \
169 DVLOG_LOC(from_here, verbose_level) << name_ << ": " 167 DVLOG_LOC(from_here, verbose_level) << name_ << ": "
170 168
171 SyncSchedulerImpl::SyncSchedulerImpl(const std::string& name, 169 SyncSchedulerImpl::SyncSchedulerImpl(const std::string& name,
172 BackoffDelayProvider* delay_provider, 170 BackoffDelayProvider* delay_provider,
173 sessions::SyncSessionContext* context, 171 SyncCycleContext* context,
174 Syncer* syncer) 172 Syncer* syncer)
175 : name_(name), 173 : name_(name),
176 started_(false), 174 started_(false),
177 syncer_short_poll_interval_seconds_( 175 syncer_short_poll_interval_seconds_(
178 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), 176 TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)),
179 syncer_long_poll_interval_seconds_( 177 syncer_long_poll_interval_seconds_(
180 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), 178 TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)),
181 mode_(CONFIGURATION_MODE), 179 mode_(CONFIGURATION_MODE),
182 delay_provider_(delay_provider), 180 delay_provider_(delay_provider),
183 syncer_(syncer), 181 syncer_(syncer),
184 session_context_(context), 182 cycle_context_(context),
185 next_sync_session_job_priority_(NORMAL_PRIORITY), 183 next_sync_cycle_job_priority_(NORMAL_PRIORITY),
186 weak_ptr_factory_(this), 184 weak_ptr_factory_(this),
187 weak_ptr_factory_for_weak_handle_(this) { 185 weak_ptr_factory_for_weak_handle_(this) {
188 weak_handle_this_ = 186 weak_handle_this_ =
189 MakeWeakHandle(weak_ptr_factory_for_weak_handle_.GetWeakPtr()); 187 MakeWeakHandle(weak_ptr_factory_for_weak_handle_.GetWeakPtr());
190 } 188 }
191 189
192 SyncSchedulerImpl::~SyncSchedulerImpl() { 190 SyncSchedulerImpl::~SyncSchedulerImpl() {
193 DCHECK(CalledOnValidThread()); 191 DCHECK(CalledOnValidThread());
194 Stop(); 192 Stop();
195 } 193 }
196 194
197 void SyncSchedulerImpl::OnCredentialsUpdated() { 195 void SyncSchedulerImpl::OnCredentialsUpdated() {
198 DCHECK(CalledOnValidThread()); 196 DCHECK(CalledOnValidThread());
199 197
200 if (HttpResponse::SYNC_AUTH_ERROR == 198 if (HttpResponse::SYNC_AUTH_ERROR ==
201 session_context_->connection_manager()->server_status()) { 199 cycle_context_->connection_manager()->server_status()) {
202 OnServerConnectionErrorFixed(); 200 OnServerConnectionErrorFixed();
203 } 201 }
204 } 202 }
205 203
206 void SyncSchedulerImpl::OnConnectionStatusChange() { 204 void SyncSchedulerImpl::OnConnectionStatusChange() {
207 if (HttpResponse::CONNECTION_UNAVAILABLE == 205 if (HttpResponse::CONNECTION_UNAVAILABLE ==
208 session_context_->connection_manager()->server_status()) { 206 cycle_context_->connection_manager()->server_status()) {
209 // Optimistically assume that the connection is fixed and try 207 // Optimistically assume that the connection is fixed and try
210 // connecting. 208 // connecting.
211 OnServerConnectionErrorFixed(); 209 OnServerConnectionErrorFixed();
212 } 210 }
213 } 211 }
214 212
215 void SyncSchedulerImpl::OnServerConnectionErrorFixed() { 213 void SyncSchedulerImpl::OnServerConnectionErrorFixed() {
216 // There could be a pending nudge or configuration job in several cases: 214 // There could be a pending nudge or configuration job in several cases:
217 // 215 //
218 // 1. We're in exponential backoff. 216 // 1. We're in exponential backoff.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 258
261 if (old_mode != mode_ && mode_ == NORMAL_MODE) { 259 if (old_mode != mode_ && mode_ == NORMAL_MODE) {
262 // We just got back to normal mode. Let's try to run the work that was 260 // We just got back to normal mode. Let's try to run the work that was
263 // queued up while we were configuring. 261 // queued up while we were configuring.
264 262
265 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. 263 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed.
266 264
267 // Update our current time before checking IsRetryRequired(). 265 // Update our current time before checking IsRetryRequired().
268 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); 266 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now());
269 if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY)) { 267 if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY)) {
270 TrySyncSessionJob(); 268 TrySyncCycleJob();
271 } 269 }
272 } 270 }
273 } 271 }
274 272
275 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { 273 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() {
276 ModelTypeSet enabled_types = session_context_->GetEnabledTypes(); 274 ModelTypeSet enabled_types = cycle_context_->GetEnabledTypes();
277 ModelTypeSet enabled_protocol_types = 275 ModelTypeSet enabled_protocol_types =
278 Intersection(ProtocolTypes(), enabled_types); 276 Intersection(ProtocolTypes(), enabled_types);
279 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); 277 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes();
280 return Difference(enabled_protocol_types, throttled_types); 278 return Difference(enabled_protocol_types, throttled_types);
281 } 279 }
282 280
283 void SyncSchedulerImpl::SendInitialSnapshot() { 281 void SyncSchedulerImpl::SendInitialSnapshot() {
284 DCHECK(CalledOnValidThread()); 282 DCHECK(CalledOnValidThread());
285 std::unique_ptr<SyncSession> dummy( 283 std::unique_ptr<SyncCycle> dummy(SyncCycle::Build(cycle_context_, this));
286 SyncSession::Build(session_context_, this));
287 SyncCycleEvent event(SyncCycleEvent::STATUS_CHANGED); 284 SyncCycleEvent event(SyncCycleEvent::STATUS_CHANGED);
288 event.snapshot = dummy->TakeSnapshot(); 285 event.snapshot = dummy->TakeSnapshot();
289 FOR_EACH_OBSERVER(SyncEngineEventListener, *session_context_->listeners(), 286 FOR_EACH_OBSERVER(SyncEngineEventListener, *cycle_context_->listeners(),
290 OnSyncCycleEvent(event)); 287 OnSyncCycleEvent(event));
291 } 288 }
292 289
293 namespace { 290 namespace {
294 291
295 // Helper to extract the routing info corresponding to types in 292 // Helper to extract the routing info corresponding to types in
296 // |types_to_download| from |current_routes|. 293 // |types_to_download| from |current_routes|.
297 void BuildModelSafeParams(ModelTypeSet types_to_download, 294 void BuildModelSafeParams(ModelTypeSet types_to_download,
298 const ModelSafeRoutingInfo& current_routes, 295 const ModelSafeRoutingInfo& current_routes,
299 ModelSafeRoutingInfo* result_routes) { 296 ModelSafeRoutingInfo* result_routes) {
(...skipping 18 matching lines...) Expand all
318 CHECK(started_) << "Scheduler must be running to configure."; 315 CHECK(started_) << "Scheduler must be running to configure.";
319 SDVLOG(2) << "Reconfiguring syncer."; 316 SDVLOG(2) << "Reconfiguring syncer.";
320 317
321 // Only one configuration is allowed at a time. Verify we're not waiting 318 // Only one configuration is allowed at a time. Verify we're not waiting
322 // for a pending configure job. 319 // for a pending configure job.
323 DCHECK(!pending_configure_params_); 320 DCHECK(!pending_configure_params_);
324 321
325 ModelSafeRoutingInfo restricted_routes; 322 ModelSafeRoutingInfo restricted_routes;
326 BuildModelSafeParams(params.types_to_download, params.routing_info, 323 BuildModelSafeParams(params.types_to_download, params.routing_info,
327 &restricted_routes); 324 &restricted_routes);
328 session_context_->SetRoutingInfo(restricted_routes); 325 cycle_context_->SetRoutingInfo(restricted_routes);
329 326
330 // Only reconfigure if we have types to download. 327 // Only reconfigure if we have types to download.
331 if (!params.types_to_download.Empty()) { 328 if (!params.types_to_download.Empty()) {
332 pending_configure_params_.reset(new ConfigurationParams(params)); 329 pending_configure_params_.reset(new ConfigurationParams(params));
333 TrySyncSessionJob(); 330 TrySyncCycleJob();
334 } else { 331 } else {
335 SDVLOG(2) << "No change in routing info, calling ready task directly."; 332 SDVLOG(2) << "No change in routing info, calling ready task directly.";
336 params.ready_task.Run(); 333 params.ready_task.Run();
337 } 334 }
338 } 335 }
339 336
340 void SyncSchedulerImpl::ScheduleClearServerData(const ClearParams& params) { 337 void SyncSchedulerImpl::ScheduleClearServerData(const ClearParams& params) {
341 DCHECK(CalledOnValidThread()); 338 DCHECK(CalledOnValidThread());
342 DCHECK_EQ(CLEAR_SERVER_DATA_MODE, mode_); 339 DCHECK_EQ(CLEAR_SERVER_DATA_MODE, mode_);
343 DCHECK(!pending_configure_params_); 340 DCHECK(!pending_configure_params_);
344 DCHECK(!params.report_success_task.is_null()); 341 DCHECK(!params.report_success_task.is_null());
345 CHECK(started_) << "Scheduler must be running to clear."; 342 CHECK(started_) << "Scheduler must be running to clear.";
346 pending_clear_params_.reset(new ClearParams(params)); 343 pending_clear_params_.reset(new ClearParams(params));
347 TrySyncSessionJob(); 344 TrySyncCycleJob();
348 } 345 }
349 346
350 bool SyncSchedulerImpl::CanRunJobNow(JobPriority priority) { 347 bool SyncSchedulerImpl::CanRunJobNow(JobPriority priority) {
351 DCHECK(CalledOnValidThread()); 348 DCHECK(CalledOnValidThread());
352 if (IsCurrentlyThrottled()) { 349 if (IsCurrentlyThrottled()) {
353 SDVLOG(1) << "Unable to run a job because we're throttled."; 350 SDVLOG(1) << "Unable to run a job because we're throttled.";
354 return false; 351 return false;
355 } 352 }
356 353
357 if (IsBackingOff() && priority != CANARY_PRIORITY) { 354 if (IsBackingOff() && priority != CANARY_PRIORITY) {
358 SDVLOG(1) << "Unable to run a job because we're backing off."; 355 SDVLOG(1) << "Unable to run a job because we're backing off.";
359 return false; 356 return false;
360 } 357 }
361 358
362 if (session_context_->connection_manager()->HasInvalidAuthToken()) { 359 if (cycle_context_->connection_manager()->HasInvalidAuthToken()) {
363 SDVLOG(1) << "Unable to run a job because we have no valid auth token."; 360 SDVLOG(1) << "Unable to run a job because we have no valid auth token.";
364 return false; 361 return false;
365 } 362 }
366 363
367 return true; 364 return true;
368 } 365 }
369 366
370 bool SyncSchedulerImpl::CanRunNudgeJobNow(JobPriority priority) { 367 bool SyncSchedulerImpl::CanRunNudgeJobNow(JobPriority priority) {
371 DCHECK(CalledOnValidThread()); 368 DCHECK(CalledOnValidThread());
372 369
373 if (!CanRunJobNow(priority)) { 370 if (!CanRunJobNow(priority)) {
374 SDVLOG(1) << "Unable to run a nudge job right now"; 371 SDVLOG(1) << "Unable to run a nudge job right now";
375 return false; 372 return false;
376 } 373 }
377 374
378 const ModelTypeSet enabled_types = session_context_->GetEnabledTypes(); 375 const ModelTypeSet enabled_types = cycle_context_->GetEnabledTypes();
379 if (nudge_tracker_.GetThrottledTypes().HasAll(enabled_types)) { 376 if (nudge_tracker_.GetThrottledTypes().HasAll(enabled_types)) {
380 SDVLOG(1) << "Not running a nudge because we're fully type throttled."; 377 SDVLOG(1) << "Not running a nudge because we're fully type throttled.";
381 return false; 378 return false;
382 } 379 }
383 380
384 if (mode_ != NORMAL_MODE) { 381 if (mode_ != NORMAL_MODE) {
385 SDVLOG(1) << "Not running nudge because we're not in normal mode."; 382 SDVLOG(1) << "Not running nudge because we're not in normal mode.";
386 return false; 383 return false;
387 } 384 }
388 385
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 ENUM_CASE(NORMAL_MODE); 480 ENUM_CASE(NORMAL_MODE);
484 } 481 }
485 return ""; 482 return "";
486 } 483 }
487 484
488 void SyncSchedulerImpl::SetDefaultNudgeDelay(base::TimeDelta delay_ms) { 485 void SyncSchedulerImpl::SetDefaultNudgeDelay(base::TimeDelta delay_ms) {
489 DCHECK(CalledOnValidThread()); 486 DCHECK(CalledOnValidThread());
490 nudge_tracker_.SetDefaultNudgeDelay(delay_ms); 487 nudge_tracker_.SetDefaultNudgeDelay(delay_ms);
491 } 488 }
492 489
493 void SyncSchedulerImpl::DoNudgeSyncSessionJob(JobPriority priority) { 490 void SyncSchedulerImpl::DoNudgeSyncCycleJob(JobPriority priority) {
494 DCHECK(CalledOnValidThread()); 491 DCHECK(CalledOnValidThread());
495 DCHECK(CanRunNudgeJobNow(priority)); 492 DCHECK(CanRunNudgeJobNow(priority));
496 493
497 DVLOG(2) << "Will run normal mode sync cycle with types " 494 DVLOG(2) << "Will run normal mode sync cycle with types "
498 << ModelTypeSetToString(session_context_->GetEnabledTypes()); 495 << ModelTypeSetToString(cycle_context_->GetEnabledTypes());
499 std::unique_ptr<SyncSession> session( 496 std::unique_ptr<SyncCycle> cycle(SyncCycle::Build(cycle_context_, this));
500 SyncSession::Build(session_context_, this));
501 bool success = syncer_->NormalSyncShare(GetEnabledAndUnthrottledTypes(), 497 bool success = syncer_->NormalSyncShare(GetEnabledAndUnthrottledTypes(),
502 &nudge_tracker_, session.get()); 498 &nudge_tracker_, cycle.get());
503 499
504 if (success) { 500 if (success) {
505 // That cycle took care of any outstanding work we had. 501 // That cycle took care of any outstanding work we had.
506 SDVLOG(2) << "Nudge succeeded."; 502 SDVLOG(2) << "Nudge succeeded.";
507 nudge_tracker_.RecordSuccessfulSyncCycle(); 503 nudge_tracker_.RecordSuccessfulSyncCycle();
508 scheduled_nudge_time_ = base::TimeTicks(); 504 scheduled_nudge_time_ = base::TimeTicks();
509 HandleSuccess(); 505 HandleSuccess();
510 506
511 // If this was a canary, we may need to restart the poll timer (the poll 507 // If this was a canary, we may need to restart the poll timer (the poll
512 // timer may have fired while the scheduler was in an error state, ignoring 508 // timer may have fired while the scheduler was in an error state, ignoring
513 // the poll). 509 // the poll).
514 if (!poll_timer_.IsRunning()) { 510 if (!poll_timer_.IsRunning()) {
515 SDVLOG(1) << "Canary succeeded, restarting polling."; 511 SDVLOG(1) << "Canary succeeded, restarting polling.";
516 AdjustPolling(UPDATE_INTERVAL); 512 AdjustPolling(UPDATE_INTERVAL);
517 } 513 }
518 } else { 514 } else {
519 HandleFailure(session->status_controller().model_neutral_state()); 515 HandleFailure(cycle->status_controller().model_neutral_state());
520 } 516 }
521 } 517 }
522 518
523 void SyncSchedulerImpl::DoConfigurationSyncSessionJob(JobPriority priority) { 519 void SyncSchedulerImpl::DoConfigurationSyncCycleJob(JobPriority priority) {
524 DCHECK(CalledOnValidThread()); 520 DCHECK(CalledOnValidThread());
525 DCHECK_EQ(mode_, CONFIGURATION_MODE); 521 DCHECK_EQ(mode_, CONFIGURATION_MODE);
526 DCHECK(pending_configure_params_ != NULL); 522 DCHECK(pending_configure_params_ != NULL);
527 523
528 if (!CanRunJobNow(priority)) { 524 if (!CanRunJobNow(priority)) {
529 SDVLOG(2) << "Unable to run configure job right now."; 525 SDVLOG(2) << "Unable to run configure job right now.";
530 RunAndReset(&pending_configure_params_->retry_task); 526 RunAndReset(&pending_configure_params_->retry_task);
531 return; 527 return;
532 } 528 }
533 529
534 SDVLOG(2) << "Will run configure SyncShare with types " 530 SDVLOG(2) << "Will run configure SyncShare with types "
535 << ModelTypeSetToString(session_context_->GetEnabledTypes()); 531 << ModelTypeSetToString(cycle_context_->GetEnabledTypes());
536 std::unique_ptr<SyncSession> session( 532 std::unique_ptr<SyncCycle> cycle(SyncCycle::Build(cycle_context_, this));
537 SyncSession::Build(session_context_, this));
538 bool success = syncer_->ConfigureSyncShare( 533 bool success = syncer_->ConfigureSyncShare(
539 pending_configure_params_->types_to_download, 534 pending_configure_params_->types_to_download,
540 pending_configure_params_->source, session.get()); 535 pending_configure_params_->source, cycle.get());
541 536
542 if (success) { 537 if (success) {
543 SDVLOG(2) << "Configure succeeded."; 538 SDVLOG(2) << "Configure succeeded.";
544 pending_configure_params_->ready_task.Run(); 539 pending_configure_params_->ready_task.Run();
545 pending_configure_params_.reset(); 540 pending_configure_params_.reset();
546 HandleSuccess(); 541 HandleSuccess();
547 } else { 542 } else {
548 HandleFailure(session->status_controller().model_neutral_state()); 543 HandleFailure(cycle->status_controller().model_neutral_state());
549 // Sync cycle might receive response from server that causes scheduler to 544 // Sync cycle might receive response from server that causes scheduler to
550 // stop and draws pending_configure_params_ invalid. 545 // stop and draws pending_configure_params_ invalid.
551 if (started_) 546 if (started_)
552 RunAndReset(&pending_configure_params_->retry_task); 547 RunAndReset(&pending_configure_params_->retry_task);
553 } 548 }
554 } 549 }
555 550
556 void SyncSchedulerImpl::DoClearServerDataSyncSessionJob(JobPriority priority) { 551 void SyncSchedulerImpl::DoClearServerDataSyncCycleJob(JobPriority priority) {
557 DCHECK(CalledOnValidThread()); 552 DCHECK(CalledOnValidThread());
558 DCHECK_EQ(mode_, CLEAR_SERVER_DATA_MODE); 553 DCHECK_EQ(mode_, CLEAR_SERVER_DATA_MODE);
559 554
560 if (!CanRunJobNow(priority)) { 555 if (!CanRunJobNow(priority)) {
561 SDVLOG(2) << "Unable to run clear server data job right now."; 556 SDVLOG(2) << "Unable to run clear server data job right now.";
562 RunAndReset(&pending_configure_params_->retry_task); 557 RunAndReset(&pending_configure_params_->retry_task);
563 return; 558 return;
564 } 559 }
565 560
566 std::unique_ptr<SyncSession> session( 561 std::unique_ptr<SyncCycle> cycle(SyncCycle::Build(cycle_context_, this));
567 SyncSession::Build(session_context_, this)); 562 const bool success = syncer_->PostClearServerData(cycle.get());
568 const bool success = syncer_->PostClearServerData(session.get());
569 if (!success) { 563 if (!success) {
570 HandleFailure(session->status_controller().model_neutral_state()); 564 HandleFailure(cycle->status_controller().model_neutral_state());
571 return; 565 return;
572 } 566 }
573 567
574 SDVLOG(2) << "Clear succeeded."; 568 SDVLOG(2) << "Clear succeeded.";
575 pending_clear_params_->report_success_task.Run(); 569 pending_clear_params_->report_success_task.Run();
576 pending_clear_params_.reset(); 570 pending_clear_params_.reset();
577 HandleSuccess(); 571 HandleSuccess();
578 } 572 }
579 573
580 void SyncSchedulerImpl::HandleSuccess() { 574 void SyncSchedulerImpl::HandleSuccess() {
581 // If we're here, then we successfully reached the server. End all backoff. 575 // If we're here, then we successfully reached the server. End all backoff.
582 wait_interval_.reset(); 576 wait_interval_.reset();
583 NotifyRetryTime(base::Time()); 577 NotifyRetryTime(base::Time());
584 } 578 }
585 579
586 void SyncSchedulerImpl::HandleFailure( 580 void SyncSchedulerImpl::HandleFailure(
587 const sessions::ModelNeutralState& model_neutral_state) { 581 const ModelNeutralState& model_neutral_state) {
588 if (IsCurrentlyThrottled()) { 582 if (IsCurrentlyThrottled()) {
589 SDVLOG(2) << "Was throttled during previous sync cycle."; 583 SDVLOG(2) << "Was throttled during previous sync cycle.";
590 } else if (!IsBackingOff()) { 584 } else if (!IsBackingOff()) {
591 // Setup our backoff if this is our first such failure. 585 // Setup our backoff if this is our first such failure.
592 TimeDelta length = delay_provider_->GetDelay( 586 TimeDelta length = delay_provider_->GetDelay(
593 delay_provider_->GetInitialDelay(model_neutral_state)); 587 delay_provider_->GetInitialDelay(model_neutral_state));
594 wait_interval_.reset( 588 wait_interval_.reset(
595 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); 589 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length));
596 SDVLOG(2) << "Sync cycle failed. Will back off for " 590 SDVLOG(2) << "Sync cycle failed. Will back off for "
597 << wait_interval_->length.InMilliseconds() << "ms."; 591 << wait_interval_->length.InMilliseconds() << "ms.";
598 } else { 592 } else {
599 // Increase our backoff interval and schedule another retry. 593 // Increase our backoff interval and schedule another retry.
600 TimeDelta length = delay_provider_->GetDelay(wait_interval_->length); 594 TimeDelta length = delay_provider_->GetDelay(wait_interval_->length);
601 wait_interval_.reset( 595 wait_interval_.reset(
602 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); 596 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length));
603 SDVLOG(2) << "Sync cycle failed. Will back off for " 597 SDVLOG(2) << "Sync cycle failed. Will back off for "
604 << wait_interval_->length.InMilliseconds() << "ms."; 598 << wait_interval_->length.InMilliseconds() << "ms.";
605 } 599 }
606 RestartWaiting(); 600 RestartWaiting();
607 } 601 }
608 602
609 void SyncSchedulerImpl::DoPollSyncSessionJob() { 603 void SyncSchedulerImpl::DoPollSyncCycleJob() {
610 SDVLOG(2) << "Polling with types " 604 SDVLOG(2) << "Polling with types "
611 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); 605 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes());
612 std::unique_ptr<SyncSession> session( 606 std::unique_ptr<SyncCycle> cycle(SyncCycle::Build(cycle_context_, this));
613 SyncSession::Build(session_context_, this));
614 bool success = 607 bool success =
615 syncer_->PollSyncShare(GetEnabledAndUnthrottledTypes(), session.get()); 608 syncer_->PollSyncShare(GetEnabledAndUnthrottledTypes(), cycle.get());
616 609
617 // Only restart the timer if the poll succeeded. Otherwise rely on normal 610 // Only restart the timer if the poll succeeded. Otherwise rely on normal
618 // failure handling to retry with backoff. 611 // failure handling to retry with backoff.
619 if (success) { 612 if (success) {
620 AdjustPolling(FORCE_RESET); 613 AdjustPolling(FORCE_RESET);
621 HandleSuccess(); 614 HandleSuccess();
622 } else { 615 } else {
623 HandleFailure(session->status_controller().model_neutral_state()); 616 HandleFailure(cycle->status_controller().model_neutral_state());
624 } 617 }
625 } 618 }
626 619
627 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { 620 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) {
628 DCHECK(CalledOnValidThread()); 621 DCHECK(CalledOnValidThread());
629 base::TimeTicks now = TimeTicks::Now(); 622 base::TimeTicks now = TimeTicks::Now();
630 // Update timing information for how often datatypes are triggering nudges. 623 // Update timing information for how often datatypes are triggering nudges.
631 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { 624 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) {
632 base::TimeTicks previous = last_local_nudges_by_model_type_[iter.Get()]; 625 base::TimeTicks previous = last_local_nudges_by_model_type_[iter.Get()];
633 last_local_nudges_by_model_type_[iter.Get()] = now; 626 last_local_nudges_by_model_type_[iter.Get()] = now;
634 if (previous.is_null()) 627 if (previous.is_null())
635 continue; 628 continue;
636 629
637 #define PER_DATA_TYPE_MACRO(type_str) \ 630 #define PER_DATA_TYPE_MACRO(type_str) \
638 SYNC_FREQ_HISTOGRAM("Sync.Freq" type_str, now - previous); 631 SYNC_FREQ_HISTOGRAM("Sync.Freq" type_str, now - previous);
639 SYNC_DATA_TYPE_HISTOGRAM(iter.Get()); 632 SYNC_DATA_TYPE_HISTOGRAM(iter.Get());
640 #undef PER_DATA_TYPE_MACRO 633 #undef PER_DATA_TYPE_MACRO
641 } 634 }
642 } 635 }
643 636
644 TimeDelta SyncSchedulerImpl::GetPollInterval() { 637 TimeDelta SyncSchedulerImpl::GetPollInterval() {
645 return (!session_context_->notifications_enabled() || 638 return (!cycle_context_->notifications_enabled() ||
646 !session_context_->ShouldFetchUpdatesBeforeCommit()) 639 !cycle_context_->ShouldFetchUpdatesBeforeCommit())
647 ? syncer_short_poll_interval_seconds_ 640 ? syncer_short_poll_interval_seconds_
648 : syncer_long_poll_interval_seconds_; 641 : syncer_long_poll_interval_seconds_;
649 } 642 }
650 643
651 void SyncSchedulerImpl::AdjustPolling(PollAdjustType type) { 644 void SyncSchedulerImpl::AdjustPolling(PollAdjustType type) {
652 DCHECK(CalledOnValidThread()); 645 DCHECK(CalledOnValidThread());
653 if (!started_) 646 if (!started_)
654 return; 647 return;
655 648
656 TimeDelta poll_interval = GetPollInterval(); 649 TimeDelta poll_interval = GetPollInterval();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 wait_interval_.reset(); 711 wait_interval_.reset();
719 NotifyRetryTime(base::Time()); 712 NotifyRetryTime(base::Time());
720 poll_timer_.Stop(); 713 poll_timer_.Stop();
721 pending_wakeup_timer_.Stop(); 714 pending_wakeup_timer_.Stop();
722 pending_configure_params_.reset(); 715 pending_configure_params_.reset();
723 pending_clear_params_.reset(); 716 pending_clear_params_.reset();
724 if (started_) 717 if (started_)
725 started_ = false; 718 started_ = false;
726 } 719 }
727 720
728 // This is the only place where we invoke DoSyncSessionJob with canary 721 // This is the only place where we invoke DoSyncCycleJob with canary
729 // privileges. Everyone else should use NORMAL_PRIORITY. 722 // privileges. Everyone else should use NORMAL_PRIORITY.
730 void SyncSchedulerImpl::TryCanaryJob() { 723 void SyncSchedulerImpl::TryCanaryJob() {
731 next_sync_session_job_priority_ = CANARY_PRIORITY; 724 next_sync_cycle_job_priority_ = CANARY_PRIORITY;
732 SDVLOG(2) << "Attempting canary job"; 725 SDVLOG(2) << "Attempting canary job";
733 TrySyncSessionJob(); 726 TrySyncCycleJob();
734 } 727 }
735 728
736 void SyncSchedulerImpl::TrySyncSessionJob() { 729 void SyncSchedulerImpl::TrySyncCycleJob() {
737 // Post call to TrySyncSessionJobImpl on current thread. Later request for 730 // Post call to TrySyncCycleJobImpl on current thread. Later request for
738 // access token will be here. 731 // access token will be here.
739 base::ThreadTaskRunnerHandle::Get()->PostTask( 732 base::ThreadTaskRunnerHandle::Get()->PostTask(
740 FROM_HERE, base::Bind(&SyncSchedulerImpl::TrySyncSessionJobImpl, 733 FROM_HERE, base::Bind(&SyncSchedulerImpl::TrySyncCycleJobImpl,
741 weak_ptr_factory_.GetWeakPtr())); 734 weak_ptr_factory_.GetWeakPtr()));
742 } 735 }
743 736
744 void SyncSchedulerImpl::TrySyncSessionJobImpl() { 737 void SyncSchedulerImpl::TrySyncCycleJobImpl() {
745 JobPriority priority = next_sync_session_job_priority_; 738 JobPriority priority = next_sync_cycle_job_priority_;
746 next_sync_session_job_priority_ = NORMAL_PRIORITY; 739 next_sync_cycle_job_priority_ = NORMAL_PRIORITY;
747 740
748 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); 741 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now());
749 742
750 DCHECK(CalledOnValidThread()); 743 DCHECK(CalledOnValidThread());
751 if (mode_ == CONFIGURATION_MODE) { 744 if (mode_ == CONFIGURATION_MODE) {
752 if (pending_configure_params_) { 745 if (pending_configure_params_) {
753 SDVLOG(2) << "Found pending configure job"; 746 SDVLOG(2) << "Found pending configure job";
754 DoConfigurationSyncSessionJob(priority); 747 DoConfigurationSyncCycleJob(priority);
755 } 748 }
756 } else if (mode_ == CLEAR_SERVER_DATA_MODE) { 749 } else if (mode_ == CLEAR_SERVER_DATA_MODE) {
757 if (pending_clear_params_) { 750 if (pending_clear_params_) {
758 DoClearServerDataSyncSessionJob(priority); 751 DoClearServerDataSyncCycleJob(priority);
759 } 752 }
760 } else if (CanRunNudgeJobNow(priority)) { 753 } else if (CanRunNudgeJobNow(priority)) {
761 if (nudge_tracker_.IsSyncRequired()) { 754 if (nudge_tracker_.IsSyncRequired()) {
762 SDVLOG(2) << "Found pending nudge job"; 755 SDVLOG(2) << "Found pending nudge job";
763 DoNudgeSyncSessionJob(priority); 756 DoNudgeSyncCycleJob(priority);
764 } else if (((base::TimeTicks::Now() - last_poll_reset_) >= 757 } else if (((base::TimeTicks::Now() - last_poll_reset_) >=
765 GetPollInterval())) { 758 GetPollInterval())) {
766 SDVLOG(2) << "Found pending poll"; 759 SDVLOG(2) << "Found pending poll";
767 DoPollSyncSessionJob(); 760 DoPollSyncCycleJob();
768 } 761 }
769 } else { 762 } else {
770 // We must be in an error state. Transitioning out of each of these 763 // We must be in an error state. Transitioning out of each of these
771 // error states should trigger a canary job. 764 // error states should trigger a canary job.
772 DCHECK(IsCurrentlyThrottled() || IsBackingOff() || 765 DCHECK(IsCurrentlyThrottled() || IsBackingOff() ||
773 session_context_->connection_manager()->HasInvalidAuthToken()); 766 cycle_context_->connection_manager()->HasInvalidAuthToken());
774 } 767 }
775 768
776 if (IsBackingOff() && !pending_wakeup_timer_.IsRunning()) { 769 if (IsBackingOff() && !pending_wakeup_timer_.IsRunning()) {
777 // If we succeeded, our wait interval would have been cleared. If it hasn't 770 // If we succeeded, our wait interval would have been cleared. If it hasn't
778 // been cleared, then we should increase our backoff interval and schedule 771 // been cleared, then we should increase our backoff interval and schedule
779 // another retry. 772 // another retry.
780 TimeDelta length = delay_provider_->GetDelay(wait_interval_->length); 773 TimeDelta length = delay_provider_->GetDelay(wait_interval_->length);
781 wait_interval_.reset( 774 wait_interval_.reset(
782 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); 775 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length));
783 SDVLOG(2) << "Sync cycle failed. Will back off for " 776 SDVLOG(2) << "Sync cycle failed. Will back off for "
784 << wait_interval_->length.InMilliseconds() << "ms."; 777 << wait_interval_->length.InMilliseconds() << "ms.";
785 RestartWaiting(); 778 RestartWaiting();
786 } 779 }
787 } 780 }
788 781
789 void SyncSchedulerImpl::PollTimerCallback() { 782 void SyncSchedulerImpl::PollTimerCallback() {
790 DCHECK(CalledOnValidThread()); 783 DCHECK(CalledOnValidThread());
791 CHECK(!syncer_->IsSyncing()); 784 CHECK(!syncer_->IsSyncing());
792 785
793 TrySyncSessionJob(); 786 TrySyncCycleJob();
794 } 787 }
795 788
796 void SyncSchedulerImpl::RetryTimerCallback() { 789 void SyncSchedulerImpl::RetryTimerCallback() {
797 TrySyncSessionJob(); 790 TrySyncCycleJob();
798 } 791 }
799 792
800 void SyncSchedulerImpl::Unthrottle() { 793 void SyncSchedulerImpl::Unthrottle() {
801 DCHECK(CalledOnValidThread()); 794 DCHECK(CalledOnValidThread());
802 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); 795 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode);
803 796
804 // We're no longer throttled, so clear the wait interval. 797 // We're no longer throttled, so clear the wait interval.
805 wait_interval_.reset(); 798 wait_interval_.reset();
806 NotifyRetryTime(base::Time()); 799 NotifyRetryTime(base::Time());
807 NotifyThrottledTypesChanged(nudge_tracker_.GetThrottledTypes()); 800 NotifyThrottledTypesChanged(nudge_tracker_.GetThrottledTypes());
(...skipping 16 matching lines...) Expand all
824 base::TimeDelta time_until_next_unthrottle = 817 base::TimeDelta time_until_next_unthrottle =
825 nudge_tracker_.GetTimeUntilNextUnthrottle(now); 818 nudge_tracker_.GetTimeUntilNextUnthrottle(now);
826 type_unthrottle_timer_.Start(FROM_HERE, time_until_next_unthrottle, 819 type_unthrottle_timer_.Start(FROM_HERE, time_until_next_unthrottle,
827 base::Bind(&SyncSchedulerImpl::TypeUnthrottle, 820 base::Bind(&SyncSchedulerImpl::TypeUnthrottle,
828 weak_ptr_factory_.GetWeakPtr(), 821 weak_ptr_factory_.GetWeakPtr(),
829 now + time_until_next_unthrottle)); 822 now + time_until_next_unthrottle));
830 } 823 }
831 824
832 // Maybe this is a good time to run a nudge job. Let's try it. 825 // Maybe this is a good time to run a nudge job. Let's try it.
833 if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY)) 826 if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY))
834 TrySyncSessionJob(); 827 TrySyncCycleJob();
835 } 828 }
836 829
837 void SyncSchedulerImpl::PerformDelayedNudge() { 830 void SyncSchedulerImpl::PerformDelayedNudge() {
838 // Circumstances may have changed since we scheduled this delayed nudge. 831 // Circumstances may have changed since we scheduled this delayed nudge.
839 // We must check to see if it's OK to run the job before we do so. 832 // We must check to see if it's OK to run the job before we do so.
840 if (CanRunNudgeJobNow(NORMAL_PRIORITY)) 833 if (CanRunNudgeJobNow(NORMAL_PRIORITY))
841 TrySyncSessionJob(); 834 TrySyncCycleJob();
842 835
843 // We're not responsible for setting up any retries here. The functions that 836 // We're not responsible for setting up any retries here. The functions that
844 // first put us into a state that prevents successful sync cycles (eg. global 837 // first put us into a state that prevents successful sync cycles (eg. global
845 // throttling, type throttling, network errors, transient errors) will also 838 // throttling, type throttling, network errors, transient errors) will also
846 // setup the appropriate retry logic (eg. retry after timeout, exponential 839 // setup the appropriate retry logic (eg. retry after timeout, exponential
847 // backoff, retry when the network changes). 840 // backoff, retry when the network changes).
848 } 841 }
849 842
850 void SyncSchedulerImpl::ExponentialBackoffRetry() { 843 void SyncSchedulerImpl::ExponentialBackoffRetry() {
851 TryCanaryJob(); 844 TryCanaryJob();
852 } 845 }
853 846
854 void SyncSchedulerImpl::NotifyRetryTime(base::Time retry_time) { 847 void SyncSchedulerImpl::NotifyRetryTime(base::Time retry_time) {
855 FOR_EACH_OBSERVER(SyncEngineEventListener, *session_context_->listeners(), 848 FOR_EACH_OBSERVER(SyncEngineEventListener, *cycle_context_->listeners(),
856 OnRetryTimeChanged(retry_time)); 849 OnRetryTimeChanged(retry_time));
857 } 850 }
858 851
859 void SyncSchedulerImpl::NotifyThrottledTypesChanged(ModelTypeSet types) { 852 void SyncSchedulerImpl::NotifyThrottledTypesChanged(ModelTypeSet types) {
860 FOR_EACH_OBSERVER(SyncEngineEventListener, *session_context_->listeners(), 853 FOR_EACH_OBSERVER(SyncEngineEventListener, *cycle_context_->listeners(),
861 OnThrottledTypesChanged(types)); 854 OnThrottledTypesChanged(types));
862 } 855 }
863 856
864 bool SyncSchedulerImpl::IsBackingOff() const { 857 bool SyncSchedulerImpl::IsBackingOff() const {
865 DCHECK(CalledOnValidThread()); 858 DCHECK(CalledOnValidThread());
866 return wait_interval_.get() && 859 return wait_interval_.get() &&
867 wait_interval_->mode == WaitInterval::EXPONENTIAL_BACKOFF; 860 wait_interval_->mode == WaitInterval::EXPONENTIAL_BACKOFF;
868 } 861 }
869 862
870 void SyncSchedulerImpl::OnThrottled(const base::TimeDelta& throttle_duration) { 863 void SyncSchedulerImpl::OnThrottled(const base::TimeDelta& throttle_duration) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 929
937 void SyncSchedulerImpl::OnSyncProtocolError( 930 void SyncSchedulerImpl::OnSyncProtocolError(
938 const SyncProtocolError& sync_protocol_error) { 931 const SyncProtocolError& sync_protocol_error) {
939 DCHECK(CalledOnValidThread()); 932 DCHECK(CalledOnValidThread());
940 if (ShouldRequestEarlyExit(sync_protocol_error)) { 933 if (ShouldRequestEarlyExit(sync_protocol_error)) {
941 SDVLOG(2) << "Sync Scheduler requesting early exit."; 934 SDVLOG(2) << "Sync Scheduler requesting early exit.";
942 Stop(); 935 Stop();
943 } 936 }
944 if (IsActionableError(sync_protocol_error)) { 937 if (IsActionableError(sync_protocol_error)) {
945 SDVLOG(2) << "OnActionableError"; 938 SDVLOG(2) << "OnActionableError";
946 FOR_EACH_OBSERVER(SyncEngineEventListener, *session_context_->listeners(), 939 FOR_EACH_OBSERVER(SyncEngineEventListener, *cycle_context_->listeners(),
947 OnActionableError(sync_protocol_error)); 940 OnActionableError(sync_protocol_error));
948 } 941 }
949 } 942 }
950 943
951 void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) { 944 void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) {
952 nudge_tracker_.SetNextRetryTime(TimeTicks::Now() + delay); 945 nudge_tracker_.SetNextRetryTime(TimeTicks::Now() + delay);
953 retry_timer_.Start(FROM_HERE, delay, this, 946 retry_timer_.Start(FROM_HERE, delay, this,
954 &SyncSchedulerImpl::RetryTimerCallback); 947 &SyncSchedulerImpl::RetryTimerCallback);
955 } 948 }
956 949
957 void SyncSchedulerImpl::OnReceivedMigrationRequest(ModelTypeSet types) { 950 void SyncSchedulerImpl::OnReceivedMigrationRequest(ModelTypeSet types) {
958 FOR_EACH_OBSERVER(SyncEngineEventListener, *session_context_->listeners(), 951 FOR_EACH_OBSERVER(SyncEngineEventListener, *cycle_context_->listeners(),
959 OnMigrationRequested(types)); 952 OnMigrationRequested(types));
960 } 953 }
961 954
962 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { 955 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) {
963 DCHECK(CalledOnValidThread()); 956 DCHECK(CalledOnValidThread());
964 session_context_->set_notifications_enabled(notifications_enabled); 957 cycle_context_->set_notifications_enabled(notifications_enabled);
965 if (notifications_enabled) 958 if (notifications_enabled)
966 nudge_tracker_.OnInvalidationsEnabled(); 959 nudge_tracker_.OnInvalidationsEnabled();
967 else 960 else
968 nudge_tracker_.OnInvalidationsDisabled(); 961 nudge_tracker_.OnInvalidationsDisabled();
969 } 962 }
970 963
971 #undef SDVLOG_LOC 964 #undef SDVLOG_LOC
972 965
973 #undef SDVLOG 966 #undef SDVLOG
974 967
975 #undef SLOG 968 #undef SLOG
976 969
977 #undef ENUM_CASE 970 #undef ENUM_CASE
978 971
979 } // namespace syncer 972 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/engine_impl/sync_scheduler_impl.h ('k') | components/sync/engine_impl/sync_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698