blob: 7c0d0d3ab66a60542bd8b20b1574166559dffbe1 [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
David Zeuthene4c58bf2013-06-18 17:26:50 -070088 virtual void UpdateEngineStarted();
89
Jay Srinivasan08262882012-12-28 19:29:43 -080090 private:
91 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080092 // at the time of the next update check.
93 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080094
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080095 // Advances the current URL index to the next available one. If all URLs have
96 // been exhausted during the current payload download attempt (as indicated
97 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -070098 // number and wrap around again with the first URL in the list. This also
99 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800100 void IncrementUrlIndex();
101
102 // Increments the failure count of the current URL. If the configured max
103 // failure count is reached for this URL, it advances the current URL index
104 // to the next URL and resets the failure count for that URL.
105 void IncrementFailureCount();
106
Jay Srinivasan08262882012-12-28 19:29:43 -0800107 // Updates the backoff expiry time exponentially based on the current
108 // payload attempt number.
109 void UpdateBackoffExpiryTime();
110
Jay Srinivasan19409b72013-04-12 19:23:36 -0700111 // Updates the value of current download source based on the current URL
112 // index. If the download source is not one of the known sources, it's set
113 // to kNumDownloadSources.
114 void UpdateCurrentDownloadSource();
115
116 // Updates the various metrics corresponding with the given number of bytes
117 // that were downloaded recently.
118 void UpdateBytesDownloaded(size_t count);
119
120 // Reports the various metrics related to the number of bytes downloaded.
121 void ReportBytesDownloadedMetrics();
122
David Zeuthencc6f9962013-04-18 11:57:24 -0700123 // Reports the metric related to number of URL switches.
124 void ReportUpdateUrlSwitchesMetric();
125
Chris Sosabe45bef2013-04-09 18:25:12 -0700126 // Reports the various metrics related to rebooting during an update.
127 void ReportRebootMetrics();
128
David Zeuthen674c3182013-04-18 14:05:20 -0700129 // Reports the various metrics related to update duration.
130 void ReportDurationMetrics();
131
Jay Srinivasan08262882012-12-28 19:29:43 -0800132 // Resets all the persisted state values which are maintained relative to the
133 // current response signature. The response signature itself is not reset.
134 void ResetPersistedState();
135
Jay Srinivasan19409b72013-04-12 19:23:36 -0700136 // Resets the appropriate state related to download sources that need to be
137 // reset on a new update.
138 void ResetDownloadSourcesOnNewUpdate();
139
140 // Returns the persisted value for the given key. It also validates that
141 // the value returned is non-negative.
142 int64_t GetPersistedValue(const std::string& key);
143
Jay Srinivasan08262882012-12-28 19:29:43 -0800144 // Calculates the response "signature", which is basically a string composed
145 // of the subset of the fields in the current response that affect the
146 // behavior of the PayloadState.
147 std::string CalculateResponseSignature();
148
149 // Initializes the current response signature from the persisted state.
150 void LoadResponseSignature();
151
152 // Sets the response signature to the given value. Also persists the value
153 // being set so that we resume from the save value in case of a process
154 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700155 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800156
157 // Initializes the payload attempt number from the persisted state.
158 void LoadPayloadAttemptNumber();
159
160 // Sets the payload attempt number to the given value. Also persists the
161 // value being set so that we resume from the same value in case of a process
162 // restart.
163 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
164
165 // Initializes the current URL index from the persisted state.
166 void LoadUrlIndex();
167
168 // Sets the current URL index to the given value. Also persists the value
169 // being set so that we resume from the same value in case of a process
170 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800171 void SetUrlIndex(uint32_t url_index);
172
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800173 // Initializes the current URL's failure count from the persisted stae.
174 void LoadUrlFailureCount();
175
176 // Sets the current URL's failure count to the given value. Also persists the
177 // value being set so that we resume from the same value in case of a process
178 // restart.
179 void SetUrlFailureCount(uint32_t url_failure_count);
180
David Zeuthencc6f9962013-04-18 11:57:24 -0700181 // Sets |url_switch_count_| to the given value and persists the value.
182 void SetUrlSwitchCount(uint32_t url_switch_count);
183
184 // Initializes |url_switch_count_| from the persisted stae.
185 void LoadUrlSwitchCount();
186
Jay Srinivasan08262882012-12-28 19:29:43 -0800187 // Initializes the backoff expiry time from the persisted state.
188 void LoadBackoffExpiryTime();
189
190 // Sets the backoff expiry time to the given value. Also persists the value
191 // being set so that we resume from the same value in case of a process
192 // restart.
193 void SetBackoffExpiryTime(const base::Time& new_time);
194
David Zeuthen9a017f22013-04-11 16:10:26 -0700195 // Initializes |update_timestamp_start_| from the persisted state.
196 void LoadUpdateTimestampStart();
197
198 // Sets |update_timestamp_start_| to the given value and persists the value.
199 void SetUpdateTimestampStart(const base::Time& value);
200
201 // Sets |update_timestamp_end_| to the given value. This is not persisted
202 // as it happens at the end of the update process where state is deleted
203 // anyway.
204 void SetUpdateTimestampEnd(const base::Time& value);
205
206 // Initializes |update_duration_uptime_| from the persisted state.
207 void LoadUpdateDurationUptime();
208
209 // Helper method used in SetUpdateDurationUptime() and
210 // CalculateUpdateDurationUptime().
211 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
212 const base::Time& timestamp,
213 bool use_logging);
214
215 // Sets |update_duration_uptime_| to the given value and persists
216 // the value and sets |update_duration_uptime_timestamp_| to the
217 // current monotonic time.
218 void SetUpdateDurationUptime(const base::TimeDelta& value);
219
220 // Adds the difference between current monotonic time and
221 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
222 // sets |update_duration_uptime_timestamp_| to current monotonic time.
223 void CalculateUpdateDurationUptime();
224
Jay Srinivasan19409b72013-04-12 19:23:36 -0700225 // Returns the full key for a download source given the prefix.
226 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
227
228 // Loads the number of bytes that have been currently downloaded through the
229 // previous attempts from the persisted state for the given source. It's
230 // reset to 0 everytime we begin a full update and is continued from previous
231 // attempt if we're resuming the update.
232 void LoadCurrentBytesDownloaded(DownloadSource source);
233
234 // Sets the number of bytes that have been currently downloaded for the
235 // given source. This value is also persisted.
236 void SetCurrentBytesDownloaded(DownloadSource source,
237 uint64_t current_bytes_downloaded,
238 bool log);
239
240 // Loads the total number of bytes that have been downloaded (since the last
241 // successful update) from the persisted state for the given source. It's
242 // reset to 0 everytime we successfully apply an update and counts the bytes
243 // downloaded for both successful and failed attempts since then.
244 void LoadTotalBytesDownloaded(DownloadSource source);
245
246 // Sets the total number of bytes that have been downloaded so far for the
247 // given source. This value is also persisted.
248 void SetTotalBytesDownloaded(DownloadSource source,
249 uint64_t total_bytes_downloaded,
250 bool log);
251
Jay Srinivasan53173b92013-05-17 17:13:01 -0700252 inline uint32_t GetUrlIndex() {
253 return url_index_;
254 }
255
256 // Computes the list of candidate URLs from the total list of payload URLs in
257 // the Omaha response.
258 void ComputeCandidateUrls();
259
David Zeuthena573d6f2013-06-14 16:13:36 -0700260 // Sets |num_responses_seen_| and persist it to disk.
261 void SetNumResponsesSeen(int num_responses_seen);
262
263 // Initializes |num_responses_seen_| from persisted state.
264 void LoadNumResponsesSeen();
265
266 // Reports metric conveying how many times updates were abandoned
267 // before an update was applied.
268 void ReportUpdatesAbandonedCountMetric();
269
Jay Srinivasan19409b72013-04-12 19:23:36 -0700270 // The global state of the system.
271 SystemState* system_state_;
272
Chris Sosabe45bef2013-04-09 18:25:12 -0700273 // Initializes |num_reboots_| from the persisted state.
274 void LoadNumReboots();
275
276 // Sets |num_reboots| for the update attempt. Also persists the
277 // value being set so that we resume from the same value in case of a process
278 // restart.
279 void SetNumReboots(uint32_t num_reboots);
280
281 // Checks to see if the device rebooted since the last call and if so
282 // increments num_reboots.
283 void UpdateNumReboots();
284
David Zeuthene4c58bf2013-06-18 17:26:50 -0700285 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
286 // state variable.
287 void CreateSystemUpdatedMarkerFile();
288
289 // Called at program startup if the device booted into a new update.
290 // The |time_to_reboot| parameter contains the (wall-clock) duration
291 // from when the update successfully completed (the value written
292 // into the kPrefsSystemUpdatedMarker state variable) until the device
293 // was booted into the update (current wall-clock time).
294 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
295
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800296 // Interface object with which we read/write persisted state. This must
297 // be set by calling the Initialize method before calling any other method.
298 PrefsInterface* prefs_;
299
Jay Srinivasan08262882012-12-28 19:29:43 -0800300 // This is the current response object from Omaha.
301 OmahaResponse response_;
302
303 // This stores a "signature" of the current response. The signature here
304 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800305 // this value is persisted so we resume from the same value in case of a
306 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800307 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800309 // The number of times we've tried to download the payload in full. This is
310 // incremented each time we download the payload in full successsfully or
311 // when we exhaust all failure limits for all URLs and are about to wrap
312 // around back to the first URL. Each update to this value is persisted so
313 // we resume from the same value in case of a process restart.
314 uint32_t payload_attempt_number_;
315
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800316 // The index of the current URL. This type is different from the one in the
317 // accessor methods because PrefsInterface supports only int64_t but we want
318 // to provide a stronger abstraction of uint32_t. Each update to this value
319 // is persisted so we resume from the same value in case of a process
320 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800321 int64_t url_index_;
322
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800323 // The count of failures encountered in the current attempt to download using
324 // the current URL (specified by url_index_). Each update to this value is
325 // persisted so we resume from the same value in case of a process restart.
326 int64_t url_failure_count_;
327
David Zeuthencc6f9962013-04-18 11:57:24 -0700328 // The number of times we've switched URLs.
329 int32_t url_switch_count_;
330
Jay Srinivasan19409b72013-04-12 19:23:36 -0700331 // The current download source based on the current URL. This value is
332 // not persisted as it can be recomputed everytime we update the URL.
333 // We're storing this so as not to recompute this on every few bytes of
334 // data we read from the socket.
335 DownloadSource current_download_source_;
336
David Zeuthena573d6f2013-06-14 16:13:36 -0700337 // The number of different Omaha responses seen. Increases every time
338 // a new response is seen. Resets to 0 only when the system has been
339 // successfully updated.
340 int num_responses_seen_;
341
Chris Sosabe45bef2013-04-09 18:25:12 -0700342 // The number of system reboots during an update attempt. Technically since
343 // we don't go out of our way to not update it when not attempting an update,
344 // also records the number of reboots before the next update attempt starts.
345 uint32_t num_reboots_;
346
Jay Srinivasan08262882012-12-28 19:29:43 -0800347 // The timestamp until which we've to wait before attempting to download the
348 // payload again, so as to backoff repeated downloads.
349 base::Time backoff_expiry_time_;
350
David Zeuthen9a017f22013-04-11 16:10:26 -0700351 // The most recently calculated value of the update duration.
352 base::TimeDelta update_duration_current_;
353
354 // The point in time (wall-clock) that the update was started.
355 base::Time update_timestamp_start_;
356
357 // The point in time (wall-clock) that the update ended. If the update
358 // is still in progress, this is set to the Epoch (e.g. 0).
359 base::Time update_timestamp_end_;
360
361 // The update duration uptime
362 base::TimeDelta update_duration_uptime_;
363
364 // The monotonic time when |update_duration_uptime_| was last set
365 base::Time update_duration_uptime_timestamp_;
366
Jay Srinivasan19409b72013-04-12 19:23:36 -0700367 // The number of bytes that have been downloaded for each source for each new
368 // update attempt. If we resume an update, we'll continue from the previous
369 // value, but if we get a new response or if the previous attempt failed,
370 // we'll reset this to 0 to start afresh. Each update to this value is
371 // persisted so we resume from the same value in case of a process restart.
372 // The extra index in the array is to no-op accidental access in case the
373 // return value from GetCurrentDownloadSource is used without validation.
374 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
375
376 // The number of bytes that have been downloaded for each source since the
377 // the last successful update. This is used to compute the overhead we incur.
378 // Each update to this value is persisted so we resume from the same value in
379 // case of a process restart.
380 // The extra index in the array is to no-op accidental access in case the
381 // return value from GetCurrentDownloadSource is used without validation.
382 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
383
David Zeuthen9a017f22013-04-11 16:10:26 -0700384 // A small timespan used when comparing wall-clock times for coping
385 // with the fact that clocks drift and consequently are adjusted
386 // (either forwards or backwards) via NTP.
387 static const base::TimeDelta kDurationSlack;
388
Jay Srinivasan53173b92013-05-17 17:13:01 -0700389 // The ordered list of the subset of payload URL candidates which are
390 // allowed as per device policy.
391 std::vector<std::string> candidate_urls_;
392
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800393 DISALLOW_COPY_AND_ASSIGN(PayloadState);
394};
395
396} // namespace chromeos_update_engine
397
398#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__