blob: 10f884383a406ef395022474530d8dddcb67b54a [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>
David Garcia Quintasc9516d42015-06-02 13:28:46 -070036#include <memory>
Vijay Pai8ad32092015-03-23 12:44:28 -070037#include <mutex>
vjpaidea740f2015-02-26 16:35:35 -080038#include <sys/time.h>
39#include <sys/resource.h>
40#include <sys/signal.h>
41#include <thread>
42
43#include <gflags/gflags.h>
44#include <grpc/support/alloc.h>
45#include <grpc/support/host_port.h>
46#include <grpc++/async_unary_call.h>
47#include <grpc++/config.h>
48#include <grpc++/server.h>
49#include <grpc++/server_builder.h>
50#include <grpc++/server_context.h>
Craig Tiller61691f92015-03-02 08:51:52 -080051#include <grpc++/server_credentials.h>
vjpaidea740f2015-02-26 16:35:35 -080052#include <grpc++/status.h>
vjpai46f65232015-03-23 10:10:27 -070053#include <grpc++/stream.h>
vjpaidea740f2015-02-26 16:35:35 -080054#include <gtest/gtest.h>
55#include "src/cpp/server/thread_pool.h"
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020056#include "test/cpp/qps/qpstest.grpc.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 Tiller27df2cf2015-07-01 15:11:29 -070067 AsyncQpsServerTest(const ServerConfig &config, int port) {
Craig Tiller5c8737d2015-05-21 11:42:17 -070068 char *server_address = NULL;
Craig Tillerd6479d62015-03-04 12:50:11 -080069 gpr_join_host_port(&server_address, "::", port);
vjpaidea740f2015-02-26 16:35:35 -080070
71 ServerBuilder builder;
Nicolas Noblecfd60732015-03-18 16:27:43 -070072 builder.AddListeningPort(server_address, InsecureServerCredentials());
Craig Tillerd6479d62015-03-04 12:50:11 -080073 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080074
75 builder.RegisterAsyncService(&async_service_);
Craig Tiller51f938f2015-06-09 23:32:57 -070076 for (int i = 0; i < config.threads(); i++) {
77 srv_cqs_.emplace_back(std::move(builder.AddCompletionQueue()));
78 }
vjpaidea740f2015-02-26 16:35:35 -080079
80 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080081
82 using namespace std::placeholders;
Craig Tillerca83dc82015-07-15 11:43:26 -070083 for (int i = 0; i < 10000 / config.threads(); i++) {
Craig Tiller51f938f2015-06-09 23:32:57 -070084 for (int j = 0; j < config.threads(); j++) {
85 auto request_unary = std::bind(
86 &TestService::AsyncService::RequestUnaryCall, &async_service_, _1,
87 _2, _3, srv_cqs_[j].get(), srv_cqs_[j].get(), _4);
88 auto request_streaming = std::bind(
89 &TestService::AsyncService::RequestStreamingCall, &async_service_,
90 _1, _2, srv_cqs_[j].get(), srv_cqs_[j].get(), _3);
91 contexts_.push_front(
92 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
93 request_unary, ProcessRPC));
94 contexts_.push_front(
95 new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
96 request_streaming, ProcessRPC));
97 }
vjpaidea740f2015-02-26 16:35:35 -080098 }
Craig Tillerd6479d62015-03-04 12:50:11 -080099 for (int i = 0; i < config.threads(); i++) {
Craig Tiller27df2cf2015-07-01 15:11:29 -0700100 shutdown_state_.emplace_back(new PerThreadShutdownState());
101 }
102 for (int i = 0; i < config.threads(); i++) {
Vijay Paiacf6f312015-03-02 15:13:39 -0800103 threads_.push_back(std::thread([=]() {
vjpaidea740f2015-02-26 16:35:35 -0800104 // Wait until work is available or we are shutting down
105 bool ok;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700106 void *got_tag;
Craig Tiller51f938f2015-06-09 23:32:57 -0700107 while (srv_cqs_[i]->Next(&got_tag, &ok)) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700108 ServerRpcContext *ctx = detag(got_tag);
Craig Tiller8221e402015-04-09 12:23:21 -0700109 // The tag is a pointer to an RPC context to invoke
Craig Tilleraeea2f22015-05-21 13:54:42 -0700110 bool still_going = ctx->RunNextState(ok);
Craig Tiller27df2cf2015-07-01 15:11:29 -0700111 if (!shutdown_state_[i]->shutdown()) {
Craig Tiller8221e402015-04-09 12:23:21 -0700112 // this RPC context is done, so refresh it
Craig Tilleraeea2f22015-05-21 13:54:42 -0700113 if (!still_going) {
Vijay Paiacf6f312015-03-02 15:13:39 -0800114 ctx->Reset();
115 }
Craig Tilleraeea2f22015-05-21 13:54:42 -0700116 } else {
117 return;
Craig Tiller8221e402015-04-09 12:23:21 -0700118 }
vjpaidea740f2015-02-26 16:35:35 -0800119 }
120 return;
121 }));
122 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800123 }
124 ~AsyncQpsServerTest() {
125 server_->Shutdown();
Craig Tiller27df2cf2015-07-01 15:11:29 -0700126 for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) {
127 (*ss)->set_shutdown();
Vijay Pai8ad32092015-03-23 12:44:28 -0700128 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700129 for (auto thr = threads_.begin(); thr != threads_.end(); thr++) {
130 thr->join();
Craig Tillerd6479d62015-03-04 12:50:11 -0800131 }
Craig Tiller51f938f2015-06-09 23:32:57 -0700132 for (auto cq = srv_cqs_.begin(); cq != srv_cqs_.end(); ++cq) {
133 (*cq)->Shutdown();
134 bool ok;
135 void *got_tag;
136 while ((*cq)->Next(&got_tag, &ok))
137 ;
138 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800139 while (!contexts_.empty()) {
140 delete contexts_.front();
141 contexts_.pop_front();
vjpaidea740f2015-02-26 16:35:35 -0800142 }
143 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800144
vjpaidea740f2015-02-26 16:35:35 -0800145 private:
146 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800147 public:
vjpaidea740f2015-02-26 16:35:35 -0800148 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800149 virtual ~ServerRpcContext(){};
vjpai46f65232015-03-23 10:10:27 -0700150 virtual bool RunNextState(bool) = 0; // next state, return false if done
Craig Tiller5c8737d2015-05-21 11:42:17 -0700151 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800152 };
Craig Tiller5c8737d2015-05-21 11:42:17 -0700153 static void *tag(ServerRpcContext *func) {
154 return reinterpret_cast<void *>(func);
vjpaidea740f2015-02-26 16:35:35 -0800155 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700156 static ServerRpcContext *detag(void *tag) {
157 return reinterpret_cast<ServerRpcContext *>(tag);
vjpaidea740f2015-02-26 16:35:35 -0800158 }
159
160 template <class RequestType, class ResponseType>
vjpai46f65232015-03-23 10:10:27 -0700161 class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext {
vjpaidea740f2015-02-26 16:35:35 -0800162 public:
163 ServerRpcContextUnaryImpl(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700164 std::function<void(ServerContext *, RequestType *,
165 grpc::ServerAsyncResponseWriter<ResponseType> *,
166 void *)> request_method,
167 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800168 invoke_method)
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700169 : srv_ctx_(new ServerContext),
170 next_state_(&ServerRpcContextUnaryImpl::invoker),
vjpaidea740f2015-02-26 16:35:35 -0800171 request_method_(request_method),
172 invoke_method_(invoke_method),
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700173 response_writer_(srv_ctx_.get()) {
174 request_method_(srv_ctx_.get(), &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800175 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800176 }
vjpai3c110662015-02-27 07:17:16 -0800177 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700178 bool RunNextState(bool ok) GRPC_OVERRIDE {
179 return (this->*next_state_)(ok);
180 }
vjpai5b39f9a2015-02-27 09:33:00 -0800181 void Reset() GRPC_OVERRIDE {
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700182 srv_ctx_.reset(new ServerContext);
vjpaidea740f2015-02-26 16:35:35 -0800183 req_ = RequestType();
184 response_writer_ =
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700185 grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get());
vjpaidea740f2015-02-26 16:35:35 -0800186
187 // Then request the method
188 next_state_ = &ServerRpcContextUnaryImpl::invoker;
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700189 request_method_(srv_ctx_.get(), &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800190 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800191 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800192
vjpaidea740f2015-02-26 16:35:35 -0800193 private:
vjpai46f65232015-03-23 10:10:27 -0700194 bool finisher(bool) { return false; }
195 bool invoker(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700196 if (!ok) {
197 return false;
198 }
vjpai46f65232015-03-23 10:10:27 -0700199
vjpaidea740f2015-02-26 16:35:35 -0800200 ResponseType response;
201
202 // Call the RPC processing function
203 grpc::Status status = invoke_method_(&req_, &response);
204
205 // Have the response writer work and invoke on_finish when done
206 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800207 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800208 return true;
209 }
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700210 std::unique_ptr<ServerContext> srv_ctx_;
vjpaidea740f2015-02-26 16:35:35 -0800211 RequestType req_;
vjpai46f65232015-03-23 10:10:27 -0700212 bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700213 std::function<void(ServerContext *, RequestType *,
214 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800215 request_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700216 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800217 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800218 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
219 };
220
vjpai46f65232015-03-23 10:10:27 -0700221 template <class RequestType, class ResponseType>
222 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext {
223 public:
224 ServerRpcContextStreamingImpl(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700225 std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter<
226 ResponseType, RequestType> *,
227 void *)> request_method,
vjpai46f65232015-03-23 10:10:27 -0700228 std::function<grpc::Status(const RequestType *, ResponseType *)>
229 invoke_method)
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700230 : srv_ctx_(new ServerContext),
231 next_state_(&ServerRpcContextStreamingImpl::request_done),
vjpai46f65232015-03-23 10:10:27 -0700232 request_method_(request_method),
233 invoke_method_(invoke_method),
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700234 stream_(srv_ctx_.get()) {
235 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700236 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700237 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {}
238 bool RunNextState(bool ok) GRPC_OVERRIDE {
239 return (this->*next_state_)(ok);
vjpai46f65232015-03-23 10:10:27 -0700240 }
vjpai46f65232015-03-23 10:10:27 -0700241 void Reset() GRPC_OVERRIDE {
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700242 srv_ctx_.reset(new ServerContext);
vjpai46f65232015-03-23 10:10:27 -0700243 req_ = RequestType();
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700244 stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(
245 srv_ctx_.get());
vjpai46f65232015-03-23 10:10:27 -0700246
247 // Then request the method
248 next_state_ = &ServerRpcContextStreamingImpl::request_done;
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700249 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700250 }
251
252 private:
253 bool request_done(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700254 if (!ok) {
255 return false;
256 }
vjpai46f65232015-03-23 10:10:27 -0700257 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
258 next_state_ = &ServerRpcContextStreamingImpl::read_done;
259 return true;
260 }
Vijay Pai8ad32092015-03-23 12:44:28 -0700261
vjpai46f65232015-03-23 10:10:27 -0700262 bool read_done(bool ok) {
263 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700264 // invoke the method
265 ResponseType response;
266 // Call the RPC processing function
267 grpc::Status status = invoke_method_(&req_, &response);
268 // initiate the write
269 stream_.Write(response, AsyncQpsServerTest::tag(this));
270 next_state_ = &ServerRpcContextStreamingImpl::write_done;
271 } else { // client has sent writes done
272 // finish the stream
273 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
274 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700275 }
276 return true;
277 }
278 bool write_done(bool ok) {
279 // now go back and get another streaming read!
280 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700281 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
282 next_state_ = &ServerRpcContextStreamingImpl::read_done;
283 } else {
284 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
285 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700286 }
287 return true;
288 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700289 bool finish_done(bool ok) { return false; /* reset the context */ }
vjpai46f65232015-03-23 10:10:27 -0700290
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700291 std::unique_ptr<ServerContext> srv_ctx_;
vjpai46f65232015-03-23 10:10:27 -0700292 RequestType req_;
293 bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700294 std::function<void(
295 ServerContext *,
296 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)>
297 request_method_;
vjpai46f65232015-03-23 10:10:27 -0700298 std::function<grpc::Status(const RequestType *, ResponseType *)>
299 invoke_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700300 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_;
vjpai46f65232015-03-23 10:10:27 -0700301 };
302
Craig Tiller5c8737d2015-05-21 11:42:17 -0700303 static Status ProcessRPC(const SimpleRequest *request,
304 SimpleResponse *response) {
vjpai46f65232015-03-23 10:10:27 -0700305 if (request->response_size() > 0) {
vjpaidea740f2015-02-26 16:35:35 -0800306 if (!SetPayload(request->response_type(), request->response_size(),
307 response->mutable_payload())) {
308 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
309 }
310 }
311 return Status::OK;
312 }
Vijay Paiacf6f312015-03-02 15:13:39 -0800313 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800314 std::unique_ptr<grpc::Server> server_;
Craig Tiller51f938f2015-06-09 23:32:57 -0700315 std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_;
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700316 TestService::AsyncService async_service_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700317 std::forward_list<ServerRpcContext *> contexts_;
Vijay Pai8ad32092015-03-23 12:44:28 -0700318
Craig Tiller27df2cf2015-07-01 15:11:29 -0700319 class PerThreadShutdownState {
320 public:
321 PerThreadShutdownState() : shutdown_(false) {}
322
323 bool shutdown() const {
324 std::lock_guard<std::mutex> lock(mutex_);
325 return shutdown_;
326 }
327
328 void set_shutdown() {
329 std::lock_guard<std::mutex> lock(mutex_);
330 shutdown_ = true;
331 }
332
333 private:
334 mutable std::mutex mutex_;
335 bool shutdown_;
336 };
337 std::vector<std::unique_ptr<PerThreadShutdownState>> shutdown_state_;
vjpaidea740f2015-02-26 16:35:35 -0800338};
339
Craig Tiller5c8737d2015-05-21 11:42:17 -0700340std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config,
Craig Tillera182bf12015-03-04 13:54:39 -0800341 int port) {
342 return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port));
vjpaidea740f2015-02-26 16:35:35 -0800343}
344
Craig Tillera182bf12015-03-04 13:54:39 -0800345} // namespace testing
346} // namespace grpc