QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / components / domain_reliability / scheduler.h
blobb08754e82dd70bb708ed687e38072d8c8f7d7646
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.
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/time/time.h"
13 #include "components/domain_reliability/domain_reliability_export.h"
14 #include "components/domain_reliability/uploader.h"
15 #include "net/base/backoff_entry.h"
17 namespace base {
18 class Value;
19 } // namespace base
21 namespace domain_reliability {
23 class DomainReliabilityConfig;
24 class MockableTime;
26 // Determines when an upload should be scheduled. A domain's config will
27 // specify minimum and maximum upload delays; the minimum upload delay ensures
28 // that Chrome will not send too many upload requests to a site by waiting at
29 // least that long after the first beacon, while the maximum upload delay makes
30 // sure the server receives the reports while they are still fresh.
32 // When everything is working fine, the scheduler will return precisely that
33 // interval. If all uploaders have failed, then the beginning or ending points
34 // of the interval may be pushed later to accomodate the retry with exponential
35 // backoff.
37 // See dispatcher.h for an explanation of what happens with the scheduled
38 // interval.
39 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
40 public:
41 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
42 ScheduleUploadCallback;
44 struct Params {
45 public:
46 base::TimeDelta minimum_upload_delay;
47 base::TimeDelta maximum_upload_delay;
48 base::TimeDelta upload_retry_interval;
50 static Params GetFromFieldTrialsOrDefaults();
53 DomainReliabilityScheduler(MockableTime* time,
54 size_t num_collectors,
55 const Params& params,
56 const ScheduleUploadCallback& callback);
57 ~DomainReliabilityScheduler();
59 // If there is no upload pending, schedules an upload based on the provided
60 // parameters (some time between the minimum and maximum delay from now).
61 // May call the ScheduleUploadCallback.
62 void OnBeaconAdded();
64 // Returns which collector to use for an upload that is about to start. Must
65 // be called exactly once during or after the ScheduleUploadCallback but
66 // before OnUploadComplete is called. (Also records the upload start time for
67 // future retries, if the upload ends up failing.)
68 size_t OnUploadStart();
70 // Updates the scheduler state based on the result of an upload. Must be
71 // called exactly once after |OnUploadStart|. |result| should be the result
72 // passed to the upload callback by the Uploader.
73 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
75 scoped_ptr<base::Value> GetWebUIData() const;
77 // Disables jitter in BackoffEntries to make scheduling deterministic for
78 // unit tests.
79 void MakeDeterministicForTesting();
81 // Gets the time of the first beacon that has not yet been successfully
82 // uploaded.
83 base::TimeTicks first_beacon_time() const { return first_beacon_time_; }
85 // Gets the time until the next upload attempt on the last collector used.
86 // This will be 0 if the upload was a success; it does not take into account
87 // minimum_upload_delay and maximum_upload_delay.
88 base::TimeDelta last_collector_retry_delay() const {
89 return last_collector_retry_delay_;
92 private:
93 void MaybeScheduleUpload();
95 void GetNextUploadTimeAndCollector(base::TimeTicks now,
96 base::TimeTicks* upload_time_out,
97 size_t* collector_index_out);
99 MockableTime* time_;
100 Params params_;
101 ScheduleUploadCallback callback_;
102 net::BackoffEntry::Policy backoff_policy_;
103 ScopedVector<net::BackoffEntry> collectors_;
105 // Whether there are beacons that have not yet been uploaded. Set when a
106 // beacon arrives or an upload fails, and cleared when an upload starts.
107 bool upload_pending_;
109 // Whether the scheduler has called the ScheduleUploadCallback to schedule
110 // the next upload. Set when an upload is scheduled and cleared when the
111 // upload starts.
112 bool upload_scheduled_;
114 // Whether the last scheduled upload is in progress. Set when the upload
115 // starts and cleared when the upload completes (successfully or not).
116 bool upload_running_;
118 // Index of the collector selected for the next upload. (Set in
119 // |OnUploadStart| and cleared in |OnUploadComplete|.)
120 size_t collector_index_;
122 // Time of the first beacon that was not included in the last successful
123 // upload.
124 base::TimeTicks first_beacon_time_;
126 // first_beacon_time_ saved during uploads. Restored if upload fails.
127 base::TimeTicks old_first_beacon_time_;
129 // Time until the next upload attempt on the last collector used. (Saved for
130 // histograms in Context.)
131 base::TimeDelta last_collector_retry_delay_;
133 // Extra bits to return in GetWebUIData.
134 base::TimeTicks scheduled_min_time_;
135 base::TimeTicks scheduled_max_time_;
136 // Whether the other last_upload_* fields are populated.
137 bool last_upload_finished_;
138 base::TimeTicks last_upload_start_time_;
139 base::TimeTicks last_upload_end_time_;
140 size_t last_upload_collector_index_;
141 bool last_upload_success_;
144 } // namespace domain_reliability
146 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_