Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / components / domain_reliability / context.cc
blob230581f875ccbae73d5be787e16d7de19f47112a
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/context.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/values.h"
15 #include "components/domain_reliability/dispatcher.h"
16 #include "components/domain_reliability/uploader.h"
17 #include "components/domain_reliability/util.h"
18 #include "net/base/net_errors.h"
19 #include "net/url_request/url_request_context_getter.h"
21 using base::DictionaryValue;
22 using base::ListValue;
23 using base::Value;
25 namespace domain_reliability {
27 namespace {
28 typedef std::deque<DomainReliabilityBeacon> BeaconDeque;
29 typedef BeaconDeque::iterator BeaconIterator;
30 typedef BeaconDeque::const_iterator BeaconConstIterator;
31 } // namespace
33 DomainReliabilityContext::Factory::~Factory() {
36 class DomainReliabilityContext::ResourceState {
37 public:
38 ResourceState(DomainReliabilityContext* context,
39 const DomainReliabilityConfig::Resource* config)
40 : context(context),
41 config(config),
42 successful_requests(0),
43 failed_requests(0),
44 uploading_successful_requests(0),
45 uploading_failed_requests(0) {}
46 ~ResourceState() {}
48 // Serializes the resource state into a Value to be included in an upload.
49 // If there is nothing to report (no beacons and all request counters are 0),
50 // returns a scoped_ptr to NULL instead so the resource can be omitted.
51 scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const {
52 if (successful_requests == 0 && failed_requests == 0)
53 return scoped_ptr<base::Value>();
55 DictionaryValue* resource_value = new DictionaryValue();
56 resource_value->SetString("name", config->name);
57 resource_value->SetInteger("successful_requests", successful_requests);
58 resource_value->SetInteger("failed_requests", failed_requests);
60 return scoped_ptr<Value>(resource_value);
63 // Remembers the current state of the resource data when an upload starts.
64 void MarkUpload() {
65 DCHECK_EQ(0u, uploading_successful_requests);
66 DCHECK_EQ(0u, uploading_failed_requests);
67 uploading_successful_requests = successful_requests;
68 uploading_failed_requests = failed_requests;
71 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
72 // data but keep beacons and request counts added after the upload started.
73 void CommitUpload() {
74 successful_requests -= uploading_successful_requests;
75 failed_requests -= uploading_failed_requests;
76 uploading_successful_requests = 0;
77 uploading_failed_requests = 0;
80 void RollbackUpload() {
81 uploading_successful_requests = 0;
82 uploading_failed_requests = 0;
85 DomainReliabilityContext* context;
86 const DomainReliabilityConfig::Resource* config;
88 uint32 successful_requests;
89 uint32 failed_requests;
91 // State saved during uploads; if an upload succeeds, these are used to
92 // remove uploaded data from the beacon list and request counters.
93 uint32 uploading_successful_requests;
94 uint32 uploading_failed_requests;
96 private:
97 DISALLOW_COPY_AND_ASSIGN(ResourceState);
100 // static
101 const size_t DomainReliabilityContext::kMaxQueuedBeacons = 150;
103 DomainReliabilityContext::DomainReliabilityContext(
104 MockableTime* time,
105 const DomainReliabilityScheduler::Params& scheduler_params,
106 const std::string& upload_reporter_string,
107 const base::TimeTicks* last_network_change_time,
108 DomainReliabilityDispatcher* dispatcher,
109 DomainReliabilityUploader* uploader,
110 scoped_ptr<const DomainReliabilityConfig> config)
111 : config_(config.Pass()),
112 time_(time),
113 upload_reporter_string_(upload_reporter_string),
114 scheduler_(time,
115 config_->collectors.size(),
116 scheduler_params,
117 base::Bind(&DomainReliabilityContext::ScheduleUpload,
118 base::Unretained(this))),
119 dispatcher_(dispatcher),
120 uploader_(uploader),
121 uploading_beacons_size_(0),
122 last_network_change_time_(last_network_change_time),
123 weak_factory_(this) {
124 InitializeResourceStates();
127 DomainReliabilityContext::~DomainReliabilityContext() {}
129 void DomainReliabilityContext::OnBeacon(const GURL& url,
130 const DomainReliabilityBeacon& beacon) {
131 size_t index = config_->GetResourceIndexForUrl(url);
132 if (index == DomainReliabilityConfig::kInvalidResourceIndex)
133 return;
134 DCHECK_GT(states_.size(), index);
136 bool success = (beacon.status == "ok");
138 ResourceState* state = states_[index];
139 if (success)
140 ++state->successful_requests;
141 else
142 ++state->failed_requests;
144 bool reported = false;
145 bool evicted = false;
146 if (state->config->DecideIfShouldReportRequest(success)) {
147 beacons_.push_back(beacon);
148 beacons_.back().resource = state->config->name;
149 if (beacons_.size() > kMaxQueuedBeacons) {
150 RemoveOldestBeacon();
151 evicted = true;
153 scheduler_.OnBeaconAdded();
154 reported = true;
155 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.ReportedBeaconError",
156 -beacon.chrome_error);
157 // TODO(ttuttle): Histogram HTTP response code?
160 UMA_HISTOGRAM_BOOLEAN("DomainReliability.BeaconReported", reported);
161 UMA_HISTOGRAM_BOOLEAN("DomainReliability.OnBeaconDidEvict", evicted);
164 void DomainReliabilityContext::ClearBeacons() {
165 for (auto& state : states_) {
166 state->successful_requests = 0;
167 state->failed_requests = 0;
168 state->uploading_successful_requests = 0;
169 state->uploading_failed_requests = 0;
171 beacons_.clear();
172 uploading_beacons_size_ = 0;
175 scoped_ptr<base::Value> DomainReliabilityContext::GetWebUIData() const {
176 base::DictionaryValue* context_value = new base::DictionaryValue();
178 context_value->SetString("domain", config().domain);
179 context_value->SetInteger("beacon_count", static_cast<int>(beacons_.size()));
180 context_value->SetInteger("uploading_beacon_count",
181 static_cast<int>(uploading_beacons_size_));
182 context_value->Set("scheduler", scheduler_.GetWebUIData());
184 return scoped_ptr<base::Value>(context_value);
187 void DomainReliabilityContext::GetQueuedBeaconsForTesting(
188 std::vector<DomainReliabilityBeacon>* beacons_out) const {
189 beacons_out->assign(beacons_.begin(), beacons_.end());
192 void DomainReliabilityContext::GetRequestCountsForTesting(
193 size_t resource_index,
194 uint32_t* successful_requests_out,
195 uint32_t* failed_requests_out) const {
196 DCHECK_NE(DomainReliabilityConfig::kInvalidResourceIndex, resource_index);
197 DCHECK_GT(states_.size(), resource_index);
199 const ResourceState& state = *states_[resource_index];
200 *successful_requests_out = state.successful_requests;
201 *failed_requests_out = state.failed_requests;
204 void DomainReliabilityContext::InitializeResourceStates() {
205 for (auto& resource : config_->resources)
206 states_.push_back(new ResourceState(this, resource));
209 void DomainReliabilityContext::ScheduleUpload(
210 base::TimeDelta min_delay,
211 base::TimeDelta max_delay) {
212 dispatcher_->ScheduleTask(
213 base::Bind(
214 &DomainReliabilityContext::StartUpload,
215 weak_factory_.GetWeakPtr()),
216 min_delay,
217 max_delay);
220 void DomainReliabilityContext::StartUpload() {
221 MarkUpload();
223 DCHECK(upload_time_.is_null());
224 upload_time_ = time_->NowTicks();
225 std::string report_json;
226 scoped_ptr<const Value> report_value(CreateReport(upload_time_));
227 base::JSONWriter::Write(report_value.get(), &report_json);
228 report_value.reset();
230 size_t collector_index = scheduler_.OnUploadStart();
232 uploader_->UploadReport(
233 report_json,
234 config_->collectors[collector_index]->upload_url,
235 base::Bind(
236 &DomainReliabilityContext::OnUploadComplete,
237 weak_factory_.GetWeakPtr()));
239 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.UploadCollectorIndex",
240 static_cast<int>(collector_index));
241 if (!last_upload_time_.is_null()) {
242 UMA_HISTOGRAM_LONG_TIMES("DomainReliability.UploadInterval",
243 upload_time_ - last_upload_time_);
247 void DomainReliabilityContext::OnUploadComplete(
248 const DomainReliabilityUploader::UploadResult& result) {
249 if (result.is_success())
250 CommitUpload();
251 else
252 RollbackUpload();
253 base::TimeTicks first_beacon_time = scheduler_.first_beacon_time();
254 scheduler_.OnUploadComplete(result);
255 UMA_HISTOGRAM_BOOLEAN("DomainReliability.UploadSuccess",
256 result.is_success());
257 base::TimeTicks now = time_->NowTicks();
258 UMA_HISTOGRAM_LONG_TIMES("DomainReliability.UploadLatency",
259 now - first_beacon_time);
260 DCHECK(!upload_time_.is_null());
261 UMA_HISTOGRAM_MEDIUM_TIMES("DomainReliability.UploadDuration",
262 now - upload_time_);
263 UMA_HISTOGRAM_LONG_TIMES("DomainReliability.UploadCollectorRetryDelay",
264 scheduler_.last_collector_retry_delay());
265 last_upload_time_ = upload_time_;
266 upload_time_ = base::TimeTicks();
269 scoped_ptr<const Value> DomainReliabilityContext::CreateReport(
270 base::TimeTicks upload_time) const {
271 scoped_ptr<ListValue> beacons_value(new ListValue());
272 for (const auto& beacon : beacons_) {
273 beacons_value->Append(
274 beacon.ToValue(upload_time, *last_network_change_time_));
277 scoped_ptr<ListValue> resources_value(new ListValue());
278 for (const auto& state : states_) {
279 scoped_ptr<Value> resource_report = state->ToValue(upload_time);
280 if (resource_report)
281 resources_value->Append(resource_report.release());
284 DictionaryValue* report_value = new DictionaryValue();
285 if (!config().version.empty())
286 report_value->SetString("config_version", config().version);
287 report_value->SetString("reporter", upload_reporter_string_);
288 report_value->Set("entries", beacons_value.release());
289 if (!resources_value->empty())
290 report_value->Set("resources", resources_value.release());
292 return scoped_ptr<const Value>(report_value);
295 void DomainReliabilityContext::MarkUpload() {
296 for (auto& state : states_)
297 state->MarkUpload();
298 DCHECK_EQ(0u, uploading_beacons_size_);
299 uploading_beacons_size_ = beacons_.size();
300 DCHECK_NE(0u, uploading_beacons_size_);
303 void DomainReliabilityContext::CommitUpload() {
304 for (auto& state : states_)
305 state->CommitUpload();
306 BeaconIterator begin = beacons_.begin();
307 BeaconIterator end = begin + uploading_beacons_size_;
308 beacons_.erase(begin, end);
309 DCHECK_NE(0u, uploading_beacons_size_);
310 uploading_beacons_size_ = 0;
313 void DomainReliabilityContext::RollbackUpload() {
314 for (auto& state : states_)
315 state->RollbackUpload();
316 DCHECK_NE(0u, uploading_beacons_size_);
317 uploading_beacons_size_ = 0;
320 void DomainReliabilityContext::RemoveOldestBeacon() {
321 DCHECK(!beacons_.empty());
323 VLOG(1) << "Beacon queue for " << config().domain << " full; "
324 << "removing oldest beacon";
326 beacons_.pop_front();
328 // If that just removed a beacon counted in uploading_beacons_size_, decrement
329 // that.
330 if (uploading_beacons_size_ > 0)
331 --uploading_beacons_size_;
334 } // namespace domain_reliability