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