TensorFlow Serving C++ API Documentation
http_rest_api_handler.h
1 /* Copyright 2018 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 #ifndef TENSORFLOW_SERVING_MODEL_SERVERS_HTTP_REST_API_HANDLER_H_
16 #define TENSORFLOW_SERVING_MODEL_SERVERS_HTTP_REST_API_HANDLER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "re2/re2.h"
26 #include "tensorflow/core/lib/core/status.h"
27 #include "tensorflow/core/protobuf/config.pb.h"
28 #include "tensorflow/core/protobuf/meta_graph.pb.h"
29 #include "tensorflow_serving/model_servers/http_rest_api_handler_base.h"
30 
31 namespace tensorflow {
32 
33 class SignatureDef;
34 
35 namespace serving {
36 
37 class ServerCore;
38 class TensorflowPredictor;
39 class ModelSpec;
40 
41 // HttpRestApiHandler handles HTTP/REST APIs of TF serving.
42 //
43 // Currently supported APIs are as follows:
44 //
45 // o Inference - Classify/Regress/Predict
46 //
47 // POST /v1/models/<model_name>:(classify|regress|predict)
48 // POST /v1/models/<model_name>/versions/<ver>:(classify|regress|predict)
49 //
50 // o Model status
51 //
52 // GET /v1/models/<model_name> (status of all versions)
53 // GET /v1/models/<model_name>/versions/<ver> (status of specific version)
54 //
55 // The API is documented here:
56 // tensorflow_serving/g3doc/api_rest.md
57 //
58 // Users of this class should typically create one instance of it at process
59 // startup, register paths defined by kPathRegex with the in-process HTTP
60 // server, and when a request arrives, forward the request to ProcessRequest()
61 // method.
62 //
63 // This class is thread safe.
65  public:
66  // Returns a regex that captures all API paths handled by this handler.
67  // Typical use of this method is to register request paths with underlying
68  // HTTP server, so incoming requests can be forwarded to this handler.
69  static const char* const kPathRegex;
70 
71  // API calls are configured to timeout after `timeout_in_ms`.
72  // `core` is not owned and is expected to outlive HttpRestApiHandler
73  // instance.
74  HttpRestApiHandler(int timeout_in_ms, ServerCore* core);
75 
76  ~HttpRestApiHandler() override;
77 
78  // Process a HTTP request.
79  //
80  // In case of errors, the `headers` and `output` are still relevant as they
81  // contain detailed error messages, that can be relayed back to the client.
82  Status ProcessRequest(const absl::string_view http_method,
83  const absl::string_view request_path,
84  const absl::string_view request_body,
85  std::vector<std::pair<string, string>>* headers,
86  string* model_name, string* method,
87  string* output) override;
88 
89  private:
90  Status ProcessClassifyRequest(
91  const absl::string_view model_name,
92  const absl::optional<int64_t>& model_version,
93  const absl::optional<absl::string_view>& model_version_label,
94  const absl::string_view request_body, string* output);
95  Status ProcessRegressRequest(
96  const absl::string_view model_name,
97  const absl::optional<int64_t>& model_version,
98  const absl::optional<absl::string_view>& model_version_label,
99  const absl::string_view request_body, string* output);
100  Status ProcessPredictRequest(
101  const absl::string_view model_name,
102  const absl::optional<int64_t>& model_version,
103  const absl::optional<absl::string_view>& model_version_label,
104  const absl::string_view request_body, string* output);
105  Status ProcessModelStatusRequest(
106  const absl::string_view model_name,
107  const absl::optional<int64_t>& model_version,
108  const absl::optional<absl::string_view>& model_version_label,
109  string* output);
110  Status ProcessModelMetadataRequest(
111  const absl::string_view model_name,
112  const absl::optional<int64_t>& model_version,
113  const absl::optional<absl::string_view>& model_version_label,
114  string* output);
115  Status GetInfoMap(const ModelSpec& model_spec, const string& signature_name,
116  ::google::protobuf::Map<string, tensorflow::TensorInfo>* infomap);
117 
118  RunOptions run_options_;
119  ServerCore* core_;
120  std::unique_ptr<TensorflowPredictor> predictor_;
121 };
122 
123 } // namespace serving
124 } // namespace tensorflow
125 
126 #endif // TENSORFLOW_SERVING_MODEL_SERVERS_HTTP_REST_API_HANDLER_H_