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