blob: f8a2d0c5ebbae046c5e3bbb590905433e6e84031 [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
7
Jay Srinivasan08262882012-12-28 19:29:43 -08008#include <base/time.h>
9
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080010#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080011#include "update_engine/prefs_interface.h"
12
13namespace chromeos_update_engine {
14
Jay Srinivasan19409b72013-04-12 19:23:36 -070015class SystemState;
16
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080017// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080018// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019// state, etc. All state is persisted so that we use the most recently saved
20// value when resuming the update_engine process. All state is also cached in
21// memory so that we ensure we always make progress based on last known good
22// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080023class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070025 PayloadState();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080026 virtual ~PayloadState() {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027
Jay Srinivasan19409b72013-04-12 19:23:36 -070028 // Initializes a payload state object using the given global system state.
29 // It performs the initial loading of all persisted state into memory and
30 // dumps the initial state for debugging purposes. Note: the other methods
31 // should be called only after calling Initialize on this object.
32 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080033
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080034 // Implementation of PayloadStateInterface methods.
35 virtual void SetResponse(const OmahaResponse& response);
36 virtual void DownloadComplete();
37 virtual void DownloadProgress(size_t count);
Chris Sosabe45bef2013-04-09 18:25:12 -070038 virtual void UpdateResumed();
Jay Srinivasan19409b72013-04-12 19:23:36 -070039 virtual void UpdateRestarted();
David Zeuthen9a017f22013-04-11 16:10:26 -070040 virtual void UpdateSucceeded();
David Zeuthena99981f2013-04-29 13:42:47 -070041 virtual void UpdateFailed(ErrorCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080042 virtual bool ShouldBackoffDownload();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080043
Jay Srinivasan08262882012-12-28 19:29:43 -080044 virtual inline std::string GetResponseSignature() {
45 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046 }
47
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080048 virtual inline uint32_t GetPayloadAttemptNumber() {
49 return payload_attempt_number_;
50 }
51
Jay Srinivasan53173b92013-05-17 17:13:01 -070052 virtual inline std::string GetCurrentUrl() {
53 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080054 }
55
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080056 virtual inline uint32_t GetUrlFailureCount() {
57 return url_failure_count_;
58 }
59
David Zeuthencc6f9962013-04-18 11:57:24 -070060 virtual inline uint32_t GetUrlSwitchCount() {
61 return url_switch_count_;
62 }
63
David Zeuthena573d6f2013-06-14 16:13:36 -070064 virtual inline int GetNumResponsesSeen() {
65 return num_responses_seen_;
66 }
67
Jay Srinivasan08262882012-12-28 19:29:43 -080068 virtual inline base::Time GetBackoffExpiryTime() {
69 return backoff_expiry_time_;
70 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080071
David Zeuthen9a017f22013-04-11 16:10:26 -070072 virtual base::TimeDelta GetUpdateDuration();
73
74 virtual base::TimeDelta GetUpdateDurationUptime();
75
Jay Srinivasan19409b72013-04-12 19:23:36 -070076 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
77 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
78 }
79
80 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
81 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
82 }
83
Chris Sosabe45bef2013-04-09 18:25:12 -070084 virtual inline uint32_t GetNumReboots() {
85 return num_reboots_;
86 }
87
Jay Srinivasan08262882012-12-28 19:29:43 -080088 private:
89 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080090 // at the time of the next update check.
91 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080093 // Advances the current URL index to the next available one. If all URLs have
94 // been exhausted during the current payload download attempt (as indicated
95 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -070096 // number and wrap around again with the first URL in the list. This also
97 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080098 void IncrementUrlIndex();
99
100 // Increments the failure count of the current URL. If the configured max
101 // failure count is reached for this URL, it advances the current URL index
102 // to the next URL and resets the failure count for that URL.
103 void IncrementFailureCount();
104
Jay Srinivasan08262882012-12-28 19:29:43 -0800105 // Updates the backoff expiry time exponentially based on the current
106 // payload attempt number.
107 void UpdateBackoffExpiryTime();
108
Jay Srinivasan19409b72013-04-12 19:23:36 -0700109 // Updates the value of current download source based on the current URL
110 // index. If the download source is not one of the known sources, it's set
111 // to kNumDownloadSources.
112 void UpdateCurrentDownloadSource();
113
114 // Updates the various metrics corresponding with the given number of bytes
115 // that were downloaded recently.
116 void UpdateBytesDownloaded(size_t count);
117
118 // Reports the various metrics related to the number of bytes downloaded.
119 void ReportBytesDownloadedMetrics();
120
David Zeuthencc6f9962013-04-18 11:57:24 -0700121 // Reports the metric related to number of URL switches.
122 void ReportUpdateUrlSwitchesMetric();
123
Chris Sosabe45bef2013-04-09 18:25:12 -0700124 // Reports the various metrics related to rebooting during an update.
125 void ReportRebootMetrics();
126
David Zeuthen674c3182013-04-18 14:05:20 -0700127 // Reports the various metrics related to update duration.
128 void ReportDurationMetrics();
129
Jay Srinivasan08262882012-12-28 19:29:43 -0800130 // Resets all the persisted state values which are maintained relative to the
131 // current response signature. The response signature itself is not reset.
132 void ResetPersistedState();
133
Jay Srinivasan19409b72013-04-12 19:23:36 -0700134 // Resets the appropriate state related to download sources that need to be
135 // reset on a new update.
136 void ResetDownloadSourcesOnNewUpdate();
137
138 // Returns the persisted value for the given key. It also validates that
139 // the value returned is non-negative.
140 int64_t GetPersistedValue(const std::string& key);
141
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 // Calculates the response "signature", which is basically a string composed
143 // of the subset of the fields in the current response that affect the
144 // behavior of the PayloadState.
145 std::string CalculateResponseSignature();
146
147 // Initializes the current response signature from the persisted state.
148 void LoadResponseSignature();
149
150 // Sets the response signature to the given value. Also persists the value
151 // being set so that we resume from the save value in case of a process
152 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700153 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800154
155 // Initializes the payload attempt number from the persisted state.
156 void LoadPayloadAttemptNumber();
157
158 // Sets the payload attempt number to the given value. Also persists the
159 // value being set so that we resume from the same value in case of a process
160 // restart.
161 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
162
163 // Initializes the current URL index from the persisted state.
164 void LoadUrlIndex();
165
166 // Sets the current URL index to the given value. Also persists the value
167 // being set so that we resume from the same value in case of a process
168 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800169 void SetUrlIndex(uint32_t url_index);
170
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800171 // Initializes the current URL's failure count from the persisted stae.
172 void LoadUrlFailureCount();
173
174 // Sets the current URL's failure count to the given value. Also persists the
175 // value being set so that we resume from the same value in case of a process
176 // restart.
177 void SetUrlFailureCount(uint32_t url_failure_count);
178
David Zeuthencc6f9962013-04-18 11:57:24 -0700179 // Sets |url_switch_count_| to the given value and persists the value.
180 void SetUrlSwitchCount(uint32_t url_switch_count);
181
182 // Initializes |url_switch_count_| from the persisted stae.
183 void LoadUrlSwitchCount();
184
Jay Srinivasan08262882012-12-28 19:29:43 -0800185 // Initializes the backoff expiry time from the persisted state.
186 void LoadBackoffExpiryTime();
187
188 // Sets the backoff expiry time to the given value. Also persists the value
189 // being set so that we resume from the same value in case of a process
190 // restart.
191 void SetBackoffExpiryTime(const base::Time& new_time);
192
David Zeuthen9a017f22013-04-11 16:10:26 -0700193 // Initializes |update_timestamp_start_| from the persisted state.
194 void LoadUpdateTimestampStart();
195
196 // Sets |update_timestamp_start_| to the given value and persists the value.
197 void SetUpdateTimestampStart(const base::Time& value);
198
199 // Sets |update_timestamp_end_| to the given value. This is not persisted
200 // as it happens at the end of the update process where state is deleted
201 // anyway.
202 void SetUpdateTimestampEnd(const base::Time& value);
203
204 // Initializes |update_duration_uptime_| from the persisted state.
205 void LoadUpdateDurationUptime();
206
207 // Helper method used in SetUpdateDurationUptime() and
208 // CalculateUpdateDurationUptime().
209 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
210 const base::Time& timestamp,
211 bool use_logging);
212
213 // Sets |update_duration_uptime_| to the given value and persists
214 // the value and sets |update_duration_uptime_timestamp_| to the
215 // current monotonic time.
216 void SetUpdateDurationUptime(const base::TimeDelta& value);
217
218 // Adds the difference between current monotonic time and
219 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
220 // sets |update_duration_uptime_timestamp_| to current monotonic time.
221 void CalculateUpdateDurationUptime();
222
Jay Srinivasan19409b72013-04-12 19:23:36 -0700223 // Returns the full key for a download source given the prefix.
224 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
225
226 // Loads the number of bytes that have been currently downloaded through the
227 // previous attempts from the persisted state for the given source. It's
228 // reset to 0 everytime we begin a full update and is continued from previous
229 // attempt if we're resuming the update.
230 void LoadCurrentBytesDownloaded(DownloadSource source);
231
232 // Sets the number of bytes that have been currently downloaded for the
233 // given source. This value is also persisted.
234 void SetCurrentBytesDownloaded(DownloadSource source,
235 uint64_t current_bytes_downloaded,
236 bool log);
237
238 // Loads the total number of bytes that have been downloaded (since the last
239 // successful update) from the persisted state for the given source. It's
240 // reset to 0 everytime we successfully apply an update and counts the bytes
241 // downloaded for both successful and failed attempts since then.
242 void LoadTotalBytesDownloaded(DownloadSource source);
243
244 // Sets the total number of bytes that have been downloaded so far for the
245 // given source. This value is also persisted.
246 void SetTotalBytesDownloaded(DownloadSource source,
247 uint64_t total_bytes_downloaded,
248 bool log);
249
Jay Srinivasan53173b92013-05-17 17:13:01 -0700250 inline uint32_t GetUrlIndex() {
251 return url_index_;
252 }
253
254 // Computes the list of candidate URLs from the total list of payload URLs in
255 // the Omaha response.
256 void ComputeCandidateUrls();
257
David Zeuthena573d6f2013-06-14 16:13:36 -0700258 // Sets |num_responses_seen_| and persist it to disk.
259 void SetNumResponsesSeen(int num_responses_seen);
260
261 // Initializes |num_responses_seen_| from persisted state.
262 void LoadNumResponsesSeen();
263
264 // Reports metric conveying how many times updates were abandoned
265 // before an update was applied.
266 void ReportUpdatesAbandonedCountMetric();
267
Jay Srinivasan19409b72013-04-12 19:23:36 -0700268 // The global state of the system.
269 SystemState* system_state_;
270
Chris Sosabe45bef2013-04-09 18:25:12 -0700271 // Initializes |num_reboots_| from the persisted state.
272 void LoadNumReboots();
273
274 // Sets |num_reboots| for the update attempt. Also persists the
275 // value being set so that we resume from the same value in case of a process
276 // restart.
277 void SetNumReboots(uint32_t num_reboots);
278
279 // Checks to see if the device rebooted since the last call and if so
280 // increments num_reboots.
281 void UpdateNumReboots();
282
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800283 // Interface object with which we read/write persisted state. This must
284 // be set by calling the Initialize method before calling any other method.
285 PrefsInterface* prefs_;
286
Jay Srinivasan08262882012-12-28 19:29:43 -0800287 // This is the current response object from Omaha.
288 OmahaResponse response_;
289
290 // This stores a "signature" of the current response. The signature here
291 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800292 // this value is persisted so we resume from the same value in case of a
293 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800294 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800295
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800296 // The number of times we've tried to download the payload in full. This is
297 // incremented each time we download the payload in full successsfully or
298 // when we exhaust all failure limits for all URLs and are about to wrap
299 // around back to the first URL. Each update to this value is persisted so
300 // we resume from the same value in case of a process restart.
301 uint32_t payload_attempt_number_;
302
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800303 // The index of the current URL. This type is different from the one in the
304 // accessor methods because PrefsInterface supports only int64_t but we want
305 // to provide a stronger abstraction of uint32_t. Each update to this value
306 // is persisted so we resume from the same value in case of a process
307 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 int64_t url_index_;
309
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800310 // The count of failures encountered in the current attempt to download using
311 // the current URL (specified by url_index_). Each update to this value is
312 // persisted so we resume from the same value in case of a process restart.
313 int64_t url_failure_count_;
314
David Zeuthencc6f9962013-04-18 11:57:24 -0700315 // The number of times we've switched URLs.
316 int32_t url_switch_count_;
317
Jay Srinivasan19409b72013-04-12 19:23:36 -0700318 // The current download source based on the current URL. This value is
319 // not persisted as it can be recomputed everytime we update the URL.
320 // We're storing this so as not to recompute this on every few bytes of
321 // data we read from the socket.
322 DownloadSource current_download_source_;
323
David Zeuthena573d6f2013-06-14 16:13:36 -0700324 // The number of different Omaha responses seen. Increases every time
325 // a new response is seen. Resets to 0 only when the system has been
326 // successfully updated.
327 int num_responses_seen_;
328
Chris Sosabe45bef2013-04-09 18:25:12 -0700329 // The number of system reboots during an update attempt. Technically since
330 // we don't go out of our way to not update it when not attempting an update,
331 // also records the number of reboots before the next update attempt starts.
332 uint32_t num_reboots_;
333
Jay Srinivasan08262882012-12-28 19:29:43 -0800334 // The timestamp until which we've to wait before attempting to download the
335 // payload again, so as to backoff repeated downloads.
336 base::Time backoff_expiry_time_;
337
David Zeuthen9a017f22013-04-11 16:10:26 -0700338 // The most recently calculated value of the update duration.
339 base::TimeDelta update_duration_current_;
340
341 // The point in time (wall-clock) that the update was started.
342 base::Time update_timestamp_start_;
343
344 // The point in time (wall-clock) that the update ended. If the update
345 // is still in progress, this is set to the Epoch (e.g. 0).
346 base::Time update_timestamp_end_;
347
348 // The update duration uptime
349 base::TimeDelta update_duration_uptime_;
350
351 // The monotonic time when |update_duration_uptime_| was last set
352 base::Time update_duration_uptime_timestamp_;
353
Jay Srinivasan19409b72013-04-12 19:23:36 -0700354 // The number of bytes that have been downloaded for each source for each new
355 // update attempt. If we resume an update, we'll continue from the previous
356 // value, but if we get a new response or if the previous attempt failed,
357 // we'll reset this to 0 to start afresh. Each update to this value is
358 // persisted so we resume from the same value in case of a process restart.
359 // The extra index in the array is to no-op accidental access in case the
360 // return value from GetCurrentDownloadSource is used without validation.
361 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
362
363 // The number of bytes that have been downloaded for each source since the
364 // the last successful update. This is used to compute the overhead we incur.
365 // Each update to this value is persisted so we resume from the same value in
366 // case of a process restart.
367 // The extra index in the array is to no-op accidental access in case the
368 // return value from GetCurrentDownloadSource is used without validation.
369 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
370
David Zeuthen9a017f22013-04-11 16:10:26 -0700371 // A small timespan used when comparing wall-clock times for coping
372 // with the fact that clocks drift and consequently are adjusted
373 // (either forwards or backwards) via NTP.
374 static const base::TimeDelta kDurationSlack;
375
Jay Srinivasan53173b92013-05-17 17:13:01 -0700376 // The ordered list of the subset of payload URL candidates which are
377 // allowed as per device policy.
378 std::vector<std::string> candidate_urls_;
379
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800380 DISALLOW_COPY_AND_ASSIGN(PayloadState);
381};
382
383} // namespace chromeos_update_engine
384
385#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__