worker.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <cassert>
  34. #include <memory>
  35. #include <mutex>
  36. #include <string>
  37. #include <thread>
  38. #include <vector>
  39. #include <sstream>
  40. #include <sys/signal.h>
  41. #include <grpc/grpc.h>
  42. #include <grpc/support/alloc.h>
  43. #include <grpc/support/histogram.h>
  44. #include <grpc/support/log.h>
  45. #include <grpc/support/host_port.h>
  46. #include <gflags/gflags.h>
  47. #include <grpc++/client_context.h>
  48. #include <grpc++/status.h>
  49. #include <grpc++/server.h>
  50. #include <grpc++/server_builder.h>
  51. #include <grpc++/stream.h>
  52. #include "test/core/util/grpc_profiler.h"
  53. #include "test/cpp/util/create_test_channel.h"
  54. #include "test/cpp/qps/qpstest.pb.h"
  55. #include "test/cpp/qps/client.h"
  56. #include "test/cpp/qps/server.h"
  57. DEFINE_int32(driver_port, 0, "Driver server port.");
  58. DEFINE_int32(server_port, 0, "Spawned server port.");
  59. // In some distros, gflags is in the namespace google, and in some others,
  60. // in gflags. This hack is enabling us to find both.
  61. namespace google { }
  62. namespace gflags { }
  63. using namespace google;
  64. using namespace gflags;
  65. static bool got_sigint = false;
  66. static void sigint_handler(int x) { got_sigint = 1; }
  67. namespace grpc {
  68. namespace testing {
  69. std::unique_ptr<Client> CreateClient(const ClientConfig& config) {
  70. switch (config.client_type()) {
  71. case ClientType::SYNCHRONOUS_CLIENT: return CreateSynchronousClient(config);
  72. case ClientType::ASYNC_CLIENT: return CreateAsyncClient(config);
  73. }
  74. abort();
  75. }
  76. std::unique_ptr<Server> CreateServer(const ServerConfig& config) {
  77. switch (config.server_type()) {
  78. case ServerType::SYNCHRONOUS_SERVER: return CreateSynchronousServer(config, FLAGS_server_port);
  79. case ServerType::ASYNC_SERVER: return CreateAsyncServer(config, FLAGS_server_port);
  80. }
  81. abort();
  82. }
  83. class WorkerImpl final : public Worker::Service {
  84. public:
  85. WorkerImpl() : acquired_(false) {}
  86. Status RunTest(ServerContext* ctx, ServerReaderWriter<ClientStatus, ClientArgs>* stream) GRPC_OVERRIDE {
  87. InstanceGuard g(this);
  88. if (!g.Acquired()) {
  89. return Status(RESOURCE_EXHAUSTED);
  90. }
  91. ClientArgs args;
  92. if (!stream->Read(&args)) {
  93. return Status(INVALID_ARGUMENT);
  94. }
  95. if (!args.has_setup()) {
  96. return Status(INVALID_ARGUMENT);
  97. }
  98. auto client = CreateClient(args.setup());
  99. if (!client) {
  100. return Status(INVALID_ARGUMENT);
  101. }
  102. return Status::OK;
  103. }
  104. Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) GRPC_OVERRIDE {
  105. InstanceGuard g(this);
  106. if (!g.Acquired()) {
  107. return Status(RESOURCE_EXHAUSTED);
  108. }
  109. ServerArgs args;
  110. if (!stream->Read(&args)) {
  111. return Status(INVALID_ARGUMENT);
  112. }
  113. if (!args.has_config()) {
  114. return Status(INVALID_ARGUMENT);
  115. }
  116. auto server = CreateServer(args.config());
  117. if (!server) {
  118. return Status(INVALID_ARGUMENT);
  119. }
  120. return Status::OK;
  121. }
  122. private:
  123. class InstanceGuard {
  124. public:
  125. InstanceGuard(WorkerImpl* impl) : impl_(impl), acquired_(impl->TryAcquireInstance()) {}
  126. ~InstanceGuard() { if (acquired_) { impl_->ReleaseInstance(); } }
  127. bool Acquired() const { return acquired_; }
  128. private:
  129. WorkerImpl* const impl_;
  130. const bool acquired_;
  131. };
  132. bool TryAcquireInstance() {
  133. std::lock_guard<std::mutex> g(mu_);
  134. if (acquired_) return false;
  135. acquired_ = true;
  136. return true;
  137. }
  138. void ReleaseInstance() {
  139. std::lock_guard<std::mutex> g(mu_);
  140. GPR_ASSERT(acquired_);
  141. acquired_ = false;
  142. }
  143. std::mutex mu_;
  144. bool acquired_;
  145. };
  146. static void RunServer() {
  147. char* server_address = NULL;
  148. gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
  149. WorkerImpl service;
  150. ServerBuilder builder;
  151. builder.AddPort(server_address);
  152. builder.RegisterService(&service);
  153. gpr_free(server_address);
  154. auto server = builder.BuildAndStart();
  155. while (!got_sigint) {
  156. std::this_thread::sleep_for(std::chrono::seconds(5));
  157. }
  158. }
  159. } // namespace testing
  160. } // namespace grpc
  161. int main(int argc, char **argv) {
  162. signal(SIGINT, sigint_handler);
  163. grpc_init();
  164. ParseCommandLineFlags(&argc, &argv, true);
  165. grpc::testing::RunServer();
  166. grpc_shutdown();
  167. return 0;
  168. }