1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sync/sessions/nudge_tracker.h"
#include <stddef.h>
#include <utility>
#include "sync/internal_api/public/engine/polling_constants.h"
#include "sync/protocol/sync.pb.h"
namespace syncer {
namespace sessions {
namespace {
// Delays for syncer nudges.
const int kDefaultNudgeDelayMilliseconds = 200;
const int kSlowNudgeDelayMilliseconds = 2000;
const int kDefaultSessionsCommitDelaySeconds = 10;
const int kSyncRefreshDelayMilliseconds = 500;
const int kSyncSchedulerDelayMilliseconds = 250;
base::TimeDelta GetDefaultDelayForType(ModelType model_type,
base::TimeDelta minimum_delay) {
switch (model_type) {
case AUTOFILL:
// Accompany types rely on nudges from other types, and hence have long
// nudge delays.
return base::TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds);
case BOOKMARKS:
case PREFERENCES:
// Types with sometimes automatic changes get longer delays to allow more
// coalescing.
return base::TimeDelta::FromMilliseconds(kSlowNudgeDelayMilliseconds);
case SESSIONS:
case FAVICON_IMAGES:
case FAVICON_TRACKING:
// Types with navigation triggered changes get longer delays to allow more
// coalescing.
return base::TimeDelta::FromSeconds(kDefaultSessionsCommitDelaySeconds);
default:
return minimum_delay;
}
}
} // namespace
size_t NudgeTracker::kDefaultMaxPayloadsPerType = 10;
NudgeTracker::NudgeTracker()
: invalidations_enabled_(false),
invalidations_out_of_sync_(true),
minimum_local_nudge_delay_(
base::TimeDelta::FromMilliseconds(kDefaultNudgeDelayMilliseconds)),
local_refresh_nudge_delay_(
base::TimeDelta::FromMilliseconds(kSyncRefreshDelayMilliseconds)),
remote_invalidation_nudge_delay_(
base::TimeDelta::FromMilliseconds(kSyncSchedulerDelayMilliseconds)) {
ModelTypeSet protocol_types = ProtocolTypes();
// Default initialize all the type trackers.
for (ModelTypeSet::Iterator it = protocol_types.First(); it.Good();
it.Inc()) {
type_trackers_.insert(
std::make_pair(it.Get(), make_scoped_ptr(new DataTypeTracker())));
}
}
NudgeTracker::~NudgeTracker() { }
bool NudgeTracker::IsSyncRequired() const {
if (IsRetryRequired())
return true;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->IsSyncRequired()) {
return true;
}
}
return false;
}
bool NudgeTracker::IsGetUpdatesRequired() const {
if (invalidations_out_of_sync_)
return true;
if (IsRetryRequired())
return true;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->IsGetUpdatesRequired()) {
return true;
}
}
return false;
}
bool NudgeTracker::IsRetryRequired() const {
if (sync_cycle_start_time_.is_null())
return false;
if (current_retry_time_.is_null())
return false;
return current_retry_time_ <= sync_cycle_start_time_;
}
void NudgeTracker::RecordSuccessfulSyncCycle() {
// If a retry was required, we've just serviced it. Unset the flag.
if (IsRetryRequired())
current_retry_time_ = base::TimeTicks();
// A successful cycle while invalidations are enabled puts us back into sync.
invalidations_out_of_sync_ = !invalidations_enabled_;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
it->second->RecordSuccessfulSyncCycle();
}
}
base::TimeDelta NudgeTracker::RecordLocalChange(ModelTypeSet types) {
// Start with the longest delay.
base::TimeDelta delay =
base::TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds);
for (ModelTypeSet::Iterator type_it = types.First(); type_it.Good();
type_it.Inc()) {
TypeTrackerMap::const_iterator tracker_it =
type_trackers_.find(type_it.Get());
DCHECK(tracker_it != type_trackers_.end());
// Only if the type tracker has a valid delay (non-zero) that is shorter
// than the calculated delay do we update the calculated delay.
base::TimeDelta type_delay = tracker_it->second->RecordLocalChange();
if (type_delay == base::TimeDelta()) {
type_delay = GetDefaultDelayForType(type_it.Get(),
minimum_local_nudge_delay_);
}
if (type_delay < delay)
delay = type_delay;
}
return delay;
}
base::TimeDelta NudgeTracker::RecordLocalRefreshRequest(ModelTypeSet types) {
for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) {
TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(it.Get());
DCHECK(tracker_it != type_trackers_.end());
tracker_it->second->RecordLocalRefreshRequest();
}
return local_refresh_nudge_delay_;
}
base::TimeDelta NudgeTracker::RecordRemoteInvalidation(
syncer::ModelType type,
scoped_ptr<InvalidationInterface> invalidation) {
// Forward the invalidations to the proper recipient.
TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type);
DCHECK(tracker_it != type_trackers_.end());
tracker_it->second->RecordRemoteInvalidation(std::move(invalidation));
return remote_invalidation_nudge_delay_;
}
void NudgeTracker::RecordInitialSyncRequired(syncer::ModelType type) {
TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type);
DCHECK(tracker_it != type_trackers_.end());
tracker_it->second->RecordInitialSyncRequired();
}
void NudgeTracker::RecordCommitConflict(syncer::ModelType type) {
TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(type);
DCHECK(tracker_it != type_trackers_.end());
tracker_it->second->RecordCommitConflict();
}
void NudgeTracker::OnInvalidationsEnabled() {
invalidations_enabled_ = true;
}
void NudgeTracker::OnInvalidationsDisabled() {
invalidations_enabled_ = false;
invalidations_out_of_sync_ = true;
}
void NudgeTracker::SetTypesThrottledUntil(
ModelTypeSet types,
base::TimeDelta length,
base::TimeTicks now) {
for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) {
TypeTrackerMap::const_iterator tracker_it = type_trackers_.find(it.Get());
tracker_it->second->ThrottleType(length, now);
}
}
void NudgeTracker::UpdateTypeThrottlingState(base::TimeTicks now) {
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
it->second->UpdateThrottleState(now);
}
}
bool NudgeTracker::IsAnyTypeThrottled() const {
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->IsThrottled()) {
return true;
}
}
return false;
}
bool NudgeTracker::IsTypeThrottled(ModelType type) const {
DCHECK(type_trackers_.find(type) != type_trackers_.end());
return type_trackers_.find(type)->second->IsThrottled();
}
base::TimeDelta NudgeTracker::GetTimeUntilNextUnthrottle(
base::TimeTicks now) const {
DCHECK(IsAnyTypeThrottled()) << "This function requires a pending unthrottle";
// Return min of GetTimeUntilUnthrottle() values for all IsThrottled() types.
base::TimeDelta time_until_next_unthrottle = base::TimeDelta::Max();
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->IsThrottled()) {
time_until_next_unthrottle = std::min(
time_until_next_unthrottle, it->second->GetTimeUntilUnthrottle(now));
}
}
DCHECK(!time_until_next_unthrottle.is_max());
return time_until_next_unthrottle;
}
ModelTypeSet NudgeTracker::GetThrottledTypes() const {
ModelTypeSet result;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->IsThrottled()) {
result.Put(it->first);
}
}
return result;
}
ModelTypeSet NudgeTracker::GetNudgedTypes() const {
ModelTypeSet result;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->HasLocalChangePending()) {
result.Put(it->first);
}
}
return result;
}
ModelTypeSet NudgeTracker::GetNotifiedTypes() const {
ModelTypeSet result;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->HasPendingInvalidation()) {
result.Put(it->first);
}
}
return result;
}
ModelTypeSet NudgeTracker::GetRefreshRequestedTypes() const {
ModelTypeSet result;
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
if (it->second->HasRefreshRequestPending()) {
result.Put(it->first);
}
}
return result;
}
void NudgeTracker::SetLegacyNotificationHint(
ModelType type,
sync_pb::DataTypeProgressMarker* progress) const {
DCHECK(type_trackers_.find(type) != type_trackers_.end());
type_trackers_.find(type)->second->SetLegacyNotificationHint(progress);
}
sync_pb::GetUpdatesCallerInfo::GetUpdatesSource NudgeTracker::GetLegacySource()
const {
// There's an order to these sources: NOTIFICATION, DATATYPE_REFRESH, LOCAL,
// RETRY. The server makes optimization decisions based on this field, so
// it's important to get this right. Setting it wrong could lead to missed
// updates.
//
// This complexity is part of the reason why we're deprecating 'source' in
// favor of 'origin'.
bool has_invalidation_pending = false;
bool has_refresh_request_pending = false;
bool has_commit_pending = false;
bool is_initial_sync_required = false;
bool has_retry = IsRetryRequired();
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
const DataTypeTracker& tracker = *it->second;
if (!tracker.IsThrottled() && tracker.HasPendingInvalidation()) {
has_invalidation_pending = true;
}
if (!tracker.IsThrottled() && tracker.HasRefreshRequestPending()) {
has_refresh_request_pending = true;
}
if (!tracker.IsThrottled() && tracker.HasLocalChangePending()) {
has_commit_pending = true;
}
if (!tracker.IsThrottled() && tracker.IsInitialSyncRequired()) {
is_initial_sync_required = true;
}
}
if (has_invalidation_pending) {
return sync_pb::GetUpdatesCallerInfo::NOTIFICATION;
} else if (has_refresh_request_pending) {
return sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH;
} else if (is_initial_sync_required) {
// Not quite accurate, but good enough for our purposes. This setting of
// SOURCE is just a backward-compatibility hack anyway.
return sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH;
} else if (has_commit_pending) {
return sync_pb::GetUpdatesCallerInfo::LOCAL;
} else if (has_retry) {
return sync_pb::GetUpdatesCallerInfo::RETRY;
} else {
return sync_pb::GetUpdatesCallerInfo::UNKNOWN;
}
}
void NudgeTracker::FillProtoMessage(
ModelType type,
sync_pb::GetUpdateTriggers* msg) const {
DCHECK(type_trackers_.find(type) != type_trackers_.end());
// Fill what we can from the global data.
msg->set_invalidations_out_of_sync(invalidations_out_of_sync_);
// Delegate the type-specific work to the DataTypeTracker class.
type_trackers_.find(type)->second->FillGetUpdatesTriggersMessage(msg);
}
void NudgeTracker::SetSyncCycleStartTime(base::TimeTicks now) {
sync_cycle_start_time_ = now;
// If current_retry_time_ is still set, that means we have an old retry time
// left over from a previous cycle. For example, maybe we tried to perform
// this retry, hit a network connection error, and now we're in exponential
// backoff. In that case, we want this sync cycle to include the GU retry
// flag so we leave this variable set regardless of whether or not there is an
// overwrite pending.
if (!current_retry_time_.is_null()) {
return;
}
// If do not have a current_retry_time_, but we do have a next_retry_time_ and
// it is ready to go, then we set it as the current_retry_time_. It will stay
// there until a GU retry has succeeded.
if (!next_retry_time_.is_null() &&
next_retry_time_ <= sync_cycle_start_time_) {
current_retry_time_ = next_retry_time_;
next_retry_time_ = base::TimeTicks();
}
}
void NudgeTracker::SetHintBufferSize(size_t size) {
for (TypeTrackerMap::const_iterator it = type_trackers_.begin();
it != type_trackers_.end(); ++it) {
it->second->UpdatePayloadBufferSize(size);
}
}
void NudgeTracker::SetNextRetryTime(base::TimeTicks retry_time) {
next_retry_time_ = retry_time;
}
void NudgeTracker::OnReceivedCustomNudgeDelays(
const std::map<ModelType, base::TimeDelta>& delay_map) {
for (std::map<ModelType, base::TimeDelta>::const_iterator iter =
delay_map.begin();
iter != delay_map.end();
++iter) {
ModelType type = iter->first;
DCHECK(syncer::ProtocolTypes().Has(type));
TypeTrackerMap::const_iterator type_iter = type_trackers_.find(type);
if (type_iter == type_trackers_.end())
continue;
if (iter->second > minimum_local_nudge_delay_) {
type_iter->second->UpdateLocalNudgeDelay(iter->second);
} else {
type_iter->second->UpdateLocalNudgeDelay(
GetDefaultDelayForType(type,
minimum_local_nudge_delay_));
}
}
}
void NudgeTracker::SetDefaultNudgeDelay(base::TimeDelta nudge_delay) {
minimum_local_nudge_delay_ = nudge_delay;
}
} // namespace sessions
} // namespace syncer
|