Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / components / domain_reliability / scheduler.cc
blobd16dfe355edea9e0861356109d52c506b7ed73d0
1 // Copyright 2014 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.
5 #include "components/domain_reliability/scheduler.h"
7 #include <algorithm>
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/values.h"
12 #include "components/domain_reliability/config.h"
13 #include "components/domain_reliability/util.h"
15 namespace {
17 const unsigned kInvalidCollectorIndex = static_cast<unsigned>(-1);
19 const unsigned kDefaultMinimumUploadDelaySec = 60;
20 const unsigned kDefaultMaximumUploadDelaySec = 300;
21 const unsigned kDefaultUploadRetryIntervalSec = 60;
23 const char* kMinimumUploadDelayFieldTrialName = "DomRel-MinimumUploadDelay";
24 const char* kMaximumUploadDelayFieldTrialName = "DomRel-MaximumUploadDelay";
25 const char* kUploadRetryIntervalFieldTrialName = "DomRel-UploadRetryInterval";
27 // Fixed elements of backoff policy
28 const double kMultiplyFactor = 2.0;
29 const double kJitterFactor = 0.1;
30 const int64 kMaximumBackoffMs = 60 * 1000 * 1000;
32 unsigned GetUnsignedFieldTrialValueOrDefault(std::string field_trial_name,
33 unsigned default_value) {
34 if (!base::FieldTrialList::TrialExists(field_trial_name))
35 return default_value;
37 std::string group_name = base::FieldTrialList::FindFullName(field_trial_name);
38 unsigned value;
39 if (!base::StringToUint(group_name, &value)) {
40 LOG(ERROR) << "Expected unsigned integer for field trial "
41 << field_trial_name << " group name, but got \"" << group_name
42 << "\".";
43 return default_value;
46 return value;
49 } // namespace
51 namespace domain_reliability {
53 // static
54 DomainReliabilityScheduler::Params
55 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults() {
56 DomainReliabilityScheduler::Params params;
58 params.minimum_upload_delay =
59 base::TimeDelta::FromSeconds(GetUnsignedFieldTrialValueOrDefault(
60 kMinimumUploadDelayFieldTrialName, kDefaultMinimumUploadDelaySec));
61 params.maximum_upload_delay =
62 base::TimeDelta::FromSeconds(GetUnsignedFieldTrialValueOrDefault(
63 kMaximumUploadDelayFieldTrialName, kDefaultMaximumUploadDelaySec));
64 params.upload_retry_interval =
65 base::TimeDelta::FromSeconds(GetUnsignedFieldTrialValueOrDefault(
66 kUploadRetryIntervalFieldTrialName, kDefaultUploadRetryIntervalSec));
68 return params;
71 DomainReliabilityScheduler::DomainReliabilityScheduler(
72 MockableTime* time,
73 size_t num_collectors,
74 const Params& params,
75 const ScheduleUploadCallback& callback)
76 : time_(time),
77 params_(params),
78 callback_(callback),
79 upload_pending_(false),
80 upload_scheduled_(false),
81 upload_running_(false),
82 collector_index_(kInvalidCollectorIndex),
83 last_upload_finished_(false) {
84 backoff_policy_.num_errors_to_ignore = 0;
85 backoff_policy_.initial_delay_ms =
86 params.upload_retry_interval.InMilliseconds();
87 backoff_policy_.multiply_factor = kMultiplyFactor;
88 backoff_policy_.jitter_factor = kJitterFactor;
89 backoff_policy_.maximum_backoff_ms = kMaximumBackoffMs;
90 backoff_policy_.entry_lifetime_ms = 0;
91 backoff_policy_.always_use_initial_delay = false;
93 for (size_t i = 0; i < num_collectors; ++i) {
94 collectors_.push_back(
95 new MockableTimeBackoffEntry(&backoff_policy_, time_));
99 DomainReliabilityScheduler::~DomainReliabilityScheduler() {}
101 void DomainReliabilityScheduler::OnBeaconAdded() {
102 if (!upload_pending_)
103 first_beacon_time_ = time_->NowTicks();
104 upload_pending_ = true;
105 MaybeScheduleUpload();
108 size_t DomainReliabilityScheduler::OnUploadStart() {
109 DCHECK(upload_scheduled_);
110 DCHECK_EQ(kInvalidCollectorIndex, collector_index_);
111 upload_pending_ = false;
112 upload_scheduled_ = false;
113 upload_running_ = true;
115 base::TimeTicks now = time_->NowTicks();
116 base::TimeTicks min_upload_time;
117 GetNextUploadTimeAndCollector(now, &min_upload_time, &collector_index_);
118 DCHECK(min_upload_time <= now);
120 VLOG(1) << "Starting upload to collector " << collector_index_ << ".";
122 last_upload_start_time_ = now;
123 last_upload_collector_index_ = collector_index_;
125 return collector_index_;
128 void DomainReliabilityScheduler::OnUploadComplete(
129 const DomainReliabilityUploader::UploadResult& result) {
130 DCHECK(upload_running_);
131 DCHECK_NE(kInvalidCollectorIndex, collector_index_);
132 upload_running_ = false;
134 VLOG(1) << "Upload to collector " << collector_index_
135 << (result.is_success() ? " succeeded." : " failed.");
137 net::BackoffEntry* backoff = collectors_[collector_index_];
138 collector_index_ = kInvalidCollectorIndex;
140 backoff->InformOfRequest(result.is_success());
141 if (result.is_retry_after())
142 backoff->SetCustomReleaseTime(time_->NowTicks() + result.retry_after);
143 last_collector_retry_delay_ = backoff->GetTimeUntilRelease();
145 if (!result.is_success()) {
146 // Restore upload_pending_ and first_beacon_time_ to pre-upload state,
147 // since upload failed.
148 upload_pending_ = true;
149 first_beacon_time_ = old_first_beacon_time_;
152 last_upload_end_time_ = time_->NowTicks();
153 last_upload_success_ = result.is_success();
154 last_upload_finished_ = true;
156 MaybeScheduleUpload();
159 scoped_ptr<base::Value> DomainReliabilityScheduler::GetWebUIData() const {
160 base::TimeTicks now = time_->NowTicks();
162 scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
164 data->SetBoolean("upload_pending", upload_pending_);
165 data->SetBoolean("upload_scheduled", upload_scheduled_);
166 data->SetBoolean("upload_running", upload_running_);
168 data->SetInteger("scheduled_min", (scheduled_min_time_ - now).InSeconds());
169 data->SetInteger("scheduled_max", (scheduled_max_time_ - now).InSeconds());
171 data->SetInteger("collector_index", static_cast<int>(collector_index_));
173 if (last_upload_finished_) {
174 scoped_ptr<base::DictionaryValue> last(new base::DictionaryValue());
175 last->SetInteger("start_time", (now - last_upload_start_time_).InSeconds());
176 last->SetInteger("end_time", (now - last_upload_end_time_).InSeconds());
177 last->SetInteger("collector_index",
178 static_cast<int>(last_upload_collector_index_));
179 last->SetBoolean("success", last_upload_success_);
180 data->Set("last_upload", last.Pass());
183 scoped_ptr<base::ListValue> collectors_value(new base::ListValue());
184 for (const auto& collector : collectors_) {
185 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
186 value->SetInteger("failures", collector->failure_count());
187 value->SetInteger("next_upload",
188 (collector->GetReleaseTime() - now).InSeconds());
189 // Using release instead of Pass because Pass can't implicitly upcast.
190 collectors_value->Append(value.release());
192 data->Set("collectors", collectors_value.Pass());
194 return data.Pass();
197 void DomainReliabilityScheduler::MakeDeterministicForTesting() {
198 backoff_policy_.jitter_factor = 0.0;
201 void DomainReliabilityScheduler::MaybeScheduleUpload() {
202 if (!upload_pending_ || upload_scheduled_ || upload_running_)
203 return;
205 upload_scheduled_ = true;
206 old_first_beacon_time_ = first_beacon_time_;
208 base::TimeTicks now = time_->NowTicks();
210 base::TimeTicks min_by_deadline, max_by_deadline;
211 min_by_deadline = first_beacon_time_ + params_.minimum_upload_delay;
212 max_by_deadline = first_beacon_time_ + params_.maximum_upload_delay;
213 DCHECK(min_by_deadline <= max_by_deadline);
215 base::TimeTicks min_by_backoff;
216 size_t collector_index;
217 GetNextUploadTimeAndCollector(now, &min_by_backoff, &collector_index);
219 scheduled_min_time_ = std::max(min_by_deadline, min_by_backoff);
220 scheduled_max_time_ = std::max(max_by_deadline, min_by_backoff);
222 base::TimeDelta min_delay = scheduled_min_time_ - now;
223 base::TimeDelta max_delay = scheduled_max_time_ - now;
225 VLOG(1) << "Scheduling upload for between " << min_delay.InSeconds()
226 << " and " << max_delay.InSeconds() << " seconds from now.";
228 callback_.Run(min_delay, max_delay);
231 // TODO(ttuttle): Add min and max interval to config, use that instead.
233 // TODO(ttuttle): Cap min and max intervals received from config.
235 void DomainReliabilityScheduler::GetNextUploadTimeAndCollector(
236 base::TimeTicks now,
237 base::TimeTicks* upload_time_out,
238 size_t* collector_index_out) {
239 DCHECK(upload_time_out);
240 DCHECK(collector_index_out);
242 base::TimeTicks min_time;
243 size_t min_index = kInvalidCollectorIndex;
245 for (size_t i = 0; i < collectors_.size(); ++i) {
246 net::BackoffEntry* backoff = collectors_[i];
247 // If a collector is usable, use the first one in the list.
248 if (!backoff->ShouldRejectRequest()) {
249 min_time = now;
250 min_index = i;
251 break;
254 // If not, keep track of which will be usable soonest:
255 base::TimeTicks time = backoff->GetReleaseTime();
256 if (min_index == kInvalidCollectorIndex || time < min_time) {
257 min_time = time;
258 min_index = i;
262 DCHECK_NE(kInvalidCollectorIndex, min_index);
263 *upload_time_out = min_time;
264 *collector_index_out = min_index;
267 } // namespace domain_reliability