summaryrefslogtreecommitdiffstats
path: root/sync/notifier/registration_manager.cc
blob: 8e049166e18f2f131b02a0595cbee59e25a7ea19 (plain)
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
// Copyright (c) 2012 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/notifier/registration_manager.h"

#include <algorithm>
#include <cstddef>
#include <string>

#include "base/rand_util.h"
#include "google/cacheinvalidation/include/invalidation-client.h"
#include "google/cacheinvalidation/include/types.h"
#include "sync/internal_api/public/syncable/model_type.h"
#include "sync/notifier/invalidation_util.h"

namespace sync_notifier {

RegistrationManager::PendingRegistrationInfo::PendingRegistrationInfo() {}

RegistrationManager::RegistrationStatus::RegistrationStatus()
    : model_type(syncable::UNSPECIFIED),
      registration_manager(NULL),
      enabled(true),
      state(invalidation::InvalidationListener::UNREGISTERED) {}

RegistrationManager::RegistrationStatus::~RegistrationStatus() {}

void RegistrationManager::RegistrationStatus::DoRegister() {
  DCHECK_NE(model_type, syncable::UNSPECIFIED);
  DCHECK(registration_manager);
  CHECK(enabled);
  // We might be called explicitly, so stop the timer manually and
  // reset the delay.
  registration_timer.Stop();
  delay = base::TimeDelta();
  registration_manager->DoRegisterType(model_type);
  DCHECK(!last_registration_request.is_null());
}

void RegistrationManager::RegistrationStatus::Disable() {
  enabled = false;
  state = invalidation::InvalidationListener::UNREGISTERED;
  registration_timer.Stop();
  delay = base::TimeDelta();
}

const int RegistrationManager::kInitialRegistrationDelaySeconds = 5;
const int RegistrationManager::kRegistrationDelayExponent = 2;
const double RegistrationManager::kRegistrationDelayMaxJitter = 0.5;
const int RegistrationManager::kMinRegistrationDelaySeconds = 1;
// 1 hour.
const int RegistrationManager::kMaxRegistrationDelaySeconds = 60 * 60;

RegistrationManager::RegistrationManager(
    invalidation::InvalidationClient* invalidation_client)
    : invalidation_client_(invalidation_client) {
  DCHECK(invalidation_client_);
  // Initialize statuses.
  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    RegistrationStatus* status = &registration_statuses_[model_type];
    status->model_type = model_type;
    status->registration_manager = this;
  }
}

RegistrationManager::~RegistrationManager() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
}

void RegistrationManager::SetRegisteredTypes(
    syncable::ModelTypeSet types) {
  DCHECK(non_thread_safe_.CalledOnValidThread());

  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    if (types.Has(model_type)) {
      if (!IsTypeRegistered(model_type)) {
        TryRegisterType(model_type, false /* is_retry */);
      }
    } else {
      if (IsTypeRegistered(model_type)) {
        UnregisterType(model_type);
      }
    }
  }
}

void RegistrationManager::MarkRegistrationLost(
    syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  RegistrationStatus* status = &registration_statuses_[model_type];
  if (!status->enabled) {
    return;
  }
  status->state = invalidation::InvalidationListener::UNREGISTERED;
  bool is_retry = !status->last_registration_request.is_null();
  TryRegisterType(model_type, is_retry);
}

void RegistrationManager::MarkAllRegistrationsLost() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    if (IsTypeRegistered(model_type)) {
      MarkRegistrationLost(model_type);
    }
  }
}

void RegistrationManager::DisableType(syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  RegistrationStatus* status = &registration_statuses_[model_type];
  LOG(INFO) << "Disabling " << syncable::ModelTypeToString(model_type);
  status->Disable();
}

syncable::ModelTypeSet RegistrationManager::GetRegisteredTypes() const {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  syncable::ModelTypeSet registered_types;
  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    if (IsTypeRegistered(model_type)) {
      registered_types.Put(model_type);
    }
  }
  return registered_types;
}

RegistrationManager::PendingRegistrationMap
    RegistrationManager::GetPendingRegistrations() const {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  PendingRegistrationMap pending_registrations;
  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    const RegistrationStatus& status = registration_statuses_[model_type];
    if (status.registration_timer.IsRunning()) {
      pending_registrations[model_type].last_registration_request =
          status.last_registration_request;
      pending_registrations[model_type].registration_attempt =
          status.last_registration_attempt;
      pending_registrations[model_type].delay = status.delay;
      pending_registrations[model_type].actual_delay =
          status.registration_timer.GetCurrentDelay();
    }
  }
  return pending_registrations;
}

void RegistrationManager::FirePendingRegistrationsForTest() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  for (int i = syncable::FIRST_REAL_MODEL_TYPE;
       i < syncable::MODEL_TYPE_COUNT; ++i) {
    syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
    RegistrationStatus* status = &registration_statuses_[model_type];
    if (status->registration_timer.IsRunning()) {
      status->DoRegister();
    }
  }
}

// static
double RegistrationManager::CalculateBackoff(
    double retry_interval,
    double initial_retry_interval,
    double min_retry_interval,
    double max_retry_interval,
    double backoff_exponent,
    double jitter,
    double max_jitter) {
  // scaled_jitter lies in [-max_jitter, max_jitter].
  double scaled_jitter = jitter * max_jitter;
  double new_retry_interval =
      (retry_interval == 0.0) ?
      (initial_retry_interval * (1.0 + scaled_jitter)) :
      (retry_interval * (backoff_exponent + scaled_jitter));
  return std::max(min_retry_interval,
                  std::min(max_retry_interval, new_retry_interval));
}

double RegistrationManager::GetJitter() {
  // |jitter| lies in [-1.0, 1.0), which is low-biased, but only
  // barely.
  //
  // TODO(akalin): Fix the bias.
  return 2.0 * base::RandDouble() - 1.0;
}

void RegistrationManager::TryRegisterType(syncable::ModelType model_type,
                                          bool is_retry) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  RegistrationStatus* status = &registration_statuses_[model_type];
  if (!status->enabled) {
    // Disabled, so do nothing.
    return;
  }
  status->last_registration_attempt = base::Time::Now();
  if (is_retry) {
    // If we're a retry, we must have tried at least once before.
    DCHECK(!status->last_registration_request.is_null());
    // delay = max(0, (now - last request) + next_delay)
    status->delay =
        (status->last_registration_request -
         status->last_registration_attempt) +
        status->next_delay;
    base::TimeDelta delay =
        (status->delay <= base::TimeDelta()) ?
        base::TimeDelta() : status->delay;
    DVLOG(2) << "Registering "
             << syncable::ModelTypeToString(model_type) << " in "
             << delay.InMilliseconds() << " ms";
    status->registration_timer.Stop();
    status->registration_timer.Start(FROM_HERE,
        delay, status, &RegistrationManager::RegistrationStatus::DoRegister);
    double next_delay_seconds =
        CalculateBackoff(static_cast<double>(status->next_delay.InSeconds()),
                         kInitialRegistrationDelaySeconds,
                         kMinRegistrationDelaySeconds,
                         kMaxRegistrationDelaySeconds,
                         kRegistrationDelayExponent,
                         GetJitter(),
                         kRegistrationDelayMaxJitter);
    status->next_delay =
        base::TimeDelta::FromSeconds(static_cast<int64>(next_delay_seconds));
    DVLOG(2) << "New next delay for "
             << syncable::ModelTypeToString(model_type) << " is "
             << status->next_delay.InSeconds() << " seconds";
  } else {
    DVLOG(2) << "Not a retry -- registering "
             << syncable::ModelTypeToString(model_type) << " immediately";
    status->delay = base::TimeDelta();
    status->next_delay = base::TimeDelta();
    status->DoRegister();
  }
}

void RegistrationManager::DoRegisterType(syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  invalidation::ObjectId object_id;
  if (!RealModelTypeToObjectId(model_type, &object_id)) {
    LOG(DFATAL) << "Invalid model type: " << model_type;
    return;
  }
  invalidation_client_->Register(object_id);
  RegistrationStatus* status = &registration_statuses_[model_type];
  status->state = invalidation::InvalidationListener::REGISTERED;
  status->last_registration_request = base::Time::Now();
}

void RegistrationManager::UnregisterType(syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  invalidation::ObjectId object_id;
  if (!RealModelTypeToObjectId(model_type, &object_id)) {
    LOG(DFATAL) << "Invalid model type: " << model_type;
    return;
  }
  invalidation_client_->Unregister(object_id);
  RegistrationStatus* status = &registration_statuses_[model_type];
  status->state = invalidation::InvalidationListener::UNREGISTERED;
}

bool RegistrationManager::IsTypeRegistered(
    syncable::ModelType model_type) const {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  return registration_statuses_[model_type].state ==
      invalidation::InvalidationListener::REGISTERED;
}

}  // namespace sync_notifier