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.
4
5#ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
6#define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
7
8#include <vector>
9
10#include "base/callback.h"
11#include "base/time/time.h"
12#include "components/domain_reliability/domain_reliability_export.h"
13
14namespace base {
15class Value;
16}  // namespace base
17
18namespace domain_reliability {
19
20class DomainReliabilityConfig;
21class MockableTime;
22
23// Determines when an upload should be scheduled. A domain's config will
24// specify minimum and maximum upload delays; the minimum upload delay ensures
25// that Chrome will not send too many upload requests to a site by waiting at
26// least that long after the first beacon, while the maximum upload delay makes
27// sure the server receives the reports while they are still fresh.
28//
29// When everything is working fine, the scheduler will return precisely that
30// interval. If all uploaders have failed, then the beginning or ending points
31// of the interval may be pushed later to accomodate the retry with exponential
32// backoff.
33//
34// See dispatcher.h for an explanation of what happens with the scheduled
35// interval.
36class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
37 public:
38  typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
39      ScheduleUploadCallback;
40
41  struct Params {
42   public:
43    base::TimeDelta minimum_upload_delay;
44    base::TimeDelta maximum_upload_delay;
45    base::TimeDelta upload_retry_interval;
46
47    static Params GetFromFieldTrialsOrDefaults();
48  };
49
50  DomainReliabilityScheduler(MockableTime* time,
51                             size_t num_collectors,
52                             const Params& params,
53                             const ScheduleUploadCallback& callback);
54  ~DomainReliabilityScheduler();
55
56  // If there is no upload pending, schedules an upload based on the provided
57  // parameters (some time between the minimum and maximum delay from now).
58  // May call the ScheduleUploadCallback.
59  void OnBeaconAdded();
60
61  // Returns which collector to use for an upload that is about to start. Must
62  // be called exactly once during or after the ScheduleUploadCallback but
63  // before OnUploadComplete is called. (Also records the upload start time for
64  // future retries, if the upload ends up failing.)
65  size_t OnUploadStart();
66
67  // Updates the scheduler state based on the result of an upload. Must be
68  // called exactly once after |OnUploadStart|. |success| should be true if the
69  // upload was successful, and false otherwise.
70  void OnUploadComplete(bool success);
71
72  base::Value* GetWebUIData() const;
73
74 private:
75  struct CollectorState {
76    CollectorState();
77
78    // The number of consecutive failures to upload to this collector, or 0 if
79    // the most recent upload succeeded.
80    unsigned failures;
81    base::TimeTicks next_upload;
82  };
83
84  void MaybeScheduleUpload();
85
86  void GetNextUploadTimeAndCollector(base::TimeTicks now,
87                                     base::TimeTicks* upload_time_out,
88                                     size_t* collector_index_out);
89
90  base::TimeDelta GetUploadRetryInterval(unsigned failures);
91
92  MockableTime* time_;
93  std::vector<CollectorState> collectors_;
94  Params params_;
95  ScheduleUploadCallback callback_;
96
97  // Whether there are beacons that have not yet been uploaded. Set when a
98  // beacon arrives or an upload fails, and cleared when an upload starts.
99  bool upload_pending_;
100
101  // Whether the scheduler has called the ScheduleUploadCallback to schedule
102  // the next upload. Set when an upload is scheduled and cleared when the
103  // upload starts.
104  bool upload_scheduled_;
105
106  // Whether the last scheduled upload is in progress. Set when the upload
107  // starts and cleared when the upload completes (successfully or not).
108  bool upload_running_;
109
110  // Index of the collector selected for the next upload.  (Set in
111  // |OnUploadStart| and cleared in |OnUploadComplete|.)
112  size_t collector_index_;
113
114  // Time of the first beacon that was not included in the last successful
115  // upload.
116  base::TimeTicks first_beacon_time_;
117
118  // first_beacon_time_ saved during uploads.  Restored if upload fails.
119  base::TimeTicks old_first_beacon_time_;
120
121  // Extra bits to return in GetWebUIData.
122  base::TimeTicks scheduled_min_time_;
123  base::TimeTicks scheduled_max_time_;
124  // Whether the other last_upload_* fields are populated.
125  bool last_upload_finished_;
126  base::TimeTicks last_upload_start_time_;
127  base::TimeTicks last_upload_end_time_;
128  size_t last_upload_collector_index_;
129  bool last_upload_success_;
130};
131
132}  // namespace domain_reliability
133
134#endif  // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
135