Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A class to run the syncer on a thread. | 5 // A class to run the syncer on a thread. |
| 6 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 6 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| 7 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 7 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| 8 #pragma once | 8 #pragma once |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 // In this mode, the thread only performs configuration tasks. This is | 36 // In this mode, the thread only performs configuration tasks. This is |
| 37 // designed to make the case where we want to download updates for a | 37 // designed to make the case where we want to download updates for a |
| 38 // specific type only, and not continue syncing until we are moved into | 38 // specific type only, and not continue syncing until we are moved into |
| 39 // normal mode. | 39 // normal mode. |
| 40 CONFIGURATION_MODE, | 40 CONFIGURATION_MODE, |
| 41 // Resumes polling and allows nudges, drops configuration tasks. Runs | 41 // Resumes polling and allows nudges, drops configuration tasks. Runs |
| 42 // through entire sync cycle. | 42 // through entire sync cycle. |
| 43 NORMAL_MODE, | 43 NORMAL_MODE, |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 enum JobProcessDecision { | |
| 47 // Indicates we should continue with the current job. | |
| 48 CONTINUE, | |
| 49 // Indicates that we should save it to be processed later. | |
| 50 SAVE, | |
| 51 // Indicates we should drop this job. | |
| 52 DROP, | |
| 53 }; | |
| 54 | |
| 55 // An enum used to describe jobs for scheduling purposes. | |
| 56 enum SyncSessionJobPurpose { | |
|
tim (not reviewing)
2011/04/09 23:52:58
This was originally in the header file, and was ca
lipalani1
2011/04/12 02:33:00
Done.
| |
| 57 // Our poll timer schedules POLL jobs periodically based on a server | |
| 58 // assigned poll interval. | |
| 59 POLL, | |
| 60 // A nudge task can come from a variety of components needing to force | |
| 61 // a sync. The source is inferable from |session.source()|. | |
| 62 NUDGE, | |
| 63 // The user invoked a function in the UI to clear their entire account | |
| 64 // and stop syncing (globally). | |
| 65 CLEAR_USER_DATA, | |
| 66 // Typically used for fetching updates for a subset of the enabled types | |
| 67 // during initial sync or reconfiguration. We don't run all steps of | |
| 68 // the sync cycle for these (e.g. CleanupDisabledTypes is skipped). | |
| 69 CONFIGURATION, | |
| 70 }; | |
| 71 | |
| 72 struct SyncSessionJob { | |
| 73 SyncSessionJobPurpose purpose; | |
| 74 base::TimeTicks scheduled_start; | |
| 75 linked_ptr<sessions::SyncSession> session; | |
| 76 bool is_canary_job; | |
| 77 | |
| 78 // This is the location the nudge came from. used for debugging purpose. | |
| 79 // In case of multiple nudges getting coalesced this stores the first nudge | |
| 80 // that came in. | |
| 81 tracked_objects::Location nudge_location; | |
| 82 }; | |
| 83 | |
| 84 struct WaitInterval { | |
| 85 enum Mode { | |
| 86 // A wait interval whose duration has been affected by exponential | |
| 87 // backoff. | |
| 88 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval. | |
| 89 EXPONENTIAL_BACKOFF, | |
| 90 // A server-initiated throttled interval. We do not allow any syncing | |
| 91 // during such an interval. | |
| 92 THROTTLED, | |
| 93 }; | |
| 94 Mode mode; | |
| 95 | |
| 96 // This bool is set to true if we have observed a nudge during this | |
| 97 // interval and mode == EXPONENTIAL_BACKOFF. | |
| 98 bool had_nudge; | |
| 99 base::TimeDelta length; | |
| 100 base::OneShotTimer<SyncerThread> timer; | |
| 101 | |
| 102 // Configure jobs are saved only when backing off or throttling. So we | |
| 103 // expose the pointer here. | |
| 104 scoped_ptr<SyncSessionJob> pending_configure_job; | |
| 105 WaitInterval(Mode mode, base::TimeDelta length); | |
| 106 }; | |
| 107 | |
| 46 // Takes ownership of both |context| and |syncer|. | 108 // Takes ownership of both |context| and |syncer|. |
| 47 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer); | 109 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer); |
| 48 virtual ~SyncerThread(); | 110 virtual ~SyncerThread(); |
| 49 | 111 |
| 50 typedef Callback0::Type ModeChangeCallback; | 112 typedef Callback0::Type ModeChangeCallback; |
| 51 | 113 |
| 52 // Change the mode of operation. | 114 // Change the mode of operation. |
| 53 // We don't use a lock when changing modes, so we won't cause currently | 115 // We don't use a lock when changing modes, so we won't cause currently |
| 54 // scheduled jobs to adhere to the new mode. We could protect it, but it | 116 // scheduled jobs to adhere to the new mode. We could protect it, but it |
| 55 // doesn't buy very much as a) a session could already be in progress and it | 117 // doesn't buy very much as a) a session could already be in progress and it |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 virtual void OnReceivedShortPollIntervalUpdate( | 153 virtual void OnReceivedShortPollIntervalUpdate( |
| 92 const base::TimeDelta& new_interval); | 154 const base::TimeDelta& new_interval); |
| 93 virtual void OnReceivedLongPollIntervalUpdate( | 155 virtual void OnReceivedLongPollIntervalUpdate( |
| 94 const base::TimeDelta& new_interval); | 156 const base::TimeDelta& new_interval); |
| 95 virtual void OnShouldStopSyncingPermanently(); | 157 virtual void OnShouldStopSyncingPermanently(); |
| 96 | 158 |
| 97 // ServerConnectionEventListener implementation. | 159 // ServerConnectionEventListener implementation. |
| 98 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute? | 160 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute? |
| 99 virtual void OnServerConnectionEvent(const ServerConnectionEvent2& event); | 161 virtual void OnServerConnectionEvent(const ServerConnectionEvent2& event); |
| 100 | 162 |
| 163 | |
| 101 private: | 164 private: |
| 102 friend class SyncerThread2Test; | 165 friend class SyncerThread2Test; |
| 103 | 166 FRIEND_TEST_ALL_PREFIXES(SyncerThread2Test, DropNudgeWhileExponentialBackOff); |
| 104 // State pertaining to exponential backoff or throttling periods. | |
| 105 struct WaitInterval; | |
| 106 | |
| 107 // An enum used to describe jobs for scheduling purposes. | |
| 108 enum SyncSessionJobPurpose { | |
| 109 // Our poll timer schedules POLL jobs periodically based on a server | |
| 110 // assigned poll interval. | |
| 111 POLL, | |
| 112 // A nudge task can come from a variety of components needing to force | |
| 113 // a sync. The source is inferable from |session.source()|. | |
| 114 NUDGE, | |
| 115 // The user invoked a function in the UI to clear their entire account | |
| 116 // and stop syncing (globally). | |
| 117 CLEAR_USER_DATA, | |
| 118 // Typically used for fetching updates for a subset of the enabled types | |
| 119 // during initial sync or reconfiguration. We don't run all steps of | |
| 120 // the sync cycle for these (e.g. CleanupDisabledTypes is skipped). | |
| 121 CONFIGURATION, | |
| 122 }; | |
| 123 | |
| 124 // Internal state for every sync task that is scheduled. | |
| 125 struct SyncSessionJob; | |
| 126 | 167 |
| 127 // A component used to get time delays associated with exponential backoff. | 168 // A component used to get time delays associated with exponential backoff. |
| 128 // Encapsulated into a class to facilitate testing. | 169 // Encapsulated into a class to facilitate testing. |
| 129 class DelayProvider { | 170 class DelayProvider { |
| 130 public: | 171 public: |
| 131 DelayProvider(); | 172 DelayProvider(); |
| 132 virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay); | 173 virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay); |
| 133 virtual ~DelayProvider(); | 174 virtual ~DelayProvider(); |
| 134 private: | 175 private: |
| 135 DISALLOW_COPY_AND_ASSIGN(DelayProvider); | 176 DISALLOW_COPY_AND_ASSIGN(DelayProvider); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 154 | 195 |
| 155 // Helper to FinishSyncSessionJob to schedule the next sync operation. | 196 // Helper to FinishSyncSessionJob to schedule the next sync operation. |
| 156 void ScheduleNextSync(const SyncSessionJob& old_job); | 197 void ScheduleNextSync(const SyncSessionJob& old_job); |
| 157 | 198 |
| 158 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. | 199 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. |
| 159 void AdjustPolling(const SyncSessionJob* old_job); | 200 void AdjustPolling(const SyncSessionJob* old_job); |
| 160 | 201 |
| 161 // Helper to ScheduleNextSync in case of consecutive sync errors. | 202 // Helper to ScheduleNextSync in case of consecutive sync errors. |
| 162 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job); | 203 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job); |
| 163 | 204 |
| 164 // Determines if it is legal to run a sync job for |purpose| at | 205 // Determines if it is legal to run a sync |job|. |
| 165 // |scheduled_start|. This checks current operational mode, backoff or | 206 // This checks current operational mode, backoff or |
| 166 // throttling, freshness (so we don't make redundant syncs), and connection. | 207 // throttling, freshness (so we don't make redundant syncs), and connection. |
| 167 bool ShouldRunJob(SyncSessionJobPurpose purpose, | 208 bool ShouldRunJob(const SyncSessionJob& job); |
| 168 const base::TimeTicks& scheduled_start); | 209 |
| 210 // Saves the job for future executio. | |
| 211 void SaveJob(const SyncSessionJob& job); | |
| 212 | |
| 213 // Decide on whether to run, save or discard the job when we are in | |
| 214 // back off mode. | |
| 215 JobProcessDecision DecideWhileInWaitInterval(const SyncSessionJob& job); | |
|
tim (not reviewing)
2011/04/09 23:52:58
the functions in this file are roughly ordered in
lipalani1
2011/04/12 02:33:00
Done.
| |
| 216 | |
| 217 // Decide whether we should run, save or discard the job. | |
| 218 JobProcessDecision DecideOnJob(const SyncSessionJob& job); | |
| 219 | |
| 220 // Coalesces the current job with the pending nudge. | |
| 221 void UpdatePendingJob(const SyncSessionJob& job); | |
| 169 | 222 |
| 170 // 'Impl' here refers to real implementation of public functions, running on | 223 // 'Impl' here refers to real implementation of public functions, running on |
| 171 // |thread_|. | 224 // |thread_|. |
| 172 void StartImpl(Mode mode, linked_ptr<ModeChangeCallback> callback); | 225 void StartImpl(Mode mode, linked_ptr<ModeChangeCallback> callback); |
| 173 void ScheduleNudgeImpl( | 226 void ScheduleNudgeImpl( |
| 174 const base::TimeDelta& delay, | 227 const base::TimeDelta& delay, |
| 175 NudgeSource source, | 228 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source, |
| 176 const syncable::ModelTypePayloadMap& types_with_payloads, | 229 const syncable::ModelTypePayloadMap& types_with_payloads, |
| 177 const tracked_objects::Location& nudge_location); | 230 bool is_canary_job, const tracked_objects::Location& nudge_location); |
| 178 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, | 231 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, |
| 179 const std::vector<ModelSafeWorker*>& workers); | 232 const std::vector<ModelSafeWorker*>& workers, |
| 233 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source); | |
| 180 void ScheduleClearUserDataImpl(); | 234 void ScheduleClearUserDataImpl(); |
| 181 | 235 |
| 182 // Returns true if the client is currently in exponential backoff. | 236 // Returns true if the client is currently in exponential backoff. |
| 183 bool IsBackingOff() const; | 237 bool IsBackingOff() const; |
| 184 | 238 |
| 185 // Helper to signal all listeners registered with |session_context_|. | 239 // Helper to signal all listeners registered with |session_context_|. |
| 186 void Notify(SyncEngineEvent::EventCause cause); | 240 void Notify(SyncEngineEvent::EventCause cause); |
| 187 | 241 |
| 188 // Callback to change backoff state. | 242 // Callback to change backoff state. |
| 189 void DoCanaryJob(); | 243 void DoCanaryJob(); |
| 190 void Unthrottle(); | 244 void Unthrottle(); |
| 191 | 245 |
| 246 void ExecutePendingJob(); | |
|
tim (not reviewing)
2011/04/09 23:52:58
How is execute different from 'DoSyncSessionJob'.
lipalani1
2011/04/12 02:33:00
Done.
| |
| 247 | |
| 248 void ExecuteJobByMakingACopy(SyncSessionJob* job); | |
|
tim (not reviewing)
2011/04/09 23:52:58
These two functions together are confusing, especi
lipalani1
2011/04/12 02:33:00
Done.
| |
| 249 | |
| 192 // Creates a session for a poll and performs the sync. | 250 // Creates a session for a poll and performs the sync. |
| 193 void PollTimerCallback(); | 251 void PollTimerCallback(); |
| 194 | 252 |
| 195 // Assign |start| and |end| to appropriate SyncerStep values for the | 253 // Assign |start| and |end| to appropriate SyncerStep values for the |
| 196 // specified |purpose|. | 254 // specified |purpose|. |
| 197 void SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, | 255 void SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, |
| 198 SyncerStep* start, | 256 SyncerStep* start, |
| 199 SyncerStep* end); | 257 SyncerStep* end); |
| 200 | 258 |
| 201 // Initializes the hookup between the ServerConnectionManager and us. | 259 // Initializes the hookup between the ServerConnectionManager and us. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 | 306 |
| 249 } // namespace s3 | 307 } // namespace s3 |
| 250 | 308 |
| 251 } // namespace browser_sync | 309 } // namespace browser_sync |
| 252 | 310 |
| 253 // The SyncerThread manages its own internal thread and thus outlives it. We | 311 // The SyncerThread manages its own internal thread and thus outlives it. We |
| 254 // don't need refcounting for posting tasks to this internal thread. | 312 // don't need refcounting for posting tasks to this internal thread. |
| 255 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread); | 313 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread); |
| 256 | 314 |
| 257 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 315 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| OLD | NEW |