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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/proximity_auth/cryptauth/sync_scheduler_impl.cc
diff --git a/components/proximity_auth/cryptauth/sync_scheduler_impl.cc b/components/proximity_auth/cryptauth/sync_scheduler_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..42aa704391bba86ee88afad04c16d9ff9b8f9895
--- /dev/null
+++ b/components/proximity_auth/cryptauth/sync_scheduler_impl.cc
@@ -0,0 +1,186 @@
+// Copyright 2015 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 "components/proximity_auth/cryptauth/sync_scheduler_impl.h"
+
+#include <algorithm>
+#include <cmath>
+#include <limits>
+
+#include "base/bind.h"
+#include "base/numerics/safe_conversions.h"
+#include "base/rand_util.h"
+#include "base/strings/stringprintf.h"
+#include "components/proximity_auth/logging/logging.h"
+
+namespace proximity_auth {
+
+namespace {
+
+// Returns a human readable string given a |time_delta|.
+std::string TimeDeltaToString(const base::TimeDelta& time_delta) {
+ if (time_delta.InDays() > 0)
+ return base::StringPrintf("%d days", time_delta.InDays());
+
+ if (time_delta.InHours() > 0)
+ return base::StringPrintf("%d hours", time_delta.InHours());
+
+ if (time_delta.InMinutes() > 0)
+ return base::StringPrintf("%d minutes", time_delta.InMinutes());
+
+ return base::StringPrintf("%d seconds",
+ base::saturated_cast<int>(time_delta.InSeconds()));
+}
+
+} // namespace
+
+SyncSchedulerImpl::SyncSchedulerImpl(Delegate* delegate,
+ base::TimeDelta refresh_period,
+ base::TimeDelta base_recovery_period,
+ double max_jitter_percentage,
+ const std::string& scheduler_name)
+ : delegate_(delegate),
+ refresh_period_(refresh_period),
+ base_recovery_period_(base_recovery_period),
+ max_jitter_percentage_(max_jitter_percentage),
+ scheduler_name_(scheduler_name),
+ state_(State::NOT_STARTED),
+ weak_ptr_factory_(this) {
+}
+
+SyncSchedulerImpl::~SyncSchedulerImpl() {
+}
+
+void SyncSchedulerImpl::Start(
+ const base::TimeDelta& elapsed_time_since_last_sync,
+ bool is_aggressive_recovery) {
+ state_ = is_aggressive_recovery ? State::AGGRESSIVE_RECOVERY
+ : State::PERIODIC_REFRESH;
+ // We reset the failure backoff when the scheduler is started again, as
+ // most likely the configuration that caused the previous attempts to fail
+ // 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.
+ if (is_aggressive_recovery)
+ 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
+
+ // To take into account the time waited when the system is powered off, we
+ // subtract the time elapsed with a normal sync period to the initial time
+ // to wait.
+ base::TimeDelta sync_delta =
+ GetJitteredPeriod() - elapsed_time_since_last_sync;
+ if (sync_delta < base::TimeDelta::FromSeconds(0))
+ sync_delta = base::TimeDelta::FromSeconds(0);
+
+ ScheduleNextSync(sync_delta);
+}
+
+void SyncSchedulerImpl::ForceSync() {
+ OnTimerFired();
+}
+
+base::TimeDelta SyncSchedulerImpl::GetTimeToNextSync() const {
+ if (!timer_)
+ return base::TimeDelta::FromSeconds(0);
+ return timer_->GetCurrentDelay();
+}
+
+SyncScheduler::State SyncSchedulerImpl::GetState() const {
+ return state_;
+}
+
+void SyncSchedulerImpl::OnTimerFired() {
+ timer_.reset();
+ if (state_ == State::PERIODIC_REFRESH) {
+ PA_LOG(INFO) << "Timer fired for periodic refresh, making request...";
+ state_ = State::SYNCING_FOR_PERIODIC_REFRESH;
+ } else if (state_ == State::AGGRESSIVE_RECOVERY) {
+ PA_LOG(INFO) << "Timer fired for aggressive recovery, making request...";
+ state_ = State::SYNCING_FOR_AGGRESSIVE_RECOVERY;
+ } else {
+ PA_LOG(ERROR) << "Timer fired for unexpected state: "
+ << static_cast<int>(state_);
+ return;
+ }
+
+ delegate_->OnSyncRequested(make_scoped_ptr(
+ new SyncRequest(weak_ptr_factory_.GetWeakPtr(),
+ state_ == State::SYNCING_FOR_AGGRESSIVE_RECOVERY)));
+}
+
+scoped_ptr<base::Timer> SyncSchedulerImpl::CreateTimer() {
+ bool retain_user_task = false;
+ bool is_repeating = false;
+ return make_scoped_ptr(new base::Timer(retain_user_task, is_repeating));
+}
+
+void SyncSchedulerImpl::ScheduleNextSync(const base::TimeDelta& sync_delta) {
+ if (state_ != State::PERIODIC_REFRESH &&
+ state_ != State::AGGRESSIVE_RECOVERY) {
+ PA_LOG(ERROR) << "Unexpected state when scheduling next sync: state="
+ << static_cast<int>(state_);
+ }
+
+ bool is_aggressive_recovery = (state_ == State::AGGRESSIVE_RECOVERY);
+ 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.
+ << " Strategy: " << (is_aggressive_recovery
+ ? "Aggressive Recovery"
+ : "Periodic Refresh") << "\r\n"
+ << " Time Delta: " << TimeDeltaToString(sync_delta)
+ << (is_aggressive_recovery
+ ? base::StringPrintf("\r\n Previous Failures: %d",
+ failure_count_)
+ : "");
+
+ timer_ = CreateTimer();
+ timer_->Start(FROM_HERE, sync_delta,
+ base::Bind(&SyncSchedulerImpl::OnTimerFired,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void SyncSchedulerImpl::OnSyncCompleted(bool success) {
+ if (state_ != State::SYNCING_FOR_PERIODIC_REFRESH &&
+ state_ != State::SYNCING_FOR_AGGRESSIVE_RECOVERY) {
+ PA_LOG(ERROR) << "Unexpected state when sync completed: state="
+ << static_cast<int>(state_);
+ return;
+ }
+
+ if (success) {
+ state_ = State::PERIODIC_REFRESH;
+ failure_count_ = 0;
+ } else {
+ state_ = State::AGGRESSIVE_RECOVERY;
+ failure_count_++;
Ilya Sherman 2015/05/18 23:25:38 nit: ++failure_count_
Tim Song 2015/05/19 22:13:18 Done.
+ }
+
+ ScheduleNextSync(GetJitteredPeriod());
+}
+
+base::TimeDelta SyncSchedulerImpl::GetJitteredPeriod() {
+ double jitter = 2 * max_jitter_percentage_ * (base::RandDouble() - 0.5);
+ base::TimeDelta period = GetPeriod();
+ 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.
+ if (jittered_time_delta.InMilliseconds() < 0)
+ jittered_time_delta = base::TimeDelta::FromMilliseconds(0);
+ return jittered_time_delta;
+}
+
+base::TimeDelta SyncSchedulerImpl::GetPeriod() {
+ if (state_ == State::PERIODIC_REFRESH) {
+ return refresh_period_;
+ } else if (state_ == State::AGGRESSIVE_RECOVERY && failure_count_ > 0) {
+ // The backoff for each consecutive failure is exponentially doubled until
+ // it is equal to the normal refresh period.
+ // Note: |backoff_factor| may evaulate to INF if |failure_count_| is large,
+ // but multiplication operations for TimeDelta objects are saturated.
+ double backoff_factor = pow(2, failure_count_ - 1);
+ base::TimeDelta backoff_period = base_recovery_period_ * backoff_factor;
+ return backoff_period < refresh_period_ ? backoff_period : refresh_period_;
+ } else {
+ PA_LOG(ERROR) << "Error getting period in state: "
+ << static_cast<int>(state_);
+ return base::TimeDelta();
+ }
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698