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