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 <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 Tiller | 61691f9 | 2015-03-02 08:51:52 -0800 | [diff] [blame] | 51 | #include <grpc++/server_credentials.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 52 | #include <grpc++/status.h> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 53 | #include <grpc++/stream.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 54 | #include <gtest/gtest.h> |
| 55 | #include "src/cpp/server/thread_pool.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 56 | #include "test/cpp/qps/qpstest.grpc.pb.h" |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 57 | #include "test/cpp/qps/server.h" |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 58 | |
| 59 | #include <grpc/grpc.h> |
| 60 | #include <grpc/support/log.h> |
| 61 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 62 | namespace grpc { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 63 | namespace testing { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 64 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 65 | class AsyncQpsServerTest : public Server { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 66 | public: |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 67 | AsyncQpsServerTest(const ServerConfig &config, int port) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 68 | char *server_address = NULL; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 69 | gpr_join_host_port(&server_address, "::", port); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 70 | |
| 71 | ServerBuilder builder; |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 72 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 73 | gpr_free(server_address); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 74 | |
| 75 | builder.RegisterAsyncService(&async_service_); |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 76 | for (int i = 0; i < config.threads(); i++) { |
| 77 | srv_cqs_.emplace_back(std::move(builder.AddCompletionQueue())); |
| 78 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 79 | |
| 80 | server_ = builder.BuildAndStart(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 81 | |
| 82 | using namespace std::placeholders; |
Craig Tiller | ca83dc8 | 2015-07-15 11:43:26 -0700 | [diff] [blame^] | 83 | for (int i = 0; i < 10000 / config.threads(); i++) { |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 84 | 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 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 98 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 99 | for (int i = 0; i < config.threads(); i++) { |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 100 | shutdown_state_.emplace_back(new PerThreadShutdownState()); |
| 101 | } |
| 102 | for (int i = 0; i < config.threads(); i++) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 103 | threads_.push_back(std::thread([=]() { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 104 | // Wait until work is available or we are shutting down |
| 105 | bool ok; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 106 | void *got_tag; |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 107 | while (srv_cqs_[i]->Next(&got_tag, &ok)) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 108 | ServerRpcContext *ctx = detag(got_tag); |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 109 | // The tag is a pointer to an RPC context to invoke |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame] | 110 | bool still_going = ctx->RunNextState(ok); |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 111 | if (!shutdown_state_[i]->shutdown()) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 112 | // this RPC context is done, so refresh it |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame] | 113 | if (!still_going) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 114 | ctx->Reset(); |
| 115 | } |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame] | 116 | } else { |
| 117 | return; |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 118 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 119 | } |
| 120 | return; |
| 121 | })); |
| 122 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 123 | } |
| 124 | ~AsyncQpsServerTest() { |
| 125 | server_->Shutdown(); |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 126 | for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) { |
| 127 | (*ss)->set_shutdown(); |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 128 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 129 | for (auto thr = threads_.begin(); thr != threads_.end(); thr++) { |
| 130 | thr->join(); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 131 | } |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 132 | 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 Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 139 | while (!contexts_.empty()) { |
| 140 | delete contexts_.front(); |
| 141 | contexts_.pop_front(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 144 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 145 | private: |
| 146 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 147 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 148 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 149 | virtual ~ServerRpcContext(){}; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 150 | virtual bool RunNextState(bool) = 0; // next state, return false if done |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 151 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 152 | }; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 153 | static void *tag(ServerRpcContext *func) { |
| 154 | return reinterpret_cast<void *>(func); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 155 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 156 | static ServerRpcContext *detag(void *tag) { |
| 157 | return reinterpret_cast<ServerRpcContext *>(tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | template <class RequestType, class ResponseType> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 161 | class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 162 | public: |
| 163 | ServerRpcContextUnaryImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 164 | std::function<void(ServerContext *, RequestType *, |
| 165 | grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 166 | void *)> request_method, |
| 167 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 168 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 169 | : srv_ctx_(new ServerContext), |
| 170 | next_state_(&ServerRpcContextUnaryImpl::invoker), |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 171 | request_method_(request_method), |
| 172 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 173 | response_writer_(srv_ctx_.get()) { |
| 174 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 175 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 176 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 177 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 178 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 179 | return (this->*next_state_)(ok); |
| 180 | } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 181 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 182 | srv_ctx_.reset(new ServerContext); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 183 | req_ = RequestType(); |
| 184 | response_writer_ = |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 185 | grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get()); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 186 | |
| 187 | // Then request the method |
| 188 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 189 | request_method_(srv_ctx_.get(), &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 190 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 191 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 192 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 193 | private: |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 194 | bool finisher(bool) { return false; } |
| 195 | bool invoker(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 196 | if (!ok) { |
| 197 | return false; |
| 198 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 199 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 200 | 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 Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 207 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 208 | return true; |
| 209 | } |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 210 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 211 | RequestType req_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 212 | bool (ServerRpcContextUnaryImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 213 | std::function<void(ServerContext *, RequestType *, |
| 214 | grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 215 | request_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 216 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 217 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 218 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 219 | }; |
| 220 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 221 | template <class RequestType, class ResponseType> |
| 222 | class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext { |
| 223 | public: |
| 224 | ServerRpcContextStreamingImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 225 | std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter< |
| 226 | ResponseType, RequestType> *, |
| 227 | void *)> request_method, |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 228 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 229 | invoke_method) |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 230 | : srv_ctx_(new ServerContext), |
| 231 | next_state_(&ServerRpcContextStreamingImpl::request_done), |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 232 | request_method_(request_method), |
| 233 | invoke_method_(invoke_method), |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 234 | stream_(srv_ctx_.get()) { |
| 235 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 236 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 237 | ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| 238 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 239 | return (this->*next_state_)(ok); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 240 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 241 | void Reset() GRPC_OVERRIDE { |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 242 | srv_ctx_.reset(new ServerContext); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 243 | req_ = RequestType(); |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 244 | stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>( |
| 245 | srv_ctx_.get()); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 246 | |
| 247 | // Then request the method |
| 248 | next_state_ = &ServerRpcContextStreamingImpl::request_done; |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 249 | request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | private: |
| 253 | bool request_done(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 254 | if (!ok) { |
| 255 | return false; |
| 256 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 257 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 258 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 259 | return true; |
| 260 | } |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 261 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 262 | bool read_done(bool ok) { |
| 263 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 264 | // 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; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | bool write_done(bool ok) { |
| 279 | // now go back and get another streaming read! |
| 280 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 281 | 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; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 286 | } |
| 287 | return true; |
| 288 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 289 | bool finish_done(bool ok) { return false; /* reset the context */ } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 290 | |
David Garcia Quintas | c9516d4 | 2015-06-02 13:28:46 -0700 | [diff] [blame] | 291 | std::unique_ptr<ServerContext> srv_ctx_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 292 | RequestType req_; |
| 293 | bool (ServerRpcContextStreamingImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 294 | std::function<void( |
| 295 | ServerContext *, |
| 296 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 297 | request_method_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 298 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 299 | invoke_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 300 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 303 | static Status ProcessRPC(const SimpleRequest *request, |
| 304 | SimpleResponse *response) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 305 | if (request->response_size() > 0) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 306 | 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 Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 313 | std::vector<std::thread> threads_; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 314 | std::unique_ptr<grpc::Server> server_; |
Craig Tiller | 51f938f | 2015-06-09 23:32:57 -0700 | [diff] [blame] | 315 | std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 316 | TestService::AsyncService async_service_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 317 | std::forward_list<ServerRpcContext *> contexts_; |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 318 | |
Craig Tiller | 27df2cf | 2015-07-01 15:11:29 -0700 | [diff] [blame] | 319 | 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_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 338 | }; |
| 339 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 340 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config, |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 341 | int port) { |
| 342 | return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 345 | } // namespace testing |
| 346 | } // namespace grpc |