blob: a36d189678af673cdfa082c054e7063fdbf0874d [file] [log] [blame]
holte68395852017-01-10 20:40:211// 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
holte85bbd9d2017-02-14 20:55:589#include "base/feature_list.h"
10#include "base/metrics/field_trial_params.h"
holte68395852017-01-10 20:40:2111#include "base/metrics/histogram_macros.h"
holte85bbd9d2017-02-14 20:55:5812#include "base/strings/string_number_conversions.h"
holte68395852017-01-10 20:40:2113#include "build/build_config.h"
14#include "components/metrics/metrics_scheduler.h"
15
16namespace metrics {
17
holte85bbd9d2017-02-14 20:55:5818namespace {
holte68395852017-01-10 20:40:2119
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.
Robert Kaplow12e51882018-08-13 23:06:2424const double kBackoffMultiplier = 2;
holte68395852017-01-10 20:40:2125
Robert Kaplow12e51882018-08-13 23:06:2426// The maximum backoff interval in hours.
27const int kMaxBackoffIntervalHours = 24;
holte68395852017-01-10 20:40:2128
29// Minutes to wait if we are unable to upload due to data usage cap.
30const 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.
34base::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 =
Robert Kaplow12e51882018-08-13 23:06:2440 base::TimeDelta::FromHours(kMaxBackoffIntervalHours);
holte68395852017-01-10 20:40:2141 if (interval > max_interval || interval.InSeconds() < 0) {
42 interval = max_interval;
43 }
44 return interval;
45}
46
holte85bbd9d2017-02-14 20:55:5847// 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.
50base::TimeDelta GetUnsentLogsInterval() {
Steven Holtea2c02632018-01-30 21:24:0451 return base::TimeDelta::FromSeconds(3);
holte85bbd9d2017-02-14 20:55:5852}
53
Robert Kaplow12e51882018-08-13 23:06:2454// Initial time delay after a log uploaded fails before retrying it.
holte85bbd9d2017-02-14 20:55:5855base::TimeDelta GetInitialBackoffInterval() {
Robert Kaplow12e51882018-08-13 23:06:2456 return base::TimeDelta::FromMinutes(5);
holte85bbd9d2017-02-14 20:55:5857}
58
holte68395852017-01-10 20:40:2159} // namespace
60
61MetricsUploadScheduler::MetricsUploadScheduler(
Nate Fischere8236432019-10-25 02:36:3262 const base::Closure& upload_callback,
63 bool fast_startup_for_testing)
64 : MetricsScheduler(upload_callback, fast_startup_for_testing),
holte85bbd9d2017-02-14 20:55:5865 unsent_logs_interval_(GetUnsentLogsInterval()),
66 initial_backoff_interval_(GetInitialBackoffInterval()),
67 backoff_interval_(initial_backoff_interval_) {}
holte68395852017-01-10 20:40:2168
69MetricsUploadScheduler::~MetricsUploadScheduler() {}
70
71void MetricsUploadScheduler::UploadFinished(bool server_is_healthy) {
72 // If the server is having issues, back off. Otherwise, reset to default
73 // (unless there are more logs to send, in which case the next upload should
74 // happen sooner).
75 if (!server_is_healthy) {
holte85bbd9d2017-02-14 20:55:5876 TaskDone(backoff_interval_);
77 backoff_interval_ = BackOffUploadInterval(backoff_interval_);
holte68395852017-01-10 20:40:2178 } else {
holte85bbd9d2017-02-14 20:55:5879 backoff_interval_ = initial_backoff_interval_;
80 TaskDone(unsent_logs_interval_);
holte68395852017-01-10 20:40:2181 }
holte68395852017-01-10 20:40:2182}
83
holte85bbd9d2017-02-14 20:55:5884void MetricsUploadScheduler::StopAndUploadCancelled() {
85 Stop();
86 TaskDone(unsent_logs_interval_);
holte68395852017-01-10 20:40:2187}
88
89void MetricsUploadScheduler::UploadOverDataUsageCap() {
90 TaskDone(base::TimeDelta::FromMinutes(kOverDataUsageIntervalMinutes));
91}
92
holte68395852017-01-10 20:40:2193} // namespace metrics