OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "sync/sessions/nudge_tracker.h" | 5 #include "sync/sessions/nudge_tracker.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "sync/internal_api/public/engine/polling_constants.h" | 8 #include "sync/internal_api/public/engine/polling_constants.h" |
9 #include "sync/protocol/sync.pb.h" | 9 #include "sync/protocol/sync.pb.h" |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 default: | 41 default: |
42 return minimum_delay; | 42 return minimum_delay; |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 size_t NudgeTracker::kDefaultMaxPayloadsPerType = 10; | 48 size_t NudgeTracker::kDefaultMaxPayloadsPerType = 10; |
49 | 49 |
50 NudgeTracker::NudgeTracker() | 50 NudgeTracker::NudgeTracker() |
51 : type_tracker_deleter_(&type_trackers_), | 51 : invalidations_enabled_(false), |
52 invalidations_enabled_(false), | |
53 invalidations_out_of_sync_(true), | 52 invalidations_out_of_sync_(true), |
54 minimum_local_nudge_delay_( | 53 minimum_local_nudge_delay_( |
55 base::TimeDelta::FromMilliseconds(kDefaultNudgeDelayMilliseconds)), | 54 base::TimeDelta::FromMilliseconds(kDefaultNudgeDelayMilliseconds)), |
56 local_refresh_nudge_delay_( | 55 local_refresh_nudge_delay_( |
57 base::TimeDelta::FromMilliseconds(kSyncRefreshDelayMilliseconds)), | 56 base::TimeDelta::FromMilliseconds(kSyncRefreshDelayMilliseconds)), |
58 remote_invalidation_nudge_delay_( | 57 remote_invalidation_nudge_delay_( |
59 base::TimeDelta::FromMilliseconds(kSyncSchedulerDelayMilliseconds)) { | 58 base::TimeDelta::FromMilliseconds(kSyncSchedulerDelayMilliseconds)) { |
60 ModelTypeSet protocol_types = ProtocolTypes(); | 59 ModelTypeSet protocol_types = ProtocolTypes(); |
61 // Default initialize all the type trackers. | 60 // Default initialize all the type trackers. |
62 for (ModelTypeSet::Iterator it = protocol_types.First(); it.Good(); | 61 for (ModelTypeSet::Iterator it = protocol_types.First(); it.Good(); |
63 it.Inc()) { | 62 it.Inc()) { |
64 type_trackers_.insert(std::make_pair(it.Get(), new DataTypeTracker())); | 63 type_trackers_.insert(it.Get(), make_scoped_ptr(new DataTypeTracker())); |
65 } | 64 } |
66 } | 65 } |
67 | 66 |
68 NudgeTracker::~NudgeTracker() { } | 67 NudgeTracker::~NudgeTracker() { } |
69 | 68 |
70 bool NudgeTracker::IsSyncRequired() const { | 69 bool NudgeTracker::IsSyncRequired() const { |
71 if (IsRetryRequired()) | 70 if (IsRetryRequired()) |
72 return true; | 71 return true; |
73 | 72 |
74 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); | 73 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 } | 107 } |
109 | 108 |
110 void NudgeTracker::RecordSuccessfulSyncCycle() { | 109 void NudgeTracker::RecordSuccessfulSyncCycle() { |
111 // If a retry was required, we've just serviced it. Unset the flag. | 110 // If a retry was required, we've just serviced it. Unset the flag. |
112 if (IsRetryRequired()) | 111 if (IsRetryRequired()) |
113 current_retry_time_ = base::TimeTicks(); | 112 current_retry_time_ = base::TimeTicks(); |
114 | 113 |
115 // A successful cycle while invalidations are enabled puts us back into sync. | 114 // A successful cycle while invalidations are enabled puts us back into sync. |
116 invalidations_out_of_sync_ = !invalidations_enabled_; | 115 invalidations_out_of_sync_ = !invalidations_enabled_; |
117 | 116 |
118 for (TypeTrackerMap::iterator it = type_trackers_.begin(); | 117 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); |
119 it != type_trackers_.end(); ++it) { | 118 it != type_trackers_.end(); ++it) { |
120 it->second->RecordSuccessfulSyncCycle(); | 119 it->second->RecordSuccessfulSyncCycle(); |
121 } | 120 } |
122 } | 121 } |
123 | 122 |
124 base::TimeDelta NudgeTracker::RecordLocalChange(ModelTypeSet types) { | 123 base::TimeDelta NudgeTracker::RecordLocalChange(ModelTypeSet types) { |
125 // Start with the longest delay. | 124 // Start with the longest delay. |
126 base::TimeDelta delay = | 125 base::TimeDelta delay = |
127 base::TimeDelta::FromMilliseconds(kDefaultShortPollIntervalSeconds); | 126 base::TimeDelta::FromMilliseconds(kDefaultShortPollIntervalSeconds); |
128 for (ModelTypeSet::Iterator type_it = types.First(); type_it.Good(); | 127 for (ModelTypeSet::Iterator type_it = types.First(); type_it.Good(); |
129 type_it.Inc()) { | 128 type_it.Inc()) { |
130 TypeTrackerMap::iterator tracker_it = type_trackers_.find(type_it.Get()); | 129 TypeTrackerMap::const_iterator tracker_it = |
| 130 type_trackers_.find(type_it.Get()); |
131 DCHECK(tracker_it != type_trackers_.end()); | 131 DCHECK(tracker_it != type_trackers_.end()); |
132 | 132 |
133 // Only if the type tracker has a valid delay (non-zero) that is shorter | 133 // Only if the type tracker has a valid delay (non-zero) that is shorter |
134 // than the calculated delay do we update the calculated delay. | 134 // than the calculated delay do we update the calculated delay. |
135 base::TimeDelta type_delay = tracker_it->second->RecordLocalChange(); | 135 base::TimeDelta type_delay = tracker_it->second->RecordLocalChange(); |
136 if (type_delay == base::TimeDelta()) { | 136 if (type_delay == base::TimeDelta()) { |
137 type_delay = GetDefaultDelayForType(type_it.Get(), | 137 type_delay = GetDefaultDelayForType(type_it.Get(), |
138 minimum_local_nudge_delay_); | 138 minimum_local_nudge_delay_); |
139 } | 139 } |
140 if (type_delay < delay) | 140 if (type_delay < delay) |
141 delay = type_delay; | 141 delay = type_delay; |
142 } | 142 } |
143 return delay; | 143 return delay; |
144 } | 144 } |
145 | 145 |
146 base::TimeDelta NudgeTracker::RecordLocalRefreshRequest(ModelTypeSet types) { | 146 base::TimeDelta NudgeTracker::RecordLocalRefreshRequest(ModelTypeSet types) { |
147 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | 147 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
148 TypeTrackerMap::iterator tracker_it = type_trackers_.find(it.Get()); | 148 TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(it.Get()); |
149 DCHECK(tracker_it != type_trackers_.end()); | 149 DCHECK(tracker_it != type_trackers_.end()); |
150 tracker_it->second->RecordLocalRefreshRequest(); | 150 tracker_it->second->RecordLocalRefreshRequest(); |
151 } | 151 } |
152 return local_refresh_nudge_delay_; | 152 return local_refresh_nudge_delay_; |
153 } | 153 } |
154 | 154 |
155 base::TimeDelta NudgeTracker::RecordRemoteInvalidation( | 155 base::TimeDelta NudgeTracker::RecordRemoteInvalidation( |
156 syncer::ModelType type, | 156 syncer::ModelType type, |
157 scoped_ptr<InvalidationInterface> invalidation) { | 157 scoped_ptr<InvalidationInterface> invalidation) { |
158 // Forward the invalidations to the proper recipient. | 158 // Forward the invalidations to the proper recipient. |
159 TypeTrackerMap::iterator tracker_it = type_trackers_.find(type); | 159 TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type); |
160 DCHECK(tracker_it != type_trackers_.end()); | 160 DCHECK(tracker_it != type_trackers_.end()); |
161 tracker_it->second->RecordRemoteInvalidation(invalidation.Pass()); | 161 tracker_it->second->RecordRemoteInvalidation(invalidation.Pass()); |
162 return remote_invalidation_nudge_delay_; | 162 return remote_invalidation_nudge_delay_; |
163 } | 163 } |
164 | 164 |
165 void NudgeTracker::RecordInitialSyncRequired(syncer::ModelType type) { | 165 void NudgeTracker::RecordInitialSyncRequired(syncer::ModelType type) { |
166 TypeTrackerMap::iterator tracker_it = type_trackers_.find(type); | 166 TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type); |
167 DCHECK(tracker_it != type_trackers_.end()); | 167 DCHECK(tracker_it != type_trackers_.end()); |
168 tracker_it->second->RecordInitialSyncRequired(); | 168 tracker_it->second->RecordInitialSyncRequired(); |
169 } | 169 } |
170 | 170 |
171 void NudgeTracker::RecordCommitConflict(syncer::ModelType type) { | 171 void NudgeTracker::RecordCommitConflict(syncer::ModelType type) { |
172 TypeTrackerMap::iterator tracker_it = type_trackers_.find(type); | 172 TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type); |
173 DCHECK(tracker_it != type_trackers_.end()); | 173 DCHECK(tracker_it != type_trackers_.end()); |
174 tracker_it->second->RecordCommitConflict(); | 174 tracker_it->second->RecordCommitConflict(); |
175 } | 175 } |
176 | 176 |
177 void NudgeTracker::OnInvalidationsEnabled() { | 177 void NudgeTracker::OnInvalidationsEnabled() { |
178 invalidations_enabled_ = true; | 178 invalidations_enabled_ = true; |
179 } | 179 } |
180 | 180 |
181 void NudgeTracker::OnInvalidationsDisabled() { | 181 void NudgeTracker::OnInvalidationsDisabled() { |
182 invalidations_enabled_ = false; | 182 invalidations_enabled_ = false; |
183 invalidations_out_of_sync_ = true; | 183 invalidations_out_of_sync_ = true; |
184 } | 184 } |
185 | 185 |
186 void NudgeTracker::SetTypesThrottledUntil( | 186 void NudgeTracker::SetTypesThrottledUntil( |
187 ModelTypeSet types, | 187 ModelTypeSet types, |
188 base::TimeDelta length, | 188 base::TimeDelta length, |
189 base::TimeTicks now) { | 189 base::TimeTicks now) { |
190 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | 190 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
191 TypeTrackerMap::iterator tracker_it = type_trackers_.find(it.Get()); | 191 TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(it.Get()); |
192 tracker_it->second->ThrottleType(length, now); | 192 tracker_it->second->ThrottleType(length, now); |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 void NudgeTracker::UpdateTypeThrottlingState(base::TimeTicks now) { | 196 void NudgeTracker::UpdateTypeThrottlingState(base::TimeTicks now) { |
197 for (TypeTrackerMap::iterator it = type_trackers_.begin(); | 197 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); |
198 it != type_trackers_.end(); ++it) { | 198 it != type_trackers_.end(); ++it) { |
199 it->second->UpdateThrottleState(now); | 199 it->second->UpdateThrottleState(now); |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 bool NudgeTracker::IsAnyTypeThrottled() const { | 203 bool NudgeTracker::IsAnyTypeThrottled() const { |
204 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); | 204 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); |
205 it != type_trackers_.end(); ++it) { | 205 it != type_trackers_.end(); ++it) { |
206 if (it->second->IsThrottled()) { | 206 if (it->second->IsThrottled()) { |
207 return true; | 207 return true; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 // it is ready to go, then we set it as the current_retry_time_. It will stay | 362 // it is ready to go, then we set it as the current_retry_time_. It will stay |
363 // there until a GU retry has succeeded. | 363 // there until a GU retry has succeeded. |
364 if (!next_retry_time_.is_null() && | 364 if (!next_retry_time_.is_null() && |
365 next_retry_time_ <= sync_cycle_start_time_) { | 365 next_retry_time_ <= sync_cycle_start_time_) { |
366 current_retry_time_ = next_retry_time_; | 366 current_retry_time_ = next_retry_time_; |
367 next_retry_time_ = base::TimeTicks(); | 367 next_retry_time_ = base::TimeTicks(); |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 void NudgeTracker::SetHintBufferSize(size_t size) { | 371 void NudgeTracker::SetHintBufferSize(size_t size) { |
372 for (TypeTrackerMap::iterator it = type_trackers_.begin(); | 372 for (TypeTrackerMap::const_iterator it = type_trackers_.begin(); |
373 it != type_trackers_.end(); ++it) { | 373 it != type_trackers_.end(); ++it) { |
374 it->second->UpdatePayloadBufferSize(size); | 374 it->second->UpdatePayloadBufferSize(size); |
375 } | 375 } |
376 } | 376 } |
377 | 377 |
378 void NudgeTracker::SetNextRetryTime(base::TimeTicks retry_time) { | 378 void NudgeTracker::SetNextRetryTime(base::TimeTicks retry_time) { |
379 next_retry_time_ = retry_time; | 379 next_retry_time_ = retry_time; |
380 } | 380 } |
381 | 381 |
382 void NudgeTracker::OnReceivedCustomNudgeDelays( | 382 void NudgeTracker::OnReceivedCustomNudgeDelays( |
383 const std::map<ModelType, base::TimeDelta>& delay_map) { | 383 const std::map<ModelType, base::TimeDelta>& delay_map) { |
384 for (std::map<ModelType, base::TimeDelta>::const_iterator iter = | 384 for (std::map<ModelType, base::TimeDelta>::const_iterator iter = |
385 delay_map.begin(); | 385 delay_map.begin(); |
386 iter != delay_map.end(); | 386 iter != delay_map.end(); |
387 ++iter) { | 387 ++iter) { |
388 ModelType type = iter->first; | 388 ModelType type = iter->first; |
389 DCHECK(syncer::ProtocolTypes().Has(type)); | 389 DCHECK(syncer::ProtocolTypes().Has(type)); |
390 TypeTrackerMap::iterator type_iter = type_trackers_.find(type); | 390 TypeTrackerMap::const_iterator type_iter = type_trackers_.find(type); |
391 if (type_iter == type_trackers_.end()) | 391 if (type_iter == type_trackers_.end()) |
392 continue; | 392 continue; |
393 | 393 |
394 if (iter->second > minimum_local_nudge_delay_) { | 394 if (iter->second > minimum_local_nudge_delay_) { |
395 type_iter->second->UpdateLocalNudgeDelay(iter->second); | 395 type_iter->second->UpdateLocalNudgeDelay(iter->second); |
396 } else { | 396 } else { |
397 type_iter->second->UpdateLocalNudgeDelay( | 397 type_iter->second->UpdateLocalNudgeDelay( |
398 GetDefaultDelayForType(type, | 398 GetDefaultDelayForType(type, |
399 minimum_local_nudge_delay_)); | 399 minimum_local_nudge_delay_)); |
400 } | 400 } |
401 } | 401 } |
402 } | 402 } |
403 | 403 |
404 void NudgeTracker::SetDefaultNudgeDelay(base::TimeDelta nudge_delay) { | 404 void NudgeTracker::SetDefaultNudgeDelay(base::TimeDelta nudge_delay) { |
405 minimum_local_nudge_delay_ = nudge_delay; | 405 minimum_local_nudge_delay_ = nudge_delay; |
406 } | 406 } |
407 | 407 |
408 } // namespace sessions | 408 } // namespace sessions |
409 } // namespace syncer | 409 } // namespace syncer |
OLD | NEW |