OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // *THIS EXISTS FOR EXPERIMENTATION AND TESTING WHILE WE REPLACE PTHREADS |
| 6 // WITH CHROME THREADS IN SYNC CODE* |
6 | 7 |
7 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 8 // A class to run the syncer on a thread. Uses PIMPL to wrap the old, original |
8 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 9 // pthreads implementation of SyncerThread. |
| 10 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_PTHREADS_H_ |
| 11 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_PTHREADS_H_ |
9 | 12 |
10 #include <list> | 13 #include <list> |
11 #include <map> | 14 #include <map> |
12 #include <queue> | 15 #include <queue> |
13 #include <vector> | 16 #include <vector> |
14 | 17 |
15 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
16 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
17 #include "chrome/browser/sync/engine/all_status.h" | 20 #include "chrome/browser/sync/engine/all_status.h" |
18 #include "chrome/browser/sync/engine/client_command_channel.h" | 21 #include "chrome/browser/sync/engine/client_command_channel.h" |
19 #include "chrome/browser/sync/util/event_sys-inl.h" | 22 #include "chrome/browser/sync/util/event_sys-inl.h" |
20 #include "chrome/browser/sync/util/pthread_helpers.h" | 23 #include "chrome/browser/sync/util/pthread_helpers.h" |
| 24 #include "chrome/browser/sync/engine/syncer_thread.h" |
21 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 25 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
22 | 26 |
23 class EventListenerHookup; | 27 class EventListenerHookup; |
24 | 28 |
25 namespace syncable { | 29 namespace syncable { |
26 class DirectoryManager; | 30 class DirectoryManager; |
27 struct DirectoryManagerEvent; | 31 struct DirectoryManagerEvent; |
28 } | 32 } |
29 | 33 |
30 namespace browser_sync { | 34 namespace browser_sync { |
31 | 35 |
32 class ModelSafeWorker; | 36 class ModelSafeWorker; |
33 class ServerConnectionManager; | 37 class ServerConnectionManager; |
34 class Syncer; | 38 class Syncer; |
35 class TalkMediator; | 39 class TalkMediator; |
36 class URLFactory; | 40 class URLFactory; |
37 struct ServerConnectionEvent; | 41 struct ServerConnectionEvent; |
38 struct SyncerEvent; | 42 struct SyncerEvent; |
39 struct SyncerShutdownEvent; | 43 struct SyncerShutdownEvent; |
40 struct TalkMediatorEvent; | 44 struct TalkMediatorEvent; |
41 | 45 |
42 class SyncerThread { | 46 // The legacy implementation of SyncerThread using pthreads, kept around for |
43 FRIEND_TEST(SyncerThreadTest, CalculateSyncWaitTime); | 47 // historical experimentation until a new version is finalized. |
44 FRIEND_TEST(SyncerThreadTest, CalculatePollingWaitTime); | 48 class SyncerThreadPthreadImpl { |
| 49 public: |
| 50 virtual ~SyncerThreadPthreadImpl(); |
45 | 51 |
46 public: | 52 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr); |
47 friend class SyncerThreadTest; | 53 // Creates and starts a syncer thread. |
| 54 // Returns true if it creates a thread or if there's currently a thread |
| 55 // running and false otherwise. |
| 56 virtual bool Start(); |
48 | 57 |
49 enum NudgeSource { | 58 // Stop processing. A max wait of at least 2*server RTT time is recommended. |
50 kUnknown = 0, | 59 // returns true if we stopped, false otherwise. |
51 kNotification, | 60 virtual bool Stop(int max_wait); |
52 kLocal, | |
53 kContinuation | |
54 }; | |
55 | 61 |
56 // Server can overwrite these values via client commands. | 62 // Nudges the syncer to sync with a delay specified. This API is for access |
57 // Standard short poll. This is used when XMPP is off. | 63 // from the SyncerThread's controller and will cause a mutex lock. |
58 static const int kDefaultShortPollIntervalSeconds = 60; | 64 virtual bool NudgeSyncer(int milliseconds_from_now, |
59 // Long poll is used when XMPP is on. | 65 SyncerThread::NudgeSource source); |
60 static const int kDefaultLongPollIntervalSeconds = 3600; | |
61 // 30 minutes by default. If exponential backoff kicks in, this is the | |
62 // longest possible poll interval. | |
63 static const int kDefaultMaxPollIntervalMs = 30 * 60 * 1000; | |
64 | 66 |
65 SyncerThread(ClientCommandChannel* command_channel, | 67 // Registers this thread to watch talk mediator events. |
| 68 virtual void WatchTalkMediator(TalkMediator* talk_mediator); |
| 69 |
| 70 virtual void WatchClientCommands(ClientCommandChannel* channel); |
| 71 |
| 72 virtual SyncerEventChannel* channel(); |
| 73 |
| 74 private: |
| 75 friend class SyncerThreadPthreads; |
| 76 SyncerThreadPthreadImpl(ClientCommandChannel* command_channel, |
66 syncable::DirectoryManager* mgr, | 77 syncable::DirectoryManager* mgr, |
67 ServerConnectionManager* connection_manager, AllStatus* all_status, | 78 ServerConnectionManager* connection_manager, AllStatus* all_status, |
68 ModelSafeWorker* model_safe_worker); | 79 ModelSafeWorker* model_safe_worker); |
69 ~SyncerThread(); | |
70 | 80 |
71 void WatchConnectionManager(ServerConnectionManager* conn_mgr); | |
72 // Creates and starts a syncer thread. | |
73 // Returns true if it creates a thread or if there's currently a thread | |
74 // running and false otherwise. | |
75 bool Start(); | |
76 | |
77 // Stop processing. A max wait of at least 2*server RTT time is recommended. | |
78 // returns true if we stopped, false otherwise. | |
79 bool Stop(int max_wait); | |
80 | |
81 // Nudges the syncer to sync with a delay specified. This API is for access | |
82 // from the SyncerThread's controller and will cause a mutex lock. | |
83 bool NudgeSyncer(int milliseconds_from_now, NudgeSource source); | |
84 | |
85 // Registers this thread to watch talk mediator events. | |
86 void WatchTalkMediator(TalkMediator* talk_mediator); | |
87 | |
88 void WatchClientCommands(ClientCommandChannel* channel); | |
89 | |
90 SyncerEventChannel* channel(); | |
91 | |
92 private: | |
93 // A few members to gate the rate at which we nudge the syncer. | 81 // A few members to gate the rate at which we nudge the syncer. |
94 enum { | 82 enum { |
95 kNudgeRateLimitCount = 6, | 83 kNudgeRateLimitCount = 6, |
96 kNudgeRateLimitTime = 180, | 84 kNudgeRateLimitTime = 180, |
97 }; | 85 }; |
98 | 86 |
99 // A queue of all scheduled nudges. One insertion for every call to | 87 // A queue of all scheduled nudges. One insertion for every call to |
100 // NudgeQueue(). | 88 // NudgeQueue(). |
101 typedef std::pair<timespec, NudgeSource> NudgeObject; | 89 typedef std::pair<timespec, SyncerThread::NudgeSource> NudgeObject; |
102 | 90 |
103 struct IsTimeSpecGreater { | 91 struct IsTimeSpecGreater { |
104 inline bool operator() (const NudgeObject& lhs, const NudgeObject& rhs) { | 92 inline bool operator() (const NudgeObject& lhs, const NudgeObject& rhs) { |
105 return lhs.first.tv_sec == rhs.first.tv_sec ? | 93 return lhs.first.tv_sec == rhs.first.tv_sec ? |
106 lhs.first.tv_nsec > rhs.first.tv_nsec : | 94 lhs.first.tv_nsec > rhs.first.tv_nsec : |
107 lhs.first.tv_sec > rhs.first.tv_sec; | 95 lhs.first.tv_sec > rhs.first.tv_sec; |
108 } | 96 } |
109 }; | 97 }; |
110 | 98 |
111 typedef std::priority_queue<NudgeObject, std::vector<NudgeObject>, | 99 typedef std::priority_queue<NudgeObject, std::vector<NudgeObject>, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 int* user_idle_milliseconds, | 131 int* user_idle_milliseconds, |
144 bool* continue_sync_cycle); | 132 bool* continue_sync_cycle); |
145 // Helper to above function, considers effect of user idle time. | 133 // Helper to above function, considers effect of user idle time. |
146 int CalculateSyncWaitTime(int last_wait, int user_idle_ms); | 134 int CalculateSyncWaitTime(int last_wait, int user_idle_ms); |
147 | 135 |
148 // Sets the source value of the controlled syncer's updates_source value. | 136 // Sets the source value of the controlled syncer's updates_source value. |
149 // The initial sync boolean is updated if read as a sentinel. The following | 137 // The initial sync boolean is updated if read as a sentinel. The following |
150 // two methods work in concert to achieve this goal. | 138 // two methods work in concert to achieve this goal. |
151 void UpdateNudgeSource(const timespec& now, bool* continue_sync_cycle, | 139 void UpdateNudgeSource(const timespec& now, bool* continue_sync_cycle, |
152 bool* initial_sync); | 140 bool* initial_sync); |
153 void SetUpdatesSource(bool nudged, NudgeSource nudge_source, | 141 void SetUpdatesSource(bool nudged, SyncerThread::NudgeSource nudge_source, |
154 bool* initial_sync); | 142 bool* initial_sync); |
155 | 143 |
156 // For unit tests only. | 144 // For unit tests only. |
157 void DisableIdleDetection() { disable_idle_detection_ = true; } | 145 void DisableIdleDetection() { disable_idle_detection_ = true; } |
158 | 146 |
159 // False when we want to stop the thread. | 147 // False when we want to stop the thread. |
160 bool stop_syncer_thread_; | 148 bool stop_syncer_thread_; |
161 | 149 |
162 // We use one mutex for all members except the channel. | 150 // We use one mutex for all members except the channel. |
163 PThreadMutex mutex_; | 151 PThreadMutex mutex_; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // The upper bound on the nominal wait between polls in seconds. Note that | 187 // The upper bound on the nominal wait between polls in seconds. Note that |
200 // this bounds the "nominal" poll interval, while the the actual interval | 188 // this bounds the "nominal" poll interval, while the the actual interval |
201 // also takes previous failures into account. | 189 // also takes previous failures into account. |
202 int syncer_max_interval_; | 190 int syncer_max_interval_; |
203 | 191 |
204 scoped_ptr<SyncerEventChannel> syncer_event_channel_; | 192 scoped_ptr<SyncerEventChannel> syncer_event_channel_; |
205 | 193 |
206 // This causes syncer to start syncing ASAP. If the rate of requests is too | 194 // This causes syncer to start syncing ASAP. If the rate of requests is too |
207 // high the request will be silently dropped. mutex_ should be held when | 195 // high the request will be silently dropped. mutex_ should be held when |
208 // this is called. | 196 // this is called. |
209 void NudgeSyncImpl(int milliseconds_from_now, NudgeSource source); | 197 void NudgeSyncImpl(int milliseconds_from_now, |
| 198 SyncerThread::NudgeSource source); |
210 | 199 |
211 NudgeQueue nudge_queue_; | 200 NudgeQueue nudge_queue_; |
212 | 201 |
213 scoped_ptr<EventListenerHookup> talk_mediator_hookup_; | 202 scoped_ptr<EventListenerHookup> talk_mediator_hookup_; |
214 ClientCommandChannel* const command_channel_; | 203 ClientCommandChannel* const command_channel_; |
215 scoped_ptr<EventListenerHookup> directory_manager_hookup_; | 204 scoped_ptr<EventListenerHookup> directory_manager_hookup_; |
216 scoped_ptr<EventListenerHookup> syncer_events_; | 205 scoped_ptr<EventListenerHookup> syncer_events_; |
217 | 206 |
218 // Handles any tasks that will result in model changes (modifications of | 207 // Handles any tasks that will result in model changes (modifications of |
219 // syncable::Entries). Pass this to the syncer created and managed by |this|. | 208 // syncable::Entries). Pass this to the syncer created and managed by |this|. |
220 // Only non-null in syncapi case. | 209 // Only non-null in syncapi case. |
221 scoped_ptr<ModelSafeWorker> model_safe_worker_; | 210 scoped_ptr<ModelSafeWorker> model_safe_worker_; |
222 | 211 |
223 // Useful for unit tests | 212 // Useful for unit tests |
224 bool disable_idle_detection_; | 213 bool disable_idle_detection_; |
225 | 214 |
226 DISALLOW_COPY_AND_ASSIGN(SyncerThread); | 215 DISALLOW_COPY_AND_ASSIGN(SyncerThreadPthreadImpl); |
| 216 }; |
| 217 |
| 218 // A new-version SyncerThread pimpl wrapper for the old legacy implementation. |
| 219 class SyncerThreadPthreads : public SyncerThread { |
| 220 FRIEND_TEST(SyncerThreadTest, CalculateSyncWaitTime); |
| 221 FRIEND_TEST(SyncerThreadTest, CalculatePollingWaitTime); |
| 222 FRIEND_TEST(SyncerThreadWithSyncerTest, Polling); |
| 223 FRIEND_TEST(SyncerThreadWithSyncerTest, Nudge); |
| 224 friend class SyncerThreadWithSyncerTest; |
| 225 friend class SyncerThreadFactory; |
| 226 public: |
| 227 virtual ~SyncerThreadPthreads() {} |
| 228 |
| 229 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr) { |
| 230 impl_->WatchConnectionManager(conn_mgr); |
| 231 } |
| 232 virtual bool Start() { |
| 233 return impl_->Start(); |
| 234 } |
| 235 virtual bool Stop(int max_wait) { |
| 236 return impl_->Stop(max_wait); |
| 237 } |
| 238 virtual bool NudgeSyncer(int milliseconds_from_now, NudgeSource source) { |
| 239 return impl_->NudgeSyncer(milliseconds_from_now, source); |
| 240 } |
| 241 virtual void WatchTalkMediator(TalkMediator* talk_mediator) { |
| 242 impl_->WatchTalkMediator(talk_mediator); |
| 243 } |
| 244 virtual void WatchClientCommands(ClientCommandChannel* channel) { |
| 245 impl_->WatchClientCommands(channel); |
| 246 } |
| 247 virtual SyncerEventChannel* channel() { |
| 248 return impl_->channel(); |
| 249 } |
| 250 protected: |
| 251 SyncerThreadPthreads(ClientCommandChannel* command_channel, |
| 252 syncable::DirectoryManager* mgr, |
| 253 ServerConnectionManager* connection_manager, AllStatus* all_status, |
| 254 ModelSafeWorker* model_safe_worker); |
| 255 virtual void SetConnected(bool connected) { |
| 256 impl_->connected_ = connected; |
| 257 } |
| 258 virtual void SetSyncerPollingInterval(int interval) { |
| 259 impl_->syncer_polling_interval_ = interval; |
| 260 } |
| 261 virtual void SetSyncerShortPollInterval(base::TimeDelta interval) { |
| 262 impl_->syncer_short_poll_interval_seconds_ = static_cast<int>( |
| 263 interval.InSeconds()); |
| 264 } |
| 265 virtual void DisableIdleDetection() { impl_->disable_idle_detection_ = true; } |
| 266 virtual int CalculateSyncWaitTime(int last_wait, int user_idle_ms) { |
| 267 return impl_->CalculateSyncWaitTime(last_wait, user_idle_ms); |
| 268 } |
| 269 virtual int CalculatePollingWaitTime( |
| 270 const AllStatus::Status& status, |
| 271 int last_poll_wait, // in s |
| 272 int* user_idle_milliseconds, |
| 273 bool* continue_sync_cycle) { |
| 274 return impl_->CalculatePollingWaitTime(status, last_poll_wait, |
| 275 user_idle_milliseconds, continue_sync_cycle); |
| 276 } |
| 277 private: |
| 278 scoped_ptr<SyncerThreadPthreadImpl> impl_; |
| 279 DISALLOW_COPY_AND_ASSIGN(SyncerThreadPthreads); |
227 }; | 280 }; |
228 | 281 |
229 } // namespace browser_sync | 282 } // namespace browser_sync |
230 | 283 |
231 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 284 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_PTHREADS_H_ |
OLD | NEW |