OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // A class to track the outstanding work required to bring the client back into | |
6 // sync with the server. | |
7 #ifndef SYNC_SESSIONS_NUDGE_TRACKER_H_ | |
8 #define SYNC_SESSIONS_NUDGE_TRACKER_H_ | |
9 | |
10 #include <stddef.h> | |
11 | |
12 #include <list> | |
13 #include <map> | |
14 #include <memory> | |
15 | |
16 #include "base/compiler_specific.h" | |
17 #include "base/macros.h" | |
18 #include "base/time/time.h" | |
19 #include "sync/base/sync_export.h" | |
20 #include "sync/internal_api/public/base/invalidation_interface.h" | |
21 #include "sync/internal_api/public/base/model_type.h" | |
22 #include "sync/protocol/sync.pb.h" | |
23 #include "sync/sessions/data_type_tracker.h" | |
24 | |
25 namespace syncer { | |
26 | |
27 class ObjectIdInvalidationMap; | |
28 | |
29 namespace sessions { | |
30 | |
31 class SYNC_EXPORT NudgeTracker { | |
32 public: | |
33 static size_t kDefaultMaxPayloadsPerType; | |
34 | |
35 NudgeTracker(); | |
36 ~NudgeTracker(); | |
37 | |
38 // Returns true if there is a good reason for performing a sync cycle. | |
39 // This does not take into account whether or not this is a good *time* to | |
40 // perform a sync cycle; that's the scheduler's job. | |
41 bool IsSyncRequired() const; | |
42 | |
43 // Returns true if there is a good reason for performing a get updates | |
44 // request as part of the next sync cycle. | |
45 bool IsGetUpdatesRequired() const; | |
46 | |
47 // Return true if should perform a sync cycle for GU retry. | |
48 // | |
49 // This is sensitive to changes in 'current time'. Its value can be affected | |
50 // by SetSyncCycleStartTime(), SetNextRetryTime(), and | |
51 // RecordSuccessfulSyncCycle(). Please refer to those functions for more | |
52 // information on how this flag is maintained. | |
53 bool IsRetryRequired() const; | |
54 | |
55 // Tells this class that all required update fetching or committing has | |
56 // completed successfully. | |
57 void RecordSuccessfulSyncCycle(); | |
58 | |
59 // Takes note of a local change. | |
60 // Returns the shortest nudge delay from the tracker of each type in |types|. | |
61 base::TimeDelta RecordLocalChange(ModelTypeSet types); | |
62 | |
63 // Takes note of a locally issued request to refresh a data type. | |
64 // Returns the current nudge delay for a local refresh. | |
65 base::TimeDelta RecordLocalRefreshRequest(ModelTypeSet types); | |
66 | |
67 // Takes note of the receipt of an invalidation notice from the server. | |
68 // Returns the current nudge delay for a remote invalidation. | |
69 base::TimeDelta RecordRemoteInvalidation( | |
70 syncer::ModelType type, | |
71 std::unique_ptr<InvalidationInterface> invalidation); | |
72 | |
73 // Take note that an initial sync is pending for this type. | |
74 void RecordInitialSyncRequired(syncer::ModelType type); | |
75 | |
76 // Takes note that the conflict happended for this type, need to sync to | |
77 // resolve conflict locally. | |
78 void RecordCommitConflict(syncer::ModelType type); | |
79 | |
80 // These functions should be called to keep this class informed of the status | |
81 // of the connection to the invalidations server. | |
82 void OnInvalidationsEnabled(); | |
83 void OnInvalidationsDisabled(); | |
84 | |
85 // Marks |types| as being throttled from |now| until |now| + |length|. | |
86 void SetTypesThrottledUntil(ModelTypeSet types, | |
87 base::TimeDelta length, | |
88 base::TimeTicks now); | |
89 | |
90 // Removes any throttling that have expired by time |now|. | |
91 void UpdateTypeThrottlingState(base::TimeTicks now); | |
92 | |
93 // Returns the time of the next type unthrottling, relative to | |
94 // the input |now| value. | |
95 base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const; | |
96 | |
97 // Returns true if any type is currenlty throttled. | |
98 bool IsAnyTypeThrottled() const; | |
99 | |
100 // Returns true if |type| is currently throttled. | |
101 bool IsTypeThrottled(ModelType type) const; | |
102 | |
103 // Returns the set of currently throttled types. | |
104 ModelTypeSet GetThrottledTypes() const; | |
105 | |
106 // Returns the set of types with local changes pending. | |
107 ModelTypeSet GetNudgedTypes() const; | |
108 | |
109 // Returns the set of types that have pending invalidations. | |
110 ModelTypeSet GetNotifiedTypes() const; | |
111 | |
112 // Returns the set of types that have pending refresh requests. | |
113 ModelTypeSet GetRefreshRequestedTypes() const; | |
114 | |
115 // Returns the 'source' of the GetUpdate request. | |
116 // | |
117 // This flag is deprecated, but still used by the server. There can be more | |
118 // than one reason to perform a particular sync cycle. The GetUpdatesTrigger | |
119 // message will contain more reliable information about the reasons for | |
120 // performing a sync. | |
121 // | |
122 // See the implementation for important information about the coalesce logic. | |
123 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource GetLegacySource() const; | |
124 | |
125 // Fills a GetUpdatesTrigger message for the next GetUpdates request. This is | |
126 // used by the DownloadUpdatesCommand to dump lots of useful per-type state | |
127 // information into the GetUpdate request before sending it off to the server. | |
128 void FillProtoMessage( | |
129 ModelType type, | |
130 sync_pb::GetUpdateTriggers* msg) const; | |
131 | |
132 // Fills a ProgressMarker with single legacy notification hint expected by the | |
133 // sync server. Newer servers will rely on the data set by FillProtoMessage() | |
134 // instead of this. | |
135 void SetLegacyNotificationHint( | |
136 ModelType type, | |
137 sync_pb::DataTypeProgressMarker* progress) const; | |
138 | |
139 // Flips the flag if we're due for a retry. | |
140 void SetSyncCycleStartTime(base::TimeTicks now); | |
141 | |
142 // Adjusts the number of hints that can be stored locally. | |
143 void SetHintBufferSize(size_t size); | |
144 | |
145 // Schedules a retry GetUpdate request for some time in the future. | |
146 // | |
147 // This is a request sent to us as part of a server response requesting | |
148 // that the client perform a GetUpdate request at |next_retry_time| to | |
149 // fetch any updates it may have missed in the first attempt. | |
150 // | |
151 // To avoid strange results from IsRetryRequired() during a sync cycle, the | |
152 // effects of this change are not guaranteed to take effect until | |
153 // SetSyncCycleStartTime() is called at the start of the *next* sync cycle. | |
154 void SetNextRetryTime(base::TimeTicks next_retry_time); | |
155 | |
156 // Update the per-datatype local change nudge delays. | |
157 void OnReceivedCustomNudgeDelays( | |
158 const std::map<ModelType, base::TimeDelta>& delay_map); | |
159 | |
160 // Update the default nudge delay. | |
161 void SetDefaultNudgeDelay(base::TimeDelta nudge_delay); | |
162 | |
163 private: | |
164 using TypeTrackerMap = std::map<ModelType, std::unique_ptr<DataTypeTracker>>; | |
165 | |
166 TypeTrackerMap type_trackers_; | |
167 | |
168 // Tracks whether or not invalidations are currently enabled. | |
169 bool invalidations_enabled_; | |
170 | |
171 // This flag is set if suspect that some technical malfunction or known bug | |
172 // may have left us with some unserviced invalidations. | |
173 // | |
174 // Keeps track of whether or not we're fully in sync with the invalidation | |
175 // server. This can be false even if invalidations are enabled and working | |
176 // correctly. For example, until we get ack-tracking working properly, we | |
177 // won't persist invalidations between restarts, so we may be out of sync when | |
178 // we restart. The only way to get back into sync is to have invalidations | |
179 // enabled, then complete a sync cycle to make sure we're fully up to date. | |
180 bool invalidations_out_of_sync_; | |
181 | |
182 base::TimeTicks last_successful_sync_time_; | |
183 | |
184 // A pending update to the current_retry_time_. | |
185 // | |
186 // The GU retry time is specified by a call to SetNextRetryTime, but we don't | |
187 // want that change to take effect right away, since it could happen in the | |
188 // middle of a sync cycle. We delay the update until the start of the next | |
189 // sync cycle, which is indicated by a call to SetSyncCycleStartTime(). | |
190 base::TimeTicks next_retry_time_; | |
191 | |
192 // The currently active retry GU time. Will be null if there is no retry GU | |
193 // pending at this time. | |
194 base::TimeTicks current_retry_time_; | |
195 | |
196 // The time when the sync cycle started. This value is maintained by | |
197 // SetSyncCycleStartTime(). This may contain a stale value if we're not | |
198 // currently in a sync cycle. | |
199 base::TimeTicks sync_cycle_start_time_; | |
200 | |
201 // Nudge delays for various events. | |
202 base::TimeDelta minimum_local_nudge_delay_; | |
203 base::TimeDelta local_refresh_nudge_delay_; | |
204 base::TimeDelta remote_invalidation_nudge_delay_; | |
205 | |
206 DISALLOW_COPY_AND_ASSIGN(NudgeTracker); | |
207 }; | |
208 | |
209 } // namespace sessions | |
210 } // namespace syncer | |
211 | |
212 #endif // SYNC_SESSIONS_NUDGE_TRACKER_H_ | |
OLD | NEW |