| 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 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ | 5 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ |
| 6 #define SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ | 6 #define SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "sync/engine/sync_scheduler.h" | 21 #include "sync/engine/sync_scheduler.h" |
| 22 #include "sync/engine/syncer.h" | 22 #include "sync/engine/syncer.h" |
| 23 #include "sync/internal_api/public/base/model_type_payload_map.h" | 23 #include "sync/internal_api/public/base/model_type_payload_map.h" |
| 24 #include "sync/internal_api/public/engine/polling_constants.h" | 24 #include "sync/internal_api/public/engine/polling_constants.h" |
| 25 #include "sync/internal_api/public/util/weak_handle.h" | 25 #include "sync/internal_api/public/util/weak_handle.h" |
| 26 #include "sync/sessions/sync_session.h" | 26 #include "sync/sessions/sync_session.h" |
| 27 #include "sync/sessions/sync_session_context.h" | 27 #include "sync/sessions/sync_session_context.h" |
| 28 | 28 |
| 29 namespace syncer { | 29 namespace syncer { |
| 30 | 30 |
| 31 class BackoffDelayProvider; |
| 32 |
| 31 class SyncSchedulerImpl : public SyncScheduler { | 33 class SyncSchedulerImpl : public SyncScheduler { |
| 32 public: | 34 public: |
| 33 // |name| is a display string to identify the syncer thread. Takes | 35 // |name| is a display string to identify the syncer thread. Takes |
| 34 // |ownership of |syncer|. | 36 // |ownership of |syncer| and |delay_provider|. |
| 35 SyncSchedulerImpl(const std::string& name, | 37 SyncSchedulerImpl(const std::string& name, |
| 36 sessions::SyncSessionContext* context, Syncer* syncer); | 38 BackoffDelayProvider* delay_provider, |
| 39 sessions::SyncSessionContext* context, |
| 40 Syncer* syncer); |
| 37 | 41 |
| 38 // Calls Stop(). | 42 // Calls Stop(). |
| 39 virtual ~SyncSchedulerImpl(); | 43 virtual ~SyncSchedulerImpl(); |
| 40 | 44 |
| 41 virtual void Start(Mode mode) OVERRIDE; | 45 virtual void Start(Mode mode) OVERRIDE; |
| 42 virtual bool ScheduleConfiguration( | 46 virtual bool ScheduleConfiguration( |
| 43 const ConfigurationParams& params) OVERRIDE; | 47 const ConfigurationParams& params) OVERRIDE; |
| 44 virtual void RequestStop(const base::Closure& callback) OVERRIDE; | 48 virtual void RequestStop(const base::Closure& callback) OVERRIDE; |
| 45 virtual void ScheduleNudgeAsync( | 49 virtual void ScheduleNudgeAsync( |
| 46 const base::TimeDelta& delay, | 50 const base::TimeDelta& delay, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 virtual void OnReceivedShortPollIntervalUpdate( | 69 virtual void OnReceivedShortPollIntervalUpdate( |
| 66 const base::TimeDelta& new_interval) OVERRIDE; | 70 const base::TimeDelta& new_interval) OVERRIDE; |
| 67 virtual void OnReceivedLongPollIntervalUpdate( | 71 virtual void OnReceivedLongPollIntervalUpdate( |
| 68 const base::TimeDelta& new_interval) OVERRIDE; | 72 const base::TimeDelta& new_interval) OVERRIDE; |
| 69 virtual void OnReceivedSessionsCommitDelay( | 73 virtual void OnReceivedSessionsCommitDelay( |
| 70 const base::TimeDelta& new_delay) OVERRIDE; | 74 const base::TimeDelta& new_delay) OVERRIDE; |
| 71 virtual void OnShouldStopSyncingPermanently() OVERRIDE; | 75 virtual void OnShouldStopSyncingPermanently() OVERRIDE; |
| 72 virtual void OnSyncProtocolError( | 76 virtual void OnSyncProtocolError( |
| 73 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; | 77 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; |
| 74 | 78 |
| 75 // DDOS avoidance function. Calculates how long we should wait before trying | |
| 76 // again after a failed sync attempt, where the last delay was |base_delay|. | |
| 77 // TODO(tim): Look at URLRequestThrottlerEntryInterface. | |
| 78 static base::TimeDelta GetRecommendedDelay(const base::TimeDelta& base_delay); | |
| 79 | |
| 80 // For integration tests only. Override initial backoff value. | |
| 81 // TODO(tim): Remove this, use command line flag and plumb through. Done | |
| 82 // this way to reduce diffs in hotfix. | |
| 83 static void ForceShortInitialBackoffRetry(); | |
| 84 | |
| 85 private: | 79 private: |
| 86 enum JobProcessDecision { | 80 enum JobProcessDecision { |
| 87 // Indicates we should continue with the current job. | 81 // Indicates we should continue with the current job. |
| 88 CONTINUE, | 82 CONTINUE, |
| 89 // Indicates that we should save it to be processed later. | 83 // Indicates that we should save it to be processed later. |
| 90 SAVE, | 84 SAVE, |
| 91 // Indicates we should drop this job. | 85 // Indicates we should drop this job. |
| 92 DROP, | 86 DROP, |
| 93 }; | 87 }; |
| 94 | 88 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 SaveConfigurationWhileThrottled); | 137 SaveConfigurationWhileThrottled); |
| 144 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, | 138 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, |
| 145 SaveNudgeWhileThrottled); | 139 SaveNudgeWhileThrottled); |
| 146 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, | 140 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, |
| 147 ContinueCanaryJobConfig); | 141 ContinueCanaryJobConfig); |
| 148 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, | 142 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, |
| 149 ContinueNudgeWhileExponentialBackOff); | 143 ContinueNudgeWhileExponentialBackOff); |
| 150 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerTest, TransientPollFailure); | 144 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerTest, TransientPollFailure); |
| 151 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerTest, GetInitialBackoffDelay); | 145 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerTest, GetInitialBackoffDelay); |
| 152 | 146 |
| 153 // A component used to get time delays associated with exponential backoff. | |
| 154 // Encapsulated into a class to facilitate testing. | |
| 155 class DelayProvider { | |
| 156 public: | |
| 157 DelayProvider(); | |
| 158 virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay); | |
| 159 virtual ~DelayProvider(); | |
| 160 private: | |
| 161 DISALLOW_COPY_AND_ASSIGN(DelayProvider); | |
| 162 }; | |
| 163 | |
| 164 struct WaitInterval { | 147 struct WaitInterval { |
| 165 enum Mode { | 148 enum Mode { |
| 166 // Uninitialized state, should not be set in practice. | 149 // Uninitialized state, should not be set in practice. |
| 167 UNKNOWN = -1, | 150 UNKNOWN = -1, |
| 168 // A wait interval whose duration has been affected by exponential | 151 // A wait interval whose duration has been affected by exponential |
| 169 // backoff. | 152 // backoff. |
| 170 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval. | 153 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval. |
| 171 EXPONENTIAL_BACKOFF, | 154 EXPONENTIAL_BACKOFF, |
| 172 // A server-initiated throttled interval. We do not allow any syncing | 155 // A server-initiated throttled interval. We do not allow any syncing |
| 173 // during such an interval. | 156 // during such an interval. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 210 |
| 228 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. | 211 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. |
| 229 void AdjustPolling(const SyncSessionJob* old_job); | 212 void AdjustPolling(const SyncSessionJob* old_job); |
| 230 | 213 |
| 231 // Helper to restart waiting with |wait_interval_|'s timer. | 214 // Helper to restart waiting with |wait_interval_|'s timer. |
| 232 void RestartWaiting(); | 215 void RestartWaiting(); |
| 233 | 216 |
| 234 // Helper to ScheduleNextSync in case of consecutive sync errors. | 217 // Helper to ScheduleNextSync in case of consecutive sync errors. |
| 235 void HandleContinuationError(const SyncSessionJob& old_job); | 218 void HandleContinuationError(const SyncSessionJob& old_job); |
| 236 | 219 |
| 237 // Helper to calculate the initial value for exponential backoff. | |
| 238 // See possible values and comments in polling_constants.h. | |
| 239 base::TimeDelta GetInitialBackoffDelay( | |
| 240 const sessions::ModelNeutralState& state) const; | |
| 241 | |
| 242 // Determines if it is legal to run |job| by checking current | 220 // Determines if it is legal to run |job| by checking current |
| 243 // operational mode, backoff or throttling, freshness | 221 // operational mode, backoff or throttling, freshness |
| 244 // (so we don't make redundant syncs), and connection. | 222 // (so we don't make redundant syncs), and connection. |
| 245 bool ShouldRunJob(const SyncSessionJob& job); | 223 bool ShouldRunJob(const SyncSessionJob& job); |
| 246 | 224 |
| 247 // Decide whether we should CONTINUE, SAVE or DROP the job. | 225 // Decide whether we should CONTINUE, SAVE or DROP the job. |
| 248 JobProcessDecision DecideOnJob(const SyncSessionJob& job); | 226 JobProcessDecision DecideOnJob(const SyncSessionJob& job); |
| 249 | 227 |
| 250 // Decide on whether to CONTINUE, SAVE or DROP the job when we are in | 228 // Decide on whether to CONTINUE, SAVE or DROP the job when we are in |
| 251 // backoff mode. | 229 // backoff mode. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 | 319 |
| 342 // The latest connection code we got while trying to connect. | 320 // The latest connection code we got while trying to connect. |
| 343 HttpResponse::ServerConnectionCode connection_code_; | 321 HttpResponse::ServerConnectionCode connection_code_; |
| 344 | 322 |
| 345 // Tracks in-flight nudges so we can coalesce. | 323 // Tracks in-flight nudges so we can coalesce. |
| 346 scoped_ptr<SyncSessionJob> pending_nudge_; | 324 scoped_ptr<SyncSessionJob> pending_nudge_; |
| 347 | 325 |
| 348 // Current wait state. Null if we're not in backoff and not throttled. | 326 // Current wait state. Null if we're not in backoff and not throttled. |
| 349 scoped_ptr<WaitInterval> wait_interval_; | 327 scoped_ptr<WaitInterval> wait_interval_; |
| 350 | 328 |
| 351 scoped_ptr<DelayProvider> delay_provider_; | 329 scoped_ptr<BackoffDelayProvider> delay_provider_; |
| 352 | 330 |
| 353 // Invoked to run through the sync cycle. | 331 // Invoked to run through the sync cycle. |
| 354 scoped_ptr<Syncer> syncer_; | 332 scoped_ptr<Syncer> syncer_; |
| 355 | 333 |
| 356 sessions::SyncSessionContext *session_context_; | 334 sessions::SyncSessionContext *session_context_; |
| 357 | 335 |
| 358 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); | 336 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); |
| 359 }; | 337 }; |
| 360 | 338 |
| 361 } // namespace syncer | 339 } // namespace syncer |
| 362 | 340 |
| 363 #endif // SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ | 341 #endif // SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ |
| OLD | NEW |