blob: 3fa8fc77f4f99b5a293026f11dab63fcafa0ccaa [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 <thread>
39
40#include <gflags/gflags.h>
yang-g9e2f90c2015-08-21 15:35:03 -070041#include <grpc/grpc.h>
vjpaidea740f2015-02-26 16:35:35 -080042#include <grpc/support/alloc.h>
43#include <grpc/support/host_port.h>
yang-g9e2f90c2015-08-21 15:35:03 -070044#include <grpc/support/log.h>
45#include <grpc++/support/config.h>
vjpaidea740f2015-02-26 16:35:35 -080046#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070049#include <grpc++/security/server_credentials.h>
vjpaidea740f2015-02-26 16:35:35 -080050#include <gtest/gtest.h>
yang-g9e2f90c2015-08-21 15:35:03 -070051
Craig Tillerd6479d62015-03-04 12:50:11 -080052#include "test/cpp/qps/server.h"
vjpaid08a7382015-11-02 16:45:08 -080053#include "test/proto/benchmarks/services.grpc.pb.h"
vjpaidea740f2015-02-26 16:35:35 -080054
Craig Tillerd6479d62015-03-04 12:50:11 -080055namespace grpc {
Craig Tillera182bf12015-03-04 13:54:39 -080056namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080057
Craig Tillerd6479d62015-03-04 12:50:11 -080058class AsyncQpsServerTest : public Server {
vjpaidea740f2015-02-26 16:35:35 -080059 public:
Vijay Paice846702015-11-04 00:30:12 -080060 explicit AsyncQpsServerTest(const ServerConfig &config) : Server(config) {
Craig Tiller5c8737d2015-05-21 11:42:17 -070061 char *server_address = NULL;
vjpai72a63322015-10-29 02:23:11 -070062
Jan Tattermuschc5ebbd52015-11-18 16:50:47 -080063 gpr_join_host_port(&server_address, config.host().c_str(), port());
vjpaidea740f2015-02-26 16:35:35 -080064
65 ServerBuilder builder;
Vijay Paice846702015-11-04 00:30:12 -080066 builder.AddListeningPort(server_address,
67 Server::CreateServerCredentials(config));
Craig Tillerd6479d62015-03-04 12:50:11 -080068 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080069
Craig Tiller15f383c2016-01-07 12:45:32 -080070 builder.RegisterService(&async_service_);
vjpaid08a7382015-11-02 16:45:08 -080071 for (int i = 0; i < config.async_server_threads(); i++) {
Nicolas "Pixel" Noble59588c62015-09-03 02:29:30 +020072 srv_cqs_.emplace_back(builder.AddCompletionQueue());
Craig Tiller51f938f2015-06-09 23:32:57 -070073 }
vjpaidea740f2015-02-26 16:35:35 -080074
75 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080076
77 using namespace std::placeholders;
vjpaid08a7382015-11-02 16:45:08 -080078 for (int i = 0; i < 10000 / config.async_server_threads(); i++) {
79 for (int j = 0; j < config.async_server_threads(); j++) {
Craig Tiller51f938f2015-06-09 23:32:57 -070080 auto request_unary = std::bind(
Vijay Paice846702015-11-04 00:30:12 -080081 &BenchmarkService::AsyncService::RequestUnaryCall, &async_service_,
82 _1, _2, _3, srv_cqs_[j].get(), srv_cqs_[j].get(), _4);
Craig Tiller51f938f2015-06-09 23:32:57 -070083 auto request_streaming = std::bind(
Vijay Paice846702015-11-04 00:30:12 -080084 &BenchmarkService::AsyncService::RequestStreamingCall,
85 &async_service_, _1, _2, srv_cqs_[j].get(), srv_cqs_[j].get(), _3);
Craig Tiller51f938f2015-06-09 23:32:57 -070086 contexts_.push_front(
87 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
88 request_unary, ProcessRPC));
89 contexts_.push_front(
90 new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
91 request_streaming, ProcessRPC));
92 }
vjpaidea740f2015-02-26 16:35:35 -080093 }
vjpaid08a7382015-11-02 16:45:08 -080094 for (int i = 0; i < config.async_server_threads(); i++) {
Craig Tiller27df2cf2015-07-01 15:11:29 -070095 shutdown_state_.emplace_back(new PerThreadShutdownState());
96 }
vjpaid08a7382015-11-02 16:45:08 -080097 for (int i = 0; i < config.async_server_threads(); i++) {
Vijay Pai458faa92015-07-31 10:30:13 -070098 threads_.emplace_back(&AsyncQpsServerTest::ThreadFunc, this, i);
vjpaidea740f2015-02-26 16:35:35 -080099 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800100 }
101 ~AsyncQpsServerTest() {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700102 auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(10);
vjpaid514b212015-08-31 16:27:35 -0700103 server_->Shutdown(deadline);
Craig Tiller27df2cf2015-07-01 15:11:29 -0700104 for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) {
105 (*ss)->set_shutdown();
Vijay Pai8ad32092015-03-23 12:44:28 -0700106 }
Vijay Pai82dd80a2015-03-24 10:36:08 -0700107 for (auto thr = threads_.begin(); thr != threads_.end(); thr++) {
108 thr->join();
Craig Tillerd6479d62015-03-04 12:50:11 -0800109 }
Craig Tiller51f938f2015-06-09 23:32:57 -0700110 for (auto cq = srv_cqs_.begin(); cq != srv_cqs_.end(); ++cq) {
111 (*cq)->Shutdown();
112 bool ok;
113 void *got_tag;
114 while ((*cq)->Next(&got_tag, &ok))
115 ;
116 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800117 while (!contexts_.empty()) {
118 delete contexts_.front();
119 contexts_.pop_front();
vjpaidea740f2015-02-26 16:35:35 -0800120 }
121 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800122
vjpaidea740f2015-02-26 16:35:35 -0800123 private:
Vijay Pai458faa92015-07-31 10:30:13 -0700124 void ThreadFunc(int rank) {
125 // Wait until work is available or we are shutting down
126 bool ok;
127 void *got_tag;
128 while (srv_cqs_[rank]->Next(&got_tag, &ok)) {
129 ServerRpcContext *ctx = detag(got_tag);
130 // The tag is a pointer to an RPC context to invoke
vjpaib1db8692015-08-11 22:41:02 -0700131 const bool still_going = ctx->RunNextState(ok);
Vijay Pai458faa92015-07-31 10:30:13 -0700132 if (!shutdown_state_[rank]->shutdown()) {
133 // this RPC context is done, so refresh it
134 if (!still_going) {
135 ctx->Reset();
136 }
137 } else {
138 return;
139 }
140 }
141 return;
142 }
143
vjpaidea740f2015-02-26 16:35:35 -0800144 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800145 public:
vjpaidea740f2015-02-26 16:35:35 -0800146 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800147 virtual ~ServerRpcContext(){};
vjpai46f65232015-03-23 10:10:27 -0700148 virtual bool RunNextState(bool) = 0; // next state, return false if done
Craig Tiller5c8737d2015-05-21 11:42:17 -0700149 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800150 };
Craig Tiller5c8737d2015-05-21 11:42:17 -0700151 static void *tag(ServerRpcContext *func) {
152 return reinterpret_cast<void *>(func);
vjpaidea740f2015-02-26 16:35:35 -0800153 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700154 static ServerRpcContext *detag(void *tag) {
155 return reinterpret_cast<ServerRpcContext *>(tag);
vjpaidea740f2015-02-26 16:35:35 -0800156 }
157
158 template <class RequestType, class ResponseType>
vjpai46f65232015-03-23 10:10:27 -0700159 class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext {
vjpaidea740f2015-02-26 16:35:35 -0800160 public:
161 ServerRpcContextUnaryImpl(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700162 std::function<void(ServerContext *, RequestType *,
163 grpc::ServerAsyncResponseWriter<ResponseType> *,
164 void *)> request_method,
165 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800166 invoke_method)
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700167 : srv_ctx_(new ServerContext),
168 next_state_(&ServerRpcContextUnaryImpl::invoker),
vjpaidea740f2015-02-26 16:35:35 -0800169 request_method_(request_method),
170 invoke_method_(invoke_method),
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700171 response_writer_(srv_ctx_.get()) {
172 request_method_(srv_ctx_.get(), &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800173 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800174 }
vjpai3c110662015-02-27 07:17:16 -0800175 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
Craig Tiller5c8737d2015-05-21 11:42:17 -0700176 bool RunNextState(bool ok) GRPC_OVERRIDE {
177 return (this->*next_state_)(ok);
178 }
vjpai5b39f9a2015-02-27 09:33:00 -0800179 void Reset() GRPC_OVERRIDE {
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700180 srv_ctx_.reset(new ServerContext);
vjpaidea740f2015-02-26 16:35:35 -0800181 req_ = RequestType();
182 response_writer_ =
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700183 grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get());
vjpaidea740f2015-02-26 16:35:35 -0800184
185 // Then request the method
186 next_state_ = &ServerRpcContextUnaryImpl::invoker;
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700187 request_method_(srv_ctx_.get(), &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800188 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800189 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800190
vjpaidea740f2015-02-26 16:35:35 -0800191 private:
vjpai46f65232015-03-23 10:10:27 -0700192 bool finisher(bool) { return false; }
193 bool invoker(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700194 if (!ok) {
195 return false;
196 }
vjpai46f65232015-03-23 10:10:27 -0700197
vjpaidea740f2015-02-26 16:35:35 -0800198 ResponseType response;
199
200 // Call the RPC processing function
201 grpc::Status status = invoke_method_(&req_, &response);
202
203 // Have the response writer work and invoke on_finish when done
204 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800205 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800206 return true;
207 }
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700208 std::unique_ptr<ServerContext> srv_ctx_;
vjpaidea740f2015-02-26 16:35:35 -0800209 RequestType req_;
vjpai46f65232015-03-23 10:10:27 -0700210 bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700211 std::function<void(ServerContext *, RequestType *,
212 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800213 request_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700214 std::function<grpc::Status(const RequestType *, ResponseType *)>
vjpai4e1e1bc2015-02-27 23:47:12 -0800215 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800216 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
217 };
218
vjpai46f65232015-03-23 10:10:27 -0700219 template <class RequestType, class ResponseType>
220 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext {
221 public:
222 ServerRpcContextStreamingImpl(
Craig Tiller5c8737d2015-05-21 11:42:17 -0700223 std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter<
224 ResponseType, RequestType> *,
225 void *)> request_method,
vjpai46f65232015-03-23 10:10:27 -0700226 std::function<grpc::Status(const RequestType *, ResponseType *)>
227 invoke_method)
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700228 : srv_ctx_(new ServerContext),
229 next_state_(&ServerRpcContextStreamingImpl::request_done),
vjpai46f65232015-03-23 10:10:27 -0700230 request_method_(request_method),
231 invoke_method_(invoke_method),
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700232 stream_(srv_ctx_.get()) {
233 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700234 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700235 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {}
236 bool RunNextState(bool ok) GRPC_OVERRIDE {
237 return (this->*next_state_)(ok);
vjpai46f65232015-03-23 10:10:27 -0700238 }
vjpai46f65232015-03-23 10:10:27 -0700239 void Reset() GRPC_OVERRIDE {
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700240 srv_ctx_.reset(new ServerContext);
vjpai46f65232015-03-23 10:10:27 -0700241 req_ = RequestType();
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700242 stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(
243 srv_ctx_.get());
vjpai46f65232015-03-23 10:10:27 -0700244
245 // Then request the method
246 next_state_ = &ServerRpcContextStreamingImpl::request_done;
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700247 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
vjpai46f65232015-03-23 10:10:27 -0700248 }
249
250 private:
251 bool request_done(bool ok) {
Craig Tiller8221e402015-04-09 12:23:21 -0700252 if (!ok) {
253 return false;
254 }
vjpai46f65232015-03-23 10:10:27 -0700255 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
256 next_state_ = &ServerRpcContextStreamingImpl::read_done;
257 return true;
258 }
Vijay Pai8ad32092015-03-23 12:44:28 -0700259
vjpai46f65232015-03-23 10:10:27 -0700260 bool read_done(bool ok) {
261 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700262 // invoke the method
263 ResponseType response;
264 // Call the RPC processing function
265 grpc::Status status = invoke_method_(&req_, &response);
266 // initiate the write
267 stream_.Write(response, AsyncQpsServerTest::tag(this));
268 next_state_ = &ServerRpcContextStreamingImpl::write_done;
269 } else { // client has sent writes done
270 // finish the stream
271 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
272 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700273 }
274 return true;
275 }
276 bool write_done(bool ok) {
277 // now go back and get another streaming read!
278 if (ok) {
Craig Tiller5c8737d2015-05-21 11:42:17 -0700279 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
280 next_state_ = &ServerRpcContextStreamingImpl::read_done;
281 } else {
282 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
283 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
vjpai46f65232015-03-23 10:10:27 -0700284 }
285 return true;
286 }
Craig Tiller5c8737d2015-05-21 11:42:17 -0700287 bool finish_done(bool ok) { return false; /* reset the context */ }
vjpai46f65232015-03-23 10:10:27 -0700288
David Garcia Quintasc9516d42015-06-02 13:28:46 -0700289 std::unique_ptr<ServerContext> srv_ctx_;
vjpai46f65232015-03-23 10:10:27 -0700290 RequestType req_;
291 bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
Craig Tiller5c8737d2015-05-21 11:42:17 -0700292 std::function<void(
293 ServerContext *,
294 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)>
295 request_method_;
vjpai46f65232015-03-23 10:10:27 -0700296 std::function<grpc::Status(const RequestType *, ResponseType *)>
297 invoke_method_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700298 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_;
vjpai46f65232015-03-23 10:10:27 -0700299 };
300
Craig Tiller5c8737d2015-05-21 11:42:17 -0700301 static Status ProcessRPC(const SimpleRequest *request,
302 SimpleResponse *response) {
vjpai46f65232015-03-23 10:10:27 -0700303 if (request->response_size() > 0) {
vjpaidea740f2015-02-26 16:35:35 -0800304 if (!SetPayload(request->response_type(), request->response_size(),
305 response->mutable_payload())) {
306 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
307 }
308 }
309 return Status::OK;
310 }
Vijay Paiacf6f312015-03-02 15:13:39 -0800311 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800312 std::unique_ptr<grpc::Server> server_;
Craig Tiller51f938f2015-06-09 23:32:57 -0700313 std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_;
vjpai119c1032015-10-29 01:21:04 -0700314 BenchmarkService::AsyncService async_service_;
Craig Tiller5c8737d2015-05-21 11:42:17 -0700315 std::forward_list<ServerRpcContext *> contexts_;
Vijay Pai8ad32092015-03-23 12:44:28 -0700316
Craig Tiller27df2cf2015-07-01 15:11:29 -0700317 class PerThreadShutdownState {
318 public:
319 PerThreadShutdownState() : shutdown_(false) {}
320
321 bool shutdown() const {
322 std::lock_guard<std::mutex> lock(mutex_);
323 return shutdown_;
324 }
325
326 void set_shutdown() {
327 std::lock_guard<std::mutex> lock(mutex_);
328 shutdown_ = true;
329 }
330
331 private:
332 mutable std::mutex mutex_;
333 bool shutdown_;
334 };
335 std::vector<std::unique_ptr<PerThreadShutdownState>> shutdown_state_;
vjpaidea740f2015-02-26 16:35:35 -0800336};
337
Vijay Paice846702015-11-04 00:30:12 -0800338std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config) {
vjpai72a63322015-10-29 02:23:11 -0700339 return std::unique_ptr<Server>(new AsyncQpsServerTest(config));
vjpaidea740f2015-02-26 16:35:35 -0800340}
341
Craig Tillera182bf12015-03-04 13:54:39 -0800342} // namespace testing
343} // namespace grpc