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