grpc 1.0.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (705) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3696 -867
  3. data/etc/roots.pem +39 -111
  4. data/include/grpc/byte_buffer.h +64 -1
  5. data/include/grpc/census.h +40 -96
  6. data/include/grpc/compression.h +2 -1
  7. data/include/grpc/grpc.h +42 -7
  8. data/include/grpc/grpc_posix.h +8 -5
  9. data/include/grpc/impl/codegen/atm.h +3 -0
  10. data/include/grpc/impl/codegen/atm_gcc_atomic.h +2 -0
  11. data/include/grpc/impl/codegen/atm_gcc_sync.h +8 -0
  12. data/include/grpc/impl/codegen/atm_windows.h +4 -0
  13. data/include/grpc/impl/codegen/byte_buffer_reader.h +4 -4
  14. data/include/grpc/impl/codegen/compression_types.h +1 -1
  15. data/include/grpc/impl/codegen/connectivity_state.h +2 -0
  16. data/include/grpc/impl/codegen/exec_ctx_fwd.h +41 -0
  17. data/include/grpc/impl/codegen/gpr_slice.h +84 -0
  18. data/include/grpc/impl/codegen/{alloc.h → gpr_types.h} +30 -29
  19. data/include/grpc/impl/codegen/grpc_types.h +91 -9
  20. data/include/grpc/impl/codegen/port_platform.h +25 -92
  21. data/include/grpc/impl/codegen/slice.h +54 -97
  22. data/include/grpc/impl/codegen/sync.h +0 -253
  23. data/include/grpc/module.modulemap +0 -2
  24. data/include/grpc/slice.h +132 -0
  25. data/include/grpc/{impl/codegen/slice_buffer.h → slice_buffer.h} +22 -39
  26. data/include/grpc/support/alloc.h +40 -1
  27. data/include/grpc/support/log.h +80 -1
  28. data/include/grpc/support/log_windows.h +2 -0
  29. data/include/grpc/support/string_util.h +1 -1
  30. data/include/grpc/support/sync.h +252 -0
  31. data/include/grpc/support/time.h +67 -1
  32. data/src/boringssl/err_data.c +639 -627
  33. data/src/core/ext/census/base_resources.c +71 -0
  34. data/src/core/ext/census/base_resources.h +39 -0
  35. data/src/core/ext/census/gen/census.pb.c +26 -29
  36. data/src/core/ext/census/gen/census.pb.h +68 -67
  37. data/src/core/ext/census/gen/trace_context.pb.c +81 -0
  38. data/src/core/ext/census/gen/trace_context.pb.h +99 -0
  39. data/src/core/ext/census/grpc_filter.c +22 -16
  40. data/src/core/ext/census/grpc_plugin.c +2 -1
  41. data/src/core/ext/census/initialize.c +16 -4
  42. data/src/core/ext/census/mlog.h +1 -1
  43. data/src/core/ext/census/placeholders.c +0 -45
  44. data/src/core/ext/census/resource.c +312 -0
  45. data/src/core/ext/census/resource.h +63 -0
  46. data/src/core/ext/census/trace_context.c +86 -0
  47. data/src/core/ext/census/trace_context.h +68 -0
  48. data/src/core/ext/census/tracing.c +8 -2
  49. data/src/core/ext/{client_config → client_channel}/channel_connectivity.c +8 -4
  50. data/src/core/ext/client_channel/client_channel.c +1218 -0
  51. data/src/core/ext/{client_config → client_channel}/client_channel.h +8 -11
  52. data/src/core/ext/{client_config → client_channel}/client_channel_factory.c +33 -3
  53. data/src/core/ext/{client_config → client_channel}/client_channel_factory.h +15 -8
  54. data/src/core/ext/{client_config/client_config_plugin.c → client_channel/client_channel_plugin.c} +16 -15
  55. data/src/core/ext/{client_config → client_channel}/connector.c +1 -1
  56. data/src/core/ext/{client_config → client_channel}/connector.h +5 -8
  57. data/{include/grpc/support/slice_buffer.h → src/core/ext/client_channel/default_initial_connect_string.c} +4 -5
  58. data/src/core/ext/client_channel/http_connect_handshaker.c +399 -0
  59. data/src/core/ext/client_channel/http_connect_handshaker.h +52 -0
  60. data/src/core/ext/{client_config → client_channel}/initial_connect_string.c +6 -7
  61. data/src/core/ext/{client_config → client_channel}/initial_connect_string.h +10 -10
  62. data/src/core/ext/{client_config → client_channel}/lb_policy.c +11 -11
  63. data/src/core/ext/{client_config → client_channel}/lb_policy.h +68 -27
  64. data/src/core/ext/client_channel/lb_policy_factory.c +163 -0
  65. data/src/core/ext/{client_config → client_channel}/lb_policy_factory.h +64 -9
  66. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.c +6 -4
  67. data/src/core/ext/{client_config → client_channel}/lb_policy_registry.h +4 -4
  68. data/src/core/ext/{client_config → client_channel}/parse_address.c +21 -14
  69. data/src/core/ext/{client_config → client_channel}/parse_address.h +8 -10
  70. data/src/core/ext/{client_config → client_channel}/resolver.c +3 -4
  71. data/src/core/ext/{client_config → client_channel}/resolver.h +11 -15
  72. data/src/core/ext/{client_config → client_channel}/resolver_factory.c +4 -3
  73. data/src/core/ext/{client_config → client_channel}/resolver_factory.h +13 -11
  74. data/src/core/ext/{client_config → client_channel}/resolver_registry.c +54 -34
  75. data/src/core/ext/{client_config → client_channel}/resolver_registry.h +21 -8
  76. data/src/core/ext/{client_config → client_channel}/subchannel.c +208 -119
  77. data/src/core/ext/{client_config → client_channel}/subchannel.h +21 -11
  78. data/src/core/ext/{client_config → client_channel}/subchannel_index.c +6 -17
  79. data/src/core/ext/{client_config → client_channel}/subchannel_index.h +7 -7
  80. data/src/core/ext/{client_config → client_channel}/uri_parser.c +21 -28
  81. data/src/core/ext/{client_config → client_channel}/uri_parser.h +3 -3
  82. data/src/core/ext/lb_policy/grpclb/grpclb.c +1406 -0
  83. data/src/core/ext/lb_policy/grpclb/grpclb.h +44 -0
  84. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +117 -37
  85. data/src/core/ext/lb_policy/grpclb/load_balancer_api.h +31 -12
  86. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +6 -36
  87. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +22 -42
  88. data/src/core/ext/lb_policy/pick_first/pick_first.c +64 -46
  89. data/src/core/ext/lb_policy/round_robin/round_robin.c +324 -160
  90. data/src/core/ext/load_reporting/load_reporting.c +7 -56
  91. data/src/core/ext/load_reporting/load_reporting.h +41 -28
  92. data/src/core/ext/load_reporting/load_reporting_filter.c +132 -42
  93. data/src/core/ext/load_reporting/load_reporting_filter.h +1 -0
  94. data/src/core/ext/resolver/dns/native/dns_resolver.c +88 -80
  95. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +57 -102
  96. data/src/core/ext/transport/chttp2/alpn/alpn.c +1 -1
  97. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +253 -0
  98. data/src/core/{lib/iomgr/ev_poll_and_epoll_posix.h → ext/transport/chttp2/client/chttp2_connector.h} +5 -5
  99. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +31 -160
  100. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +5 -5
  101. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +44 -243
  102. data/src/core/ext/transport/chttp2/server/chttp2_server.c +342 -0
  103. data/src/core/ext/transport/chttp2/server/chttp2_server.h +47 -0
  104. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +11 -124
  105. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.c +20 -9
  106. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +28 -236
  107. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +31 -27
  108. data/src/core/ext/transport/chttp2/transport/bin_decoder.h +5 -4
  109. data/src/core/ext/transport/chttp2/transport/bin_encoder.c +25 -22
  110. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +8 -7
  111. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +0 -3
  112. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +1345 -1521
  113. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  114. data/src/core/ext/transport/chttp2/transport/frame.h +3 -5
  115. data/src/core/ext/transport/chttp2/transport/frame_data.c +50 -47
  116. data/src/core/ext/transport/chttp2/transport/frame_data.h +8 -9
  117. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +19 -21
  118. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +9 -8
  119. data/src/core/ext/transport/chttp2/transport/frame_ping.c +13 -12
  120. data/src/core/ext/transport/chttp2/transport/frame_ping.h +6 -6
  121. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +31 -19
  122. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +8 -7
  123. data/src/core/ext/transport/chttp2/transport/frame_settings.c +22 -25
  124. data/src/core/ext/transport/chttp2/transport/frame_settings.h +9 -8
  125. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +26 -18
  126. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +5 -6
  127. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +68 -58
  128. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +8 -5
  129. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +327 -214
  130. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +14 -9
  131. data/src/core/ext/transport/chttp2/transport/hpack_table.c +24 -19
  132. data/src/core/ext/transport/chttp2/transport/hpack_table.h +9 -6
  133. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +2 -2
  134. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +1 -1
  135. data/src/core/ext/transport/chttp2/transport/internal.h +284 -436
  136. data/src/core/ext/transport/chttp2/transport/parsing.c +355 -590
  137. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -309
  138. data/src/core/ext/transport/chttp2/transport/stream_map.c +13 -34
  139. data/src/core/ext/transport/chttp2/transport/stream_map.h +3 -4
  140. data/src/core/ext/transport/chttp2/transport/writing.c +174 -286
  141. data/src/core/lib/channel/channel_args.c +70 -13
  142. data/src/core/lib/channel/channel_args.h +28 -2
  143. data/src/core/lib/channel/channel_stack.c +77 -28
  144. data/src/core/lib/channel/channel_stack.h +61 -23
  145. data/src/core/lib/channel/channel_stack_builder.c +33 -25
  146. data/src/core/lib/channel/channel_stack_builder.h +17 -8
  147. data/src/core/lib/channel/compress_filter.c +52 -36
  148. data/src/core/lib/channel/connected_channel.c +20 -12
  149. data/src/core/lib/channel/connected_channel.h +2 -1
  150. data/src/core/lib/channel/context.h +13 -1
  151. data/src/core/lib/channel/deadline_filter.c +344 -0
  152. data/src/core/lib/channel/deadline_filter.h +99 -0
  153. data/src/core/lib/channel/handshaker.c +240 -0
  154. data/src/core/lib/channel/handshaker.h +164 -0
  155. data/src/core/lib/{security/credentials/google_default/credentials_windows.c → channel/handshaker_factory.c} +16 -23
  156. data/src/core/lib/channel/handshaker_factory.h +66 -0
  157. data/src/core/lib/channel/handshaker_registry.c +113 -0
  158. data/src/core/{ext/client_config/client_config.h → lib/channel/handshaker_registry.h} +26 -16
  159. data/src/core/lib/channel/http_client_filter.c +248 -46
  160. data/src/core/lib/channel/http_client_filter.h +3 -0
  161. data/src/core/lib/channel/http_server_filter.c +136 -24
  162. data/src/core/lib/channel/message_size_filter.c +261 -0
  163. data/src/core/lib/channel/message_size_filter.h +39 -0
  164. data/src/core/lib/compression/message_compress.c +43 -37
  165. data/src/core/lib/compression/message_compress.h +7 -5
  166. data/src/core/lib/http/format_request.c +26 -11
  167. data/src/core/lib/http/format_request.h +7 -5
  168. data/src/core/lib/http/httpcli.c +45 -27
  169. data/src/core/lib/http/httpcli.h +4 -4
  170. data/src/core/lib/http/httpcli_security_connector.c +56 -46
  171. data/src/core/lib/http/parser.c +17 -14
  172. data/src/core/lib/http/parser.h +4 -2
  173. data/src/core/lib/iomgr/closure.c +49 -7
  174. data/src/core/lib/iomgr/closure.h +56 -14
  175. data/src/core/lib/iomgr/combiner.c +422 -0
  176. data/src/core/lib/iomgr/combiner.h +64 -0
  177. data/src/core/lib/iomgr/endpoint.c +8 -2
  178. data/src/core/lib/iomgr/endpoint.h +17 -7
  179. data/src/core/lib/iomgr/endpoint_pair.h +3 -2
  180. data/src/core/lib/iomgr/endpoint_pair_posix.c +9 -8
  181. data/src/core/{ext/client_config/lb_policy_factory.c → lib/iomgr/endpoint_pair_uv.c} +18 -13
  182. data/src/core/lib/iomgr/endpoint_pair_windows.c +7 -6
  183. data/src/core/lib/iomgr/error.c +72 -6
  184. data/src/core/lib/iomgr/error.h +30 -3
  185. data/src/core/lib/iomgr/ev_epoll_linux.c +500 -382
  186. data/src/core/lib/iomgr/ev_epoll_linux.h +3 -2
  187. data/src/core/lib/iomgr/ev_poll_posix.c +317 -30
  188. data/src/core/lib/iomgr/ev_poll_posix.h +1 -0
  189. data/src/core/lib/iomgr/ev_posix.c +26 -5
  190. data/src/core/lib/iomgr/ev_posix.h +12 -1
  191. data/src/core/lib/iomgr/exec_ctx.c +27 -94
  192. data/src/core/lib/iomgr/exec_ctx.h +19 -22
  193. data/src/core/lib/iomgr/executor.c +29 -8
  194. data/src/core/lib/iomgr/executor.h +2 -4
  195. data/src/core/lib/iomgr/iocp_windows.c +3 -4
  196. data/src/core/lib/iomgr/iomgr.c +14 -10
  197. data/src/core/lib/iomgr/iomgr.h +6 -2
  198. data/src/core/lib/iomgr/iomgr_posix.c +2 -2
  199. data/src/core/lib/iomgr/iomgr_uv.c +49 -0
  200. data/src/core/lib/iomgr/iomgr_windows.c +2 -2
  201. data/src/core/lib/iomgr/load_file.c +3 -3
  202. data/src/core/lib/iomgr/load_file.h +2 -2
  203. data/src/core/lib/iomgr/network_status_tracker.c +1 -1
  204. data/src/core/lib/iomgr/pollset_set_uv.c +62 -0
  205. data/src/core/lib/iomgr/pollset_set_windows.c +3 -3
  206. data/src/core/lib/iomgr/pollset_uv.c +142 -0
  207. data/src/core/lib/iomgr/pollset_uv.h +42 -0
  208. data/src/core/lib/iomgr/pollset_windows.c +5 -6
  209. data/src/core/lib/iomgr/port.h +129 -0
  210. data/src/core/lib/iomgr/resolve_address.h +2 -1
  211. data/src/core/lib/iomgr/resolve_address_posix.c +14 -13
  212. data/src/core/lib/iomgr/resolve_address_uv.c +233 -0
  213. data/src/core/lib/iomgr/resolve_address_windows.c +14 -12
  214. data/src/core/lib/iomgr/resource_quota.c +832 -0
  215. data/src/core/lib/iomgr/resource_quota.h +159 -0
  216. data/src/core/lib/iomgr/sockaddr.h +10 -2
  217. data/src/core/lib/iomgr/sockaddr_utils.c +63 -36
  218. data/src/core/lib/iomgr/sockaddr_utils.h +14 -14
  219. data/src/core/lib/iomgr/socket_mutator.c +98 -0
  220. data/src/core/lib/iomgr/socket_mutator.h +80 -0
  221. data/src/core/lib/iomgr/socket_utils.h +42 -0
  222. data/src/core/lib/iomgr/socket_utils_common_posix.c +28 -13
  223. data/src/core/lib/iomgr/socket_utils_linux.c +11 -5
  224. data/src/core/lib/iomgr/socket_utils_posix.c +10 -7
  225. data/src/core/lib/iomgr/socket_utils_posix.h +11 -4
  226. data/src/core/lib/iomgr/socket_utils_uv.c +49 -0
  227. data/src/core/lib/iomgr/socket_utils_windows.c +52 -0
  228. data/src/core/lib/iomgr/socket_windows.c +14 -6
  229. data/src/core/lib/iomgr/socket_windows.h +1 -0
  230. data/src/core/lib/iomgr/tcp_client.h +8 -2
  231. data/src/core/lib/iomgr/tcp_client_posix.c +131 -82
  232. data/src/core/lib/iomgr/tcp_client_posix.h +45 -0
  233. data/src/core/lib/iomgr/tcp_client_uv.c +190 -0
  234. data/src/core/lib/iomgr/tcp_client_windows.c +54 -30
  235. data/src/core/lib/iomgr/tcp_posix.c +135 -56
  236. data/src/core/lib/iomgr/tcp_posix.h +2 -2
  237. data/src/core/lib/iomgr/tcp_server.h +14 -6
  238. data/src/core/lib/iomgr/tcp_server_posix.c +154 -118
  239. data/src/core/lib/iomgr/tcp_server_uv.c +388 -0
  240. data/src/core/lib/iomgr/tcp_server_windows.c +127 -100
  241. data/src/core/lib/iomgr/tcp_uv.c +367 -0
  242. data/src/core/lib/iomgr/tcp_uv.h +59 -0
  243. data/src/core/lib/iomgr/tcp_windows.c +65 -48
  244. data/src/core/lib/iomgr/tcp_windows.h +3 -1
  245. data/src/core/lib/iomgr/timer.h +21 -21
  246. data/src/core/lib/iomgr/{timer.c → timer_generic.c} +15 -10
  247. data/src/core/lib/iomgr/timer_generic.h +49 -0
  248. data/src/core/lib/iomgr/timer_heap.c +6 -0
  249. data/src/core/lib/iomgr/timer_uv.c +99 -0
  250. data/src/core/lib/iomgr/timer_uv.h +47 -0
  251. data/src/core/lib/iomgr/udp_server.c +116 -98
  252. data/src/core/lib/iomgr/udp_server.h +5 -3
  253. data/src/core/lib/iomgr/unix_sockets_posix.c +14 -6
  254. data/src/core/lib/iomgr/unix_sockets_posix.h +6 -5
  255. data/src/core/lib/iomgr/unix_sockets_posix_noop.c +4 -4
  256. data/src/core/lib/iomgr/wakeup_fd_cv.c +118 -0
  257. data/src/core/lib/iomgr/wakeup_fd_cv.h +80 -0
  258. data/src/core/lib/iomgr/wakeup_fd_eventfd.c +3 -3
  259. data/src/core/lib/iomgr/wakeup_fd_nospecial.c +3 -3
  260. data/src/core/lib/iomgr/wakeup_fd_pipe.c +12 -6
  261. data/src/core/lib/iomgr/wakeup_fd_posix.c +34 -5
  262. data/src/core/lib/iomgr/wakeup_fd_posix.h +5 -0
  263. data/src/core/lib/iomgr/workqueue.h +12 -20
  264. data/src/core/{ext/client_config/client_config.c → lib/iomgr/workqueue_uv.c} +24 -33
  265. data/{include/grpc/support/slice.h → src/core/lib/iomgr/workqueue_uv.h} +4 -6
  266. data/src/core/lib/iomgr/workqueue_windows.c +9 -8
  267. data/src/core/lib/json/json.c +3 -3
  268. data/src/core/lib/json/json.h +11 -11
  269. data/src/core/lib/json/json_reader.c +9 -5
  270. data/src/core/lib/profiling/basic_timers.c +10 -1
  271. data/src/core/lib/profiling/timers.h +2 -0
  272. data/src/core/lib/security/context/security_context.c +13 -3
  273. data/src/core/lib/security/context/security_context.h +20 -0
  274. data/src/core/lib/security/credentials/composite/composite_credentials.c +28 -14
  275. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -2
  276. data/src/core/lib/security/credentials/credentials.c +48 -19
  277. data/src/core/lib/security/credentials/credentials.h +36 -19
  278. data/src/core/lib/security/credentials/credentials_metadata.c +11 -8
  279. data/src/core/lib/security/credentials/fake/fake_credentials.c +15 -11
  280. data/src/core/lib/security/credentials/google_default/{credentials_posix.c → credentials_generic.c} +7 -14
  281. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +33 -21
  282. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +14 -0
  283. data/src/core/lib/security/credentials/iam/iam_credentials.c +3 -2
  284. data/src/core/lib/security/credentials/jwt/json_token.c +1 -0
  285. data/src/core/lib/security/credentials/jwt/json_token.h +1 -1
  286. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +54 -19
  287. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -1
  288. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +129 -79
  289. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +9 -6
  290. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +63 -28
  291. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  292. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +32 -11
  293. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +13 -9
  294. data/src/core/lib/security/transport/client_auth_filter.c +33 -27
  295. data/src/core/lib/security/transport/secure_endpoint.c +93 -68
  296. data/src/core/lib/security/transport/secure_endpoint.h +2 -2
  297. data/src/core/lib/security/transport/security_connector.c +133 -168
  298. data/src/core/lib/security/transport/security_connector.h +31 -46
  299. data/src/core/lib/security/transport/security_handshaker.c +501 -0
  300. data/src/core/lib/security/transport/{handshake.h → security_handshaker.h} +10 -10
  301. data/src/core/lib/security/transport/server_auth_filter.c +50 -38
  302. data/src/core/lib/security/util/b64.c +11 -8
  303. data/src/core/lib/security/util/b64.h +5 -4
  304. data/src/core/lib/slice/percent_encoding.c +182 -0
  305. data/src/core/lib/slice/percent_encoding.h +78 -0
  306. data/src/core/lib/{support → slice}/slice.c +81 -50
  307. data/src/core/lib/{support → slice}/slice_buffer.c +78 -60
  308. data/src/core/lib/slice/slice_internal.h +49 -0
  309. data/src/core/lib/slice/slice_string_helpers.c +90 -0
  310. data/src/core/lib/{iomgr/workqueue_posix.h → slice/slice_string_helpers.h} +18 -18
  311. data/src/core/lib/support/backoff.c +24 -13
  312. data/src/core/lib/support/backoff.h +5 -2
  313. data/src/core/lib/support/env.h +0 -2
  314. data/src/core/lib/support/log.c +5 -4
  315. data/src/core/lib/support/log_linux.c +0 -1
  316. data/src/core/lib/support/log_posix.c +1 -1
  317. data/src/core/lib/support/mpscq.c +83 -0
  318. data/src/core/lib/support/mpscq.h +65 -0
  319. data/src/core/lib/support/string.c +58 -49
  320. data/src/core/lib/support/string.h +11 -8
  321. data/src/core/lib/support/subprocess_posix.c +5 -2
  322. data/src/core/lib/support/thd.c +1 -1
  323. data/src/core/lib/support/time.c +43 -79
  324. data/src/core/lib/support/time_posix.c +1 -1
  325. data/src/core/lib/support/tmpfile.h +0 -2
  326. data/src/core/lib/surface/alarm.c +4 -1
  327. data/src/core/lib/surface/byte_buffer.c +17 -11
  328. data/src/core/lib/surface/byte_buffer_reader.c +23 -15
  329. data/src/core/lib/surface/call.c +294 -276
  330. data/src/core/lib/surface/call.h +24 -9
  331. data/src/core/lib/surface/call_log_batch.c +5 -3
  332. data/src/core/lib/surface/channel.c +127 -111
  333. data/src/core/lib/surface/channel.h +14 -5
  334. data/src/core/lib/surface/channel_init.c +1 -1
  335. data/src/core/lib/surface/channel_init.h +10 -1
  336. data/src/core/lib/surface/channel_ping.c +7 -6
  337. data/src/core/lib/surface/completion_queue.c +154 -18
  338. data/src/core/lib/surface/completion_queue.h +5 -0
  339. data/src/core/lib/surface/init.c +40 -6
  340. data/src/core/lib/surface/init.h +1 -0
  341. data/src/core/lib/surface/init_secure.c +5 -2
  342. data/src/core/lib/surface/lame_client.c +28 -18
  343. data/src/core/lib/surface/server.c +134 -87
  344. data/src/core/lib/surface/server.h +8 -0
  345. data/src/core/lib/surface/validate_metadata.c +1 -1
  346. data/src/core/lib/surface/version.c +3 -1
  347. data/src/core/lib/transport/byte_stream.c +7 -4
  348. data/src/core/lib/transport/byte_stream.h +6 -10
  349. data/src/core/lib/transport/connectivity_state.c +21 -12
  350. data/src/core/lib/transport/connectivity_state.h +4 -1
  351. data/src/core/lib/transport/mdstr_hash_table.c +118 -0
  352. data/src/core/lib/transport/mdstr_hash_table.h +77 -0
  353. data/src/core/lib/transport/metadata.c +83 -60
  354. data/src/core/lib/transport/metadata.h +41 -23
  355. data/src/core/lib/transport/metadata_batch.c +17 -11
  356. data/src/core/lib/transport/metadata_batch.h +20 -6
  357. data/src/core/lib/transport/pid_controller.c +57 -0
  358. data/src/core/lib/transport/pid_controller.h +64 -0
  359. data/src/core/lib/transport/service_config.c +251 -0
  360. data/src/core/lib/transport/service_config.h +71 -0
  361. data/src/core/lib/transport/static_metadata.c +18 -16
  362. data/src/core/lib/transport/static_metadata.h +113 -107
  363. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.c +3 -3
  364. data/src/core/{ext/transport/chttp2 → lib}/transport/timeout_encoding.h +7 -7
  365. data/src/core/lib/transport/transport.c +84 -23
  366. data/src/core/lib/transport/transport.h +53 -8
  367. data/src/core/lib/transport/transport_impl.h +3 -0
  368. data/src/core/lib/transport/transport_op_string.c +92 -20
  369. data/src/core/lib/tsi/ssl_transport_security.c +3 -1
  370. data/src/core/plugin_registry/grpc_plugin_registry.c +8 -4
  371. data/src/ruby/ext/grpc/extconf.rb +0 -1
  372. data/src/ruby/ext/grpc/rb_byte_buffer.c +8 -7
  373. data/src/ruby/ext/grpc/rb_call.c +15 -5
  374. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  375. data/src/ruby/ext/grpc/rb_compression_options.c +466 -0
  376. data/src/{core/ext/client_config/default_initial_connect_string.c → ruby/ext/grpc/rb_compression_options.h} +10 -5
  377. data/src/ruby/ext/grpc/rb_grpc.c +3 -1
  378. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +198 -190
  379. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +306 -294
  380. data/src/ruby/ext/grpc/rb_server.c +18 -12
  381. data/src/ruby/lib/grpc/errors.rb +154 -2
  382. data/src/ruby/lib/grpc/generic/active_call.rb +144 -63
  383. data/src/ruby/lib/grpc/generic/bidi_call.rb +18 -2
  384. data/src/ruby/lib/grpc/generic/client_stub.rb +7 -5
  385. data/src/ruby/lib/grpc/generic/rpc_desc.rb +39 -13
  386. data/src/ruby/lib/grpc/generic/rpc_server.rb +51 -24
  387. data/src/ruby/lib/grpc/generic/service.rb +3 -2
  388. data/src/ruby/lib/grpc/version.rb +1 -1
  389. data/src/ruby/pb/grpc/health/checker.rb +3 -1
  390. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +7 -0
  391. data/src/ruby/pb/test/client.rb +307 -7
  392. data/src/ruby/pb/test/server.rb +26 -1
  393. data/src/ruby/spec/compression_options_spec.rb +164 -0
  394. data/src/ruby/spec/error_sanity_spec.rb +64 -0
  395. data/src/ruby/spec/generic/active_call_spec.rb +290 -12
  396. data/src/ruby/spec/generic/client_stub_spec.rb +91 -41
  397. data/src/ruby/spec/generic/rpc_desc_spec.rb +36 -16
  398. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +22 -28
  399. data/src/ruby/spec/generic/rpc_server_spec.rb +6 -6
  400. data/src/ruby/spec/pb/health/checker_spec.rb +27 -19
  401. data/src/ruby/spec/spec_helper.rb +2 -0
  402. data/third_party/boringssl/crypto/aes/aes.c +12 -12
  403. data/third_party/boringssl/crypto/aes/mode_wrappers.c +6 -2
  404. data/third_party/boringssl/crypto/asn1/a_d2i_fp.c +28 -13
  405. data/third_party/boringssl/crypto/asn1/a_gentm.c +2 -0
  406. data/third_party/boringssl/crypto/asn1/a_object.c +7 -3
  407. data/third_party/boringssl/crypto/asn1/a_strnid.c +1 -0
  408. data/third_party/boringssl/crypto/asn1/a_time.c +0 -11
  409. data/third_party/boringssl/crypto/asn1/a_type.c +0 -2
  410. data/third_party/boringssl/crypto/asn1/a_utctm.c +1 -30
  411. data/third_party/boringssl/crypto/asn1/asn1_lib.c +56 -76
  412. data/third_party/boringssl/crypto/asn1/asn1_locl.h +0 -10
  413. data/third_party/boringssl/crypto/asn1/asn1_par.c +0 -322
  414. data/third_party/boringssl/crypto/asn1/f_enum.c +1 -108
  415. data/third_party/boringssl/crypto/asn1/f_int.c +1 -106
  416. data/third_party/boringssl/crypto/asn1/f_string.c +1 -106
  417. data/third_party/boringssl/crypto/asn1/tasn_dec.c +10 -14
  418. data/third_party/boringssl/crypto/asn1/tasn_enc.c +17 -11
  419. data/third_party/boringssl/crypto/asn1/tasn_typ.c +29 -42
  420. data/third_party/boringssl/crypto/asn1/tasn_utl.c +1 -1
  421. data/third_party/boringssl/crypto/base64/base64.c +249 -285
  422. data/third_party/boringssl/crypto/bio/bio.c +13 -23
  423. data/third_party/boringssl/crypto/bio/bio_mem.c +3 -2
  424. data/third_party/boringssl/crypto/bio/connect.c +12 -3
  425. data/third_party/boringssl/crypto/bio/fd.c +22 -15
  426. data/third_party/boringssl/crypto/bio/file.c +2 -38
  427. data/third_party/boringssl/crypto/bio/hexdump.c +1 -2
  428. data/third_party/boringssl/crypto/bio/internal.h +3 -0
  429. data/third_party/boringssl/crypto/bio/pair.c +1 -1
  430. data/third_party/boringssl/crypto/bio/socket.c +10 -2
  431. data/third_party/boringssl/crypto/bio/socket_helper.c +2 -2
  432. data/third_party/boringssl/crypto/bn/asm/x86_64-gcc.c +0 -8
  433. data/third_party/boringssl/crypto/bn/bn.c +38 -0
  434. data/third_party/boringssl/crypto/bn/cmp.c +25 -0
  435. data/third_party/boringssl/crypto/bn/convert.c +73 -76
  436. data/third_party/boringssl/crypto/bn/div.c +136 -70
  437. data/third_party/boringssl/crypto/bn/exponentiation.c +86 -381
  438. data/third_party/boringssl/crypto/bn/gcd.c +213 -296
  439. data/third_party/boringssl/crypto/bn/generic.c +0 -80
  440. data/third_party/boringssl/crypto/bn/internal.h +15 -3
  441. data/third_party/boringssl/crypto/bn/montgomery.c +57 -207
  442. data/third_party/boringssl/crypto/bn/montgomery_inv.c +160 -0
  443. data/third_party/boringssl/crypto/bn/mul.c +2 -1
  444. data/third_party/boringssl/crypto/bn/prime.c +24 -8
  445. data/third_party/boringssl/crypto/bn/random.c +47 -33
  446. data/third_party/boringssl/crypto/bn/sqrt.c +4 -5
  447. data/third_party/boringssl/crypto/buf/buf.c +25 -21
  448. data/third_party/boringssl/crypto/bytestring/ber.c +1 -0
  449. data/third_party/boringssl/crypto/bytestring/cbb.c +50 -22
  450. data/third_party/boringssl/crypto/bytestring/cbs.c +28 -4
  451. data/third_party/boringssl/crypto/chacha/{chacha_generic.c → chacha.c} +56 -29
  452. data/third_party/boringssl/crypto/cipher/aead.c +11 -22
  453. data/third_party/boringssl/crypto/cipher/cipher.c +2 -2
  454. data/third_party/boringssl/crypto/cipher/e_aes.c +53 -103
  455. data/third_party/boringssl/crypto/cipher/e_chacha20poly1305.c +2 -8
  456. data/third_party/boringssl/crypto/cipher/e_des.c +3 -5
  457. data/third_party/boringssl/crypto/cipher/e_null.c +1 -1
  458. data/third_party/boringssl/crypto/cipher/e_rc2.c +1 -1
  459. data/third_party/boringssl/crypto/cipher/e_rc4.c +1 -1
  460. data/third_party/boringssl/crypto/cipher/e_ssl3.c +3 -63
  461. data/third_party/boringssl/crypto/cipher/e_tls.c +12 -83
  462. data/third_party/boringssl/crypto/cipher/internal.h +8 -10
  463. data/third_party/boringssl/crypto/cipher/tls_cbc.c +69 -40
  464. data/third_party/boringssl/crypto/conf/conf.c +2 -1
  465. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +61 -0
  466. data/third_party/boringssl/crypto/cpu-arm-linux.c +360 -0
  467. data/third_party/boringssl/crypto/cpu-arm.c +0 -161
  468. data/third_party/boringssl/crypto/cpu-intel.c +5 -3
  469. data/third_party/boringssl/{ssl/test/scoped_types.h → crypto/cpu-ppc64le.c} +21 -9
  470. data/third_party/boringssl/crypto/crypto.c +29 -7
  471. data/third_party/boringssl/crypto/curve25519/curve25519.c +284 -242
  472. data/third_party/boringssl/crypto/curve25519/internal.h +64 -0
  473. data/third_party/boringssl/crypto/curve25519/spake25519.c +464 -0
  474. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +21 -0
  475. data/third_party/boringssl/crypto/dh/check.c +22 -6
  476. data/third_party/boringssl/crypto/dh/dh.c +45 -21
  477. data/third_party/boringssl/crypto/dh/dh_asn1.c +96 -20
  478. data/third_party/boringssl/crypto/dh/params.c +30 -78
  479. data/third_party/boringssl/crypto/digest/digest.c +3 -3
  480. data/third_party/boringssl/crypto/dsa/dsa.c +59 -29
  481. data/third_party/boringssl/crypto/dsa/dsa_asn1.c +4 -0
  482. data/third_party/boringssl/crypto/ec/ec.c +84 -140
  483. data/third_party/boringssl/crypto/ec/ec_asn1.c +82 -52
  484. data/third_party/boringssl/crypto/ec/ec_key.c +15 -15
  485. data/third_party/boringssl/crypto/ec/ec_montgomery.c +87 -50
  486. data/third_party/boringssl/crypto/ec/internal.h +12 -36
  487. data/third_party/boringssl/crypto/ec/oct.c +11 -11
  488. data/third_party/boringssl/crypto/ec/p224-64.c +59 -116
  489. data/third_party/boringssl/crypto/ec/p256-64.c +88 -163
  490. data/third_party/boringssl/crypto/ec/p256-x86_64.c +46 -58
  491. data/third_party/boringssl/crypto/ec/simple.c +81 -201
  492. data/third_party/boringssl/crypto/ec/util-64.c +0 -74
  493. data/third_party/boringssl/crypto/ecdh/ecdh.c +7 -1
  494. data/third_party/boringssl/crypto/ecdsa/ecdsa.c +28 -46
  495. data/third_party/boringssl/crypto/ecdsa/ecdsa_asn1.c +1 -0
  496. data/third_party/boringssl/crypto/engine/engine.c +1 -1
  497. data/third_party/boringssl/crypto/err/err.c +3 -3
  498. data/third_party/boringssl/crypto/evp/evp.c +14 -59
  499. data/third_party/boringssl/crypto/evp/evp_asn1.c +144 -87
  500. data/third_party/boringssl/crypto/evp/evp_ctx.c +7 -7
  501. data/third_party/boringssl/crypto/evp/internal.h +4 -46
  502. data/third_party/boringssl/crypto/evp/p_dsa_asn1.c +8 -157
  503. data/third_party/boringssl/crypto/evp/p_ec.c +1 -1
  504. data/third_party/boringssl/crypto/evp/p_ec_asn1.c +22 -170
  505. data/third_party/boringssl/crypto/evp/p_rsa.c +1 -1
  506. data/third_party/boringssl/crypto/evp/p_rsa_asn1.c +10 -548
  507. data/third_party/boringssl/crypto/evp/print.c +520 -0
  508. data/third_party/boringssl/crypto/ex_data.c +4 -6
  509. data/third_party/boringssl/crypto/hkdf/hkdf.c +38 -17
  510. data/third_party/boringssl/crypto/hmac/hmac.c +6 -6
  511. data/third_party/boringssl/crypto/internal.h +57 -77
  512. data/third_party/boringssl/crypto/lhash/lhash.c +6 -10
  513. data/third_party/boringssl/crypto/md4/md4.c +9 -0
  514. data/third_party/boringssl/crypto/mem.c +19 -19
  515. data/third_party/boringssl/crypto/modes/cfb.c +5 -6
  516. data/third_party/boringssl/crypto/modes/ctr.c +10 -18
  517. data/third_party/boringssl/crypto/modes/gcm.c +100 -66
  518. data/third_party/boringssl/crypto/modes/internal.h +15 -27
  519. data/third_party/boringssl/crypto/modes/ofb.c +9 -22
  520. data/third_party/boringssl/crypto/newhope/error_correction.c +131 -0
  521. data/third_party/boringssl/crypto/newhope/internal.h +71 -0
  522. data/third_party/boringssl/crypto/newhope/newhope.c +174 -0
  523. data/third_party/boringssl/crypto/newhope/ntt.c +148 -0
  524. data/third_party/boringssl/crypto/newhope/poly.c +183 -0
  525. data/third_party/boringssl/crypto/newhope/precomp.c +306 -0
  526. data/third_party/boringssl/crypto/newhope/reduce.c +42 -0
  527. data/third_party/boringssl/crypto/obj/obj.c +111 -135
  528. data/third_party/boringssl/crypto/obj/obj_dat.h +4 -10
  529. data/third_party/boringssl/crypto/pem/pem_lib.c +6 -43
  530. data/third_party/boringssl/crypto/pem/pem_pkey.c +10 -19
  531. data/third_party/boringssl/crypto/pkcs8/p5_pbe.c +1 -0
  532. data/third_party/boringssl/crypto/pkcs8/p5_pbev2.c +2 -1
  533. data/third_party/boringssl/crypto/pkcs8/p8_pkey.c +2 -2
  534. data/third_party/boringssl/crypto/pkcs8/pkcs8.c +95 -87
  535. data/third_party/boringssl/crypto/{test/test_util.h → poly1305/internal.h} +15 -10
  536. data/third_party/boringssl/crypto/poly1305/poly1305.c +8 -15
  537. data/third_party/boringssl/crypto/poly1305/poly1305_arm.c +1 -0
  538. data/third_party/boringssl/crypto/poly1305/poly1305_vec.c +3 -3
  539. data/third_party/boringssl/crypto/rand/deterministic.c +47 -0
  540. data/third_party/boringssl/crypto/rand/rand.c +4 -1
  541. data/third_party/boringssl/crypto/rand/urandom.c +5 -7
  542. data/third_party/boringssl/crypto/rand/windows.c +5 -8
  543. data/third_party/boringssl/crypto/rc4/rc4.c +24 -209
  544. data/third_party/boringssl/crypto/refcount_lock.c +2 -2
  545. data/third_party/boringssl/crypto/rsa/blinding.c +74 -232
  546. data/third_party/boringssl/crypto/rsa/internal.h +5 -13
  547. data/third_party/boringssl/crypto/rsa/padding.c +64 -63
  548. data/third_party/boringssl/crypto/rsa/rsa.c +50 -28
  549. data/third_party/boringssl/crypto/rsa/rsa_asn1.c +8 -16
  550. data/third_party/boringssl/crypto/rsa/rsa_impl.c +134 -122
  551. data/third_party/boringssl/crypto/sha/sha256.c +2 -2
  552. data/third_party/boringssl/crypto/sha/sha512.c +7 -7
  553. data/third_party/boringssl/crypto/stack/stack.c +13 -22
  554. data/third_party/boringssl/crypto/thread.c +21 -12
  555. data/third_party/boringssl/crypto/thread_none.c +6 -2
  556. data/third_party/boringssl/crypto/thread_pthread.c +16 -7
  557. data/third_party/boringssl/crypto/thread_win.c +38 -85
  558. data/third_party/boringssl/crypto/x509/a_sign.c +3 -3
  559. data/third_party/boringssl/crypto/x509/a_strex.c +1 -1
  560. data/third_party/boringssl/crypto/x509/a_verify.c +2 -2
  561. data/third_party/boringssl/crypto/{evp → x509}/algorithm.c +37 -53
  562. data/third_party/boringssl/crypto/x509/asn1_gen.c +1 -2
  563. data/third_party/boringssl/crypto/x509/by_dir.c +6 -6
  564. data/third_party/boringssl/crypto/x509/internal.h +66 -0
  565. data/third_party/boringssl/crypto/x509/rsa_pss.c +385 -0
  566. data/third_party/boringssl/crypto/x509/t_x509.c +10 -12
  567. data/third_party/boringssl/crypto/x509/x509.c +5 -0
  568. data/third_party/boringssl/crypto/x509/x509_att.c +9 -3
  569. data/third_party/boringssl/crypto/x509/x509_lu.c +34 -44
  570. data/third_party/boringssl/crypto/x509/x509_obj.c +19 -2
  571. data/third_party/boringssl/crypto/x509/x509_r2x.c +9 -5
  572. data/third_party/boringssl/crypto/x509/x509_set.c +5 -0
  573. data/third_party/boringssl/crypto/x509/x509_txt.c +5 -0
  574. data/third_party/boringssl/crypto/x509/x509_vfy.c +63 -32
  575. data/third_party/boringssl/crypto/x509/x509_vpm.c +29 -18
  576. data/third_party/boringssl/crypto/x509/x509cset.c +2 -1
  577. data/third_party/boringssl/crypto/x509/x_crl.c +2 -2
  578. data/third_party/boringssl/crypto/x509/x_name.c +14 -17
  579. data/third_party/boringssl/crypto/x509/x_pubkey.c +10 -7
  580. data/third_party/boringssl/crypto/x509/x_x509.c +67 -6
  581. data/third_party/boringssl/crypto/x509v3/pcy_cache.c +2 -2
  582. data/third_party/boringssl/crypto/x509v3/pcy_tree.c +2 -1
  583. data/third_party/boringssl/crypto/x509v3/v3_conf.c +4 -3
  584. data/third_party/boringssl/crypto/x509v3/v3_cpols.c +5 -0
  585. data/third_party/boringssl/crypto/x509v3/v3_prn.c +0 -3
  586. data/third_party/boringssl/crypto/x509v3/v3_purp.c +2 -2
  587. data/third_party/boringssl/crypto/x509v3/v3_utl.c +2 -1
  588. data/third_party/boringssl/include/openssl/aead.h +72 -73
  589. data/third_party/boringssl/include/openssl/arm_arch.h +0 -6
  590. data/third_party/boringssl/include/openssl/asn1.h +103 -235
  591. data/third_party/boringssl/include/openssl/asn1_mac.h +17 -74
  592. data/third_party/boringssl/include/openssl/asn1t.h +1 -11
  593. data/third_party/boringssl/include/openssl/base.h +145 -3
  594. data/third_party/boringssl/include/openssl/base64.h +20 -17
  595. data/third_party/boringssl/include/openssl/bio.h +59 -34
  596. data/third_party/boringssl/include/openssl/bn.h +118 -51
  597. data/third_party/boringssl/include/openssl/buf.h +15 -0
  598. data/third_party/boringssl/include/openssl/bytestring.h +52 -4
  599. data/third_party/boringssl/include/openssl/chacha.h +2 -2
  600. data/third_party/boringssl/include/openssl/cipher.h +18 -1
  601. data/third_party/boringssl/include/openssl/cmac.h +11 -0
  602. data/third_party/boringssl/include/openssl/conf.h +13 -2
  603. data/third_party/boringssl/include/openssl/cpu.h +20 -23
  604. data/third_party/boringssl/include/openssl/crypto.h +22 -1
  605. data/third_party/boringssl/include/openssl/curve25519.h +96 -4
  606. data/third_party/boringssl/include/openssl/dh.h +71 -16
  607. data/third_party/boringssl/include/openssl/digest.h +38 -11
  608. data/third_party/boringssl/include/openssl/dsa.h +40 -4
  609. data/third_party/boringssl/include/openssl/ec.h +44 -18
  610. data/third_party/boringssl/include/openssl/ec_key.h +27 -6
  611. data/third_party/boringssl/include/openssl/ecdsa.h +11 -0
  612. data/third_party/boringssl/include/openssl/engine.h +11 -0
  613. data/third_party/boringssl/include/openssl/evp.h +52 -88
  614. data/third_party/boringssl/include/openssl/hkdf.h +24 -4
  615. data/third_party/boringssl/include/openssl/hmac.h +20 -6
  616. data/third_party/boringssl/include/openssl/md4.h +4 -0
  617. data/third_party/boringssl/include/openssl/mem.h +19 -0
  618. data/third_party/boringssl/include/openssl/newhope.h +158 -0
  619. data/third_party/boringssl/include/openssl/nid.h +4166 -0
  620. data/third_party/boringssl/include/openssl/obj.h +31 -3
  621. data/third_party/boringssl/include/openssl/obj_mac.h +17 -4143
  622. data/third_party/boringssl/include/openssl/{opensslfeatures.h → opensslconf.h} +3 -3
  623. data/third_party/boringssl/include/openssl/pem.h +5 -0
  624. data/third_party/boringssl/include/openssl/pkcs8.h +12 -0
  625. data/third_party/boringssl/include/openssl/rand.h +6 -0
  626. data/third_party/boringssl/include/openssl/rc4.h +6 -0
  627. data/third_party/boringssl/{crypto/dh/internal.h → include/openssl/ripemd.h} +38 -11
  628. data/third_party/boringssl/include/openssl/rsa.h +127 -65
  629. data/third_party/boringssl/include/openssl/sha.h +14 -10
  630. data/third_party/boringssl/include/openssl/ssl.h +561 -275
  631. data/third_party/boringssl/include/openssl/ssl3.h +18 -25
  632. data/third_party/boringssl/include/openssl/stack.h +2 -4
  633. data/third_party/boringssl/include/openssl/stack_macros.h +321 -353
  634. data/third_party/boringssl/include/openssl/thread.h +31 -13
  635. data/third_party/boringssl/include/openssl/time_support.h +1 -0
  636. data/third_party/boringssl/include/openssl/tls1.h +37 -33
  637. data/third_party/boringssl/include/openssl/x509.h +69 -26
  638. data/third_party/boringssl/include/openssl/x509_vfy.h +12 -10
  639. data/third_party/boringssl/include/openssl/x509v3.h +23 -2
  640. data/third_party/boringssl/ssl/custom_extensions.c +3 -5
  641. data/third_party/boringssl/ssl/d1_both.c +463 -499
  642. data/third_party/boringssl/ssl/d1_lib.c +38 -109
  643. data/third_party/boringssl/ssl/d1_pkt.c +173 -334
  644. data/third_party/boringssl/ssl/d1_srtp.c +20 -18
  645. data/third_party/boringssl/ssl/{d1_meth.c → dtls_method.c} +88 -15
  646. data/third_party/boringssl/ssl/dtls_record.c +27 -26
  647. data/third_party/boringssl/ssl/{s3_clnt.c → handshake_client.c} +816 -904
  648. data/third_party/boringssl/ssl/handshake_server.c +1932 -0
  649. data/third_party/boringssl/ssl/internal.h +712 -439
  650. data/third_party/boringssl/ssl/s3_both.c +445 -257
  651. data/third_party/boringssl/ssl/s3_enc.c +53 -36
  652. data/third_party/boringssl/ssl/s3_lib.c +23 -268
  653. data/third_party/boringssl/ssl/s3_pkt.c +168 -364
  654. data/third_party/boringssl/ssl/ssl_aead_ctx.c +46 -17
  655. data/third_party/boringssl/ssl/ssl_asn1.c +56 -26
  656. data/third_party/boringssl/ssl/ssl_buffer.c +16 -24
  657. data/third_party/boringssl/ssl/ssl_cert.c +324 -49
  658. data/third_party/boringssl/ssl/ssl_cipher.c +205 -150
  659. data/third_party/boringssl/ssl/ssl_ecdh.c +287 -51
  660. data/third_party/boringssl/ssl/ssl_file.c +21 -68
  661. data/third_party/boringssl/ssl/ssl_lib.c +881 -510
  662. data/third_party/boringssl/ssl/ssl_rsa.c +404 -34
  663. data/third_party/boringssl/ssl/ssl_session.c +324 -103
  664. data/third_party/boringssl/ssl/ssl_stat.c +6 -88
  665. data/third_party/boringssl/ssl/t1_enc.c +23 -39
  666. data/third_party/boringssl/ssl/t1_lib.c +1120 -622
  667. data/third_party/boringssl/ssl/tls13_both.c +440 -0
  668. data/third_party/boringssl/ssl/tls13_client.c +682 -0
  669. data/third_party/boringssl/ssl/tls13_enc.c +391 -0
  670. data/third_party/boringssl/ssl/tls13_server.c +672 -0
  671. data/third_party/boringssl/ssl/{s3_meth.c → tls_method.c} +100 -21
  672. data/third_party/boringssl/ssl/tls_record.c +159 -77
  673. data/third_party/nanopb/pb.h +60 -28
  674. data/third_party/nanopb/pb_decode.c +120 -92
  675. data/third_party/nanopb/pb_decode.h +3 -3
  676. data/third_party/nanopb/pb_encode.c +73 -67
  677. data/third_party/nanopb/pb_encode.h +4 -4
  678. metadata +155 -89
  679. data/include/grpc/impl/codegen/byte_buffer.h +0 -122
  680. data/include/grpc/impl/codegen/log.h +0 -118
  681. data/include/grpc/impl/codegen/time.h +0 -130
  682. data/src/core/ext/client_config/client_channel.c +0 -593
  683. data/src/core/ext/client_config/subchannel_call_holder.c +0 -272
  684. data/src/core/ext/client_config/subchannel_call_holder.h +0 -99
  685. data/src/core/lib/iomgr/ev_poll_and_epoll_posix.c +0 -2046
  686. data/src/core/lib/iomgr/workqueue_posix.c +0 -151
  687. data/src/core/lib/security/transport/handshake.c +0 -368
  688. data/third_party/boringssl/crypto/asn1/a_bytes.c +0 -308
  689. data/third_party/boringssl/crypto/asn1/bio_asn1.c +0 -477
  690. data/third_party/boringssl/crypto/asn1/bio_ndef.c +0 -251
  691. data/third_party/boringssl/crypto/asn1/t_pkey.c +0 -110
  692. data/third_party/boringssl/crypto/asn1/tasn_prn.c +0 -596
  693. data/third_party/boringssl/crypto/chacha/chacha_vec.c +0 -328
  694. data/third_party/boringssl/crypto/directory.h +0 -66
  695. data/third_party/boringssl/crypto/directory_posix.c +0 -108
  696. data/third_party/boringssl/crypto/directory_win.c +0 -144
  697. data/third_party/boringssl/crypto/test/scoped_types.h +0 -140
  698. data/third_party/boringssl/include/openssl/pqueue.h +0 -146
  699. data/third_party/boringssl/ssl/d1_clnt.c +0 -561
  700. data/third_party/boringssl/ssl/d1_srvr.c +0 -476
  701. data/third_party/boringssl/ssl/pqueue/pqueue.c +0 -197
  702. data/third_party/boringssl/ssl/s3_srvr.c +0 -2272
  703. data/third_party/boringssl/ssl/test/async_bio.h +0 -45
  704. data/third_party/boringssl/ssl/test/packeted_bio.h +0 -44
  705. data/third_party/boringssl/ssl/test/test_config.h +0 -110
@@ -31,13 +31,17 @@
31
31
  *
32
32
  */
33
33
 
34
- #ifndef GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H
35
- #define GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H
34
+ #ifndef GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_H
35
+ #define GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_H
36
36
 
37
- #include "src/core/ext/client_config/connector.h"
37
+ #include "src/core/ext/client_channel/connector.h"
38
38
  #include "src/core/lib/channel/channel_stack.h"
39
39
  #include "src/core/lib/iomgr/polling_entity.h"
40
40
  #include "src/core/lib/transport/connectivity_state.h"
41
+ #include "src/core/lib/transport/metadata.h"
42
+
43
+ // Channel arg containing a grpc_resolved_address to connect to.
44
+ #define GRPC_ARG_SUBCHANNEL_ADDRESS "grpc.subchannel_address"
41
45
 
42
46
  /** A (sub-)channel that knows how to connect to exactly one target
43
47
  address. Provides a target for load balancing. */
@@ -96,7 +100,7 @@ grpc_subchannel *grpc_subchannel_weak_ref(
96
100
  void grpc_subchannel_weak_unref(grpc_exec_ctx *exec_ctx,
97
101
  grpc_subchannel *channel
98
102
  GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
99
- void grpc_connected_subchannel_ref(
103
+ grpc_connected_subchannel *grpc_connected_subchannel_ref(
100
104
  grpc_connected_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
101
105
  void grpc_connected_subchannel_unref(grpc_exec_ctx *exec_ctx,
102
106
  grpc_connected_subchannel *channel
@@ -108,9 +112,10 @@ void grpc_subchannel_call_unref(grpc_exec_ctx *exec_ctx,
108
112
  GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
109
113
 
110
114
  /** construct a subchannel call */
111
- grpc_subchannel_call *grpc_connected_subchannel_create_call(
115
+ grpc_error *grpc_connected_subchannel_create_call(
112
116
  grpc_exec_ctx *exec_ctx, grpc_connected_subchannel *connected_subchannel,
113
- grpc_polling_entity *pollent);
117
+ grpc_polling_entity *pollent, grpc_mdstr *path, gpr_timespec start_time,
118
+ gpr_timespec deadline, grpc_subchannel_call **subchannel_call);
114
119
 
115
120
  /** process a transport level op */
116
121
  void grpc_connected_subchannel_process_transport_op(
@@ -162,14 +167,19 @@ struct grpc_subchannel_args {
162
167
  size_t filter_count;
163
168
  /** Channel arguments to be supplied to the newly created channel */
164
169
  const grpc_channel_args *args;
165
- /** Address to connect to */
166
- struct sockaddr *addr;
167
- size_t addr_len;
168
170
  };
169
171
 
170
172
  /** create a subchannel given a connector */
171
173
  grpc_subchannel *grpc_subchannel_create(grpc_exec_ctx *exec_ctx,
172
174
  grpc_connector *connector,
173
- grpc_subchannel_args *args);
175
+ const grpc_subchannel_args *args);
176
+
177
+ /// Sets \a addr from \a args.
178
+ void grpc_get_subchannel_address_arg(const grpc_channel_args *args,
179
+ grpc_resolved_address *addr);
180
+
181
+ /// Returns a new channel arg encoding the subchannel address as a string.
182
+ /// Caller is responsible for freeing the string.
183
+ grpc_arg grpc_create_subchannel_address_arg(const grpc_resolved_address *addr);
174
184
 
175
- #endif /* GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_H */
185
+ #endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_H */
@@ -31,13 +31,14 @@
31
31
  //
32
32
  //
33
33
 
34
- #include "src/core/ext/client_config/subchannel_index.h"
34
+ #include "src/core/ext/client_channel/subchannel_index.h"
35
35
 
36
36
  #include <stdbool.h>
37
37
  #include <string.h>
38
38
 
39
39
  #include <grpc/support/alloc.h>
40
40
  #include <grpc/support/avl.h>
41
+ #include <grpc/support/string_util.h>
41
42
  #include <grpc/support/tls.h>
42
43
 
43
44
  #include "src/core/lib/channel/channel_args.h"
@@ -72,7 +73,7 @@ static grpc_exec_ctx *current_ctx() {
72
73
  }
73
74
 
74
75
  static grpc_subchannel_key *create_key(
75
- grpc_connector *connector, grpc_subchannel_args *args,
76
+ grpc_connector *connector, const grpc_subchannel_args *args,
76
77
  grpc_channel_args *(*copy_channel_args)(const grpc_channel_args *args)) {
77
78
  grpc_subchannel_key *k = gpr_malloc(sizeof(*k));
78
79
  k->connector = grpc_connector_ref(connector);
@@ -85,17 +86,12 @@ static grpc_subchannel_key *create_key(
85
86
  } else {
86
87
  k->args.filters = NULL;
87
88
  }
88
- k->args.addr_len = args->addr_len;
89
- k->args.addr = gpr_malloc(args->addr_len);
90
- if (k->args.addr_len > 0) {
91
- memcpy(k->args.addr, args->addr, k->args.addr_len);
92
- }
93
89
  k->args.args = copy_channel_args(args->args);
94
90
  return k;
95
91
  }
96
92
 
97
- grpc_subchannel_key *grpc_subchannel_key_create(grpc_connector *connector,
98
- grpc_subchannel_args *args) {
93
+ grpc_subchannel_key *grpc_subchannel_key_create(
94
+ grpc_connector *connector, const grpc_subchannel_args *args) {
99
95
  return create_key(connector, args, grpc_channel_args_normalize);
100
96
  }
101
97
 
@@ -107,14 +103,8 @@ static int subchannel_key_compare(grpc_subchannel_key *a,
107
103
  grpc_subchannel_key *b) {
108
104
  int c = GPR_ICMP(a->connector, b->connector);
109
105
  if (c != 0) return c;
110
- c = GPR_ICMP(a->args.addr_len, b->args.addr_len);
111
- if (c != 0) return c;
112
106
  c = GPR_ICMP(a->args.filter_count, b->args.filter_count);
113
107
  if (c != 0) return c;
114
- if (a->args.addr_len) {
115
- c = memcmp(a->args.addr, b->args.addr, a->args.addr_len);
116
- if (c != 0) return c;
117
- }
118
108
  if (a->args.filter_count > 0) {
119
109
  c = memcmp(a->args.filters, b->args.filters,
120
110
  a->args.filter_count * sizeof(*a->args.filters));
@@ -126,9 +116,8 @@ static int subchannel_key_compare(grpc_subchannel_key *a,
126
116
  void grpc_subchannel_key_destroy(grpc_exec_ctx *exec_ctx,
127
117
  grpc_subchannel_key *k) {
128
118
  grpc_connector_unref(exec_ctx, k->connector);
129
- gpr_free(k->args.addr);
130
119
  gpr_free((grpc_channel_args *)k->args.filters);
131
- grpc_channel_args_destroy((grpc_channel_args *)k->args.args);
120
+ grpc_channel_args_destroy(exec_ctx, (grpc_channel_args *)k->args.args);
132
121
  gpr_free(k);
133
122
  }
134
123
 
@@ -31,11 +31,11 @@
31
31
  *
32
32
  */
33
33
 
34
- #ifndef GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_INDEX_H
35
- #define GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_INDEX_H
34
+ #ifndef GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_INDEX_H
35
+ #define GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_INDEX_H
36
36
 
37
- #include "src/core/ext/client_config/connector.h"
38
- #include "src/core/ext/client_config/subchannel.h"
37
+ #include "src/core/ext/client_channel/connector.h"
38
+ #include "src/core/ext/client_channel/subchannel.h"
39
39
 
40
40
  /** \file Provides an index of active subchannels so that they can be
41
41
  shared amongst channels */
@@ -43,8 +43,8 @@
43
43
  typedef struct grpc_subchannel_key grpc_subchannel_key;
44
44
 
45
45
  /** Create a key that can be used to uniquely identify a subchannel */
46
- grpc_subchannel_key *grpc_subchannel_key_create(grpc_connector *con,
47
- grpc_subchannel_args *args);
46
+ grpc_subchannel_key *grpc_subchannel_key_create(
47
+ grpc_connector *con, const grpc_subchannel_args *args);
48
48
 
49
49
  /** Destroy a subchannel key */
50
50
  void grpc_subchannel_key_destroy(grpc_exec_ctx *exec_ctx,
@@ -74,4 +74,4 @@ void grpc_subchannel_index_init(void);
74
74
  /** Shutdown the subchannel index (global) */
75
75
  void grpc_subchannel_index_shutdown(void);
76
76
 
77
- #endif /* GRPC_CORE_EXT_CLIENT_CONFIG_SUBCHANNEL_INDEX_H */
77
+ #endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_SUBCHANNEL_INDEX_H */
@@ -31,15 +31,15 @@
31
31
  *
32
32
  */
33
33
 
34
- #include "src/core/ext/client_config/uri_parser.h"
34
+ #include "src/core/ext/client_channel/uri_parser.h"
35
35
 
36
36
  #include <string.h>
37
37
 
38
+ #include <grpc/slice.h>
39
+ #include <grpc/slice_buffer.h>
38
40
  #include <grpc/support/alloc.h>
39
41
  #include <grpc/support/log.h>
40
42
  #include <grpc/support/port_platform.h>
41
- #include <grpc/support/slice.h>
42
- #include <grpc/support/slice_buffer.h>
43
43
  #include <grpc/support/string_util.h>
44
44
 
45
45
  #include "src/core/lib/support/string.h"
@@ -137,7 +137,6 @@ static int parse_fragment_or_query(const char *uri_text, size_t *i) {
137
137
  return 1;
138
138
  }
139
139
 
140
- static void do_nothing(void *ignored) {}
141
140
  static void parse_query_parts(grpc_uri *uri) {
142
141
  static const char *QUERY_PARTS_SEPARATOR = "&";
143
142
  static const char *QUERY_PARTS_VALUE_SEPARATOR = "=";
@@ -148,38 +147,32 @@ static void parse_query_parts(grpc_uri *uri) {
148
147
  uri->num_query_parts = 0;
149
148
  return;
150
149
  }
151
- gpr_slice query_slice =
152
- gpr_slice_new(uri->query, strlen(uri->query), do_nothing);
153
- gpr_slice_buffer query_parts; /* the &-separated elements of the query */
154
- gpr_slice_buffer query_param_parts; /* the =-separated subelements */
155
150
 
156
- gpr_slice_buffer_init(&query_parts);
157
- gpr_slice_buffer_init(&query_param_parts);
158
-
159
- gpr_slice_split(query_slice, QUERY_PARTS_SEPARATOR, &query_parts);
160
- uri->query_parts = gpr_malloc(query_parts.count * sizeof(char *));
161
- uri->query_parts_values = gpr_malloc(query_parts.count * sizeof(char *));
162
- uri->num_query_parts = query_parts.count;
163
- for (size_t i = 0; i < query_parts.count; i++) {
164
- gpr_slice_split(query_parts.slices[i], QUERY_PARTS_VALUE_SEPARATOR,
165
- &query_param_parts);
166
- GPR_ASSERT(query_param_parts.count > 0);
167
- uri->query_parts[i] =
168
- gpr_dump_slice(query_param_parts.slices[0], GPR_DUMP_ASCII);
169
- if (query_param_parts.count > 1) {
151
+ gpr_string_split(uri->query, QUERY_PARTS_SEPARATOR, &uri->query_parts,
152
+ &uri->num_query_parts);
153
+ uri->query_parts_values = gpr_malloc(uri->num_query_parts * sizeof(char **));
154
+ for (size_t i = 0; i < uri->num_query_parts; i++) {
155
+ char **query_param_parts;
156
+ size_t num_query_param_parts;
157
+ char *full = uri->query_parts[i];
158
+ gpr_string_split(full, QUERY_PARTS_VALUE_SEPARATOR, &query_param_parts,
159
+ &num_query_param_parts);
160
+ GPR_ASSERT(num_query_param_parts > 0);
161
+ uri->query_parts[i] = query_param_parts[0];
162
+ if (num_query_param_parts > 1) {
170
163
  /* TODO(dgq): only the first value after the separator is considered.
171
164
  * Perhaps all chars after the first separator for the query part should
172
165
  * be included, even if they include the separator. */
173
- uri->query_parts_values[i] =
174
- gpr_dump_slice(query_param_parts.slices[1], GPR_DUMP_ASCII);
166
+ uri->query_parts_values[i] = query_param_parts[1];
175
167
  } else {
176
168
  uri->query_parts_values[i] = NULL;
177
169
  }
178
- gpr_slice_buffer_reset_and_unref(&query_param_parts);
170
+ for (size_t j = 2; j < num_query_param_parts; j++) {
171
+ gpr_free(query_param_parts[j]);
172
+ }
173
+ gpr_free(query_param_parts);
174
+ gpr_free(full);
179
175
  }
180
- gpr_slice_buffer_destroy(&query_parts);
181
- gpr_slice_buffer_destroy(&query_param_parts);
182
- gpr_slice_unref(query_slice);
183
176
  }
184
177
 
185
178
  grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) {
@@ -31,8 +31,8 @@
31
31
  *
32
32
  */
33
33
 
34
- #ifndef GRPC_CORE_EXT_CLIENT_CONFIG_URI_PARSER_H
35
- #define GRPC_CORE_EXT_CLIENT_CONFIG_URI_PARSER_H
34
+ #ifndef GRPC_CORE_EXT_CLIENT_CHANNEL_URI_PARSER_H
35
+ #define GRPC_CORE_EXT_CLIENT_CHANNEL_URI_PARSER_H
36
36
 
37
37
  #include <stddef.h>
38
38
 
@@ -60,4 +60,4 @@ const char *grpc_uri_get_query_arg(const grpc_uri *uri, const char *key);
60
60
  /** destroy a uri */
61
61
  void grpc_uri_destroy(grpc_uri *uri);
62
62
 
63
- #endif /* GRPC_CORE_EXT_CLIENT_CONFIG_URI_PARSER_H */
63
+ #endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_URI_PARSER_H */
@@ -0,0 +1,1406 @@
1
+ /*
2
+ *
3
+ * Copyright 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
+ /** Implementation of the gRPC LB policy.
35
+ *
36
+ * This policy takes as input a set of resolved addresses {a1..an} for which the
37
+ * LB set was set (it's the resolver's responsibility to ensure this). That is
38
+ * to say, {a1..an} represent a collection of LB servers.
39
+ *
40
+ * An internal channel (\a glb_lb_policy.lb_channel) is created over {a1..an}.
41
+ * This channel behaves just like a regular channel. In particular, the
42
+ * constructed URI over the addresses a1..an will use the default pick first
43
+ * policy to select from this list of LB server backends.
44
+ *
45
+ * The first time the policy gets a request for a pick, a ping, or to exit the
46
+ * idle state, \a query_for_backends_locked() is called. This function sets up
47
+ * and initiates the internal communication with the LB server. In particular,
48
+ * it's responsible for instantiating the internal *streaming* call to the LB
49
+ * server (whichever address from {a1..an} pick-first chose). This call is
50
+ * serviced by two callbacks, \a lb_on_server_status_received and \a
51
+ * lb_on_response_received. The former will be called when the call to the LB
52
+ * server completes. This can happen if the LB server closes the connection or
53
+ * if this policy itself cancels the call (for example because it's shutting
54
+ * down). If the internal call times out, the usual behavior of pick-first
55
+ * applies, continuing to pick from the list {a1..an}.
56
+ *
57
+ * Upon sucesss, the incoming \a LoadBalancingResponse is processed by \a
58
+ * res_recv. An invalid one results in the termination of the streaming call. A
59
+ * new streaming call should be created if possible, failing the original call
60
+ * otherwise. For a valid \a LoadBalancingResponse, the server list of actual
61
+ * backends is extracted. A Round Robin policy will be created from this list.
62
+ * There are two possible scenarios:
63
+ *
64
+ * 1. This is the first server list received. There was no previous instance of
65
+ * the Round Robin policy. \a rr_handover_locked() will instantiate the RR
66
+ * policy and perform all the pending operations over it.
67
+ * 2. There's already a RR policy instance active. We need to introduce the new
68
+ * one build from the new serverlist, but taking care not to disrupt the
69
+ * operations in progress over the old RR instance. This is done by
70
+ * decreasing the reference count on the old policy. The moment no more
71
+ * references are held on the old RR policy, it'll be destroyed and \a
72
+ * glb_rr_connectivity_changed notified with a \a GRPC_CHANNEL_SHUTDOWN
73
+ * state. At this point we can transition to a new RR instance safely, which
74
+ * is done once again via \a rr_handover_locked().
75
+ *
76
+ *
77
+ * Once a RR policy instance is in place (and getting updated as described),
78
+ * calls to for a pick, a ping or a cancellation will be serviced right away by
79
+ * forwarding them to the RR instance. Any time there's no RR policy available
80
+ * (ie, right after the creation of the gRPCLB policy, if an empty serverlist is
81
+ * received, etc), pick/ping requests are added to a list of pending picks/pings
82
+ * to be flushed and serviced as part of \a rr_handover_locked() the moment the
83
+ * RR policy instance becomes available.
84
+ *
85
+ * \see https://github.com/grpc/grpc/blob/master/doc/load-balancing.md for the
86
+ * high level design and details. */
87
+
88
+ /* TODO(dgq):
89
+ * - Implement LB service forwarding (point 2c. in the doc's diagram).
90
+ */
91
+
92
+ /* With the addition of a libuv endpoint, sockaddr.h now includes uv.h when
93
+ using that endpoint. Because of various transitive includes in uv.h,
94
+ including windows.h on Windows, uv.h must be included before other system
95
+ headers. Therefore, sockaddr.h must always be included first */
96
+ #include "src/core/lib/iomgr/sockaddr.h"
97
+
98
+ #include <errno.h>
99
+
100
+ #include <string.h>
101
+
102
+ #include <grpc/byte_buffer_reader.h>
103
+ #include <grpc/grpc.h>
104
+ #include <grpc/support/alloc.h>
105
+ #include <grpc/support/host_port.h>
106
+ #include <grpc/support/string_util.h>
107
+ #include <grpc/support/time.h>
108
+
109
+ #include "src/core/ext/client_channel/client_channel.h"
110
+ #include "src/core/ext/client_channel/client_channel_factory.h"
111
+ #include "src/core/ext/client_channel/lb_policy_factory.h"
112
+ #include "src/core/ext/client_channel/lb_policy_registry.h"
113
+ #include "src/core/ext/client_channel/parse_address.h"
114
+ #include "src/core/ext/lb_policy/grpclb/grpclb.h"
115
+ #include "src/core/ext/lb_policy/grpclb/load_balancer_api.h"
116
+ #include "src/core/lib/channel/channel_args.h"
117
+ #include "src/core/lib/iomgr/sockaddr.h"
118
+ #include "src/core/lib/iomgr/sockaddr_utils.h"
119
+ #include "src/core/lib/iomgr/timer.h"
120
+ #include "src/core/lib/slice/slice_internal.h"
121
+ #include "src/core/lib/slice/slice_string_helpers.h"
122
+ #include "src/core/lib/support/backoff.h"
123
+ #include "src/core/lib/support/string.h"
124
+ #include "src/core/lib/surface/call.h"
125
+ #include "src/core/lib/surface/channel.h"
126
+ #include "src/core/lib/transport/static_metadata.h"
127
+
128
+ #define GRPC_GRPCLB_MIN_CONNECT_TIMEOUT_SECONDS 20
129
+ #define GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS 1
130
+ #define GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER 1.6
131
+ #define GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS 120
132
+ #define GRPC_GRPCLB_RECONNECT_JITTER 0.2
133
+
134
+ int grpc_lb_glb_trace = 0;
135
+
136
+ /* add lb_token of selected subchannel (address) to the call's initial
137
+ * metadata */
138
+ static void initial_metadata_add_lb_token(
139
+ grpc_metadata_batch *initial_metadata,
140
+ grpc_linked_mdelem *lb_token_mdelem_storage, grpc_mdelem *lb_token) {
141
+ GPR_ASSERT(lb_token_mdelem_storage != NULL);
142
+ GPR_ASSERT(lb_token != NULL);
143
+ grpc_metadata_batch_add_tail(initial_metadata, lb_token_mdelem_storage,
144
+ lb_token);
145
+ }
146
+
147
+ typedef struct wrapped_rr_closure_arg {
148
+ /* the closure instance using this struct as argument */
149
+ grpc_closure wrapper_closure;
150
+
151
+ /* the original closure. Usually a on_complete/notify cb for pick() and ping()
152
+ * calls against the internal RR instance, respectively. */
153
+ grpc_closure *wrapped_closure;
154
+
155
+ /* the pick's initial metadata, kept in order to append the LB token for the
156
+ * pick */
157
+ grpc_metadata_batch *initial_metadata;
158
+
159
+ /* the picked target, used to determine which LB token to add to the pick's
160
+ * initial metadata */
161
+ grpc_connected_subchannel **target;
162
+
163
+ /* the LB token associated with the pick */
164
+ grpc_mdelem *lb_token;
165
+
166
+ /* storage for the lb token initial metadata mdelem */
167
+ grpc_linked_mdelem *lb_token_mdelem_storage;
168
+
169
+ /* The RR instance related to the closure */
170
+ grpc_lb_policy *rr_policy;
171
+
172
+ /* heap memory to be freed upon closure execution. */
173
+ void *free_when_done;
174
+ } wrapped_rr_closure_arg;
175
+
176
+ /* The \a on_complete closure passed as part of the pick requires keeping a
177
+ * reference to its associated round robin instance. We wrap this closure in
178
+ * order to unref the round robin instance upon its invocation */
179
+ static void wrapped_rr_closure(grpc_exec_ctx *exec_ctx, void *arg,
180
+ grpc_error *error) {
181
+ wrapped_rr_closure_arg *wc_arg = arg;
182
+
183
+ GPR_ASSERT(wc_arg->wrapped_closure != NULL);
184
+ grpc_closure_sched(exec_ctx, wc_arg->wrapped_closure, GRPC_ERROR_REF(error));
185
+
186
+ if (wc_arg->rr_policy != NULL) {
187
+ /* if *target is NULL, no pick has been made by the RR policy (eg, all
188
+ * addresses failed to connect). There won't be any user_data/token
189
+ * available */
190
+ if (*wc_arg->target != NULL) {
191
+ if (wc_arg->lb_token != NULL) {
192
+ initial_metadata_add_lb_token(wc_arg->initial_metadata,
193
+ wc_arg->lb_token_mdelem_storage,
194
+ GRPC_MDELEM_REF(wc_arg->lb_token));
195
+ } else {
196
+ gpr_log(GPR_ERROR,
197
+ "No LB token for connected subchannel pick %p (from RR "
198
+ "instance %p).",
199
+ (void *)*wc_arg->target, (void *)wc_arg->rr_policy);
200
+ abort();
201
+ }
202
+ }
203
+ if (grpc_lb_glb_trace) {
204
+ gpr_log(GPR_INFO, "Unreffing RR %p", (void *)wc_arg->rr_policy);
205
+ }
206
+ GRPC_LB_POLICY_UNREF(exec_ctx, wc_arg->rr_policy, "wrapped_rr_closure");
207
+ }
208
+ GPR_ASSERT(wc_arg->free_when_done != NULL);
209
+ gpr_free(wc_arg->free_when_done);
210
+ }
211
+
212
+ /* Linked list of pending pick requests. It stores all information needed to
213
+ * eventually call (Round Robin's) pick() on them. They mainly stay pending
214
+ * waiting for the RR policy to be created/updated.
215
+ *
216
+ * One particularity is the wrapping of the user-provided \a on_complete closure
217
+ * (in \a wrapped_on_complete and \a wrapped_on_complete_arg). This is needed in
218
+ * order to correctly unref the RR policy instance upon completion of the pick.
219
+ * See \a wrapped_rr_closure for details. */
220
+ typedef struct pending_pick {
221
+ struct pending_pick *next;
222
+
223
+ /* original pick()'s arguments */
224
+ grpc_lb_policy_pick_args pick_args;
225
+
226
+ /* output argument where to store the pick()ed connected subchannel, or NULL
227
+ * upon error. */
228
+ grpc_connected_subchannel **target;
229
+
230
+ /* args for wrapped_on_complete */
231
+ wrapped_rr_closure_arg wrapped_on_complete_arg;
232
+ } pending_pick;
233
+
234
+ static void add_pending_pick(pending_pick **root,
235
+ const grpc_lb_policy_pick_args *pick_args,
236
+ grpc_connected_subchannel **target,
237
+ grpc_closure *on_complete) {
238
+ pending_pick *pp = gpr_malloc(sizeof(*pp));
239
+ memset(pp, 0, sizeof(pending_pick));
240
+ memset(&pp->wrapped_on_complete_arg, 0, sizeof(wrapped_rr_closure_arg));
241
+ pp->next = *root;
242
+ pp->pick_args = *pick_args;
243
+ pp->target = target;
244
+ pp->wrapped_on_complete_arg.wrapped_closure = on_complete;
245
+ pp->wrapped_on_complete_arg.target = target;
246
+ pp->wrapped_on_complete_arg.initial_metadata = pick_args->initial_metadata;
247
+ pp->wrapped_on_complete_arg.lb_token_mdelem_storage =
248
+ pick_args->lb_token_mdelem_storage;
249
+ pp->wrapped_on_complete_arg.free_when_done = pp;
250
+ grpc_closure_init(&pp->wrapped_on_complete_arg.wrapper_closure,
251
+ wrapped_rr_closure, &pp->wrapped_on_complete_arg,
252
+ grpc_schedule_on_exec_ctx);
253
+ *root = pp;
254
+ }
255
+
256
+ /* Same as the \a pending_pick struct but for ping operations */
257
+ typedef struct pending_ping {
258
+ struct pending_ping *next;
259
+
260
+ /* args for wrapped_notify */
261
+ wrapped_rr_closure_arg wrapped_notify_arg;
262
+ } pending_ping;
263
+
264
+ static void add_pending_ping(pending_ping **root, grpc_closure *notify) {
265
+ pending_ping *pping = gpr_malloc(sizeof(*pping));
266
+ memset(pping, 0, sizeof(pending_ping));
267
+ memset(&pping->wrapped_notify_arg, 0, sizeof(wrapped_rr_closure_arg));
268
+ pping->wrapped_notify_arg.wrapped_closure = notify;
269
+ pping->wrapped_notify_arg.free_when_done = pping;
270
+ pping->next = *root;
271
+ grpc_closure_init(&pping->wrapped_notify_arg.wrapper_closure,
272
+ wrapped_rr_closure, &pping->wrapped_notify_arg,
273
+ grpc_schedule_on_exec_ctx);
274
+ *root = pping;
275
+ }
276
+
277
+ /*
278
+ * glb_lb_policy
279
+ */
280
+ typedef struct rr_connectivity_data rr_connectivity_data;
281
+ static const grpc_lb_policy_vtable glb_lb_policy_vtable;
282
+ typedef struct glb_lb_policy {
283
+ /** base policy: must be first */
284
+ grpc_lb_policy base;
285
+
286
+ /** mutex protecting remaining members */
287
+ gpr_mu mu;
288
+
289
+ /** who the client is trying to communicate with */
290
+ const char *server_name;
291
+ grpc_client_channel_factory *cc_factory;
292
+ grpc_channel_args *args;
293
+
294
+ /** deadline for the LB's call */
295
+ gpr_timespec deadline;
296
+
297
+ /** for communicating with the LB server */
298
+ grpc_channel *lb_channel;
299
+
300
+ /** the RR policy to use of the backend servers returned by the LB server */
301
+ grpc_lb_policy *rr_policy;
302
+
303
+ bool started_picking;
304
+
305
+ /** our connectivity state tracker */
306
+ grpc_connectivity_state_tracker state_tracker;
307
+
308
+ /** stores the deserialized response from the LB. May be NULL until one such
309
+ * response has arrived. */
310
+ grpc_grpclb_serverlist *serverlist;
311
+
312
+ /** list of picks that are waiting on RR's policy connectivity */
313
+ pending_pick *pending_picks;
314
+
315
+ /** list of pings that are waiting on RR's policy connectivity */
316
+ pending_ping *pending_pings;
317
+
318
+ bool shutting_down;
319
+
320
+ /************************************************************/
321
+ /* client data associated with the LB server communication */
322
+ /************************************************************/
323
+ /* Status from the LB server has been received. This signals the end of the LB
324
+ * call. */
325
+ grpc_closure lb_on_server_status_received;
326
+
327
+ /* A response from the LB server has been received. Process it */
328
+ grpc_closure lb_on_response_received;
329
+
330
+ /* LB call retry timer callback. */
331
+ grpc_closure lb_on_call_retry;
332
+
333
+ grpc_call *lb_call; /* streaming call to the LB server, */
334
+
335
+ grpc_metadata_array lb_initial_metadata_recv; /* initial MD from LB server */
336
+ grpc_metadata_array
337
+ lb_trailing_metadata_recv; /* trailing MD from LB server */
338
+
339
+ /* what's being sent to the LB server. Note that its value may vary if the LB
340
+ * server indicates a redirect. */
341
+ grpc_byte_buffer *lb_request_payload;
342
+
343
+ /* response the LB server, if any. Processed in lb_on_response_received() */
344
+ grpc_byte_buffer *lb_response_payload;
345
+
346
+ /* call status code and details, set in lb_on_server_status_received() */
347
+ grpc_status_code lb_call_status;
348
+ char *lb_call_status_details;
349
+ size_t lb_call_status_details_capacity;
350
+
351
+ /** LB call retry backoff state */
352
+ gpr_backoff lb_call_backoff_state;
353
+
354
+ /** LB call retry timer */
355
+ grpc_timer lb_call_retry_timer;
356
+ } glb_lb_policy;
357
+
358
+ /* Keeps track and reacts to changes in connectivity of the RR instance */
359
+ struct rr_connectivity_data {
360
+ grpc_closure on_change;
361
+ grpc_connectivity_state state;
362
+ glb_lb_policy *glb_policy;
363
+ };
364
+
365
+ static bool is_server_valid(const grpc_grpclb_server *server, size_t idx,
366
+ bool log) {
367
+ const grpc_grpclb_ip_address *ip = &server->ip_address;
368
+ if (server->port >> 16 != 0) {
369
+ if (log) {
370
+ gpr_log(GPR_ERROR,
371
+ "Invalid port '%d' at index %lu of serverlist. Ignoring.",
372
+ server->port, (unsigned long)idx);
373
+ }
374
+ return false;
375
+ }
376
+
377
+ if (ip->size != 4 && ip->size != 16) {
378
+ if (log) {
379
+ gpr_log(GPR_ERROR,
380
+ "Expected IP to be 4 or 16 bytes, got %d at index %lu of "
381
+ "serverlist. Ignoring",
382
+ ip->size, (unsigned long)idx);
383
+ }
384
+ return false;
385
+ }
386
+ return true;
387
+ }
388
+
389
+ /* vtable for LB tokens in grpc_lb_addresses. */
390
+ static void *lb_token_copy(void *token) {
391
+ return token == NULL ? NULL : GRPC_MDELEM_REF(token);
392
+ }
393
+ static void lb_token_destroy(grpc_exec_ctx *exec_ctx, void *token) {
394
+ if (token != NULL) GRPC_MDELEM_UNREF(exec_ctx, token);
395
+ }
396
+ static int lb_token_cmp(void *token1, void *token2) {
397
+ if (token1 > token2) return 1;
398
+ if (token1 < token2) return -1;
399
+ return 0;
400
+ }
401
+ static const grpc_lb_user_data_vtable lb_token_vtable = {
402
+ lb_token_copy, lb_token_destroy, lb_token_cmp};
403
+
404
+ static void parse_server(const grpc_grpclb_server *server,
405
+ grpc_resolved_address *addr) {
406
+ const uint16_t netorder_port = htons((uint16_t)server->port);
407
+ /* the addresses are given in binary format (a in(6)_addr struct) in
408
+ * server->ip_address.bytes. */
409
+ const grpc_grpclb_ip_address *ip = &server->ip_address;
410
+ memset(addr, 0, sizeof(*addr));
411
+ if (ip->size == 4) {
412
+ addr->len = sizeof(struct sockaddr_in);
413
+ struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr->addr;
414
+ addr4->sin_family = AF_INET;
415
+ memcpy(&addr4->sin_addr, ip->bytes, ip->size);
416
+ addr4->sin_port = netorder_port;
417
+ } else if (ip->size == 16) {
418
+ addr->len = sizeof(struct sockaddr_in6);
419
+ struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr->addr;
420
+ addr6->sin6_family = AF_INET6;
421
+ memcpy(&addr6->sin6_addr, ip->bytes, ip->size);
422
+ addr6->sin6_port = netorder_port;
423
+ }
424
+ }
425
+
426
+ /* Returns addresses extracted from \a serverlist. */
427
+ static grpc_lb_addresses *process_serverlist_locked(
428
+ grpc_exec_ctx *exec_ctx, const grpc_grpclb_serverlist *serverlist) {
429
+ size_t num_valid = 0;
430
+ /* first pass: count how many are valid in order to allocate the necessary
431
+ * memory in a single block */
432
+ for (size_t i = 0; i < serverlist->num_servers; ++i) {
433
+ if (is_server_valid(serverlist->servers[i], i, true)) ++num_valid;
434
+ }
435
+ if (num_valid == 0) return NULL;
436
+
437
+ grpc_lb_addresses *lb_addresses =
438
+ grpc_lb_addresses_create(num_valid, &lb_token_vtable);
439
+
440
+ /* second pass: actually populate the addresses and LB tokens (aka user data
441
+ * to the outside world) to be read by the RR policy during its creation.
442
+ * Given that the validity tests are very cheap, they are performed again
443
+ * instead of marking the valid ones during the first pass, as this would
444
+ * incurr in an allocation due to the arbitrary number of server */
445
+ size_t addr_idx = 0;
446
+ for (size_t sl_idx = 0; sl_idx < serverlist->num_servers; ++sl_idx) {
447
+ GPR_ASSERT(addr_idx < num_valid);
448
+ const grpc_grpclb_server *server = serverlist->servers[sl_idx];
449
+ if (!is_server_valid(serverlist->servers[sl_idx], sl_idx, false)) continue;
450
+
451
+ /* address processing */
452
+ grpc_resolved_address addr;
453
+ parse_server(server, &addr);
454
+
455
+ /* lb token processing */
456
+ void *user_data;
457
+ if (server->has_load_balance_token) {
458
+ const size_t lb_token_max_length =
459
+ GPR_ARRAY_SIZE(server->load_balance_token);
460
+ const size_t lb_token_length =
461
+ strnlen(server->load_balance_token, lb_token_max_length);
462
+ grpc_mdstr *lb_token_mdstr = grpc_mdstr_from_buffer(
463
+ (uint8_t *)server->load_balance_token, lb_token_length);
464
+ user_data = grpc_mdelem_from_metadata_strings(
465
+ exec_ctx, GRPC_MDSTR_LB_TOKEN, lb_token_mdstr);
466
+ } else {
467
+ char *uri = grpc_sockaddr_to_uri(&addr);
468
+ gpr_log(GPR_INFO,
469
+ "Missing LB token for backend address '%s'. The empty token will "
470
+ "be used instead",
471
+ uri);
472
+ gpr_free(uri);
473
+ user_data = GRPC_MDELEM_LB_TOKEN_EMPTY;
474
+ }
475
+
476
+ grpc_lb_addresses_set_address(lb_addresses, addr_idx, &addr.addr, addr.len,
477
+ false /* is_balancer */,
478
+ NULL /* balancer_name */, user_data);
479
+ ++addr_idx;
480
+ }
481
+ GPR_ASSERT(addr_idx == num_valid);
482
+ return lb_addresses;
483
+ }
484
+
485
+ /* returns true if the new RR policy should replace the current one, if any */
486
+ static bool update_lb_connectivity_status_locked(
487
+ grpc_exec_ctx *exec_ctx, glb_lb_policy *glb_policy,
488
+ grpc_connectivity_state new_rr_state, grpc_error *new_rr_state_error) {
489
+ grpc_error *curr_state_error;
490
+ const grpc_connectivity_state curr_glb_state = grpc_connectivity_state_check(
491
+ &glb_policy->state_tracker, &curr_state_error);
492
+
493
+ /* The new connectivity status is a function of the previous one and the new
494
+ * input coming from the status of the RR policy.
495
+ *
496
+ * current state (grpclb's)
497
+ * |
498
+ * v || I | C | R | TF | SD | <- new state (RR's)
499
+ * ===++====+=====+=====+======+======+
500
+ * I || I | C | R | [I] | [I] |
501
+ * ---++----+-----+-----+------+------+
502
+ * C || I | C | R | [C] | [C] |
503
+ * ---++----+-----+-----+------+------+
504
+ * R || I | C | R | [R] | [R] |
505
+ * ---++----+-----+-----+------+------+
506
+ * TF || I | C | R | [TF] | [TF] |
507
+ * ---++----+-----+-----+------+------+
508
+ * SD || NA | NA | NA | NA | NA | (*)
509
+ * ---++----+-----+-----+------+------+
510
+ *
511
+ * A [STATE] indicates that the old RR policy is kept. In those cases, STATE
512
+ * is the current state of grpclb, which is left untouched.
513
+ *
514
+ * In summary, if the new state is TRANSIENT_FAILURE or SHUTDOWN, stick to
515
+ * the previous RR instance.
516
+ *
517
+ * Note that the status is never updated to SHUTDOWN as a result of calling
518
+ * this function. Only glb_shutdown() has the power to set that state.
519
+ *
520
+ * (*) This function mustn't be called during shutting down. */
521
+ GPR_ASSERT(curr_glb_state != GRPC_CHANNEL_SHUTDOWN);
522
+
523
+ switch (new_rr_state) {
524
+ case GRPC_CHANNEL_TRANSIENT_FAILURE:
525
+ case GRPC_CHANNEL_SHUTDOWN:
526
+ GPR_ASSERT(new_rr_state_error != GRPC_ERROR_NONE);
527
+ return false; /* don't replace the RR policy */
528
+ case GRPC_CHANNEL_INIT:
529
+ case GRPC_CHANNEL_IDLE:
530
+ case GRPC_CHANNEL_CONNECTING:
531
+ case GRPC_CHANNEL_READY:
532
+ GPR_ASSERT(new_rr_state_error == GRPC_ERROR_NONE);
533
+ }
534
+
535
+ if (grpc_lb_glb_trace) {
536
+ gpr_log(GPR_INFO,
537
+ "Setting grpclb's state to %s from new RR policy %p state.",
538
+ grpc_connectivity_state_name(new_rr_state),
539
+ (void *)glb_policy->rr_policy);
540
+ }
541
+ grpc_connectivity_state_set(exec_ctx, &glb_policy->state_tracker,
542
+ new_rr_state, GRPC_ERROR_REF(new_rr_state_error),
543
+ "update_lb_connectivity_status_locked");
544
+ return true;
545
+ }
546
+
547
+ /* perform a pick over \a rr_policy. Given that a pick can return immediately
548
+ * (ignoring its completion callback) we need to perform the cleanups this
549
+ * callback would be otherwise resposible for */
550
+ static bool pick_from_internal_rr_locked(
551
+ grpc_exec_ctx *exec_ctx, grpc_lb_policy *rr_policy,
552
+ const grpc_lb_policy_pick_args *pick_args,
553
+ grpc_connected_subchannel **target, wrapped_rr_closure_arg *wc_arg) {
554
+ GPR_ASSERT(rr_policy != NULL);
555
+ const bool pick_done =
556
+ grpc_lb_policy_pick(exec_ctx, rr_policy, pick_args, target,
557
+ (void **)&wc_arg->lb_token, &wc_arg->wrapper_closure);
558
+ if (pick_done) {
559
+ /* synchronous grpc_lb_policy_pick call. Unref the RR policy. */
560
+ if (grpc_lb_glb_trace) {
561
+ gpr_log(GPR_INFO, "Unreffing RR (0x%" PRIxPTR ")",
562
+ (intptr_t)wc_arg->rr_policy);
563
+ }
564
+ GRPC_LB_POLICY_UNREF(exec_ctx, wc_arg->rr_policy, "glb_pick_sync");
565
+
566
+ /* add the load reporting initial metadata */
567
+ initial_metadata_add_lb_token(pick_args->initial_metadata,
568
+ pick_args->lb_token_mdelem_storage,
569
+ GRPC_MDELEM_REF(wc_arg->lb_token));
570
+
571
+ gpr_free(wc_arg);
572
+ }
573
+ /* else, the pending pick will be registered and taken care of by the
574
+ * pending pick list inside the RR policy (glb_policy->rr_policy).
575
+ * Eventually, wrapped_on_complete will be called, which will -among other
576
+ * things- add the LB token to the call's initial metadata */
577
+ return pick_done;
578
+ }
579
+
580
+ static grpc_lb_policy *create_rr_locked(
581
+ grpc_exec_ctx *exec_ctx, const grpc_grpclb_serverlist *serverlist,
582
+ glb_lb_policy *glb_policy) {
583
+ GPR_ASSERT(serverlist != NULL && serverlist->num_servers > 0);
584
+
585
+ grpc_lb_policy_args args;
586
+ memset(&args, 0, sizeof(args));
587
+ args.client_channel_factory = glb_policy->cc_factory;
588
+ grpc_lb_addresses *addresses =
589
+ process_serverlist_locked(exec_ctx, serverlist);
590
+
591
+ // Replace the LB addresses in the channel args that we pass down to
592
+ // the subchannel.
593
+ static const char *keys_to_remove[] = {GRPC_ARG_LB_ADDRESSES};
594
+ const grpc_arg arg = grpc_lb_addresses_create_channel_arg(addresses);
595
+ args.args = grpc_channel_args_copy_and_add_and_remove(
596
+ glb_policy->args, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), &arg,
597
+ 1);
598
+
599
+ grpc_lb_policy *rr = grpc_lb_policy_create(exec_ctx, "round_robin", &args);
600
+ GPR_ASSERT(rr != NULL);
601
+ grpc_lb_addresses_destroy(exec_ctx, addresses);
602
+ grpc_channel_args_destroy(exec_ctx, args.args);
603
+ return rr;
604
+ }
605
+
606
+ static void glb_rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
607
+ grpc_error *error);
608
+ /* glb_policy->rr_policy may be NULL (initial handover) */
609
+ static void rr_handover_locked(grpc_exec_ctx *exec_ctx,
610
+ glb_lb_policy *glb_policy) {
611
+ GPR_ASSERT(glb_policy->serverlist != NULL &&
612
+ glb_policy->serverlist->num_servers > 0);
613
+
614
+ if (glb_policy->shutting_down) return;
615
+
616
+ grpc_lb_policy *new_rr_policy =
617
+ create_rr_locked(exec_ctx, glb_policy->serverlist, glb_policy);
618
+ if (new_rr_policy == NULL) {
619
+ gpr_log(GPR_ERROR,
620
+ "Failure creating a RoundRobin policy for serverlist update with "
621
+ "%lu entries. The previous RR instance (%p), if any, will continue "
622
+ "to be used. Future updates from the LB will attempt to create new "
623
+ "instances.",
624
+ (unsigned long)glb_policy->serverlist->num_servers,
625
+ (void *)glb_policy->rr_policy);
626
+ return;
627
+ }
628
+
629
+ grpc_error *new_rr_state_error = NULL;
630
+ const grpc_connectivity_state new_rr_state =
631
+ grpc_lb_policy_check_connectivity(exec_ctx, new_rr_policy,
632
+ &new_rr_state_error);
633
+ /* Connectivity state is a function of the new RR policy just created */
634
+ const bool replace_old_rr = update_lb_connectivity_status_locked(
635
+ exec_ctx, glb_policy, new_rr_state, new_rr_state_error);
636
+
637
+ if (!replace_old_rr) {
638
+ /* dispose of the new RR policy that won't be used after all */
639
+ GRPC_LB_POLICY_UNREF(exec_ctx, new_rr_policy, "rr_handover_no_replace");
640
+ if (grpc_lb_glb_trace) {
641
+ gpr_log(GPR_INFO,
642
+ "Keeping old RR policy (%p) despite new serverlist: new RR "
643
+ "policy was in %s connectivity state.",
644
+ (void *)glb_policy->rr_policy,
645
+ grpc_connectivity_state_name(new_rr_state));
646
+ }
647
+ return;
648
+ }
649
+
650
+ if (grpc_lb_glb_trace) {
651
+ gpr_log(GPR_INFO, "Created RR policy (%p) to replace old RR (%p)",
652
+ (void *)new_rr_policy, (void *)glb_policy->rr_policy);
653
+ }
654
+
655
+ if (glb_policy->rr_policy != NULL) {
656
+ /* if we are phasing out an existing RR instance, unref it. */
657
+ GRPC_LB_POLICY_UNREF(exec_ctx, glb_policy->rr_policy, "rr_handover");
658
+ }
659
+
660
+ /* Finally update the RR policy to the newly created one */
661
+ glb_policy->rr_policy = new_rr_policy;
662
+
663
+ /* Add the gRPC LB's interested_parties pollset_set to that of the newly
664
+ * created RR policy. This will make the RR policy progress upon activity on
665
+ * gRPC LB, which in turn is tied to the application's call */
666
+ grpc_pollset_set_add_pollset_set(exec_ctx,
667
+ glb_policy->rr_policy->interested_parties,
668
+ glb_policy->base.interested_parties);
669
+
670
+ /* Allocate the data for the tracking of the new RR policy's connectivity.
671
+ * It'll be deallocated in glb_rr_connectivity_changed() */
672
+ rr_connectivity_data *rr_connectivity =
673
+ gpr_malloc(sizeof(rr_connectivity_data));
674
+ memset(rr_connectivity, 0, sizeof(rr_connectivity_data));
675
+ grpc_closure_init(&rr_connectivity->on_change, glb_rr_connectivity_changed,
676
+ rr_connectivity, grpc_schedule_on_exec_ctx);
677
+ rr_connectivity->glb_policy = glb_policy;
678
+ rr_connectivity->state = new_rr_state;
679
+
680
+ /* Subscribe to changes to the connectivity of the new RR */
681
+ GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "rr_connectivity_cb");
682
+ grpc_lb_policy_notify_on_state_change(exec_ctx, glb_policy->rr_policy,
683
+ &rr_connectivity->state,
684
+ &rr_connectivity->on_change);
685
+ grpc_lb_policy_exit_idle(exec_ctx, glb_policy->rr_policy);
686
+
687
+ /* Update picks and pings in wait */
688
+ pending_pick *pp;
689
+ while ((pp = glb_policy->pending_picks)) {
690
+ glb_policy->pending_picks = pp->next;
691
+ GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_pick");
692
+ pp->wrapped_on_complete_arg.rr_policy = glb_policy->rr_policy;
693
+ if (grpc_lb_glb_trace) {
694
+ gpr_log(GPR_INFO, "Pending pick about to PICK from 0x%" PRIxPTR "",
695
+ (intptr_t)glb_policy->rr_policy);
696
+ }
697
+ pick_from_internal_rr_locked(exec_ctx, glb_policy->rr_policy,
698
+ &pp->pick_args, pp->target,
699
+ &pp->wrapped_on_complete_arg);
700
+ }
701
+
702
+ pending_ping *pping;
703
+ while ((pping = glb_policy->pending_pings)) {
704
+ glb_policy->pending_pings = pping->next;
705
+ GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_ping");
706
+ pping->wrapped_notify_arg.rr_policy = glb_policy->rr_policy;
707
+ if (grpc_lb_glb_trace) {
708
+ gpr_log(GPR_INFO, "Pending ping about to PING from 0x%" PRIxPTR "",
709
+ (intptr_t)glb_policy->rr_policy);
710
+ }
711
+ grpc_lb_policy_ping_one(exec_ctx, glb_policy->rr_policy,
712
+ &pping->wrapped_notify_arg.wrapper_closure);
713
+ }
714
+ }
715
+
716
+ static void glb_rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
717
+ grpc_error *error) {
718
+ rr_connectivity_data *rr_connectivity = arg;
719
+ glb_lb_policy *glb_policy = rr_connectivity->glb_policy;
720
+
721
+ gpr_mu_lock(&glb_policy->mu);
722
+ const bool shutting_down = glb_policy->shutting_down;
723
+ bool unref_needed = false;
724
+ GRPC_ERROR_REF(error);
725
+
726
+ if (rr_connectivity->state == GRPC_CHANNEL_SHUTDOWN || shutting_down) {
727
+ /* RR policy shutting down. Don't renew subscription and free the arg of
728
+ * this callback. In addition we need to stash away the current policy to
729
+ * be UNREF'd after releasing the lock. Otherwise, if the UNREF is the last
730
+ * one, the policy would be destroyed, alongside the lock, which would
731
+ * result in a use-after-free */
732
+ unref_needed = true;
733
+ gpr_free(rr_connectivity);
734
+ } else { /* rr state != SHUTDOWN && !shutting down: biz as usual */
735
+ update_lb_connectivity_status_locked(exec_ctx, glb_policy,
736
+ rr_connectivity->state, error);
737
+ /* Resubscribe. Reuse the "rr_connectivity_cb" weak ref. */
738
+ grpc_lb_policy_notify_on_state_change(exec_ctx, glb_policy->rr_policy,
739
+ &rr_connectivity->state,
740
+ &rr_connectivity->on_change);
741
+ }
742
+ gpr_mu_unlock(&glb_policy->mu);
743
+ if (unref_needed) {
744
+ GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &glb_policy->base,
745
+ "rr_connectivity_cb");
746
+ }
747
+ GRPC_ERROR_UNREF(error);
748
+ }
749
+
750
+ static grpc_lb_policy *glb_create(grpc_exec_ctx *exec_ctx,
751
+ grpc_lb_policy_factory *factory,
752
+ grpc_lb_policy_args *args) {
753
+ /* Count the number of gRPC-LB addresses. There must be at least one.
754
+ * TODO(roth): For now, we ignore non-balancer addresses, but in the
755
+ * future, we may change the behavior such that we fall back to using
756
+ * the non-balancer addresses if we cannot reach any balancers. At that
757
+ * time, this should be changed to allow a list with no balancer addresses,
758
+ * since the resolver might fail to return a balancer address even when
759
+ * this is the right LB policy to use. */
760
+ const grpc_arg *arg =
761
+ grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES);
762
+ GPR_ASSERT(arg != NULL && arg->type == GRPC_ARG_POINTER);
763
+ grpc_lb_addresses *addresses = arg->value.pointer.p;
764
+ size_t num_grpclb_addrs = 0;
765
+ for (size_t i = 0; i < addresses->num_addresses; ++i) {
766
+ if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs;
767
+ }
768
+ if (num_grpclb_addrs == 0) return NULL;
769
+
770
+ glb_lb_policy *glb_policy = gpr_malloc(sizeof(*glb_policy));
771
+ memset(glb_policy, 0, sizeof(*glb_policy));
772
+
773
+ /* Get server name. */
774
+ arg = grpc_channel_args_find(args->args, GRPC_ARG_SERVER_URI);
775
+ GPR_ASSERT(arg != NULL);
776
+ GPR_ASSERT(arg->type == GRPC_ARG_STRING);
777
+ grpc_uri *uri = grpc_uri_parse(arg->value.string, true);
778
+ GPR_ASSERT(uri->path[0] != '\0');
779
+ glb_policy->server_name =
780
+ gpr_strdup(uri->path[0] == '/' ? uri->path + 1 : uri->path);
781
+ if (grpc_lb_glb_trace) {
782
+ gpr_log(GPR_INFO, "Will use '%s' as the server name for LB request.",
783
+ glb_policy->server_name);
784
+ }
785
+ grpc_uri_destroy(uri);
786
+
787
+ /* All input addresses in addresses come from a resolver that claims
788
+ * they are LB services. It's the resolver's responsibility to make sure
789
+ * this policy is only instantiated and used in that case.
790
+ *
791
+ * Create a client channel over them to communicate with a LB service */
792
+ glb_policy->cc_factory = args->client_channel_factory;
793
+ glb_policy->args = grpc_channel_args_copy(args->args);
794
+ GPR_ASSERT(glb_policy->cc_factory != NULL);
795
+
796
+ /* construct a target from the addresses in args, given in the form
797
+ * ipvX://ip1:port1,ip2:port2,...
798
+ * TODO(dgq): support mixed ip version */
799
+ char **addr_strs = gpr_malloc(sizeof(char *) * num_grpclb_addrs);
800
+ size_t addr_index = 0;
801
+ for (size_t i = 0; i < addresses->num_addresses; i++) {
802
+ if (addresses->addresses[i].user_data != NULL) {
803
+ gpr_log(GPR_ERROR,
804
+ "This LB policy doesn't support user data. It will be ignored");
805
+ }
806
+ if (addresses->addresses[i].is_balancer) {
807
+ if (addr_index == 0) {
808
+ addr_strs[addr_index++] =
809
+ grpc_sockaddr_to_uri(&addresses->addresses[i].address);
810
+ } else {
811
+ GPR_ASSERT(grpc_sockaddr_to_string(&addr_strs[addr_index++],
812
+ &addresses->addresses[i].address,
813
+ true) > 0);
814
+ }
815
+ }
816
+ }
817
+ size_t uri_path_len;
818
+ char *target_uri_str = gpr_strjoin_sep((const char **)addr_strs,
819
+ num_grpclb_addrs, ",", &uri_path_len);
820
+
821
+ /* Create a channel to talk to the LBs.
822
+ *
823
+ * We strip out the channel arg for the LB policy name, since we want
824
+ * to use the default (pick_first) in this case.
825
+ *
826
+ * We also strip out the channel arg for the resolved addresses, since
827
+ * that will be generated by the name resolver used in the LB channel.
828
+ * Note that the LB channel will use the sockaddr resolver, so this
829
+ * won't actually generate a query to DNS (or some other name service).
830
+ * However, the addresses returned by the sockaddr resolver will have
831
+ * is_balancer=false, whereas our own addresses have is_balancer=true.
832
+ * We need the LB channel to return addresses with is_balancer=false
833
+ * so that it does not wind up recursively using the grpclb LB policy,
834
+ * as per the special case logic in client_channel.c.
835
+ *
836
+ * Finally, we also strip out the channel arg for the server URI,
837
+ * since that will be different for the LB channel than for the parent
838
+ * channel. (The client channel factory will re-add this arg with
839
+ * the right value.)
840
+ */
841
+ static const char *keys_to_remove[] = {
842
+ GRPC_ARG_LB_POLICY_NAME, GRPC_ARG_LB_ADDRESSES, GRPC_ARG_SERVER_URI};
843
+ grpc_channel_args *new_args = grpc_channel_args_copy_and_remove(
844
+ args->args, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove));
845
+ glb_policy->lb_channel = grpc_client_channel_factory_create_channel(
846
+ exec_ctx, glb_policy->cc_factory, target_uri_str,
847
+ GRPC_CLIENT_CHANNEL_TYPE_LOAD_BALANCING, new_args);
848
+ grpc_channel_args_destroy(exec_ctx, new_args);
849
+
850
+ gpr_free(target_uri_str);
851
+ for (size_t i = 0; i < num_grpclb_addrs; i++) {
852
+ gpr_free(addr_strs[i]);
853
+ }
854
+ gpr_free(addr_strs);
855
+
856
+ if (glb_policy->lb_channel == NULL) {
857
+ gpr_free(glb_policy);
858
+ return NULL;
859
+ }
860
+
861
+ grpc_lb_policy_init(&glb_policy->base, &glb_lb_policy_vtable);
862
+ gpr_mu_init(&glb_policy->mu);
863
+ grpc_connectivity_state_init(&glb_policy->state_tracker, GRPC_CHANNEL_IDLE,
864
+ "grpclb");
865
+
866
+ return &glb_policy->base;
867
+ }
868
+
869
+ static void glb_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
870
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
871
+ GPR_ASSERT(glb_policy->pending_picks == NULL);
872
+ GPR_ASSERT(glb_policy->pending_pings == NULL);
873
+ gpr_free((void *)glb_policy->server_name);
874
+ grpc_channel_args_destroy(exec_ctx, glb_policy->args);
875
+ grpc_channel_destroy(glb_policy->lb_channel);
876
+ glb_policy->lb_channel = NULL;
877
+ grpc_connectivity_state_destroy(exec_ctx, &glb_policy->state_tracker);
878
+ if (glb_policy->serverlist != NULL) {
879
+ grpc_grpclb_destroy_serverlist(glb_policy->serverlist);
880
+ }
881
+ gpr_mu_destroy(&glb_policy->mu);
882
+ gpr_free(glb_policy);
883
+ }
884
+
885
+ static void glb_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
886
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
887
+ gpr_mu_lock(&glb_policy->mu);
888
+ glb_policy->shutting_down = true;
889
+
890
+ pending_pick *pp = glb_policy->pending_picks;
891
+ glb_policy->pending_picks = NULL;
892
+ pending_ping *pping = glb_policy->pending_pings;
893
+ glb_policy->pending_pings = NULL;
894
+ if (glb_policy->rr_policy) {
895
+ GRPC_LB_POLICY_UNREF(exec_ctx, glb_policy->rr_policy, "glb_shutdown");
896
+ }
897
+ grpc_connectivity_state_set(
898
+ exec_ctx, &glb_policy->state_tracker, GRPC_CHANNEL_SHUTDOWN,
899
+ GRPC_ERROR_CREATE("Channel Shutdown"), "glb_shutdown");
900
+ /* We need a copy of the lb_call pointer because we can't cancell the call
901
+ * while holding glb_policy->mu: lb_on_server_status_received, invoked due to
902
+ * the cancel, needs to acquire that same lock */
903
+ grpc_call *lb_call = glb_policy->lb_call;
904
+ gpr_mu_unlock(&glb_policy->mu);
905
+
906
+ /* glb_policy->lb_call and this local lb_call must be consistent at this point
907
+ * because glb_policy->lb_call is only assigned in lb_call_init_locked as part
908
+ * of query_for_backends_locked, which can only be invoked while
909
+ * glb_policy->shutting_down is false. */
910
+ if (lb_call != NULL) {
911
+ grpc_call_cancel(lb_call, NULL);
912
+ /* lb_on_server_status_received will pick up the cancel and clean up */
913
+ }
914
+ while (pp != NULL) {
915
+ pending_pick *next = pp->next;
916
+ *pp->target = NULL;
917
+ grpc_closure_sched(exec_ctx, &pp->wrapped_on_complete_arg.wrapper_closure,
918
+ GRPC_ERROR_NONE);
919
+ pp = next;
920
+ }
921
+
922
+ while (pping != NULL) {
923
+ pending_ping *next = pping->next;
924
+ grpc_closure_sched(exec_ctx, &pping->wrapped_notify_arg.wrapper_closure,
925
+ GRPC_ERROR_NONE);
926
+ pping = next;
927
+ }
928
+ }
929
+
930
+ static void glb_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
931
+ grpc_connected_subchannel **target,
932
+ grpc_error *error) {
933
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
934
+ gpr_mu_lock(&glb_policy->mu);
935
+ pending_pick *pp = glb_policy->pending_picks;
936
+ glb_policy->pending_picks = NULL;
937
+ while (pp != NULL) {
938
+ pending_pick *next = pp->next;
939
+ if (pp->target == target) {
940
+ *target = NULL;
941
+ grpc_closure_sched(
942
+ exec_ctx, &pp->wrapped_on_complete_arg.wrapper_closure,
943
+ GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1));
944
+ } else {
945
+ pp->next = glb_policy->pending_picks;
946
+ glb_policy->pending_picks = pp;
947
+ }
948
+ pp = next;
949
+ }
950
+ gpr_mu_unlock(&glb_policy->mu);
951
+ GRPC_ERROR_UNREF(error);
952
+ }
953
+
954
+ static void glb_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
955
+ uint32_t initial_metadata_flags_mask,
956
+ uint32_t initial_metadata_flags_eq,
957
+ grpc_error *error) {
958
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
959
+ gpr_mu_lock(&glb_policy->mu);
960
+ pending_pick *pp = glb_policy->pending_picks;
961
+ glb_policy->pending_picks = NULL;
962
+ while (pp != NULL) {
963
+ pending_pick *next = pp->next;
964
+ if ((pp->pick_args.initial_metadata_flags & initial_metadata_flags_mask) ==
965
+ initial_metadata_flags_eq) {
966
+ grpc_closure_sched(
967
+ exec_ctx, &pp->wrapped_on_complete_arg.wrapper_closure,
968
+ GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1));
969
+ } else {
970
+ pp->next = glb_policy->pending_picks;
971
+ glb_policy->pending_picks = pp;
972
+ }
973
+ pp = next;
974
+ }
975
+ gpr_mu_unlock(&glb_policy->mu);
976
+ GRPC_ERROR_UNREF(error);
977
+ }
978
+
979
+ static void query_for_backends_locked(grpc_exec_ctx *exec_ctx,
980
+ glb_lb_policy *glb_policy);
981
+ static void start_picking_locked(grpc_exec_ctx *exec_ctx,
982
+ glb_lb_policy *glb_policy) {
983
+ glb_policy->started_picking = true;
984
+ gpr_backoff_reset(&glb_policy->lb_call_backoff_state);
985
+ query_for_backends_locked(exec_ctx, glb_policy);
986
+ }
987
+
988
+ static void glb_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
989
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
990
+ gpr_mu_lock(&glb_policy->mu);
991
+ if (!glb_policy->started_picking) {
992
+ start_picking_locked(exec_ctx, glb_policy);
993
+ }
994
+ gpr_mu_unlock(&glb_policy->mu);
995
+ }
996
+
997
+ static int glb_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
998
+ const grpc_lb_policy_pick_args *pick_args,
999
+ grpc_connected_subchannel **target, void **user_data,
1000
+ grpc_closure *on_complete) {
1001
+ if (pick_args->lb_token_mdelem_storage == NULL) {
1002
+ *target = NULL;
1003
+ grpc_closure_sched(
1004
+ exec_ctx, on_complete,
1005
+ GRPC_ERROR_CREATE("No mdelem storage for the LB token. Load reporting "
1006
+ "won't work without it. Failing"));
1007
+ return 0;
1008
+ }
1009
+
1010
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
1011
+ gpr_mu_lock(&glb_policy->mu);
1012
+ glb_policy->deadline = pick_args->deadline;
1013
+ bool pick_done;
1014
+
1015
+ if (glb_policy->rr_policy != NULL) {
1016
+ if (grpc_lb_glb_trace) {
1017
+ gpr_log(GPR_INFO, "grpclb %p about to PICK from RR %p",
1018
+ (void *)glb_policy, (void *)glb_policy->rr_policy);
1019
+ }
1020
+ GRPC_LB_POLICY_REF(glb_policy->rr_policy, "glb_pick");
1021
+
1022
+ wrapped_rr_closure_arg *wc_arg = gpr_malloc(sizeof(wrapped_rr_closure_arg));
1023
+ memset(wc_arg, 0, sizeof(wrapped_rr_closure_arg));
1024
+
1025
+ grpc_closure_init(&wc_arg->wrapper_closure, wrapped_rr_closure, wc_arg,
1026
+ grpc_schedule_on_exec_ctx);
1027
+ wc_arg->rr_policy = glb_policy->rr_policy;
1028
+ wc_arg->target = target;
1029
+ wc_arg->wrapped_closure = on_complete;
1030
+ wc_arg->lb_token_mdelem_storage = pick_args->lb_token_mdelem_storage;
1031
+ wc_arg->initial_metadata = pick_args->initial_metadata;
1032
+ wc_arg->free_when_done = wc_arg;
1033
+ pick_done = pick_from_internal_rr_locked(exec_ctx, glb_policy->rr_policy,
1034
+ pick_args, target, wc_arg);
1035
+ } else {
1036
+ if (grpc_lb_glb_trace) {
1037
+ gpr_log(GPR_DEBUG,
1038
+ "No RR policy in grpclb instance %p. Adding to grpclb's pending "
1039
+ "picks",
1040
+ (void *)(glb_policy));
1041
+ }
1042
+ add_pending_pick(&glb_policy->pending_picks, pick_args, target,
1043
+ on_complete);
1044
+
1045
+ if (!glb_policy->started_picking) {
1046
+ start_picking_locked(exec_ctx, glb_policy);
1047
+ }
1048
+ pick_done = false;
1049
+ }
1050
+ gpr_mu_unlock(&glb_policy->mu);
1051
+ return pick_done;
1052
+ }
1053
+
1054
+ static grpc_connectivity_state glb_check_connectivity(
1055
+ grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
1056
+ grpc_error **connectivity_error) {
1057
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
1058
+ grpc_connectivity_state st;
1059
+ gpr_mu_lock(&glb_policy->mu);
1060
+ st = grpc_connectivity_state_check(&glb_policy->state_tracker,
1061
+ connectivity_error);
1062
+ gpr_mu_unlock(&glb_policy->mu);
1063
+ return st;
1064
+ }
1065
+
1066
+ static void glb_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
1067
+ grpc_closure *closure) {
1068
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
1069
+ gpr_mu_lock(&glb_policy->mu);
1070
+ if (glb_policy->rr_policy) {
1071
+ grpc_lb_policy_ping_one(exec_ctx, glb_policy->rr_policy, closure);
1072
+ } else {
1073
+ add_pending_ping(&glb_policy->pending_pings, closure);
1074
+ if (!glb_policy->started_picking) {
1075
+ start_picking_locked(exec_ctx, glb_policy);
1076
+ }
1077
+ }
1078
+ gpr_mu_unlock(&glb_policy->mu);
1079
+ }
1080
+
1081
+ static void glb_notify_on_state_change(grpc_exec_ctx *exec_ctx,
1082
+ grpc_lb_policy *pol,
1083
+ grpc_connectivity_state *current,
1084
+ grpc_closure *notify) {
1085
+ glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
1086
+ gpr_mu_lock(&glb_policy->mu);
1087
+ grpc_connectivity_state_notify_on_state_change(
1088
+ exec_ctx, &glb_policy->state_tracker, current, notify);
1089
+
1090
+ gpr_mu_unlock(&glb_policy->mu);
1091
+ }
1092
+
1093
+ static void lb_on_server_status_received(grpc_exec_ctx *exec_ctx, void *arg,
1094
+ grpc_error *error);
1095
+ static void lb_on_response_received(grpc_exec_ctx *exec_ctx, void *arg,
1096
+ grpc_error *error);
1097
+ static void lb_call_init_locked(grpc_exec_ctx *exec_ctx,
1098
+ glb_lb_policy *glb_policy) {
1099
+ GPR_ASSERT(glb_policy->server_name != NULL);
1100
+ GPR_ASSERT(glb_policy->server_name[0] != '\0');
1101
+ GPR_ASSERT(!glb_policy->shutting_down);
1102
+
1103
+ /* Note the following LB call progresses every time there's activity in \a
1104
+ * glb_policy->base.interested_parties, which is comprised of the polling
1105
+ * entities from \a client_channel. */
1106
+ glb_policy->lb_call = grpc_channel_create_pollset_set_call(
1107
+ exec_ctx, glb_policy->lb_channel, NULL, GRPC_PROPAGATE_DEFAULTS,
1108
+ glb_policy->base.interested_parties,
1109
+ "/grpc.lb.v1.LoadBalancer/BalanceLoad", glb_policy->server_name,
1110
+ glb_policy->deadline, NULL);
1111
+
1112
+ grpc_metadata_array_init(&glb_policy->lb_initial_metadata_recv);
1113
+ grpc_metadata_array_init(&glb_policy->lb_trailing_metadata_recv);
1114
+
1115
+ grpc_grpclb_request *request =
1116
+ grpc_grpclb_request_create(glb_policy->server_name);
1117
+ grpc_slice request_payload_slice = grpc_grpclb_request_encode(request);
1118
+ glb_policy->lb_request_payload =
1119
+ grpc_raw_byte_buffer_create(&request_payload_slice, 1);
1120
+ grpc_slice_unref_internal(exec_ctx, request_payload_slice);
1121
+ grpc_grpclb_request_destroy(request);
1122
+
1123
+ glb_policy->lb_call_status_details = NULL;
1124
+ glb_policy->lb_call_status_details_capacity = 0;
1125
+
1126
+ grpc_closure_init(&glb_policy->lb_on_server_status_received,
1127
+ lb_on_server_status_received, glb_policy,
1128
+ grpc_schedule_on_exec_ctx);
1129
+ grpc_closure_init(&glb_policy->lb_on_response_received,
1130
+ lb_on_response_received, glb_policy,
1131
+ grpc_schedule_on_exec_ctx);
1132
+
1133
+ gpr_backoff_init(&glb_policy->lb_call_backoff_state,
1134
+ GRPC_GRPCLB_INITIAL_CONNECT_BACKOFF_SECONDS,
1135
+ GRPC_GRPCLB_RECONNECT_BACKOFF_MULTIPLIER,
1136
+ GRPC_GRPCLB_RECONNECT_JITTER,
1137
+ GRPC_GRPCLB_MIN_CONNECT_TIMEOUT_SECONDS * 1000,
1138
+ GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS * 1000);
1139
+ }
1140
+
1141
+ static void lb_call_destroy_locked(glb_lb_policy *glb_policy) {
1142
+ GPR_ASSERT(glb_policy->lb_call != NULL);
1143
+ grpc_call_destroy(glb_policy->lb_call);
1144
+ glb_policy->lb_call = NULL;
1145
+
1146
+ grpc_metadata_array_destroy(&glb_policy->lb_initial_metadata_recv);
1147
+ grpc_metadata_array_destroy(&glb_policy->lb_trailing_metadata_recv);
1148
+
1149
+ grpc_byte_buffer_destroy(glb_policy->lb_request_payload);
1150
+ gpr_free(glb_policy->lb_call_status_details);
1151
+ }
1152
+
1153
+ /*
1154
+ * Auxiliary functions and LB client callbacks.
1155
+ */
1156
+ static void query_for_backends_locked(grpc_exec_ctx *exec_ctx,
1157
+ glb_lb_policy *glb_policy) {
1158
+ GPR_ASSERT(glb_policy->lb_channel != NULL);
1159
+ if (glb_policy->shutting_down) return;
1160
+
1161
+ lb_call_init_locked(exec_ctx, glb_policy);
1162
+
1163
+ if (grpc_lb_glb_trace) {
1164
+ gpr_log(GPR_INFO, "Query for backends (grpclb: %p, lb_call: %p)",
1165
+ (void *)glb_policy, (void *)glb_policy->lb_call);
1166
+ }
1167
+ GPR_ASSERT(glb_policy->lb_call != NULL);
1168
+
1169
+ grpc_call_error call_error;
1170
+ grpc_op ops[4];
1171
+ memset(ops, 0, sizeof(ops));
1172
+
1173
+ grpc_op *op = ops;
1174
+ op->op = GRPC_OP_SEND_INITIAL_METADATA;
1175
+ op->data.send_initial_metadata.count = 0;
1176
+ op->flags = 0;
1177
+ op->reserved = NULL;
1178
+ op++;
1179
+
1180
+ op->op = GRPC_OP_RECV_INITIAL_METADATA;
1181
+ op->data.recv_initial_metadata.recv_initial_metadata =
1182
+ &glb_policy->lb_initial_metadata_recv;
1183
+ op->flags = 0;
1184
+ op->reserved = NULL;
1185
+ op++;
1186
+
1187
+ GPR_ASSERT(glb_policy->lb_request_payload != NULL);
1188
+ op->op = GRPC_OP_SEND_MESSAGE;
1189
+ op->data.send_message.send_message = glb_policy->lb_request_payload;
1190
+ op->flags = 0;
1191
+ op->reserved = NULL;
1192
+ op++;
1193
+
1194
+ op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
1195
+ op->data.recv_status_on_client.trailing_metadata =
1196
+ &glb_policy->lb_trailing_metadata_recv;
1197
+ op->data.recv_status_on_client.status = &glb_policy->lb_call_status;
1198
+ op->data.recv_status_on_client.status_details =
1199
+ &glb_policy->lb_call_status_details;
1200
+ op->data.recv_status_on_client.status_details_capacity =
1201
+ &glb_policy->lb_call_status_details_capacity;
1202
+ op->flags = 0;
1203
+ op->reserved = NULL;
1204
+ op++;
1205
+ /* take a weak ref (won't prevent calling of \a glb_shutdown if the strong ref
1206
+ * count goes to zero) to be unref'd in lb_on_server_status_received */
1207
+ GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "lb_on_server_status_received");
1208
+ call_error = grpc_call_start_batch_and_execute(
1209
+ exec_ctx, glb_policy->lb_call, ops, (size_t)(op - ops),
1210
+ &glb_policy->lb_on_server_status_received);
1211
+ GPR_ASSERT(GRPC_CALL_OK == call_error);
1212
+
1213
+ op = ops;
1214
+ op->op = GRPC_OP_RECV_MESSAGE;
1215
+ op->data.recv_message.recv_message = &glb_policy->lb_response_payload;
1216
+ op->flags = 0;
1217
+ op->reserved = NULL;
1218
+ op++;
1219
+ /* take another weak ref to be unref'd in lb_on_response_received */
1220
+ GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "lb_on_response_received");
1221
+ call_error = grpc_call_start_batch_and_execute(
1222
+ exec_ctx, glb_policy->lb_call, ops, (size_t)(op - ops),
1223
+ &glb_policy->lb_on_response_received);
1224
+ GPR_ASSERT(GRPC_CALL_OK == call_error);
1225
+ }
1226
+
1227
+ static void lb_on_response_received(grpc_exec_ctx *exec_ctx, void *arg,
1228
+ grpc_error *error) {
1229
+ glb_lb_policy *glb_policy = arg;
1230
+
1231
+ grpc_op ops[2];
1232
+ memset(ops, 0, sizeof(ops));
1233
+ grpc_op *op = ops;
1234
+ gpr_mu_lock(&glb_policy->mu);
1235
+ if (glb_policy->lb_response_payload != NULL) {
1236
+ gpr_backoff_reset(&glb_policy->lb_call_backoff_state);
1237
+ /* Received data from the LB server. Look inside
1238
+ * glb_policy->lb_response_payload, for a serverlist. */
1239
+ grpc_byte_buffer_reader bbr;
1240
+ grpc_byte_buffer_reader_init(&bbr, glb_policy->lb_response_payload);
1241
+ grpc_slice response_slice = grpc_byte_buffer_reader_readall(&bbr);
1242
+ grpc_byte_buffer_destroy(glb_policy->lb_response_payload);
1243
+ grpc_grpclb_serverlist *serverlist =
1244
+ grpc_grpclb_response_parse_serverlist(response_slice);
1245
+ if (serverlist != NULL) {
1246
+ GPR_ASSERT(glb_policy->lb_call != NULL);
1247
+ grpc_slice_unref_internal(exec_ctx, response_slice);
1248
+ if (grpc_lb_glb_trace) {
1249
+ gpr_log(GPR_INFO, "Serverlist with %lu servers received",
1250
+ (unsigned long)serverlist->num_servers);
1251
+ for (size_t i = 0; i < serverlist->num_servers; ++i) {
1252
+ grpc_resolved_address addr;
1253
+ parse_server(serverlist->servers[i], &addr);
1254
+ char *ipport;
1255
+ grpc_sockaddr_to_string(&ipport, &addr, false);
1256
+ gpr_log(GPR_INFO, "Serverlist[%lu]: %s", (unsigned long)i, ipport);
1257
+ gpr_free(ipport);
1258
+ }
1259
+ }
1260
+
1261
+ /* update serverlist */
1262
+ if (serverlist->num_servers > 0) {
1263
+ if (grpc_grpclb_serverlist_equals(glb_policy->serverlist, serverlist)) {
1264
+ if (grpc_lb_glb_trace) {
1265
+ gpr_log(GPR_INFO,
1266
+ "Incoming server list identical to current, ignoring.");
1267
+ }
1268
+ grpc_grpclb_destroy_serverlist(serverlist);
1269
+ } else { /* new serverlist */
1270
+ if (glb_policy->serverlist != NULL) {
1271
+ /* dispose of the old serverlist */
1272
+ grpc_grpclb_destroy_serverlist(glb_policy->serverlist);
1273
+ }
1274
+ /* and update the copy in the glb_lb_policy instance. This serverlist
1275
+ * instance will be destroyed either upon the next update or in
1276
+ * glb_destroy() */
1277
+ glb_policy->serverlist = serverlist;
1278
+
1279
+ rr_handover_locked(exec_ctx, glb_policy);
1280
+ }
1281
+ } else {
1282
+ if (grpc_lb_glb_trace) {
1283
+ gpr_log(GPR_INFO,
1284
+ "Received empty server list. Picks will stay pending until a "
1285
+ "response with > 0 servers is received");
1286
+ }
1287
+ }
1288
+ } else { /* serverlist == NULL */
1289
+ gpr_log(GPR_ERROR, "Invalid LB response received: '%s'. Ignoring.",
1290
+ grpc_dump_slice(response_slice, GPR_DUMP_ASCII | GPR_DUMP_HEX));
1291
+ grpc_slice_unref_internal(exec_ctx, response_slice);
1292
+ }
1293
+
1294
+ if (!glb_policy->shutting_down) {
1295
+ /* keep listening for serverlist updates */
1296
+ op->op = GRPC_OP_RECV_MESSAGE;
1297
+ op->data.recv_message.recv_message = &glb_policy->lb_response_payload;
1298
+ op->flags = 0;
1299
+ op->reserved = NULL;
1300
+ op++;
1301
+ /* reuse the "lb_on_response_received" weak ref taken in
1302
+ * query_for_backends_locked() */
1303
+ const grpc_call_error call_error = grpc_call_start_batch_and_execute(
1304
+ exec_ctx, glb_policy->lb_call, ops, (size_t)(op - ops),
1305
+ &glb_policy->lb_on_response_received); /* loop */
1306
+ GPR_ASSERT(GRPC_CALL_OK == call_error);
1307
+ }
1308
+ gpr_mu_unlock(&glb_policy->mu);
1309
+ } else { /* empty payload: call cancelled. */
1310
+ /* dispose of the "lb_on_response_received" weak ref taken in
1311
+ * query_for_backends_locked() and reused in every reception loop */
1312
+ gpr_mu_unlock(&glb_policy->mu);
1313
+ GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &glb_policy->base,
1314
+ "lb_on_response_received_empty_payload");
1315
+ }
1316
+ }
1317
+
1318
+ static void lb_call_on_retry_timer(grpc_exec_ctx *exec_ctx, void *arg,
1319
+ grpc_error *error) {
1320
+ glb_lb_policy *glb_policy = arg;
1321
+ gpr_mu_lock(&glb_policy->mu);
1322
+
1323
+ if (!glb_policy->shutting_down) {
1324
+ if (grpc_lb_glb_trace) {
1325
+ gpr_log(GPR_INFO, "Restaring call to LB server (grpclb %p)",
1326
+ (void *)glb_policy);
1327
+ }
1328
+ GPR_ASSERT(glb_policy->lb_call == NULL);
1329
+ query_for_backends_locked(exec_ctx, glb_policy);
1330
+ }
1331
+ gpr_mu_unlock(&glb_policy->mu);
1332
+ GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &glb_policy->base,
1333
+ "grpclb_on_retry_timer");
1334
+ }
1335
+
1336
+ static void lb_on_server_status_received(grpc_exec_ctx *exec_ctx, void *arg,
1337
+ grpc_error *error) {
1338
+ glb_lb_policy *glb_policy = arg;
1339
+ gpr_mu_lock(&glb_policy->mu);
1340
+
1341
+ GPR_ASSERT(glb_policy->lb_call != NULL);
1342
+
1343
+ if (grpc_lb_glb_trace) {
1344
+ gpr_log(GPR_DEBUG,
1345
+ "Status from LB server received. Status = %d, Details = '%s', "
1346
+ "(call: %p)",
1347
+ glb_policy->lb_call_status, glb_policy->lb_call_status_details,
1348
+ (void *)glb_policy->lb_call);
1349
+ }
1350
+
1351
+ /* We need to performe cleanups no matter what. */
1352
+ lb_call_destroy_locked(glb_policy);
1353
+
1354
+ if (!glb_policy->shutting_down) {
1355
+ /* if we aren't shutting down, restart the LB client call after some time */
1356
+ gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
1357
+ gpr_timespec next_try =
1358
+ gpr_backoff_step(&glb_policy->lb_call_backoff_state, now);
1359
+ if (grpc_lb_glb_trace) {
1360
+ gpr_log(GPR_DEBUG, "Connection to LB server lost (grpclb: %p)...",
1361
+ (void *)glb_policy);
1362
+ gpr_timespec timeout = gpr_time_sub(next_try, now);
1363
+ if (gpr_time_cmp(timeout, gpr_time_0(timeout.clock_type)) > 0) {
1364
+ gpr_log(GPR_DEBUG, "... retrying in %" PRId64 ".%09d seconds.",
1365
+ timeout.tv_sec, timeout.tv_nsec);
1366
+ } else {
1367
+ gpr_log(GPR_DEBUG, "... retrying immediately.");
1368
+ }
1369
+ }
1370
+ GRPC_LB_POLICY_WEAK_REF(&glb_policy->base, "grpclb_retry_timer");
1371
+ grpc_closure_init(&glb_policy->lb_on_call_retry, lb_call_on_retry_timer,
1372
+ glb_policy, grpc_schedule_on_exec_ctx);
1373
+ grpc_timer_init(exec_ctx, &glb_policy->lb_call_retry_timer, next_try,
1374
+ &glb_policy->lb_on_call_retry, now);
1375
+ }
1376
+ gpr_mu_unlock(&glb_policy->mu);
1377
+ GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &glb_policy->base,
1378
+ "lb_on_server_status_received");
1379
+ }
1380
+
1381
+ /* Code wiring the policy with the rest of the core */
1382
+ static const grpc_lb_policy_vtable glb_lb_policy_vtable = {
1383
+ glb_destroy, glb_shutdown, glb_pick,
1384
+ glb_cancel_pick, glb_cancel_picks, glb_ping_one,
1385
+ glb_exit_idle, glb_check_connectivity, glb_notify_on_state_change};
1386
+
1387
+ static void glb_factory_ref(grpc_lb_policy_factory *factory) {}
1388
+
1389
+ static void glb_factory_unref(grpc_lb_policy_factory *factory) {}
1390
+
1391
+ static const grpc_lb_policy_factory_vtable glb_factory_vtable = {
1392
+ glb_factory_ref, glb_factory_unref, glb_create, "grpclb"};
1393
+
1394
+ static grpc_lb_policy_factory glb_lb_policy_factory = {&glb_factory_vtable};
1395
+
1396
+ grpc_lb_policy_factory *grpc_glb_lb_factory_create() {
1397
+ return &glb_lb_policy_factory;
1398
+ }
1399
+
1400
+ /* Plugin registration */
1401
+ void grpc_lb_policy_grpclb_init() {
1402
+ grpc_register_lb_policy(grpc_glb_lb_factory_create());
1403
+ grpc_register_tracer("glb", &grpc_lb_glb_trace);
1404
+ }
1405
+
1406
+ void grpc_lb_policy_grpclb_shutdown() {}