blob: 58499f345f524c5bb36d6ce907f746a9f5b65a11 [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"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020055#include "test/cpp/qps/qpstest.grpc.pb.h"
Craig Tillerd6479d62015-03-04 12:50:11 -080056#include "test/cpp/qps/server.h"
vjpaidea740f2015-02-26 16:35:35 -080057
58#include <grpc/grpc.h>
59#include <grpc/support/log.h>
60
Craig Tillerd6479d62015-03-04 12:50:11 -080061namespace grpc {
Craig Tillera182bf12015-03-04 13:54:39 -080062namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080063
Craig Tillerd6479d62015-03-04 12:50:11 -080064class AsyncQpsServerTest : public Server {
vjpaidea740f2015-02-26 16:35:35 -080065 public:
Craig Tillerf9e6adf2015-05-06 11:45:59 -070066 AsyncQpsServerTest(const ServerConfig &config, int port) : shutdown_(false) {
Craig Tiller5c8737d2015-05-21 11:42:17 -070067 char *server_address = NULL;
Craig Tillerd6479d62015-03-04 12:50:11 -080068 gpr_join_host_port(&server_address, "::", port);
vjpaidea740f2015-02-26 16:35:35 -080069
70 ServerBuilder builder;
Nicolas Noblecfd60732015-03-18 16:27:43 -070071 builder.AddListeningPort(server_address, InsecureServerCredentials());
Craig Tillerd6479d62015-03-04 12:50:11 -080072 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080073
74 builder.RegisterAsyncService(&async_service_);
Craig Tillerf9e6adf2015-05-06 11:45:59 -070075 srv_cq_ = builder.AddCompletionQueue();
vjpaidea740f2015-02-26 16:35:35 -080076
77 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080078
79 using namespace std::placeholders;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070080 request_unary_ =
81 std::bind(&TestService::AsyncService::RequestUnaryCall, &async_service_,
82 _1, _2, _3, srv_cq_.get(), srv_cq_.get(), _4);
vjpai46f65232015-03-23 10:10:27 -070083 request_streaming_ =
Craig Tillerf9e6adf2015-05-06 11:45:59 -070084 std::bind(&TestService::AsyncService::RequestStreamingCall,
85 &async_service_, _1, _2, srv_cq_.get(), srv_cq_.get(), _3);
vjpaidea740f2015-02-26 16:35:35 -080086 for (int i = 0; i < 100; i++) {
Vijay Pai64ac47f2015-02-26 17:59:51 -080087 contexts_.push_front(
88 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
vjpai46f65232015-03-23 10:10:27 -070089 request_unary_, ProcessRPC));
90 contexts_.push_front(
91 new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
92 request_streaming_, ProcessRPC));
vjpaidea740f2015-02-26 16:35:35 -080093 }
Craig Tillerd6479d62015-03-04 12:50:11 -080094 for (int i = 0; i < config.threads(); i++) {
Vijay Paiacf6f312015-03-02 15:13:39 -080095 threads_.push_back(std::thread([=]() {
vjpaidea740f2015-02-26 16:35:35 -080096 // Wait until work is available or we are shutting down
97 bool ok;
Craig Tiller5c8737d2015-05-21 11:42:17 -070098 void *got_tag;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070099 while (srv_cq_->Next(&got_tag, &ok)) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700100 ServerRpcContext *ctx = detag(got_tag);
Craig Tiller8221e402015-04-09 12:23:21 -0700101 // The tag is a pointer to an RPC context to invoke
102 if (ctx->RunNextState(ok) == false) {
103 // this RPC context is done, so refresh it
Vijay Pai8ad32092015-03-23 12:44:28 -0700104 std::lock_guard<std::mutex> g(shutdown_mutex_);
Vijay Pai1da729a2015-03-23 12:52:56 -0700105 if (!shutdown_) {
Vijay Paiacf6f312015-03-02 15:13:39 -0800106 ctx->Reset();
107 }
Craig Tiller8221e402015-04-09 12:23:21 -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;
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700119 srv_cq_->Shutdown();
Vijay Pai8ad32092015-03-23 12:44:28 -0700120 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700121 for (auto thr = threads_.begin(); thr != threads_.end(); thr++) {
122 thr->join();
Craig Tillerd6479d62015-03-04 12:50:11 -0800123 }
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 Tiller5c8737d2015-05-21 11:42:17 -0700136 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800137 };
Craig Tiller5c8737d2015-05-21 11:42:17 -0700138 static void *tag(ServerRpcContext *func) {
139 return reinterpret_cast<void *>(func);
vjpaidea740f2015-02-26 16:35:35 -0800140 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700141 static ServerRpcContext *detag(void *tag) {
142 return reinterpret_cast<ServerRpcContext *>(tag);
vjpaidea740f2015-02-26 16:35:35 -0800143 }
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(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700149 std::function<void(ServerContext *, RequestType *,
150 grpc::ServerAsyncResponseWriter<ResponseType> *,
151 void *)> request_method,
152 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800153 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 {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700162 bool RunNextState(bool ok) GRPC_OVERRIDE {
163 return (this->*next_state_)(ok);
164 }
vjpai5b39f9a2015-02-27 09:33:00 -0800165 void Reset() GRPC_OVERRIDE {
vjpaidea740f2015-02-26 16:35:35 -0800166 srv_ctx_ = ServerContext();
167 req_ = RequestType();
168 response_writer_ =
Vijay Pai64ac47f2015-02-26 17:59:51 -0800169 grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
vjpaidea740f2015-02-26 16:35:35 -0800170
171 // Then request the method
172 next_state_ = &ServerRpcContextUnaryImpl::invoker;
173 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800174 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800175 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800176
vjpaidea740f2015-02-26 16:35:35 -0800177 private:
vjpai46f65232015-03-23 10:10:27 -0700178 bool finisher(bool) { return false; }
179 bool invoker(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700180 if (!ok) {
181 return false;
182 }
vjpai46f65232015-03-23 10:10:27 -0700183
vjpaidea740f2015-02-26 16:35:35 -0800184 ResponseType response;
185
186 // Call the RPC processing function
187 grpc::Status status = invoke_method_(&req_, &response);
188
189 // Have the response writer work and invoke on_finish when done
190 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800191 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800192 return true;
193 }
194 ServerContext srv_ctx_;
195 RequestType req_;
vjpai46f65232015-03-23 10:10:27 -0700196 bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700197 std::function<void(ServerContext *, RequestType *,
198 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800199 request_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700200 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800201 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800202 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
203 };
204
vjpai46f65232015-03-23 10:10:27 -0700205 template <class RequestType, class ResponseType>
206 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext {
207 public:
208 ServerRpcContextStreamingImpl(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700209 std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter<
210 ResponseType, RequestType> *,
211 void *)> request_method,
vjpai46f65232015-03-23 10:10:27 -0700212 std::function<grpc::Status(const RequestType *, ResponseType *)>
213 invoke_method)
214 : next_state_(&ServerRpcContextStreamingImpl::request_done),
215 request_method_(request_method),
216 invoke_method_(invoke_method),
217 stream_(&srv_ctx_) {
218 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
219 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700220 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {}
221 bool RunNextState(bool ok) GRPC_OVERRIDE {
222 return (this->*next_state_)(ok);
vjpai46f65232015-03-23 10:10:27 -0700223 }
vjpai46f65232015-03-23 10:10:27 -0700224 void Reset() GRPC_OVERRIDE {
225 srv_ctx_ = ServerContext();
226 req_ = RequestType();
Craig Tiller5c8737d2015-05-21 11:42:17 -0700227 stream_ =
228 grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(&srv_ctx_);
vjpai46f65232015-03-23 10:10:27 -0700229
230 // Then request the method
231 next_state_ = &ServerRpcContextStreamingImpl::request_done;
232 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
233 }
234
235 private:
236 bool request_done(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700237 if (!ok) {
238 return false;
239 }
vjpai46f65232015-03-23 10:10:27 -0700240 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
241 next_state_ = &ServerRpcContextStreamingImpl::read_done;
242 return true;
243 }
Vijay Pai8ad32092015-03-23 12:44:28 -0700244
vjpai46f65232015-03-23 10:10:27 -0700245 bool read_done(bool ok) {
246 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700247 // invoke the method
248 ResponseType response;
249 // Call the RPC processing function
250 grpc::Status status = invoke_method_(&req_, &response);
251 // initiate the write
252 stream_.Write(response, AsyncQpsServerTest::tag(this));
253 next_state_ = &ServerRpcContextStreamingImpl::write_done;
254 } else { // client has sent writes done
255 // finish the stream
256 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
257 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700258 }
259 return true;
260 }
261 bool write_done(bool ok) {
262 // now go back and get another streaming read!
263 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700264 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
265 next_state_ = &ServerRpcContextStreamingImpl::read_done;
266 } else {
267 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
268 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700269 }
270 return true;
271 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700272 bool finish_done(bool ok) { return false; /* reset the context */ }
vjpai46f65232015-03-23 10:10:27 -0700273
274 ServerContext srv_ctx_;
275 RequestType req_;
276 bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700277 std::function<void(
278 ServerContext *,
279 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)>
280 request_method_;
vjpai46f65232015-03-23 10:10:27 -0700281 std::function<grpc::Status(const RequestType *, ResponseType *)>
282 invoke_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700283 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_;
vjpai46f65232015-03-23 10:10:27 -0700284 };
285
Craig Tiller5c8737d2015-05-21 11:42:17 -0700286 static Status ProcessRPC(const SimpleRequest *request,
287 SimpleResponse *response) {
vjpai46f65232015-03-23 10:10:27 -0700288 if (request->response_size() > 0) {
vjpaidea740f2015-02-26 16:35:35 -0800289 if (!SetPayload(request->response_type(), request->response_size(),
290 response->mutable_payload())) {
291 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
292 }
293 }
294 return Status::OK;
295 }
Vijay Paiacf6f312015-03-02 15:13:39 -0800296 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800297 std::unique_ptr<grpc::Server> server_;
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700298 std::unique_ptr<grpc::ServerCompletionQueue> srv_cq_;
299 TestService::AsyncService async_service_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700300 std::function<void(ServerContext *, SimpleRequest *,
301 grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800302 request_unary_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700303 std::function<void(
304 ServerContext *,
305 grpc::ServerAsyncReaderWriter<SimpleResponse, SimpleRequest> *, void *)>
vjpai46f65232015-03-23 10:10:27 -0700306 request_streaming_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700307 std::forward_list<ServerRpcContext *> contexts_;
Vijay Pai8ad32092015-03-23 12:44:28 -0700308
309 std::mutex shutdown_mutex_;
310 bool shutdown_;
vjpaidea740f2015-02-26 16:35:35 -0800311};
312
Craig Tiller5c8737d2015-05-21 11:42:17 -0700313std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config,
Craig Tillera182bf12015-03-04 13:54:39 -0800314 int port) {
315 return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port));
vjpaidea740f2015-02-26 16:35:35 -0800316}
317
Craig Tillera182bf12015-03-04 13:54:39 -0800318} // namespace testing
319} // namespace grpc