OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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> |
| 36 #include <memory> |
| 37 #include <mutex> |
| 38 #include <thread> |
| 39 |
| 40 #include <gflags/gflags.h> |
| 41 #include <grpc++/generic/async_generic_service.h> |
| 42 #include <grpc++/security/server_credentials.h> |
| 43 #include <grpc++/server.h> |
| 44 #include <grpc++/server_builder.h> |
| 45 #include <grpc++/server_context.h> |
| 46 #include <grpc++/support/config.h> |
| 47 #include <grpc/grpc.h> |
| 48 #include <grpc/support/alloc.h> |
| 49 #include <grpc/support/host_port.h> |
| 50 #include <grpc/support/log.h> |
| 51 #include <gtest/gtest.h> |
| 52 |
| 53 #include "src/proto/grpc/testing/services.grpc.pb.h" |
| 54 #include "test/core/util/test_config.h" |
| 55 #include "test/cpp/qps/server.h" |
| 56 |
| 57 namespace grpc { |
| 58 namespace testing { |
| 59 |
| 60 template <class RequestType, class ResponseType, class ServiceType, |
| 61 class ServerContextType> |
| 62 class AsyncQpsServerTest : public Server { |
| 63 public: |
| 64 AsyncQpsServerTest( |
| 65 const ServerConfig &config, |
| 66 std::function<void(ServerBuilder *, ServiceType *)> register_service, |
| 67 std::function<void(ServiceType *, ServerContextType *, RequestType *, |
| 68 ServerAsyncResponseWriter<ResponseType> *, |
| 69 CompletionQueue *, ServerCompletionQueue *, void *)> |
| 70 request_unary_function, |
| 71 std::function<void(ServiceType *, ServerContextType *, |
| 72 ServerAsyncReaderWriter<ResponseType, RequestType> *, |
| 73 CompletionQueue *, ServerCompletionQueue *, void *)> |
| 74 request_streaming_function, |
| 75 std::function<grpc::Status(const PayloadConfig &, const RequestType *, |
| 76 ResponseType *)> process_rpc) |
| 77 : Server(config) { |
| 78 char *server_address = NULL; |
| 79 |
| 80 gpr_join_host_port(&server_address, "::", port()); |
| 81 |
| 82 ServerBuilder builder; |
| 83 builder.AddListeningPort(server_address, |
| 84 Server::CreateServerCredentials(config)); |
| 85 gpr_free(server_address); |
| 86 |
| 87 register_service(&builder, &async_service_); |
| 88 |
| 89 int num_threads = config.async_server_threads(); |
| 90 if (num_threads <= 0) { // dynamic sizing |
| 91 num_threads = cores(); |
| 92 gpr_log(GPR_INFO, "Sizing async server to %d threads", num_threads); |
| 93 } |
| 94 |
| 95 for (int i = 0; i < num_threads; i++) { |
| 96 srv_cqs_.emplace_back(builder.AddCompletionQueue()); |
| 97 } |
| 98 |
| 99 server_ = builder.BuildAndStart(); |
| 100 |
| 101 using namespace std::placeholders; |
| 102 |
| 103 auto process_rpc_bound = |
| 104 std::bind(process_rpc, config.payload_config(), _1, _2); |
| 105 |
| 106 for (int i = 0; i < 10000 / num_threads; i++) { |
| 107 for (int j = 0; j < num_threads; j++) { |
| 108 if (request_unary_function) { |
| 109 auto request_unary = |
| 110 std::bind(request_unary_function, &async_service_, _1, _2, _3, |
| 111 srv_cqs_[j].get(), srv_cqs_[j].get(), _4); |
| 112 contexts_.push_front( |
| 113 new ServerRpcContextUnaryImpl(request_unary, process_rpc_bound)); |
| 114 } |
| 115 if (request_streaming_function) { |
| 116 auto request_streaming = |
| 117 std::bind(request_streaming_function, &async_service_, _1, _2, |
| 118 srv_cqs_[j].get(), srv_cqs_[j].get(), _3); |
| 119 contexts_.push_front(new ServerRpcContextStreamingImpl( |
| 120 request_streaming, process_rpc_bound)); |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 125 for (int i = 0; i < num_threads; i++) { |
| 126 shutdown_state_.emplace_back(new PerThreadShutdownState()); |
| 127 } |
| 128 for (int i = 0; i < num_threads; i++) { |
| 129 threads_.emplace_back(&AsyncQpsServerTest::ThreadFunc, this, i); |
| 130 } |
| 131 } |
| 132 ~AsyncQpsServerTest() { |
| 133 auto deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); |
| 134 server_->Shutdown(deadline); |
| 135 for (auto ss = shutdown_state_.begin(); ss != shutdown_state_.end(); ++ss) { |
| 136 (*ss)->set_shutdown(); |
| 137 } |
| 138 for (auto thr = threads_.begin(); thr != threads_.end(); thr++) { |
| 139 thr->join(); |
| 140 } |
| 141 for (auto cq = srv_cqs_.begin(); cq != srv_cqs_.end(); ++cq) { |
| 142 (*cq)->Shutdown(); |
| 143 bool ok; |
| 144 void *got_tag; |
| 145 while ((*cq)->Next(&got_tag, &ok)) |
| 146 ; |
| 147 } |
| 148 while (!contexts_.empty()) { |
| 149 delete contexts_.front(); |
| 150 contexts_.pop_front(); |
| 151 } |
| 152 } |
| 153 |
| 154 private: |
| 155 void ThreadFunc(int rank) { |
| 156 // Wait until work is available or we are shutting down |
| 157 bool ok; |
| 158 void *got_tag; |
| 159 while (srv_cqs_[rank]->Next(&got_tag, &ok)) { |
| 160 ServerRpcContext *ctx = detag(got_tag); |
| 161 // The tag is a pointer to an RPC context to invoke |
| 162 const bool still_going = ctx->RunNextState(ok); |
| 163 if (!shutdown_state_[rank]->shutdown()) { |
| 164 // this RPC context is done, so refresh it |
| 165 if (!still_going) { |
| 166 ctx->Reset(); |
| 167 } |
| 168 } else { |
| 169 return; |
| 170 } |
| 171 } |
| 172 return; |
| 173 } |
| 174 |
| 175 class ServerRpcContext { |
| 176 public: |
| 177 ServerRpcContext() {} |
| 178 virtual ~ServerRpcContext(){}; |
| 179 virtual bool RunNextState(bool) = 0; // next state, return false if done |
| 180 virtual void Reset() = 0; // start this back at a clean state |
| 181 }; |
| 182 static void *tag(ServerRpcContext *func) { |
| 183 return reinterpret_cast<void *>(func); |
| 184 } |
| 185 static ServerRpcContext *detag(void *tag) { |
| 186 return reinterpret_cast<ServerRpcContext *>(tag); |
| 187 } |
| 188 |
| 189 class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext { |
| 190 public: |
| 191 ServerRpcContextUnaryImpl( |
| 192 std::function<void(ServerContextType *, RequestType *, |
| 193 grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 194 void *)> request_method, |
| 195 std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 196 invoke_method) |
| 197 : srv_ctx_(new ServerContextType), |
| 198 next_state_(&ServerRpcContextUnaryImpl::invoker), |
| 199 request_method_(request_method), |
| 200 invoke_method_(invoke_method), |
| 201 response_writer_(srv_ctx_.get()) { |
| 202 request_method_(srv_ctx_.get(), &req_, &response_writer_, |
| 203 AsyncQpsServerTest::tag(this)); |
| 204 } |
| 205 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
| 206 bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 207 return (this->*next_state_)(ok); |
| 208 } |
| 209 void Reset() GRPC_OVERRIDE { |
| 210 srv_ctx_.reset(new ServerContextType); |
| 211 req_ = RequestType(); |
| 212 response_writer_ = |
| 213 grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get()); |
| 214 |
| 215 // Then request the method |
| 216 next_state_ = &ServerRpcContextUnaryImpl::invoker; |
| 217 request_method_(srv_ctx_.get(), &req_, &response_writer_, |
| 218 AsyncQpsServerTest::tag(this)); |
| 219 } |
| 220 |
| 221 private: |
| 222 bool finisher(bool) { return false; } |
| 223 bool invoker(bool ok) { |
| 224 if (!ok) { |
| 225 return false; |
| 226 } |
| 227 |
| 228 ResponseType response; |
| 229 |
| 230 // Call the RPC processing function |
| 231 grpc::Status status = invoke_method_(&req_, &response); |
| 232 |
| 233 // Have the response writer work and invoke on_finish when done |
| 234 next_state_ = &ServerRpcContextUnaryImpl::finisher; |
| 235 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
| 236 return true; |
| 237 } |
| 238 std::unique_ptr<ServerContextType> srv_ctx_; |
| 239 RequestType req_; |
| 240 bool (ServerRpcContextUnaryImpl::*next_state_)(bool); |
| 241 std::function<void(ServerContextType *, RequestType *, |
| 242 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
| 243 request_method_; |
| 244 std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 245 invoke_method_; |
| 246 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 247 }; |
| 248 |
| 249 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext { |
| 250 public: |
| 251 ServerRpcContextStreamingImpl( |
| 252 std::function<void( |
| 253 ServerContextType *, |
| 254 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 255 request_method, |
| 256 std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 257 invoke_method) |
| 258 : srv_ctx_(new ServerContextType), |
| 259 next_state_(&ServerRpcContextStreamingImpl::request_done), |
| 260 request_method_(request_method), |
| 261 invoke_method_(invoke_method), |
| 262 stream_(srv_ctx_.get()) { |
| 263 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
| 264 } |
| 265 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| 266 bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 267 return (this->*next_state_)(ok); |
| 268 } |
| 269 void Reset() GRPC_OVERRIDE { |
| 270 srv_ctx_.reset(new ServerContextType); |
| 271 req_ = RequestType(); |
| 272 stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>( |
| 273 srv_ctx_.get()); |
| 274 |
| 275 // Then request the method |
| 276 next_state_ = &ServerRpcContextStreamingImpl::request_done; |
| 277 request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this)); |
| 278 } |
| 279 |
| 280 private: |
| 281 bool request_done(bool ok) { |
| 282 if (!ok) { |
| 283 return false; |
| 284 } |
| 285 stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 286 next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 287 return true; |
| 288 } |
| 289 |
| 290 bool read_done(bool ok) { |
| 291 if (ok) { |
| 292 // invoke the method |
| 293 ResponseType response; |
| 294 // Call the RPC processing function |
| 295 grpc::Status status = invoke_method_(&req_, &response); |
| 296 // initiate the write |
| 297 stream_.Write(response, AsyncQpsServerTest::tag(this)); |
| 298 next_state_ = &ServerRpcContextStreamingImpl::write_done; |
| 299 } else { // client has sent writes done |
| 300 // finish the stream |
| 301 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 302 next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
| 303 } |
| 304 return true; |
| 305 } |
| 306 bool write_done(bool ok) { |
| 307 // now go back and get another streaming read! |
| 308 if (ok) { |
| 309 stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 310 next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 311 } else { |
| 312 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 313 next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
| 314 } |
| 315 return true; |
| 316 } |
| 317 bool finish_done(bool ok) { return false; /* reset the context */ } |
| 318 |
| 319 std::unique_ptr<ServerContextType> srv_ctx_; |
| 320 RequestType req_; |
| 321 bool (ServerRpcContextStreamingImpl::*next_state_)(bool); |
| 322 std::function<void( |
| 323 ServerContextType *, |
| 324 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 325 request_method_; |
| 326 std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 327 invoke_method_; |
| 328 grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_; |
| 329 }; |
| 330 |
| 331 std::vector<std::thread> threads_; |
| 332 std::unique_ptr<grpc::Server> server_; |
| 333 std::vector<std::unique_ptr<grpc::ServerCompletionQueue>> srv_cqs_; |
| 334 ServiceType async_service_; |
| 335 std::forward_list<ServerRpcContext *> contexts_; |
| 336 |
| 337 class PerThreadShutdownState { |
| 338 public: |
| 339 PerThreadShutdownState() : shutdown_(false) {} |
| 340 |
| 341 bool shutdown() const { |
| 342 std::lock_guard<std::mutex> lock(mutex_); |
| 343 return shutdown_; |
| 344 } |
| 345 |
| 346 void set_shutdown() { |
| 347 std::lock_guard<std::mutex> lock(mutex_); |
| 348 shutdown_ = true; |
| 349 } |
| 350 |
| 351 private: |
| 352 mutable std::mutex mutex_; |
| 353 bool shutdown_; |
| 354 }; |
| 355 std::vector<std::unique_ptr<PerThreadShutdownState>> shutdown_state_; |
| 356 }; |
| 357 |
| 358 static void RegisterBenchmarkService(ServerBuilder *builder, |
| 359 BenchmarkService::AsyncService *service) { |
| 360 builder->RegisterService(service); |
| 361 } |
| 362 static void RegisterGenericService(ServerBuilder *builder, |
| 363 grpc::AsyncGenericService *service) { |
| 364 builder->RegisterAsyncGenericService(service); |
| 365 } |
| 366 |
| 367 static Status ProcessSimpleRPC(const PayloadConfig &, |
| 368 const SimpleRequest *request, |
| 369 SimpleResponse *response) { |
| 370 if (request->response_size() > 0) { |
| 371 if (!Server::SetPayload(request->response_type(), request->response_size(), |
| 372 response->mutable_payload())) { |
| 373 return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 374 } |
| 375 } |
| 376 return Status::OK; |
| 377 } |
| 378 |
| 379 static Status ProcessGenericRPC(const PayloadConfig &payload_config, |
| 380 const ByteBuffer *request, |
| 381 ByteBuffer *response) { |
| 382 int resp_size = payload_config.bytebuf_params().resp_size(); |
| 383 std::unique_ptr<char[]> buf(new char[resp_size]); |
| 384 gpr_slice s = gpr_slice_from_copied_buffer(buf.get(), resp_size); |
| 385 Slice slice(s, Slice::STEAL_REF); |
| 386 *response = ByteBuffer(&slice, 1); |
| 387 return Status::OK; |
| 388 } |
| 389 |
| 390 std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config) { |
| 391 return std::unique_ptr<Server>(new AsyncQpsServerTest< |
| 392 SimpleRequest, SimpleResponse, BenchmarkService::AsyncService, |
| 393 grpc::ServerContext>( |
| 394 config, RegisterBenchmarkService, |
| 395 &BenchmarkService::AsyncService::RequestUnaryCall, |
| 396 &BenchmarkService::AsyncService::RequestStreamingCall, ProcessSimpleRPC)); |
| 397 } |
| 398 std::unique_ptr<Server> CreateAsyncGenericServer(const ServerConfig &config) { |
| 399 return std::unique_ptr<Server>( |
| 400 new AsyncQpsServerTest<ByteBuffer, ByteBuffer, grpc::AsyncGenericService, |
| 401 grpc::GenericServerContext>( |
| 402 config, RegisterGenericService, nullptr, |
| 403 &grpc::AsyncGenericService::RequestCall, ProcessGenericRPC)); |
| 404 } |
| 405 |
| 406 } // namespace testing |
| 407 } // namespace grpc |
OLD | NEW |