Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: components/proximity_auth/cryptauth/sync_scheduler_impl.cc

Issue 1147563002: Add SyncScheduler for scheduling CryptAuth enrollments and syncing devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "components/proximity_auth/cryptauth/sync_scheduler_impl.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10
11 #include "base/bind.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/rand_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "components/proximity_auth/logging/logging.h"
16
17 namespace proximity_auth {
18
19 namespace {
20
21 // Returns a human readable string given a |time_delta|.
22 std::string TimeDeltaToString(const base::TimeDelta& time_delta) {
23 if (time_delta.InDays() > 0)
24 return base::StringPrintf("%d days", time_delta.InDays());
25
26 if (time_delta.InHours() > 0)
27 return base::StringPrintf("%d hours", time_delta.InHours());
28
29 if (time_delta.InMinutes() > 0)
30 return base::StringPrintf("%d minutes", time_delta.InMinutes());
31
32 return base::StringPrintf("%d seconds",
33 base::saturated_cast<int>(time_delta.InSeconds()));
34 }
35
36 } // namespace
37
38 SyncSchedulerImpl::SyncSchedulerImpl(Delegate* delegate,
39 base::TimeDelta refresh_period,
40 base::TimeDelta base_recovery_period,
41 double max_jitter_percentage,
42 const std::string& scheduler_name)
43 : delegate_(delegate),
44 refresh_period_(refresh_period),
45 base_recovery_period_(base_recovery_period),
46 max_jitter_percentage_(max_jitter_percentage),
47 scheduler_name_(scheduler_name),
48 state_(State::NOT_STARTED),
49 weak_ptr_factory_(this) {
50 }
51
52 SyncSchedulerImpl::~SyncSchedulerImpl() {
53 }
54
55 void SyncSchedulerImpl::Start(
56 const base::TimeDelta& elapsed_time_since_last_sync,
57 bool is_aggressive_recovery) {
58 state_ = is_aggressive_recovery ? State::AGGRESSIVE_RECOVERY
59 : State::PERIODIC_REFRESH;
60 // We reset the failure backoff when the scheduler is started again, as
61 // most likely the configuration that caused the previous attempts to fail
62 // most likely won't be present after a restart.
Ilya Sherman 2015/05/18 23:25:38 nit: Double "most likely"
Tim Song 2015/05/19 22:13:18 Done.
63 if (is_aggressive_recovery)
64 failure_count_ = 1;
Ilya Sherman 2015/05/18 23:25:38 Why 1 and not 0?
Tim Song 2015/05/19 22:13:18 This is so the backoff is calculated properly, as
65
66 // To take into account the time waited when the system is powered off, we
67 // subtract the time elapsed with a normal sync period to the initial time
68 // to wait.
69 base::TimeDelta sync_delta =
70 GetJitteredPeriod() - elapsed_time_since_last_sync;
71 if (sync_delta < base::TimeDelta::FromSeconds(0))
72 sync_delta = base::TimeDelta::FromSeconds(0);
73
74 ScheduleNextSync(sync_delta);
75 }
76
77 void SyncSchedulerImpl::ForceSync() {
78 OnTimerFired();
79 }
80
81 base::TimeDelta SyncSchedulerImpl::GetTimeToNextSync() const {
82 if (!timer_)
83 return base::TimeDelta::FromSeconds(0);
84 return timer_->GetCurrentDelay();
85 }
86
87 SyncScheduler::State SyncSchedulerImpl::GetState() const {
88 return state_;
89 }
90
91 void SyncSchedulerImpl::OnTimerFired() {
92 timer_.reset();
93 if (state_ == State::PERIODIC_REFRESH) {
94 PA_LOG(INFO) << "Timer fired for periodic refresh, making request...";
95 state_ = State::SYNCING_FOR_PERIODIC_REFRESH;
96 } else if (state_ == State::AGGRESSIVE_RECOVERY) {
97 PA_LOG(INFO) << "Timer fired for aggressive recovery, making request...";
98 state_ = State::SYNCING_FOR_AGGRESSIVE_RECOVERY;
99 } else {
100 PA_LOG(ERROR) << "Timer fired for unexpected state: "
101 << static_cast<int>(state_);
102 return;
103 }
104
105 delegate_->OnSyncRequested(make_scoped_ptr(
106 new SyncRequest(weak_ptr_factory_.GetWeakPtr(),
107 state_ == State::SYNCING_FOR_AGGRESSIVE_RECOVERY)));
108 }
109
110 scoped_ptr<base::Timer> SyncSchedulerImpl::CreateTimer() {
111 bool retain_user_task = false;
112 bool is_repeating = false;
113 return make_scoped_ptr(new base::Timer(retain_user_task, is_repeating));
114 }
115
116 void SyncSchedulerImpl::ScheduleNextSync(const base::TimeDelta& sync_delta) {
117 if (state_ != State::PERIODIC_REFRESH &&
118 state_ != State::AGGRESSIVE_RECOVERY) {
119 PA_LOG(ERROR) << "Unexpected state when scheduling next sync: state="
120 << static_cast<int>(state_);
121 }
122
123 bool is_aggressive_recovery = (state_ == State::AGGRESSIVE_RECOVERY);
124 PA_LOG(INFO) << "Scheduling next sync for " << scheduler_name_ << ":\r\n"
Ilya Sherman 2015/05/18 23:25:38 nit: Why "\r"?
Tim Song 2015/05/19 22:13:18 I think this looks a bit more readable in the logs
Ilya Sherman 2015/05/19 22:46:55 Sorry, maybe my question wasn't clear enough. Cou
Tim Song 2015/05/20 00:13:22 Done.
125 << " Strategy: " << (is_aggressive_recovery
126 ? "Aggressive Recovery"
127 : "Periodic Refresh") << "\r\n"
128 << " Time Delta: " << TimeDeltaToString(sync_delta)
129 << (is_aggressive_recovery
130 ? base::StringPrintf("\r\n Previous Failures: %d",
131 failure_count_)
132 : "");
133
134 timer_ = CreateTimer();
135 timer_->Start(FROM_HERE, sync_delta,
136 base::Bind(&SyncSchedulerImpl::OnTimerFired,
137 weak_ptr_factory_.GetWeakPtr()));
138 }
139
140 void SyncSchedulerImpl::OnSyncCompleted(bool success) {
141 if (state_ != State::SYNCING_FOR_PERIODIC_REFRESH &&
142 state_ != State::SYNCING_FOR_AGGRESSIVE_RECOVERY) {
143 PA_LOG(ERROR) << "Unexpected state when sync completed: state="
144 << static_cast<int>(state_);
145 return;
146 }
147
148 if (success) {
149 state_ = State::PERIODIC_REFRESH;
150 failure_count_ = 0;
151 } else {
152 state_ = State::AGGRESSIVE_RECOVERY;
153 failure_count_++;
Ilya Sherman 2015/05/18 23:25:38 nit: ++failure_count_
Tim Song 2015/05/19 22:13:18 Done.
154 }
155
156 ScheduleNextSync(GetJitteredPeriod());
157 }
158
159 base::TimeDelta SyncSchedulerImpl::GetJitteredPeriod() {
160 double jitter = 2 * max_jitter_percentage_ * (base::RandDouble() - 0.5);
161 base::TimeDelta period = GetPeriod();
162 base::TimeDelta jittered_time_delta = period + (period * jitter);
Ilya Sherman 2015/05/18 23:25:38 nit: It looks like "max_jitter_percentage_" is act
Tim Song 2015/05/19 22:13:18 Done.
163 if (jittered_time_delta.InMilliseconds() < 0)
164 jittered_time_delta = base::TimeDelta::FromMilliseconds(0);
165 return jittered_time_delta;
166 }
167
168 base::TimeDelta SyncSchedulerImpl::GetPeriod() {
169 if (state_ == State::PERIODIC_REFRESH) {
170 return refresh_period_;
171 } else if (state_ == State::AGGRESSIVE_RECOVERY && failure_count_ > 0) {
172 // The backoff for each consecutive failure is exponentially doubled until
173 // it is equal to the normal refresh period.
174 // Note: |backoff_factor| may evaulate to INF if |failure_count_| is large,
175 // but multiplication operations for TimeDelta objects are saturated.
176 double backoff_factor = pow(2, failure_count_ - 1);
177 base::TimeDelta backoff_period = base_recovery_period_ * backoff_factor;
178 return backoff_period < refresh_period_ ? backoff_period : refresh_period_;
179 } else {
180 PA_LOG(ERROR) << "Error getting period in state: "
181 << static_cast<int>(state_);
182 return base::TimeDelta();
183 }
184 }
185
186 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698