Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/payload_state.h" |
| 18 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 19 | #include <algorithm> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <string> |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 21 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/strings/string_util.h> |
| 24 | #include <base/strings/stringprintf.h> |
Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 25 | #include <metrics/metrics_library.h> |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 26 | #include <policy/device_policy.h> |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/clock.h" |
| 29 | #include "update_engine/common/constants.h" |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 30 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/hardware_interface.h" |
| 32 | #include "update_engine/common/prefs.h" |
| 33 | #include "update_engine/common/utils.h" |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 34 | #include "update_engine/connection_manager_interface.h" |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 35 | #include "update_engine/metrics_reporter_interface.h" |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 36 | #include "update_engine/metrics_utils.h" |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 37 | #include "update_engine/omaha_request_params.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 38 | #include "update_engine/payload_consumer/install_plan.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 39 | #include "update_engine/system_state.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 40 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 41 | using base::Time; |
| 42 | using base::TimeDelta; |
| 43 | using std::min; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 44 | using std::string; |
| 45 | |
| 46 | namespace chromeos_update_engine { |
| 47 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 48 | using metrics_utils::GetPersistedValue; |
| 49 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 50 | const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600); |
| 51 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 52 | // We want to upperbound backoffs to 16 days |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 53 | static const int kMaxBackoffDays = 16; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 54 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 55 | // We want to randomize retry attempts after the backoff by +/- 6 hours. |
| 56 | static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 57 | |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 58 | // Limit persisting current update duration uptime to once per second |
| 59 | static const uint64_t kUptimeResolution = 1; |
| 60 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 61 | PayloadState::PayloadState() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 62 | : prefs_(nullptr), |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 63 | using_p2p_for_downloading_(false), |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 64 | p2p_num_attempts_(0), |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 65 | payload_attempt_number_(0), |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 66 | full_payload_attempt_number_(0), |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 67 | url_index_(0), |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 68 | url_failure_count_(0), |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 69 | url_switch_count_(0), |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 70 | rollback_happened_(false), |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 71 | attempt_num_bytes_downloaded_(0), |
| 72 | attempt_connection_type_(metrics::ConnectionType::kUnknown), |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 73 | attempt_type_(AttemptType::kUpdate) { |
| 74 | for (int i = 0; i <= kNumDownloadSources; i++) |
| 75 | total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool PayloadState::Initialize(SystemState* system_state) { |
| 79 | system_state_ = system_state; |
| 80 | prefs_ = system_state_->prefs(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 81 | powerwash_safe_prefs_ = system_state_->powerwash_safe_prefs(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 82 | LoadResponseSignature(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 83 | LoadPayloadAttemptNumber(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 84 | LoadFullPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 85 | LoadUrlIndex(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 86 | LoadUrlFailureCount(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 87 | LoadUrlSwitchCount(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 88 | LoadBackoffExpiryTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 89 | LoadUpdateTimestampStart(); |
| 90 | // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart() |
| 91 | // being called before it. Don't reorder. |
| 92 | LoadUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 93 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 94 | DownloadSource source = static_cast<DownloadSource>(i); |
| 95 | LoadCurrentBytesDownloaded(source); |
| 96 | LoadTotalBytesDownloaded(source); |
| 97 | } |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 98 | LoadNumReboots(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 99 | LoadNumResponsesSeen(); |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 100 | LoadRollbackHappened(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 101 | LoadRollbackVersion(); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 102 | LoadP2PFirstAttemptTimestamp(); |
| 103 | LoadP2PNumAttempts(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 107 | void PayloadState::SetResponse(const OmahaResponse& omaha_response) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 108 | // Always store the latest response. |
| 109 | response_ = omaha_response; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 110 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 111 | // Compute the candidate URLs first as they are used to calculate the |
| 112 | // response signature so that a change in enterprise policy for |
| 113 | // HTTP downloads being enabled or not could be honored as soon as the |
| 114 | // next update check happens. |
| 115 | ComputeCandidateUrls(); |
| 116 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 117 | // Check if the "signature" of this response (i.e. the fields we care about) |
| 118 | // has changed. |
| 119 | string new_response_signature = CalculateResponseSignature(); |
| 120 | bool has_response_changed = (response_signature_ != new_response_signature); |
| 121 | |
| 122 | // If the response has changed, we should persist the new signature and |
| 123 | // clear away all the existing state. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 124 | if (has_response_changed) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 125 | LOG(INFO) << "Resetting all persisted state as this is a new response"; |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 126 | SetNumResponsesSeen(num_responses_seen_ + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 127 | SetResponseSignature(new_response_signature); |
| 128 | ResetPersistedState(); |
| 129 | return; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 132 | // Always start from payload index 0, even for resume, to download partition |
| 133 | // info from previous payloads. |
| 134 | payload_index_ = 0; |
| 135 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 136 | // This is the earliest point at which we can validate whether the URL index |
| 137 | // we loaded from the persisted state is a valid value. If the response |
| 138 | // hasn't changed but the URL index is invalid, it's indicative of some |
| 139 | // tampering of the persisted state. |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 140 | if (payload_index_ >= candidate_urls_.size() || |
| 141 | url_index_ >= candidate_urls_[payload_index_].size()) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 142 | LOG(INFO) << "Resetting all payload state as the url index seems to have " |
| 143 | "been tampered with"; |
| 144 | ResetPersistedState(); |
| 145 | return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 146 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 147 | |
| 148 | // Update the current download source which depends on the latest value of |
| 149 | // the response. |
| 150 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 151 | } |
| 152 | |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 153 | void PayloadState::SetUsingP2PForDownloading(bool value) { |
| 154 | using_p2p_for_downloading_ = value; |
| 155 | // Update the current download source which depends on whether we are |
| 156 | // using p2p or not. |
| 157 | UpdateCurrentDownloadSource(); |
| 158 | } |
| 159 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 160 | void PayloadState::DownloadComplete() { |
| 161 | LOG(INFO) << "Payload downloaded successfully"; |
| 162 | IncrementPayloadAttemptNumber(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 163 | IncrementFullPayloadAttemptNumber(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void PayloadState::DownloadProgress(size_t count) { |
| 167 | if (count == 0) |
| 168 | return; |
| 169 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 170 | CalculateUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 171 | UpdateBytesDownloaded(count); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 172 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 173 | // We've received non-zero bytes from a recent download operation. Since our |
| 174 | // URL failure count is meant to penalize a URL only for consecutive |
| 175 | // failures, downloading bytes successfully means we should reset the failure |
| 176 | // count (as we know at least that the URL is working). In future, we can |
| 177 | // design this to be more sophisticated to check for more intelligent failure |
| 178 | // patterns, but right now, even 1 byte downloaded will mark the URL to be |
| 179 | // good unless it hits 10 (or configured number of) consecutive failures |
| 180 | // again. |
| 181 | |
| 182 | if (GetUrlFailureCount() == 0) |
| 183 | return; |
| 184 | |
| 185 | LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex() |
| 186 | << " to 0 as we received " << count << " bytes successfully"; |
| 187 | SetUrlFailureCount(0); |
| 188 | } |
| 189 | |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 190 | void PayloadState::AttemptStarted(AttemptType attempt_type) { |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 191 | // Flush previous state from abnormal attempt failure, if any. |
| 192 | ReportAndClearPersistedAttemptMetrics(); |
| 193 | |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 194 | attempt_type_ = attempt_type; |
| 195 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 196 | ClockInterface *clock = system_state_->clock(); |
| 197 | attempt_start_time_boot_ = clock->GetBootTime(); |
| 198 | attempt_start_time_monotonic_ = clock->GetMonotonicTime(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 199 | attempt_num_bytes_downloaded_ = 0; |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 200 | |
| 201 | metrics::ConnectionType type; |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 202 | ConnectionType network_connection_type; |
| 203 | ConnectionTethering tethering; |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 204 | ConnectionManagerInterface* connection_manager = |
| 205 | system_state_->connection_manager(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 206 | if (!connection_manager->GetConnectionProperties(&network_connection_type, |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 207 | &tethering)) { |
| 208 | LOG(ERROR) << "Failed to determine connection type."; |
| 209 | type = metrics::ConnectionType::kUnknown; |
| 210 | } else { |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 211 | type = metrics_utils::GetConnectionType(network_connection_type, tethering); |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 212 | } |
| 213 | attempt_connection_type_ = type; |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 214 | |
| 215 | if (attempt_type == AttemptType::kUpdate) |
| 216 | PersistAttemptMetrics(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 219 | void PayloadState::UpdateResumed() { |
| 220 | LOG(INFO) << "Resuming an update that was previously started."; |
| 221 | UpdateNumReboots(); |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 222 | AttemptStarted(AttemptType::kUpdate); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 225 | void PayloadState::UpdateRestarted() { |
| 226 | LOG(INFO) << "Starting a new update"; |
| 227 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 228 | SetNumReboots(0); |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 229 | AttemptStarted(AttemptType::kUpdate); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 232 | void PayloadState::UpdateSucceeded() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 233 | // Send the relevant metrics that are tracked in this class to UMA. |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 234 | CalculateUpdateDurationUptime(); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 235 | SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime()); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 236 | |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 237 | switch (attempt_type_) { |
| 238 | case AttemptType::kUpdate: |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 239 | CollectAndReportAttemptMetrics(ErrorCode::kSuccess); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 240 | CollectAndReportSuccessfulUpdateMetrics(); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 241 | ClearPersistedAttemptMetrics(); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 242 | break; |
| 243 | |
| 244 | case AttemptType::kRollback: |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 245 | system_state_->metrics_reporter()->ReportRollbackMetrics( |
| 246 | metrics::RollbackResult::kSuccess); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 247 | break; |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 248 | } |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 249 | |
| 250 | // Reset the number of responses seen since it counts from the last |
| 251 | // successful update, e.g. now. |
| 252 | SetNumResponsesSeen(0); |
Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 253 | SetPayloadIndex(0); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 254 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 255 | metrics_utils::SetSystemUpdatedMarker(system_state_->clock(), prefs_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 258 | void PayloadState::UpdateFailed(ErrorCode error) { |
| 259 | ErrorCode base_error = utils::GetBaseErrorCode(error); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 260 | LOG(INFO) << "Updating payload state for error code: " << base_error |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 261 | << " (" << utils::ErrorCodeToString(base_error) << ")"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 262 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 263 | if (candidate_urls_.size() == 0) { |
| 264 | // This means we got this error even before we got a valid Omaha response |
| 265 | // or don't have any valid candidates in the Omaha response. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 266 | // So we should not advance the url_index_ in such cases. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 267 | LOG(INFO) << "Ignoring failures until we get a valid Omaha response."; |
| 268 | return; |
| 269 | } |
| 270 | |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 271 | switch (attempt_type_) { |
| 272 | case AttemptType::kUpdate: |
| 273 | CollectAndReportAttemptMetrics(base_error); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 274 | ClearPersistedAttemptMetrics(); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | case AttemptType::kRollback: |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 278 | system_state_->metrics_reporter()->ReportRollbackMetrics( |
| 279 | metrics::RollbackResult::kFailed); |
David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 280 | break; |
| 281 | } |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 282 | |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 283 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 284 | switch (base_error) { |
| 285 | // Errors which are good indicators of a problem with a particular URL or |
| 286 | // the protocol used in the URL or entities in the communication channel |
| 287 | // (e.g. proxies). We should try the next available URL in the next update |
| 288 | // check to quickly recover from these errors. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 289 | case ErrorCode::kPayloadHashMismatchError: |
| 290 | case ErrorCode::kPayloadSizeMismatchError: |
| 291 | case ErrorCode::kDownloadPayloadVerificationError: |
| 292 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 293 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 294 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 295 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 296 | case ErrorCode::kDownloadManifestParseError: |
| 297 | case ErrorCode::kDownloadMetadataSignatureError: |
| 298 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 299 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 300 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 301 | case ErrorCode::kDownloadOperationExecutionError: |
| 302 | case ErrorCode::kDownloadOperationHashMismatch: |
| 303 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 304 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 305 | case ErrorCode::kDownloadOperationHashMissingError: |
| 306 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 307 | case ErrorCode::kPayloadMismatchedType: |
| 308 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 309 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 310 | case ErrorCode::kPayloadTimestampError: |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 311 | case ErrorCode::kVerityCalculationError: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 312 | IncrementUrlIndex(); |
| 313 | break; |
| 314 | |
| 315 | // Errors which seem to be just transient network/communication related |
| 316 | // failures and do not indicate any inherent problem with the URL itself. |
| 317 | // So, we should keep the current URL but just increment the |
| 318 | // failure count to give it more chances. This way, while we maximize our |
| 319 | // chances of downloading from the URLs that appear earlier in the response |
| 320 | // (because download from a local server URL that appears earlier in a |
| 321 | // response is preferable than downloading from the next URL which could be |
| 322 | // a internet URL and thus could be more expensive). |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 323 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 324 | case ErrorCode::kError: |
| 325 | case ErrorCode::kDownloadTransferError: |
| 326 | case ErrorCode::kDownloadWriteError: |
| 327 | case ErrorCode::kDownloadStateInitializationError: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 328 | case ErrorCode::kOmahaErrorInHTTPResponse: // Aggregate for HTTP errors. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 329 | IncrementFailureCount(); |
| 330 | break; |
| 331 | |
| 332 | // Errors which are not specific to a URL and hence shouldn't result in |
| 333 | // the URL being penalized. This can happen in two cases: |
| 334 | // 1. We haven't started downloading anything: These errors don't cost us |
| 335 | // anything in terms of actual payload bytes, so we should just do the |
| 336 | // regular retries at the next update check. |
| 337 | // 2. We have successfully downloaded the payload: In this case, the |
| 338 | // payload attempt number would have been incremented and would take care |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 339 | // of the backoff at the next update check. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 340 | // In either case, there's no need to update URL index or failure count. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 341 | case ErrorCode::kOmahaRequestError: |
| 342 | case ErrorCode::kOmahaResponseHandlerError: |
| 343 | case ErrorCode::kPostinstallRunnerError: |
| 344 | case ErrorCode::kFilesystemCopierError: |
| 345 | case ErrorCode::kInstallDeviceOpenError: |
| 346 | case ErrorCode::kKernelDeviceOpenError: |
| 347 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 348 | case ErrorCode::kNewRootfsVerificationError: |
| 349 | case ErrorCode::kNewKernelVerificationError: |
| 350 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 351 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 352 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 353 | case ErrorCode::kOmahaRequestXMLParseError: |
| 354 | case ErrorCode::kOmahaResponseInvalid: |
| 355 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 356 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
Kevin Cernekee | 2494e28 | 2016-03-29 18:03:53 -0700 | [diff] [blame] | 357 | case ErrorCode::kNonCriticalUpdateInOOBE: |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 358 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 359 | case ErrorCode::kPostinstallPowerwashError: |
| 360 | case ErrorCode::kUpdateCanceledByChannelChange: |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 361 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 362 | case ErrorCode::kFilesystemVerifierError: |
Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 363 | case ErrorCode::kUserCanceled: |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 364 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 365 | case ErrorCode::kUpdatedButNotActive: |
Sen Jiang | 89e24c1 | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 366 | case ErrorCode::kNoUpdate: |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 367 | case ErrorCode::kRollbackNotPossible: |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 368 | case ErrorCode::kFirstActiveOmahaPingSentPersistenceError: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 369 | LOG(INFO) << "Not incrementing URL index or failure count for this error"; |
| 370 | break; |
| 371 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 372 | case ErrorCode::kSuccess: // success code |
| 373 | case ErrorCode::kUmaReportedMax: // not an error code |
| 374 | case ErrorCode::kOmahaRequestHTTPResponseBase: // aggregated already |
| 375 | case ErrorCode::kDevModeFlag: // not an error code |
| 376 | case ErrorCode::kResumedFlag: // not an error code |
| 377 | case ErrorCode::kTestImageFlag: // not an error code |
| 378 | case ErrorCode::kTestOmahaUrlFlag: // not an error code |
| 379 | case ErrorCode::kSpecialFlags: // not an error code |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 380 | // These shouldn't happen. Enumerating these explicitly here so that we |
| 381 | // can let the compiler warn about new error codes that are added to |
| 382 | // action_processor.h but not added here. |
| 383 | LOG(WARNING) << "Unexpected error code for UpdateFailed"; |
| 384 | break; |
| 385 | |
| 386 | // Note: Not adding a default here so as to let the compiler warn us of |
| 387 | // any new enums that were added in the .h but not listed in this switch. |
| 388 | } |
| 389 | } |
| 390 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 391 | bool PayloadState::ShouldBackoffDownload() { |
| 392 | if (response_.disable_payload_backoff) { |
| 393 | LOG(INFO) << "Payload backoff logic is disabled. " |
| 394 | "Can proceed with the download"; |
| 395 | return false; |
| 396 | } |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 397 | if (GetUsingP2PForDownloading() && !GetP2PUrl().empty()) { |
Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 398 | LOG(INFO) << "Payload backoff logic is disabled because download " |
| 399 | << "will happen from local peer (via p2p)."; |
| 400 | return false; |
| 401 | } |
| 402 | if (system_state_->request_params()->interactive()) { |
| 403 | LOG(INFO) << "Payload backoff disabled for interactive update checks."; |
| 404 | return false; |
| 405 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 406 | for (const auto& package : response_.packages) { |
| 407 | if (package.is_delta) { |
| 408 | // If delta payloads fail, we want to fallback quickly to full payloads as |
| 409 | // they are more likely to succeed. Exponential backoffs would greatly |
| 410 | // slow down the fallback to full payloads. So we don't backoff for delta |
| 411 | // payloads. |
| 412 | LOG(INFO) << "No backoffs for delta payloads. " |
| 413 | << "Can proceed with the download"; |
| 414 | return false; |
| 415 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Amin Hassani | ffb6d80 | 2018-03-30 11:43:57 -0700 | [diff] [blame] | 418 | if (!system_state_->hardware()->IsOfficialBuild() && |
| 419 | !prefs_->Exists(kPrefsNoIgnoreBackoff)) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 420 | // Backoffs are needed only for official builds. We do not want any delays |
Amin Hassani | ffb6d80 | 2018-03-30 11:43:57 -0700 | [diff] [blame] | 421 | // or update failures due to backoffs during testing or development. Unless |
| 422 | // the |kPrefsNoIgnoreBackoff| is manually set. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 423 | LOG(INFO) << "No backoffs for test/dev images. " |
| 424 | << "Can proceed with the download"; |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | if (backoff_expiry_time_.is_null()) { |
| 429 | LOG(INFO) << "No backoff expiry time has been set. " |
| 430 | << "Can proceed with the download"; |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | if (backoff_expiry_time_ < Time::Now()) { |
| 435 | LOG(INFO) << "The backoff expiry time (" |
| 436 | << utils::ToString(backoff_expiry_time_) |
| 437 | << ") has elapsed. Can proceed with the download"; |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | LOG(INFO) << "Cannot proceed with downloads as we need to backoff until " |
| 442 | << utils::ToString(backoff_expiry_time_); |
| 443 | return true; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 446 | void PayloadState::Rollback() { |
| 447 | SetRollbackVersion(system_state_->request_params()->app_version()); |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 448 | AttemptStarted(AttemptType::kRollback); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 451 | void PayloadState::IncrementPayloadAttemptNumber() { |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 452 | // Update the payload attempt number for both payload types: full and delta. |
| 453 | SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1); |
| 454 | } |
| 455 | |
| 456 | void PayloadState::IncrementFullPayloadAttemptNumber() { |
| 457 | // Update the payload attempt number for full payloads and the backoff time. |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 458 | if (response_.packages[payload_index_].is_delta) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 459 | LOG(INFO) << "Not incrementing payload attempt number for delta payloads"; |
| 460 | return; |
| 461 | } |
| 462 | |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 463 | LOG(INFO) << "Incrementing the full payload attempt number"; |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 464 | SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 465 | UpdateBackoffExpiryTime(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void PayloadState::IncrementUrlIndex() { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 469 | size_t next_url_index = url_index_ + 1; |
| 470 | size_t max_url_size = 0; |
| 471 | for (const auto& urls : candidate_urls_) |
| 472 | max_url_size = std::max(max_url_size, urls.size()); |
| 473 | if (next_url_index < max_url_size) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 474 | LOG(INFO) << "Incrementing the URL index for next attempt"; |
| 475 | SetUrlIndex(next_url_index); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 476 | } else { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 477 | LOG(INFO) << "Resetting the current URL index (" << url_index_ << ") to " |
| 478 | << "0 as we only have " << max_url_size << " candidate URL(s)"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 479 | SetUrlIndex(0); |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 480 | IncrementPayloadAttemptNumber(); |
| 481 | IncrementFullPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 482 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 483 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 484 | // If we have multiple URLs, record that we just switched to another one |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 485 | if (max_url_size > 1) |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 486 | SetUrlSwitchCount(url_switch_count_ + 1); |
| 487 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 488 | // Whenever we update the URL index, we should also clear the URL failure |
| 489 | // count so we can start over fresh for the new URL. |
| 490 | SetUrlFailureCount(0); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 493 | void PayloadState::IncrementFailureCount() { |
| 494 | uint32_t next_url_failure_count = GetUrlFailureCount() + 1; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 495 | if (next_url_failure_count < response_.max_failure_count_per_url) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 496 | LOG(INFO) << "Incrementing the URL failure count"; |
| 497 | SetUrlFailureCount(next_url_failure_count); |
| 498 | } else { |
| 499 | LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex() |
| 500 | << ". Trying next available URL"; |
| 501 | IncrementUrlIndex(); |
| 502 | } |
| 503 | } |
| 504 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 505 | void PayloadState::UpdateBackoffExpiryTime() { |
| 506 | if (response_.disable_payload_backoff) { |
| 507 | LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled"; |
| 508 | SetBackoffExpiryTime(Time()); |
| 509 | return; |
| 510 | } |
| 511 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 512 | if (GetFullPayloadAttemptNumber() == 0) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 513 | SetBackoffExpiryTime(Time()); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | // Since we're doing left-shift below, make sure we don't shift more |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 518 | // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits, |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 519 | // since we don't expect value of kMaxBackoffDays to be more than 100 anyway. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 520 | int num_days = 1; // the value to be shifted. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 521 | const int kMaxShifts = (sizeof(num_days) * 8) - 2; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 522 | |
| 523 | // Normal backoff days is 2 raised to (payload_attempt_number - 1). |
| 524 | // E.g. if payload_attempt_number is over 30, limit power to 30. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 525 | int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 526 | |
| 527 | // The number of days is the minimum of 2 raised to (payload_attempt_number |
| 528 | // - 1) or kMaxBackoffDays. |
| 529 | num_days = min(num_days << power, kMaxBackoffDays); |
| 530 | |
| 531 | // We don't want all retries to happen exactly at the same time when |
| 532 | // retrying after backoff. So add some random minutes to fuzz. |
| 533 | int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes); |
| 534 | TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) + |
| 535 | TimeDelta::FromMinutes(fuzz_minutes); |
| 536 | LOG(INFO) << "Incrementing the backoff expiry time by " |
| 537 | << utils::FormatTimeDelta(next_backoff_interval); |
| 538 | SetBackoffExpiryTime(Time::Now() + next_backoff_interval); |
| 539 | } |
| 540 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 541 | void PayloadState::UpdateCurrentDownloadSource() { |
| 542 | current_download_source_ = kNumDownloadSources; |
| 543 | |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 544 | if (using_p2p_for_downloading_) { |
| 545 | current_download_source_ = kDownloadSourceHttpPeer; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 546 | } else if (payload_index_ < candidate_urls_.size() && |
| 547 | candidate_urls_[payload_index_].size() != 0) { |
| 548 | const string& current_url = candidate_urls_[payload_index_][GetUrlIndex()]; |
| 549 | if (base::StartsWith( |
| 550 | current_url, "https://", base::CompareCase::INSENSITIVE_ASCII)) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 551 | current_download_source_ = kDownloadSourceHttpsServer; |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 552 | } else if (base::StartsWith(current_url, |
| 553 | "http://", |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 554 | base::CompareCase::INSENSITIVE_ASCII)) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 555 | current_download_source_ = kDownloadSourceHttpServer; |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 556 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | LOG(INFO) << "Current download source: " |
| 560 | << utils::ToString(current_download_source_); |
| 561 | } |
| 562 | |
| 563 | void PayloadState::UpdateBytesDownloaded(size_t count) { |
| 564 | SetCurrentBytesDownloaded( |
| 565 | current_download_source_, |
| 566 | GetCurrentBytesDownloaded(current_download_source_) + count, |
| 567 | false); |
| 568 | SetTotalBytesDownloaded( |
| 569 | current_download_source_, |
| 570 | GetTotalBytesDownloaded(current_download_source_) + count, |
| 571 | false); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 572 | |
| 573 | attempt_num_bytes_downloaded_ += count; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 576 | PayloadType PayloadState::CalculatePayloadType() { |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 577 | for (const auto& package : response_.packages) { |
| 578 | if (package.is_delta) { |
| 579 | return kPayloadTypeDelta; |
| 580 | } |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 581 | } |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 582 | OmahaRequestParams* params = system_state_->request_params(); |
| 583 | if (params->delta_okay()) { |
| 584 | return kPayloadTypeFull; |
| 585 | } |
| 586 | // Full payload, delta was not allowed by request. |
| 587 | return kPayloadTypeForcedFull; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // TODO(zeuthen): Currently we don't report the UpdateEngine.Attempt.* |
| 591 | // metrics if the attempt ends abnormally, e.g. if the update_engine |
| 592 | // process crashes or the device is rebooted. See |
| 593 | // http://crbug.com/357676 |
| 594 | void PayloadState::CollectAndReportAttemptMetrics(ErrorCode code) { |
| 595 | int attempt_number = GetPayloadAttemptNumber(); |
| 596 | |
| 597 | PayloadType payload_type = CalculatePayloadType(); |
| 598 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 599 | int64_t payload_size = GetPayloadSize(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 600 | |
| 601 | int64_t payload_bytes_downloaded = attempt_num_bytes_downloaded_; |
| 602 | |
| 603 | ClockInterface *clock = system_state_->clock(); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 604 | TimeDelta duration = clock->GetBootTime() - attempt_start_time_boot_; |
| 605 | TimeDelta duration_uptime = clock->GetMonotonicTime() - |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 606 | attempt_start_time_monotonic_; |
| 607 | |
| 608 | int64_t payload_download_speed_bps = 0; |
| 609 | int64_t usec = duration_uptime.InMicroseconds(); |
| 610 | if (usec > 0) { |
| 611 | double sec = static_cast<double>(usec) / Time::kMicrosecondsPerSecond; |
| 612 | double bps = static_cast<double>(payload_bytes_downloaded) / sec; |
| 613 | payload_download_speed_bps = static_cast<int64_t>(bps); |
| 614 | } |
| 615 | |
| 616 | DownloadSource download_source = current_download_source_; |
| 617 | |
| 618 | metrics::DownloadErrorCode payload_download_error_code = |
| 619 | metrics::DownloadErrorCode::kUnset; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 620 | ErrorCode internal_error_code = ErrorCode::kSuccess; |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 621 | metrics::AttemptResult attempt_result = metrics_utils::GetAttemptResult(code); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 622 | |
| 623 | // Add additional detail to AttemptResult |
| 624 | switch (attempt_result) { |
| 625 | case metrics::AttemptResult::kPayloadDownloadError: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 626 | payload_download_error_code = metrics_utils::GetDownloadErrorCode(code); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | case metrics::AttemptResult::kInternalError: |
| 630 | internal_error_code = code; |
| 631 | break; |
| 632 | |
| 633 | // Explicit fall-through for cases where we do not have additional |
| 634 | // detail. We avoid the default keyword to force people adding new |
| 635 | // AttemptResult values to visit this code and examine whether |
| 636 | // additional detail is needed. |
| 637 | case metrics::AttemptResult::kUpdateSucceeded: |
| 638 | case metrics::AttemptResult::kMetadataMalformed: |
| 639 | case metrics::AttemptResult::kOperationMalformed: |
| 640 | case metrics::AttemptResult::kOperationExecutionError: |
| 641 | case metrics::AttemptResult::kMetadataVerificationFailed: |
| 642 | case metrics::AttemptResult::kPayloadVerificationFailed: |
| 643 | case metrics::AttemptResult::kVerificationFailed: |
| 644 | case metrics::AttemptResult::kPostInstallFailed: |
| 645 | case metrics::AttemptResult::kAbnormalTermination: |
Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 646 | case metrics::AttemptResult::kUpdateCanceled: |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 647 | case metrics::AttemptResult::kUpdateSucceededNotActive: |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 648 | case metrics::AttemptResult::kNumConstants: |
| 649 | case metrics::AttemptResult::kUnset: |
| 650 | break; |
| 651 | } |
| 652 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 653 | system_state_->metrics_reporter()->ReportUpdateAttemptMetrics( |
| 654 | system_state_, |
| 655 | attempt_number, |
| 656 | payload_type, |
| 657 | duration, |
| 658 | duration_uptime, |
| 659 | payload_size, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 660 | attempt_result, |
| 661 | internal_error_code); |
| 662 | |
| 663 | system_state_->metrics_reporter()->ReportUpdateAttemptDownloadMetrics( |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 664 | payload_bytes_downloaded, |
| 665 | payload_download_speed_bps, |
| 666 | download_source, |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 667 | payload_download_error_code, |
| 668 | attempt_connection_type_); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 669 | } |
| 670 | |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 671 | void PayloadState::PersistAttemptMetrics() { |
| 672 | // TODO(zeuthen): For now we only persist whether an attempt was in |
| 673 | // progress and not values/metrics related to the attempt. This |
| 674 | // means that when this happens, of all the UpdateEngine.Attempt.* |
| 675 | // metrics, only UpdateEngine.Attempt.Result is reported (with the |
| 676 | // value |kAbnormalTermination|). In the future we might want to |
| 677 | // persist more data so we can report other metrics in the |
| 678 | // UpdateEngine.Attempt.* namespace when this happens. |
| 679 | prefs_->SetBoolean(kPrefsAttemptInProgress, true); |
| 680 | } |
| 681 | |
| 682 | void PayloadState::ClearPersistedAttemptMetrics() { |
| 683 | prefs_->Delete(kPrefsAttemptInProgress); |
| 684 | } |
| 685 | |
| 686 | void PayloadState::ReportAndClearPersistedAttemptMetrics() { |
| 687 | bool attempt_in_progress = false; |
| 688 | if (!prefs_->GetBoolean(kPrefsAttemptInProgress, &attempt_in_progress)) |
| 689 | return; |
| 690 | if (!attempt_in_progress) |
| 691 | return; |
| 692 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 693 | system_state_->metrics_reporter() |
| 694 | ->ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 695 | |
| 696 | ClearPersistedAttemptMetrics(); |
| 697 | } |
| 698 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 699 | void PayloadState::CollectAndReportSuccessfulUpdateMetrics() { |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 700 | string metric; |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 701 | |
| 702 | // Report metrics collected from all known download sources to UMA. |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 703 | int64_t total_bytes_by_source[kNumDownloadSources]; |
| 704 | int64_t successful_bytes = 0; |
| 705 | int64_t total_bytes = 0; |
| 706 | int64_t successful_mbs = 0; |
| 707 | int64_t total_mbs = 0; |
| 708 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 709 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 710 | DownloadSource source = static_cast<DownloadSource>(i); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 711 | int64_t bytes; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 712 | |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 713 | // Only consider this download source (and send byte counts) as |
| 714 | // having been used if we downloaded a non-trivial amount of bytes |
| 715 | // (e.g. at least 1 MiB) that contributed to the final success of |
| 716 | // the update. Otherwise we're going to end up with a lot of |
| 717 | // zero-byte events in the histogram. |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 718 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 719 | bytes = GetCurrentBytesDownloaded(source); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 720 | successful_bytes += bytes; |
| 721 | successful_mbs += bytes / kNumBytesInOneMiB; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 722 | SetCurrentBytesDownloaded(source, 0, true); |
| 723 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 724 | bytes = GetTotalBytesDownloaded(source); |
| 725 | total_bytes_by_source[i] = bytes; |
| 726 | total_bytes += bytes; |
| 727 | total_mbs += bytes / kNumBytesInOneMiB; |
| 728 | SetTotalBytesDownloaded(source, 0, true); |
| 729 | } |
| 730 | |
| 731 | int download_overhead_percentage = 0; |
| 732 | if (successful_bytes > 0) { |
| 733 | download_overhead_percentage = (total_bytes - successful_bytes) * 100ULL / |
| 734 | successful_bytes; |
| 735 | } |
| 736 | |
| 737 | int url_switch_count = static_cast<int>(url_switch_count_); |
| 738 | |
| 739 | int reboot_count = GetNumReboots(); |
| 740 | |
| 741 | SetNumReboots(0); |
| 742 | |
| 743 | TimeDelta duration = GetUpdateDuration(); |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 744 | TimeDelta duration_uptime = GetUpdateDurationUptime(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 745 | |
| 746 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 747 | prefs_->Delete(kPrefsUpdateDurationUptime); |
| 748 | |
| 749 | PayloadType payload_type = CalculatePayloadType(); |
| 750 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 751 | int64_t payload_size = GetPayloadSize(); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 752 | |
| 753 | int attempt_count = GetPayloadAttemptNumber(); |
| 754 | |
| 755 | int updates_abandoned_count = num_responses_seen_ - 1; |
| 756 | |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 757 | system_state_->metrics_reporter()->ReportSuccessfulUpdateMetrics( |
| 758 | attempt_count, |
| 759 | updates_abandoned_count, |
| 760 | payload_type, |
| 761 | payload_size, |
| 762 | total_bytes_by_source, |
| 763 | download_overhead_percentage, |
| 764 | duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 765 | duration_uptime, |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 766 | reboot_count, |
| 767 | url_switch_count); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void PayloadState::UpdateNumReboots() { |
| 771 | // We only update the reboot count when the system has been detected to have |
| 772 | // been rebooted. |
| 773 | if (!system_state_->system_rebooted()) { |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | SetNumReboots(GetNumReboots() + 1); |
| 778 | } |
| 779 | |
| 780 | void PayloadState::SetNumReboots(uint32_t num_reboots) { |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 781 | num_reboots_ = num_reboots; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 782 | metrics_utils::SetNumReboots(num_reboots, prefs_); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 785 | void PayloadState::ResetPersistedState() { |
| 786 | SetPayloadAttemptNumber(0); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 787 | SetFullPayloadAttemptNumber(0); |
Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 788 | SetPayloadIndex(0); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 789 | SetUrlIndex(0); |
| 790 | SetUrlFailureCount(0); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 791 | SetUrlSwitchCount(0); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 792 | UpdateBackoffExpiryTime(); // This will reset the backoff expiry time. |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 793 | SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime()); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 794 | SetUpdateTimestampEnd(Time()); // Set to null time |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 795 | SetUpdateDurationUptime(TimeDelta::FromSeconds(0)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 796 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 797 | ResetRollbackVersion(); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 798 | SetP2PNumAttempts(0); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 799 | SetP2PFirstAttemptTimestamp(Time()); // Set to null time |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 800 | SetScatteringWaitPeriod(TimeDelta()); |
Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 801 | SetStagingWaitPeriod(TimeDelta()); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | void PayloadState::ResetRollbackVersion() { |
| 805 | CHECK(powerwash_safe_prefs_); |
| 806 | rollback_version_ = ""; |
| 807 | powerwash_safe_prefs_->Delete(kPrefsRollbackVersion); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | void PayloadState::ResetDownloadSourcesOnNewUpdate() { |
| 811 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 812 | DownloadSource source = static_cast<DownloadSource>(i); |
| 813 | SetCurrentBytesDownloaded(source, 0, true); |
| 814 | // Note: Not resetting the TotalBytesDownloaded as we want that metric |
| 815 | // to count the bytes downloaded across various update attempts until |
| 816 | // we have successfully applied the update. |
| 817 | } |
| 818 | } |
| 819 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 820 | string PayloadState::CalculateResponseSignature() { |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 821 | string response_sign; |
| 822 | for (size_t i = 0; i < response_.packages.size(); i++) { |
| 823 | const auto& package = response_.packages[i]; |
| 824 | response_sign += base::StringPrintf( |
| 825 | "Payload %zu:\n" |
| 826 | " Size = %ju\n" |
| 827 | " Sha256 Hash = %s\n" |
| 828 | " Metadata Size = %ju\n" |
| 829 | " Metadata Signature = %s\n" |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 830 | " Is Delta = %d\n" |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 831 | " NumURLs = %zu\n", |
| 832 | i, |
| 833 | static_cast<uintmax_t>(package.size), |
| 834 | package.hash.c_str(), |
| 835 | static_cast<uintmax_t>(package.metadata_size), |
| 836 | package.metadata_signature.c_str(), |
Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 837 | package.is_delta, |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 838 | candidate_urls_[i].size()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 839 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 840 | for (size_t j = 0; j < candidate_urls_[i].size(); j++) |
| 841 | response_sign += base::StringPrintf( |
| 842 | " Candidate Url%zu = %s\n", j, candidate_urls_[i][j].c_str()); |
| 843 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 844 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 845 | response_sign += base::StringPrintf( |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 846 | "Max Failure Count Per Url = %d\n" |
| 847 | "Disable Payload Backoff = %d\n", |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 848 | response_.max_failure_count_per_url, |
| 849 | response_.disable_payload_backoff); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 850 | return response_sign; |
| 851 | } |
| 852 | |
| 853 | void PayloadState::LoadResponseSignature() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 854 | CHECK(prefs_); |
| 855 | string stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 856 | if (prefs_->Exists(kPrefsCurrentResponseSignature) && |
| 857 | prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) { |
| 858 | SetResponseSignature(stored_value); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 859 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 862 | void PayloadState::SetResponseSignature(const string& response_signature) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 863 | CHECK(prefs_); |
| 864 | response_signature_ = response_signature; |
| 865 | LOG(INFO) << "Current Response Signature = \n" << response_signature_; |
| 866 | prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_); |
| 867 | } |
| 868 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 869 | void PayloadState::LoadPayloadAttemptNumber() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 870 | SetPayloadAttemptNumber( |
| 871 | GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 874 | void PayloadState::LoadFullPayloadAttemptNumber() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 875 | SetFullPayloadAttemptNumber( |
| 876 | GetPersistedValue(kPrefsFullPayloadAttemptNumber, prefs_)); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 880 | payload_attempt_number_ = payload_attempt_number; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 881 | metrics_utils::SetPayloadAttemptNumber(payload_attempt_number, prefs_); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 884 | void PayloadState::SetFullPayloadAttemptNumber( |
| 885 | int full_payload_attempt_number) { |
| 886 | CHECK(prefs_); |
| 887 | full_payload_attempt_number_ = full_payload_attempt_number; |
| 888 | LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_; |
| 889 | prefs_->SetInt64(kPrefsFullPayloadAttemptNumber, |
| 890 | full_payload_attempt_number_); |
| 891 | } |
| 892 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 893 | void PayloadState::SetPayloadIndex(size_t payload_index) { |
| 894 | CHECK(prefs_); |
| 895 | payload_index_ = payload_index; |
| 896 | LOG(INFO) << "Payload Index = " << payload_index_; |
| 897 | prefs_->SetInt64(kPrefsUpdateStatePayloadIndex, payload_index_); |
| 898 | } |
| 899 | |
| 900 | bool PayloadState::NextPayload() { |
| 901 | if (payload_index_ + 1 >= candidate_urls_.size()) |
| 902 | return false; |
| 903 | SetPayloadIndex(payload_index_ + 1); |
| 904 | return true; |
| 905 | } |
| 906 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 907 | void PayloadState::LoadUrlIndex() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 908 | SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex, prefs_)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | void PayloadState::SetUrlIndex(uint32_t url_index) { |
| 912 | CHECK(prefs_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 913 | url_index_ = url_index; |
| 914 | LOG(INFO) << "Current URL Index = " << url_index_; |
| 915 | prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 916 | |
| 917 | // Also update the download source, which is purely dependent on the |
| 918 | // current URL index alone. |
| 919 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 922 | void PayloadState::LoadScatteringWaitPeriod() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 923 | SetScatteringWaitPeriod(TimeDelta::FromSeconds( |
Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 924 | GetPersistedValue(kPrefsWallClockScatteringWaitPeriod, prefs_))); |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 927 | void PayloadState::SetScatteringWaitPeriod(TimeDelta wait_period) { |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 928 | CHECK(prefs_); |
| 929 | scattering_wait_period_ = wait_period; |
| 930 | LOG(INFO) << "Scattering Wait Period (seconds) = " |
| 931 | << scattering_wait_period_.InSeconds(); |
| 932 | if (scattering_wait_period_.InSeconds() > 0) { |
Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 933 | prefs_->SetInt64(kPrefsWallClockScatteringWaitPeriod, |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 934 | scattering_wait_period_.InSeconds()); |
| 935 | } else { |
Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 936 | prefs_->Delete(kPrefsWallClockScatteringWaitPeriod); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | void PayloadState::LoadStagingWaitPeriod() { |
| 941 | SetStagingWaitPeriod(TimeDelta::FromSeconds( |
| 942 | GetPersistedValue(kPrefsWallClockStagingWaitPeriod, prefs_))); |
| 943 | } |
| 944 | |
| 945 | void PayloadState::SetStagingWaitPeriod(TimeDelta wait_period) { |
| 946 | CHECK(prefs_); |
| 947 | staging_wait_period_ = wait_period; |
| 948 | LOG(INFO) << "Staging Wait Period (days) =" << staging_wait_period_.InDays(); |
| 949 | if (staging_wait_period_.InSeconds() > 0) { |
| 950 | prefs_->SetInt64(kPrefsWallClockStagingWaitPeriod, |
| 951 | staging_wait_period_.InSeconds()); |
| 952 | } else { |
| 953 | prefs_->Delete(kPrefsWallClockStagingWaitPeriod); |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 957 | void PayloadState::LoadUrlSwitchCount() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 958 | SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount, prefs_)); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) { |
| 962 | CHECK(prefs_); |
| 963 | url_switch_count_ = url_switch_count; |
| 964 | LOG(INFO) << "URL Switch Count = " << url_switch_count_; |
| 965 | prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_); |
| 966 | } |
| 967 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 968 | void PayloadState::LoadUrlFailureCount() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 969 | SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount, prefs_)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) { |
| 973 | CHECK(prefs_); |
| 974 | url_failure_count_ = url_failure_count; |
| 975 | LOG(INFO) << "Current URL (Url" << GetUrlIndex() |
| 976 | << ")'s Failure Count = " << url_failure_count_; |
| 977 | prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 980 | void PayloadState::LoadBackoffExpiryTime() { |
| 981 | CHECK(prefs_); |
| 982 | int64_t stored_value; |
| 983 | if (!prefs_->Exists(kPrefsBackoffExpiryTime)) |
| 984 | return; |
| 985 | |
| 986 | if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value)) |
| 987 | return; |
| 988 | |
| 989 | Time stored_time = Time::FromInternalValue(stored_value); |
| 990 | if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) { |
| 991 | LOG(ERROR) << "Invalid backoff expiry time (" |
| 992 | << utils::ToString(stored_time) |
| 993 | << ") in persisted state. Resetting."; |
| 994 | stored_time = Time(); |
| 995 | } |
| 996 | SetBackoffExpiryTime(stored_time); |
| 997 | } |
| 998 | |
| 999 | void PayloadState::SetBackoffExpiryTime(const Time& new_time) { |
| 1000 | CHECK(prefs_); |
| 1001 | backoff_expiry_time_ = new_time; |
| 1002 | LOG(INFO) << "Backoff Expiry Time = " |
| 1003 | << utils::ToString(backoff_expiry_time_); |
| 1004 | prefs_->SetInt64(kPrefsBackoffExpiryTime, |
| 1005 | backoff_expiry_time_.ToInternalValue()); |
| 1006 | } |
| 1007 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1008 | TimeDelta PayloadState::GetUpdateDuration() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1009 | Time end_time = update_timestamp_end_.is_null() |
| 1010 | ? system_state_->clock()->GetWallclockTime() : |
| 1011 | update_timestamp_end_; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1012 | return end_time - update_timestamp_start_; |
| 1013 | } |
| 1014 | |
| 1015 | void PayloadState::LoadUpdateTimestampStart() { |
| 1016 | int64_t stored_value; |
| 1017 | Time stored_time; |
| 1018 | |
| 1019 | CHECK(prefs_); |
| 1020 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1021 | Time now = system_state_->clock()->GetWallclockTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1022 | |
| 1023 | if (!prefs_->Exists(kPrefsUpdateTimestampStart)) { |
| 1024 | // The preference missing is not unexpected - in that case, just |
| 1025 | // use the current time as start time |
| 1026 | stored_time = now; |
| 1027 | } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) { |
| 1028 | LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting."; |
| 1029 | stored_time = now; |
| 1030 | } else { |
| 1031 | stored_time = Time::FromInternalValue(stored_value); |
| 1032 | } |
| 1033 | |
| 1034 | // Sanity check: If the time read from disk is in the future |
| 1035 | // (modulo some slack to account for possible NTP drift |
| 1036 | // adjustments), something is fishy and we should report and |
| 1037 | // reset. |
| 1038 | TimeDelta duration_according_to_stored_time = now - stored_time; |
| 1039 | if (duration_according_to_stored_time < -kDurationSlack) { |
| 1040 | LOG(ERROR) << "The UpdateTimestampStart value (" |
| 1041 | << utils::ToString(stored_time) |
| 1042 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1043 | << utils::FormatTimeDelta(duration_according_to_stored_time) |
| 1044 | << " in the future. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1045 | stored_time = now; |
| 1046 | } |
| 1047 | |
| 1048 | SetUpdateTimestampStart(stored_time); |
| 1049 | } |
| 1050 | |
| 1051 | void PayloadState::SetUpdateTimestampStart(const Time& value) { |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1052 | update_timestamp_start_ = value; |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1053 | metrics_utils::SetUpdateTimestampStart(value, prefs_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | void PayloadState::SetUpdateTimestampEnd(const Time& value) { |
| 1057 | update_timestamp_end_ = value; |
| 1058 | LOG(INFO) << "Update Timestamp End = " |
| 1059 | << utils::ToString(update_timestamp_end_); |
| 1060 | } |
| 1061 | |
| 1062 | TimeDelta PayloadState::GetUpdateDurationUptime() { |
| 1063 | return update_duration_uptime_; |
| 1064 | } |
| 1065 | |
| 1066 | void PayloadState::LoadUpdateDurationUptime() { |
| 1067 | int64_t stored_value; |
| 1068 | TimeDelta stored_delta; |
| 1069 | |
| 1070 | CHECK(prefs_); |
| 1071 | |
| 1072 | if (!prefs_->Exists(kPrefsUpdateDurationUptime)) { |
| 1073 | // The preference missing is not unexpected - in that case, just |
| 1074 | // we'll use zero as the delta |
| 1075 | } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) { |
| 1076 | LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting."; |
| 1077 | stored_delta = TimeDelta::FromSeconds(0); |
| 1078 | } else { |
| 1079 | stored_delta = TimeDelta::FromInternalValue(stored_value); |
| 1080 | } |
| 1081 | |
| 1082 | // Sanity-check: Uptime can never be greater than the wall-clock |
| 1083 | // difference (modulo some slack). If it is, report and reset |
| 1084 | // to the wall-clock difference. |
| 1085 | TimeDelta diff = GetUpdateDuration() - stored_delta; |
| 1086 | if (diff < -kDurationSlack) { |
| 1087 | LOG(ERROR) << "The UpdateDurationUptime value (" |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1088 | << utils::FormatTimeDelta(stored_delta) |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1089 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1090 | << utils::FormatTimeDelta(diff) |
| 1091 | << " larger than the wall-clock delta. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1092 | stored_delta = update_duration_current_; |
| 1093 | } |
| 1094 | |
| 1095 | SetUpdateDurationUptime(stored_delta); |
| 1096 | } |
| 1097 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1098 | void PayloadState::LoadNumReboots() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1099 | SetNumReboots(GetPersistedValue(kPrefsNumReboots, prefs_)); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1102 | void PayloadState::LoadRollbackHappened() { |
| 1103 | CHECK(powerwash_safe_prefs_); |
| 1104 | bool rollback_happened = false; |
| 1105 | powerwash_safe_prefs_->GetBoolean(kPrefsRollbackHappened, &rollback_happened); |
| 1106 | SetRollbackHappened(rollback_happened); |
| 1107 | } |
| 1108 | |
| 1109 | void PayloadState::SetRollbackHappened(bool rollback_happened) { |
| 1110 | CHECK(powerwash_safe_prefs_); |
| 1111 | LOG(INFO) << "Setting rollback-happened to " << rollback_happened << "."; |
| 1112 | rollback_happened_ = rollback_happened; |
| 1113 | if (rollback_happened) { |
| 1114 | powerwash_safe_prefs_->SetBoolean(kPrefsRollbackHappened, |
| 1115 | rollback_happened); |
| 1116 | } else { |
| 1117 | powerwash_safe_prefs_->Delete(kPrefsRollbackHappened); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1121 | void PayloadState::LoadRollbackVersion() { |
Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 1122 | CHECK(powerwash_safe_prefs_); |
| 1123 | string rollback_version; |
| 1124 | if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion, |
| 1125 | &rollback_version)) { |
| 1126 | SetRollbackVersion(rollback_version); |
| 1127 | } |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | void PayloadState::SetRollbackVersion(const string& rollback_version) { |
| 1131 | CHECK(powerwash_safe_prefs_); |
| 1132 | LOG(INFO) << "Blacklisting version "<< rollback_version; |
| 1133 | rollback_version_ = rollback_version; |
| 1134 | powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1137 | void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value, |
| 1138 | const Time& timestamp, |
| 1139 | bool use_logging) { |
| 1140 | CHECK(prefs_); |
| 1141 | update_duration_uptime_ = value; |
| 1142 | update_duration_uptime_timestamp_ = timestamp; |
| 1143 | prefs_->SetInt64(kPrefsUpdateDurationUptime, |
| 1144 | update_duration_uptime_.ToInternalValue()); |
| 1145 | if (use_logging) { |
| 1146 | LOG(INFO) << "Update Duration Uptime = " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1147 | << utils::FormatTimeDelta(update_duration_uptime_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1152 | Time now = system_state_->clock()->GetMonotonicTime(); |
| 1153 | SetUpdateDurationUptimeExtended(value, now, true); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | void PayloadState::CalculateUpdateDurationUptime() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1157 | Time now = system_state_->clock()->GetMonotonicTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1158 | TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_; |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 1159 | |
| 1160 | if (uptime_since_last_update > TimeDelta::FromSeconds(kUptimeResolution)) { |
| 1161 | TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update; |
| 1162 | // We're frequently called so avoid logging this write |
| 1163 | SetUpdateDurationUptimeExtended(new_uptime, now, false); |
| 1164 | } |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1167 | string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) { |
| 1168 | return prefix + "-from-" + utils::ToString(source); |
| 1169 | } |
| 1170 | |
| 1171 | void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) { |
| 1172 | string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1173 | SetCurrentBytesDownloaded(source, GetPersistedValue(key, prefs_), true); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | void PayloadState::SetCurrentBytesDownloaded( |
| 1177 | DownloadSource source, |
| 1178 | uint64_t current_bytes_downloaded, |
| 1179 | bool log) { |
| 1180 | CHECK(prefs_); |
| 1181 | |
| 1182 | if (source >= kNumDownloadSources) |
| 1183 | return; |
| 1184 | |
| 1185 | // Update the in-memory value. |
| 1186 | current_bytes_downloaded_[source] = current_bytes_downloaded; |
| 1187 | |
| 1188 | string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 1189 | prefs_->SetInt64(prefs_key, current_bytes_downloaded); |
| 1190 | LOG_IF(INFO, log) << "Current bytes downloaded for " |
| 1191 | << utils::ToString(source) << " = " |
| 1192 | << GetCurrentBytesDownloaded(source); |
| 1193 | } |
| 1194 | |
| 1195 | void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) { |
| 1196 | string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1197 | SetTotalBytesDownloaded(source, GetPersistedValue(key, prefs_), true); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | void PayloadState::SetTotalBytesDownloaded( |
| 1201 | DownloadSource source, |
| 1202 | uint64_t total_bytes_downloaded, |
| 1203 | bool log) { |
| 1204 | CHECK(prefs_); |
| 1205 | |
| 1206 | if (source >= kNumDownloadSources) |
| 1207 | return; |
| 1208 | |
| 1209 | // Update the in-memory value. |
| 1210 | total_bytes_downloaded_[source] = total_bytes_downloaded; |
| 1211 | |
| 1212 | // Persist. |
| 1213 | string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 1214 | prefs_->SetInt64(prefs_key, total_bytes_downloaded); |
| 1215 | LOG_IF(INFO, log) << "Total bytes downloaded for " |
| 1216 | << utils::ToString(source) << " = " |
| 1217 | << GetTotalBytesDownloaded(source); |
| 1218 | } |
| 1219 | |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1220 | void PayloadState::LoadNumResponsesSeen() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1221 | SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen, prefs_)); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void PayloadState::SetNumResponsesSeen(int num_responses_seen) { |
| 1225 | CHECK(prefs_); |
| 1226 | num_responses_seen_ = num_responses_seen; |
| 1227 | LOG(INFO) << "Num Responses Seen = " << num_responses_seen_; |
| 1228 | prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_); |
| 1229 | } |
| 1230 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1231 | void PayloadState::ComputeCandidateUrls() { |
Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1232 | bool http_url_ok = true; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1233 | |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 1234 | if (system_state_->hardware()->IsOfficialBuild()) { |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1235 | const policy::DevicePolicy* policy = system_state_->device_policy(); |
Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1236 | if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok) |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1237 | LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy"; |
| 1238 | } else { |
| 1239 | LOG(INFO) << "Allowing HTTP downloads for unofficial builds"; |
| 1240 | http_url_ok = true; |
| 1241 | } |
| 1242 | |
| 1243 | candidate_urls_.clear(); |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1244 | for (const auto& package : response_.packages) { |
| 1245 | candidate_urls_.emplace_back(); |
| 1246 | for (const string& candidate_url : package.payload_urls) { |
| 1247 | if (base::StartsWith( |
| 1248 | candidate_url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
| 1249 | !http_url_ok) { |
| 1250 | continue; |
| 1251 | } |
| 1252 | candidate_urls_.back().push_back(candidate_url); |
| 1253 | LOG(INFO) << "Candidate Url" << (candidate_urls_.back().size() - 1) |
| 1254 | << ": " << candidate_url; |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 1255 | } |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1256 | LOG(INFO) << "Found " << candidate_urls_.back().size() << " candidate URLs " |
| 1257 | << "out of " << package.payload_urls.size() |
| 1258 | << " URLs supplied in package " << candidate_urls_.size() - 1; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1259 | } |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1262 | void PayloadState::UpdateEngineStarted() { |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1263 | // Flush previous state from abnormal attempt failure, if any. |
| 1264 | ReportAndClearPersistedAttemptMetrics(); |
| 1265 | |
Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1266 | // Avoid the UpdateEngineStarted actions if this is not the first time we |
| 1267 | // run the update engine since reboot. |
| 1268 | if (!system_state_->system_rebooted()) |
| 1269 | return; |
| 1270 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1271 | // Report time_to_reboot if we booted into a new update. |
| 1272 | metrics_utils::LoadAndReportTimeToReboot( |
| 1273 | system_state_->metrics_reporter(), prefs_, system_state_->clock()); |
| 1274 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 1275 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1276 | // Check if it is needed to send metrics about a failed reboot into a new |
| 1277 | // version. |
| 1278 | ReportFailedBootIfNeeded(); |
| 1279 | } |
| 1280 | |
| 1281 | void PayloadState::ReportFailedBootIfNeeded() { |
| 1282 | // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied |
| 1283 | // payload was marked as ready immediately before the last reboot, and we |
| 1284 | // need to check if such payload successfully rebooted or not. |
| 1285 | if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 1286 | int64_t installed_from = 0; |
| 1287 | if (!prefs_->GetInt64(kPrefsTargetVersionInstalledFrom, &installed_from)) { |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1288 | LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot."; |
| 1289 | return; |
| 1290 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1291 | // Old Chrome OS devices will write 2 or 4 in this setting, with the |
| 1292 | // partition number. We are now using slot numbers (0 or 1) instead, so |
| 1293 | // the following comparison will not match if we are comparing an old |
| 1294 | // partition number against a new slot number, which is the correct outcome |
| 1295 | // since we successfully booted the new update in that case. If the boot |
| 1296 | // failed, we will read this value from the same version, so it will always |
| 1297 | // be compatible. |
| 1298 | if (installed_from == system_state_->boot_control()->GetCurrentSlot()) { |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1299 | // A reboot was pending, but the chromebook is again in the same |
| 1300 | // BootDevice where the update was installed from. |
| 1301 | int64_t target_attempt; |
| 1302 | if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) { |
| 1303 | LOG(ERROR) << "Error reading TargetVersionAttempt when " |
| 1304 | "TargetVersionInstalledFrom was present."; |
| 1305 | target_attempt = 1; |
| 1306 | } |
| 1307 | |
| 1308 | // Report the UMA metric of the current boot failure. |
Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 1309 | system_state_->metrics_reporter()->ReportFailedUpdateCount( |
| 1310 | target_attempt); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1311 | } else { |
| 1312 | prefs_->Delete(kPrefsTargetVersionAttempt); |
| 1313 | prefs_->Delete(kPrefsTargetVersionUniqueId); |
| 1314 | } |
| 1315 | prefs_->Delete(kPrefsTargetVersionInstalledFrom); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) { |
| 1320 | // Expect to boot into the new partition in the next reboot setting the |
| 1321 | // TargetVersion* flags in the Prefs. |
| 1322 | string stored_target_version_uid; |
| 1323 | string target_version_id; |
| 1324 | string target_partition; |
| 1325 | int64_t target_attempt; |
| 1326 | |
| 1327 | if (prefs_->Exists(kPrefsTargetVersionUniqueId) && |
| 1328 | prefs_->GetString(kPrefsTargetVersionUniqueId, |
| 1329 | &stored_target_version_uid) && |
| 1330 | stored_target_version_uid == target_version_uid) { |
| 1331 | if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) |
| 1332 | target_attempt = 0; |
| 1333 | } else { |
| 1334 | prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid); |
| 1335 | target_attempt = 0; |
| 1336 | } |
| 1337 | prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1); |
| 1338 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 1339 | prefs_->SetInt64(kPrefsTargetVersionInstalledFrom, |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1340 | system_state_->boot_control()->GetCurrentSlot()); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | void PayloadState::ResetUpdateStatus() { |
| 1344 | // Remove the TargetVersionInstalledFrom pref so that if the machine is |
| 1345 | // rebooted the next boot is not flagged as failed to rebooted into the |
| 1346 | // new applied payload. |
| 1347 | prefs_->Delete(kPrefsTargetVersionInstalledFrom); |
| 1348 | |
| 1349 | // Also decrement the attempt number if it exists. |
| 1350 | int64_t target_attempt; |
| 1351 | if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1352 | prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt - 1); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1355 | int PayloadState::GetP2PNumAttempts() { |
| 1356 | return p2p_num_attempts_; |
| 1357 | } |
| 1358 | |
| 1359 | void PayloadState::SetP2PNumAttempts(int value) { |
| 1360 | p2p_num_attempts_ = value; |
| 1361 | LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_; |
| 1362 | CHECK(prefs_); |
| 1363 | prefs_->SetInt64(kPrefsP2PNumAttempts, value); |
| 1364 | } |
| 1365 | |
| 1366 | void PayloadState::LoadP2PNumAttempts() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1367 | SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts, prefs_)); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | Time PayloadState::GetP2PFirstAttemptTimestamp() { |
| 1371 | return p2p_first_attempt_timestamp_; |
| 1372 | } |
| 1373 | |
| 1374 | void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) { |
| 1375 | p2p_first_attempt_timestamp_ = time; |
| 1376 | LOG(INFO) << "p2p First Attempt Timestamp = " |
| 1377 | << utils::ToString(p2p_first_attempt_timestamp_); |
| 1378 | CHECK(prefs_); |
| 1379 | int64_t stored_value = time.ToInternalValue(); |
| 1380 | prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value); |
| 1381 | } |
| 1382 | |
| 1383 | void PayloadState::LoadP2PFirstAttemptTimestamp() { |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1384 | int64_t stored_value = |
| 1385 | GetPersistedValue(kPrefsP2PFirstAttemptTimestamp, prefs_); |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1386 | Time stored_time = Time::FromInternalValue(stored_value); |
| 1387 | SetP2PFirstAttemptTimestamp(stored_time); |
| 1388 | } |
| 1389 | |
| 1390 | void PayloadState::P2PNewAttempt() { |
| 1391 | CHECK(prefs_); |
| 1392 | // Set timestamp, if it hasn't been set already |
| 1393 | if (p2p_first_attempt_timestamp_.is_null()) { |
| 1394 | SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime()); |
| 1395 | } |
| 1396 | // Increase number of attempts |
| 1397 | SetP2PNumAttempts(GetP2PNumAttempts() + 1); |
| 1398 | } |
| 1399 | |
| 1400 | bool PayloadState::P2PAttemptAllowed() { |
| 1401 | if (p2p_num_attempts_ > kMaxP2PAttempts) { |
| 1402 | LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_ |
| 1403 | << " which is greater than " |
| 1404 | << kMaxP2PAttempts |
| 1405 | << " - disallowing p2p."; |
| 1406 | return false; |
| 1407 | } |
| 1408 | |
| 1409 | if (!p2p_first_attempt_timestamp_.is_null()) { |
| 1410 | Time now = system_state_->clock()->GetWallclockTime(); |
| 1411 | TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_; |
| 1412 | if (time_spent_attempting_p2p.InSeconds() < 0) { |
| 1413 | LOG(ERROR) << "Time spent attempting p2p is negative" |
| 1414 | << " - disallowing p2p."; |
| 1415 | return false; |
| 1416 | } |
| 1417 | if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) { |
| 1418 | LOG(INFO) << "Time spent attempting p2p is " |
| 1419 | << utils::FormatTimeDelta(time_spent_attempting_p2p) |
| 1420 | << " which is greater than " |
| 1421 | << utils::FormatTimeDelta(TimeDelta::FromSeconds( |
| 1422 | kMaxP2PAttemptTimeSeconds)) |
| 1423 | << " - disallowing p2p."; |
| 1424 | return false; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | return true; |
| 1429 | } |
| 1430 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1431 | int64_t PayloadState::GetPayloadSize() { |
| 1432 | int64_t payload_size = 0; |
| 1433 | for (const auto& package : response_.packages) |
| 1434 | payload_size += package.size; |
| 1435 | return payload_size; |
| 1436 | } |
| 1437 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1438 | } // namespace chromeos_update_engine |