Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / domain_reliability / scheduler.h
blobaac257ed6e8d7466b0d22269767828fc93e0dd33
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 base::Value* GetWebUIData() const;
77 // Disables jitter in BackoffEntries to make scheduling deterministic for
78 // unit tests.
79 void MakeDeterministicForTesting();
81 private:
82 void MaybeScheduleUpload();
84 void GetNextUploadTimeAndCollector(base::TimeTicks now,
85 base::TimeTicks* upload_time_out,
86 size_t* collector_index_out);
88 MockableTime* time_;
89 Params params_;
90 ScheduleUploadCallback callback_;
91 net::BackoffEntry::Policy backoff_policy_;
92 ScopedVector<net::BackoffEntry> collectors_;
94 // Whether there are beacons that have not yet been uploaded. Set when a
95 // beacon arrives or an upload fails, and cleared when an upload starts.
96 bool upload_pending_;
98 // Whether the scheduler has called the ScheduleUploadCallback to schedule
99 // the next upload. Set when an upload is scheduled and cleared when the
100 // upload starts.
101 bool upload_scheduled_;
103 // Whether the last scheduled upload is in progress. Set when the upload
104 // starts and cleared when the upload completes (successfully or not).
105 bool upload_running_;
107 // Index of the collector selected for the next upload. (Set in
108 // |OnUploadStart| and cleared in |OnUploadComplete|.)
109 size_t collector_index_;
111 // Time of the first beacon that was not included in the last successful
112 // upload.
113 base::TimeTicks first_beacon_time_;
115 // first_beacon_time_ saved during uploads. Restored if upload fails.
116 base::TimeTicks old_first_beacon_time_;
118 // Extra bits to return in GetWebUIData.
119 base::TimeTicks scheduled_min_time_;
120 base::TimeTicks scheduled_max_time_;
121 // Whether the other last_upload_* fields are populated.
122 bool last_upload_finished_;
123 base::TimeTicks last_upload_start_time_;
124 base::TimeTicks last_upload_end_time_;
125 size_t last_upload_collector_index_;
126 bool last_upload_success_;
129 } // namespace domain_reliability
131 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_