TensorFlow Serving C++ API Documentation
static_source_router.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_SOURCE_ROUTER_H_
17 #define TENSORFLOW_SERVING_CORE_STATIC_SOURCE_ROUTER_H_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "tensorflow/core/lib/strings/str_util.h"
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow_serving/core/source_router.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 
30 // A SourceRouter with N statically-configured output ports. Items are routed to
31 // output ports based on substring matching against the servable name. The
32 // router is configured with N-1 substrings, with "fall-through" semantics. In
33 // particular: The substrings are numbered 0, 1, ..., N-2. Items whose servable
34 // name matches substring 0 are sent to port 0; items that fail to match
35 // substring 0 but do match substring 1 are sent to port 1; and so on. Items
36 // that match none of the substrings are sent to port N-1.
37 template <typename T>
38 class StaticSourceRouter final : public SourceRouter<T> {
39  public:
40  // Creates a StaticSourceRouter with 'route_substrings.size() + 1' output
41  // ports, based on cascading substring matching as described above.
42  static Status Create(const std::vector<string>& route_substrings,
43  std::unique_ptr<StaticSourceRouter<T>>* result);
44  ~StaticSourceRouter() override;
45 
46  protected:
47  int num_output_ports() const override {
48  return routes_except_default_.size() + 1;
49  }
50 
51  int Route(const StringPiece servable_name,
52  const std::vector<ServableData<T>>& versions) override;
53 
54  private:
55  explicit StaticSourceRouter(const std::vector<string>& route_substrings);
56 
57  // The substrings of the first N-1 routes (the Nth route is the default
58  // route).
59  std::vector<string> routes_except_default_;
60 
61  TF_DISALLOW_COPY_AND_ASSIGN(StaticSourceRouter);
62 };
63 
65 // Implementation details follow. API users need not read.
66 
67 template <typename T>
69  const std::vector<string>& route_substrings,
70  std::unique_ptr<StaticSourceRouter<T>>* result) {
71  result->reset(new StaticSourceRouter<T>(route_substrings));
72  return Status();
73 }
74 
75 template <typename T>
76 StaticSourceRouter<T>::~StaticSourceRouter() {
77  TargetBase<T>::Detach();
78 }
79 
80 template <typename T>
81 int StaticSourceRouter<T>::Route(const StringPiece servable_name,
82  const std::vector<ServableData<T>>& versions) {
83  for (int i = 0; i < routes_except_default_.size(); ++i) {
84  if (str_util::StrContains(servable_name, routes_except_default_[i])) {
85  VLOG(2) << "Routing servable(s) from stream " << servable_name
86  << " to route " << i;
87  return i;
88  }
89  }
90  // None of the substrings matched, so return the "default" Nth route.
91  VLOG(2) << "Routing servable(s) from stream " << servable_name
92  << " to default route " << routes_except_default_.size();
93  return routes_except_default_.size();
94 }
95 
96 template <typename T>
97 StaticSourceRouter<T>::StaticSourceRouter(
98  const std::vector<string>& route_substrings)
99  : routes_except_default_(route_substrings) {}
100 
101 } // namespace serving
102 } // namespace tensorflow
103 
104 #endif // TENSORFLOW_SERVING_CORE_STATIC_SOURCE_ROUTER_H_