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 | namespace { |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 19 | |
| 20 | // When uploading metrics to the server fails, we progressively wait longer and |
| 21 | // longer before sending the next log. This backoff process helps reduce load |
| 22 | // on a server that is having issues. |
| 23 | // The following is the multiplier we use to expand that inter-log duration. |
| 24 | const double kBackoffMultiplier = 1.1; |
| 25 | |
| 26 | // The maximum backoff interval in minutes. |
| 27 | const int kMaxBackoffIntervalMinutes = 10; |
| 28 | |
| 29 | // Minutes to wait if we are unable to upload due to data usage cap. |
| 30 | const int kOverDataUsageIntervalMinutes = 5; |
| 31 | |
| 32 | // Increases the upload interval each time it's called, to handle the case |
| 33 | // where the server is having issues. |
| 34 | base::TimeDelta BackOffUploadInterval(base::TimeDelta interval) { |
| 35 | DCHECK_GT(kBackoffMultiplier, 1.0); |
| 36 | interval = base::TimeDelta::FromMicroseconds(static_cast<int64_t>( |
| 37 | kBackoffMultiplier * interval.InMicroseconds())); |
| 38 | |
| 39 | base::TimeDelta max_interval = |
| 40 | base::TimeDelta::FromMinutes(kMaxBackoffIntervalMinutes); |
| 41 | if (interval > max_interval || interval.InSeconds() < 0) { |
| 42 | interval = max_interval; |
| 43 | } |
| 44 | return interval; |
| 45 | } |
| 46 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 47 | // Time delay after a log is uploaded successfully before attempting another. |
| 48 | // On mobile, keeping the radio on is very expensive, so prefer to keep this |
| 49 | // short and send in bursts. |
| 50 | base::TimeDelta GetUnsentLogsInterval() { |
Steven Holte | a2c0263 | 2018-01-30 21:24:04 | [diff] [blame] | 51 | return base::TimeDelta::FromSeconds(3); |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // Inital time delay after a log uploaded fails before retrying it. |
| 55 | base::TimeDelta GetInitialBackoffInterval() { |
Steven Holte | a2c0263 | 2018-01-30 21:24:04 | [diff] [blame] | 56 | return base::TimeDelta::FromSeconds(15); |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 57 | } |
| 58 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 59 | } // namespace |
| 60 | |
| 61 | MetricsUploadScheduler::MetricsUploadScheduler( |
| 62 | const base::Closure& upload_callback) |
| 63 | : MetricsScheduler(upload_callback), |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 64 | unsent_logs_interval_(GetUnsentLogsInterval()), |
| 65 | initial_backoff_interval_(GetInitialBackoffInterval()), |
| 66 | backoff_interval_(initial_backoff_interval_) {} |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 67 | |
| 68 | MetricsUploadScheduler::~MetricsUploadScheduler() {} |
| 69 | |
| 70 | void MetricsUploadScheduler::UploadFinished(bool server_is_healthy) { |
| 71 | // If the server is having issues, back off. Otherwise, reset to default |
| 72 | // (unless there are more logs to send, in which case the next upload should |
| 73 | // happen sooner). |
| 74 | if (!server_is_healthy) { |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 75 | TaskDone(backoff_interval_); |
| 76 | backoff_interval_ = BackOffUploadInterval(backoff_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 77 | } else { |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 78 | backoff_interval_ = initial_backoff_interval_; |
| 79 | TaskDone(unsent_logs_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 80 | } |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 81 | } |
| 82 | |
holte | 85bbd9d | 2017-02-14 20:55:58 | [diff] [blame] | 83 | void MetricsUploadScheduler::StopAndUploadCancelled() { |
| 84 | Stop(); |
| 85 | TaskDone(unsent_logs_interval_); |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void MetricsUploadScheduler::UploadOverDataUsageCap() { |
| 89 | TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes)); |
| 90 | } |
| 91 | |
holte | 6839585 | 2017-01-10 20:40:21 | [diff] [blame] | 92 | } // namespace metrics |