vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 36 | #include <memory> |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 37 | #include <mutex> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 38 | #include <thread> |
| 39 | |
| 40 | #include <gflags/gflags.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/host_port.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
| 45 | #include <grpc++/support/config.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 46 | #include <grpc++/server.h> |
| 47 | #include <grpc++/server_builder.h> |
| 48 | #include <grpc++/server_context.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 49 | #include <grpc++/security/server_credentials.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 50 | #include <gtest/gtest.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 51 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 52 | #include "test/cpp/qps/server.h" |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 53 | #include "test/proto/benchmarks/services.grpc.pb.h" |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 54 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 55 | namespace grpc { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 56 | namespace testing { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 57 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 58 | class AsyncQpsServerTest : public Server { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 59 | public: |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 60 | explicit AsyncQpsServerTest(const ServerConfig &config) : Server(config) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 61 | char *server_address = NULL; |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 62 | |
Jan Tattermusch | c5ebbd5 | 2015-11-18 16:50:47 -0800 | [diff] [blame] | 63 | gpr_join_host_port(&server_address, config.host().c_str(), port()); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 64 | |
| 65 | ServerBuilder builder; |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 66 | builder.AddListeningPort(server_address, |
| 67 | Server::CreateServerCredentials(config)); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 68 | gpr_free(server_address); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 69 | |
| 70 | builder.RegisterAsyncService(&async_service_); |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 71 | for (int i = 0; i < config.async_server_threads(); i++) { |
Nicolas "Pixel" Noble | 59588c6 | 2015-09-03 02:29:30 +0200 | [diff] [blame] | 72 | srv_cqs_.emplace_back(builder.AddCompletionQueue()); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 73 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 74 | |
| 75 | server_ = builder.BuildAndStart(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 76 | |
| 77 | using namespace std::placeholders; |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 78 | for (int i = 0; i < 10000 / config.async_server_threads(); i++) { |
| 79 | for (int j = 0; j < config.async_server_threads(); j++) { |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 80 | auto request_unary = std::bind( |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 81 | &BenchmarkService::AsyncService::RequestUnaryCall, &async_service_, |
| 82 | _1, _2, _3, srv_cqs_[j].get(), srv_cqs_[j].get(), _4); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 83 | auto request_streaming = std::bind( |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 84 | &BenchmarkService::AsyncService::RequestStreamingCall, |
| 85 | &async_service_, _1, _2, srv_cqs_[j].get(), srv_cqs_[j].get(), _3); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 86 | 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 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 93 | } |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 94 | for (int i = 0; i < config.async_server_threads(); i++) { |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 95 | shutdown_state_.emplace_back(new PerThreadShutdownState()); |
| 96 | } |
vjpai | d08a738 | 2015-11-02 16:45:08 -0800 | [diff] [blame] | 97 | for (int i = 0; i < config.async_server_threads(); i++) { |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 98 | threads_.emplace_back(&AsyncQpsServerTest::ThreadFunc, this, i); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 99 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 100 | } |
| 101 | ~AsyncQpsServerTest() { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 102 | auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(10); |
vjpai | d514b21 | 2015-08-31 16:27:35 -0700 | [diff] [blame] | 103 | server_->Shutdown(deadline); |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 104 | for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) { |
| 105 | (*ss)->set_shutdown(); |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 106 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 107 | for (auto thr = threads_.begin(); thr != threads_.end(); thr++) { |
| 108 | thr->join(); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 109 | } |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 110 | 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 Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 117 | while (!contexts_.empty()) { |
| 118 | delete contexts_.front(); |
| 119 | contexts_.pop_front(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 122 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 123 | private: |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 124 | 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 |
vjpai | b1db869 | 2015-08-11 22:41:02 -0700 | [diff] [blame] | 131 | const bool still_going = ctx->RunNextState(ok); |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 132 | 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 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 144 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 145 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 146 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 147 | virtual ~ServerRpcContext(){}; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 148 | virtual bool RunNextState(bool) = 0; // next state, return false if done |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 149 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 150 | }; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 151 | static void *tag(ServerRpcContext *func) { |
| 152 | return reinterpret_cast<void *>(func); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 153 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 154 | static ServerRpcContext *detag(void *tag) { |
| 155 | return reinterpret_cast<ServerRpcContext *>(tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <class RequestType, class ResponseType> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 159 | class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 160 | public: |
| 161 | ServerRpcContextUnaryImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 162 | std::function<void(ServerContext *, RequestType *, |
| 163 | grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 164 | void *)> request_method, |
| 165 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 166 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 167 | : srv_ctx_(new ServerContext), |
| 168 | next_state_(&ServerRpcContextUnaryImpl::invoker), |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 169 | request_method_(request_method), |
| 170 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 171 | response_writer_(srv_ctx_.get()) { |
| 172 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 173 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 174 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 175 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 176 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 177 | return (this->*next_state_)(ok); |
| 178 | } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 179 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 180 | srv_ctx_.reset(new ServerContext); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 181 | req_ = RequestType(); |
| 182 | response_writer_ = |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 183 | grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get()); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 184 | |
| 185 | // Then request the method |
| 186 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 187 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 188 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 189 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 190 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 191 | private: |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 192 | bool finisher(bool) { return false; } |
| 193 | bool invoker(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 194 | if (!ok) { |
| 195 | return false; |
| 196 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 197 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 198 | 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 Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 205 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 206 | return true; |
| 207 | } |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 208 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 209 | RequestType req_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 210 | bool (ServerRpcContextUnaryImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 211 | std::function<void(ServerContext *, RequestType *, |
| 212 | grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 213 | request_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 214 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 215 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 216 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 217 | }; |
| 218 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 219 | template <class RequestType, class ResponseType> |
| 220 | class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext { |
| 221 | public: |
| 222 | ServerRpcContextStreamingImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 223 | std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter< |
| 224 | ResponseType, RequestType> *, |
| 225 | void *)> request_method, |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 226 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 227 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 228 | : srv_ctx_(new ServerContext), |
| 229 | next_state_(&ServerRpcContextStreamingImpl::request_done), |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 230 | request_method_(request_method), |
| 231 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 232 | stream_(srv_ctx_.get()) { |
| 233 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 234 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 235 | ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| 236 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 237 | return (this->*next_state_)(ok); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 238 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 239 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 240 | srv_ctx_.reset(new ServerContext); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 241 | req_ = RequestType(); |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 242 | stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>( |
| 243 | srv_ctx_.get()); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 244 | |
| 245 | // Then request the method |
| 246 | next_state_ = &ServerRpcContextStreamingImpl::request_done; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 247 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private: |
| 251 | bool request_done(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 252 | if (!ok) { |
| 253 | return false; |
| 254 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 255 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 256 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 257 | return true; |
| 258 | } |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 259 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 260 | bool read_done(bool ok) { |
| 261 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 262 | // 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; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | bool write_done(bool ok) { |
| 277 | // now go back and get another streaming read! |
| 278 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 279 | 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; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 284 | } |
| 285 | return true; |
| 286 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 287 | bool finish_done(bool ok) { return false; /* reset the context */ } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 288 | |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 289 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 290 | RequestType req_; |
| 291 | bool (ServerRpcContextStreamingImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 292 | std::function<void( |
| 293 | ServerContext *, |
| 294 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 295 | request_method_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 296 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 297 | invoke_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 298 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 299 | }; |
| 300 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 301 | static Status ProcessRPC(const SimpleRequest *request, |
| 302 | SimpleResponse *response) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 303 | if (request->response_size() > 0) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 304 | 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 Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 311 | std::vector<std::thread> threads_; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 312 | std::unique_ptr<grpc::Server> server_; |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 313 | std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_; |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 314 | BenchmarkService::AsyncService async_service_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 315 | std::forward_list<ServerRpcContext *> contexts_; |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 316 | |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 317 | 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_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 338 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config) { |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 339 | return std::unique_ptr<Server>(new AsyncQpsServerTest(config)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 342 | } // namespace testing |
| 343 | } // namespace grpc |