blob: 5c5b013c5f1f15c45ca8037c2c92580540ec636e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo23949d42014-02-05 15:20:59 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_
Alex Deymo23949d42014-02-05 15:20:59 -080019
20#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070021#include <memory>
Gilad Arnold48415f12014-06-27 07:10:58 -070022#include <string>
Alex Deymo23949d42014-02-05 15:20:59 -080023
Gilad Arnold83ffdda2014-08-08 13:30:31 -070024#include <base/bind.h>
Alex Deymo53556ec2014-03-17 10:05:57 -070025#include <base/callback.h>
Alex Deymodb799532014-03-21 13:00:00 -070026#include <base/memory/weak_ptr.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/message_loops/message_loop.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070031#include "update_engine/update_manager/boxed_value.h"
Alex Deymo63784a52014-05-28 10:46:14 -070032#include "update_engine/update_manager/variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080033
Alex Deymo63784a52014-05-28 10:46:14 -070034namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080035
36// The EvaluationContext class is the interface between a policy implementation
37// and the state. The EvaluationContext tracks the variables used by a policy
38// request and caches the returned values, owning those cached values.
Alex Deymo41a75a72014-04-15 15:36:22 -070039// The same EvaluationContext should be re-used for all the evaluations of the
40// same policy request (an AsyncPolicyRequest might involve several
41// re-evaluations). Each evaluation of the EvaluationContext is run at a given
42// point in time, which is used as a reference for the evaluation timeout and
Gilad Arnolda65fced2014-07-23 09:01:31 -070043// the time based queries of the policy, such as
44// Is{Wallclock,Monotonic}TimeGreaterThan().
Alex Deymo41a75a72014-04-15 15:36:22 -070045//
46// Example:
47//
Amin Hassania2c8b922019-08-14 19:41:03 -070048// auto ec = std::make_shared<EvaluationContext>(...);
Alex Deymo41a75a72014-04-15 15:36:22 -070049//
50// ...
51// // The following call to ResetEvaluation() is optional. Use it to reset the
52// // evaluation time if the EvaluationContext isn't used right after its
53// // construction.
54// ec->ResetEvaluation();
55// EvalStatus status = policy->SomeMethod(ec, state, &result, args...);
56//
57// ...
58// // Run a closure when any of the used async variables changes its value or
Alex Vakulenko072359c2014-07-18 11:41:07 -070059// // the timeout for re-query the values happens again.
Alex Deymo41a75a72014-04-15 15:36:22 -070060// ec->RunOnValueChangeOrTimeout(closure);
61// // If the provided |closure| wants to re-evaluate the policy, it should
62// // call ec->ResetEvaluation() to start a new evaluation.
63//
Amin Hassania2c8b922019-08-14 19:41:03 -070064class EvaluationContext : private BaseVariable::ObserverInterface {
Alex Deymo23949d42014-02-05 15:20:59 -080065 public:
Gilad Arnold83ffdda2014-08-08 13:30:31 -070066 EvaluationContext(
67 chromeos_update_engine::ClockInterface* clock,
68 base::TimeDelta evaluation_timeout,
69 base::TimeDelta expiration_timeout,
Ben Chan02f7c1d2014-10-18 15:18:02 -070070 std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070071 EvaluationContext(chromeos_update_engine::ClockInterface* clock,
72 base::TimeDelta evaluation_timeout)
Gilad Arnold83ffdda2014-08-08 13:30:31 -070073 : EvaluationContext(
Amin Hassani4b717432019-01-14 16:24:20 -080074 clock,
75 evaluation_timeout,
76 base::TimeDelta::Max(),
77 std::unique_ptr<base::Callback<void(EvaluationContext*)>>()) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070078 ~EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080079
80 // Returns a pointer to the value returned by the passed variable |var|. The
81 // EvaluationContext instance keeps the ownership of the returned object. The
Alex Deymo41a75a72014-04-15 15:36:22 -070082 // returned object is valid during the life of the evaluation, even if the
83 // passed Variable changes it.
Alex Deymo23949d42014-02-05 15:20:59 -080084 //
Alex Vakulenko88b591f2014-08-28 16:48:57 -070085 // In case of error, a null value is returned.
Amin Hassani4b717432019-01-14 16:24:20 -080086 template <typename T>
Alex Deymo23949d42014-02-05 15:20:59 -080087 const T* GetValue(Variable<T>* var);
88
Gilad Arnolda65fced2014-07-23 09:01:31 -070089 // Returns whether the evaluation time has surpassed |timestamp|, on either
90 // the ClockInterface::GetWallclockTime() or
91 // ClockInterface::GetMonotonicTime() scales, respectively.
92 bool IsWallclockTimeGreaterThan(base::Time timestamp);
93 bool IsMonotonicTimeGreaterThan(base::Time timestamp);
Alex Deymo41a75a72014-04-15 15:36:22 -070094
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070095 // Returns whether the evaluation context has expired.
96 bool is_expired() const { return is_expired_; }
97
Alex Deymo41a75a72014-04-15 15:36:22 -070098 // TODO(deymo): Move the following methods to an interface only visible by the
Alex Deymo63784a52014-05-28 10:46:14 -070099 // UpdateManager class and not the policy implementations.
Alex Deymo41a75a72014-04-15 15:36:22 -0700100
101 // Resets the EvaluationContext to its initial state removing all the
102 // non-const cached variables and re-setting the evaluation time. This should
103 // be called right before any new evaluation starts.
104 void ResetEvaluation();
105
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700106 // Clears the expiration status of the EvaluationContext and resets its
107 // expiration timeout based on |expiration_timeout_|. This should be called if
108 // expiration occurred, prior to re-evaluating the policy.
109 void ResetExpiration();
110
Alex Deymo53556ec2014-03-17 10:05:57 -0700111 // Schedules the passed |callback| closure to be called when a cached
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700112 // variable changes its value, a polling interval passes, or the context
113 // expiration occurs. If none of these events can happen, for example if
114 // there's no cached variable, this method returns false.
Alex Deymo53556ec2014-03-17 10:05:57 -0700115 //
116 // Right before the passed closure is called the EvaluationContext is
Sen Jiang771f6482018-04-04 17:59:10 -0700117 // reset, removing all the non-const cached values.
Alex Deymo53556ec2014-03-17 10:05:57 -0700118 bool RunOnValueChangeOrTimeout(base::Closure callback);
119
David Zeuthenc1490282014-04-29 16:25:03 -0700120 // Returns a textual representation of the evaluation context,
121 // including the variables and their values. This is intended only
122 // to help with debugging and the format may change in the future.
123 std::string DumpContext() const;
124
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700125 // Removes all the Observers callbacks and timeout events scheduled by
126 // RunOnValueChangeOrTimeout(). Also releases and returns the closure
127 // associated with these events. This method is idempotent.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700128 std::unique_ptr<base::Closure> RemoveObserversAndTimeout();
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700129
Alex Deymo23949d42014-02-05 15:20:59 -0800130 private:
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700131 friend class UmEvaluationContextTest;
132
Alex Deymo53556ec2014-03-17 10:05:57 -0700133 // BaseVariable::ObserverInterface override.
Alex Deymo610277e2014-11-11 21:18:11 -0800134 void ValueChanged(BaseVariable* var) override;
Alex Deymo53556ec2014-03-17 10:05:57 -0700135
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700136 // Called from the main loop when a scheduled timeout has passed.
137 void OnTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700138
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700139 // Removes the observers from the used Variables and cancels the timeout,
140 // then executes the scheduled callback.
141 void OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700142
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700143 // If |monotonic_deadline| is not Time::Max(), returns the remaining time
144 // until it is reached, or zero if it has passed. Otherwise, returns
145 // TimeDelta::Max().
146 base::TimeDelta RemainingTime(base::Time monotonic_deadline) const;
147
148 // Returns a monotonic clock timestamp at which |timeout| will have elapsed
149 // since the current time.
150 base::Time MonotonicDeadline(base::TimeDelta timeout);
Alex Deymo23949d42014-02-05 15:20:59 -0800151
152 // A map to hold the cached values for every variable.
153 typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap;
154
155 // The cached values of the called Variables.
156 ValueCacheMap value_cache_;
157
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700158 // A callback used for triggering re-evaluation upon a value change or poll
159 // timeout, or notifying about the evaluation context expiration. It is up to
Alex Vakulenko072359c2014-07-18 11:41:07 -0700160 // the caller to determine whether or not expiration occurred via
161 // is_expired().
Ben Chan02f7c1d2014-10-18 15:18:02 -0700162 std::unique_ptr<base::Closure> callback_;
Alex Deymo53556ec2014-03-17 10:05:57 -0700163
Alex Deymo509dd532015-06-10 14:11:05 -0700164 // The TaskId returned by the message loop identifying the timeout callback.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700165 // Used for canceling the timeout callback.
Amin Hassani4b717432019-01-14 16:24:20 -0800166 brillo::MessageLoop::TaskId timeout_event_ = brillo::MessageLoop::kTaskIdNull;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700167
168 // Whether a timeout event firing marks the expiration of the evaluation
169 // context.
170 bool timeout_marks_expiration_;
171
172 // Whether the evaluation context has indeed expired.
173 bool is_expired_ = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700174
Alex Deymo41a75a72014-04-15 15:36:22 -0700175 // Pointer to the mockable clock interface;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700176 chromeos_update_engine::ClockInterface* const clock_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700177
Gilad Arnolda65fced2014-07-23 09:01:31 -0700178 // The timestamps when the evaluation of this EvaluationContext started,
179 // corresponding to ClockInterface::GetWallclockTime() and
180 // ClockInterface::GetMonotonicTime(), respectively. These values are reset
181 // every time ResetEvaluation() is called.
182 base::Time evaluation_start_wallclock_;
183 base::Time evaluation_start_monotonic_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700184
Gilad Arnolda65fced2014-07-23 09:01:31 -0700185 // The timestamps when a reevaluation should be triggered due to various
186 // expected value changes, corresponding to ClockInterface::GetWallclockTime()
187 // and ClockInterface::GetMonotonicTIme(), respectively. These timestamps are
188 // greater or equal to corresponding |evaluation_start_{wallclock,monotonic}_|
189 // counterparts since they are in the future; however, they may be smaller
190 // than the current corresponding times during the course of evaluation.
191 base::Time reevaluation_time_wallclock_;
192 base::Time reevaluation_time_monotonic_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700193
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700194 // The timeout of an evaluation.
Gilad Arnoldb2271992014-06-19 12:35:24 -0700195 const base::TimeDelta evaluation_timeout_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700196
197 // The timestamp in the ClockInterface::GetMonotonicTime() scale at which the
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700198 // current evaluation should finish.
Alex Deymo41a75a72014-04-15 15:36:22 -0700199 base::Time evaluation_monotonic_deadline_;
200
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700201 // The expiration timeout of the evaluation context.
202 const base::TimeDelta expiration_timeout_;
203
204 // The monotonic clock deadline at which expiration occurs.
205 base::Time expiration_monotonic_deadline_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700206
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700207 // A callback for unregistering the context upon destruction.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700208 std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb_;
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700209
Alex Deymodb799532014-03-21 13:00:00 -0700210 base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_;
211
Alex Deymo23949d42014-02-05 15:20:59 -0800212 DISALLOW_COPY_AND_ASSIGN(EvaluationContext);
213};
214
Alex Deymo63784a52014-05-28 10:46:14 -0700215} // namespace chromeos_update_manager
Alex Deymo23949d42014-02-05 15:20:59 -0800216
217// Include the implementation of the template methods.
Alex Deymo63784a52014-05-28 10:46:14 -0700218#include "update_engine/update_manager/evaluation_context-inl.h"
Alex Deymo23949d42014-02-05 15:20:59 -0800219
Gilad Arnold48415f12014-06-27 07:10:58 -0700220#endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_