blob: 8c3133cb58d3b9ba2c01c141c6c47ed4c44f9d7c [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();
Chris Sosaaa18e162013-06-20 13:20:30 -070043 virtual void Rollback();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044
Jay Srinivasan08262882012-12-28 19:29:43 -080045 virtual inline std::string GetResponseSignature() {
46 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080047 }
48
Alex Deymo820cc702013-06-28 15:43:46 -070049 virtual inline int GetFullPayloadAttemptNumber() {
50 return full_payload_attempt_number_;
51 }
52
53 virtual inline int GetPayloadAttemptNumber() {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080054 return payload_attempt_number_;
55 }
56
Jay Srinivasan53173b92013-05-17 17:13:01 -070057 virtual inline std::string GetCurrentUrl() {
58 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080059 }
60
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080061 virtual inline uint32_t GetUrlFailureCount() {
62 return url_failure_count_;
63 }
64
David Zeuthencc6f9962013-04-18 11:57:24 -070065 virtual inline uint32_t GetUrlSwitchCount() {
66 return url_switch_count_;
67 }
68
David Zeuthena573d6f2013-06-14 16:13:36 -070069 virtual inline int GetNumResponsesSeen() {
70 return num_responses_seen_;
71 }
72
Jay Srinivasan08262882012-12-28 19:29:43 -080073 virtual inline base::Time GetBackoffExpiryTime() {
74 return backoff_expiry_time_;
75 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080076
David Zeuthen9a017f22013-04-11 16:10:26 -070077 virtual base::TimeDelta GetUpdateDuration();
78
79 virtual base::TimeDelta GetUpdateDurationUptime();
80
Jay Srinivasan19409b72013-04-12 19:23:36 -070081 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
82 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
83 }
84
85 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
86 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
87 }
88
Chris Sosabe45bef2013-04-09 18:25:12 -070089 virtual inline uint32_t GetNumReboots() {
90 return num_reboots_;
91 }
92
David Zeuthene4c58bf2013-06-18 17:26:50 -070093 virtual void UpdateEngineStarted();
94
Chris Sosaaa18e162013-06-20 13:20:30 -070095 virtual inline std::string GetRollbackVersion() {
96 return rollback_version_;
97 }
98
Jay Srinivasan08262882012-12-28 19:29:43 -080099 private:
Alex Deymo820cc702013-06-28 15:43:46 -0700100 // Increments the payload attempt number used for metrics.
101 void IncrementPayloadAttemptNumber();
102
Jay Srinivasan08262882012-12-28 19:29:43 -0800103 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800104 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700105 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800106
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800107 // Advances the current URL index to the next available one. If all URLs have
108 // been exhausted during the current payload download attempt (as indicated
109 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700110 // number and wrap around again with the first URL in the list. This also
111 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800112 void IncrementUrlIndex();
113
114 // Increments the failure count of the current URL. If the configured max
115 // failure count is reached for this URL, it advances the current URL index
116 // to the next URL and resets the failure count for that URL.
117 void IncrementFailureCount();
118
Jay Srinivasan08262882012-12-28 19:29:43 -0800119 // Updates the backoff expiry time exponentially based on the current
120 // payload attempt number.
121 void UpdateBackoffExpiryTime();
122
Jay Srinivasan19409b72013-04-12 19:23:36 -0700123 // Updates the value of current download source based on the current URL
124 // index. If the download source is not one of the known sources, it's set
125 // to kNumDownloadSources.
126 void UpdateCurrentDownloadSource();
127
128 // Updates the various metrics corresponding with the given number of bytes
129 // that were downloaded recently.
130 void UpdateBytesDownloaded(size_t count);
131
132 // Reports the various metrics related to the number of bytes downloaded.
133 void ReportBytesDownloadedMetrics();
134
David Zeuthencc6f9962013-04-18 11:57:24 -0700135 // Reports the metric related to number of URL switches.
136 void ReportUpdateUrlSwitchesMetric();
137
Chris Sosabe45bef2013-04-09 18:25:12 -0700138 // Reports the various metrics related to rebooting during an update.
139 void ReportRebootMetrics();
140
David Zeuthen674c3182013-04-18 14:05:20 -0700141 // Reports the various metrics related to update duration.
142 void ReportDurationMetrics();
143
Alex Deymo1c656c42013-06-28 11:02:14 -0700144 // Reports the metric related to the applied payload type.
145 void ReportPayloadTypeMetric();
146
Alex Deymo820cc702013-06-28 15:43:46 -0700147 // Reports the various metrics related to update attempts counts.
148 void ReportAttemptsCountMetrics();
149
Jay Srinivasan08262882012-12-28 19:29:43 -0800150 // Resets all the persisted state values which are maintained relative to the
151 // current response signature. The response signature itself is not reset.
152 void ResetPersistedState();
153
Jay Srinivasan19409b72013-04-12 19:23:36 -0700154 // Resets the appropriate state related to download sources that need to be
155 // reset on a new update.
156 void ResetDownloadSourcesOnNewUpdate();
157
158 // Returns the persisted value for the given key. It also validates that
Chris Sosaaa18e162013-06-20 13:20:30 -0700159 // the value returned is non-negative. If |across_powerwash| is True,
160 // get the value that will persist across a powerwash.
161 int64_t GetPersistedValue(const std::string& key, bool across_powerwash);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700162
Jay Srinivasan08262882012-12-28 19:29:43 -0800163 // Calculates the response "signature", which is basically a string composed
164 // of the subset of the fields in the current response that affect the
165 // behavior of the PayloadState.
166 std::string CalculateResponseSignature();
167
168 // Initializes the current response signature from the persisted state.
169 void LoadResponseSignature();
170
171 // Sets the response signature to the given value. Also persists the value
172 // being set so that we resume from the save value in case of a process
173 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700174 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800175
176 // Initializes the payload attempt number from the persisted state.
177 void LoadPayloadAttemptNumber();
178
Alex Deymo820cc702013-06-28 15:43:46 -0700179 // Initializes the payload attempt number for full payloads from the persisted
180 // state.
181 void LoadFullPayloadAttemptNumber();
182
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800183 // Sets the payload attempt number to the given value. Also persists the
184 // value being set so that we resume from the same value in case of a process
185 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700186 void SetPayloadAttemptNumber(int payload_attempt_number);
187
188 // Sets the payload attempt number for full updates to the given value. Also
189 // persists the value being set so that we resume from the same value in case
190 // of a process restart.
191 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800192
193 // Initializes the current URL index from the persisted state.
194 void LoadUrlIndex();
195
196 // Sets the current URL index to the given value. Also persists the value
197 // being set so that we resume from the same value in case of a process
198 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800199 void SetUrlIndex(uint32_t url_index);
200
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800201 // Initializes the current URL's failure count from the persisted stae.
202 void LoadUrlFailureCount();
203
204 // Sets the current URL's failure count to the given value. Also persists the
205 // value being set so that we resume from the same value in case of a process
206 // restart.
207 void SetUrlFailureCount(uint32_t url_failure_count);
208
David Zeuthencc6f9962013-04-18 11:57:24 -0700209 // Sets |url_switch_count_| to the given value and persists the value.
210 void SetUrlSwitchCount(uint32_t url_switch_count);
211
212 // Initializes |url_switch_count_| from the persisted stae.
213 void LoadUrlSwitchCount();
214
Jay Srinivasan08262882012-12-28 19:29:43 -0800215 // Initializes the backoff expiry time from the persisted state.
216 void LoadBackoffExpiryTime();
217
218 // Sets the backoff expiry time to the given value. Also persists the value
219 // being set so that we resume from the same value in case of a process
220 // restart.
221 void SetBackoffExpiryTime(const base::Time& new_time);
222
David Zeuthen9a017f22013-04-11 16:10:26 -0700223 // Initializes |update_timestamp_start_| from the persisted state.
224 void LoadUpdateTimestampStart();
225
226 // Sets |update_timestamp_start_| to the given value and persists the value.
227 void SetUpdateTimestampStart(const base::Time& value);
228
229 // Sets |update_timestamp_end_| to the given value. This is not persisted
230 // as it happens at the end of the update process where state is deleted
231 // anyway.
232 void SetUpdateTimestampEnd(const base::Time& value);
233
234 // Initializes |update_duration_uptime_| from the persisted state.
235 void LoadUpdateDurationUptime();
236
237 // Helper method used in SetUpdateDurationUptime() and
238 // CalculateUpdateDurationUptime().
239 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
240 const base::Time& timestamp,
241 bool use_logging);
242
243 // Sets |update_duration_uptime_| to the given value and persists
244 // the value and sets |update_duration_uptime_timestamp_| to the
245 // current monotonic time.
246 void SetUpdateDurationUptime(const base::TimeDelta& value);
247
248 // Adds the difference between current monotonic time and
249 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
250 // sets |update_duration_uptime_timestamp_| to current monotonic time.
251 void CalculateUpdateDurationUptime();
252
Jay Srinivasan19409b72013-04-12 19:23:36 -0700253 // Returns the full key for a download source given the prefix.
254 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
255
256 // Loads the number of bytes that have been currently downloaded through the
257 // previous attempts from the persisted state for the given source. It's
258 // reset to 0 everytime we begin a full update and is continued from previous
259 // attempt if we're resuming the update.
260 void LoadCurrentBytesDownloaded(DownloadSource source);
261
262 // Sets the number of bytes that have been currently downloaded for the
263 // given source. This value is also persisted.
264 void SetCurrentBytesDownloaded(DownloadSource source,
265 uint64_t current_bytes_downloaded,
266 bool log);
267
268 // Loads the total number of bytes that have been downloaded (since the last
269 // successful update) from the persisted state for the given source. It's
270 // reset to 0 everytime we successfully apply an update and counts the bytes
271 // downloaded for both successful and failed attempts since then.
272 void LoadTotalBytesDownloaded(DownloadSource source);
273
274 // Sets the total number of bytes that have been downloaded so far for the
275 // given source. This value is also persisted.
276 void SetTotalBytesDownloaded(DownloadSource source,
277 uint64_t total_bytes_downloaded,
278 bool log);
279
Chris Sosaaa18e162013-06-20 13:20:30 -0700280 // Loads the blacklisted version from our prefs file.
281 void LoadRollbackVersion();
282
283 // Blacklists this version from getting AU'd to until we receive a new update
284 // response.
285 void SetRollbackVersion(const std::string& rollback_version);
286
287 // Clears any blacklisted version.
288 void ResetRollbackVersion();
289
Jay Srinivasan53173b92013-05-17 17:13:01 -0700290 inline uint32_t GetUrlIndex() {
291 return url_index_;
292 }
293
294 // Computes the list of candidate URLs from the total list of payload URLs in
295 // the Omaha response.
296 void ComputeCandidateUrls();
297
David Zeuthena573d6f2013-06-14 16:13:36 -0700298 // Sets |num_responses_seen_| and persist it to disk.
299 void SetNumResponsesSeen(int num_responses_seen);
300
301 // Initializes |num_responses_seen_| from persisted state.
302 void LoadNumResponsesSeen();
303
304 // Reports metric conveying how many times updates were abandoned
305 // before an update was applied.
306 void ReportUpdatesAbandonedCountMetric();
307
Jay Srinivasan19409b72013-04-12 19:23:36 -0700308 // The global state of the system.
309 SystemState* system_state_;
310
Chris Sosabe45bef2013-04-09 18:25:12 -0700311 // Initializes |num_reboots_| from the persisted state.
312 void LoadNumReboots();
313
314 // Sets |num_reboots| for the update attempt. Also persists the
315 // value being set so that we resume from the same value in case of a process
316 // restart.
317 void SetNumReboots(uint32_t num_reboots);
318
319 // Checks to see if the device rebooted since the last call and if so
320 // increments num_reboots.
321 void UpdateNumReboots();
322
David Zeuthene4c58bf2013-06-18 17:26:50 -0700323 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
324 // state variable.
325 void CreateSystemUpdatedMarkerFile();
326
327 // Called at program startup if the device booted into a new update.
328 // The |time_to_reboot| parameter contains the (wall-clock) duration
329 // from when the update successfully completed (the value written
330 // into the kPrefsSystemUpdatedMarker state variable) until the device
331 // was booted into the update (current wall-clock time).
332 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
333
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800334 // Interface object with which we read/write persisted state. This must
335 // be set by calling the Initialize method before calling any other method.
336 PrefsInterface* prefs_;
337
Chris Sosaaa18e162013-06-20 13:20:30 -0700338 // Interface object with which we read/write persisted state. This must
339 // be set by calling the Initialize method before calling any other method.
340 // This object persists across powerwashes.
341 PrefsInterface* powerwash_safe_prefs_;
342
Jay Srinivasan08262882012-12-28 19:29:43 -0800343 // This is the current response object from Omaha.
344 OmahaResponse response_;
345
346 // This stores a "signature" of the current response. The signature here
347 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800348 // this value is persisted so we resume from the same value in case of a
349 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800350 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800351
Alex Deymo820cc702013-06-28 15:43:46 -0700352 // The number of times we've tried to download the payload. This is
353 // incremented each time we download the payload successsfully or when we
354 // exhaust all failure limits for all URLs and are about to wrap around back
355 // to the first URL. Each update to this value is persisted so we resume from
356 // the same value in case of a process restart.
357 int payload_attempt_number_;
358
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800359 // The number of times we've tried to download the payload in full. This is
360 // incremented each time we download the payload in full successsfully or
361 // when we exhaust all failure limits for all URLs and are about to wrap
362 // around back to the first URL. Each update to this value is persisted so
363 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700364 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800365
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800366 // The index of the current URL. This type is different from the one in the
367 // accessor methods because PrefsInterface supports only int64_t but we want
368 // to provide a stronger abstraction of uint32_t. Each update to this value
369 // is persisted so we resume from the same value in case of a process
370 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800371 int64_t url_index_;
372
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800373 // The count of failures encountered in the current attempt to download using
374 // the current URL (specified by url_index_). Each update to this value is
375 // persisted so we resume from the same value in case of a process restart.
376 int64_t url_failure_count_;
377
David Zeuthencc6f9962013-04-18 11:57:24 -0700378 // The number of times we've switched URLs.
379 int32_t url_switch_count_;
380
Jay Srinivasan19409b72013-04-12 19:23:36 -0700381 // The current download source based on the current URL. This value is
382 // not persisted as it can be recomputed everytime we update the URL.
383 // We're storing this so as not to recompute this on every few bytes of
384 // data we read from the socket.
385 DownloadSource current_download_source_;
386
David Zeuthena573d6f2013-06-14 16:13:36 -0700387 // The number of different Omaha responses seen. Increases every time
388 // a new response is seen. Resets to 0 only when the system has been
389 // successfully updated.
390 int num_responses_seen_;
391
Chris Sosabe45bef2013-04-09 18:25:12 -0700392 // The number of system reboots during an update attempt. Technically since
393 // we don't go out of our way to not update it when not attempting an update,
394 // also records the number of reboots before the next update attempt starts.
395 uint32_t num_reboots_;
396
Jay Srinivasan08262882012-12-28 19:29:43 -0800397 // The timestamp until which we've to wait before attempting to download the
398 // payload again, so as to backoff repeated downloads.
399 base::Time backoff_expiry_time_;
400
David Zeuthen9a017f22013-04-11 16:10:26 -0700401 // The most recently calculated value of the update duration.
402 base::TimeDelta update_duration_current_;
403
404 // The point in time (wall-clock) that the update was started.
405 base::Time update_timestamp_start_;
406
407 // The point in time (wall-clock) that the update ended. If the update
408 // is still in progress, this is set to the Epoch (e.g. 0).
409 base::Time update_timestamp_end_;
410
411 // The update duration uptime
412 base::TimeDelta update_duration_uptime_;
413
414 // The monotonic time when |update_duration_uptime_| was last set
415 base::Time update_duration_uptime_timestamp_;
416
Jay Srinivasan19409b72013-04-12 19:23:36 -0700417 // The number of bytes that have been downloaded for each source for each new
418 // update attempt. If we resume an update, we'll continue from the previous
419 // value, but if we get a new response or if the previous attempt failed,
420 // we'll reset this to 0 to start afresh. Each update to this value is
421 // persisted so we resume from the same value in case of a process restart.
422 // The extra index in the array is to no-op accidental access in case the
423 // return value from GetCurrentDownloadSource is used without validation.
424 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
425
426 // The number of bytes that have been downloaded for each source since the
427 // the last successful update. This is used to compute the overhead we incur.
428 // Each update to this value is persisted so we resume from the same value in
429 // case of a process restart.
430 // The extra index in the array is to no-op accidental access in case the
431 // return value from GetCurrentDownloadSource is used without validation.
432 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
433
David Zeuthen9a017f22013-04-11 16:10:26 -0700434 // A small timespan used when comparing wall-clock times for coping
435 // with the fact that clocks drift and consequently are adjusted
436 // (either forwards or backwards) via NTP.
437 static const base::TimeDelta kDurationSlack;
438
Jay Srinivasan53173b92013-05-17 17:13:01 -0700439 // The ordered list of the subset of payload URL candidates which are
440 // allowed as per device policy.
441 std::vector<std::string> candidate_urls_;
442
Chris Sosaaa18e162013-06-20 13:20:30 -0700443 // This stores a blacklisted version set as part of rollback. When we rollback
444 // we store the version of the os from which we are rolling back from in order
445 // to guarantee that we do not re-update to it on the next au attempt after
446 // reboot.
447 std::string rollback_version_;
448
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800449 DISALLOW_COPY_AND_ASSIGN(PayloadState);
450};
451
452} // namespace chromeos_update_engine
453
454#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__