Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 8 | #include <base/time.h> |
| 9 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 10 | #include "update_engine/payload_state_interface.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 11 | #include "update_engine/prefs_interface.h" |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 15 | // Encapsulates all the payload state required for download. This includes the |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 16 | // state necessary for handling multiple URLs in Omaha response, the backoff |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 17 | // state, etc. All state is persisted so that we use the most recently saved |
| 18 | // value when resuming the update_engine process. All state is also cached in |
| 19 | // memory so that we ensure we always make progress based on last known good |
| 20 | // state even when there's any issue in reading/writing from the file system. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 21 | class PayloadState : public PayloadStateInterface { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | public: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 23 | PayloadState() |
| 24 | : prefs_(NULL), |
| 25 | payload_attempt_number_(0), |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 26 | url_index_(0), |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 27 | url_failure_count_(0) {} |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 28 | |
| 29 | virtual ~PayloadState() {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 30 | |
| 31 | // Initializes a payload state object using |prefs| for storing the |
| 32 | // persisted state. It also performs the initial loading of all persisted |
| 33 | // state into memory and dumps the initial state for debugging purposes. |
| 34 | // Note: the other methods should be called only after calling Initialize |
| 35 | // on this object. |
| 36 | bool Initialize(PrefsInterface* prefs); |
| 37 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 39 | // Implementation of PayloadStateInterface methods. |
| 40 | virtual void SetResponse(const OmahaResponse& response); |
| 41 | virtual void DownloadComplete(); |
| 42 | virtual void DownloadProgress(size_t count); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 43 | virtual void UpdateSucceeded(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 44 | virtual void UpdateFailed(ActionExitCode error); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 45 | virtual bool ShouldBackoffDownload(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 46 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 47 | virtual inline std::string GetResponseSignature() { |
| 48 | return response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 51 | virtual inline uint32_t GetPayloadAttemptNumber() { |
| 52 | return payload_attempt_number_; |
| 53 | } |
| 54 | |
| 55 | virtual inline uint32_t GetUrlIndex() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 56 | return url_index_; |
| 57 | } |
| 58 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 59 | virtual inline uint32_t GetUrlFailureCount() { |
| 60 | return url_failure_count_; |
| 61 | } |
| 62 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 63 | virtual inline base::Time GetBackoffExpiryTime() { |
| 64 | return backoff_expiry_time_; |
| 65 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 66 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 67 | virtual base::TimeDelta GetUpdateDuration(); |
| 68 | |
| 69 | virtual base::TimeDelta GetUpdateDurationUptime(); |
| 70 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 71 | private: |
| 72 | // Increments the payload attempt number which governs the backoff behavior |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 73 | // at the time of the next update check. |
| 74 | void IncrementPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 75 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 76 | // Advances the current URL index to the next available one. If all URLs have |
| 77 | // been exhausted during the current payload download attempt (as indicated |
| 78 | // by the payload attempt number), then it will increment the payload attempt |
| 79 | // number and wrap around again with the first URL in the list. |
| 80 | void IncrementUrlIndex(); |
| 81 | |
| 82 | // Increments the failure count of the current URL. If the configured max |
| 83 | // failure count is reached for this URL, it advances the current URL index |
| 84 | // to the next URL and resets the failure count for that URL. |
| 85 | void IncrementFailureCount(); |
| 86 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 87 | // Updates the backoff expiry time exponentially based on the current |
| 88 | // payload attempt number. |
| 89 | void UpdateBackoffExpiryTime(); |
| 90 | |
| 91 | // Resets all the persisted state values which are maintained relative to the |
| 92 | // current response signature. The response signature itself is not reset. |
| 93 | void ResetPersistedState(); |
| 94 | |
| 95 | // Calculates the response "signature", which is basically a string composed |
| 96 | // of the subset of the fields in the current response that affect the |
| 97 | // behavior of the PayloadState. |
| 98 | std::string CalculateResponseSignature(); |
| 99 | |
| 100 | // Initializes the current response signature from the persisted state. |
| 101 | void LoadResponseSignature(); |
| 102 | |
| 103 | // Sets the response signature to the given value. Also persists the value |
| 104 | // being set so that we resume from the save value in case of a process |
| 105 | // restart. |
| 106 | void SetResponseSignature(std::string response_signature); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 107 | |
| 108 | // Initializes the payload attempt number from the persisted state. |
| 109 | void LoadPayloadAttemptNumber(); |
| 110 | |
| 111 | // Sets the payload attempt number to the given value. Also persists the |
| 112 | // value being set so that we resume from the same value in case of a process |
| 113 | // restart. |
| 114 | void SetPayloadAttemptNumber(uint32_t payload_attempt_number); |
| 115 | |
| 116 | // Initializes the current URL index from the persisted state. |
| 117 | void LoadUrlIndex(); |
| 118 | |
| 119 | // Sets the current URL index to the given value. Also persists the value |
| 120 | // being set so that we resume from the same value in case of a process |
| 121 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 122 | void SetUrlIndex(uint32_t url_index); |
| 123 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 124 | // Initializes the current URL's failure count from the persisted stae. |
| 125 | void LoadUrlFailureCount(); |
| 126 | |
| 127 | // Sets the current URL's failure count to the given value. Also persists the |
| 128 | // value being set so that we resume from the same value in case of a process |
| 129 | // restart. |
| 130 | void SetUrlFailureCount(uint32_t url_failure_count); |
| 131 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 132 | // Initializes the backoff expiry time from the persisted state. |
| 133 | void LoadBackoffExpiryTime(); |
| 134 | |
| 135 | // Sets the backoff expiry time to the given value. Also persists the value |
| 136 | // being set so that we resume from the same value in case of a process |
| 137 | // restart. |
| 138 | void SetBackoffExpiryTime(const base::Time& new_time); |
| 139 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 140 | // Initializes |update_timestamp_start_| from the persisted state. |
| 141 | void LoadUpdateTimestampStart(); |
| 142 | |
| 143 | // Sets |update_timestamp_start_| to the given value and persists the value. |
| 144 | void SetUpdateTimestampStart(const base::Time& value); |
| 145 | |
| 146 | // Sets |update_timestamp_end_| to the given value. This is not persisted |
| 147 | // as it happens at the end of the update process where state is deleted |
| 148 | // anyway. |
| 149 | void SetUpdateTimestampEnd(const base::Time& value); |
| 150 | |
| 151 | // Initializes |update_duration_uptime_| from the persisted state. |
| 152 | void LoadUpdateDurationUptime(); |
| 153 | |
| 154 | // Helper method used in SetUpdateDurationUptime() and |
| 155 | // CalculateUpdateDurationUptime(). |
| 156 | void SetUpdateDurationUptimeExtended(const base::TimeDelta& value, |
| 157 | const base::Time& timestamp, |
| 158 | bool use_logging); |
| 159 | |
| 160 | // Sets |update_duration_uptime_| to the given value and persists |
| 161 | // the value and sets |update_duration_uptime_timestamp_| to the |
| 162 | // current monotonic time. |
| 163 | void SetUpdateDurationUptime(const base::TimeDelta& value); |
| 164 | |
| 165 | // Adds the difference between current monotonic time and |
| 166 | // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and |
| 167 | // sets |update_duration_uptime_timestamp_| to current monotonic time. |
| 168 | void CalculateUpdateDurationUptime(); |
| 169 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 170 | // Interface object with which we read/write persisted state. This must |
| 171 | // be set by calling the Initialize method before calling any other method. |
| 172 | PrefsInterface* prefs_; |
| 173 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 174 | // This is the current response object from Omaha. |
| 175 | OmahaResponse response_; |
| 176 | |
| 177 | // This stores a "signature" of the current response. The signature here |
| 178 | // refers to a subset of the current response from Omaha. Each update to |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 179 | // this value is persisted so we resume from the same value in case of a |
| 180 | // process restart. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 181 | std::string response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 182 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 183 | // The number of times we've tried to download the payload in full. This is |
| 184 | // incremented each time we download the payload in full successsfully or |
| 185 | // when we exhaust all failure limits for all URLs and are about to wrap |
| 186 | // around back to the first URL. Each update to this value is persisted so |
| 187 | // we resume from the same value in case of a process restart. |
| 188 | uint32_t payload_attempt_number_; |
| 189 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 190 | // The index of the current URL. This type is different from the one in the |
| 191 | // accessor methods because PrefsInterface supports only int64_t but we want |
| 192 | // to provide a stronger abstraction of uint32_t. Each update to this value |
| 193 | // is persisted so we resume from the same value in case of a process |
| 194 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 195 | int64_t url_index_; |
| 196 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 197 | // The count of failures encountered in the current attempt to download using |
| 198 | // the current URL (specified by url_index_). Each update to this value is |
| 199 | // persisted so we resume from the same value in case of a process restart. |
| 200 | int64_t url_failure_count_; |
| 201 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 202 | // The timestamp until which we've to wait before attempting to download the |
| 203 | // payload again, so as to backoff repeated downloads. |
| 204 | base::Time backoff_expiry_time_; |
| 205 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 206 | // The most recently calculated value of the update duration. |
| 207 | base::TimeDelta update_duration_current_; |
| 208 | |
| 209 | // The point in time (wall-clock) that the update was started. |
| 210 | base::Time update_timestamp_start_; |
| 211 | |
| 212 | // The point in time (wall-clock) that the update ended. If the update |
| 213 | // is still in progress, this is set to the Epoch (e.g. 0). |
| 214 | base::Time update_timestamp_end_; |
| 215 | |
| 216 | // The update duration uptime |
| 217 | base::TimeDelta update_duration_uptime_; |
| 218 | |
| 219 | // The monotonic time when |update_duration_uptime_| was last set |
| 220 | base::Time update_duration_uptime_timestamp_; |
| 221 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 222 | // Returns the number of URLs in the current response. |
| 223 | // Note: This value will be 0 if this method is called before we receive |
| 224 | // the first valid Omaha response in this process. |
| 225 | uint32_t GetNumUrls() { |
| 226 | return response_.payload_urls.size(); |
| 227 | } |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 228 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 229 | // A small timespan used when comparing wall-clock times for coping |
| 230 | // with the fact that clocks drift and consequently are adjusted |
| 231 | // (either forwards or backwards) via NTP. |
| 232 | static const base::TimeDelta kDurationSlack; |
| 233 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 234 | DISALLOW_COPY_AND_ASSIGN(PayloadState); |
| 235 | }; |
| 236 | |
| 237 | } // namespace chromeos_update_engine |
| 238 | |
| 239 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__ |