Chromium Code Reviews| 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/context.h" | 5 #include "components/domain_reliability/context.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "components/domain_reliability/dispatcher.h" | 15 #include "components/domain_reliability/dispatcher.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 18 | 18 |
| 19 using base::DictionaryValue; | 19 using base::DictionaryValue; |
| 20 using base::ListValue; | 20 using base::ListValue; |
| 21 using base::Value; | 21 using base::Value; |
| 22 | 22 |
| 23 namespace domain_reliability { | 23 namespace domain_reliability { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | |
| 26 const char* kReporter = "chrome"; | 27 const char* kReporter = "chrome"; |
|
Ryan Sleevi
2014/04/25 01:00:02
layering violation: This should not be hardcoded i
Deprecated (see juliatuttle)
2014/04/28 22:01:13
Done.
| |
| 27 typedef std::deque<DomainReliabilityBeacon> BeaconDeque; | 28 typedef std::deque<DomainReliabilityBeacon> BeaconDeque; |
| 28 typedef BeaconDeque::iterator BeaconIterator; | 29 typedef BeaconDeque::iterator BeaconIterator; |
| 29 typedef BeaconDeque::const_iterator BeaconConstIterator; | 30 typedef BeaconDeque::const_iterator BeaconConstIterator; |
| 31 | |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 const int DomainReliabilityContext::kMaxQueuedBeacons = 150; | 34 const int DomainReliabilityContext::kMaxQueuedBeacons = 150; |
| 33 | 35 |
| 34 DomainReliabilityContext::DomainReliabilityContext( | 36 DomainReliabilityContext::DomainReliabilityContext( |
| 35 MockableTime* time, | 37 MockableTime* time, |
| 36 const DomainReliabilityScheduler::Params& scheduler_params, | 38 const DomainReliabilityScheduler::Params& scheduler_params, |
| 37 DomainReliabilityDispatcher* dispatcher, | 39 DomainReliabilityDispatcher* dispatcher, |
| 38 DomainReliabilityUploader* uploader, | 40 DomainReliabilityUploader* uploader, |
| 39 scoped_ptr<const DomainReliabilityConfig> config) | 41 scoped_ptr<const DomainReliabilityConfig> config) |
| 40 : config_(config.Pass()), | 42 : config_(config.Pass()), |
| 41 time_(time), | 43 time_(time), |
| 42 scheduler_(time, config_->collectors.size(), scheduler_params, | 44 scheduler_(time, config_->collectors.size(), scheduler_params, |
| 43 base::Bind(&DomainReliabilityContext::ScheduleUpload, | 45 base::Bind(&DomainReliabilityContext::ScheduleUpload, |
| 44 base::Unretained(this))), | 46 base::Unretained(this))), |
| 45 dispatcher_(dispatcher), | 47 dispatcher_(dispatcher), |
| 46 uploader_(uploader), | 48 uploader_(uploader), |
| 47 beacon_count_(0), | 49 beacon_count_(0), |
| 48 weak_factory_(this) { | 50 weak_factory_(this) { |
| 49 InitializeResourceStates(); | 51 InitializeResourceStates(); |
| 50 } | 52 } |
| 51 | 53 |
| 52 DomainReliabilityContext::~DomainReliabilityContext() {} | 54 DomainReliabilityContext::~DomainReliabilityContext() {} |
| 53 | 55 |
| 54 void DomainReliabilityContext::AddBeacon( | 56 void DomainReliabilityContext::AddBeacon( |
| 55 const DomainReliabilityBeacon& beacon, | 57 const DomainReliabilityBeacon& beacon, |
| 56 const GURL& url) { | 58 const GURL& url) { |
| 57 int index = config_->GetResourceIndexForUrl(url); | 59 int index = config_->GetResourceIndexForUrl(url); |
| 58 if (index < 0) | 60 if (index < 0) |
| 59 return; | 61 return; |
|
Ryan Sleevi
2014/04/25 01:00:02
size_t & npos (aka ((size_t)~0)) for a safer appro
Deprecated (see juliatuttle)
2014/04/28 22:01:13
Done.
| |
| 60 DCHECK_GT(states_.size(), static_cast<size_t>(index)); | 62 DCHECK_GT(states_.size(), static_cast<size_t>(index)); |
| 61 | 63 |
| 62 ResourceState* state = states_[index]; | 64 ResourceState* state = states_[index]; |
| 63 bool success = beacon.http_response_code >= 200 && | 65 bool success = beacon.http_response_code >= 200 && |
| 64 beacon.http_response_code < 400; | 66 beacon.http_response_code < 400; |
| 65 if (success) | 67 if (success) |
| 66 ++state->successful_requests; | 68 ++state->successful_requests; |
| 67 else | 69 else |
| 68 ++state->failed_requests; | 70 ++state->failed_requests; |
| 69 | 71 |
| 70 VLOG(1) << "Received Beacon: " | 72 VLOG(1) << "Received Beacon: " |
| 71 << state->config->name << " " | 73 << state->config->name << " " |
| 72 << beacon.status << " " | 74 << beacon.status << " " |
| 73 << beacon.chrome_error << " " | 75 << beacon.chrome_error << " " |
| 74 << beacon.http_response_code << " " | 76 << beacon.http_response_code << " " |
| 75 << beacon.server_ip << " " | 77 << beacon.server_ip << " " |
| 76 << beacon.elapsed.InMilliseconds() << "ms"; | 78 << beacon.elapsed.InMilliseconds() << "ms"; |
|
Ryan Sleevi
2014/04/25 01:00:02
every VLOG costs space in release. Make sure you'r
| |
| 77 | 79 |
| 78 bool reported = false; | 80 bool reported = false; |
| 79 bool evicted = false; | 81 bool evicted = false; |
| 80 if (state->config->DecideIfShouldReportRequest(success)) { | 82 if (state->config->DecideIfShouldReportRequest(success)) { |
| 81 state->beacons.push_back(beacon); | 83 state->beacons.push_back(beacon); |
| 82 ++beacon_count_; | 84 ++beacon_count_; |
| 83 if (beacon_count_ > kMaxQueuedBeacons) { | 85 if (beacon_count_ > kMaxQueuedBeacons) { |
| 84 RemoveOldestBeacon(); | 86 RemoveOldestBeacon(); |
| 85 evicted = true; | 87 evicted = true; |
| 86 } | 88 } |
| 87 scheduler_.OnBeaconAdded(); | 89 scheduler_.OnBeaconAdded(); |
| 88 reported = true; | 90 reported = true; |
| 89 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.ReportedBeaconError", | 91 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.ReportedBeaconError", |
| 90 -beacon.chrome_error); | 92 -beacon.chrome_error); |
| 91 // TODO(ttuttle): Histogram HTTP response code? | 93 // TODO(ttuttle): Histogram HTTP response code? |
| 92 } | 94 } |
| 93 | 95 |
| 94 UMA_HISTOGRAM_BOOLEAN("DomainReliability.BeaconReported", reported); | 96 UMA_HISTOGRAM_BOOLEAN("DomainReliability.BeaconReported", reported); |
| 95 UMA_HISTOGRAM_BOOLEAN("DomainReliability.AddBeaconDidEvict", evicted); | 97 UMA_HISTOGRAM_BOOLEAN("DomainReliability.AddBeaconDidEvict", evicted); |
| 96 } | 98 } |
| 97 | 99 |
| 98 void DomainReliabilityContext::GetQueuedDataForTesting( | 100 void DomainReliabilityContext::GetQueuedDataForTesting( |
| 99 int resource_index, | 101 int resource_index, |
| 100 std::vector<DomainReliabilityBeacon>* beacons_out, | 102 std::vector<DomainReliabilityBeacon>* beacons_out, |
| 101 int* successful_requests_out, | 103 int* successful_requests_out, |
| 102 int* failed_requests_out) const { | 104 int* failed_requests_out) const { |
| 103 DCHECK_LE(0, resource_index); | 105 DCHECK_LE(0, resource_index); |
| 104 DCHECK_GT(static_cast<int>(states_.size()), resource_index); | 106 DCHECK_GT(static_cast<int>(states_.size()), resource_index); |
|
Ryan Sleevi
2014/04/25 01:00:02
-_-
static_cast<int> of a size_t? Aww hell naw...
Deprecated (see juliatuttle)
2014/04/28 22:01:13
Done.
| |
| 105 const ResourceState& state = *states_[resource_index]; | 107 const ResourceState& state = *states_[resource_index]; |
| 106 if (beacons_out) { | 108 if (beacons_out) { |
| 107 beacons_out->resize(state.beacons.size()); | 109 beacons_out->resize(state.beacons.size()); |
| 108 std::copy(state.beacons.begin(), state.beacons.end(), beacons_out->begin()); | 110 std::copy(state.beacons.begin(), state.beacons.end(), beacons_out->begin()); |
|
Ryan Sleevi
2014/04/25 01:00:02
beacons_out->assign(state.becons.begin(), state.be
Deprecated (see juliatuttle)
2014/04/28 22:01:13
Done.
| |
| 109 } | 111 } |
| 110 if (successful_requests_out) | 112 if (successful_requests_out) |
| 111 *successful_requests_out = state.successful_requests; | 113 *successful_requests_out = state.successful_requests; |
| 112 if (failed_requests_out) | 114 if (failed_requests_out) |
| 113 *failed_requests_out = state.failed_requests; | 115 *failed_requests_out = state.failed_requests; |
| 114 } | 116 } |
| 115 | 117 |
| 116 DomainReliabilityContext::ResourceState::ResourceState( | 118 DomainReliabilityContext::ResourceState::ResourceState( |
| 117 DomainReliabilityContext* context, | 119 DomainReliabilityContext* context, |
| 118 const DomainReliabilityConfig::Resource* config) | 120 const DomainReliabilityConfig::Resource* config) |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 | 274 |
| 273 min_resource->RemoveOldestBeacon(); | 275 min_resource->RemoveOldestBeacon(); |
| 274 --beacon_count_; | 276 --beacon_count_; |
| 275 // If that just removed a beacon counted in uploading_beacon_count_, decrement | 277 // If that just removed a beacon counted in uploading_beacon_count_, decrement |
| 276 // that. | 278 // that. |
| 277 if (uploading_beacon_count_ > 0) | 279 if (uploading_beacon_count_ > 0) |
| 278 --uploading_beacon_count_; | 280 --uploading_beacon_count_; |
| 279 } | 281 } |
| 280 | 282 |
| 281 } // namespace domain_reliability | 283 } // namespace domain_reliability |
| OLD | NEW |