grpclb_fallback_test.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include <arpa/inet.h>
  20. #include <fcntl.h>
  21. #include <inttypes.h>
  22. #include <netinet/in.h>
  23. #include <netinet/tcp.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #include <chrono>
  27. #include <cstdlib>
  28. #include <memory>
  29. #include <string>
  30. #include <thread>
  31. #include "absl/flags/flag.h"
  32. #include <grpc/support/alloc.h>
  33. #include <grpc/support/log.h>
  34. #include <grpcpp/channel.h>
  35. #include <grpcpp/client_context.h>
  36. #include <grpcpp/grpcpp.h>
  37. #include <grpcpp/support/channel_arguments.h>
  38. #include "src/core/lib/gpr/string.h"
  39. #include "src/core/lib/iomgr/port.h"
  40. #include "src/core/lib/iomgr/socket_mutator.h"
  41. #include "src/proto/grpc/testing/empty.pb.h"
  42. #include "src/proto/grpc/testing/messages.pb.h"
  43. #include "src/proto/grpc/testing/test.grpc.pb.h"
  44. #include "src/proto/grpc/testing/test.pb.h"
  45. #include "test/cpp/util/test_config.h"
  46. #include "test/cpp/util/test_credentials_provider.h"
  47. ABSL_FLAG(std::string, custom_credentials_type, "",
  48. "User provided credentials type.");
  49. ABSL_FLAG(std::string, server_uri, "localhost:1000", "Server URI target");
  50. ABSL_FLAG(std::string, unroute_lb_and_backend_addrs_cmd, "exit 1",
  51. "Shell command used to make LB and backend addresses unroutable");
  52. ABSL_FLAG(std::string, blackhole_lb_and_backend_addrs_cmd, "exit 1",
  53. "Shell command used to make LB and backend addresses blackholed");
  54. ABSL_FLAG(
  55. std::string, test_case, "",
  56. "Test case to run. Valid options are:\n\n"
  57. "fast_fallback_before_startup : fallback before establishing connection to "
  58. "LB;\n"
  59. "fast_fallback_after_startup : fallback after startup due to LB/backend "
  60. "addresses becoming unroutable;\n"
  61. "slow_fallback_before_startup : fallback before startup due to LB address "
  62. "being blackholed;\n"
  63. "slow_fallback_after_startup : fallback after startup due to LB/backend "
  64. "addresses becoming blackholed;\n");
  65. #ifdef LINUX_VERSION_CODE
  66. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
  67. #define SOCKET_SUPPORTS_TCP_USER_TIMEOUT
  68. #endif
  69. #endif
  70. #ifdef SOCKET_SUPPORTS_TCP_USER_TIMEOUT
  71. using grpc::testing::GrpclbRouteType;
  72. using grpc::testing::SimpleRequest;
  73. using grpc::testing::SimpleResponse;
  74. using grpc::testing::TestService;
  75. namespace {
  76. enum RpcMode {
  77. FailFast,
  78. WaitForReady,
  79. };
  80. GrpclbRouteType DoRPCAndGetPath(TestService::Stub* stub, int deadline_seconds,
  81. RpcMode rpc_mode) {
  82. gpr_log(GPR_INFO, "DoRPCAndGetPath deadline_seconds:%d rpc_mode:%d",
  83. deadline_seconds, rpc_mode);
  84. SimpleRequest request;
  85. SimpleResponse response;
  86. grpc::ClientContext context;
  87. if (rpc_mode == WaitForReady) {
  88. context.set_wait_for_ready(true);
  89. }
  90. request.set_fill_grpclb_route_type(true);
  91. std::chrono::system_clock::time_point deadline =
  92. std::chrono::system_clock::now() + std::chrono::seconds(deadline_seconds);
  93. context.set_deadline(deadline);
  94. grpc::Status s = stub->UnaryCall(&context, request, &response);
  95. if (!s.ok()) {
  96. gpr_log(GPR_INFO, "DoRPCAndGetPath failed. status-message: %s",
  97. s.error_message().c_str());
  98. return GrpclbRouteType::GRPCLB_ROUTE_TYPE_UNKNOWN;
  99. }
  100. GPR_ASSERT(response.grpclb_route_type() ==
  101. GrpclbRouteType::GRPCLB_ROUTE_TYPE_BACKEND ||
  102. response.grpclb_route_type() ==
  103. GrpclbRouteType::GRPCLB_ROUTE_TYPE_FALLBACK);
  104. gpr_log(GPR_INFO, "DoRPCAndGetPath done. grpclb_route_type:%d",
  105. response.grpclb_route_type());
  106. return response.grpclb_route_type();
  107. }
  108. GrpclbRouteType DoRPCAndGetPath(TestService::Stub* stub, int deadline_seconds) {
  109. return DoRPCAndGetPath(stub, deadline_seconds, FailFast);
  110. }
  111. GrpclbRouteType DoWaitForReadyRPCAndGetPath(TestService::Stub* stub,
  112. int deadline_seconds) {
  113. return DoRPCAndGetPath(stub, deadline_seconds, WaitForReady);
  114. }
  115. bool TcpUserTimeoutMutateFd(int fd, grpc_socket_mutator* /*mutator*/) {
  116. int timeout = 20000; // 20 seconds
  117. gpr_log(GPR_INFO, "Setting socket option TCP_USER_TIMEOUT on fd: %d", fd);
  118. if (0 != setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout,
  119. sizeof(timeout))) {
  120. gpr_log(GPR_ERROR, "Failed to set socket option TCP_USER_TIMEOUT");
  121. abort();
  122. }
  123. int newval;
  124. socklen_t len = sizeof(newval);
  125. if (0 != getsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &newval, &len) ||
  126. newval != timeout) {
  127. gpr_log(GPR_ERROR, "Failed to get expected socket option TCP_USER_TIMEOUT");
  128. abort();
  129. }
  130. return true;
  131. }
  132. int TcpUserTimeoutCompare(grpc_socket_mutator* /*a*/,
  133. grpc_socket_mutator* /*b*/) {
  134. return 0;
  135. }
  136. void TcpUserTimeoutDestroy(grpc_socket_mutator* mutator) { delete mutator; }
  137. const grpc_socket_mutator_vtable kTcpUserTimeoutMutatorVtable =
  138. grpc_socket_mutator_vtable{TcpUserTimeoutMutateFd, TcpUserTimeoutCompare,
  139. TcpUserTimeoutDestroy, nullptr};
  140. std::unique_ptr<TestService::Stub> CreateFallbackTestStub() {
  141. grpc::ChannelArguments channel_args;
  142. grpc_socket_mutator* tcp_user_timeout_mutator = new grpc_socket_mutator();
  143. grpc_socket_mutator_init(tcp_user_timeout_mutator,
  144. &kTcpUserTimeoutMutatorVtable);
  145. channel_args.SetSocketMutator(tcp_user_timeout_mutator);
  146. // Allow LB policy to be configured by service config
  147. channel_args.SetInt(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION, 0);
  148. std::shared_ptr<grpc::ChannelCredentials> channel_creds =
  149. grpc::testing::GetCredentialsProvider()->GetChannelCredentials(
  150. absl::GetFlag(FLAGS_custom_credentials_type), &channel_args);
  151. return TestService::NewStub(grpc::CreateCustomChannel(
  152. absl::GetFlag(FLAGS_server_uri), channel_creds, channel_args));
  153. }
  154. void RunCommand(const std::string& command) {
  155. gpr_log(GPR_INFO, "RunCommand: |%s|", command.c_str());
  156. int out = std::system(command.c_str());
  157. if (WIFEXITED(out)) {
  158. int code = WEXITSTATUS(out);
  159. if (code != 0) {
  160. gpr_log(GPR_ERROR, "RunCommand failed exit code:%d command:|%s|", code,
  161. command.c_str());
  162. abort();
  163. }
  164. } else {
  165. gpr_log(GPR_ERROR, "RunCommand failed command:|%s|", command.c_str());
  166. abort();
  167. }
  168. }
  169. void RunFallbackBeforeStartupTest(
  170. const std::string& break_lb_and_backend_conns_cmd,
  171. int per_rpc_deadline_seconds) {
  172. std::unique_ptr<TestService::Stub> stub = CreateFallbackTestStub();
  173. RunCommand(break_lb_and_backend_conns_cmd);
  174. for (size_t i = 0; i < 30; i++) {
  175. GrpclbRouteType grpclb_route_type =
  176. DoRPCAndGetPath(stub.get(), per_rpc_deadline_seconds);
  177. if (grpclb_route_type != GrpclbRouteType::GRPCLB_ROUTE_TYPE_FALLBACK) {
  178. gpr_log(GPR_ERROR, "Expected grpclb route type: FALLBACK. Got: %d",
  179. grpclb_route_type);
  180. abort();
  181. }
  182. std::this_thread::sleep_for(std::chrono::seconds(1));
  183. }
  184. }
  185. void DoFastFallbackBeforeStartup() {
  186. RunFallbackBeforeStartupTest(
  187. absl::GetFlag(FLAGS_unroute_lb_and_backend_addrs_cmd), 9);
  188. }
  189. void DoSlowFallbackBeforeStartup() {
  190. RunFallbackBeforeStartupTest(
  191. absl::GetFlag(FLAGS_blackhole_lb_and_backend_addrs_cmd), 20);
  192. }
  193. void RunFallbackAfterStartupTest(
  194. const std::string& break_lb_and_backend_conns_cmd) {
  195. std::unique_ptr<TestService::Stub> stub = CreateFallbackTestStub();
  196. GrpclbRouteType grpclb_route_type = DoRPCAndGetPath(stub.get(), 20);
  197. if (grpclb_route_type != GrpclbRouteType::GRPCLB_ROUTE_TYPE_BACKEND) {
  198. gpr_log(GPR_ERROR, "Expected grpclb route type: BACKEND. Got: %d",
  199. grpclb_route_type);
  200. abort();
  201. }
  202. RunCommand(break_lb_and_backend_conns_cmd);
  203. for (size_t i = 0; i < 40; i++) {
  204. GrpclbRouteType grpclb_route_type =
  205. DoWaitForReadyRPCAndGetPath(stub.get(), 1);
  206. // Backends should be unreachable by now, otherwise the test is broken.
  207. GPR_ASSERT(grpclb_route_type != GrpclbRouteType::GRPCLB_ROUTE_TYPE_BACKEND);
  208. if (grpclb_route_type == GrpclbRouteType::GRPCLB_ROUTE_TYPE_FALLBACK) {
  209. gpr_log(GPR_INFO,
  210. "Made one successul RPC to a fallback. Now expect the same for "
  211. "the rest.");
  212. break;
  213. } else {
  214. gpr_log(GPR_ERROR, "Retryable RPC failure on iteration: %" PRIdPTR, i);
  215. }
  216. }
  217. for (size_t i = 0; i < 30; i++) {
  218. GrpclbRouteType grpclb_route_type = DoRPCAndGetPath(stub.get(), 20);
  219. if (grpclb_route_type != GrpclbRouteType::GRPCLB_ROUTE_TYPE_FALLBACK) {
  220. gpr_log(GPR_ERROR, "Expected grpclb route type: FALLBACK. Got: %d",
  221. grpclb_route_type);
  222. abort();
  223. }
  224. std::this_thread::sleep_for(std::chrono::seconds(1));
  225. }
  226. }
  227. void DoFastFallbackAfterStartup() {
  228. RunFallbackAfterStartupTest(
  229. absl::GetFlag(FLAGS_unroute_lb_and_backend_addrs_cmd));
  230. }
  231. void DoSlowFallbackAfterStartup() {
  232. RunFallbackAfterStartupTest(
  233. absl::GetFlag(FLAGS_blackhole_lb_and_backend_addrs_cmd));
  234. }
  235. } // namespace
  236. int main(int argc, char** argv) {
  237. grpc::testing::InitTest(&argc, &argv, true);
  238. gpr_log(GPR_INFO, "Testing: %s", absl::GetFlag(FLAGS_test_case).c_str());
  239. if (absl::GetFlag(FLAGS_test_case) == "fast_fallback_before_startup") {
  240. DoFastFallbackBeforeStartup();
  241. gpr_log(GPR_INFO, "DoFastFallbackBeforeStartup done!");
  242. } else if (absl::GetFlag(FLAGS_test_case) == "slow_fallback_before_startup") {
  243. DoSlowFallbackBeforeStartup();
  244. gpr_log(GPR_INFO, "DoSlowFallbackBeforeStartup done!");
  245. } else if (absl::GetFlag(FLAGS_test_case) == "fast_fallback_after_startup") {
  246. DoFastFallbackAfterStartup();
  247. gpr_log(GPR_INFO, "DoFastFallbackAfterStartup done!");
  248. } else if (absl::GetFlag(FLAGS_test_case) == "slow_fallback_after_startup") {
  249. DoSlowFallbackAfterStartup();
  250. gpr_log(GPR_INFO, "DoSlowFallbackAfterStartup done!");
  251. } else {
  252. gpr_log(GPR_ERROR, "Invalid test case: %s",
  253. absl::GetFlag(FLAGS_test_case).c_str());
  254. abort();
  255. }
  256. }
  257. #else
  258. int main(int argc, char** argv) {
  259. grpc::testing::InitTest(&argc, &argv, true);
  260. gpr_log(GPR_ERROR,
  261. "This test requires TCP_USER_TIMEOUT, which isn't available");
  262. abort();
  263. }
  264. #endif // SOCKET_SUPPORTS_TCP_USER_TIMEOUT