OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/domain_reliability/scheduler.h" | 5 #include "components/domain_reliability/scheduler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "components/domain_reliability/config.h" | 12 #include "components/domain_reliability/config.h" |
13 #include "components/domain_reliability/util.h" | 13 #include "components/domain_reliability/util.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const unsigned kInvalidCollectorIndex = static_cast<unsigned>(-1); | 17 const unsigned kInvalidCollectorIndex = static_cast<unsigned>(-1); |
18 | 18 |
19 const unsigned kDefaultMinimumUploadDelaySec = 60; | 19 const unsigned kDefaultMinimumUploadDelaySec = 60; |
20 const unsigned kDefaultMaximumUploadDelaySec = 300; | 20 const unsigned kDefaultMaximumUploadDelaySec = 300; |
21 const unsigned kDefaultUploadRetryIntervalSec = 60; | 21 const unsigned kDefaultUploadRetryIntervalSec = 60; |
22 | 22 |
23 const char* kMinimumUploadDelayFieldTrialName = "DomRel-MinimumUploadDelay"; | 23 const char* kMinimumUploadDelayFieldTrialName = "DomRel-MinimumUploadDelay"; |
24 const char* kMaximumUploadDelayFieldTrialName = "DomRel-MaximumUploadDelay"; | 24 const char* kMaximumUploadDelayFieldTrialName = "DomRel-MaximumUploadDelay"; |
25 const char* kUploadRetryIntervalFieldTrialName = "DomRel-UploadRetryInterval"; | 25 const char* kUploadRetryIntervalFieldTrialName = "DomRel-UploadRetryInterval"; |
26 | 26 |
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; | |
31 | |
27 unsigned GetUnsignedFieldTrialValueOrDefault(std::string field_trial_name, | 32 unsigned GetUnsignedFieldTrialValueOrDefault(std::string field_trial_name, |
28 unsigned default_value) { | 33 unsigned default_value) { |
29 if (!base::FieldTrialList::TrialExists(field_trial_name)) | 34 if (!base::FieldTrialList::TrialExists(field_trial_name)) |
30 return default_value; | 35 return default_value; |
31 | 36 |
32 std::string group_name = base::FieldTrialList::FindFullName(field_trial_name); | 37 std::string group_name = base::FieldTrialList::FindFullName(field_trial_name); |
33 unsigned value; | 38 unsigned value; |
34 if (!base::StringToUint(group_name, &value)) { | 39 if (!base::StringToUint(group_name, &value)) { |
35 LOG(ERROR) << "Expected unsigned integer for field trial " | 40 LOG(ERROR) << "Expected unsigned integer for field trial " |
36 << field_trial_name << " group name, but got \"" << group_name | 41 << field_trial_name << " group name, but got \"" << group_name |
(...skipping 25 matching lines...) Expand all Loading... | |
62 | 67 |
63 return params; | 68 return params; |
64 } | 69 } |
65 | 70 |
66 DomainReliabilityScheduler::DomainReliabilityScheduler( | 71 DomainReliabilityScheduler::DomainReliabilityScheduler( |
67 MockableTime* time, | 72 MockableTime* time, |
68 size_t num_collectors, | 73 size_t num_collectors, |
69 const Params& params, | 74 const Params& params, |
70 const ScheduleUploadCallback& callback) | 75 const ScheduleUploadCallback& callback) |
71 : time_(time), | 76 : time_(time), |
72 collectors_(num_collectors), | |
73 params_(params), | 77 params_(params), |
74 callback_(callback), | 78 callback_(callback), |
75 upload_pending_(false), | 79 upload_pending_(false), |
76 upload_scheduled_(false), | 80 upload_scheduled_(false), |
77 upload_running_(false), | 81 upload_running_(false), |
78 collector_index_(kInvalidCollectorIndex), | 82 collector_index_(kInvalidCollectorIndex), |
79 last_upload_finished_(false) { | 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; | |
davidben
2014/11/07 20:06:04
Just to confirm, this bit is a no-op from patch se
| |
91 backoff_policy_.always_use_initial_delay = false; | |
92 | |
93 for (size_t i = 0; i < num_collectors; ++i) { | |
94 collectors_.push_back( | |
95 new MockableTimeBackoffEntry(&backoff_policy_, time_)); | |
96 } | |
80 } | 97 } |
81 | 98 |
82 DomainReliabilityScheduler::~DomainReliabilityScheduler() {} | 99 DomainReliabilityScheduler::~DomainReliabilityScheduler() {} |
83 | 100 |
84 void DomainReliabilityScheduler::OnBeaconAdded() { | 101 void DomainReliabilityScheduler::OnBeaconAdded() { |
85 if (!upload_pending_) | 102 if (!upload_pending_) |
86 first_beacon_time_ = time_->NowTicks(); | 103 first_beacon_time_ = time_->NowTicks(); |
87 upload_pending_ = true; | 104 upload_pending_ = true; |
88 MaybeScheduleUpload(); | 105 MaybeScheduleUpload(); |
89 } | 106 } |
(...skipping 19 matching lines...) Expand all Loading... | |
109 } | 126 } |
110 | 127 |
111 void DomainReliabilityScheduler::OnUploadComplete(bool success) { | 128 void DomainReliabilityScheduler::OnUploadComplete(bool success) { |
112 DCHECK(upload_running_); | 129 DCHECK(upload_running_); |
113 DCHECK_NE(kInvalidCollectorIndex, collector_index_); | 130 DCHECK_NE(kInvalidCollectorIndex, collector_index_); |
114 upload_running_ = false; | 131 upload_running_ = false; |
115 | 132 |
116 VLOG(1) << "Upload to collector " << collector_index_ | 133 VLOG(1) << "Upload to collector " << collector_index_ |
117 << (success ? " succeeded." : " failed."); | 134 << (success ? " succeeded." : " failed."); |
118 | 135 |
119 CollectorState* collector = &collectors_[collector_index_]; | 136 net::BackoffEntry* backoff = collectors_[collector_index_]; |
120 collector_index_ = kInvalidCollectorIndex; | 137 collector_index_ = kInvalidCollectorIndex; |
138 backoff->InformOfRequest(success); | |
121 | 139 |
122 if (success) { | 140 if (!success) { |
123 collector->failures = 0; | |
124 } else { | |
125 // Restore upload_pending_ and first_beacon_time_ to pre-upload state, | 141 // Restore upload_pending_ and first_beacon_time_ to pre-upload state, |
126 // since upload failed. | 142 // since upload failed. |
127 upload_pending_ = true; | 143 upload_pending_ = true; |
128 first_beacon_time_ = old_first_beacon_time_; | 144 first_beacon_time_ = old_first_beacon_time_; |
129 | |
130 ++collector->failures; | |
131 } | 145 } |
132 | 146 |
133 base::TimeTicks now = time_->NowTicks(); | 147 last_upload_end_time_ = time_->NowTicks(); |
134 base::TimeDelta retry_interval = GetUploadRetryInterval(collector->failures); | |
135 collector->next_upload = now + retry_interval; | |
136 | |
137 last_upload_end_time_ = now; | |
138 last_upload_success_ = success; | 148 last_upload_success_ = success; |
139 last_upload_finished_ = true; | 149 last_upload_finished_ = true; |
140 | 150 |
141 VLOG(1) << "Next upload to collector at least " | |
142 << retry_interval.InSeconds() << " seconds from now."; | |
143 | |
144 MaybeScheduleUpload(); | 151 MaybeScheduleUpload(); |
145 } | 152 } |
146 | 153 |
147 base::Value* DomainReliabilityScheduler::GetWebUIData() const { | 154 base::Value* DomainReliabilityScheduler::GetWebUIData() const { |
148 base::TimeTicks now = time_->NowTicks(); | 155 base::TimeTicks now = time_->NowTicks(); |
149 | 156 |
150 base::DictionaryValue* data = new base::DictionaryValue(); | 157 base::DictionaryValue* data = new base::DictionaryValue(); |
151 | 158 |
152 data->SetBoolean("upload_pending", upload_pending_); | 159 data->SetBoolean("upload_pending", upload_pending_); |
153 data->SetBoolean("upload_scheduled", upload_scheduled_); | 160 data->SetBoolean("upload_scheduled", upload_scheduled_); |
154 data->SetBoolean("upload_running", upload_running_); | 161 data->SetBoolean("upload_running", upload_running_); |
155 | 162 |
156 data->SetInteger("scheduled_min", (scheduled_min_time_ - now).InSeconds()); | 163 data->SetInteger("scheduled_min", (scheduled_min_time_ - now).InSeconds()); |
157 data->SetInteger("scheduled_max", (scheduled_max_time_ - now).InSeconds()); | 164 data->SetInteger("scheduled_max", (scheduled_max_time_ - now).InSeconds()); |
158 | 165 |
159 data->SetInteger("collector_index", static_cast<int>(collector_index_)); | 166 data->SetInteger("collector_index", static_cast<int>(collector_index_)); |
160 | 167 |
161 if (last_upload_finished_) { | 168 if (last_upload_finished_) { |
162 base::DictionaryValue* last = new base::DictionaryValue(); | 169 base::DictionaryValue* last = new base::DictionaryValue(); |
163 last->SetInteger("start_time", (now - last_upload_start_time_).InSeconds()); | 170 last->SetInteger("start_time", (now - last_upload_start_time_).InSeconds()); |
164 last->SetInteger("end_time", (now - last_upload_end_time_).InSeconds()); | 171 last->SetInteger("end_time", (now - last_upload_end_time_).InSeconds()); |
165 last->SetInteger("collector_index", | 172 last->SetInteger("collector_index", |
166 static_cast<int>(last_upload_collector_index_)); | 173 static_cast<int>(last_upload_collector_index_)); |
167 last->SetBoolean("success", last_upload_success_); | 174 last->SetBoolean("success", last_upload_success_); |
168 data->Set("last_upload", last); | 175 data->Set("last_upload", last); |
169 } | 176 } |
170 | 177 |
171 base::ListValue* collectors = new base::ListValue(); | 178 base::ListValue* collectors = new base::ListValue(); |
172 for (size_t i = 0; i < collectors_.size(); ++i) { | 179 for (size_t i = 0; i < collectors_.size(); ++i) { |
173 const CollectorState* state = &collectors_[i]; | 180 const net::BackoffEntry* backoff = collectors_[i]; |
174 base::DictionaryValue* value = new base::DictionaryValue(); | 181 base::DictionaryValue* value = new base::DictionaryValue(); |
175 value->SetInteger("failures", state->failures); | 182 value->SetInteger("failures", backoff->failure_count()); |
176 value->SetInteger("next_upload", (state->next_upload - now).InSeconds()); | 183 value->SetInteger("next_upload", |
184 (backoff->GetReleaseTime() - now).InSeconds()); | |
177 collectors->Append(value); | 185 collectors->Append(value); |
178 } | 186 } |
179 data->Set("collectors", collectors); | 187 data->Set("collectors", collectors); |
180 | 188 |
181 return data; | 189 return data; |
182 } | 190 } |
183 | 191 |
184 DomainReliabilityScheduler::CollectorState::CollectorState() : failures(0) {} | 192 void DomainReliabilityScheduler::MakeDeterministicForTesting() { |
193 backoff_policy_.jitter_factor = 0.0; | |
194 } | |
185 | 195 |
186 void DomainReliabilityScheduler::MaybeScheduleUpload() { | 196 void DomainReliabilityScheduler::MaybeScheduleUpload() { |
187 if (!upload_pending_ || upload_scheduled_ || upload_running_) | 197 if (!upload_pending_ || upload_scheduled_ || upload_running_) |
188 return; | 198 return; |
189 | 199 |
190 upload_scheduled_ = true; | 200 upload_scheduled_ = true; |
191 old_first_beacon_time_ = first_beacon_time_; | 201 old_first_beacon_time_ = first_beacon_time_; |
192 | 202 |
193 base::TimeTicks now = time_->NowTicks(); | 203 base::TimeTicks now = time_->NowTicks(); |
194 | 204 |
(...skipping 26 matching lines...) Expand all Loading... | |
221 base::TimeTicks now, | 231 base::TimeTicks now, |
222 base::TimeTicks* upload_time_out, | 232 base::TimeTicks* upload_time_out, |
223 size_t* collector_index_out) { | 233 size_t* collector_index_out) { |
224 DCHECK(upload_time_out); | 234 DCHECK(upload_time_out); |
225 DCHECK(collector_index_out); | 235 DCHECK(collector_index_out); |
226 | 236 |
227 base::TimeTicks min_time; | 237 base::TimeTicks min_time; |
228 size_t min_index = kInvalidCollectorIndex; | 238 size_t min_index = kInvalidCollectorIndex; |
229 | 239 |
230 for (size_t i = 0; i < collectors_.size(); ++i) { | 240 for (size_t i = 0; i < collectors_.size(); ++i) { |
231 CollectorState* collector = &collectors_[i]; | 241 net::BackoffEntry* backoff = collectors_[i]; |
232 // If a collector is usable, use the first one in the list. | 242 // If a collector is usable, use the first one in the list. |
233 if (collector->failures == 0 || collector->next_upload <= now) { | 243 if (!backoff->ShouldRejectRequest()) { |
234 min_time = now; | 244 min_time = now; |
235 min_index = i; | 245 min_index = i; |
236 break; | 246 break; |
247 } | |
248 | |
237 // If not, keep track of which will be usable soonest: | 249 // If not, keep track of which will be usable soonest: |
238 } else if (min_index == kInvalidCollectorIndex || | 250 base::TimeTicks time = backoff->GetReleaseTime(); |
239 collector->next_upload < min_time) { | 251 if (min_index == kInvalidCollectorIndex || time < min_time) { |
240 min_time = collector->next_upload; | 252 min_time = time; |
241 min_index = i; | 253 min_index = i; |
242 } | 254 } |
243 } | 255 } |
244 | 256 |
245 DCHECK_NE(kInvalidCollectorIndex, min_index); | 257 DCHECK_NE(kInvalidCollectorIndex, min_index); |
246 *upload_time_out = min_time; | 258 *upload_time_out = min_time; |
247 *collector_index_out = min_index; | 259 *collector_index_out = min_index; |
248 } | 260 } |
249 | 261 |
250 base::TimeDelta DomainReliabilityScheduler::GetUploadRetryInterval( | |
251 unsigned failures) { | |
252 if (failures == 0) | |
253 return base::TimeDelta::FromSeconds(0); | |
254 else { | |
255 // Don't back off more than 64x the original delay. | |
256 if (failures > 7) | |
257 failures = 7; | |
258 return params_.upload_retry_interval * (1 << (failures - 1)); | |
259 } | |
260 } | |
261 | |
262 } // namespace domain_reliability | 262 } // namespace domain_reliability |
OLD | NEW |