TensorFlow Serving C++ API Documentation
manager.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_MANAGER_H_
17 #define TENSORFLOW_SERVING_CORE_MANAGER_H_
18 
19 #include <algorithm>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "absl/types/optional.h"
26 #include "tensorflow/core/lib/core/errors.h"
27 #include "tensorflow/core/lib/core/status.h"
28 #include "tensorflow/core/lib/strings/strcat.h"
29 #include "tensorflow/core/platform/types.h"
30 #include "tensorflow_serving/core/servable_handle.h"
31 #include "tensorflow_serving/core/servable_id.h"
32 
33 namespace tensorflow {
34 namespace serving {
35 
40  // Initialization factories, for convenience and readability.
41  static ServableRequest Specific(const string& name, const int64_t version);
42  static ServableRequest Earliest(const string& name);
43  static ServableRequest Latest(const string& name);
44  static ServableRequest FromId(const ServableId& id);
45 
46  // The name of a servable stream.
47  string name;
48 
49  // An optional specific version number to use.
50  absl::optional<int64_t> version;
51 
52  // How to choose a version number automatically, if 'version' is left unset.
53  enum class AutoVersionPolicy {
54  // Choose the loaded version with the smallest version number.
55  kEarliest,
56 
57  // Choose the loaded version with the largest version number.
58  kLatest,
59  };
60  AutoVersionPolicy auto_version_policy = AutoVersionPolicy::kLatest;
61 
62  // Emits a string representation; for logging and debugging use only.
63  string DebugString() const;
64 
66  // Legacy constructors. Do not use in new code.
67  ServableRequest() = default;
68  ServableRequest(const string& name_in,
69  const absl::optional<int64_t>& version_in)
70  : name(name_in),
71  version(version_in),
72  auto_version_policy(AutoVersionPolicy::kLatest) {}
73 };
74 
77 class Manager {
78  public:
79  virtual ~Manager() = default;
80 
83  virtual std::vector<ServableId> ListAvailableServableIds() const = 0;
84 
90  template <typename T>
91  std::map<ServableId, ServableHandle<T>> GetAvailableServableHandles() const;
92 
99  template <typename T>
100  Status GetServableHandle(const ServableRequest& request,
101  ServableHandle<T>* const handle);
102 
103  private:
104  friend class ManagerWrapper;
105 
106  // Returns an UntypedServableHandle given a ServableRequest.
107  // Returns error if no such Servable is available -- e.g. not yet loaded, has
108  // been quiesced/unloaded, etc.
109  virtual Status GetUntypedServableHandle(
110  const ServableRequest& request,
111  std::unique_ptr<UntypedServableHandle>* untyped_handle) = 0;
112 
113  // Returns a map of all the available servable ids to their corresponding
114  // UntypedServableHandles.
115  virtual std::map<ServableId, std::unique_ptr<UntypedServableHandle>>
116  GetAvailableUntypedServableHandles() const = 0;
117 };
118 
119 //
120 // Implementation details follow. API users need not read.
121 //
122 
123 inline ServableRequest ServableRequest::Specific(const string& name,
124  const int64_t version) {
125  ServableRequest request;
126  request.name = name;
127  request.version = version;
128  return request;
129 }
130 
131 inline ServableRequest ServableRequest::Earliest(const string& name) {
132  ServableRequest request;
133  request.name = name;
134  request.auto_version_policy = AutoVersionPolicy::kEarliest;
135  return request;
136 }
137 
138 inline ServableRequest ServableRequest::Latest(const string& name) {
139  ServableRequest request;
140  request.name = name;
141  request.auto_version_policy = AutoVersionPolicy::kLatest;
142  return request;
143 }
144 
145 inline ServableRequest ServableRequest::FromId(const ServableId& id) {
146  DCHECK_GE(id.version, 0);
147  return Specific(id.name, id.version);
148 }
149 
150 inline string ServableRequest::DebugString() const {
151  if (version) {
152  return strings::StrCat("Specific(", name, ", ", version.value(), ")");
153  } else {
154  switch (auto_version_policy) {
155  case AutoVersionPolicy::kEarliest:
156  return strings::StrCat("Earliest(", name, ")");
157  case AutoVersionPolicy::kLatest:
158  return strings::StrCat("Latest(", name, ")");
159  }
160  }
161 }
162 
163 template <typename T>
165  ServableHandle<T>* const handle) {
166  std::unique_ptr<UntypedServableHandle> untyped_handle;
167  TF_RETURN_IF_ERROR(GetUntypedServableHandle(request, &untyped_handle));
168  if (untyped_handle == nullptr) {
169  return errors::Internal("Manager returned a null handle with OK status.");
170  }
171  *handle = ServableHandle<T>(std::move(untyped_handle));
172  if (handle->get() == nullptr) {
173  return errors::InvalidArgument(
174  "Servable type doesn't match the asked for type.");
175  }
176  return Status();
177 }
178 
179 template <typename T>
180 std::map<ServableId, ServableHandle<T>> Manager::GetAvailableServableHandles()
181  const {
182  std::map<ServableId, ServableHandle<T>> id_and_handles;
183  std::map<ServableId, std::unique_ptr<UntypedServableHandle>>
184  id_and_untyped_handles = GetAvailableUntypedServableHandles();
185  for (auto& id_and_untyped_handle : id_and_untyped_handles) {
186  auto handle = ServableHandle<T>(std::move(id_and_untyped_handle.second));
187  if (handle.get() != nullptr) {
188  id_and_handles.emplace(id_and_untyped_handle.first, std::move(handle));
189  }
190  }
191  return id_and_handles;
192 }
193 
194 } // namespace serving
195 } // namespace tensorflow
196 
197 #endif // TENSORFLOW_SERVING_CORE_MANAGER_H_
virtual std::vector< ServableId > ListAvailableServableIds() const =0
std::map< ServableId, ServableHandle< T > > GetAvailableServableHandles() const
Definition: manager.h:180
Status GetServableHandle(const ServableRequest &request, ServableHandle< T > *const handle)
Definition: manager.h:164