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

Side by Side Diff: sync/engine/sync_scheduler_impl.h

Issue 13743003: sync: Finish the SyncScheduler refactor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ModelNeutralState forward decl Created 7 years, 8 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/cancelable_callback.h" 12 #include "base/cancelable_callback.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/memory/linked_ptr.h" 15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h" 18 #include "base/threading/non_thread_safe.h"
19 #include "base/time.h" 19 #include "base/time.h"
20 #include "base/timer.h" 20 #include "base/timer.h"
21 #include "sync/base/sync_export.h" 21 #include "sync/base/sync_export.h"
22 #include "sync/engine/net/server_connection_manager.h" 22 #include "sync/engine/net/server_connection_manager.h"
23 #include "sync/engine/nudge_source.h" 23 #include "sync/engine/nudge_source.h"
24 #include "sync/engine/nudge_tracker.h"
24 #include "sync/engine/sync_scheduler.h" 25 #include "sync/engine/sync_scheduler.h"
25 #include "sync/engine/sync_session_job.h"
26 #include "sync/engine/syncer.h" 26 #include "sync/engine/syncer.h"
27 #include "sync/internal_api/public/base/model_type_invalidation_map.h" 27 #include "sync/internal_api/public/base/model_type_invalidation_map.h"
28 #include "sync/internal_api/public/engine/polling_constants.h" 28 #include "sync/internal_api/public/engine/polling_constants.h"
29 #include "sync/internal_api/public/util/weak_handle.h" 29 #include "sync/internal_api/public/util/weak_handle.h"
30 #include "sync/sessions/sync_session.h" 30 #include "sync/sessions/sync_session.h"
31 #include "sync/sessions/sync_session_context.h" 31 #include "sync/sessions/sync_session_context.h"
32 32
33 namespace syncer { 33 namespace syncer {
34 34
35 class BackoffDelayProvider; 35 class BackoffDelayProvider;
36 36
37 namespace sessions {
38 struct ModelNeutralState;
39 } // namespace sessions
tim (not reviewing) 2013/04/15 22:37:49 nit - don't need comment.
rlarocque 2013/04/16 01:30:26 Done.
40
37 class SYNC_EXPORT_PRIVATE SyncSchedulerImpl 41 class SYNC_EXPORT_PRIVATE SyncSchedulerImpl
38 : public SyncScheduler, 42 : public SyncScheduler,
39 public base::NonThreadSafe { 43 public base::NonThreadSafe {
40 public: 44 public:
41 // |name| is a display string to identify the syncer thread. Takes 45 // |name| is a display string to identify the syncer thread. Takes
42 // |ownership of |syncer| and |delay_provider|. 46 // |ownership of |syncer| and |delay_provider|.
43 SyncSchedulerImpl(const std::string& name, 47 SyncSchedulerImpl(const std::string& name,
44 BackoffDelayProvider* delay_provider, 48 BackoffDelayProvider* delay_provider,
45 sessions::SyncSessionContext* context, 49 sessions::SyncSessionContext* context,
46 Syncer* syncer); 50 Syncer* syncer);
(...skipping 29 matching lines...) Expand all
76 const base::TimeDelta& new_interval) OVERRIDE; 80 const base::TimeDelta& new_interval) OVERRIDE;
77 virtual void OnReceivedLongPollIntervalUpdate( 81 virtual void OnReceivedLongPollIntervalUpdate(
78 const base::TimeDelta& new_interval) OVERRIDE; 82 const base::TimeDelta& new_interval) OVERRIDE;
79 virtual void OnReceivedSessionsCommitDelay( 83 virtual void OnReceivedSessionsCommitDelay(
80 const base::TimeDelta& new_delay) OVERRIDE; 84 const base::TimeDelta& new_delay) OVERRIDE;
81 virtual void OnShouldStopSyncingPermanently() OVERRIDE; 85 virtual void OnShouldStopSyncingPermanently() OVERRIDE;
82 virtual void OnSyncProtocolError( 86 virtual void OnSyncProtocolError(
83 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; 87 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE;
84 88
85 private: 89 private:
86 enum JobProcessDecision {
87 // Indicates we should continue with the current job.
88 CONTINUE,
89 // Indicates that we should save it to be processed later.
90 SAVE,
91 // Indicates we should drop this job.
92 DROP,
93 };
94
95 enum JobPriority { 90 enum JobPriority {
96 // Non-canary jobs respect exponential backoff. 91 // Non-canary jobs respect exponential backoff.
97 NORMAL_PRIORITY, 92 NORMAL_PRIORITY,
98 // Canary jobs bypass exponential backoff, so use with extreme caution. 93 // Canary jobs bypass exponential backoff, so use with extreme caution.
99 CANARY_PRIORITY 94 CANARY_PRIORITY
100 }; 95 };
101 96
102 friend class SyncSchedulerTest; 97 friend class SyncSchedulerTest;
103 friend class SyncSchedulerWhiteboxTest; 98 friend class SyncSchedulerWhiteboxTest;
104 friend class SyncerTest; 99 friend class SyncerTest;
105 100
101 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, NoNudgesInConfigureMode);
106 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, 102 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest,
107 DropNudgeWhileExponentialBackOff); 103 DropNudgeWhileExponentialBackOff);
108 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, SaveNudge); 104 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, SaveNudge);
109 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, 105 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest,
110 SaveNudgeWhileTypeThrottled); 106 SaveNudgeWhileTypeThrottled);
111 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, ContinueNudge); 107 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, ContinueNudge);
112 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, ContinueConfiguration); 108 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, ContinueConfiguration);
113 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, 109 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest,
114 SaveConfigurationWhileThrottled); 110 SaveConfigurationWhileThrottled);
115 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest, 111 FRIEND_TEST_ALL_PREFIXES(SyncSchedulerWhiteboxTest,
(...skipping 22 matching lines...) Expand all
138 WaitInterval(Mode mode, base::TimeDelta length); 134 WaitInterval(Mode mode, base::TimeDelta length);
139 135
140 static const char* GetModeString(Mode mode); 136 static const char* GetModeString(Mode mode);
141 137
142 Mode mode; 138 Mode mode;
143 base::TimeDelta length; 139 base::TimeDelta length;
144 }; 140 };
145 141
146 static const char* GetModeString(Mode mode); 142 static const char* GetModeString(Mode mode);
147 143
148 static const char* GetDecisionString(JobProcessDecision decision);
149
150 // Invoke the syncer to perform a non-POLL job.
151 bool DoSyncSessionJobImpl(scoped_ptr<SyncSessionJob> job,
152 JobPriority priority);
153
154 // Invoke the syncer to perform a nudge job. 144 // Invoke the syncer to perform a nudge job.
155 void DoNudgeSyncSessionJob(JobPriority priority); 145 void DoNudgeSyncSessionJob(JobPriority priority);
156 146
157 // Invoke the syncer to perform a configuration job. 147 // Invoke the syncer to perform a configuration job.
158 bool DoConfigurationSyncSessionJob(JobPriority priority); 148 bool DoConfigurationSyncSessionJob(JobPriority priority);
159 149
160 // Returns whether or not it's safe to run a poll job at this time.
161 bool ShouldPoll();
162
163 // Invoke the Syncer to perform a poll job. 150 // Invoke the Syncer to perform a poll job.
164 void DoPollSyncSessionJob(); 151 void DoPollSyncSessionJob();
165 152
166 // Called after the Syncer has performed the sync represented by |job|, to 153 // Helper to configure polling intervals.
167 // reset our state. |exited_prematurely| is true if the Syncer did not 154 void AdjustPolling(bool force_reset);
tim (not reviewing) 2013/04/15 22:37:49 nit - enum vs bool parameter as the intent of the
rlarocque 2013/04/16 01:30:26 I'd prefer to not add another enum in that style.
168 // cycle from job.start_step() to job.end_step(), likely because the
169 // scheduler was forced to quit the job mid-way through.
170 bool FinishSyncSessionJob(SyncSessionJob* job,
171 bool exited_prematurely,
172 sessions::SyncSession* session);
173
174 // Helper to schedule retries of a failed configure or nudge job.
175 void ScheduleNextSync(scoped_ptr<SyncSessionJob> finished_job,
176 sessions::SyncSession* session);
177
178 // Helper to configure polling intervals. Used by Start and ScheduleNextSync.
179 void AdjustPolling(const SyncSessionJob* old_job);
180 155
181 // Helper to restart waiting with |wait_interval_|'s timer. 156 // Helper to restart waiting with |wait_interval_|'s timer.
182 void RestartWaiting(); 157 void RestartWaiting();
183 158
184 // Helper to ScheduleNextSync in case of consecutive sync errors. 159 // Helper to adjust our wait interval when we expereince a transient failure.
185 void HandleContinuationError(scoped_ptr<SyncSessionJob> old_job, 160 void UpdateExponentialBackoff(
186 sessions::SyncSession* session); 161 const sessions::ModelNeutralState& model_neutral_state);
187 162
188 // Decide whether we should CONTINUE, SAVE or DROP the job. 163 // Determines if we're allowed to contact the server right now.
189 JobProcessDecision DecideOnJob(const SyncSessionJob& job, 164 bool CanRunJobNow(JobPriority priority);
190 JobPriority priority);
191 165
192 // Decide on whether to CONTINUE, SAVE or DROP the job when we are in 166 // Determines if we're allowed to contact the server right now.
193 // backoff mode. 167 bool CanRunNudgeJobNow(JobPriority priority);
194 JobProcessDecision DecideWhileInWaitInterval(const SyncSessionJob& job,
195 JobPriority priority);
196 168
197 // 'Impl' here refers to real implementation of public functions, running on 169 // 'Impl' here refers to real implementation of public functions.
198 // |thread_|.
199 void StopImpl(const base::Closure& callback); 170 void StopImpl(const base::Closure& callback);
200 171
201 // If the scheduler's current state supports it, this will create a job based 172 // If the scheduler's current state supports it, this will create a job based
202 // on the passed in parameters and coalesce it with any other pending jobs, 173 // on the passed in parameters and coalesce it with any other pending jobs,
203 // then post a delayed task to run it. It may also choose to drop the job or 174 // then post a delayed task to run it. It may also choose to drop the job or
204 // save it for later, depending on the scheduler's current state. 175 // save it for later, depending on the scheduler's current state.
205 void ScheduleNudgeImpl( 176 void ScheduleNudgeImpl(
206 const base::TimeDelta& delay, 177 const base::TimeDelta& delay,
207 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source, 178 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source,
208 const ModelTypeInvalidationMap& invalidation_map, 179 const ModelTypeInvalidationMap& invalidation_map,
(...skipping 14 matching lines...) Expand all
223 194
224 // Transitions out of the THROTTLED WaitInterval then calls TryCanaryJob(). 195 // Transitions out of the THROTTLED WaitInterval then calls TryCanaryJob().
225 void Unthrottle(); 196 void Unthrottle();
226 197
227 // Called when the root cause of the current connection error is fixed. 198 // Called when the root cause of the current connection error is fixed.
228 void OnServerConnectionErrorFixed(); 199 void OnServerConnectionErrorFixed();
229 200
230 // Creates a session for a poll and performs the sync. 201 // Creates a session for a poll and performs the sync.
231 void PollTimerCallback(); 202 void PollTimerCallback();
232 203
233 // Called once the first time thread_ is started to broadcast an initial 204 // Called as we are started to broadcast an initial session snapshot
234 // session snapshot containing data like initial_sync_ended. Important when 205 // containing data like initial_sync_ended. Important when the client starts
235 // the client starts up and does not need to perform an initial sync. 206 // up and does not need to perform an initial sync.
236 void SendInitialSnapshot(); 207 void SendInitialSnapshot();
237 208
238 // This is used for histogramming and analysis of ScheduleNudge* APIs. 209 // This is used for histogramming and analysis of ScheduleNudge* APIs.
239 // SyncScheduler is the ultimate choke-point for all such invocations (with 210 // SyncScheduler is the ultimate choke-point for all such invocations (with
240 // and without InvalidationState variants, all NudgeSources, etc) and as such 211 // and without InvalidationState variants, all NudgeSources, etc) and as such
241 // is the most flexible place to do this bookkeeping. 212 // is the most flexible place to do this bookkeeping.
242 void UpdateNudgeTimeRecords(const sessions::SyncSourceInfo& info); 213 void UpdateNudgeTimeRecords(const sessions::SyncSourceInfo& info);
243 214
244 virtual void OnActionableError(const sessions::SyncSessionSnapshot& snapshot); 215 virtual void OnActionableError(const sessions::SyncSessionSnapshot& snapshot);
245 216
(...skipping 27 matching lines...) Expand all
273 Mode mode_; 244 Mode mode_;
274 245
275 // Current wait state. Null if we're not in backoff and not throttled. 246 // Current wait state. Null if we're not in backoff and not throttled.
276 scoped_ptr<WaitInterval> wait_interval_; 247 scoped_ptr<WaitInterval> wait_interval_;
277 248
278 scoped_ptr<BackoffDelayProvider> delay_provider_; 249 scoped_ptr<BackoffDelayProvider> delay_provider_;
279 250
280 // The event that will wake us up. 251 // The event that will wake us up.
281 base::OneShotTimer<SyncSchedulerImpl> pending_wakeup_timer_; 252 base::OneShotTimer<SyncSchedulerImpl> pending_wakeup_timer_;
282 253
283 // Pending configure job storage. Note that 254 // Storage for variables related to an in-progress configure request. Note
284 // (mode_ != CONFIGURATION_MODE) \implies !pending_configure_job_. 255 // that (mode_ != CONFIGURATION_MODE) \implies !pending_configure_params_.
285 scoped_ptr<SyncSessionJob> pending_configure_job_; 256 scoped_ptr<ConfigurationParams> pending_configure_params_;
286 257
287 // Pending nudge job storage. These jobs can exist in CONFIGURATION_MODE, but 258 // If we have a nudge pending to run soon, it will be listed here.
288 // they will be run only in NORMAL_MODE. 259 base::TimeTicks scheduled_nudge_time_;
289 scoped_ptr<SyncSessionJob> pending_nudge_job_; 260
261 // Keeps track of work that the syncer needs to handle.
262 NudgeTracker nudge_tracker_;
290 263
291 // Invoked to run through the sync cycle. 264 // Invoked to run through the sync cycle.
292 scoped_ptr<Syncer> syncer_; 265 scoped_ptr<Syncer> syncer_;
293 266
294 sessions::SyncSessionContext* session_context_; 267 sessions::SyncSessionContext* session_context_;
295 268
296 // A map tracking LOCAL NudgeSource invocations of ScheduleNudge* APIs, 269 // A map tracking LOCAL NudgeSource invocations of ScheduleNudge* APIs,
297 // organized by datatype. Each datatype that was part of the types requested 270 // organized by datatype. Each datatype that was part of the types requested
298 // in the call will have its TimeTicks value updated. 271 // in the call will have its TimeTicks value updated.
299 typedef std::map<ModelType, base::TimeTicks> ModelTypeTimeMap; 272 typedef std::map<ModelType, base::TimeTicks> ModelTypeTimeMap;
300 ModelTypeTimeMap last_local_nudges_by_model_type_; 273 ModelTypeTimeMap last_local_nudges_by_model_type_;
301 274
302 // Used as an "anti-reentrancy defensive assertion". 275 // Used as an "anti-reentrancy defensive assertion".
303 // While true, it is illegal for any new scheduling activity to take place. 276 // While true, it is illegal for any new scheduling activity to take place.
304 // Ensures that higher layers don't break this law in response to events that 277 // Ensures that higher layers don't break this law in response to events that
305 // take place during a sync cycle. We call this out because such violations 278 // take place during a sync cycle. We call this out because such violations
306 // could result in tight sync loops hitting sync servers. 279 // could result in tight sync loops hitting sync servers.
307 bool no_scheduling_allowed_; 280 bool no_scheduling_allowed_;
308 281
309 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); 282 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl);
310 }; 283 };
311 284
312 } // namespace syncer 285 } // namespace syncer
313 286
314 #endif // SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_ 287 #endif // SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698