[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 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 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
dcheng | 04a35cd | 2016-04-22 15:07:24 | [diff] [blame] | 10 | #include <memory> |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
| 13 | #include "base/callback.h" |
| 14 | #include "base/time/time.h" |
| 15 | #include "components/domain_reliability/domain_reliability_export.h" |
ttuttle | 280a73d | 2014-11-13 21:38:04 | [diff] [blame] | 16 | #include "components/domain_reliability/uploader.h" |
raymes | 390c049 | 2014-11-10 03:09:00 | [diff] [blame] | 17 | #include "net/base/backoff_entry.h" |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 18 | |
[email protected] | a6093f41 | 2014-07-10 00:57:09 | [diff] [blame] | 19 | namespace base { |
| 20 | class Value; |
| 21 | } // namespace base |
| 22 | |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 23 | namespace domain_reliability { |
| 24 | |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 25 | class 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. |
| 40 | class 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] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 60 | // 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] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 63 | void OnBeaconAdded(); |
| 64 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 65 | // 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] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 70 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 71 | // Updates the scheduler state based on the result of an upload. Must be |
ttuttle | 280a73d | 2014-11-13 21:38:04 | [diff] [blame] | 72 | // 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] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 75 | |
dcheng | 04a35cd | 2016-04-22 15:07:24 | [diff] [blame] | 76 | std::unique_ptr<base::Value> GetWebUIData() const; |
[email protected] | a6093f41 | 2014-07-10 00:57:09 | [diff] [blame] | 77 | |
raymes | 390c049 | 2014-11-10 03:09:00 | [diff] [blame] | 78 | // Disables jitter in BackoffEntries to make scheduling deterministic for |
| 79 | // unit tests. |
| 80 | void MakeDeterministicForTesting(); |
| 81 | |
ttuttle | 17d1302 | 2015-02-11 22:17:35 | [diff] [blame] | 82 | // 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] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 86 | private: |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 87 | void MaybeScheduleUpload(); |
| 88 | |
| 89 | void GetNextUploadTimeAndCollector(base::TimeTicks now, |
| 90 | base::TimeTicks* upload_time_out, |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 91 | size_t* collector_index_out); |
| 92 | |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 93 | MockableTime* time_; |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 94 | Params params_; |
| 95 | ScheduleUploadCallback callback_; |
raymes | 390c049 | 2014-11-10 03:09:00 | [diff] [blame] | 96 | net::BackoffEntry::Policy backoff_policy_; |
ke.he | b08ece2 | 2017-02-08 02:38:35 | [diff] [blame] | 97 | std::vector<std::unique_ptr<net::BackoffEntry>> collectors_; |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 98 | |
| 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] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 114 | size_t collector_index_; |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 115 | |
| 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] | a6093f41 | 2014-07-10 00:57:09 | [diff] [blame] | 122 | |
| 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.he | b08ece2 | 2017-02-08 02:38:35 | [diff] [blame] | 132 | |
| 133 | DISALLOW_COPY_AND_ASSIGN(DomainReliabilityScheduler); |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace domain_reliability |
| 137 | |
| 138 | #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_ |