blob: 7c26960d98415d1d1b644d6fea00cbbf879d9644 [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"
14#include "base/time/time.h"
15#include "components/domain_reliability/domain_reliability_export.h"
ttuttle280a73d2014-11-13 21:38:0416#include "components/domain_reliability/uploader.h"
raymes390c0492014-11-10 03:09:0017#include "net/base/backoff_entry.h"
[email protected]bda8e362014-03-24 18:21:0318
[email protected]a6093f412014-07-10 00:57:0919namespace base {
20class Value;
21} // namespace base
22
[email protected]bda8e362014-03-24 18:21:0323namespace domain_reliability {
24
[email protected]bda8e362014-03-24 18:21:0325class MockableTime;
26
27// Determines when an upload should be scheduled. A domain's config will
28// specify minimum and maximum upload delays; the minimum upload delay ensures
29// that Chrome will not send too many upload requests to a site by waiting at
30// least that long after the first beacon, while the maximum upload delay makes
31// sure the server receives the reports while they are still fresh.
32//
33// When everything is working fine, the scheduler will return precisely that
34// interval. If all uploaders have failed, then the beginning or ending points
35// of the interval may be pushed later to accomodate the retry with exponential
36// backoff.
37//
38// See dispatcher.h for an explanation of what happens with the scheduled
39// interval.
40class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
41 public:
42 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
43 ScheduleUploadCallback;
44
45 struct Params {
46 public:
47 base::TimeDelta minimum_upload_delay;
48 base::TimeDelta maximum_upload_delay;
49 base::TimeDelta upload_retry_interval;
50
51 static Params GetFromFieldTrialsOrDefaults();
52 };
53
54 DomainReliabilityScheduler(MockableTime* time,
55 size_t num_collectors,
56 const Params& params,
57 const ScheduleUploadCallback& callback);
58 ~DomainReliabilityScheduler();
59
[email protected]84d2a492014-05-09 22:18:5060 // If there is no upload pending, schedules an upload based on the provided
61 // parameters (some time between the minimum and maximum delay from now).
62 // May call the ScheduleUploadCallback.
[email protected]bda8e362014-03-24 18:21:0363 void OnBeaconAdded();
64
[email protected]84d2a492014-05-09 22:18:5065 // Returns which collector to use for an upload that is about to start. Must
66 // be called exactly once during or after the ScheduleUploadCallback but
67 // before OnUploadComplete is called. (Also records the upload start time for
68 // future retries, if the upload ends up failing.)
69 size_t OnUploadStart();
[email protected]bda8e362014-03-24 18:21:0370
[email protected]84d2a492014-05-09 22:18:5071 // Updates the scheduler state based on the result of an upload. Must be
ttuttle280a73d2014-11-13 21:38:0472 // called exactly once after |OnUploadStart|. |result| should be the result
73 // passed to the upload callback by the Uploader.
74 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
[email protected]bda8e362014-03-24 18:21:0375
dcheng04a35cd2016-04-22 15:07:2476 std::unique_ptr<base::Value> GetWebUIData() const;
[email protected]a6093f412014-07-10 00:57:0977
raymes390c0492014-11-10 03:09:0078 // Disables jitter in BackoffEntries to make scheduling deterministic for
79 // unit tests.
80 void MakeDeterministicForTesting();
81
ttuttle17d13022015-02-11 22:17:3582 // Gets the time of the first beacon that has not yet been successfully
83 // uploaded.
84 base::TimeTicks first_beacon_time() const { return first_beacon_time_; }
85
[email protected]bda8e362014-03-24 18:21:0386 private:
[email protected]bda8e362014-03-24 18:21:0387 void MaybeScheduleUpload();
88
89 void GetNextUploadTimeAndCollector(base::TimeTicks now,
90 base::TimeTicks* upload_time_out,
[email protected]84d2a492014-05-09 22:18:5091 size_t* collector_index_out);
92
[email protected]bda8e362014-03-24 18:21:0393 MockableTime* time_;
[email protected]bda8e362014-03-24 18:21:0394 Params params_;
95 ScheduleUploadCallback callback_;
raymes390c0492014-11-10 03:09:0096 net::BackoffEntry::Policy backoff_policy_;
ke.heb08ece22017-02-08 02:38:3597 std::vector<std::unique_ptr<net::BackoffEntry>> collectors_;
[email protected]bda8e362014-03-24 18:21:0398
99 // Whether there are beacons that have not yet been uploaded. Set when a
100 // beacon arrives or an upload fails, and cleared when an upload starts.
101 bool upload_pending_;
102
103 // Whether the scheduler has called the ScheduleUploadCallback to schedule
104 // the next upload. Set when an upload is scheduled and cleared when the
105 // upload starts.
106 bool upload_scheduled_;
107
108 // Whether the last scheduled upload is in progress. Set when the upload
109 // starts and cleared when the upload completes (successfully or not).
110 bool upload_running_;
111
112 // Index of the collector selected for the next upload. (Set in
113 // |OnUploadStart| and cleared in |OnUploadComplete|.)
[email protected]84d2a492014-05-09 22:18:50114 size_t collector_index_;
[email protected]bda8e362014-03-24 18:21:03115
116 // Time of the first beacon that was not included in the last successful
117 // upload.
118 base::TimeTicks first_beacon_time_;
119
120 // first_beacon_time_ saved during uploads. Restored if upload fails.
121 base::TimeTicks old_first_beacon_time_;
[email protected]a6093f412014-07-10 00:57:09122
123 // Extra bits to return in GetWebUIData.
124 base::TimeTicks scheduled_min_time_;
125 base::TimeTicks scheduled_max_time_;
126 // Whether the other last_upload_* fields are populated.
127 bool last_upload_finished_;
128 base::TimeTicks last_upload_start_time_;
129 base::TimeTicks last_upload_end_time_;
130 size_t last_upload_collector_index_;
131 bool last_upload_success_;
ke.heb08ece22017-02-08 02:38:35132
133 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityScheduler);
[email protected]bda8e362014-03-24 18:21:03134};
135
136} // namespace domain_reliability
137
138#endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_