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