blob: b34422f1c06507ce9102d47e77a771545e8d1203 [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);
Jay Srinivasan19409b72013-04-12 19:23:36 -070038 virtual void UpdateRestarted();
David Zeuthen9a017f22013-04-11 16:10:26 -070039 virtual void UpdateSucceeded();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080040 virtual void UpdateFailed(ActionExitCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080041 virtual bool ShouldBackoffDownload();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042
Jay Srinivasan08262882012-12-28 19:29:43 -080043 virtual inline std::string GetResponseSignature() {
44 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045 }
46
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080047 virtual inline uint32_t GetPayloadAttemptNumber() {
48 return payload_attempt_number_;
49 }
50
51 virtual inline uint32_t GetUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 return url_index_;
53 }
54
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080055 virtual inline uint32_t GetUrlFailureCount() {
56 return url_failure_count_;
57 }
58
Jay Srinivasan08262882012-12-28 19:29:43 -080059 virtual inline base::Time GetBackoffExpiryTime() {
60 return backoff_expiry_time_;
61 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080062
David Zeuthen9a017f22013-04-11 16:10:26 -070063 virtual base::TimeDelta GetUpdateDuration();
64
65 virtual base::TimeDelta GetUpdateDurationUptime();
66
Jay Srinivasan19409b72013-04-12 19:23:36 -070067 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
68 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
69 }
70
71 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
72 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
73 }
74
Jay Srinivasan08262882012-12-28 19:29:43 -080075 private:
76 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080077 // at the time of the next update check.
78 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080079
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080080 // Advances the current URL index to the next available one. If all URLs have
81 // been exhausted during the current payload download attempt (as indicated
82 // by the payload attempt number), then it will increment the payload attempt
83 // number and wrap around again with the first URL in the list.
84 void IncrementUrlIndex();
85
86 // Increments the failure count of the current URL. If the configured max
87 // failure count is reached for this URL, it advances the current URL index
88 // to the next URL and resets the failure count for that URL.
89 void IncrementFailureCount();
90
Jay Srinivasan08262882012-12-28 19:29:43 -080091 // Updates the backoff expiry time exponentially based on the current
92 // payload attempt number.
93 void UpdateBackoffExpiryTime();
94
Jay Srinivasan19409b72013-04-12 19:23:36 -070095 // Updates the value of current download source based on the current URL
96 // index. If the download source is not one of the known sources, it's set
97 // to kNumDownloadSources.
98 void UpdateCurrentDownloadSource();
99
100 // Updates the various metrics corresponding with the given number of bytes
101 // that were downloaded recently.
102 void UpdateBytesDownloaded(size_t count);
103
104 // Reports the various metrics related to the number of bytes downloaded.
105 void ReportBytesDownloadedMetrics();
106
Jay Srinivasan08262882012-12-28 19:29:43 -0800107 // Resets all the persisted state values which are maintained relative to the
108 // current response signature. The response signature itself is not reset.
109 void ResetPersistedState();
110
Jay Srinivasan19409b72013-04-12 19:23:36 -0700111 // Resets the appropriate state related to download sources that need to be
112 // reset on a new update.
113 void ResetDownloadSourcesOnNewUpdate();
114
115 // Returns the persisted value for the given key. It also validates that
116 // the value returned is non-negative.
117 int64_t GetPersistedValue(const std::string& key);
118
Jay Srinivasan08262882012-12-28 19:29:43 -0800119 // Calculates the response "signature", which is basically a string composed
120 // of the subset of the fields in the current response that affect the
121 // behavior of the PayloadState.
122 std::string CalculateResponseSignature();
123
124 // Initializes the current response signature from the persisted state.
125 void LoadResponseSignature();
126
127 // Sets the response signature to the given value. Also persists the value
128 // being set so that we resume from the save value in case of a process
129 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700130 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800131
132 // Initializes the payload attempt number from the persisted state.
133 void LoadPayloadAttemptNumber();
134
135 // Sets the payload attempt number to the given value. Also persists the
136 // value being set so that we resume from the same value in case of a process
137 // restart.
138 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
139
140 // Initializes the current URL index from the persisted state.
141 void LoadUrlIndex();
142
143 // Sets the current URL index to the given value. Also persists the value
144 // being set so that we resume from the same value in case of a process
145 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800146 void SetUrlIndex(uint32_t url_index);
147
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800148 // Initializes the current URL's failure count from the persisted stae.
149 void LoadUrlFailureCount();
150
151 // Sets the current URL's failure count to the given value. Also persists the
152 // value being set so that we resume from the same value in case of a process
153 // restart.
154 void SetUrlFailureCount(uint32_t url_failure_count);
155
Jay Srinivasan08262882012-12-28 19:29:43 -0800156 // Initializes the backoff expiry time from the persisted state.
157 void LoadBackoffExpiryTime();
158
159 // Sets the backoff expiry time to the given value. Also persists the value
160 // being set so that we resume from the same value in case of a process
161 // restart.
162 void SetBackoffExpiryTime(const base::Time& new_time);
163
David Zeuthen9a017f22013-04-11 16:10:26 -0700164 // Initializes |update_timestamp_start_| from the persisted state.
165 void LoadUpdateTimestampStart();
166
167 // Sets |update_timestamp_start_| to the given value and persists the value.
168 void SetUpdateTimestampStart(const base::Time& value);
169
170 // Sets |update_timestamp_end_| to the given value. This is not persisted
171 // as it happens at the end of the update process where state is deleted
172 // anyway.
173 void SetUpdateTimestampEnd(const base::Time& value);
174
175 // Initializes |update_duration_uptime_| from the persisted state.
176 void LoadUpdateDurationUptime();
177
178 // Helper method used in SetUpdateDurationUptime() and
179 // CalculateUpdateDurationUptime().
180 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
181 const base::Time& timestamp,
182 bool use_logging);
183
184 // Sets |update_duration_uptime_| to the given value and persists
185 // the value and sets |update_duration_uptime_timestamp_| to the
186 // current monotonic time.
187 void SetUpdateDurationUptime(const base::TimeDelta& value);
188
189 // Adds the difference between current monotonic time and
190 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
191 // sets |update_duration_uptime_timestamp_| to current monotonic time.
192 void CalculateUpdateDurationUptime();
193
Jay Srinivasan19409b72013-04-12 19:23:36 -0700194 // Returns the full key for a download source given the prefix.
195 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
196
197 // Loads the number of bytes that have been currently downloaded through the
198 // previous attempts from the persisted state for the given source. It's
199 // reset to 0 everytime we begin a full update and is continued from previous
200 // attempt if we're resuming the update.
201 void LoadCurrentBytesDownloaded(DownloadSource source);
202
203 // Sets the number of bytes that have been currently downloaded for the
204 // given source. This value is also persisted.
205 void SetCurrentBytesDownloaded(DownloadSource source,
206 uint64_t current_bytes_downloaded,
207 bool log);
208
209 // Loads the total number of bytes that have been downloaded (since the last
210 // successful update) from the persisted state for the given source. It's
211 // reset to 0 everytime we successfully apply an update and counts the bytes
212 // downloaded for both successful and failed attempts since then.
213 void LoadTotalBytesDownloaded(DownloadSource source);
214
215 // Sets the total number of bytes that have been downloaded so far for the
216 // given source. This value is also persisted.
217 void SetTotalBytesDownloaded(DownloadSource source,
218 uint64_t total_bytes_downloaded,
219 bool log);
220
221 // The global state of the system.
222 SystemState* system_state_;
223
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800224 // Interface object with which we read/write persisted state. This must
225 // be set by calling the Initialize method before calling any other method.
226 PrefsInterface* prefs_;
227
Jay Srinivasan08262882012-12-28 19:29:43 -0800228 // This is the current response object from Omaha.
229 OmahaResponse response_;
230
231 // This stores a "signature" of the current response. The signature here
232 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800233 // this value is persisted so we resume from the same value in case of a
234 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800235 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800236
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800237 // The number of times we've tried to download the payload in full. This is
238 // incremented each time we download the payload in full successsfully or
239 // when we exhaust all failure limits for all URLs and are about to wrap
240 // around back to the first URL. Each update to this value is persisted so
241 // we resume from the same value in case of a process restart.
242 uint32_t payload_attempt_number_;
243
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800244 // The index of the current URL. This type is different from the one in the
245 // accessor methods because PrefsInterface supports only int64_t but we want
246 // to provide a stronger abstraction of uint32_t. Each update to this value
247 // is persisted so we resume from the same value in case of a process
248 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800249 int64_t url_index_;
250
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800251 // The count of failures encountered in the current attempt to download using
252 // the current URL (specified by url_index_). Each update to this value is
253 // persisted so we resume from the same value in case of a process restart.
254 int64_t url_failure_count_;
255
Jay Srinivasan19409b72013-04-12 19:23:36 -0700256 // The current download source based on the current URL. This value is
257 // not persisted as it can be recomputed everytime we update the URL.
258 // We're storing this so as not to recompute this on every few bytes of
259 // data we read from the socket.
260 DownloadSource current_download_source_;
261
Jay Srinivasan08262882012-12-28 19:29:43 -0800262 // The timestamp until which we've to wait before attempting to download the
263 // payload again, so as to backoff repeated downloads.
264 base::Time backoff_expiry_time_;
265
David Zeuthen9a017f22013-04-11 16:10:26 -0700266 // The most recently calculated value of the update duration.
267 base::TimeDelta update_duration_current_;
268
269 // The point in time (wall-clock) that the update was started.
270 base::Time update_timestamp_start_;
271
272 // The point in time (wall-clock) that the update ended. If the update
273 // is still in progress, this is set to the Epoch (e.g. 0).
274 base::Time update_timestamp_end_;
275
276 // The update duration uptime
277 base::TimeDelta update_duration_uptime_;
278
279 // The monotonic time when |update_duration_uptime_| was last set
280 base::Time update_duration_uptime_timestamp_;
281
Jay Srinivasan19409b72013-04-12 19:23:36 -0700282 // The number of bytes that have been downloaded for each source for each new
283 // update attempt. If we resume an update, we'll continue from the previous
284 // value, but if we get a new response or if the previous attempt failed,
285 // we'll reset this to 0 to start afresh. Each update to this value is
286 // persisted so we resume from the same value in case of a process restart.
287 // The extra index in the array is to no-op accidental access in case the
288 // return value from GetCurrentDownloadSource is used without validation.
289 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
290
291 // The number of bytes that have been downloaded for each source since the
292 // the last successful update. This is used to compute the overhead we incur.
293 // Each update to this value is persisted so we resume from the same value in
294 // case of a process restart.
295 // The extra index in the array is to no-op accidental access in case the
296 // return value from GetCurrentDownloadSource is used without validation.
297 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
298
Jay Srinivasan08262882012-12-28 19:29:43 -0800299 // Returns the number of URLs in the current response.
300 // Note: This value will be 0 if this method is called before we receive
301 // the first valid Omaha response in this process.
302 uint32_t GetNumUrls() {
303 return response_.payload_urls.size();
304 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800305
David Zeuthen9a017f22013-04-11 16:10:26 -0700306 // A small timespan used when comparing wall-clock times for coping
307 // with the fact that clocks drift and consequently are adjusted
308 // (either forwards or backwards) via NTP.
309 static const base::TimeDelta kDurationSlack;
310
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800311 DISALLOW_COPY_AND_ASSIGN(PayloadState);
312};
313
314} // namespace chromeos_update_engine
315
316#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__