From 24e69bf02afb0f4abdd637d1513e93e5aa227e7e Mon Sep 17 00:00:00 2001 From: Aaron Isotton Date: Fri, 26 Feb 2016 11:53:22 -0800 Subject: Added a channel argument to set the maximum reconnect backoff duration. Extended the interop test to test the custom reconnect backoffs. This closes #5377. --- test/cpp/interop/reconnect_interop_client.cc | 26 ++++++++++++++++++++------ test/cpp/interop/reconnect_interop_server.cc | 10 ++++++++-- test/cpp/util/create_test_channel.cc | 15 ++++++++++++--- test/cpp/util/create_test_channel.h | 8 +++++++- 4 files changed, 47 insertions(+), 12 deletions(-) (limited to 'test/cpp') diff --git a/test/cpp/interop/reconnect_interop_client.cc b/test/cpp/interop/reconnect_interop_client.cc index 79a60cc860..fd0144f876 100644 --- a/test/cpp/interop/reconnect_interop_client.cc +++ b/test/cpp/interop/reconnect_interop_client.cc @@ -39,6 +39,7 @@ #include #include #include +#include #include "test/cpp/util/create_test_channel.h" #include "test/cpp/util/test_config.h" #include "src/proto/grpc/testing/test.grpc.pb.h" @@ -48,13 +49,18 @@ DEFINE_int32(server_control_port, 0, "Server port for control rpcs."); DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection."); DEFINE_string(server_host, "127.0.0.1", "Server host to connect to"); +DEFINE_int32(max_reconnect_backoff_ms, 0, + "Maximum backoff time, or 0 for default."); +using grpc::CallCredentials; using grpc::Channel; +using grpc::ChannelArguments; using grpc::ClientContext; using grpc::CreateTestChannel; using grpc::Status; using grpc::testing::Empty; using grpc::testing::ReconnectInfo; +using grpc::testing::ReconnectParams; using grpc::testing::ReconnectService; int main(int argc, char** argv) { @@ -68,17 +74,25 @@ int main(int argc, char** argv) { ReconnectService::NewStub( CreateTestChannel(server_address.str(), false))); ClientContext start_context; - Empty empty_request; + ReconnectParams reconnect_params; + reconnect_params.set_max_reconnect_backoff_ms(FLAGS_max_reconnect_backoff_ms); Empty empty_response; Status start_status = - control_stub->Start(&start_context, empty_request, &empty_response); + control_stub->Start(&start_context, reconnect_params, &empty_response); GPR_ASSERT(start_status.ok()); gpr_log(GPR_INFO, "Starting connections with retries."); server_address.str(""); server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port; + ChannelArguments channel_args; + if (FLAGS_max_reconnect_backoff_ms > 0) { + channel_args.SetInt(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, + FLAGS_max_reconnect_backoff_ms); + } std::shared_ptr retry_channel = - CreateTestChannel(server_address.str(), true); + CreateTestChannel(server_address.str(), "foo.test.google.fr", true, false, + std::shared_ptr(), channel_args); + // About 13 retries. const int kDeadlineSeconds = 540; // Use any rpc to test retry. @@ -88,15 +102,15 @@ int main(int argc, char** argv) { retry_context.set_deadline(std::chrono::system_clock::now() + std::chrono::seconds(kDeadlineSeconds)); Status retry_status = - retry_stub->Start(&retry_context, empty_request, &empty_response); + retry_stub->Start(&retry_context, reconnect_params, &empty_response); GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED); gpr_log(GPR_INFO, "Done retrying, getting final data from server"); ClientContext stop_context; ReconnectInfo response; - Status stop_status = - control_stub->Stop(&stop_context, empty_request, &response); + Status stop_status = control_stub->Stop(&stop_context, Empty(), &response); GPR_ASSERT(stop_status.ok()); GPR_ASSERT(response.passed() == true); + gpr_log(GPR_INFO, "Passed"); return 0; } diff --git a/test/cpp/interop/reconnect_interop_server.cc b/test/cpp/interop/reconnect_interop_server.cc index 3602b8c2b0..97a5afc582 100644 --- a/test/cpp/interop/reconnect_interop_server.cc +++ b/test/cpp/interop/reconnect_interop_server.cc @@ -69,6 +69,7 @@ using grpc::Status; using grpc::testing::Empty; using grpc::testing::ReconnectService; using grpc::testing::ReconnectInfo; +using grpc::testing::ReconnectParams; static bool got_sigint = false; @@ -90,7 +91,8 @@ class ReconnectServiceImpl : public ReconnectService::Service { void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); } - Status Start(ServerContext* context, const Empty* request, Empty* response) { + Status Start(ServerContext* context, const ReconnectParams* request, + Empty* response) { bool start_server = true; std::unique_lock lock(mu_); while (serving_ && !shutdown_) { @@ -103,6 +105,8 @@ class ReconnectServiceImpl : public ReconnectService::Service { if (server_started_) { start_server = false; } else { + tcp_server_.max_reconnect_backoff_ms = + request->max_reconnect_backoff_ms(); server_started_ = true; } lock.unlock(); @@ -131,7 +135,9 @@ class ReconnectServiceImpl : public ReconnectService::Service { const double kTransmissionDelay = 100.0; const double kBackoffMultiplier = 1.6; const double kJitterFactor = 0.2; - const int kMaxBackoffMs = 120 * 1000; + const int kMaxBackoffMs = tcp_server_.max_reconnect_backoff_ms + ? tcp_server_.max_reconnect_backoff_ms + : 120 * 1000; bool passed = true; for (timestamp_list* cur = tcp_server_.head; cur && cur->next; cur = cur->next) { diff --git a/test/cpp/util/create_test_channel.cc b/test/cpp/util/create_test_channel.cc index 0cd9f9e767..fe8b5d5423 100644 --- a/test/cpp/util/create_test_channel.cc +++ b/test/cpp/util/create_test_channel.cc @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -58,8 +58,9 @@ namespace grpc { std::shared_ptr CreateTestChannel( const grpc::string& server, const grpc::string& override_hostname, bool enable_ssl, bool use_prod_roots, - const std::shared_ptr& creds) { - ChannelArguments channel_args; + const std::shared_ptr& creds, + const ChannelArguments& args) { + ChannelArguments channel_args(args); if (enable_ssl) { const char* roots_certs = use_prod_roots ? "" : test_root_cert; SslCredentialsOptions ssl_opts = {roots_certs, "", ""}; @@ -81,6 +82,14 @@ std::shared_ptr CreateTestChannel( } } +std::shared_ptr CreateTestChannel( + const grpc::string& server, const grpc::string& override_hostname, + bool enable_ssl, bool use_prod_roots, + const std::shared_ptr& creds) { + return CreateTestChannel(server, override_hostname, enable_ssl, + use_prod_roots, creds, ChannelArguments()); +} + std::shared_ptr CreateTestChannel( const grpc::string& server, const grpc::string& override_hostname, bool enable_ssl, bool use_prod_roots) { diff --git a/test/cpp/util/create_test_channel.h b/test/cpp/util/create_test_channel.h index b50d653de3..4ff666dc1b 100644 --- a/test/cpp/util/create_test_channel.h +++ b/test/cpp/util/create_test_channel.h @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -53,6 +53,12 @@ std::shared_ptr CreateTestChannel( bool enable_ssl, bool use_prod_roots, const std::shared_ptr& creds); +std::shared_ptr CreateTestChannel( + const grpc::string& server, const grpc::string& override_hostname, + bool enable_ssl, bool use_prod_roots, + const std::shared_ptr& creds, + const ChannelArguments& args); + } // namespace grpc #endif // GRPC_TEST_CPP_UTIL_CREATE_TEST_CHANNEL_H -- cgit v1.2.3