Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 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 | // |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 16 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 19 | |
| 20 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 21 | #include <memory> |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 22 | #include <string> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 23 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 24 | #include <base/bind.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 25 | #include <base/callback.h> |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 26 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 27 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 28 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 29 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/boxed_value.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 31 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 32 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 33 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 34 | |
| 35 | // The EvaluationContext class is the interface between a policy implementation |
| 36 | // and the state. The EvaluationContext tracks the variables used by a policy |
| 37 | // request and caches the returned values, owning those cached values. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 38 | // The same EvaluationContext should be re-used for all the evaluations of the |
| 39 | // same policy request (an AsyncPolicyRequest might involve several |
| 40 | // re-evaluations). Each evaluation of the EvaluationContext is run at a given |
| 41 | // point in time, which is used as a reference for the evaluation timeout and |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 42 | // the time based queries of the policy, such as |
| 43 | // Is{Wallclock,Monotonic}TimeGreaterThan(). |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 44 | // |
| 45 | // Example: |
| 46 | // |
Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 47 | // auto ec = std::make_shared<EvaluationContext>(...); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 48 | // |
| 49 | // ... |
| 50 | // // The following call to ResetEvaluation() is optional. Use it to reset the |
| 51 | // // evaluation time if the EvaluationContext isn't used right after its |
| 52 | // // construction. |
| 53 | // ec->ResetEvaluation(); |
| 54 | // EvalStatus status = policy->SomeMethod(ec, state, &result, args...); |
| 55 | // |
| 56 | // ... |
| 57 | // // Run a closure when any of the used async variables changes its value or |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 58 | // // the timeout for re-query the values happens again. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 59 | // ec->RunOnValueChangeOrTimeout(closure); |
| 60 | // // If the provided |closure| wants to re-evaluate the policy, it should |
| 61 | // // call ec->ResetEvaluation() to start a new evaluation. |
| 62 | // |
Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 63 | class EvaluationContext : private BaseVariable::ObserverInterface { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 64 | public: |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 65 | EvaluationContext( |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 66 | base::TimeDelta evaluation_timeout, |
| 67 | base::TimeDelta expiration_timeout, |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 68 | std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 69 | explicit EvaluationContext(base::TimeDelta evaluation_timeout) |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 70 | : EvaluationContext( |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 71 | evaluation_timeout, |
| 72 | base::TimeDelta::Max(), |
| 73 | std::unique_ptr<base::Callback<void(EvaluationContext*)>>()) {} |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 74 | ~EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 75 | |
| 76 | // Returns a pointer to the value returned by the passed variable |var|. The |
| 77 | // EvaluationContext instance keeps the ownership of the returned object. The |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 78 | // returned object is valid during the life of the evaluation, even if the |
| 79 | // passed Variable changes it. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 80 | // |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 81 | // In case of error, a null value is returned. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 82 | template <typename T> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 83 | const T* GetValue(Variable<T>* var); |
| 84 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 85 | // Returns whether the evaluation time has surpassed |timestamp|, on either |
| 86 | // the ClockInterface::GetWallclockTime() or |
| 87 | // ClockInterface::GetMonotonicTime() scales, respectively. |
| 88 | bool IsWallclockTimeGreaterThan(base::Time timestamp); |
| 89 | bool IsMonotonicTimeGreaterThan(base::Time timestamp); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 90 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 91 | // Returns whether the evaluation context has expired. |
| 92 | bool is_expired() const { return is_expired_; } |
| 93 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 94 | // TODO(deymo): Move the following methods to an interface only visible by the |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 95 | // UpdateManager class and not the policy implementations. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 96 | |
| 97 | // Resets the EvaluationContext to its initial state removing all the |
| 98 | // non-const cached variables and re-setting the evaluation time. This should |
| 99 | // be called right before any new evaluation starts. |
| 100 | void ResetEvaluation(); |
| 101 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 102 | // Clears the expiration status of the EvaluationContext and resets its |
| 103 | // expiration timeout based on |expiration_timeout_|. This should be called if |
| 104 | // expiration occurred, prior to re-evaluating the policy. |
| 105 | void ResetExpiration(); |
| 106 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 107 | // Schedules the passed |callback| closure to be called when a cached |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 108 | // variable changes its value, a polling interval passes, or the context |
| 109 | // expiration occurs. If none of these events can happen, for example if |
| 110 | // there's no cached variable, this method returns false. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 111 | // |
| 112 | // Right before the passed closure is called the EvaluationContext is |
Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 113 | // reset, removing all the non-const cached values. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 114 | bool RunOnValueChangeOrTimeout(base::Closure callback); |
| 115 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 116 | // Returns a textual representation of the evaluation context, |
| 117 | // including the variables and their values. This is intended only |
| 118 | // to help with debugging and the format may change in the future. |
| 119 | std::string DumpContext() const; |
| 120 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 121 | // Removes all the Observers callbacks and timeout events scheduled by |
| 122 | // RunOnValueChangeOrTimeout(). Also releases and returns the closure |
| 123 | // associated with these events. This method is idempotent. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 124 | std::unique_ptr<base::Closure> RemoveObserversAndTimeout(); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 125 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 126 | private: |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 127 | friend class UmEvaluationContextTest; |
| 128 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 129 | // BaseVariable::ObserverInterface override. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 130 | void ValueChanged(BaseVariable* var) override; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 131 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 132 | // Called from the main loop when a scheduled timeout has passed. |
| 133 | void OnTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 134 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 135 | // Removes the observers from the used Variables and cancels the timeout, |
| 136 | // then executes the scheduled callback. |
| 137 | void OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 138 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 139 | // If |monotonic_deadline| is not Time::Max(), returns the remaining time |
| 140 | // until it is reached, or zero if it has passed. Otherwise, returns |
| 141 | // TimeDelta::Max(). |
| 142 | base::TimeDelta RemainingTime(base::Time monotonic_deadline) const; |
| 143 | |
| 144 | // Returns a monotonic clock timestamp at which |timeout| will have elapsed |
| 145 | // since the current time. |
| 146 | base::Time MonotonicDeadline(base::TimeDelta timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 147 | |
| 148 | // A map to hold the cached values for every variable. |
| 149 | typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap; |
| 150 | |
| 151 | // The cached values of the called Variables. |
| 152 | ValueCacheMap value_cache_; |
| 153 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 154 | // A callback used for triggering re-evaluation upon a value change or poll |
| 155 | // timeout, or notifying about the evaluation context expiration. It is up to |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 156 | // the caller to determine whether or not expiration occurred via |
| 157 | // is_expired(). |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 158 | std::unique_ptr<base::Closure> callback_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 159 | |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 160 | // The TaskId returned by the message loop identifying the timeout callback. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 161 | // Used for canceling the timeout callback. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 162 | brillo::MessageLoop::TaskId timeout_event_ = brillo::MessageLoop::kTaskIdNull; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 163 | |
| 164 | // Whether a timeout event firing marks the expiration of the evaluation |
| 165 | // context. |
| 166 | bool timeout_marks_expiration_; |
| 167 | |
| 168 | // Whether the evaluation context has indeed expired. |
| 169 | bool is_expired_ = false; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 170 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 171 | // The timestamps when the evaluation of this EvaluationContext started, |
| 172 | // corresponding to ClockInterface::GetWallclockTime() and |
| 173 | // ClockInterface::GetMonotonicTime(), respectively. These values are reset |
| 174 | // every time ResetEvaluation() is called. |
| 175 | base::Time evaluation_start_wallclock_; |
| 176 | base::Time evaluation_start_monotonic_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 177 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 178 | // The timestamps when a reevaluation should be triggered due to various |
| 179 | // expected value changes, corresponding to ClockInterface::GetWallclockTime() |
| 180 | // and ClockInterface::GetMonotonicTIme(), respectively. These timestamps are |
| 181 | // greater or equal to corresponding |evaluation_start_{wallclock,monotonic}_| |
| 182 | // counterparts since they are in the future; however, they may be smaller |
| 183 | // than the current corresponding times during the course of evaluation. |
| 184 | base::Time reevaluation_time_wallclock_; |
| 185 | base::Time reevaluation_time_monotonic_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 186 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 187 | // The timeout of an evaluation. |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 188 | const base::TimeDelta evaluation_timeout_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 189 | |
| 190 | // The timestamp in the ClockInterface::GetMonotonicTime() scale at which the |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 191 | // current evaluation should finish. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 192 | base::Time evaluation_monotonic_deadline_; |
| 193 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 194 | // The expiration timeout of the evaluation context. |
| 195 | const base::TimeDelta expiration_timeout_; |
| 196 | |
| 197 | // The monotonic clock deadline at which expiration occurs. |
| 198 | base::Time expiration_monotonic_deadline_; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 199 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 200 | // A callback for unregistering the context upon destruction. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 201 | std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb_; |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 202 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 203 | base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_; |
| 204 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 205 | DISALLOW_COPY_AND_ASSIGN(EvaluationContext); |
| 206 | }; |
| 207 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 208 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 209 | |
| 210 | // Include the implementation of the template methods. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 211 | #include "update_engine/update_manager/evaluation_context-inl.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 212 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 213 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |