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 | #include "update_engine/payload_state.h" |
| 6 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 9 | #include <base/logging.h> |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 10 | #include "base/string_util.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
| 12 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 13 | #include "update_engine/clock.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 14 | #include "update_engine/constants.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 15 | #include "update_engine/prefs.h" |
| 16 | #include "update_engine/system_state.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 18 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 19 | using base::Time; |
| 20 | using base::TimeDelta; |
| 21 | using std::min; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | using std::string; |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 26 | const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600); |
| 27 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 28 | // We want to upperbound backoffs to 16 days |
| 29 | static const uint32_t kMaxBackoffDays = 16; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 30 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 31 | // We want to randomize retry attempts after the backoff by +/- 6 hours. |
| 32 | static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 33 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 34 | PayloadState::PayloadState() |
| 35 | : prefs_(NULL), |
| 36 | payload_attempt_number_(0), |
| 37 | url_index_(0), |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 38 | url_failure_count_(0), |
| 39 | url_switch_count_(0) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 40 | for (int i = 0; i <= kNumDownloadSources; i++) |
| 41 | total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0; |
| 42 | } |
| 43 | |
| 44 | bool PayloadState::Initialize(SystemState* system_state) { |
| 45 | system_state_ = system_state; |
| 46 | prefs_ = system_state_->prefs(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 47 | LoadResponseSignature(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 48 | LoadPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 49 | LoadUrlIndex(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 50 | LoadUrlFailureCount(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 51 | LoadUrlSwitchCount(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 52 | LoadBackoffExpiryTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 53 | LoadUpdateTimestampStart(); |
| 54 | // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart() |
| 55 | // being called before it. Don't reorder. |
| 56 | LoadUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 57 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 58 | DownloadSource source = static_cast<DownloadSource>(i); |
| 59 | LoadCurrentBytesDownloaded(source); |
| 60 | LoadTotalBytesDownloaded(source); |
| 61 | } |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 62 | LoadNumReboots(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 63 | LoadNumResponsesSeen(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 67 | void PayloadState::SetResponse(const OmahaResponse& omaha_response) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 68 | // Always store the latest response. |
| 69 | response_ = omaha_response; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 70 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 71 | // Compute the candidate URLs first as they are used to calculate the |
| 72 | // response signature so that a change in enterprise policy for |
| 73 | // HTTP downloads being enabled or not could be honored as soon as the |
| 74 | // next update check happens. |
| 75 | ComputeCandidateUrls(); |
| 76 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 77 | // Check if the "signature" of this response (i.e. the fields we care about) |
| 78 | // has changed. |
| 79 | string new_response_signature = CalculateResponseSignature(); |
| 80 | bool has_response_changed = (response_signature_ != new_response_signature); |
| 81 | |
| 82 | // If the response has changed, we should persist the new signature and |
| 83 | // clear away all the existing state. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 84 | if (has_response_changed) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 85 | LOG(INFO) << "Resetting all persisted state as this is a new response"; |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 86 | SetNumResponsesSeen(num_responses_seen_ + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 87 | SetResponseSignature(new_response_signature); |
| 88 | ResetPersistedState(); |
| 89 | return; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 92 | // This is the earliest point at which we can validate whether the URL index |
| 93 | // we loaded from the persisted state is a valid value. If the response |
| 94 | // hasn't changed but the URL index is invalid, it's indicative of some |
| 95 | // tampering of the persisted state. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 96 | if (static_cast<uint32_t>(url_index_) >= candidate_urls_.size()) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 97 | LOG(INFO) << "Resetting all payload state as the url index seems to have " |
| 98 | "been tampered with"; |
| 99 | ResetPersistedState(); |
| 100 | return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 101 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 102 | |
| 103 | // Update the current download source which depends on the latest value of |
| 104 | // the response. |
| 105 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 108 | void PayloadState::DownloadComplete() { |
| 109 | LOG(INFO) << "Payload downloaded successfully"; |
| 110 | IncrementPayloadAttemptNumber(); |
| 111 | } |
| 112 | |
| 113 | void PayloadState::DownloadProgress(size_t count) { |
| 114 | if (count == 0) |
| 115 | return; |
| 116 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 117 | CalculateUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 118 | UpdateBytesDownloaded(count); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 119 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 120 | // We've received non-zero bytes from a recent download operation. Since our |
| 121 | // URL failure count is meant to penalize a URL only for consecutive |
| 122 | // failures, downloading bytes successfully means we should reset the failure |
| 123 | // count (as we know at least that the URL is working). In future, we can |
| 124 | // design this to be more sophisticated to check for more intelligent failure |
| 125 | // patterns, but right now, even 1 byte downloaded will mark the URL to be |
| 126 | // good unless it hits 10 (or configured number of) consecutive failures |
| 127 | // again. |
| 128 | |
| 129 | if (GetUrlFailureCount() == 0) |
| 130 | return; |
| 131 | |
| 132 | LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex() |
| 133 | << " to 0 as we received " << count << " bytes successfully"; |
| 134 | SetUrlFailureCount(0); |
| 135 | } |
| 136 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 137 | void PayloadState::UpdateResumed() { |
| 138 | LOG(INFO) << "Resuming an update that was previously started."; |
| 139 | UpdateNumReboots(); |
| 140 | } |
| 141 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 142 | void PayloadState::UpdateRestarted() { |
| 143 | LOG(INFO) << "Starting a new update"; |
| 144 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 145 | SetNumReboots(0); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 148 | void PayloadState::UpdateSucceeded() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 149 | // Send the relevant metrics that are tracked in this class to UMA. |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 150 | CalculateUpdateDurationUptime(); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 151 | SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 152 | ReportBytesDownloadedMetrics(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 153 | ReportUpdateUrlSwitchesMetric(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 154 | ReportRebootMetrics(); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 155 | ReportDurationMetrics(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 156 | ReportUpdatesAbandonedCountMetric(); |
| 157 | |
| 158 | // Reset the number of responses seen since it counts from the last |
| 159 | // successful update, e.g. now. |
| 160 | SetNumResponsesSeen(0); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 163 | void PayloadState::UpdateFailed(ErrorCode error) { |
| 164 | ErrorCode base_error = utils::GetBaseErrorCode(error); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 165 | LOG(INFO) << "Updating payload state for error code: " << base_error |
| 166 | << " (" << utils::CodeToString(base_error) << ")"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 167 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 168 | if (candidate_urls_.size() == 0) { |
| 169 | // This means we got this error even before we got a valid Omaha response |
| 170 | // or don't have any valid candidates in the Omaha response. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 171 | // So we should not advance the url_index_ in such cases. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 172 | LOG(INFO) << "Ignoring failures until we get a valid Omaha response."; |
| 173 | return; |
| 174 | } |
| 175 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 176 | switch (base_error) { |
| 177 | // Errors which are good indicators of a problem with a particular URL or |
| 178 | // the protocol used in the URL or entities in the communication channel |
| 179 | // (e.g. proxies). We should try the next available URL in the next update |
| 180 | // check to quickly recover from these errors. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 181 | case kErrorCodePayloadHashMismatchError: |
| 182 | case kErrorCodePayloadSizeMismatchError: |
| 183 | case kErrorCodeDownloadPayloadVerificationError: |
| 184 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 185 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 186 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 187 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 188 | case kErrorCodeDownloadManifestParseError: |
| 189 | case kErrorCodeDownloadMetadataSignatureError: |
| 190 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 191 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 192 | case kErrorCodeDownloadOperationHashVerificationError: |
| 193 | case kErrorCodeDownloadOperationExecutionError: |
| 194 | case kErrorCodeDownloadOperationHashMismatch: |
| 195 | case kErrorCodeDownloadInvalidMetadataSize: |
| 196 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 197 | case kErrorCodeDownloadOperationHashMissingError: |
| 198 | case kErrorCodeDownloadMetadataSignatureMissingError: |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 199 | case kErrorCodePayloadMismatchedType: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 200 | IncrementUrlIndex(); |
| 201 | break; |
| 202 | |
| 203 | // Errors which seem to be just transient network/communication related |
| 204 | // failures and do not indicate any inherent problem with the URL itself. |
| 205 | // So, we should keep the current URL but just increment the |
| 206 | // failure count to give it more chances. This way, while we maximize our |
| 207 | // chances of downloading from the URLs that appear earlier in the response |
| 208 | // (because download from a local server URL that appears earlier in a |
| 209 | // response is preferable than downloading from the next URL which could be |
| 210 | // a internet URL and thus could be more expensive). |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 211 | case kErrorCodeError: |
| 212 | case kErrorCodeDownloadTransferError: |
| 213 | case kErrorCodeDownloadWriteError: |
| 214 | case kErrorCodeDownloadStateInitializationError: |
| 215 | case kErrorCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 216 | IncrementFailureCount(); |
| 217 | break; |
| 218 | |
| 219 | // Errors which are not specific to a URL and hence shouldn't result in |
| 220 | // the URL being penalized. This can happen in two cases: |
| 221 | // 1. We haven't started downloading anything: These errors don't cost us |
| 222 | // anything in terms of actual payload bytes, so we should just do the |
| 223 | // regular retries at the next update check. |
| 224 | // 2. We have successfully downloaded the payload: In this case, the |
| 225 | // payload attempt number would have been incremented and would take care |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 226 | // of the backoff at the next update check. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 227 | // In either case, there's no need to update URL index or failure count. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 228 | case kErrorCodeOmahaRequestError: |
| 229 | case kErrorCodeOmahaResponseHandlerError: |
| 230 | case kErrorCodePostinstallRunnerError: |
| 231 | case kErrorCodeFilesystemCopierError: |
| 232 | case kErrorCodeInstallDeviceOpenError: |
| 233 | case kErrorCodeKernelDeviceOpenError: |
| 234 | case kErrorCodeDownloadNewPartitionInfoError: |
| 235 | case kErrorCodeNewRootfsVerificationError: |
| 236 | case kErrorCodeNewKernelVerificationError: |
| 237 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 238 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 239 | case kErrorCodeOmahaRequestXMLParseError: |
| 240 | case kErrorCodeOmahaResponseInvalid: |
| 241 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 242 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 243 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 244 | case kErrorCodePostinstallPowerwashError: |
| 245 | case kErrorCodeUpdateCanceledByChannelChange: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 246 | LOG(INFO) << "Not incrementing URL index or failure count for this error"; |
| 247 | break; |
| 248 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 249 | case kErrorCodeSuccess: // success code |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 250 | case kErrorCodeUmaReportedMax: // not an error code |
| 251 | case kErrorCodeOmahaRequestHTTPResponseBase: // aggregated already |
| 252 | case kErrorCodeDevModeFlag: // not an error code |
| 253 | case kErrorCodeResumedFlag: // not an error code |
| 254 | case kErrorCodeTestImageFlag: // not an error code |
| 255 | case kErrorCodeTestOmahaUrlFlag: // not an error code |
| 256 | case kErrorCodeSpecialFlags: // not an error code |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 257 | // These shouldn't happen. Enumerating these explicitly here so that we |
| 258 | // can let the compiler warn about new error codes that are added to |
| 259 | // action_processor.h but not added here. |
| 260 | LOG(WARNING) << "Unexpected error code for UpdateFailed"; |
| 261 | break; |
| 262 | |
| 263 | // Note: Not adding a default here so as to let the compiler warn us of |
| 264 | // any new enums that were added in the .h but not listed in this switch. |
| 265 | } |
| 266 | } |
| 267 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 268 | bool PayloadState::ShouldBackoffDownload() { |
| 269 | if (response_.disable_payload_backoff) { |
| 270 | LOG(INFO) << "Payload backoff logic is disabled. " |
| 271 | "Can proceed with the download"; |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (response_.is_delta_payload) { |
| 276 | // If delta payloads fail, we want to fallback quickly to full payloads as |
| 277 | // they are more likely to succeed. Exponential backoffs would greatly |
| 278 | // slow down the fallback to full payloads. So we don't backoff for delta |
| 279 | // payloads. |
| 280 | LOG(INFO) << "No backoffs for delta payloads. " |
| 281 | << "Can proceed with the download"; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | if (!utils::IsOfficialBuild()) { |
| 286 | // Backoffs are needed only for official builds. We do not want any delays |
| 287 | // or update failures due to backoffs during testing or development. |
| 288 | LOG(INFO) << "No backoffs for test/dev images. " |
| 289 | << "Can proceed with the download"; |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | if (backoff_expiry_time_.is_null()) { |
| 294 | LOG(INFO) << "No backoff expiry time has been set. " |
| 295 | << "Can proceed with the download"; |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | if (backoff_expiry_time_ < Time::Now()) { |
| 300 | LOG(INFO) << "The backoff expiry time (" |
| 301 | << utils::ToString(backoff_expiry_time_) |
| 302 | << ") has elapsed. Can proceed with the download"; |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | LOG(INFO) << "Cannot proceed with downloads as we need to backoff until " |
| 307 | << utils::ToString(backoff_expiry_time_); |
| 308 | return true; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void PayloadState::IncrementPayloadAttemptNumber() { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 312 | if (response_.is_delta_payload) { |
| 313 | LOG(INFO) << "Not incrementing payload attempt number for delta payloads"; |
| 314 | return; |
| 315 | } |
| 316 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 317 | LOG(INFO) << "Incrementing the payload attempt number"; |
| 318 | SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 319 | UpdateBackoffExpiryTime(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void PayloadState::IncrementUrlIndex() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 323 | uint32_t next_url_index = GetUrlIndex() + 1; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 324 | if (next_url_index < candidate_urls_.size()) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 325 | LOG(INFO) << "Incrementing the URL index for next attempt"; |
| 326 | SetUrlIndex(next_url_index); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 327 | } else { |
| 328 | LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to " |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 329 | << "0 as we only have " << candidate_urls_.size() |
| 330 | << " candidate URL(s)"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 331 | SetUrlIndex(0); |
| 332 | IncrementPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 333 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 334 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 335 | // If we have multiple URLs, record that we just switched to another one |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 336 | if (candidate_urls_.size() > 1) |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 337 | SetUrlSwitchCount(url_switch_count_ + 1); |
| 338 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 339 | // Whenever we update the URL index, we should also clear the URL failure |
| 340 | // count so we can start over fresh for the new URL. |
| 341 | SetUrlFailureCount(0); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 344 | void PayloadState::IncrementFailureCount() { |
| 345 | uint32_t next_url_failure_count = GetUrlFailureCount() + 1; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 346 | if (next_url_failure_count < response_.max_failure_count_per_url) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 347 | LOG(INFO) << "Incrementing the URL failure count"; |
| 348 | SetUrlFailureCount(next_url_failure_count); |
| 349 | } else { |
| 350 | LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex() |
| 351 | << ". Trying next available URL"; |
| 352 | IncrementUrlIndex(); |
| 353 | } |
| 354 | } |
| 355 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 356 | void PayloadState::UpdateBackoffExpiryTime() { |
| 357 | if (response_.disable_payload_backoff) { |
| 358 | LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled"; |
| 359 | SetBackoffExpiryTime(Time()); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | if (GetPayloadAttemptNumber() == 0) { |
| 364 | SetBackoffExpiryTime(Time()); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | // Since we're doing left-shift below, make sure we don't shift more |
| 369 | // than this. E.g. if uint32_t is 4-bytes, don't left-shift more than 30 bits, |
| 370 | // since we don't expect value of kMaxBackoffDays to be more than 100 anyway. |
| 371 | uint32_t num_days = 1; // the value to be shifted. |
| 372 | const uint32_t kMaxShifts = (sizeof(num_days) * 8) - 2; |
| 373 | |
| 374 | // Normal backoff days is 2 raised to (payload_attempt_number - 1). |
| 375 | // E.g. if payload_attempt_number is over 30, limit power to 30. |
| 376 | uint32_t power = min(GetPayloadAttemptNumber() - 1, kMaxShifts); |
| 377 | |
| 378 | // The number of days is the minimum of 2 raised to (payload_attempt_number |
| 379 | // - 1) or kMaxBackoffDays. |
| 380 | num_days = min(num_days << power, kMaxBackoffDays); |
| 381 | |
| 382 | // We don't want all retries to happen exactly at the same time when |
| 383 | // retrying after backoff. So add some random minutes to fuzz. |
| 384 | int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes); |
| 385 | TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) + |
| 386 | TimeDelta::FromMinutes(fuzz_minutes); |
| 387 | LOG(INFO) << "Incrementing the backoff expiry time by " |
| 388 | << utils::FormatTimeDelta(next_backoff_interval); |
| 389 | SetBackoffExpiryTime(Time::Now() + next_backoff_interval); |
| 390 | } |
| 391 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 392 | void PayloadState::UpdateCurrentDownloadSource() { |
| 393 | current_download_source_ = kNumDownloadSources; |
| 394 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 395 | if (GetUrlIndex() < candidate_urls_.size()) { |
| 396 | string current_url = candidate_urls_[GetUrlIndex()]; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 397 | if (StartsWithASCII(current_url, "https://", false)) |
| 398 | current_download_source_ = kDownloadSourceHttpsServer; |
| 399 | else if (StartsWithASCII(current_url, "http://", false)) |
| 400 | current_download_source_ = kDownloadSourceHttpServer; |
| 401 | } |
| 402 | |
| 403 | LOG(INFO) << "Current download source: " |
| 404 | << utils::ToString(current_download_source_); |
| 405 | } |
| 406 | |
| 407 | void PayloadState::UpdateBytesDownloaded(size_t count) { |
| 408 | SetCurrentBytesDownloaded( |
| 409 | current_download_source_, |
| 410 | GetCurrentBytesDownloaded(current_download_source_) + count, |
| 411 | false); |
| 412 | SetTotalBytesDownloaded( |
| 413 | current_download_source_, |
| 414 | GetTotalBytesDownloaded(current_download_source_) + count, |
| 415 | false); |
| 416 | } |
| 417 | |
| 418 | void PayloadState::ReportBytesDownloadedMetrics() { |
| 419 | // Report metrics collected from all known download sources to UMA. |
| 420 | // The reported data is in Megabytes in order to represent a larger |
| 421 | // sample range. |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 422 | int download_sources_used = 0; |
| 423 | string metric; |
| 424 | uint64_t successful_mbs = 0; |
| 425 | uint64_t total_mbs = 0; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 426 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 427 | DownloadSource source = static_cast<DownloadSource>(i); |
| 428 | const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket. |
| 429 | |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 430 | metric = "Installer.SuccessfulMBsDownloadedFrom" + utils::ToString(source); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 431 | uint64_t mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB; |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 432 | |
| 433 | // Count this download source as having been used if we downloaded any |
| 434 | // bytes that contributed to the final success of the update. |
| 435 | if (mbs) |
| 436 | download_sources_used |= (1 << source); |
| 437 | |
| 438 | successful_mbs += mbs; |
| 439 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 440 | system_state_->metrics_lib()->SendToUMA(metric, |
| 441 | mbs, |
| 442 | 0, // min |
| 443 | kMaxMiBs, |
| 444 | kNumDefaultUmaBuckets); |
| 445 | SetCurrentBytesDownloaded(source, 0, true); |
| 446 | |
| 447 | metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source); |
| 448 | mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB; |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 449 | total_mbs += mbs; |
| 450 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 451 | system_state_->metrics_lib()->SendToUMA(metric, |
| 452 | mbs, |
| 453 | 0, // min |
| 454 | kMaxMiBs, |
| 455 | kNumDefaultUmaBuckets); |
| 456 | |
| 457 | SetTotalBytesDownloaded(source, 0, true); |
| 458 | } |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 459 | |
| 460 | metric = "Installer.DownloadSourcesUsed"; |
| 461 | LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used |
| 462 | << " (bit flags) for metric " << metric; |
| 463 | int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets); |
| 464 | system_state_->metrics_lib()->SendToUMA(metric, |
| 465 | download_sources_used, |
| 466 | 0, // min |
| 467 | 1 << kNumDownloadSources, |
| 468 | num_buckets); |
| 469 | |
| 470 | if (successful_mbs) { |
| 471 | metric = "Installer.DownloadOverheadPercentage"; |
| 472 | int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs; |
| 473 | LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric; |
| 474 | system_state_->metrics_lib()->SendToUMA(metric, |
| 475 | percent_overhead, |
| 476 | 0, // min: 0% overhead |
| 477 | 1000, // max: 1000% overhead |
| 478 | kNumDefaultUmaBuckets); |
| 479 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 482 | void PayloadState::ReportUpdateUrlSwitchesMetric() { |
| 483 | string metric = "Installer.UpdateURLSwitches"; |
| 484 | int value = static_cast<int>(url_switch_count_); |
| 485 | |
| 486 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 487 | system_state_->metrics_lib()->SendToUMA( |
| 488 | metric, |
| 489 | value, |
| 490 | 0, // min value |
| 491 | 100, // max value |
| 492 | kNumDefaultUmaBuckets); |
| 493 | } |
| 494 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 495 | void PayloadState::ReportRebootMetrics() { |
| 496 | // Report the number of num_reboots. |
| 497 | string metric = "Installer.UpdateNumReboots"; |
| 498 | uint32_t num_reboots = GetNumReboots(); |
| 499 | LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric " |
| 500 | << metric; |
| 501 | system_state_->metrics_lib()->SendToUMA( |
| 502 | metric, |
| 503 | static_cast<int>(num_reboots), // sample |
| 504 | 0, // min = 0. |
| 505 | 50, // max |
| 506 | 25); // buckets |
| 507 | SetNumReboots(0); |
| 508 | } |
| 509 | |
| 510 | void PayloadState::UpdateNumReboots() { |
| 511 | // We only update the reboot count when the system has been detected to have |
| 512 | // been rebooted. |
| 513 | if (!system_state_->system_rebooted()) { |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | SetNumReboots(GetNumReboots() + 1); |
| 518 | } |
| 519 | |
| 520 | void PayloadState::SetNumReboots(uint32_t num_reboots) { |
| 521 | CHECK(prefs_); |
| 522 | num_reboots_ = num_reboots; |
| 523 | prefs_->SetInt64(kPrefsNumReboots, num_reboots); |
| 524 | LOG(INFO) << "Number of Reboots during current update attempt = " |
| 525 | << num_reboots_; |
| 526 | } |
| 527 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 528 | void PayloadState::ResetPersistedState() { |
| 529 | SetPayloadAttemptNumber(0); |
| 530 | SetUrlIndex(0); |
| 531 | SetUrlFailureCount(0); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 532 | SetUrlSwitchCount(0); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 533 | UpdateBackoffExpiryTime(); // This will reset the backoff expiry time. |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 534 | SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime()); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 535 | SetUpdateTimestampEnd(Time()); // Set to null time |
| 536 | SetUpdateDurationUptime(TimeDelta::FromSeconds(0)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 537 | ResetDownloadSourcesOnNewUpdate(); |
| 538 | } |
| 539 | |
| 540 | void PayloadState::ResetDownloadSourcesOnNewUpdate() { |
| 541 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 542 | DownloadSource source = static_cast<DownloadSource>(i); |
| 543 | SetCurrentBytesDownloaded(source, 0, true); |
| 544 | // Note: Not resetting the TotalBytesDownloaded as we want that metric |
| 545 | // to count the bytes downloaded across various update attempts until |
| 546 | // we have successfully applied the update. |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | int64_t PayloadState::GetPersistedValue(const string& key) { |
| 551 | CHECK(prefs_); |
| 552 | if (!prefs_->Exists(key)) |
| 553 | return 0; |
| 554 | |
| 555 | int64_t stored_value; |
| 556 | if (!prefs_->GetInt64(key, &stored_value)) |
| 557 | return 0; |
| 558 | |
| 559 | if (stored_value < 0) { |
| 560 | LOG(ERROR) << key << ": Invalid value (" << stored_value |
| 561 | << ") in persisted state. Defaulting to 0"; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | return stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | string PayloadState::CalculateResponseSignature() { |
| 569 | string response_sign = StringPrintf("NumURLs = %d\n", |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 570 | candidate_urls_.size()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 571 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 572 | for (size_t i = 0; i < candidate_urls_.size(); i++) |
| 573 | response_sign += StringPrintf("Candidate Url%d = %s\n", |
| 574 | i, candidate_urls_[i].c_str()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 575 | |
| 576 | response_sign += StringPrintf("Payload Size = %llu\n" |
| 577 | "Payload Sha256 Hash = %s\n" |
| 578 | "Metadata Size = %llu\n" |
| 579 | "Metadata Signature = %s\n" |
| 580 | "Is Delta Payload = %d\n" |
| 581 | "Max Failure Count Per Url = %d\n" |
| 582 | "Disable Payload Backoff = %d\n", |
| 583 | response_.size, |
| 584 | response_.hash.c_str(), |
| 585 | response_.metadata_size, |
| 586 | response_.metadata_signature.c_str(), |
| 587 | response_.is_delta_payload, |
| 588 | response_.max_failure_count_per_url, |
| 589 | response_.disable_payload_backoff); |
| 590 | return response_sign; |
| 591 | } |
| 592 | |
| 593 | void PayloadState::LoadResponseSignature() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 594 | CHECK(prefs_); |
| 595 | string stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 596 | if (prefs_->Exists(kPrefsCurrentResponseSignature) && |
| 597 | prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) { |
| 598 | SetResponseSignature(stored_value); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 599 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 602 | void PayloadState::SetResponseSignature(const string& response_signature) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 603 | CHECK(prefs_); |
| 604 | response_signature_ = response_signature; |
| 605 | LOG(INFO) << "Current Response Signature = \n" << response_signature_; |
| 606 | prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_); |
| 607 | } |
| 608 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 609 | void PayloadState::LoadPayloadAttemptNumber() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 610 | SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void PayloadState::SetPayloadAttemptNumber(uint32_t payload_attempt_number) { |
| 614 | CHECK(prefs_); |
| 615 | payload_attempt_number_ = payload_attempt_number; |
| 616 | LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_; |
| 617 | prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_); |
| 618 | } |
| 619 | |
| 620 | void PayloadState::LoadUrlIndex() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 621 | SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void PayloadState::SetUrlIndex(uint32_t url_index) { |
| 625 | CHECK(prefs_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 626 | url_index_ = url_index; |
| 627 | LOG(INFO) << "Current URL Index = " << url_index_; |
| 628 | prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 629 | |
| 630 | // Also update the download source, which is purely dependent on the |
| 631 | // current URL index alone. |
| 632 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 633 | } |
| 634 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 635 | void PayloadState::LoadUrlSwitchCount() { |
| 636 | SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount)); |
| 637 | } |
| 638 | |
| 639 | void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) { |
| 640 | CHECK(prefs_); |
| 641 | url_switch_count_ = url_switch_count; |
| 642 | LOG(INFO) << "URL Switch Count = " << url_switch_count_; |
| 643 | prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_); |
| 644 | } |
| 645 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 646 | void PayloadState::LoadUrlFailureCount() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 647 | SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) { |
| 651 | CHECK(prefs_); |
| 652 | url_failure_count_ = url_failure_count; |
| 653 | LOG(INFO) << "Current URL (Url" << GetUrlIndex() |
| 654 | << ")'s Failure Count = " << url_failure_count_; |
| 655 | prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 658 | void PayloadState::LoadBackoffExpiryTime() { |
| 659 | CHECK(prefs_); |
| 660 | int64_t stored_value; |
| 661 | if (!prefs_->Exists(kPrefsBackoffExpiryTime)) |
| 662 | return; |
| 663 | |
| 664 | if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value)) |
| 665 | return; |
| 666 | |
| 667 | Time stored_time = Time::FromInternalValue(stored_value); |
| 668 | if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) { |
| 669 | LOG(ERROR) << "Invalid backoff expiry time (" |
| 670 | << utils::ToString(stored_time) |
| 671 | << ") in persisted state. Resetting."; |
| 672 | stored_time = Time(); |
| 673 | } |
| 674 | SetBackoffExpiryTime(stored_time); |
| 675 | } |
| 676 | |
| 677 | void PayloadState::SetBackoffExpiryTime(const Time& new_time) { |
| 678 | CHECK(prefs_); |
| 679 | backoff_expiry_time_ = new_time; |
| 680 | LOG(INFO) << "Backoff Expiry Time = " |
| 681 | << utils::ToString(backoff_expiry_time_); |
| 682 | prefs_->SetInt64(kPrefsBackoffExpiryTime, |
| 683 | backoff_expiry_time_.ToInternalValue()); |
| 684 | } |
| 685 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 686 | TimeDelta PayloadState::GetUpdateDuration() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 687 | Time end_time = update_timestamp_end_.is_null() |
| 688 | ? system_state_->clock()->GetWallclockTime() : |
| 689 | update_timestamp_end_; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 690 | return end_time - update_timestamp_start_; |
| 691 | } |
| 692 | |
| 693 | void PayloadState::LoadUpdateTimestampStart() { |
| 694 | int64_t stored_value; |
| 695 | Time stored_time; |
| 696 | |
| 697 | CHECK(prefs_); |
| 698 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 699 | Time now = system_state_->clock()->GetWallclockTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 700 | |
| 701 | if (!prefs_->Exists(kPrefsUpdateTimestampStart)) { |
| 702 | // The preference missing is not unexpected - in that case, just |
| 703 | // use the current time as start time |
| 704 | stored_time = now; |
| 705 | } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) { |
| 706 | LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting."; |
| 707 | stored_time = now; |
| 708 | } else { |
| 709 | stored_time = Time::FromInternalValue(stored_value); |
| 710 | } |
| 711 | |
| 712 | // Sanity check: If the time read from disk is in the future |
| 713 | // (modulo some slack to account for possible NTP drift |
| 714 | // adjustments), something is fishy and we should report and |
| 715 | // reset. |
| 716 | TimeDelta duration_according_to_stored_time = now - stored_time; |
| 717 | if (duration_according_to_stored_time < -kDurationSlack) { |
| 718 | LOG(ERROR) << "The UpdateTimestampStart value (" |
| 719 | << utils::ToString(stored_time) |
| 720 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 721 | << utils::FormatTimeDelta(duration_according_to_stored_time) |
| 722 | << " in the future. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 723 | stored_time = now; |
| 724 | } |
| 725 | |
| 726 | SetUpdateTimestampStart(stored_time); |
| 727 | } |
| 728 | |
| 729 | void PayloadState::SetUpdateTimestampStart(const Time& value) { |
| 730 | CHECK(prefs_); |
| 731 | update_timestamp_start_ = value; |
| 732 | prefs_->SetInt64(kPrefsUpdateTimestampStart, |
| 733 | update_timestamp_start_.ToInternalValue()); |
| 734 | LOG(INFO) << "Update Timestamp Start = " |
| 735 | << utils::ToString(update_timestamp_start_); |
| 736 | } |
| 737 | |
| 738 | void PayloadState::SetUpdateTimestampEnd(const Time& value) { |
| 739 | update_timestamp_end_ = value; |
| 740 | LOG(INFO) << "Update Timestamp End = " |
| 741 | << utils::ToString(update_timestamp_end_); |
| 742 | } |
| 743 | |
| 744 | TimeDelta PayloadState::GetUpdateDurationUptime() { |
| 745 | return update_duration_uptime_; |
| 746 | } |
| 747 | |
| 748 | void PayloadState::LoadUpdateDurationUptime() { |
| 749 | int64_t stored_value; |
| 750 | TimeDelta stored_delta; |
| 751 | |
| 752 | CHECK(prefs_); |
| 753 | |
| 754 | if (!prefs_->Exists(kPrefsUpdateDurationUptime)) { |
| 755 | // The preference missing is not unexpected - in that case, just |
| 756 | // we'll use zero as the delta |
| 757 | } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) { |
| 758 | LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting."; |
| 759 | stored_delta = TimeDelta::FromSeconds(0); |
| 760 | } else { |
| 761 | stored_delta = TimeDelta::FromInternalValue(stored_value); |
| 762 | } |
| 763 | |
| 764 | // Sanity-check: Uptime can never be greater than the wall-clock |
| 765 | // difference (modulo some slack). If it is, report and reset |
| 766 | // to the wall-clock difference. |
| 767 | TimeDelta diff = GetUpdateDuration() - stored_delta; |
| 768 | if (diff < -kDurationSlack) { |
| 769 | LOG(ERROR) << "The UpdateDurationUptime value (" |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 770 | << utils::FormatTimeDelta(stored_delta) |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 771 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 772 | << utils::FormatTimeDelta(diff) |
| 773 | << " larger than the wall-clock delta. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 774 | stored_delta = update_duration_current_; |
| 775 | } |
| 776 | |
| 777 | SetUpdateDurationUptime(stored_delta); |
| 778 | } |
| 779 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 780 | void PayloadState::LoadNumReboots() { |
| 781 | SetNumReboots(GetPersistedValue(kPrefsNumReboots)); |
| 782 | } |
| 783 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 784 | void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value, |
| 785 | const Time& timestamp, |
| 786 | bool use_logging) { |
| 787 | CHECK(prefs_); |
| 788 | update_duration_uptime_ = value; |
| 789 | update_duration_uptime_timestamp_ = timestamp; |
| 790 | prefs_->SetInt64(kPrefsUpdateDurationUptime, |
| 791 | update_duration_uptime_.ToInternalValue()); |
| 792 | if (use_logging) { |
| 793 | LOG(INFO) << "Update Duration Uptime = " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 794 | << utils::FormatTimeDelta(update_duration_uptime_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 799 | Time now = system_state_->clock()->GetMonotonicTime(); |
| 800 | SetUpdateDurationUptimeExtended(value, now, true); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | void PayloadState::CalculateUpdateDurationUptime() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 804 | Time now = system_state_->clock()->GetMonotonicTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 805 | TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_; |
| 806 | TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update; |
| 807 | // We're frequently called so avoid logging this write |
| 808 | SetUpdateDurationUptimeExtended(new_uptime, now, false); |
| 809 | } |
| 810 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 811 | void PayloadState::ReportDurationMetrics() { |
| 812 | TimeDelta duration = GetUpdateDuration(); |
| 813 | TimeDelta duration_uptime = GetUpdateDurationUptime(); |
| 814 | string metric; |
| 815 | |
| 816 | metric = "Installer.UpdateDurationMinutes"; |
| 817 | system_state_->metrics_lib()->SendToUMA( |
| 818 | metric, |
| 819 | static_cast<int>(duration.InMinutes()), |
| 820 | 1, // min: 1 minute |
| 821 | 365*24*60, // max: 1 year (approx) |
| 822 | kNumDefaultUmaBuckets); |
| 823 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration) |
| 824 | << " for metric " << metric; |
| 825 | |
| 826 | metric = "Installer.UpdateDurationUptimeMinutes"; |
| 827 | system_state_->metrics_lib()->SendToUMA( |
| 828 | metric, |
| 829 | static_cast<int>(duration_uptime.InMinutes()), |
| 830 | 1, // min: 1 minute |
| 831 | 30*24*60, // max: 1 month (approx) |
| 832 | kNumDefaultUmaBuckets); |
| 833 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime) |
| 834 | << " for metric " << metric; |
| 835 | |
| 836 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 837 | prefs_->Delete(kPrefsUpdateDurationUptime); |
| 838 | } |
| 839 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 840 | string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) { |
| 841 | return prefix + "-from-" + utils::ToString(source); |
| 842 | } |
| 843 | |
| 844 | void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) { |
| 845 | string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 846 | SetCurrentBytesDownloaded(source, GetPersistedValue(key), true); |
| 847 | } |
| 848 | |
| 849 | void PayloadState::SetCurrentBytesDownloaded( |
| 850 | DownloadSource source, |
| 851 | uint64_t current_bytes_downloaded, |
| 852 | bool log) { |
| 853 | CHECK(prefs_); |
| 854 | |
| 855 | if (source >= kNumDownloadSources) |
| 856 | return; |
| 857 | |
| 858 | // Update the in-memory value. |
| 859 | current_bytes_downloaded_[source] = current_bytes_downloaded; |
| 860 | |
| 861 | string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 862 | prefs_->SetInt64(prefs_key, current_bytes_downloaded); |
| 863 | LOG_IF(INFO, log) << "Current bytes downloaded for " |
| 864 | << utils::ToString(source) << " = " |
| 865 | << GetCurrentBytesDownloaded(source); |
| 866 | } |
| 867 | |
| 868 | void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) { |
| 869 | string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 870 | SetTotalBytesDownloaded(source, GetPersistedValue(key), true); |
| 871 | } |
| 872 | |
| 873 | void PayloadState::SetTotalBytesDownloaded( |
| 874 | DownloadSource source, |
| 875 | uint64_t total_bytes_downloaded, |
| 876 | bool log) { |
| 877 | CHECK(prefs_); |
| 878 | |
| 879 | if (source >= kNumDownloadSources) |
| 880 | return; |
| 881 | |
| 882 | // Update the in-memory value. |
| 883 | total_bytes_downloaded_[source] = total_bytes_downloaded; |
| 884 | |
| 885 | // Persist. |
| 886 | string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 887 | prefs_->SetInt64(prefs_key, total_bytes_downloaded); |
| 888 | LOG_IF(INFO, log) << "Total bytes downloaded for " |
| 889 | << utils::ToString(source) << " = " |
| 890 | << GetTotalBytesDownloaded(source); |
| 891 | } |
| 892 | |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 893 | void PayloadState::LoadNumResponsesSeen() { |
| 894 | SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen)); |
| 895 | } |
| 896 | |
| 897 | void PayloadState::SetNumResponsesSeen(int num_responses_seen) { |
| 898 | CHECK(prefs_); |
| 899 | num_responses_seen_ = num_responses_seen; |
| 900 | LOG(INFO) << "Num Responses Seen = " << num_responses_seen_; |
| 901 | prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_); |
| 902 | } |
| 903 | |
| 904 | void PayloadState::ReportUpdatesAbandonedCountMetric() { |
| 905 | string metric = "Installer.UpdatesAbandonedCount"; |
| 906 | int value = num_responses_seen_ - 1; |
| 907 | |
| 908 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 909 | system_state_->metrics_lib()->SendToUMA( |
| 910 | metric, |
| 911 | value, |
| 912 | 0, // min value |
| 913 | 100, // max value |
| 914 | kNumDefaultUmaBuckets); |
| 915 | } |
| 916 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 917 | void PayloadState::ComputeCandidateUrls() { |
| 918 | bool http_url_ok = false; |
| 919 | |
| 920 | if (system_state_->IsOfficialBuild()) { |
| 921 | const policy::DevicePolicy* policy = system_state_->device_policy(); |
| 922 | if (!(policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && |
| 923 | http_url_ok)) |
| 924 | LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy"; |
| 925 | } else { |
| 926 | LOG(INFO) << "Allowing HTTP downloads for unofficial builds"; |
| 927 | http_url_ok = true; |
| 928 | } |
| 929 | |
| 930 | candidate_urls_.clear(); |
| 931 | for (size_t i = 0; i < response_.payload_urls.size(); i++) { |
| 932 | string candidate_url = response_.payload_urls[i]; |
| 933 | if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok) |
| 934 | continue; |
| 935 | candidate_urls_.push_back(candidate_url); |
| 936 | LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1) |
| 937 | << ": " << candidate_url; |
| 938 | } |
| 939 | |
| 940 | LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs " |
| 941 | << "out of " << response_.payload_urls.size() << " URLs supplied"; |
| 942 | } |
| 943 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 944 | } // namespace chromeos_update_engine |