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

Side by Side Diff: chrome/browser/sync/engine/syncer_thread2.h

Issue 6812004: sync: Make nudge + config jobs reliable in SyncerThread2 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing CR feedback. Created 9 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 | Annotate | Revision Log
OLDNEW
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
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 {
tim (not reviewing) 2011/04/13 21:40:18 This should not be public. Only things that the p
lipalani1 2011/04/13 22:45:14 yep they were for UTs. Now I made all UTs friends.
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 struct SyncSessionJob {
tim (not reviewing) 2011/04/13 21:40:18 this should not be public. Was this for tests?
lipalani1 2011/04/13 22:45:14 Done.
56 // An enum used to describe jobs for scheduling purposes.
57 enum SyncSessionJobPurpose {
58 // Our poll timer schedules POLL jobs periodically based on a server
59 // assigned poll interval.
60 POLL,
61 // A nudge task can come from a variety of components needing to force
62 // a sync. The source is inferable from |session.source()|.
63 NUDGE,
64 // The user invoked a function in the UI to clear their entire account
65 // and stop syncing (globally).
66 CLEAR_USER_DATA,
67 // Typically used for fetching updates for a subset of the enabled types
68 // during initial sync or reconfiguration. We don't run all steps of
69 // the sync cycle for these (e.g. CleanupDisabledTypes is skipped).
70 CONFIGURATION,
71 };
72
73 SyncSessionJob();
74 SyncSessionJob(SyncSessionJobPurpose purpose, base::TimeTicks start,
75 linked_ptr<sessions::SyncSession> session, bool is_canary_job,
76 const tracked_objects::Location& nudge_location);
77 ~SyncSessionJob();
78 SyncSessionJobPurpose purpose;
79 base::TimeTicks scheduled_start;
80 linked_ptr<sessions::SyncSession> session;
81 bool is_canary_job;
82
83 // This is the location the nudge came from. used for debugging purpose.
84 // In case of multiple nudges getting coalesced this stores the first nudge
85 // that came in.
86 tracked_objects::Location nudge_location;
87 };
88
89 struct WaitInterval {
tim (not reviewing) 2011/04/13 21:40:18 this should not be public.
lipalani1 2011/04/13 22:45:14 Same reason as above. Moved to private and made al
90 enum Mode {
91 // A wait interval whose duration has been affected by exponential
92 // backoff.
93 // EXPONENTIAL_BACKOFF intervals are nudge-rate limited to 1 per interval.
94 EXPONENTIAL_BACKOFF,
95 // A server-initiated throttled interval. We do not allow any syncing
96 // during such an interval.
97 THROTTLED,
98 };
99 WaitInterval();
100 ~WaitInterval();
101
102 Mode mode;
103
104 // This bool is set to true if we have observed a nudge during this
105 // interval and mode == EXPONENTIAL_BACKOFF.
106 bool had_nudge;
107 base::TimeDelta length;
108 base::OneShotTimer<SyncerThread> timer;
109
110 // Configure jobs are saved only when backing off or throttling. So we
111 // expose the pointer here.
112 scoped_ptr<SyncSessionJob> pending_configure_job;
113 WaitInterval(Mode mode, base::TimeDelta length);
114 };
115
46 // Takes ownership of both |context| and |syncer|. 116 // Takes ownership of both |context| and |syncer|.
47 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer); 117 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer);
48 virtual ~SyncerThread(); 118 virtual ~SyncerThread();
49 119
50 typedef Callback0::Type ModeChangeCallback; 120 typedef Callback0::Type ModeChangeCallback;
51 121
52 // Change the mode of operation. 122 // Change the mode of operation.
53 // We don't use a lock when changing modes, so we won't cause currently 123 // 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 124 // 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 125 // 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
91 virtual void OnReceivedShortPollIntervalUpdate( 161 virtual void OnReceivedShortPollIntervalUpdate(
92 const base::TimeDelta& new_interval); 162 const base::TimeDelta& new_interval);
93 virtual void OnReceivedLongPollIntervalUpdate( 163 virtual void OnReceivedLongPollIntervalUpdate(
94 const base::TimeDelta& new_interval); 164 const base::TimeDelta& new_interval);
95 virtual void OnShouldStopSyncingPermanently(); 165 virtual void OnShouldStopSyncingPermanently();
96 166
97 // ServerConnectionEventListener implementation. 167 // ServerConnectionEventListener implementation.
98 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute? 168 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute?
99 virtual void OnServerConnectionEvent(const ServerConnectionEvent2& event); 169 virtual void OnServerConnectionEvent(const ServerConnectionEvent2& event);
100 170
171
tim (not reviewing) 2011/04/13 21:40:18 remove newline
lipalani1 2011/04/13 22:45:14 Done.
101 private: 172 private:
102 friend class SyncerThread2Test; 173 friend class SyncerThread2Test;
103 174 friend class SyncerThread2WhiteboxTest;
104 // State pertaining to exponential backoff or throttling periods. 175 FRIEND_TEST_ALL_PREFIXES(SyncerThread2WhiteboxTest,
105 struct WaitInterval; 176 DropNudgeWhileExponentialBackOff);
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 177
127 // A component used to get time delays associated with exponential backoff. 178 // A component used to get time delays associated with exponential backoff.
128 // Encapsulated into a class to facilitate testing. 179 // Encapsulated into a class to facilitate testing.
129 class DelayProvider { 180 class DelayProvider {
130 public: 181 public:
131 DelayProvider(); 182 DelayProvider();
132 virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay); 183 virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay);
133 virtual ~DelayProvider(); 184 virtual ~DelayProvider();
134 private: 185 private:
135 DISALLOW_COPY_AND_ASSIGN(DelayProvider); 186 DISALLOW_COPY_AND_ASSIGN(DelayProvider);
136 }; 187 };
137 188
138 // Helper to assemble a job and post a delayed task to sync. 189 // Helper to assemble a job and post a delayed task to sync.
139 void ScheduleSyncSessionJob(const base::TimeDelta& delay, 190 void ScheduleSyncSessionJob(const base::TimeDelta& delay,
140 SyncSessionJobPurpose purpose, 191 SyncSessionJob::SyncSessionJobPurpose purpose,
141 sessions::SyncSession* session, 192 sessions::SyncSession* session,
142 const tracked_objects::Location& nudge_location); 193 const tracked_objects::Location& nudge_location);
143 194
144 // Invoke the Syncer to perform a sync. 195 // Invoke the Syncer to perform a sync.
145 void DoSyncSessionJob(const SyncSessionJob& job); 196 void DoSyncSessionJob(const SyncSessionJob& job);
146 197
147 // Called after the Syncer has performed the sync represented by |job|, to 198 // Called after the Syncer has performed the sync represented by |job|, to
148 // reset our state. 199 // reset our state.
149 void FinishSyncSessionJob(const SyncSessionJob& job); 200 void FinishSyncSessionJob(const SyncSessionJob& job);
150 201
151 // Record important state that might be needed in future syncs, such as which 202 // Record important state that might be needed in future syncs, such as which
152 // data types may require cleanup. 203 // data types may require cleanup.
153 void UpdateCarryoverSessionState(const SyncSessionJob& old_job); 204 void UpdateCarryoverSessionState(const SyncSessionJob& old_job);
154 205
155 // Helper to FinishSyncSessionJob to schedule the next sync operation. 206 // Helper to FinishSyncSessionJob to schedule the next sync operation.
156 void ScheduleNextSync(const SyncSessionJob& old_job); 207 void ScheduleNextSync(const SyncSessionJob& old_job);
157 208
158 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. 209 // Helper to configure polling intervals. Used by Start and ScheduleNextSync.
159 void AdjustPolling(const SyncSessionJob* old_job); 210 void AdjustPolling(const SyncSessionJob* old_job);
160 211
161 // Helper to ScheduleNextSync in case of consecutive sync errors. 212 // Helper to ScheduleNextSync in case of consecutive sync errors.
162 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job); 213 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job);
163 214
164 // Determines if it is legal to run a sync job for |purpose| at 215 // Determines if it is legal to run a sync |job|.
tim (not reviewing) 2011/04/13 21:40:18 "to run |job| by checking current operational mode
lipalani1 2011/04/13 22:45:14 Done.
165 // |scheduled_start|. This checks current operational mode, backoff or 216 // This checks current operational mode, backoff or
166 // throttling, freshness (so we don't make redundant syncs), and connection. 217 // throttling, freshness (so we don't make redundant syncs), and connection.
167 bool ShouldRunJob(SyncSessionJobPurpose purpose, 218 bool ShouldRunJob(const SyncSessionJob& job);
168 const base::TimeTicks& scheduled_start); 219
220 // Decide whether we should run, save or discard the job.
tim (not reviewing) 2011/04/13 21:40:18 You should use consistent terminology here with th
lipalani1 2011/04/13 22:45:14 Done.
221 JobProcessDecision DecideOnJob(const SyncSessionJob& job);
222
223 // Decide on whether to run, save or discard the job when we are in
224 // back off mode.
tim (not reviewing) 2011/04/13 21:40:18 nit - 'backoff' is one word, please check the new
lipalani1 2011/04/13 22:45:14 Done.
225 JobProcessDecision DecideWhileInWaitInterval(const SyncSessionJob& job);
226
227 // Saves the job for future executio.
tim (not reviewing) 2011/04/13 21:40:18 execution. typo
lipalani1 2011/04/13 22:45:14 Done.
228 void SaveJob(const SyncSessionJob& job);
229
230 // Coalesces the current job with the pending nudge.
231 void InitOrCoalesceJob(const SyncSessionJob& job);
169 232
170 // 'Impl' here refers to real implementation of public functions, running on 233 // 'Impl' here refers to real implementation of public functions, running on
171 // |thread_|. 234 // |thread_|.
172 void StartImpl(Mode mode, linked_ptr<ModeChangeCallback> callback); 235 void StartImpl(Mode mode, linked_ptr<ModeChangeCallback> callback);
173 void ScheduleNudgeImpl( 236 void ScheduleNudgeImpl(
174 const base::TimeDelta& delay, 237 const base::TimeDelta& delay,
175 NudgeSource source, 238 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source,
176 const syncable::ModelTypePayloadMap& types_with_payloads, 239 const syncable::ModelTypePayloadMap& types_with_payloads,
177 const tracked_objects::Location& nudge_location); 240 bool is_canary_job, const tracked_objects::Location& nudge_location);
178 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, 241 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info,
179 const std::vector<ModelSafeWorker*>& workers); 242 const std::vector<ModelSafeWorker*>& workers,
243 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
180 void ScheduleClearUserDataImpl(); 244 void ScheduleClearUserDataImpl();
181 245
182 // Returns true if the client is currently in exponential backoff. 246 // Returns true if the client is currently in exponential backoff.
183 bool IsBackingOff() const; 247 bool IsBackingOff() const;
184 248
185 // Helper to signal all listeners registered with |session_context_|. 249 // Helper to signal all listeners registered with |session_context_|.
186 void Notify(SyncEngineEvent::EventCause cause); 250 void Notify(SyncEngineEvent::EventCause cause);
187 251
188 // Callback to change backoff state. 252 // Callback to change backoff state.
189 void DoCanaryJob(); 253 void DoCanaryJob();
190 void Unthrottle(); 254 void Unthrottle();
191 255
256 void DoPendingJobIfPossible(bool is_canary_job);
tim (not reviewing) 2011/04/13 21:40:18 comment
lipalani1 2011/04/13 22:45:14 Done.
257
258 // The pointer is owned by the caller.
259 browser_sync::sessions::SyncSession* CreateSyncSession(
260 const browser_sync::sessions::SyncSourceInfo& info);
tim (not reviewing) 2011/04/13 21:40:18 indent
lipalani1 2011/04/13 22:45:14 Done.
261
262 void ExecuteJobByMakingACopy(SyncSessionJob* job);
tim (not reviewing) 2011/04/13 21:40:18 remove
lipalani1 2011/04/13 22:45:14 Done.
263
192 // Creates a session for a poll and performs the sync. 264 // Creates a session for a poll and performs the sync.
193 void PollTimerCallback(); 265 void PollTimerCallback();
194 266
195 // Assign |start| and |end| to appropriate SyncerStep values for the 267 // Assign |start| and |end| to appropriate SyncerStep values for the
196 // specified |purpose|. 268 // specified |purpose|.
197 void SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, 269 void SetSyncerStepsForPurpose(SyncSessionJob::SyncSessionJobPurpose purpose,
198 SyncerStep* start, 270 SyncerStep* start,
199 SyncerStep* end); 271 SyncerStep* end);
200 272
201 // Initializes the hookup between the ServerConnectionManager and us. 273 // Initializes the hookup between the ServerConnectionManager and us.
202 void WatchConnectionManager(); 274 void WatchConnectionManager();
203 275
204 // Used to update |server_connection_ok_|, see below. 276 // Used to update |server_connection_ok_|, see below.
205 void CheckServerConnectionManagerStatus( 277 void CheckServerConnectionManagerStatus(
206 HttpResponse::ServerConnectionCode code); 278 HttpResponse::ServerConnectionCode code);
207 279
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 320
249 } // namespace s3 321 } // namespace s3
250 322
251 } // namespace browser_sync 323 } // namespace browser_sync
252 324
253 // The SyncerThread manages its own internal thread and thus outlives it. We 325 // The SyncerThread manages its own internal thread and thus outlives it. We
254 // don't need refcounting for posting tasks to this internal thread. 326 // don't need refcounting for posting tasks to this internal thread.
255 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread); 327 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread);
256 328
257 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ 329 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698