TensorFlow Serving C++ API Documentation
servable_state.h
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_CORE_SERVABLE_STATE_H_
17 #define TENSORFLOW_SERVING_CORE_SERVABLE_STATE_H_
18 
19 #include <string>
20 
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow_serving/core/servable_id.h"
23 
24 namespace tensorflow {
25 namespace serving {
26 
27 // The state of a servable. Typically published on an EventBus by a manager.
28 // Since managers (and EventBus implementations) can in general have multiple
29 // threads, this is really a snapshot of a servable's recent state, not a
30 // guarantee about its current state.
31 //
32 // Note that this is a semantic state, meant to be independent of the
33 // implementation of a particular kind of manager.
34 struct ServableState {
35  // The identifier of the servable whose state is represented.
36  ServableId id;
37 
38  // The state of the servable as maintained by a manager. These states can only
39  // transition from higher to lower ones on this list.
40  enum class ManagerState : int {
41  // The manager is tracking this servable, but has not initiated any action
42  // pertaining to it.
43  kStart,
44 
45  // The manager has decided to load this servable. In particular, checks
46  // around resource availability and other aspects have passed, and the
47  // manager is about to invoke the loader's Load() method.
48  kLoading,
49 
50  // The manager has successfully loaded this servable and made it available
51  // for serving (i.e. GetServableHandle(id) will succeed). To avoid races,
52  // this state is not reported until *after* the servable is made available.
53  kAvailable,
54 
55  // The manager has decided to make this servable unavailable, and unload it.
56  // To avoid races, this state is reported *before* the servable is made
57  // unavailable.
58  kUnloading,
59 
60  // This servable has reached the end of its journey in the manager. Either
61  // it loaded and ultimately unloaded successfully, or it hit an error at
62  // some point in its lifecycle.
63  kEnd,
64  };
65 
66  static string ManagerStateString(ManagerState state) {
67  switch (state) {
68  case ManagerState::kStart:
69  return "Start";
70  case ManagerState::kLoading:
71  return "Loading";
72  case ManagerState::kAvailable:
73  return "Available";
74  case ManagerState::kUnloading:
75  return "Unloading";
76  case ManagerState::kEnd:
77  return "End";
78  }
79  }
80 
81  ManagerState manager_state;
82 
83  // Whether anything has gone wrong with this servable. If not OK, the error
84  // could be something that occurred in a Source or SourceAdapter, in the
85  // servable's Loader, in the Manager, or elsewhere. All errors pertaining to
86  // the servable are reported here, regardless of origin.
87  Status health;
88 
89  // Returns a string representation of this object. Useful in logging.
90  string DebugString() const {
91  return strings::StrCat("id: ", id.DebugString(), " manager_state: ",
92  ManagerStateString(manager_state),
93  " health: ", health.ToString());
94  }
95 };
96 
97 inline bool operator==(const ServableState& a, const ServableState& b) {
98  return a.id == b.id && a.manager_state == b.manager_state &&
99  a.health == b.health;
100 }
101 
102 inline bool operator!=(const ServableState& a, const ServableState& b) {
103  return !(a == b);
104 }
105 
106 inline std::ostream& operator<<(std::ostream& os, const ServableState& state) {
107  return os << state.DebugString();
108 }
109 
110 } // namespace serving
111 } // namespace tensorflow
112 
113 #endif // TENSORFLOW_SERVING_CORE_SERVABLE_STATE_H_