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 <signal.h> |
| 35 #include <unistd.h> |
| 36 |
| 37 #include <condition_variable> |
| 38 #include <memory> |
| 39 #include <mutex> |
| 40 #include <sstream> |
| 41 |
| 42 #include <gflags/gflags.h> |
| 43 #include <grpc/grpc.h> |
| 44 #include <grpc/support/log.h> |
| 45 #include <grpc++/server.h> |
| 46 #include <grpc++/server_builder.h> |
| 47 #include <grpc++/server_context.h> |
| 48 |
| 49 #include "test/core/util/reconnect_server.h" |
| 50 #include "test/cpp/util/test_config.h" |
| 51 #include "src/proto/grpc/testing/test.grpc.pb.h" |
| 52 #include "src/proto/grpc/testing/empty.grpc.pb.h" |
| 53 #include "src/proto/grpc/testing/messages.grpc.pb.h" |
| 54 |
| 55 DEFINE_int32(control_port, 0, "Server port for controlling the server."); |
| 56 DEFINE_int32(retry_port, 0, |
| 57 "Server port for raw tcp connections. All incoming " |
| 58 "connections will be closed immediately."); |
| 59 |
| 60 using grpc::Server; |
| 61 using grpc::ServerBuilder; |
| 62 using grpc::ServerContext; |
| 63 using grpc::ServerCredentials; |
| 64 using grpc::ServerReader; |
| 65 using grpc::ServerReaderWriter; |
| 66 using grpc::ServerWriter; |
| 67 using grpc::SslServerCredentialsOptions; |
| 68 using grpc::Status; |
| 69 using grpc::testing::Empty; |
| 70 using grpc::testing::ReconnectService; |
| 71 using grpc::testing::ReconnectInfo; |
| 72 |
| 73 static bool got_sigint = false; |
| 74 |
| 75 class ReconnectServiceImpl : public ReconnectService::Service { |
| 76 public: |
| 77 explicit ReconnectServiceImpl(int retry_port) |
| 78 : retry_port_(retry_port), |
| 79 serving_(false), |
| 80 server_started_(false), |
| 81 shutdown_(false) { |
| 82 reconnect_server_init(&tcp_server_); |
| 83 } |
| 84 |
| 85 ~ReconnectServiceImpl() { |
| 86 if (server_started_) { |
| 87 reconnect_server_destroy(&tcp_server_); |
| 88 } |
| 89 } |
| 90 |
| 91 void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); } |
| 92 |
| 93 Status Start(ServerContext* context, const Empty* request, Empty* response) { |
| 94 bool start_server = true; |
| 95 std::unique_lock<std::mutex> lock(mu_); |
| 96 while (serving_ && !shutdown_) { |
| 97 cv_.wait(lock); |
| 98 } |
| 99 if (shutdown_) { |
| 100 return Status(grpc::StatusCode::UNAVAILABLE, "shutting down"); |
| 101 } |
| 102 serving_ = true; |
| 103 if (server_started_) { |
| 104 start_server = false; |
| 105 } else { |
| 106 server_started_ = true; |
| 107 } |
| 108 lock.unlock(); |
| 109 |
| 110 if (start_server) { |
| 111 reconnect_server_start(&tcp_server_, retry_port_); |
| 112 } else { |
| 113 reconnect_server_clear_timestamps(&tcp_server_); |
| 114 } |
| 115 return Status::OK; |
| 116 } |
| 117 |
| 118 Status Stop(ServerContext* context, const Empty* request, |
| 119 ReconnectInfo* response) { |
| 120 // extract timestamps and set response |
| 121 Verify(response); |
| 122 reconnect_server_clear_timestamps(&tcp_server_); |
| 123 std::lock_guard<std::mutex> lock(mu_); |
| 124 serving_ = false; |
| 125 cv_.notify_one(); |
| 126 return Status::OK; |
| 127 } |
| 128 |
| 129 void Verify(ReconnectInfo* response) { |
| 130 double expected_backoff = 1000.0; |
| 131 const double kTransmissionDelay = 100.0; |
| 132 const double kBackoffMultiplier = 1.6; |
| 133 const double kJitterFactor = 0.2; |
| 134 const int kMaxBackoffMs = 120 * 1000; |
| 135 bool passed = true; |
| 136 for (timestamp_list* cur = tcp_server_.head; cur && cur->next; |
| 137 cur = cur->next) { |
| 138 double backoff = gpr_time_to_millis( |
| 139 gpr_time_sub(cur->next->timestamp, cur->timestamp)); |
| 140 double min_backoff = expected_backoff * (1 - kJitterFactor); |
| 141 double max_backoff = expected_backoff * (1 + kJitterFactor); |
| 142 if (backoff < min_backoff - kTransmissionDelay || |
| 143 backoff > max_backoff + kTransmissionDelay) { |
| 144 passed = false; |
| 145 } |
| 146 response->add_backoff_ms(static_cast<int32_t>(backoff)); |
| 147 expected_backoff *= kBackoffMultiplier; |
| 148 expected_backoff = |
| 149 expected_backoff > kMaxBackoffMs ? kMaxBackoffMs : expected_backoff; |
| 150 } |
| 151 response->set_passed(passed); |
| 152 } |
| 153 |
| 154 void Shutdown() { |
| 155 std::lock_guard<std::mutex> lock(mu_); |
| 156 shutdown_ = true; |
| 157 cv_.notify_all(); |
| 158 } |
| 159 |
| 160 private: |
| 161 int retry_port_; |
| 162 reconnect_server tcp_server_; |
| 163 bool serving_; |
| 164 bool server_started_; |
| 165 bool shutdown_; |
| 166 std::mutex mu_; |
| 167 std::condition_variable cv_; |
| 168 }; |
| 169 |
| 170 void RunServer() { |
| 171 std::ostringstream server_address; |
| 172 server_address << "0.0.0.0:" << FLAGS_control_port; |
| 173 ReconnectServiceImpl service(FLAGS_retry_port); |
| 174 |
| 175 ServerBuilder builder; |
| 176 builder.RegisterService(&service); |
| 177 builder.AddListeningPort(server_address.str(), |
| 178 grpc::InsecureServerCredentials()); |
| 179 std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 180 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
| 181 while (!got_sigint) { |
| 182 service.Poll(5); |
| 183 } |
| 184 service.Shutdown(); |
| 185 } |
| 186 |
| 187 static void sigint_handler(int x) { got_sigint = true; } |
| 188 |
| 189 int main(int argc, char** argv) { |
| 190 grpc::testing::InitTest(&argc, &argv, true); |
| 191 signal(SIGINT, sigint_handler); |
| 192 |
| 193 GPR_ASSERT(FLAGS_control_port != 0); |
| 194 GPR_ASSERT(FLAGS_retry_port != 0); |
| 195 RunServer(); |
| 196 |
| 197 return 0; |
| 198 } |
OLD | NEW |