Home | History | Annotate | Download | only in update_engine
      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 //
     16 
     17 #ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_
     18 #define UPDATE_ENGINE_PAYLOAD_STATE_H_
     19 
     20 #include <string>
     21 #include <vector>
     22 
     23 #include <base/time/time.h>
     24 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
     25 
     26 #include "update_engine/common/prefs_interface.h"
     27 #include "update_engine/metrics.h"
     28 #include "update_engine/payload_state_interface.h"
     29 
     30 namespace chromeos_update_engine {
     31 
     32 class SystemState;
     33 
     34 // Encapsulates all the payload state required for download. This includes the
     35 // state necessary for handling multiple URLs in Omaha response, the backoff
     36 // state, etc. All state is persisted so that we use the most recently saved
     37 // value when resuming the update_engine process. All state is also cached in
     38 // memory so that we ensure we always make progress based on last known good
     39 // state even when there's any issue in reading/writing from the file system.
     40 class PayloadState : public PayloadStateInterface {
     41  public:
     42   PayloadState();
     43   ~PayloadState() override {}
     44 
     45   // Initializes a payload state object using the given global system state.
     46   // It performs the initial loading of all persisted state into memory and
     47   // dumps the initial state for debugging purposes.  Note: the other methods
     48   // should be called only after calling Initialize on this object.
     49   bool Initialize(SystemState* system_state);
     50 
     51   // Implementation of PayloadStateInterface methods.
     52   void SetResponse(const OmahaResponse& response) override;
     53   void DownloadComplete() override;
     54   void DownloadProgress(size_t count) override;
     55   void UpdateResumed() override;
     56   void UpdateRestarted() override;
     57   void UpdateSucceeded() override;
     58   void UpdateFailed(ErrorCode error) override;
     59   void ResetUpdateStatus() override;
     60   bool ShouldBackoffDownload() override;
     61   void Rollback() override;
     62   void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
     63   void SetUsingP2PForDownloading(bool value) override;
     64 
     65   void SetUsingP2PForSharing(bool value) override {
     66     using_p2p_for_sharing_ = value;
     67   }
     68 
     69   inline std::string GetResponseSignature() override {
     70     return response_signature_;
     71   }
     72 
     73   inline int GetFullPayloadAttemptNumber() override {
     74     return full_payload_attempt_number_;
     75   }
     76 
     77   inline int GetPayloadAttemptNumber() override {
     78     return payload_attempt_number_;
     79   }
     80 
     81   inline std::string GetCurrentUrl() override {
     82     return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
     83   }
     84 
     85   inline uint32_t GetUrlFailureCount() override {
     86     return url_failure_count_;
     87   }
     88 
     89   inline uint32_t GetUrlSwitchCount() override {
     90     return url_switch_count_;
     91   }
     92 
     93   inline int GetNumResponsesSeen() override {
     94     return num_responses_seen_;
     95   }
     96 
     97   inline base::Time GetBackoffExpiryTime() override {
     98     return backoff_expiry_time_;
     99   }
    100 
    101   base::TimeDelta GetUpdateDuration() override;
    102 
    103   base::TimeDelta GetUpdateDurationUptime() override;
    104 
    105   inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
    106     return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
    107   }
    108 
    109   inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
    110     return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
    111   }
    112 
    113   inline uint32_t GetNumReboots() override {
    114     return num_reboots_;
    115   }
    116 
    117   void UpdateEngineStarted() override;
    118 
    119   inline std::string GetRollbackVersion() override {
    120     return rollback_version_;
    121   }
    122 
    123   int GetP2PNumAttempts() override;
    124   base::Time GetP2PFirstAttemptTimestamp() override;
    125   void P2PNewAttempt() override;
    126   bool P2PAttemptAllowed() override;
    127 
    128   bool GetUsingP2PForDownloading() const override {
    129     return using_p2p_for_downloading_;
    130   }
    131 
    132   bool GetUsingP2PForSharing() const override {
    133     return using_p2p_for_sharing_;
    134   }
    135 
    136   base::TimeDelta GetScatteringWaitPeriod() override {
    137     return scattering_wait_period_;
    138   }
    139 
    140   void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
    141 
    142   void SetP2PUrl(const std::string& url) override {
    143     p2p_url_ = url;
    144   }
    145 
    146   std::string GetP2PUrl() const override {
    147     return p2p_url_;
    148   }
    149 
    150   inline ErrorCode GetAttemptErrorCode() const override {
    151     return attempt_error_code_;
    152   }
    153 
    154  private:
    155   enum class AttemptType {
    156     kUpdate,
    157     kRollback,
    158   };
    159 
    160   friend class PayloadStateTest;
    161   FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
    162   FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
    163   FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
    164   FRIEND_TEST(PayloadStateTest, RollbackVersion);
    165   FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
    166 
    167   // Helper called when an attempt has begun, is called by
    168   // UpdateResumed(), UpdateRestarted() and Rollback().
    169   void AttemptStarted(AttemptType attempt_type);
    170 
    171   // Increments the payload attempt number used for metrics.
    172   void IncrementPayloadAttemptNumber();
    173 
    174   // Increments the payload attempt number which governs the backoff behavior
    175   // at the time of the next update check.
    176   void IncrementFullPayloadAttemptNumber();
    177 
    178   // Advances the current URL index to the next available one. If all URLs have
    179   // been exhausted during the current payload download attempt (as indicated
    180   // by the payload attempt number), then it will increment the payload attempt
    181   // number and wrap around again with the first URL in the list. This also
    182   // updates the URL switch count, if needed.
    183   void IncrementUrlIndex();
    184 
    185   // Increments the failure count of the current URL. If the configured max
    186   // failure count is reached for this URL, it advances the current URL index
    187   // to the next URL and resets the failure count for that URL.
    188   void IncrementFailureCount();
    189 
    190   // Updates the backoff expiry time exponentially based on the current
    191   // payload attempt number.
    192   void UpdateBackoffExpiryTime();
    193 
    194   // Updates the value of current download source based on the current URL
    195   // index. If the download source is not one of the known sources, it's set
    196   // to kNumDownloadSources.
    197   void UpdateCurrentDownloadSource();
    198 
    199   // Updates the various metrics corresponding with the given number of bytes
    200   // that were downloaded recently.
    201   void UpdateBytesDownloaded(size_t count);
    202 
    203   // Calculates the PayloadType we're using.
    204   PayloadType CalculatePayloadType();
    205 
    206   // Collects and reports the various metrics related to an update attempt.
    207   void CollectAndReportAttemptMetrics(ErrorCode code);
    208 
    209   // Persists values related to the UpdateEngine.Attempt.* metrics so
    210   // we can identify later if an update attempt ends abnormally.
    211   void PersistAttemptMetrics();
    212 
    213   // Clears persistent state previously set using AttemptMetricsPersist().
    214   void ClearPersistedAttemptMetrics();
    215 
    216   // Checks if persistent state previously set using AttemptMetricsPersist()
    217   // exists and, if so, emits it with |attempt_result| set to
    218   // metrics::AttemptResult::kAbnormalTermination.
    219   void ReportAndClearPersistedAttemptMetrics();
    220 
    221   // Collects and reports the various metrics related to a successful update.
    222   void CollectAndReportSuccessfulUpdateMetrics();
    223 
    224   // Checks if we were expecting to be running in the new version but the
    225   // boot into the new version failed for some reason. If that's the case, an
    226   // UMA metric is sent reporting the number of attempts the same applied
    227   // payload was attempted to reboot. This function is called by UpdateAttempter
    228   // every time the update engine starts and there's no reboot pending.
    229   void ReportFailedBootIfNeeded();
    230 
    231   // Resets all the persisted state values which are maintained relative to the
    232   // current response signature. The response signature itself is not reset.
    233   void ResetPersistedState();
    234 
    235   // Resets the appropriate state related to download sources that need to be
    236   // reset on a new update.
    237   void ResetDownloadSourcesOnNewUpdate();
    238 
    239   // Returns the persisted value from prefs_ for the given key. It also
    240   // validates that the value returned is non-negative.
    241   int64_t GetPersistedValue(const std::string& key);
    242 
    243   // Calculates the response "signature", which is basically a string composed
    244   // of the subset of the fields in the current response that affect the
    245   // behavior of the PayloadState.
    246   std::string CalculateResponseSignature();
    247 
    248   // Initializes the current response signature from the persisted state.
    249   void LoadResponseSignature();
    250 
    251   // Sets the response signature to the given value. Also persists the value
    252   // being set so that we resume from the save value in case of a process
    253   // restart.
    254   void SetResponseSignature(const std::string& response_signature);
    255 
    256   // Initializes the payload attempt number from the persisted state.
    257   void LoadPayloadAttemptNumber();
    258 
    259   // Initializes the payload attempt number for full payloads from the persisted
    260   // state.
    261   void LoadFullPayloadAttemptNumber();
    262 
    263   // Sets the payload attempt number to the given value. Also persists the
    264   // value being set so that we resume from the same value in case of a process
    265   // restart.
    266   void SetPayloadAttemptNumber(int payload_attempt_number);
    267 
    268   // Sets the payload attempt number for full updates to the given value. Also
    269   // persists the value being set so that we resume from the same value in case
    270   // of a process restart.
    271   void SetFullPayloadAttemptNumber(int payload_attempt_number);
    272 
    273   // Initializes the current URL index from the persisted state.
    274   void LoadUrlIndex();
    275 
    276   // Sets the current URL index to the given value. Also persists the value
    277   // being set so that we resume from the same value in case of a process
    278   // restart.
    279   void SetUrlIndex(uint32_t url_index);
    280 
    281   // Initializes the current URL's failure count from the persisted stae.
    282   void LoadUrlFailureCount();
    283 
    284   // Sets the current URL's failure count to the given value. Also persists the
    285   // value being set so that we resume from the same value in case of a process
    286   // restart.
    287   void SetUrlFailureCount(uint32_t url_failure_count);
    288 
    289   // Sets |url_switch_count_| to the given value and persists the value.
    290   void SetUrlSwitchCount(uint32_t url_switch_count);
    291 
    292   // Initializes |url_switch_count_| from the persisted stae.
    293   void LoadUrlSwitchCount();
    294 
    295   // Initializes the backoff expiry time from the persisted state.
    296   void LoadBackoffExpiryTime();
    297 
    298   // Sets the backoff expiry time to the given value. Also persists the value
    299   // being set so that we resume from the same value in case of a process
    300   // restart.
    301   void SetBackoffExpiryTime(const base::Time& new_time);
    302 
    303   // Initializes |update_timestamp_start_| from the persisted state.
    304   void LoadUpdateTimestampStart();
    305 
    306   // Sets |update_timestamp_start_| to the given value and persists the value.
    307   void SetUpdateTimestampStart(const base::Time& value);
    308 
    309   // Sets |update_timestamp_end_| to the given value. This is not persisted
    310   // as it happens at the end of the update process where state is deleted
    311   // anyway.
    312   void SetUpdateTimestampEnd(const base::Time& value);
    313 
    314   // Initializes |update_duration_uptime_| from the persisted state.
    315   void LoadUpdateDurationUptime();
    316 
    317   // Helper method used in SetUpdateDurationUptime() and
    318   // CalculateUpdateDurationUptime().
    319   void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
    320                                        const base::Time& timestamp,
    321                                        bool use_logging);
    322 
    323   // Sets |update_duration_uptime_| to the given value and persists
    324   // the value and sets |update_duration_uptime_timestamp_| to the
    325   // current monotonic time.
    326   void SetUpdateDurationUptime(const base::TimeDelta& value);
    327 
    328   // Adds the difference between current monotonic time and
    329   // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
    330   // sets |update_duration_uptime_timestamp_| to current monotonic time.
    331   void CalculateUpdateDurationUptime();
    332 
    333   // Returns the full key for a download source given the prefix.
    334   std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
    335 
    336   // Loads the number of bytes that have been currently downloaded through the
    337   // previous attempts from the persisted state for the given source. It's
    338   // reset to 0 everytime we begin a full update and is continued from previous
    339   // attempt if we're resuming the update.
    340   void LoadCurrentBytesDownloaded(DownloadSource source);
    341 
    342   // Sets the number of bytes that have been currently downloaded for the
    343   // given source. This value is also persisted.
    344   void SetCurrentBytesDownloaded(DownloadSource source,
    345                                  uint64_t current_bytes_downloaded,
    346                                  bool log);
    347 
    348   // Loads the total number of bytes that have been downloaded (since the last
    349   // successful update) from the persisted state for the given source. It's
    350   // reset to 0 everytime we successfully apply an update and counts the bytes
    351   // downloaded for both successful and failed attempts since then.
    352   void LoadTotalBytesDownloaded(DownloadSource source);
    353 
    354   // Sets the total number of bytes that have been downloaded so far for the
    355   // given source. This value is also persisted.
    356   void SetTotalBytesDownloaded(DownloadSource source,
    357                                uint64_t total_bytes_downloaded,
    358                                bool log);
    359 
    360   // Loads the blacklisted version from our prefs file.
    361   void LoadRollbackVersion();
    362 
    363   // Blacklists this version from getting AU'd to until we receive a new update
    364   // response.
    365   void SetRollbackVersion(const std::string& rollback_version);
    366 
    367   // Clears any blacklisted version.
    368   void ResetRollbackVersion();
    369 
    370   inline uint32_t GetUrlIndex() {
    371     return url_index_;
    372   }
    373 
    374   // Computes the list of candidate URLs from the total list of payload URLs in
    375   // the Omaha response.
    376   void ComputeCandidateUrls();
    377 
    378   // Sets |num_responses_seen_| and persist it to disk.
    379   void SetNumResponsesSeen(int num_responses_seen);
    380 
    381   // Initializes |num_responses_seen_| from persisted state.
    382   void LoadNumResponsesSeen();
    383 
    384   // Initializes |num_reboots_| from the persisted state.
    385   void LoadNumReboots();
    386 
    387   // Sets |num_reboots| for the update attempt. Also persists the
    388   // value being set so that we resume from the same value in case of a process
    389   // restart.
    390   void SetNumReboots(uint32_t num_reboots);
    391 
    392   // Checks to see if the device rebooted since the last call and if so
    393   // increments num_reboots.
    394   void UpdateNumReboots();
    395 
    396   // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
    397   // state variable.
    398   void CreateSystemUpdatedMarkerFile();
    399 
    400   // Called at program startup if the device booted into a new update.
    401   // The |time_to_reboot| parameter contains the (wall-clock) duration
    402   // from when the update successfully completed (the value written
    403   // into the kPrefsSystemUpdatedMarker state variable) until the device
    404   // was booted into the update (current wall-clock time).
    405   void BootedIntoUpdate(base::TimeDelta time_to_reboot);
    406 
    407   // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
    408   // into |p2p_first_attempt_timestamp_|.
    409   void LoadP2PFirstAttemptTimestamp();
    410 
    411   // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
    412   void LoadP2PNumAttempts();
    413 
    414   // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
    415   void SetP2PNumAttempts(int value);
    416 
    417   // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
    418   void SetP2PFirstAttemptTimestamp(const base::Time& time);
    419 
    420   // Loads the persisted scattering wallclock-based wait period.
    421   void LoadScatteringWaitPeriod();
    422 
    423   // The global state of the system.
    424   SystemState* system_state_;
    425 
    426   // Interface object with which we read/write persisted state. This must
    427   // be set by calling the Initialize method before calling any other method.
    428   PrefsInterface* prefs_;
    429 
    430   // Interface object with which we read/write persisted state. This must
    431   // be set by calling the Initialize method before calling any other method.
    432   // This object persists across powerwashes.
    433   PrefsInterface* powerwash_safe_prefs_;
    434 
    435   // This is the current response object from Omaha.
    436   OmahaResponse response_;
    437 
    438   // Whether P2P is being used for downloading and sharing.
    439   bool using_p2p_for_downloading_;
    440   bool using_p2p_for_sharing_;
    441 
    442   // Stores the P2P download URL, if one is used.
    443   std::string p2p_url_;
    444 
    445   // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
    446   base::Time p2p_first_attempt_timestamp_;
    447 
    448   // The cached value of |kPrefsP2PNumAttempts|.
    449   int p2p_num_attempts_;
    450 
    451   // This stores a "signature" of the current response. The signature here
    452   // refers to a subset of the current response from Omaha.  Each update to
    453   // this value is persisted so we resume from the same value in case of a
    454   // process restart.
    455   std::string response_signature_;
    456 
    457   // The number of times we've tried to download the payload. This is
    458   // incremented each time we download the payload successsfully or when we
    459   // exhaust all failure limits for all URLs and are about to wrap around back
    460   // to the first URL.  Each update to this value is persisted so we resume from
    461   // the same value in case of a process restart.
    462   int payload_attempt_number_;
    463 
    464   // The number of times we've tried to download the payload in full. This is
    465   // incremented each time we download the payload in full successsfully or
    466   // when we exhaust all failure limits for all URLs and are about to wrap
    467   // around back to the first URL.  Each update to this value is persisted so
    468   // we resume from the same value in case of a process restart.
    469   int full_payload_attempt_number_;
    470 
    471   // The index of the current URL.  This type is different from the one in the
    472   // accessor methods because PrefsInterface supports only int64_t but we want
    473   // to provide a stronger abstraction of uint32_t.  Each update to this value
    474   // is persisted so we resume from the same value in case of a process
    475   // restart.
    476   int64_t url_index_;
    477 
    478   // The count of failures encountered in the current attempt to download using
    479   // the current URL (specified by url_index_).  Each update to this value is
    480   // persisted so we resume from the same value in case of a process restart.
    481   int64_t url_failure_count_;
    482 
    483   // The number of times we've switched URLs.
    484   int32_t url_switch_count_;
    485 
    486   // The current download source based on the current URL. This value is
    487   // not persisted as it can be recomputed everytime we update the URL.
    488   // We're storing this so as not to recompute this on every few bytes of
    489   // data we read from the socket.
    490   DownloadSource current_download_source_;
    491 
    492   // The number of different Omaha responses seen. Increases every time
    493   // a new response is seen. Resets to 0 only when the system has been
    494   // successfully updated.
    495   int num_responses_seen_;
    496 
    497   // The number of system reboots during an update attempt. Technically since
    498   // we don't go out of our way to not update it when not attempting an update,
    499   // also records the number of reboots before the next update attempt starts.
    500   uint32_t num_reboots_;
    501 
    502   // The timestamp until which we've to wait before attempting to download the
    503   // payload again, so as to backoff repeated downloads.
    504   base::Time backoff_expiry_time_;
    505 
    506   // The most recently calculated value of the update duration.
    507   base::TimeDelta update_duration_current_;
    508 
    509   // The point in time (wall-clock) that the update was started.
    510   base::Time update_timestamp_start_;
    511 
    512   // The point in time (wall-clock) that the update ended. If the update
    513   // is still in progress, this is set to the Epoch (e.g. 0).
    514   base::Time update_timestamp_end_;
    515 
    516   // The update duration uptime
    517   base::TimeDelta update_duration_uptime_;
    518 
    519   // The monotonic time when |update_duration_uptime_| was last set
    520   base::Time update_duration_uptime_timestamp_;
    521 
    522   // The number of bytes that have been downloaded for each source for each new
    523   // update attempt. If we resume an update, we'll continue from the previous
    524   // value, but if we get a new response or if the previous attempt failed,
    525   // we'll reset this to 0 to start afresh. Each update to this value is
    526   // persisted so we resume from the same value in case of a process restart.
    527   // The extra index in the array is to no-op accidental access in case the
    528   // return value from GetCurrentDownloadSource is used without validation.
    529   uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
    530 
    531   // The number of bytes that have been downloaded for each source since the
    532   // the last successful update. This is used to compute the overhead we incur.
    533   // Each update to this value is persisted so we resume from the same value in
    534   // case of a process restart.
    535   // The extra index in the array is to no-op accidental access in case the
    536   // return value from GetCurrentDownloadSource is used without validation.
    537   uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
    538 
    539   // A small timespan used when comparing wall-clock times for coping
    540   // with the fact that clocks drift and consequently are adjusted
    541   // (either forwards or backwards) via NTP.
    542   static const base::TimeDelta kDurationSlack;
    543 
    544   // The ordered list of the subset of payload URL candidates which are
    545   // allowed as per device policy.
    546   std::vector<std::string> candidate_urls_;
    547 
    548   // This stores a blacklisted version set as part of rollback. When we rollback
    549   // we store the version of the os from which we are rolling back from in order
    550   // to guarantee that we do not re-update to it on the next au attempt after
    551   // reboot.
    552   std::string rollback_version_;
    553 
    554   // The number of bytes downloaded per attempt.
    555   int64_t attempt_num_bytes_downloaded_;
    556 
    557   // The boot time when the attempt was started.
    558   base::Time attempt_start_time_boot_;
    559 
    560   // The monotonic time when the attempt was started.
    561   base::Time attempt_start_time_monotonic_;
    562 
    563   // The connection type when the attempt started.
    564   metrics::ConnectionType attempt_connection_type_;
    565 
    566   // The attempt error code when the attempt finished.
    567   ErrorCode attempt_error_code_;
    568 
    569   // Whether we're currently rolling back.
    570   AttemptType attempt_type_;
    571 
    572   // The current scattering wallclock-based wait period.
    573   base::TimeDelta scattering_wait_period_;
    574 
    575   DISALLOW_COPY_AND_ASSIGN(PayloadState);
    576 };
    577 
    578 }  // namespace chromeos_update_engine
    579 
    580 #endif  // UPDATE_ENGINE_PAYLOAD_STATE_H_
    581