blob: 97f598fa1a4aea5999e1c3a48e5741e2f1668ff2 [file] [log] [blame]
[email protected]bda8e362014-03-24 18:21:031// 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
avibc5337b2015-12-25 23:16:338#include <stddef.h>
9
dcheng04a35cd2016-04-22 15:07:2410#include <memory>
[email protected]bda8e362014-03-24 18:21:0311#include <vector>
12
13#include "base/callback.h"
raymes390c0492014-11-10 03:09:0014#include "base/memory/scoped_vector.h"
[email protected]bda8e362014-03-24 18:21:0315#include "base/time/time.h"
16#include "components/domain_reliability/domain_reliability_export.h"
ttuttle280a73d2014-11-13 21:38:0417#include "components/domain_reliability/uploader.h"
raymes390c0492014-11-10 03:09:0018#include "net/base/backoff_entry.h"
[email protected]bda8e362014-03-24 18:21:0319
[email protected]a6093f412014-07-10 00:57:0920namespace base {
21class Value;
22} // namespace base
23
[email protected]bda8e362014-03-24 18:21:0324namespace domain_reliability {
25
[email protected]bda8e362014-03-24 18:21:0326class MockableTime;
27
28// Determines when an upload should be scheduled. A domain's config will
29// specify minimum and maximum upload delays; the minimum upload delay ensures
30// that Chrome will not send too many upload requests to a site by waiting at
31// least that long after the first beacon, while the maximum upload delay makes
32// sure the server receives the reports while they are still fresh.
33//
34// When everything is working fine, the scheduler will return precisely that
35// interval. If all uploaders have failed, then the beginning or ending points
36// of the interval may be pushed later to accomodate the retry with exponential
37// backoff.
38//
39// See dispatcher.h for an explanation of what happens with the scheduled
40// interval.
41class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
42 public:
43 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
44 ScheduleUploadCallback;
45
46 struct Params {
47 public:
48 base::TimeDelta minimum_upload_delay;
49 base::TimeDelta maximum_upload_delay;
50 base::TimeDelta upload_retry_interval;
51
52 static Params GetFromFieldTrialsOrDefaults();
53 };
54
55 DomainReliabilityScheduler(MockableTime* time,
56 size_t num_collectors,
57 const Params& params,
58 const ScheduleUploadCallback& callback);
59 ~DomainReliabilityScheduler();
60
[email protected]84d2a492014-05-09 22:18:5061 // If there is no upload pending, schedules an upload based on the provided
62 // parameters (some time between the minimum and maximum delay from now).
63 // May call the ScheduleUploadCallback.
[email protected]bda8e362014-03-24 18:21:0364 void OnBeaconAdded();
65
[email protected]84d2a492014-05-09 22:18:5066 // Returns which collector to use for an upload that is about to start. Must
67 // be called exactly once during or after the ScheduleUploadCallback but
68 // before OnUploadComplete is called. (Also records the upload start time for
69 // future retries, if the upload ends up failing.)
70 size_t OnUploadStart();
[email protected]bda8e362014-03-24 18:21:0371
[email protected]84d2a492014-05-09 22:18:5072 // Updates the scheduler state based on the result of an upload. Must be
ttuttle280a73d2014-11-13 21:38:0473 // called exactly once after |OnUploadStart|. |result| should be the result
74 // passed to the upload callback by the Uploader.
75 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
[email protected]bda8e362014-03-24 18:21:0376
dcheng04a35cd2016-04-22 15:07:2477 std::unique_ptr<base::Value> GetWebUIData() const;
[email protected]a6093f412014-07-10 00:57:0978
raymes390c0492014-11-10 03:09:0079 // Disables jitter in BackoffEntries to make scheduling deterministic for
80 // unit tests.
81 void MakeDeterministicForTesting();
82
ttuttle17d13022015-02-11 22:17:3583 // Gets the time of the first beacon that has not yet been successfully
84 // uploaded.
85 base::TimeTicks first_beacon_time() const { return first_beacon_time_; }
86
ttuttle4f147a62015-02-13 19:53:5287 // Gets the time until the next upload attempt on the last collector used.
88 // This will be 0 if the upload was a success; it does not take into account
89 // minimum_upload_delay and maximum_upload_delay.
90 base::TimeDelta last_collector_retry_delay() const {
91 return last_collector_retry_delay_;
92 }
93
[email protected]bda8e362014-03-24 18:21:0394 private:
[email protected]bda8e362014-03-24 18:21:0395 void MaybeScheduleUpload();
96
97 void GetNextUploadTimeAndCollector(base::TimeTicks now,
98 base::TimeTicks* upload_time_out,
[email protected]84d2a492014-05-09 22:18:5099 size_t* collector_index_out);
100
[email protected]bda8e362014-03-24 18:21:03101 MockableTime* time_;
[email protected]bda8e362014-03-24 18:21:03102 Params params_;
103 ScheduleUploadCallback callback_;
raymes390c0492014-11-10 03:09:00104 net::BackoffEntry::Policy backoff_policy_;
105 ScopedVector<net::BackoffEntry> collectors_;
[email protected]bda8e362014-03-24 18:21:03106
107 // Whether there are beacons that have not yet been uploaded. Set when a
108 // beacon arrives or an upload fails, and cleared when an upload starts.
109 bool upload_pending_;
110
111 // Whether the scheduler has called the ScheduleUploadCallback to schedule
112 // the next upload. Set when an upload is scheduled and cleared when the
113 // upload starts.
114 bool upload_scheduled_;
115
116 // Whether the last scheduled upload is in progress. Set when the upload
117 // starts and cleared when the upload completes (successfully or not).
118 bool upload_running_;
119
120 // Index of the collector selected for the next upload. (Set in
121 // |OnUploadStart| and cleared in |OnUploadComplete|.)
[email protected]84d2a492014-05-09 22:18:50122 size_t collector_index_;
[email protected]bda8e362014-03-24 18:21:03123
124 // Time of the first beacon that was not included in the last successful
125 // upload.
126 base::TimeTicks first_beacon_time_;
127
128 // first_beacon_time_ saved during uploads. Restored if upload fails.
129 base::TimeTicks old_first_beacon_time_;
[email protected]a6093f412014-07-10 00:57:09130
ttuttle4f147a62015-02-13 19:53:52131 // Time until the next upload attempt on the last collector used. (Saved for
132 // histograms in Context.)
133 base::TimeDelta last_collector_retry_delay_;
134
[email protected]a6093f412014-07-10 00:57:09135 // Extra bits to return in GetWebUIData.
136 base::TimeTicks scheduled_min_time_;
137 base::TimeTicks scheduled_max_time_;
138 // Whether the other last_upload_* fields are populated.
139 bool last_upload_finished_;
140 base::TimeTicks last_upload_start_time_;
141 base::TimeTicks last_upload_end_time_;
142 size_t last_upload_collector_index_;
143 bool last_upload_success_;
[email protected]bda8e362014-03-24 18:21:03144};
145
146} // namespace domain_reliability
147
148#endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_