holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 1 | // Copyright 2017 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. |
| 4 | |
| 5 | #include "components/metrics/metrics_upload_scheduler.h" |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 9 | #include "base/feature_list.h" |
| 10 | #include "base/metrics/field_trial_params.h" |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 11 | #include "base/metrics/histogram_macros.h" |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 12 | #include "base/strings/string_number_conversions.h" |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 13 | #include "build/build_config.h" |
| 14 | #include "components/metrics/metrics_scheduler.h" |
| 15 | |
| 16 | namespace metrics { |
| 17 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 18 | // This feature moves the upload schedule to a seperate schedule from the |
| 19 | // log rotation schedule. This may change upload timing slightly, but |
| 20 | // would allow some compartmentalization of uploader logic to allow more |
| 21 | // code reuse between different metrics services. |
| 22 | const base::Feature kUploadSchedulerFeature{"UMAUploadScheduler", |
| 23 | base::FEATURE_DISABLED_BY_DEFAULT}; |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 24 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 25 | namespace { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 26 | |
| 27 | // When uploading metrics to the server fails, we progressively wait longer and |
| 28 | // longer before sending the next log. This backoff process helps reduce load |
| 29 | // on a server that is having issues. |
| 30 | // The following is the multiplier we use to expand that inter-log duration. |
| 31 | const double kBackoffMultiplier = 1.1; |
| 32 | |
| 33 | // The maximum backoff interval in minutes. |
| 34 | const int kMaxBackoffIntervalMinutes = 10; |
| 35 | |
| 36 | // Minutes to wait if we are unable to upload due to data usage cap. |
| 37 | const int kOverDataUsageIntervalMinutes = 5; |
| 38 | |
| 39 | // Increases the upload interval each time it's called, to handle the case |
| 40 | // where the server is having issues. |
| 41 | base::TimeDelta BackOffUploadInterval(base::TimeDelta interval) { |
| 42 | DCHECK_GT(kBackoffMultiplier, 1.0); |
| 43 | interval = base::TimeDelta::FromMicroseconds(static_cast<int64_t>( |
| 44 | kBackoffMultiplier * interval.InMicroseconds())); |
| 45 | |
| 46 | base::TimeDelta max_interval = |
| 47 | base::TimeDelta::FromMinutes(kMaxBackoffIntervalMinutes); |
| 48 | if (interval > max_interval || interval.InSeconds() < 0) { |
| 49 | interval = max_interval; |
| 50 | } |
| 51 | return interval; |
| 52 | } |
| 53 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 54 | // Gets a time interval in seconds from a variations parameter. |
| 55 | base::TimeDelta GetTimeParameterSeconds(const std::string& param_name, |
| 56 | int default_seconds) { |
| 57 | int seconds = base::GetFieldTrialParamByFeatureAsInt( |
| 58 | kUploadSchedulerFeature, param_name, default_seconds); |
| 59 | return base::TimeDelta::FromSeconds(seconds); |
| 60 | } |
| 61 | |
| 62 | // Time delay after a log is uploaded successfully before attempting another. |
| 63 | // On mobile, keeping the radio on is very expensive, so prefer to keep this |
| 64 | // short and send in bursts. |
| 65 | base::TimeDelta GetUnsentLogsInterval() { |
| 66 | return GetTimeParameterSeconds("UnsentLogsIntervalSeconds", 3); |
| 67 | } |
| 68 | |
| 69 | // Inital time delay after a log uploaded fails before retrying it. |
| 70 | base::TimeDelta GetInitialBackoffInterval() { |
| 71 | return GetTimeParameterSeconds("InitialBackoffIntervalSeconds", 15); |
| 72 | } |
| 73 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 74 | } // namespace |
| 75 | |
| 76 | MetricsUploadScheduler::MetricsUploadScheduler( |
| 77 | const base::Closure& upload_callback) |
| 78 | : MetricsScheduler(upload_callback), |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 79 | unsent_logs_interval_(GetUnsentLogsInterval()), |
| 80 | initial_backoff_interval_(GetInitialBackoffInterval()), |
| 81 | backoff_interval_(initial_backoff_interval_) {} |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 82 | |
| 83 | MetricsUploadScheduler::~MetricsUploadScheduler() {} |
| 84 | |
| 85 | void MetricsUploadScheduler::UploadFinished(bool server_is_healthy) { |
| 86 | // If the server is having issues, back off. Otherwise, reset to default |
| 87 | // (unless there are more logs to send, in which case the next upload should |
| 88 | // happen sooner). |
| 89 | if (!server_is_healthy) { |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 90 | TaskDone(backoff_interval_); |
| 91 | backoff_interval_ = BackOffUploadInterval(backoff_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 92 | } else { |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 93 | backoff_interval_ = initial_backoff_interval_; |
| 94 | TaskDone(unsent_logs_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 95 | } |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 96 | } |
| 97 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame^] | 98 | void MetricsUploadScheduler::StopAndUploadCancelled() { |
| 99 | Stop(); |
| 100 | TaskDone(unsent_logs_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void MetricsUploadScheduler::UploadOverDataUsageCap() { |
| 104 | TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes)); |
| 105 | } |
| 106 | |
| 107 | void MetricsUploadScheduler::LogActualUploadInterval(base::TimeDelta interval) { |
| 108 | UMA_HISTOGRAM_CUSTOM_COUNTS("UMA.ActualLogUploadInterval", |
| 109 | interval.InMinutes(), 1, |
| 110 | base::TimeDelta::FromHours(12).InMinutes(), 50); |
| 111 | } |
| 112 | |
| 113 | void MetricsUploadScheduler::TriggerTask() { |
| 114 | if (!last_upload_finish_time_.is_null()) { |
| 115 | LogActualUploadInterval(base::TimeTicks::Now() - last_upload_finish_time_); |
| 116 | last_upload_finish_time_ = base::TimeTicks(); |
| 117 | } |
| 118 | MetricsScheduler::TriggerTask(); |
| 119 | } |
| 120 | |
| 121 | } // namespace metrics |