TensorFlow Serving C++ API Documentation
static_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_STATIC_MANAGER_H_
17 #define TENSORFLOW_SERVING_CORE_STATIC_MANAGER_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/core/lib/core/errors.h"
22 #include "tensorflow_serving/core/basic_manager.h"
23 #include "tensorflow_serving/core/manager.h"
24 #include "tensorflow_serving/core/servable_handle.h"
25 #include "tensorflow_serving/core/servable_id.h"
26 #include "tensorflow_serving/core/simple_loader.h"
27 
28 namespace tensorflow {
29 namespace serving {
30 
31 // Builds a manager that holds a static set of servables. The result is
32 // immutable, and cannot be modified after construction.
33 //
34 // Typical callers will call AddServable() for each desired servable, and then
35 // call Build() to produce a Manager.
37  public:
39 
40  // Adds a servable to the manager. Duplicate IDs and null servables will fail
41  // to be added and return a failure status.
42  template <typename T>
43  Status AddServable(const ServableId& id, std::unique_ptr<T> servable);
44 
45  // Builds the manager. This builder should not be reused after this.
46  std::unique_ptr<Manager> Build();
47 
48  private:
49  // The manager we are building.
50  std::unique_ptr<BasicManager> basic_manager_;
51 
52  // The health of the builder.
53  Status health_;
54 };
55 
56 //
57 // Implementation details follow. Clients can stop reading.
58 //
59 
60 template <typename T>
61 Status StaticManagerBuilder::AddServable(const ServableId& id,
62  std::unique_ptr<T> servable) {
63  if (servable == nullptr) {
64  return errors::InvalidArgument("Servable cannot be null.");
65  }
66  TF_RETURN_IF_ERROR(health_);
67  DCHECK(basic_manager_ != nullptr);
68 
69  TF_RETURN_IF_ERROR(basic_manager_->ManageServable(CreateServableData(
70  id, std::unique_ptr<Loader>(new SimpleLoader<T>(
71  [&servable](std::unique_ptr<T>* const returned_servable) {
72  *returned_servable = std::move(servable);
73  return Status();
74  },
75  SimpleLoader<T>::EstimateNoResources())))));
76  Status load_status;
77  Notification load_done;
78  basic_manager_->LoadServable(id, [&](const Status& status) {
79  load_status = status;
80  load_done.Notify();
81  });
82  load_done.WaitForNotification();
83  return load_status;
84 }
85 
86 } // namespace serving
87 } // namespace tensorflow
88 
89 #endif // TENSORFLOW_SERVING_CORE_STATIC_MANAGER_H_