blob: 5a27fff09a421b56bbfa8c78f322e8a3e23c5c3d [file] [log] [blame]
vjpaidea740f2015-02-26 16:35:35 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <forward_list>
35#include <functional>
Vijay Pai8ad32092015-03-23 12:44:28 -070036#include <mutex>
vjpaidea740f2015-02-26 16:35:35 -080037#include <sys/time.h>
38#include <sys/resource.h>
39#include <sys/signal.h>
40#include <thread>
41
42#include <gflags/gflags.h>
43#include <grpc/support/alloc.h>
44#include <grpc/support/host_port.h>
45#include <grpc++/async_unary_call.h>
46#include <grpc++/config.h>
47#include <grpc++/server.h>
48#include <grpc++/server_builder.h>
49#include <grpc++/server_context.h>
Craig Tiller61691f92015-03-02 08:51:52 -080050#include <grpc++/server_credentials.h>
vjpaidea740f2015-02-26 16:35:35 -080051#include <grpc++/status.h>
vjpai46f65232015-03-23 10:10:27 -070052#include <grpc++/stream.h>
vjpaidea740f2015-02-26 16:35:35 -080053#include <gtest/gtest.h>
54#include "src/cpp/server/thread_pool.h"
55#include "test/core/util/grpc_profiler.h"
56#include "test/cpp/qps/qpstest.pb.h"
Craig Tillerd6479d62015-03-04 12:50:11 -080057#include "test/cpp/qps/server.h"
vjpaidea740f2015-02-26 16:35:35 -080058
59#include <grpc/grpc.h>
60#include <grpc/support/log.h>
61
Craig Tillerd6479d62015-03-04 12:50:11 -080062namespace grpc {
Craig Tillera182bf12015-03-04 13:54:39 -080063namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080064
Craig Tillerd6479d62015-03-04 12:50:11 -080065class AsyncQpsServerTest : public Server {
vjpaidea740f2015-02-26 16:35:35 -080066 public:
Craig Tillera182bf12015-03-04 13:54:39 -080067 AsyncQpsServerTest(const ServerConfig &config, int port)
Vijay Pai8ad32092015-03-23 12:44:28 -070068 : srv_cq_(), async_service_(&srv_cq_), server_(nullptr),
69 shutdown_(false) {
vjpaidea740f2015-02-26 16:35:35 -080070 char *server_address = NULL;
Craig Tillerd6479d62015-03-04 12:50:11 -080071 gpr_join_host_port(&server_address, "::", port);
vjpaidea740f2015-02-26 16:35:35 -080072
73 ServerBuilder builder;
Craig Tiller61691f92015-03-02 08:51:52 -080074 builder.AddPort(server_address, InsecureServerCredentials());
Craig Tillerd6479d62015-03-04 12:50:11 -080075 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080076
77 builder.RegisterAsyncService(&async_service_);
78
79 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080080
81 using namespace std::placeholders;
82 request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall,
83 &async_service_, _1, _2, _3, &srv_cq_, _4);
vjpai46f65232015-03-23 10:10:27 -070084 request_streaming_ =
85 std::bind(&TestService::AsyncService::RequestStreamingCall,
86 &async_service_, _1, _2, &srv_cq_, _3);
vjpaidea740f2015-02-26 16:35:35 -080087 for (int i = 0; i < 100; i++) {
Vijay Pai64ac47f2015-02-26 17:59:51 -080088 contexts_.push_front(
89 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
vjpai46f65232015-03-23 10:10:27 -070090 request_unary_, ProcessRPC));
91 contexts_.push_front(
92 new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
93 request_streaming_, ProcessRPC));
vjpaidea740f2015-02-26 16:35:35 -080094 }
Craig Tillerd6479d62015-03-04 12:50:11 -080095 for (int i = 0; i < config.threads(); i++) {
Vijay Paiacf6f312015-03-02 15:13:39 -080096 threads_.push_back(std::thread([=]() {
vjpaidea740f2015-02-26 16:35:35 -080097 // Wait until work is available or we are shutting down
98 bool ok;
99 void *got_tag;
100 while (srv_cq_.Next(&got_tag, &ok)) {
vjpai46f65232015-03-23 10:10:27 -0700101 ServerRpcContext *ctx = detag(got_tag);
102 // The tag is a pointer to an RPC context to invoke
103 if (ctx->RunNextState(ok) == false) {
104 // this RPC context is done, so refresh it
Vijay Pai8ad32092015-03-23 12:44:28 -0700105 std::lock_guard<std::mutex> g(shutdown_mutex_);
106 if (!shutdown_)
107 ctx->Reset();
vjpai46f65232015-03-23 10:10:27 -0700108 }
vjpaidea740f2015-02-26 16:35:35 -0800109 }
110 return;
111 }));
112 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800113 }
114 ~AsyncQpsServerTest() {
115 server_->Shutdown();
Vijay Pai8ad32092015-03-23 12:44:28 -0700116 {
117 std::lock_guard<std::mutex> g(shutdown_mutex_);
118 shutdown_ = true;
119 srv_cq_.Shutdown();
120 }
Craig Tillera182bf12015-03-04 13:54:39 -0800121 for (auto &thr : threads_) {
Craig Tillerd6479d62015-03-04 12:50:11 -0800122 thr.join();
123 }
124 while (!contexts_.empty()) {
125 delete contexts_.front();
126 contexts_.pop_front();
vjpaidea740f2015-02-26 16:35:35 -0800127 }
128 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800129
vjpaidea740f2015-02-26 16:35:35 -0800130 private:
131 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800132 public:
vjpaidea740f2015-02-26 16:35:35 -0800133 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800134 virtual ~ServerRpcContext(){};
vjpai46f65232015-03-23 10:10:27 -0700135 virtual bool RunNextState(bool) = 0; // next state, return false if done
Craig Tillera182bf12015-03-04 13:54:39 -0800136 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800137 };
138 static void *tag(ServerRpcContext *func) {
139 return reinterpret_cast<void *>(func);
140 }
141 static ServerRpcContext *detag(void *tag) {
142 return reinterpret_cast<ServerRpcContext *>(tag);
143 }
144
145 template <class RequestType, class ResponseType>
vjpai46f65232015-03-23 10:10:27 -0700146 class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext {
vjpaidea740f2015-02-26 16:35:35 -0800147 public:
148 ServerRpcContextUnaryImpl(
vjpai4e1e1bc2015-02-27 23:47:12 -0800149 std::function<void(ServerContext *, RequestType *,
150 grpc::ServerAsyncResponseWriter<ResponseType> *,
151 void *)> request_method,
152 std::function<grpc::Status(const RequestType *, ResponseType *)>
153 invoke_method)
vjpaidea740f2015-02-26 16:35:35 -0800154 : next_state_(&ServerRpcContextUnaryImpl::invoker),
155 request_method_(request_method),
156 invoke_method_(invoke_method),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800157 response_writer_(&srv_ctx_) {
vjpaidea740f2015-02-26 16:35:35 -0800158 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800159 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800160 }
vjpai3c110662015-02-27 07:17:16 -0800161 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
vjpai46f65232015-03-23 10:10:27 -0700162 bool RunNextState(bool ok) GRPC_OVERRIDE {return (this->*next_state_)(ok);}
vjpai5b39f9a2015-02-27 09:33:00 -0800163 void Reset() GRPC_OVERRIDE {
vjpaidea740f2015-02-26 16:35:35 -0800164 srv_ctx_ = ServerContext();
165 req_ = RequestType();
166 response_writer_ =
Vijay Pai64ac47f2015-02-26 17:59:51 -0800167 grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
vjpaidea740f2015-02-26 16:35:35 -0800168
169 // Then request the method
170 next_state_ = &ServerRpcContextUnaryImpl::invoker;
171 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800172 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800173 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800174
vjpaidea740f2015-02-26 16:35:35 -0800175 private:
vjpai46f65232015-03-23 10:10:27 -0700176 bool finisher(bool) { return false; }
177 bool invoker(bool ok) {
178 if (!ok)
179 return false;
180
vjpaidea740f2015-02-26 16:35:35 -0800181 ResponseType response;
182
183 // Call the RPC processing function
184 grpc::Status status = invoke_method_(&req_, &response);
185
186 // Have the response writer work and invoke on_finish when done
187 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800188 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800189 return true;
190 }
191 ServerContext srv_ctx_;
192 RequestType req_;
vjpai46f65232015-03-23 10:10:27 -0700193 bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
vjpai4e1e1bc2015-02-27 23:47:12 -0800194 std::function<void(ServerContext *, RequestType *,
195 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
196 request_method_;
197 std::function<grpc::Status(const RequestType *, ResponseType *)>
198 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800199 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
200 };
201
vjpai46f65232015-03-23 10:10:27 -0700202 template <class RequestType, class ResponseType>
203 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext {
204 public:
205 ServerRpcContextStreamingImpl(
Vijay Pai8ad32092015-03-23 12:44:28 -0700206 std::function<void(ServerContext *,
vjpai46f65232015-03-23 10:10:27 -0700207 grpc::ServerAsyncReaderWriter<ResponseType,
208 RequestType> *, void *)> request_method,
209 std::function<grpc::Status(const RequestType *, ResponseType *)>
210 invoke_method)
211 : next_state_(&ServerRpcContextStreamingImpl::request_done),
212 request_method_(request_method),
213 invoke_method_(invoke_method),
214 stream_(&srv_ctx_) {
215 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
216 }
217 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {
218 }
219 bool RunNextState(bool ok) GRPC_OVERRIDE {return (this->*next_state_)(ok);}
220 void Reset() GRPC_OVERRIDE {
221 srv_ctx_ = ServerContext();
222 req_ = RequestType();
223 stream_ = grpc::ServerAsyncReaderWriter<ResponseType,
224 RequestType>(&srv_ctx_);
225
226 // Then request the method
227 next_state_ = &ServerRpcContextStreamingImpl::request_done;
228 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
229 }
230
231 private:
232 bool request_done(bool ok) {
233 if (!ok)
234 return false;
235 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
236 next_state_ = &ServerRpcContextStreamingImpl::read_done;
237 return true;
238 }
Vijay Pai8ad32092015-03-23 12:44:28 -0700239
vjpai46f65232015-03-23 10:10:27 -0700240 bool read_done(bool ok) {
241 if (ok) {
242 // invoke the method
243 ResponseType response;
244 // Call the RPC processing function
245 grpc::Status status = invoke_method_(&req_, &response);
246 // initiate the write
247 stream_.Write(response, AsyncQpsServerTest::tag(this));
248 next_state_ = &ServerRpcContextStreamingImpl::write_done;
249 } else { // client has sent writes done
250 // finish the stream
251 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
252 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
253 }
254 return true;
255 }
256 bool write_done(bool ok) {
257 // now go back and get another streaming read!
258 if (ok) {
259 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
260 next_state_ = &ServerRpcContextStreamingImpl::read_done;
261 }
262 else {
263 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
264 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
265 }
266 return true;
267 }
268 bool finish_done(bool ok) {return false; /* reset the context */ }
269
270 ServerContext srv_ctx_;
271 RequestType req_;
272 bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
273 std::function<void(ServerContext *,
274 grpc::ServerAsyncReaderWriter<ResponseType,
275 RequestType> *, void *)> request_method_;
276 std::function<grpc::Status(const RequestType *, ResponseType *)>
277 invoke_method_;
278 grpc::ServerAsyncReaderWriter<ResponseType,RequestType> stream_;
279 };
280
281 static Status ProcessRPC(const SimpleRequest *request,
282 SimpleResponse *response) {
283 if (request->response_size() > 0) {
vjpaidea740f2015-02-26 16:35:35 -0800284 if (!SetPayload(request->response_type(), request->response_size(),
285 response->mutable_payload())) {
286 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
287 }
288 }
289 return Status::OK;
290 }
291 CompletionQueue srv_cq_;
292 TestService::AsyncService async_service_;
Vijay Paiacf6f312015-03-02 15:13:39 -0800293 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800294 std::unique_ptr<grpc::Server> server_;
vjpai4e1e1bc2015-02-27 23:47:12 -0800295 std::function<void(ServerContext *, SimpleRequest *,
296 grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)>
297 request_unary_;
vjpai46f65232015-03-23 10:10:27 -0700298 std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter<
299 SimpleResponse,SimpleRequest> *, void *)>
300 request_streaming_;
vjpaidea740f2015-02-26 16:35:35 -0800301 std::forward_list<ServerRpcContext *> contexts_;
Vijay Pai8ad32092015-03-23 12:44:28 -0700302
303 std::mutex shutdown_mutex_;
304 bool shutdown_;
vjpaidea740f2015-02-26 16:35:35 -0800305};
306
Craig Tillera182bf12015-03-04 13:54:39 -0800307std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config,
308 int port) {
309 return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port));
vjpaidea740f2015-02-26 16:35:35 -0800310}
311
Craig Tillera182bf12015-03-04 13:54:39 -0800312} // namespace testing
313} // namespace grpc