OLD | NEW |
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 "sync/engine/sync_scheduler_impl.h" | 5 #include "sync/engine/sync_scheduler_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 } | 227 } |
228 | 228 |
229 DCHECK(!session_context_->account_name().empty()); | 229 DCHECK(!session_context_->account_name().empty()); |
230 DCHECK(syncer_.get()); | 230 DCHECK(syncer_.get()); |
231 Mode old_mode = mode_; | 231 Mode old_mode = mode_; |
232 mode_ = mode; | 232 mode_ = mode; |
233 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. | 233 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. |
234 | 234 |
235 if (old_mode != mode_ && | 235 if (old_mode != mode_ && |
236 mode_ == NORMAL_MODE && | 236 mode_ == NORMAL_MODE && |
237 nudge_tracker_.IsSyncRequired() && | 237 (nudge_tracker_.IsSyncRequired() || |
| 238 nudge_tracker_.IsRetryRequired(base::TimeTicks::Now())) && |
238 CanRunNudgeJobNow(NORMAL_PRIORITY)) { | 239 CanRunNudgeJobNow(NORMAL_PRIORITY)) { |
239 // We just got back to normal mode. Let's try to run the work that was | 240 // We just got back to normal mode. Let's try to run the work that was |
240 // queued up while we were configuring. | 241 // queued up while we were configuring. |
241 TrySyncSessionJob(); | 242 TrySyncSessionJob(); |
242 } | 243 } |
243 } | 244 } |
244 | 245 |
245 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { | 246 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { |
246 ModelTypeSet enabled_types = session_context_->enabled_types(); | 247 ModelTypeSet enabled_types = session_context_->enabled_types(); |
247 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); | 248 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 // Don't run poll job till the next time poll timer fires. | 463 // Don't run poll job till the next time poll timer fires. |
463 do_poll_after_credentials_updated_ = false; | 464 do_poll_after_credentials_updated_ = false; |
464 | 465 |
465 bool success = !premature_exit | 466 bool success = !premature_exit |
466 && !sessions::HasSyncerError( | 467 && !sessions::HasSyncerError( |
467 session->status_controller().model_neutral_state()); | 468 session->status_controller().model_neutral_state()); |
468 | 469 |
469 if (success) { | 470 if (success) { |
470 // That cycle took care of any outstanding work we had. | 471 // That cycle took care of any outstanding work we had. |
471 SDVLOG(2) << "Nudge succeeded."; | 472 SDVLOG(2) << "Nudge succeeded."; |
472 nudge_tracker_.RecordSuccessfulSyncCycle(); | 473 nudge_tracker_.RecordSuccessfulSyncCycle(base::TimeTicks::Now()); |
473 scheduled_nudge_time_ = base::TimeTicks(); | 474 scheduled_nudge_time_ = base::TimeTicks(); |
474 | 475 |
475 // If we're here, then we successfully reached the server. End all backoff. | 476 // If we're here, then we successfully reached the server. End all backoff. |
476 wait_interval_.reset(); | 477 wait_interval_.reset(); |
477 NotifyRetryTime(base::Time()); | 478 NotifyRetryTime(base::Time()); |
478 return; | 479 return; |
479 } else { | 480 } else { |
480 HandleFailure(session->status_controller().model_neutral_state()); | 481 HandleFailure(session->status_controller().model_neutral_state()); |
481 } | 482 } |
482 } | 483 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); | 543 new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); |
543 SDVLOG(2) << "Sync cycle failed. Will back off for " | 544 SDVLOG(2) << "Sync cycle failed. Will back off for " |
544 << wait_interval_->length.InMilliseconds() << "ms."; | 545 << wait_interval_->length.InMilliseconds() << "ms."; |
545 RestartWaiting(); | 546 RestartWaiting(); |
546 } | 547 } |
547 } | 548 } |
548 | 549 |
549 void SyncSchedulerImpl::DoPollSyncSessionJob() { | 550 void SyncSchedulerImpl::DoPollSyncSessionJob() { |
550 base::AutoReset<bool> protector(&no_scheduling_allowed_, true); | 551 base::AutoReset<bool> protector(&no_scheduling_allowed_, true); |
551 | 552 |
552 if (!CanRunJobNow(NORMAL_PRIORITY)) { | |
553 SDVLOG(2) << "Unable to run a poll job right now."; | |
554 return; | |
555 } | |
556 | |
557 if (mode_ != NORMAL_MODE) { | |
558 SDVLOG(2) << "Not running poll job in configure mode."; | |
559 return; | |
560 } | |
561 | |
562 SDVLOG(2) << "Polling with types " | 553 SDVLOG(2) << "Polling with types " |
563 << ModelTypeSetToString(session_context_->enabled_types()); | 554 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); |
564 scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); | 555 scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
565 syncer_->PollSyncShare( | 556 syncer_->PollSyncShare( |
566 GetEnabledAndUnthrottledTypes(), | 557 GetEnabledAndUnthrottledTypes(), |
567 session.get()); | 558 session.get()); |
568 | 559 |
569 AdjustPolling(FORCE_RESET); | 560 AdjustPolling(FORCE_RESET); |
570 | 561 |
571 if (IsCurrentlyThrottled()) { | 562 if (IsCurrentlyThrottled()) { |
572 SDVLOG(2) << "Poll request got us throttled."; | 563 SDVLOG(2) << "Poll request got us throttled."; |
573 // The OnSilencedUntil() call set up the WaitInterval for us. All we need | 564 // The OnSilencedUntil() call set up the WaitInterval for us. All we need |
574 // to do is start the timer. | 565 // to do is start the timer. |
575 RestartWaiting(); | 566 RestartWaiting(); |
576 } | 567 } |
577 } | 568 } |
578 | 569 |
| 570 void SyncSchedulerImpl::DoRetrySyncSessionJob() { |
| 571 DCHECK(CalledOnValidThread()); |
| 572 DCHECK_EQ(mode_, NORMAL_MODE); |
| 573 |
| 574 base::AutoReset<bool> protector(&no_scheduling_allowed_, true); |
| 575 |
| 576 SDVLOG(2) << "Retrying with types " |
| 577 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); |
| 578 scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
| 579 if (syncer_->RetrySyncShare(GetEnabledAndUnthrottledTypes(), |
| 580 session.get()) && |
| 581 !sessions::HasSyncerError( |
| 582 session->status_controller().model_neutral_state())) { |
| 583 nudge_tracker_.RecordSuccessfulSyncCycle(base::TimeTicks::Now()); |
| 584 } else { |
| 585 HandleFailure(session->status_controller().model_neutral_state()); |
| 586 } |
| 587 } |
| 588 |
579 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { | 589 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { |
580 DCHECK(CalledOnValidThread()); | 590 DCHECK(CalledOnValidThread()); |
581 base::TimeTicks now = TimeTicks::Now(); | 591 base::TimeTicks now = TimeTicks::Now(); |
582 // Update timing information for how often datatypes are triggering nudges. | 592 // Update timing information for how often datatypes are triggering nudges. |
583 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { | 593 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { |
584 base::TimeTicks previous = last_local_nudges_by_model_type_[iter.Get()]; | 594 base::TimeTicks previous = last_local_nudges_by_model_type_[iter.Get()]; |
585 last_local_nudges_by_model_type_[iter.Get()] = now; | 595 last_local_nudges_by_model_type_[iter.Get()] = now; |
586 if (previous.is_null()) | 596 if (previous.is_null()) |
587 continue; | 597 continue; |
588 | 598 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 void SyncSchedulerImpl::TrySyncSessionJobImpl() { | 686 void SyncSchedulerImpl::TrySyncSessionJobImpl() { |
677 JobPriority priority = next_sync_session_job_priority_; | 687 JobPriority priority = next_sync_session_job_priority_; |
678 next_sync_session_job_priority_ = NORMAL_PRIORITY; | 688 next_sync_session_job_priority_ = NORMAL_PRIORITY; |
679 | 689 |
680 DCHECK(CalledOnValidThread()); | 690 DCHECK(CalledOnValidThread()); |
681 if (mode_ == CONFIGURATION_MODE) { | 691 if (mode_ == CONFIGURATION_MODE) { |
682 if (pending_configure_params_) { | 692 if (pending_configure_params_) { |
683 SDVLOG(2) << "Found pending configure job"; | 693 SDVLOG(2) << "Found pending configure job"; |
684 DoConfigurationSyncSessionJob(priority); | 694 DoConfigurationSyncSessionJob(priority); |
685 } | 695 } |
686 } else { | 696 } else if (CanRunNudgeJobNow(priority)) { |
687 DCHECK(mode_ == NORMAL_MODE); | 697 if (nudge_tracker_.IsSyncRequired()) { |
688 if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(priority)) { | |
689 SDVLOG(2) << "Found pending nudge job"; | 698 SDVLOG(2) << "Found pending nudge job"; |
690 DoNudgeSyncSessionJob(priority); | 699 DoNudgeSyncSessionJob(priority); |
| 700 } else if (nudge_tracker_.IsRetryRequired(base::TimeTicks::Now())) { |
| 701 DoRetrySyncSessionJob(); |
691 } else if (do_poll_after_credentials_updated_ || | 702 } else if (do_poll_after_credentials_updated_ || |
692 ((base::TimeTicks::Now() - last_poll_reset_) >= GetPollInterval())) { | 703 ((base::TimeTicks::Now() - last_poll_reset_) >= GetPollInterval())) { |
693 DoPollSyncSessionJob(); | 704 DoPollSyncSessionJob(); |
694 // Poll timer fires infrequently. Usually by this time access token is | 705 // Poll timer fires infrequently. Usually by this time access token is |
695 // already expired and poll job will fail with auth error. Set flag to | 706 // already expired and poll job will fail with auth error. Set flag to |
696 // retry poll once ProfileSyncService gets new access token, TryCanaryJob | 707 // retry poll once ProfileSyncService gets new access token, TryCanaryJob |
697 // will be called after access token is retrieved. | 708 // will be called after access token is retrieved. |
698 if (HttpResponse::SYNC_AUTH_ERROR == | 709 if (HttpResponse::SYNC_AUTH_ERROR == |
699 session_context_->connection_manager()->server_status()) { | 710 session_context_->connection_manager()->server_status()) { |
700 do_poll_after_credentials_updated_ = true; | 711 do_poll_after_credentials_updated_ = true; |
(...skipping 29 matching lines...) Expand all Loading... |
730 // timer. If we find that no_scheduling_allowed_ is set here, then | 741 // timer. If we find that no_scheduling_allowed_ is set here, then |
731 // something is very wrong. Maybe someone mistakenly called us directly, or | 742 // something is very wrong. Maybe someone mistakenly called us directly, or |
732 // mishandled the book-keeping for no_scheduling_allowed_. | 743 // mishandled the book-keeping for no_scheduling_allowed_. |
733 NOTREACHED() << "Illegal to schedule job while session in progress."; | 744 NOTREACHED() << "Illegal to schedule job while session in progress."; |
734 return; | 745 return; |
735 } | 746 } |
736 | 747 |
737 TrySyncSessionJob(); | 748 TrySyncSessionJob(); |
738 } | 749 } |
739 | 750 |
| 751 void SyncSchedulerImpl::RetryTimerCallback() { |
| 752 TrySyncSessionJob(); |
| 753 } |
| 754 |
740 void SyncSchedulerImpl::Unthrottle() { | 755 void SyncSchedulerImpl::Unthrottle() { |
741 DCHECK(CalledOnValidThread()); | 756 DCHECK(CalledOnValidThread()); |
742 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); | 757 DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); |
743 | 758 |
744 // We're no longer throttled, so clear the wait interval. | 759 // We're no longer throttled, so clear the wait interval. |
745 wait_interval_.reset(); | 760 wait_interval_.reset(); |
746 NotifyRetryTime(base::Time()); | 761 NotifyRetryTime(base::Time()); |
747 | 762 |
748 // We treat this as a 'canary' in the sense that it was originally scheduled | 763 // We treat this as a 'canary' in the sense that it was originally scheduled |
749 // to run some time ago, failed, and we now want to retry, versus a job that | 764 // to run some time ago, failed, and we now want to retry, versus a job that |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 DCHECK(CalledOnValidThread()); | 898 DCHECK(CalledOnValidThread()); |
884 if (ShouldRequestEarlyExit( | 899 if (ShouldRequestEarlyExit( |
885 snapshot.model_neutral_state().sync_protocol_error)) { | 900 snapshot.model_neutral_state().sync_protocol_error)) { |
886 SDVLOG(2) << "Sync Scheduler requesting early exit."; | 901 SDVLOG(2) << "Sync Scheduler requesting early exit."; |
887 Stop(); | 902 Stop(); |
888 } | 903 } |
889 if (IsActionableError(snapshot.model_neutral_state().sync_protocol_error)) | 904 if (IsActionableError(snapshot.model_neutral_state().sync_protocol_error)) |
890 OnActionableError(snapshot); | 905 OnActionableError(snapshot); |
891 } | 906 } |
892 | 907 |
| 908 void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) { |
| 909 nudge_tracker_.set_next_retry_time(base::TimeTicks::Now() + delay); |
| 910 retry_timer_.Start(FROM_HERE, delay, this, |
| 911 &SyncSchedulerImpl::RetryTimerCallback); |
| 912 } |
| 913 |
893 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { | 914 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { |
894 DCHECK(CalledOnValidThread()); | 915 DCHECK(CalledOnValidThread()); |
895 session_context_->set_notifications_enabled(notifications_enabled); | 916 session_context_->set_notifications_enabled(notifications_enabled); |
896 if (notifications_enabled) | 917 if (notifications_enabled) |
897 nudge_tracker_.OnInvalidationsEnabled(); | 918 nudge_tracker_.OnInvalidationsEnabled(); |
898 else | 919 else |
899 nudge_tracker_.OnInvalidationsDisabled(); | 920 nudge_tracker_.OnInvalidationsDisabled(); |
900 } | 921 } |
901 | 922 |
902 base::TimeDelta SyncSchedulerImpl::GetSessionsCommitDelay() const { | 923 base::TimeDelta SyncSchedulerImpl::GetSessionsCommitDelay() const { |
903 DCHECK(CalledOnValidThread()); | 924 DCHECK(CalledOnValidThread()); |
904 return sessions_commit_delay_; | 925 return sessions_commit_delay_; |
905 } | 926 } |
906 | 927 |
907 #undef SDVLOG_LOC | 928 #undef SDVLOG_LOC |
908 | 929 |
909 #undef SDVLOG | 930 #undef SDVLOG |
910 | 931 |
911 #undef SLOG | 932 #undef SLOG |
912 | 933 |
913 #undef ENUM_CASE | 934 #undef ENUM_CASE |
914 | 935 |
915 } // namespace syncer | 936 } // namespace syncer |
OLD | NEW |