16 #ifndef TENSORFLOW_SERVING_CORE_LOADER_HARNESS_H_
17 #define TENSORFLOW_SERVING_CORE_LOADER_HARNESS_H_
21 #include "absl/status/status.h"
22 #include "absl/types/optional.h"
23 #include "tensorflow/core/lib/core/status.h"
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow/core/platform/mutex.h"
26 #include "tensorflow/core/platform/thread_annotations.h"
27 #include "tensorflow/core/platform/types.h"
28 #include "tensorflow_serving/core/loader.h"
29 #include "tensorflow_serving/core/servable_id.h"
31 namespace tensorflow {
37 struct ServableStateSnapshot;
108 std::function<void(
const ServableId&
id,
const Status& error)>
117 template <
typename T>
122 loader_(std::move(
loader)),
125 should_retry_([&](absl::Status
status) {
return true; }) {}
142 template <
typename T = std::
nullptr_t>
166 Status
Load() TF_LOCKS_EXCLUDED(mu_);
182 TF_LOCKS_EXCLUDED(mu_);
192 Status
Unload() TF_LOCKS_EXCLUDED(mu_);
210 void Error(const Status&
status) TF_LOCKS_EXCLUDED(mu_);
217 Status
status() const TF_LOCKS_EXCLUDED(mu_);
221 template <typename T>
223 return additional_state_.
get<T>();
232 void ErrorInternal(
const Status&
status) TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
237 Status TransitionState(
State from,
State to) TF_EXCLUSIVE_LOCKS_REQUIRED(mu_);
239 Status UnloadInternal(
State from_state) TF_LOCKS_EXCLUDED(mu_);
240 Status UnloadDueToCancelledLoad() TF_LOCKS_EXCLUDED(mu_);
243 const std::unique_ptr<
Loader> loader_;
246 const Options options_;
248 State state_ TF_GUARDED_BY(mu_) =
State::kNew;
250 Status status_ TF_GUARDED_BY(mu_);
255 std::function<
bool(absl::Status)> should_retry_ TF_GUARDED_BY(mu_);
264 template <typename T = std::nullptr_t>
271 template <
typename T>
274 return a.id == b.id && a.state == b.state &&
275 a.additional_state == b.additional_state;
278 template <
typename T>
279 inline bool operator!=(
const ServableStateSnapshot<T>& a,
280 const ServableStateSnapshot<T>& b) {
285 os << LoaderHarness::StateDebugString(
state);
293 template <
typename T>
296 if (additional_state_.
get<T>() ==
nullptr) {
297 return {id_, state_, {}};
299 return {id_, state_, {*additional_state_.
get<T>()}};
310 return {id_, state_, {}};
void set_should_retry(std::function< bool(absl::Status)> should_retry) TF_LOCKS_EXCLUDED(mu_)
Status DoneQuiescing() TF_LOCKS_EXCLUDED(mu_)
State state() const TF_LOCKS_EXCLUDED(mu_)
Returns the current state of underlying Servable.
Status Unload() TF_LOCKS_EXCLUDED(mu_)
Status UnloadRequested() TF_LOCKS_EXCLUDED(mu_)
LoaderHarness(const ServableId &id, std::unique_ptr< Loader > loader, std::unique_ptr< T > additional_state, const Options &options=Options())
Status StartQuiescing() TF_LOCKS_EXCLUDED(mu_)
@ kLoadRequested
The manager has been requested to load this servable.
@ kReady
'loader_->Load()' has succeeded.
@ kUnloadRequested
The manager has been requested to unload this servable.
@ kDisabled
'loader_->Unload()' has finished.
@ kLoading
'loader_->Load()' has been called.
@ kQuiesced
The servable has been made unavailable for serving.
@ kQuiescing
The servable is going to be made unavailable for serving.
@ kUnloading
'loader_->Unload()' has been called.
void Error(const Status &status) TF_LOCKS_EXCLUDED(mu_)
Transitions the state to kError and invokes 'options_.error_callback'.
Status LoadApproved() TF_LOCKS_EXCLUDED(mu_)
ServableId id() const
Returns the identifier of underlying Servable.
bool should_retry(absl::Status status) TF_LOCKS_EXCLUDED(mu_)
Returns true if the servable should be retried.
Status LoadRequested() TF_LOCKS_EXCLUDED(mu_)
ServableStateSnapshot< T > loader_state_snapshot() const TF_LOCKS_EXCLUDED(mu_)
Returns the current overall state snapshot of the underlying Servable.
Status Load() TF_LOCKS_EXCLUDED(mu_)
Status status() const TF_LOCKS_EXCLUDED(mu_)
T * get() const
Accessor for the underlying pointer if it is of type T, otherwise null.
Options to configure a LoaderHarness.
uint32 max_num_load_retries
std::function< void(const ServableId &id, const Status &error)> error_callback
An (optional) function to call upon transitioning to state kError.
uint64_t load_retry_interval_micros
The interval, in microseconds, between each servable load retry.