16 #ifndef TENSORFLOW_SERVING_CORE_SERVABLE_STATE_MONITOR_H_
17 #define TENSORFLOW_SERVING_CORE_SERVABLE_STATE_MONITOR_H_
23 #include "absl/time/time.h"
24 #include "absl/types/optional.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/platform/macros.h"
27 #include "tensorflow/core/platform/mutex.h"
28 #include "tensorflow/core/platform/thread_annotations.h"
29 #include "tensorflow_serving/core/manager.h"
30 #include "tensorflow_serving/core/servable_id.h"
31 #include "tensorflow_serving/core/servable_state.h"
32 #include "tensorflow_serving/util/event_bus.h"
34 namespace tensorflow {
52 const uint64_t event_time)
66 using ServableName = string;
67 using Version = int64;
69 std::map<Version, ServableStateAndTime, std::greater<Version>>;
70 using ServableMap = std::map<ServableName, VersionMap>;
71 using ServableSet = std::set<ServableName>;
82 using BoundedLog = std::deque<ServableStateAndTime>;
91 TF_LOCKS_EXCLUDED(mu_);
96 const ServableId& servable_id)
const TF_LOCKS_EXCLUDED(mu_);
101 TF_LOCKS_EXCLUDED(mu_);
118 ServableSet GetAvailableServableStates() const TF_LOCKS_EXCLUDED(mu_);
148 bool reached_goal_state,
150 void NotifyWhenServablesReachState(
165 ServableState::ManagerState goal_state, absl::Duration timeout,
167 nullptr) TF_LOCKS_EXCLUDED(mu_) TF_MUST_USE_RESULT;
168 bool WaitUntilServablesReachState(
172 nullptr) TF_MUST_USE_RESULT;
177 void Notify(const NotifyFn& notify_fn) TF_LOCKS_EXCLUDED(notify_mu_);
181 GetStateAndTimeInternal(const
ServableId& servable_id) const
182 TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
186 struct ServableStateNotificationRequest {
187 std::vector<ServableRequest> servables;
188 ServableState::ManagerState goal_state;
196 std::pair<bool, std::map<ServableId, ServableState::ManagerState>>>
197 ShouldSendStateReachedNotification(
198 const ServableStateNotificationRequest& notification_request)
199 TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
203 void MaybeSendStateReachedNotifications() TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
208 TF_LOCKS_EXCLUDED(notify_mu_);
214 virtual
void PreHandleEvent(
219 TF_LOCKS_EXCLUDED(mu_, notify_mu_);
221 const Options options_;
229 ServableMap states_ TF_GUARDED_BY(mu_);
233 ServableMap live_states_ TF_GUARDED_BY(mu_);
238 BoundedLog log_ TF_GUARDED_BY(mu_);
240 std::vector<ServableStateNotificationRequest>
241 servable_state_notification_requests_ TF_GUARDED_BY(mu_);
247 mutable mutex notify_mu_;
248 std::vector<NotifyFn> notify_fns_ TF_GUARDED_BY(notify_mu_);
255 return a.event_time_micros == b.event_time_micros && a.state == b.state;
258 inline bool operator!=(
const ServableStateMonitor::ServableStateAndTime& a,
259 const ServableStateMonitor::ServableStateAndTime& b) {
263 inline std::ostream& operator<<(
265 const ServableStateMonitor::ServableStateAndTime& state_and_time) {
266 return os << state_and_time.DebugString();
BoundedLog GetBoundedLog() const TF_LOCKS_EXCLUDED(mu_)
Returns the current bounded log of handled servable state events.
VersionMap GetVersionStates(const string &servable_name) const TF_LOCKS_EXCLUDED(mu_)
ServableMap GetLiveServableStates() const TF_LOCKS_EXCLUDED(mu_)
std::function< void(bool reached_goal_state, const std::map< ServableId, ServableState::ManagerState > &states_reached)> ServableStateNotifierFn
absl::optional< ServableState > GetState(const ServableId &servable_id) const TF_LOCKS_EXCLUDED(mu_)
absl::optional< ServableStateAndTime > GetStateAndTime(const ServableId &servable_id) const TF_LOCKS_EXCLUDED(mu_)
bool WaitUntilServablesReachStateWithTimeout(const std::vector< ServableRequest > &servables, ServableState::ManagerState goal_state, absl::Duration timeout, std::map< ServableId, ServableState::ManagerState > *states_reached=nullptr) TF_LOCKS_EXCLUDED(mu_) TF_MUST_USE_RESULT
ServableMap GetAllServableStates() const TF_LOCKS_EXCLUDED(mu_)
Returns the current states of all tracked versions of all servables.
void ForgetUnloadedServableStates() TF_LOCKS_EXCLUDED(mu_)
uint64_t max_count_log_events
string DebugString() const
ServableState state
State of the servable.
uint64_t event_time_micros
Time at which servable state event was published.