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 | |
vjpai | 52bfb25 | 2015-10-21 07:50:49 -0700 | [diff] [blame] | 52 | #include "test/proto/perf_control.grpc.pb.h" |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 53 | #include "test/cpp/qps/server.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: |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 60 | AsyncQpsServerTest(const ServerConfig &config, int port) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 61 | char *server_address = NULL; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 62 | gpr_join_host_port(&server_address, "::", port); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 63 | |
| 64 | ServerBuilder builder; |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 65 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 66 | gpr_free(server_address); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 67 | |
| 68 | builder.RegisterAsyncService(&async_service_); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 69 | for (int i = 0; i < config.threads(); i++) { |
Nicolas "Pixel" Noble | 59588c6 | 2015-09-03 02:29:30 +0200 | [diff] [blame] | 70 | srv_cqs_.emplace_back(builder.AddCompletionQueue()); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 71 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 72 | |
| 73 | server_ = builder.BuildAndStart(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 74 | |
| 75 | using namespace std::placeholders; |
Craig Tiller | ca83dc8 | 2015-07-15 11:43:26 -0700 | [diff] [blame] | 76 | for (int i = 0; i < 10000 / config.threads(); i++) { |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 77 | for (int j = 0; j < config.threads(); j++) { |
| 78 | auto request_unary = std::bind( |
| 79 | &TestService::AsyncService::RequestUnaryCall, &async_service_, _1, |
| 80 | _2, _3, srv_cqs_[j].get(), srv_cqs_[j].get(), _4); |
| 81 | auto request_streaming = std::bind( |
| 82 | &TestService::AsyncService::RequestStreamingCall, &async_service_, |
| 83 | _1, _2, srv_cqs_[j].get(), srv_cqs_[j].get(), _3); |
| 84 | contexts_.push_front( |
| 85 | new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
| 86 | request_unary, ProcessRPC)); |
| 87 | contexts_.push_front( |
| 88 | new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>( |
| 89 | request_streaming, ProcessRPC)); |
| 90 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 91 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 92 | for (int i = 0; i < config.threads(); i++) { |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 93 | shutdown_state_.emplace_back(new PerThreadShutdownState()); |
| 94 | } |
| 95 | for (int i = 0; i < config.threads(); i++) { |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 96 | threads_.emplace_back(&AsyncQpsServerTest::ThreadFunc, this, i); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 97 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 98 | } |
| 99 | ~AsyncQpsServerTest() { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 100 | auto deadline = std::chrono::system_clock::now() + std::chrono::seconds(10); |
vjpai | d514b21 | 2015-08-31 16:27:35 -0700 | [diff] [blame] | 101 | server_->Shutdown(deadline); |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 102 | for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) { |
| 103 | (*ss)->set_shutdown(); |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 104 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 105 | for (auto thr = threads_.begin(); thr != threads_.end(); thr++) { |
| 106 | thr->join(); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 107 | } |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 108 | for (auto cq = srv_cqs_.begin(); cq != srv_cqs_.end(); ++cq) { |
| 109 | (*cq)->Shutdown(); |
| 110 | bool ok; |
| 111 | void *got_tag; |
| 112 | while ((*cq)->Next(&got_tag, &ok)) |
| 113 | ; |
| 114 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 115 | while (!contexts_.empty()) { |
| 116 | delete contexts_.front(); |
| 117 | contexts_.pop_front(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 120 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 121 | private: |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 122 | void ThreadFunc(int rank) { |
| 123 | // Wait until work is available or we are shutting down |
| 124 | bool ok; |
| 125 | void *got_tag; |
| 126 | while (srv_cqs_[rank]->Next(&got_tag, &ok)) { |
| 127 | ServerRpcContext *ctx = detag(got_tag); |
| 128 | // The tag is a pointer to an RPC context to invoke |
vjpai | b1db869 | 2015-08-11 22:41:02 -0700 | [diff] [blame] | 129 | const bool still_going = ctx->RunNextState(ok); |
Vijay Pai | 458faa9 | 2015-07-31 10:30:13 -0700 | [diff] [blame] | 130 | if (!shutdown_state_[rank]->shutdown()) { |
| 131 | // this RPC context is done, so refresh it |
| 132 | if (!still_going) { |
| 133 | ctx->Reset(); |
| 134 | } |
| 135 | } else { |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | return; |
| 140 | } |
| 141 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 142 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 143 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 144 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 145 | virtual ~ServerRpcContext(){}; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 146 | virtual bool RunNextState(bool) = 0; // next state, return false if done |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 147 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 148 | }; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 149 | static void *tag(ServerRpcContext *func) { |
| 150 | return reinterpret_cast<void *>(func); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 151 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 152 | static ServerRpcContext *detag(void *tag) { |
| 153 | return reinterpret_cast<ServerRpcContext *>(tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | template <class RequestType, class ResponseType> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 157 | class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 158 | public: |
| 159 | ServerRpcContextUnaryImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 160 | std::function<void(ServerContext *, RequestType *, |
| 161 | grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 162 | void *)> request_method, |
| 163 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 164 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 165 | : srv_ctx_(new ServerContext), |
| 166 | next_state_(&ServerRpcContextUnaryImpl::invoker), |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 167 | request_method_(request_method), |
| 168 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 169 | response_writer_(srv_ctx_.get()) { |
| 170 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 171 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 172 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 173 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 174 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 175 | return (this->*next_state_)(ok); |
| 176 | } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 177 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 178 | srv_ctx_.reset(new ServerContext); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 179 | req_ = RequestType(); |
| 180 | response_writer_ = |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 181 | grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get()); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 182 | |
| 183 | // Then request the method |
| 184 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 185 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 186 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 187 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 188 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 189 | private: |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 190 | bool finisher(bool) { return false; } |
| 191 | bool invoker(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 192 | if (!ok) { |
| 193 | return false; |
| 194 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 195 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 196 | ResponseType response; |
| 197 | |
| 198 | // Call the RPC processing function |
| 199 | grpc::Status status = invoke_method_(&req_, &response); |
| 200 | |
| 201 | // Have the response writer work and invoke on_finish when done |
| 202 | next_state_ = &ServerRpcContextUnaryImpl::finisher; |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 203 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 204 | return true; |
| 205 | } |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 206 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 207 | RequestType req_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 208 | bool (ServerRpcContextUnaryImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 209 | std::function<void(ServerContext *, RequestType *, |
| 210 | grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 211 | request_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 212 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 213 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 214 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 215 | }; |
| 216 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 217 | template <class RequestType, class ResponseType> |
| 218 | class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext { |
| 219 | public: |
| 220 | ServerRpcContextStreamingImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 221 | std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter< |
| 222 | ResponseType, RequestType> *, |
| 223 | void *)> request_method, |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 224 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 225 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 226 | : srv_ctx_(new ServerContext), |
| 227 | next_state_(&ServerRpcContextStreamingImpl::request_done), |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 228 | request_method_(request_method), |
| 229 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 230 | stream_(srv_ctx_.get()) { |
| 231 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 232 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 233 | ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| 234 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 235 | return (this->*next_state_)(ok); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 236 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 237 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 238 | srv_ctx_.reset(new ServerContext); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 239 | req_ = RequestType(); |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 240 | stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>( |
| 241 | srv_ctx_.get()); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 242 | |
| 243 | // Then request the method |
| 244 | next_state_ = &ServerRpcContextStreamingImpl::request_done; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 245 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private: |
| 249 | bool request_done(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 250 | if (!ok) { |
| 251 | return false; |
| 252 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 253 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 254 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 255 | return true; |
| 256 | } |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 257 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 258 | bool read_done(bool ok) { |
| 259 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 260 | // invoke the method |
| 261 | ResponseType response; |
| 262 | // Call the RPC processing function |
| 263 | grpc::Status status = invoke_method_(&req_, &response); |
| 264 | // initiate the write |
| 265 | stream_.Write(response, AsyncQpsServerTest::tag(this)); |
| 266 | next_state_ = &ServerRpcContextStreamingImpl::write_done; |
| 267 | } else { // client has sent writes done |
| 268 | // finish the stream |
| 269 | stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 270 | next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | bool write_done(bool ok) { |
| 275 | // now go back and get another streaming read! |
| 276 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 277 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 278 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 279 | } else { |
| 280 | stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 281 | next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 282 | } |
| 283 | return true; |
| 284 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 285 | bool finish_done(bool ok) { return false; /* reset the context */ } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 286 | |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 287 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 288 | RequestType req_; |
| 289 | bool (ServerRpcContextStreamingImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 290 | std::function<void( |
| 291 | ServerContext *, |
| 292 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 293 | request_method_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 294 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 295 | invoke_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 296 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 299 | static Status ProcessRPC(const SimpleRequest *request, |
| 300 | SimpleResponse *response) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 301 | if (request->response_size() > 0) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 302 | if (!SetPayload(request->response_type(), request->response_size(), |
| 303 | response->mutable_payload())) { |
| 304 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 305 | } |
| 306 | } |
| 307 | return Status::OK; |
| 308 | } |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 309 | std::vector<std::thread> threads_; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 310 | std::unique_ptr<grpc::Server> server_; |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 311 | std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 312 | TestService::AsyncService async_service_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 313 | std::forward_list<ServerRpcContext *> contexts_; |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 314 | |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 315 | class PerThreadShutdownState { |
| 316 | public: |
| 317 | PerThreadShutdownState() : shutdown_(false) {} |
| 318 | |
| 319 | bool shutdown() const { |
| 320 | std::lock_guard<std::mutex> lock(mutex_); |
| 321 | return shutdown_; |
| 322 | } |
| 323 | |
| 324 | void set_shutdown() { |
| 325 | std::lock_guard<std::mutex> lock(mutex_); |
| 326 | shutdown_ = true; |
| 327 | } |
| 328 | |
| 329 | private: |
| 330 | mutable std::mutex mutex_; |
| 331 | bool shutdown_; |
| 332 | }; |
| 333 | std::vector<std::unique_ptr<PerThreadShutdownState>> shutdown_state_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 336 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config, |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 337 | int port) { |
| 338 | return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 341 | } // namespace testing |
| 342 | } // namespace grpc |