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