grpc 1.37.1 → 1.39.0

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 (636) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +96 -59
  3. data/include/grpc/event_engine/README.md +38 -0
  4. data/include/grpc/event_engine/endpoint_config.h +48 -0
  5. data/include/grpc/event_engine/event_engine.h +334 -0
  6. data/include/grpc/event_engine/port.h +41 -0
  7. data/include/grpc/event_engine/slice_allocator.h +91 -0
  8. data/include/grpc/grpc.h +11 -4
  9. data/include/grpc/grpc_security.h +32 -0
  10. data/include/grpc/grpc_security_constants.h +15 -0
  11. data/include/grpc/impl/codegen/grpc_types.h +28 -13
  12. data/include/grpc/impl/codegen/port_platform.h +22 -0
  13. data/include/grpc/module.modulemap +14 -14
  14. data/src/core/ext/filters/client_channel/backup_poller.cc +3 -3
  15. data/src/core/ext/filters/client_channel/channel_connectivity.cc +177 -202
  16. data/src/core/ext/filters/client_channel/client_channel.cc +630 -3103
  17. data/src/core/ext/filters/client_channel/client_channel.h +489 -55
  18. data/src/core/ext/filters/client_channel/client_channel_channelz.h +1 -1
  19. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +4 -1
  20. data/src/core/ext/filters/client_channel/config_selector.h +1 -1
  21. data/src/core/ext/filters/client_channel/connector.h +1 -1
  22. data/src/core/ext/filters/client_channel/dynamic_filters.cc +9 -10
  23. data/src/core/ext/filters/client_channel/dynamic_filters.h +3 -3
  24. data/src/core/ext/filters/client_channel/health/health_check_client.cc +28 -27
  25. data/src/core/ext/filters/client_channel/health/health_check_client.h +30 -29
  26. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +24 -21
  27. data/src/core/ext/filters/client_channel/http_proxy.cc +16 -1
  28. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +6 -6
  29. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +46 -43
  30. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +1 -1
  31. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +2 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +5 -5
  33. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +14 -12
  34. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc +755 -0
  35. data/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h +10 -0
  36. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +4 -4
  37. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +1 -1
  38. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +15 -15
  39. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +46 -54
  40. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_impl.cc +23 -23
  41. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +31 -46
  42. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_resolver.cc +146 -155
  43. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -1
  44. data/src/core/ext/filters/client_channel/lb_policy.h +4 -4
  45. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  46. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +4 -4
  47. data/src/core/ext/filters/client_channel/lb_policy_registry.h +1 -1
  48. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +24 -18
  49. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +1 -1
  50. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_event_engine.cc +31 -0
  51. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +3 -3
  52. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +2 -2
  53. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +14 -14
  54. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +33 -24
  55. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +1 -1
  56. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_event_engine.cc +28 -0
  57. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv.cc +1 -1
  58. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +1 -1
  59. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +18 -12
  60. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +20 -28
  61. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +7 -5
  62. data/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc +20 -13
  63. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +1 -1
  64. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +60 -32
  65. data/src/core/ext/filters/client_channel/resolver.h +2 -2
  66. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +32 -239
  67. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +20 -49
  68. data/src/core/ext/filters/client_channel/retry_filter.cc +2449 -0
  69. data/src/core/ext/filters/client_channel/retry_filter.h +30 -0
  70. data/src/core/ext/filters/client_channel/retry_service_config.cc +306 -0
  71. data/src/core/ext/filters/client_channel/retry_service_config.h +96 -0
  72. data/src/core/ext/filters/client_channel/server_address.cc +1 -1
  73. data/src/core/ext/filters/client_channel/service_config.cc +15 -14
  74. data/src/core/ext/filters/client_channel/service_config.h +7 -6
  75. data/src/core/ext/filters/client_channel/service_config_channel_arg_filter.cc +5 -4
  76. data/src/core/ext/filters/client_channel/service_config_parser.cc +6 -6
  77. data/src/core/ext/filters/client_channel/service_config_parser.h +7 -4
  78. data/src/core/ext/filters/client_channel/subchannel.cc +17 -16
  79. data/src/core/ext/filters/client_channel/subchannel.h +7 -6
  80. data/src/core/ext/filters/client_idle/client_idle_filter.cc +17 -16
  81. data/src/core/ext/filters/deadline/deadline_filter.cc +10 -10
  82. data/src/core/ext/filters/fault_injection/fault_injection_filter.cc +25 -18
  83. data/src/core/ext/filters/fault_injection/service_config_parser.cc +5 -5
  84. data/src/core/ext/filters/fault_injection/service_config_parser.h +1 -1
  85. data/src/core/ext/filters/http/client/http_client_filter.cc +28 -21
  86. data/src/core/ext/filters/http/client_authority_filter.cc +3 -3
  87. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +23 -22
  88. data/src/core/ext/filters/http/message_compress/message_decompress_filter.cc +21 -21
  89. data/src/core/ext/filters/http/server/http_server_filter.cc +27 -23
  90. data/src/core/ext/filters/max_age/max_age_filter.cc +12 -10
  91. data/src/core/ext/filters/message_size/message_size_filter.cc +14 -11
  92. data/src/core/ext/filters/message_size/message_size_filter.h +1 -1
  93. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.cc +4 -3
  94. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +7 -7
  95. data/src/core/ext/transport/chttp2/client/chttp2_connector.h +7 -7
  96. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +2 -2
  97. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.cc +3 -2
  98. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +3 -3
  99. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +44 -45
  100. data/src/core/ext/transport/chttp2/server/chttp2_server.h +2 -2
  101. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc +3 -4
  102. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +5 -4
  103. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +3 -4
  104. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +139 -120
  105. data/src/core/ext/transport/chttp2/transport/context_list.cc +4 -5
  106. data/src/core/ext/transport/chttp2/transport/context_list.h +4 -4
  107. data/src/core/ext/transport/chttp2/transport/flow_control.cc +3 -3
  108. data/src/core/ext/transport/chttp2/transport/flow_control.h +8 -8
  109. data/src/core/ext/transport/chttp2/transport/frame_data.cc +8 -8
  110. data/src/core/ext/transport/chttp2/transport/frame_data.h +10 -10
  111. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +7 -8
  112. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +6 -6
  113. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +7 -8
  114. data/src/core/ext/transport/chttp2/transport/frame_ping.h +7 -6
  115. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +7 -7
  116. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +6 -6
  117. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +6 -5
  118. data/src/core/ext/transport/chttp2/transport/frame_settings.h +6 -6
  119. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +4 -6
  120. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +4 -6
  121. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +237 -208
  122. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +10 -10
  123. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +4 -3
  124. data/src/core/ext/transport/chttp2/transport/hpack_table.h +4 -4
  125. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +2 -2
  126. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +2 -2
  127. data/src/core/ext/transport/chttp2/transport/internal.h +32 -27
  128. data/src/core/ext/transport/chttp2/transport/parsing.cc +65 -58
  129. data/src/core/ext/transport/chttp2/transport/writing.cc +7 -3
  130. data/src/core/ext/transport/inproc/inproc_transport.cc +72 -60
  131. data/src/core/ext/xds/certificate_provider_factory.h +1 -1
  132. data/src/core/ext/xds/certificate_provider_store.h +3 -3
  133. data/src/core/ext/xds/file_watcher_certificate_provider_factory.cc +3 -3
  134. data/src/core/ext/xds/file_watcher_certificate_provider_factory.h +2 -2
  135. data/src/core/ext/xds/xds_api.cc +348 -199
  136. data/src/core/ext/xds/xds_api.h +21 -12
  137. data/src/core/ext/xds/xds_bootstrap.cc +97 -159
  138. data/src/core/ext/xds/xds_bootstrap.h +19 -24
  139. data/src/core/ext/xds/xds_certificate_provider.cc +4 -4
  140. data/src/core/ext/xds/xds_certificate_provider.h +4 -4
  141. data/src/core/ext/xds/xds_channel_args.h +5 -2
  142. data/src/core/ext/xds/xds_client.cc +310 -178
  143. data/src/core/ext/xds/xds_client.h +41 -27
  144. data/src/core/ext/xds/xds_client_stats.h +3 -2
  145. data/src/core/ext/xds/xds_server_config_fetcher.cc +34 -20
  146. data/src/core/lib/{iomgr → address_utils}/parse_address.cc +17 -17
  147. data/src/core/lib/{iomgr → address_utils}/parse_address.h +7 -7
  148. data/src/core/lib/{iomgr → address_utils}/sockaddr_utils.cc +16 -20
  149. data/src/core/lib/{iomgr → address_utils}/sockaddr_utils.h +16 -11
  150. data/src/core/lib/channel/channel_stack.cc +10 -9
  151. data/src/core/lib/channel/channel_stack.h +10 -9
  152. data/src/core/lib/channel/channel_stack_builder.cc +2 -2
  153. data/src/core/lib/channel/channel_stack_builder.h +1 -1
  154. data/src/core/lib/channel/channelz.cc +21 -13
  155. data/src/core/lib/channel/channelz.h +3 -0
  156. data/src/core/lib/channel/connected_channel.cc +4 -4
  157. data/src/core/lib/channel/handshaker.cc +7 -6
  158. data/src/core/lib/channel/handshaker.h +5 -5
  159. data/src/core/lib/event_engine/endpoint_config.cc +46 -0
  160. data/src/core/lib/event_engine/endpoint_config_internal.h +42 -0
  161. data/src/core/lib/event_engine/event_engine.cc +50 -0
  162. data/src/core/lib/event_engine/slice_allocator.cc +89 -0
  163. data/src/core/lib/event_engine/sockaddr.cc +40 -0
  164. data/src/core/lib/event_engine/sockaddr.h +44 -0
  165. data/src/core/lib/gpr/wrap_memcpy.cc +2 -1
  166. data/src/core/lib/gprpp/ref_counted.h +28 -14
  167. data/src/core/lib/gprpp/status_helper.cc +407 -0
  168. data/src/core/lib/gprpp/status_helper.h +183 -0
  169. data/src/core/lib/http/httpcli.cc +11 -11
  170. data/src/core/lib/http/httpcli_security_connector.cc +11 -7
  171. data/src/core/lib/http/parser.cc +16 -16
  172. data/src/core/lib/http/parser.h +4 -4
  173. data/src/core/lib/iomgr/buffer_list.cc +7 -9
  174. data/src/core/lib/iomgr/buffer_list.h +4 -5
  175. data/src/core/lib/iomgr/call_combiner.cc +15 -12
  176. data/src/core/lib/iomgr/call_combiner.h +12 -14
  177. data/src/core/lib/iomgr/cfstream_handle.cc +3 -3
  178. data/src/core/lib/iomgr/cfstream_handle.h +1 -1
  179. data/src/core/lib/iomgr/closure.h +7 -6
  180. data/src/core/lib/iomgr/combiner.cc +14 -12
  181. data/src/core/lib/iomgr/combiner.h +2 -2
  182. data/src/core/lib/iomgr/endpoint.cc +1 -1
  183. data/src/core/lib/iomgr/endpoint.h +2 -2
  184. data/src/core/lib/iomgr/endpoint_cfstream.cc +11 -13
  185. data/src/core/lib/iomgr/endpoint_pair_event_engine.cc +33 -0
  186. data/src/core/lib/iomgr/endpoint_pair_windows.cc +1 -1
  187. data/src/core/lib/iomgr/error.cc +168 -61
  188. data/src/core/lib/iomgr/error.h +217 -106
  189. data/src/core/lib/iomgr/error_cfstream.cc +3 -2
  190. data/src/core/lib/iomgr/error_cfstream.h +2 -2
  191. data/src/core/lib/iomgr/error_internal.h +5 -1
  192. data/src/core/lib/iomgr/ev_apple.cc +5 -5
  193. data/src/core/lib/iomgr/ev_epoll1_linux.cc +19 -19
  194. data/src/core/lib/iomgr/ev_epollex_linux.cc +48 -45
  195. data/src/core/lib/iomgr/ev_poll_posix.cc +26 -23
  196. data/src/core/lib/iomgr/ev_posix.cc +9 -8
  197. data/src/core/lib/iomgr/ev_posix.h +9 -9
  198. data/src/core/lib/iomgr/event_engine/closure.cc +54 -0
  199. data/src/core/lib/iomgr/event_engine/closure.h +33 -0
  200. data/src/core/lib/iomgr/event_engine/endpoint.cc +194 -0
  201. data/src/core/lib/iomgr/event_engine/endpoint.h +53 -0
  202. data/src/core/lib/iomgr/event_engine/iomgr.cc +105 -0
  203. data/src/core/lib/iomgr/event_engine/iomgr.h +24 -0
  204. data/src/core/lib/iomgr/event_engine/pollset.cc +87 -0
  205. data/src/core/lib/iomgr/event_engine/pollset.h +25 -0
  206. data/src/core/lib/iomgr/event_engine/promise.h +51 -0
  207. data/src/core/lib/iomgr/event_engine/resolved_address_internal.cc +41 -0
  208. data/src/core/lib/iomgr/event_engine/resolved_address_internal.h +35 -0
  209. data/src/core/lib/iomgr/event_engine/resolver.cc +110 -0
  210. data/src/core/lib/iomgr/event_engine/tcp.cc +243 -0
  211. data/src/core/lib/iomgr/event_engine/timer.cc +57 -0
  212. data/src/core/lib/iomgr/exec_ctx.cc +12 -4
  213. data/src/core/lib/iomgr/exec_ctx.h +4 -5
  214. data/src/core/lib/iomgr/executor/threadpool.cc +2 -3
  215. data/src/core/lib/iomgr/executor/threadpool.h +2 -2
  216. data/src/core/lib/iomgr/executor.cc +8 -8
  217. data/src/core/lib/iomgr/executor.h +2 -2
  218. data/src/core/lib/iomgr/iomgr.cc +2 -2
  219. data/src/core/lib/iomgr/iomgr.h +1 -1
  220. data/src/core/lib/iomgr/iomgr_custom.cc +1 -1
  221. data/src/core/lib/iomgr/iomgr_internal.cc +2 -2
  222. data/src/core/lib/iomgr/iomgr_internal.h +3 -3
  223. data/src/core/lib/iomgr/iomgr_posix.cc +3 -1
  224. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +42 -12
  225. data/src/core/lib/iomgr/iomgr_windows.cc +1 -1
  226. data/src/core/lib/iomgr/load_file.cc +4 -4
  227. data/src/core/lib/iomgr/load_file.h +2 -2
  228. data/src/core/lib/iomgr/lockfree_event.cc +5 -5
  229. data/src/core/lib/iomgr/lockfree_event.h +1 -1
  230. data/src/core/lib/iomgr/pollset.cc +5 -5
  231. data/src/core/lib/iomgr/pollset.h +9 -9
  232. data/src/core/lib/iomgr/pollset_custom.cc +7 -7
  233. data/src/core/lib/iomgr/pollset_custom.h +3 -1
  234. data/src/core/lib/iomgr/pollset_uv.cc +3 -1
  235. data/src/core/lib/iomgr/pollset_uv.h +5 -1
  236. data/src/core/lib/iomgr/pollset_windows.cc +5 -5
  237. data/src/core/lib/iomgr/port.h +7 -5
  238. data/src/core/lib/iomgr/python_util.h +1 -1
  239. data/src/core/lib/iomgr/resolve_address.cc +8 -4
  240. data/src/core/lib/iomgr/resolve_address.h +12 -6
  241. data/src/core/lib/iomgr/resolve_address_custom.cc +10 -9
  242. data/src/core/lib/iomgr/resolve_address_custom.h +3 -3
  243. data/src/core/lib/iomgr/resolve_address_posix.cc +3 -3
  244. data/src/core/lib/iomgr/resolve_address_windows.cc +4 -4
  245. data/src/core/lib/iomgr/resource_quota.cc +11 -10
  246. data/src/core/lib/iomgr/sockaddr.h +1 -0
  247. data/src/core/lib/iomgr/socket_mutator.cc +15 -2
  248. data/src/core/lib/iomgr/socket_mutator.h +26 -2
  249. data/src/core/lib/iomgr/socket_utils_common_posix.cc +24 -22
  250. data/src/core/lib/iomgr/socket_utils_posix.h +20 -20
  251. data/src/core/lib/iomgr/tcp_client_cfstream.cc +4 -4
  252. data/src/core/lib/iomgr/tcp_client_custom.cc +5 -6
  253. data/src/core/lib/iomgr/tcp_client_posix.cc +22 -19
  254. data/src/core/lib/iomgr/tcp_client_posix.h +3 -4
  255. data/src/core/lib/iomgr/tcp_client_windows.cc +5 -5
  256. data/src/core/lib/iomgr/tcp_custom.cc +14 -16
  257. data/src/core/lib/iomgr/tcp_custom.h +13 -12
  258. data/src/core/lib/iomgr/tcp_posix.cc +78 -73
  259. data/src/core/lib/iomgr/tcp_posix.h +8 -0
  260. data/src/core/lib/iomgr/tcp_server.cc +6 -6
  261. data/src/core/lib/iomgr/tcp_server.h +12 -11
  262. data/src/core/lib/iomgr/tcp_server_custom.cc +26 -25
  263. data/src/core/lib/iomgr/tcp_server_posix.cc +28 -21
  264. data/src/core/lib/iomgr/tcp_server_utils_posix.h +13 -12
  265. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +21 -18
  266. data/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +9 -9
  267. data/src/core/lib/iomgr/tcp_server_utils_posix_noifaddrs.cc +4 -4
  268. data/src/core/lib/iomgr/tcp_server_windows.cc +26 -25
  269. data/src/core/lib/iomgr/tcp_uv.cc +25 -23
  270. data/src/core/lib/iomgr/tcp_windows.cc +13 -13
  271. data/src/core/lib/iomgr/tcp_windows.h +2 -2
  272. data/src/core/lib/iomgr/timer.h +6 -1
  273. data/src/core/lib/iomgr/timer_custom.cc +2 -1
  274. data/src/core/lib/iomgr/timer_custom.h +1 -1
  275. data/src/core/lib/iomgr/timer_generic.cc +6 -6
  276. data/src/core/lib/iomgr/udp_server.cc +21 -20
  277. data/src/core/lib/iomgr/unix_sockets_posix.cc +3 -3
  278. data/src/core/lib/iomgr/unix_sockets_posix.h +2 -2
  279. data/src/core/lib/iomgr/unix_sockets_posix_noop.cc +10 -7
  280. data/src/core/lib/iomgr/wakeup_fd_eventfd.cc +3 -3
  281. data/src/core/lib/iomgr/wakeup_fd_pipe.cc +4 -4
  282. data/src/core/lib/iomgr/wakeup_fd_posix.cc +3 -3
  283. data/src/core/lib/iomgr/wakeup_fd_posix.h +8 -6
  284. data/src/core/lib/iomgr/work_serializer.h +17 -1
  285. data/src/core/lib/json/json.h +1 -1
  286. data/src/core/lib/json/json_reader.cc +4 -4
  287. data/src/core/lib/matchers/matchers.cc +39 -39
  288. data/src/core/lib/matchers/matchers.h +28 -28
  289. data/src/core/lib/security/authorization/authorization_engine.h +44 -0
  290. data/src/core/lib/security/authorization/authorization_policy_provider.h +32 -0
  291. data/src/core/lib/security/authorization/authorization_policy_provider_vtable.cc +46 -0
  292. data/src/core/lib/security/authorization/evaluate_args.cc +209 -0
  293. data/src/core/lib/security/authorization/evaluate_args.h +91 -0
  294. data/src/core/lib/security/credentials/composite/composite_credentials.cc +4 -4
  295. data/src/core/lib/security/credentials/composite/composite_credentials.h +2 -2
  296. data/src/core/lib/security/credentials/credentials.h +2 -2
  297. data/src/core/lib/security/credentials/external/aws_external_account_credentials.cc +17 -13
  298. data/src/core/lib/security/credentials/external/aws_external_account_credentials.h +13 -11
  299. data/src/core/lib/security/credentials/external/aws_request_signer.cc +2 -1
  300. data/src/core/lib/security/credentials/external/aws_request_signer.h +1 -1
  301. data/src/core/lib/security/credentials/external/external_account_credentials.cc +15 -12
  302. data/src/core/lib/security/credentials/external/external_account_credentials.h +9 -8
  303. data/src/core/lib/security/credentials/external/file_external_account_credentials.cc +5 -4
  304. data/src/core/lib/security/credentials/external/file_external_account_credentials.h +4 -3
  305. data/src/core/lib/security/credentials/external/url_external_account_credentials.cc +8 -8
  306. data/src/core/lib/security/credentials/external/url_external_account_credentials.h +9 -7
  307. data/src/core/lib/security/credentials/fake/fake_credentials.cc +2 -2
  308. data/src/core/lib/security/credentials/fake/fake_credentials.h +2 -2
  309. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +12 -10
  310. data/src/core/lib/security/credentials/iam/iam_credentials.cc +2 -2
  311. data/src/core/lib/security/credentials/iam/iam_credentials.h +2 -2
  312. data/src/core/lib/security/credentials/jwt/json_token.cc +2 -2
  313. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +3 -3
  314. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +2 -2
  315. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +7 -5
  316. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +21 -19
  317. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +5 -5
  318. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +5 -5
  319. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -2
  320. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.cc +8 -7
  321. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h +9 -9
  322. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.cc +19 -13
  323. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +4 -0
  324. data/src/core/lib/security/credentials/tls/tls_utils.cc +32 -0
  325. data/src/core/lib/security/credentials/tls/tls_utils.h +13 -0
  326. data/src/core/lib/security/credentials/xds/xds_credentials.cc +3 -3
  327. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +13 -3
  328. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +13 -3
  329. data/src/core/lib/security/security_connector/insecure/insecure_security_connector.cc +2 -2
  330. data/src/core/lib/security/security_connector/insecure/insecure_security_connector.h +12 -2
  331. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +1 -1
  332. data/src/core/lib/security/security_connector/local/local_security_connector.cc +22 -9
  333. data/src/core/lib/security/security_connector/security_connector.h +9 -4
  334. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +16 -6
  335. data/src/core/lib/security/security_connector/ssl_utils.cc +27 -4
  336. data/src/core/lib/security/security_connector/ssl_utils.h +4 -4
  337. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +56 -60
  338. data/src/core/lib/security/security_connector/tls/tls_security_connector.h +66 -48
  339. data/src/core/lib/security/transport/client_auth_filter.cc +18 -10
  340. data/src/core/lib/security/transport/secure_endpoint.cc +4 -4
  341. data/src/core/lib/security/transport/security_handshaker.cc +33 -32
  342. data/src/core/lib/security/transport/server_auth_filter.cc +19 -13
  343. data/src/core/lib/security/transport/tsi_error.cc +2 -1
  344. data/src/core/lib/security/transport/tsi_error.h +2 -1
  345. data/src/core/lib/security/util/json_util.cc +2 -2
  346. data/src/core/lib/security/util/json_util.h +1 -1
  347. data/src/core/lib/surface/call.cc +67 -46
  348. data/src/core/lib/surface/call.h +13 -2
  349. data/src/core/lib/surface/channel.cc +6 -6
  350. data/src/core/lib/surface/channel.h +3 -2
  351. data/src/core/lib/surface/channel_ping.cc +1 -1
  352. data/src/core/lib/surface/completion_queue.cc +68 -69
  353. data/src/core/lib/surface/completion_queue.h +3 -2
  354. data/src/core/lib/surface/completion_queue_factory.cc +1 -2
  355. data/src/core/lib/surface/init.cc +1 -3
  356. data/src/core/lib/surface/init.h +10 -1
  357. data/src/core/lib/surface/lame_client.cc +11 -11
  358. data/src/core/lib/surface/lame_client.h +1 -1
  359. data/src/core/lib/surface/server.cc +28 -22
  360. data/src/core/lib/surface/server.h +16 -15
  361. data/src/core/lib/surface/validate_metadata.cc +7 -7
  362. data/src/core/lib/surface/validate_metadata.h +3 -2
  363. data/src/core/lib/surface/version.cc +4 -2
  364. data/src/core/lib/transport/byte_stream.cc +5 -5
  365. data/src/core/lib/transport/byte_stream.h +8 -8
  366. data/src/core/lib/transport/connectivity_state.cc +1 -1
  367. data/src/core/lib/transport/error_utils.cc +21 -10
  368. data/src/core/lib/transport/error_utils.h +11 -5
  369. data/src/core/lib/transport/metadata_batch.cc +37 -37
  370. data/src/core/lib/transport/metadata_batch.h +19 -18
  371. data/src/core/lib/transport/transport.cc +4 -3
  372. data/src/core/lib/transport/transport.h +6 -4
  373. data/src/core/lib/transport/transport_op_string.cc +6 -6
  374. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  375. data/src/core/tsi/alts/crypt/gsec.h +6 -0
  376. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +5 -4
  377. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +7 -6
  378. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +2 -1
  379. data/src/core/tsi/ssl_transport_security.cc +32 -14
  380. data/src/core/tsi/ssl_transport_security.h +3 -4
  381. data/src/ruby/bin/math_services_pb.rb +1 -1
  382. data/src/ruby/ext/grpc/extconf.rb +2 -0
  383. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +6 -0
  384. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +11 -2
  385. data/src/ruby/lib/grpc/version.rb +1 -1
  386. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +1 -1
  387. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +6 -6
  388. data/third_party/abseil-cpp/absl/algorithm/container.h +3 -3
  389. data/third_party/abseil-cpp/absl/base/attributes.h +24 -4
  390. data/third_party/abseil-cpp/absl/base/call_once.h +2 -9
  391. data/third_party/abseil-cpp/absl/base/config.h +37 -9
  392. data/third_party/abseil-cpp/absl/base/dynamic_annotations.h +24 -10
  393. data/third_party/abseil-cpp/absl/base/internal/direct_mmap.h +4 -1
  394. data/third_party/abseil-cpp/absl/base/internal/endian.h +61 -0
  395. data/third_party/abseil-cpp/absl/base/internal/low_level_scheduling.h +2 -3
  396. data/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +34 -32
  397. data/third_party/abseil-cpp/absl/base/internal/raw_logging.h +16 -6
  398. data/third_party/abseil-cpp/absl/base/internal/spinlock.cc +11 -2
  399. data/third_party/abseil-cpp/absl/base/internal/spinlock.h +14 -5
  400. data/third_party/abseil-cpp/absl/base/internal/spinlock_akaros.inc +2 -2
  401. data/third_party/abseil-cpp/absl/base/internal/spinlock_linux.inc +3 -3
  402. data/third_party/abseil-cpp/absl/base/internal/spinlock_posix.inc +2 -2
  403. data/third_party/abseil-cpp/absl/base/internal/spinlock_wait.h +11 -11
  404. data/third_party/abseil-cpp/absl/base/internal/spinlock_win32.inc +5 -5
  405. data/third_party/abseil-cpp/absl/base/internal/sysinfo.cc +1 -1
  406. data/third_party/abseil-cpp/absl/base/internal/thread_identity.cc +5 -2
  407. data/third_party/abseil-cpp/absl/base/internal/thread_identity.h +43 -42
  408. data/third_party/abseil-cpp/absl/base/internal/throw_delegate.cc +111 -7
  409. data/third_party/abseil-cpp/absl/base/internal/unaligned_access.h +0 -76
  410. data/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc +1 -3
  411. data/third_party/abseil-cpp/absl/base/log_severity.h +4 -4
  412. data/third_party/abseil-cpp/absl/base/macros.h +11 -0
  413. data/third_party/abseil-cpp/absl/base/optimization.h +10 -7
  414. data/third_party/abseil-cpp/absl/base/options.h +1 -1
  415. data/third_party/abseil-cpp/absl/base/port.h +0 -1
  416. data/third_party/abseil-cpp/absl/base/thread_annotations.h +1 -1
  417. data/third_party/abseil-cpp/absl/container/fixed_array.h +2 -2
  418. data/third_party/abseil-cpp/absl/container/inlined_vector.h +5 -3
  419. data/third_party/abseil-cpp/absl/container/internal/compressed_tuple.h +1 -1
  420. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.cc +5 -1
  421. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler.h +2 -1
  422. data/third_party/abseil-cpp/absl/container/internal/hashtablez_sampler_force_weak_definition.cc +2 -1
  423. data/third_party/abseil-cpp/absl/container/internal/inlined_vector.h +141 -66
  424. data/third_party/abseil-cpp/absl/container/internal/layout.h +4 -4
  425. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.cc +14 -1
  426. data/third_party/abseil-cpp/absl/container/internal/raw_hash_set.h +136 -136
  427. data/third_party/abseil-cpp/absl/debugging/internal/demangle.cc +16 -12
  428. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_aarch64-inl.inc +5 -2
  429. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_config.h +3 -12
  430. data/third_party/abseil-cpp/absl/debugging/internal/stacktrace_powerpc-inl.inc +6 -1
  431. data/third_party/abseil-cpp/absl/debugging/internal/symbolize.h +3 -5
  432. data/third_party/abseil-cpp/absl/debugging/symbolize_darwin.inc +2 -2
  433. data/third_party/abseil-cpp/absl/debugging/symbolize_elf.inc +2 -2
  434. data/third_party/abseil-cpp/absl/hash/internal/city.cc +15 -12
  435. data/third_party/abseil-cpp/absl/hash/internal/city.h +1 -19
  436. data/third_party/abseil-cpp/absl/hash/internal/hash.cc +25 -10
  437. data/third_party/abseil-cpp/absl/hash/internal/hash.h +86 -37
  438. data/third_party/abseil-cpp/absl/hash/internal/wyhash.cc +111 -0
  439. data/third_party/abseil-cpp/absl/hash/internal/wyhash.h +48 -0
  440. data/third_party/abseil-cpp/absl/meta/type_traits.h +16 -2
  441. data/third_party/abseil-cpp/absl/numeric/bits.h +177 -0
  442. data/third_party/abseil-cpp/absl/numeric/int128.cc +3 -3
  443. data/third_party/abseil-cpp/absl/numeric/internal/bits.h +358 -0
  444. data/third_party/abseil-cpp/absl/numeric/internal/representation.h +55 -0
  445. data/third_party/abseil-cpp/absl/status/internal/status_internal.h +18 -0
  446. data/third_party/abseil-cpp/absl/status/internal/statusor_internal.h +4 -7
  447. data/third_party/abseil-cpp/absl/status/status.cc +29 -22
  448. data/third_party/abseil-cpp/absl/status/status.h +81 -20
  449. data/third_party/abseil-cpp/absl/status/statusor.h +3 -3
  450. data/third_party/abseil-cpp/absl/strings/charconv.cc +5 -5
  451. data/third_party/abseil-cpp/absl/strings/cord.cc +326 -371
  452. data/third_party/abseil-cpp/absl/strings/cord.h +182 -64
  453. data/third_party/abseil-cpp/absl/strings/escaping.cc +4 -4
  454. data/third_party/abseil-cpp/absl/strings/internal/charconv_parse.cc +6 -6
  455. data/third_party/abseil-cpp/absl/strings/internal/cord_internal.cc +83 -0
  456. data/third_party/abseil-cpp/absl/strings/internal/cord_internal.h +387 -17
  457. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_flat.h +146 -0
  458. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring.cc +897 -0
  459. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring.h +589 -0
  460. data/third_party/abseil-cpp/absl/strings/internal/cord_rep_ring_reader.h +114 -0
  461. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.cc +14 -0
  462. data/third_party/abseil-cpp/absl/strings/internal/str_format/arg.h +14 -0
  463. data/third_party/abseil-cpp/absl/strings/internal/str_format/bind.cc +15 -1
  464. data/third_party/abseil-cpp/absl/strings/internal/str_format/bind.h +19 -4
  465. data/third_party/abseil-cpp/absl/strings/internal/str_format/checker.h +14 -0
  466. data/third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.cc +36 -18
  467. data/third_party/abseil-cpp/absl/strings/internal/str_format/float_conversion.h +14 -0
  468. data/third_party/abseil-cpp/absl/strings/internal/str_format/parser.cc +14 -0
  469. data/third_party/abseil-cpp/absl/strings/internal/str_format/parser.h +14 -0
  470. data/third_party/abseil-cpp/absl/strings/internal/str_split_internal.h +15 -40
  471. data/third_party/abseil-cpp/absl/strings/internal/string_constant.h +64 -0
  472. data/third_party/abseil-cpp/absl/strings/match.cc +6 -3
  473. data/third_party/abseil-cpp/absl/strings/match.h +16 -6
  474. data/third_party/abseil-cpp/absl/strings/numbers.cc +132 -4
  475. data/third_party/abseil-cpp/absl/strings/numbers.h +10 -10
  476. data/third_party/abseil-cpp/absl/strings/str_join.h +1 -1
  477. data/third_party/abseil-cpp/absl/strings/str_split.h +38 -4
  478. data/third_party/abseil-cpp/absl/synchronization/internal/futex.h +154 -0
  479. data/third_party/abseil-cpp/absl/synchronization/internal/kernel_timeout.h +2 -1
  480. data/third_party/abseil-cpp/absl/synchronization/internal/per_thread_sem.cc +2 -2
  481. data/third_party/abseil-cpp/absl/synchronization/internal/per_thread_sem.h +4 -4
  482. data/third_party/abseil-cpp/absl/synchronization/internal/waiter.cc +1 -65
  483. data/third_party/abseil-cpp/absl/synchronization/internal/waiter.h +2 -6
  484. data/third_party/abseil-cpp/absl/synchronization/mutex.cc +71 -59
  485. data/third_party/abseil-cpp/absl/synchronization/mutex.h +79 -62
  486. data/third_party/abseil-cpp/absl/time/clock.cc +146 -130
  487. data/third_party/abseil-cpp/absl/time/clock.h +2 -2
  488. data/third_party/abseil-cpp/absl/time/duration.cc +3 -2
  489. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time_detail.h +7 -11
  490. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_libc.cc +7 -1
  491. data/third_party/abseil-cpp/absl/time/internal/cctz/src/tzfile.h +4 -4
  492. data/third_party/abseil-cpp/absl/time/time.cc +4 -3
  493. data/third_party/abseil-cpp/absl/time/time.h +26 -24
  494. data/third_party/abseil-cpp/absl/types/internal/variant.h +1 -1
  495. data/third_party/abseil-cpp/absl/types/variant.h +9 -4
  496. data/third_party/boringssl-with-bazel/err_data.c +483 -461
  497. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_bool.c +1 -1
  498. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_object.c +9 -7
  499. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_type.c +18 -8
  500. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_lib.c +1 -2
  501. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_locl.h +5 -0
  502. data/third_party/boringssl-with-bazel/src/crypto/asn1/tasn_enc.c +1 -1
  503. data/third_party/boringssl-with-bazel/src/crypto/asn1/tasn_fre.c +1 -1
  504. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/cipher_extra.c +4 -0
  505. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/e_tls.c +1 -88
  506. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/internal.h +14 -3
  507. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/tls_cbc.c +119 -273
  508. data/third_party/boringssl-with-bazel/src/crypto/curve25519/curve25519.c +1 -1
  509. data/third_party/boringssl-with-bazel/src/crypto/curve25519/internal.h +1 -1
  510. data/third_party/boringssl-with-bazel/src/crypto/err/err.c +87 -80
  511. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +9 -0
  512. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm.c +1 -0
  513. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h +1 -1
  514. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/prime.c +0 -4
  515. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/cipher.c +11 -3
  516. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/e_aes.c +25 -2
  517. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +7 -0
  518. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digests.c +10 -2
  519. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/md32_common.h +87 -160
  520. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec.c +4 -0
  521. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec_key.c +0 -1
  522. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h +0 -4
  523. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/ecdsa.c +104 -93
  524. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/internal.h +39 -0
  525. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md4/md4.c +52 -65
  526. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/md5/md5.c +52 -66
  527. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/cbc.c +33 -22
  528. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/cfb.c +9 -8
  529. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/ctr.c +9 -8
  530. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/gcm.c +17 -13
  531. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/internal.h +1 -22
  532. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/ofb.c +2 -1
  533. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +1 -4
  534. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/rand.c +0 -13
  535. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/urandom.c +26 -7
  536. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +26 -24
  537. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -7
  538. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/fips.c +79 -0
  539. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/self_check.c +14 -9
  540. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha1.c +61 -75
  541. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha256.c +80 -103
  542. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha512.c +40 -49
  543. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +367 -315
  544. data/third_party/boringssl-with-bazel/src/crypto/internal.h +65 -0
  545. data/third_party/boringssl-with-bazel/src/crypto/mem.c +14 -0
  546. data/third_party/boringssl-with-bazel/src/crypto/obj/obj.c +3 -3
  547. data/third_party/boringssl-with-bazel/src/crypto/pkcs7/pkcs7_x509.c +5 -3
  548. data/third_party/boringssl-with-bazel/src/crypto/pkcs8/pkcs8_x509.c +95 -48
  549. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/passive.c +2 -2
  550. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/rand_extra.c +1 -1
  551. data/third_party/boringssl-with-bazel/src/crypto/rsa_extra/rsa_asn1.c +1 -2
  552. data/third_party/boringssl-with-bazel/src/crypto/thread_pthread.c +0 -28
  553. data/third_party/boringssl-with-bazel/src/crypto/x509/internal.h +120 -11
  554. data/third_party/boringssl-with-bazel/src/crypto/x509/t_req.c +2 -0
  555. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509a.c +3 -0
  556. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_att.c +19 -25
  557. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +3 -2
  558. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_req.c +42 -89
  559. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +9 -16
  560. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +2 -0
  561. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +14 -15
  562. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vpm.c +53 -73
  563. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +31 -0
  564. data/third_party/boringssl-with-bazel/src/crypto/x509/x509rset.c +3 -0
  565. data/third_party/boringssl-with-bazel/src/crypto/x509/x_algor.c +21 -17
  566. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +3 -0
  567. data/third_party/boringssl-with-bazel/src/crypto/x509/x_attrib.c +7 -25
  568. data/third_party/boringssl-with-bazel/src/crypto/x509/x_crl.c +5 -0
  569. data/third_party/boringssl-with-bazel/src/crypto/x509/x_pubkey.c +25 -22
  570. data/third_party/boringssl-with-bazel/src/crypto/x509/x_req.c +5 -8
  571. data/third_party/boringssl-with-bazel/src/crypto/x509/x_sig.c +5 -0
  572. data/third_party/boringssl-with-bazel/src/crypto/x509/x_val.c +2 -0
  573. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509a.c +3 -0
  574. data/third_party/boringssl-with-bazel/src/crypto/x509v3/internal.h +7 -0
  575. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_cpols.c +2 -4
  576. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +1 -1
  577. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_skey.c +1 -0
  578. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_utl.c +5 -8
  579. data/third_party/boringssl-with-bazel/src/include/openssl/aead.h +1 -4
  580. data/third_party/boringssl-with-bazel/src/include/openssl/arm_arch.h +66 -1
  581. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +120 -41
  582. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +47 -7
  583. data/third_party/boringssl-with-bazel/src/include/openssl/bytestring.h +1 -0
  584. data/third_party/boringssl-with-bazel/src/include/openssl/chacha.h +1 -1
  585. data/third_party/boringssl-with-bazel/src/include/openssl/cipher.h +0 -8
  586. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +24 -4
  587. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +6 -2
  588. data/third_party/boringssl-with-bazel/src/include/openssl/ec.h +5 -2
  589. data/third_party/boringssl-with-bazel/src/include/openssl/ecdsa.h +33 -0
  590. data/third_party/boringssl-with-bazel/src/include/openssl/err.h +3 -2
  591. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +20 -49
  592. data/third_party/boringssl-with-bazel/src/{crypto/x509/x509_r2x.c → include/openssl/evp_errors.h} +41 -58
  593. data/third_party/boringssl-with-bazel/src/include/openssl/hpke.h +325 -0
  594. data/third_party/boringssl-with-bazel/src/include/openssl/obj.h +24 -5
  595. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs7.h +25 -7
  596. data/third_party/boringssl-with-bazel/src/include/openssl/pkcs8.h +9 -1
  597. data/third_party/boringssl-with-bazel/src/include/openssl/rand.h +2 -2
  598. data/third_party/boringssl-with-bazel/src/include/openssl/rsa.h +99 -63
  599. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +283 -85
  600. data/third_party/boringssl-with-bazel/src/include/openssl/tls1.h +13 -19
  601. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +445 -152
  602. data/third_party/boringssl-with-bazel/src/include/openssl/x509_vfy.h +451 -435
  603. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +2 -1
  604. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +7 -2
  605. data/third_party/boringssl-with-bazel/src/ssl/d1_srtp.cc +1 -1
  606. data/third_party/boringssl-with-bazel/src/ssl/encrypted_client_hello.cc +1133 -0
  607. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +298 -22
  608. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +66 -30
  609. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +189 -86
  610. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +154 -24
  611. data/third_party/boringssl-with-bazel/src/ssl/internal.h +414 -135
  612. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +9 -3
  613. data/third_party/boringssl-with-bazel/src/ssl/s3_lib.cc +2 -2
  614. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +14 -19
  615. data/third_party/boringssl-with-bazel/src/ssl/ssl_cert.cc +4 -6
  616. data/third_party/boringssl-with-bazel/src/ssl/ssl_key_share.cc +23 -26
  617. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +51 -60
  618. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +2 -0
  619. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +8 -31
  620. data/third_party/boringssl-with-bazel/src/ssl/ssl_stat.cc +3 -0
  621. data/third_party/boringssl-with-bazel/src/ssl/ssl_transcript.cc +4 -3
  622. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +7 -3
  623. data/third_party/boringssl-with-bazel/src/ssl/t1_lib.cc +664 -702
  624. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +65 -7
  625. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +98 -39
  626. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +141 -94
  627. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +213 -118
  628. data/third_party/boringssl-with-bazel/src/ssl/tls_method.cc +4 -2
  629. metadata +94 -46
  630. data/src/core/lib/iomgr/poller/eventmanager_libuv.cc +0 -88
  631. data/src/core/lib/iomgr/poller/eventmanager_libuv.h +0 -88
  632. data/third_party/abseil-cpp/absl/base/internal/bits.h +0 -219
  633. data/third_party/abseil-cpp/absl/synchronization/internal/mutex_nonprod.inc +0 -249
  634. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/is_fips.c +0 -29
  635. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +0 -246
  636. data/third_party/boringssl-with-bazel/src/crypto/x509/vpm_int.h +0 -71
@@ -0,0 +1,2449 @@
1
+ //
2
+ // Copyright 2015 gRPC authors.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+
17
+ #include <grpc/support/port_platform.h>
18
+
19
+ #include "src/core/ext/filters/client_channel/retry_filter.h"
20
+
21
+ #include "absl/container/inlined_vector.h"
22
+ #include "absl/status/statusor.h"
23
+ #include "absl/strings/strip.h"
24
+
25
+ #include <grpc/support/log.h>
26
+
27
+ #include "src/core/ext/filters/client_channel/client_channel.h"
28
+ #include "src/core/ext/filters/client_channel/retry_service_config.h"
29
+ #include "src/core/ext/filters/client_channel/retry_throttle.h"
30
+ #include "src/core/ext/filters/client_channel/service_config.h"
31
+ #include "src/core/ext/filters/client_channel/service_config_call_data.h"
32
+ #include "src/core/lib/backoff/backoff.h"
33
+ #include "src/core/lib/channel/channel_args.h"
34
+ #include "src/core/lib/channel/channel_stack.h"
35
+ #include "src/core/lib/channel/status_util.h"
36
+ #include "src/core/lib/gprpp/manual_constructor.h"
37
+ #include "src/core/lib/iomgr/polling_entity.h"
38
+ #include "src/core/lib/slice/slice_internal.h"
39
+ #include "src/core/lib/slice/slice_string_helpers.h"
40
+ #include "src/core/lib/transport/error_utils.h"
41
+ #include "src/core/lib/transport/metadata.h"
42
+ #include "src/core/lib/transport/metadata_batch.h"
43
+ #include "src/core/lib/transport/static_metadata.h"
44
+ #include "src/core/lib/transport/status_metadata.h"
45
+ #include "src/core/lib/uri/uri_parser.h"
46
+
47
+ //
48
+ // Retry filter
49
+ //
50
+
51
+ // This filter is intended to be used in the DynamicFilter stack in the
52
+ // client channel, which is situated between the name resolver and the
53
+ // LB policy. Normally, the last filter in the DynamicFilter stack is
54
+ // the DynamicTerminationFilter (see client_channel.cc), which creates a
55
+ // LoadBalancedCall and delegates to it. However, when retries are
56
+ // enabled, this filter is used instead of the DynamicTerminationFilter.
57
+ //
58
+ // In order to support retries, we act as a proxy for stream op batches.
59
+ // When we get a batch from the surface, we add it to our list of pending
60
+ // batches, and we then use those batches to construct separate "child"
61
+ // batches to be started on an LB call. When the child batches return, we
62
+ // then decide which pending batches have been completed and schedule their
63
+ // callbacks accordingly. If a call attempt fails and we want to retry it,
64
+ // we create a new LB call and start again, constructing new "child" batches
65
+ // for the new LB call.
66
+ //
67
+ // Note that retries are committed when receiving data from the server
68
+ // (except for Trailers-Only responses). However, there may be many
69
+ // send ops started before receiving any data, so we may have already
70
+ // completed some number of send ops (and returned the completions up to
71
+ // the surface) by the time we realize that we need to retry. To deal
72
+ // with this, we cache data for send ops, so that we can replay them on a
73
+ // different LB call even after we have completed the original batches.
74
+ //
75
+ // The code is structured as follows:
76
+ // - In CallData (in the parent channel), we maintain a list of pending
77
+ // ops and cached data for send ops.
78
+ // - There is a CallData::CallAttempt object for each retry attempt.
79
+ // This object contains the LB call for that attempt and state to indicate
80
+ // which ops from the CallData object have already been sent down to that
81
+ // LB call.
82
+ // - There is a CallData::CallAttempt::BatchData object for each "child"
83
+ // batch sent on the LB call.
84
+ //
85
+ // When constructing the "child" batches, we compare the state in the
86
+ // CallAttempt object against the state in the CallData object to see
87
+ // which batches need to be sent on the LB call for a given attempt.
88
+
89
+ // TODO(roth): In subsequent PRs:
90
+ // - add support for transparent retries (including initial metadata)
91
+ // - figure out how to record stats in census for retries
92
+ // (census filter is on top of this one)
93
+ // - add census stats for retries
94
+
95
+ // By default, we buffer 256 KiB per RPC for retries.
96
+ // TODO(roth): Do we have any data to suggest a better value?
97
+ #define DEFAULT_PER_RPC_RETRY_BUFFER_SIZE (256 << 10)
98
+
99
+ // This value was picked arbitrarily. It can be changed if there is
100
+ // any even moderately compelling reason to do so.
101
+ #define RETRY_BACKOFF_JITTER 0.2
102
+
103
+ namespace grpc_core {
104
+
105
+ namespace {
106
+
107
+ using internal::RetryGlobalConfig;
108
+ using internal::RetryMethodConfig;
109
+ using internal::RetryServiceConfigParser;
110
+ using internal::ServerRetryThrottleData;
111
+
112
+ TraceFlag grpc_retry_trace(false, "retry");
113
+
114
+ //
115
+ // RetryFilter
116
+ //
117
+
118
+ class RetryFilter {
119
+ public:
120
+ class CallData;
121
+
122
+ static grpc_error_handle Init(grpc_channel_element* elem,
123
+ grpc_channel_element_args* args) {
124
+ GPR_ASSERT(args->is_last);
125
+ GPR_ASSERT(elem->filter == &kRetryFilterVtable);
126
+ grpc_error_handle error = GRPC_ERROR_NONE;
127
+ new (elem->channel_data) RetryFilter(args->channel_args, &error);
128
+ return error;
129
+ }
130
+
131
+ static void Destroy(grpc_channel_element* elem) {
132
+ auto* chand = static_cast<RetryFilter*>(elem->channel_data);
133
+ chand->~RetryFilter();
134
+ }
135
+
136
+ // Will never be called.
137
+ static void StartTransportOp(grpc_channel_element* /*elem*/,
138
+ grpc_transport_op* /*op*/) {}
139
+ static void GetChannelInfo(grpc_channel_element* /*elem*/,
140
+ const grpc_channel_info* /*info*/) {}
141
+
142
+ private:
143
+ static size_t GetMaxPerRpcRetryBufferSize(const grpc_channel_args* args) {
144
+ return static_cast<size_t>(grpc_channel_args_find_integer(
145
+ args, GRPC_ARG_PER_RPC_RETRY_BUFFER_SIZE,
146
+ {DEFAULT_PER_RPC_RETRY_BUFFER_SIZE, 0, INT_MAX}));
147
+ }
148
+
149
+ RetryFilter(const grpc_channel_args* args, grpc_error_handle* error)
150
+ : client_channel_(grpc_channel_args_find_pointer<ClientChannel>(
151
+ args, GRPC_ARG_CLIENT_CHANNEL)),
152
+ per_rpc_retry_buffer_size_(GetMaxPerRpcRetryBufferSize(args)) {
153
+ // Get retry throttling parameters from service config.
154
+ auto* service_config = grpc_channel_args_find_pointer<ServiceConfig>(
155
+ args, GRPC_ARG_SERVICE_CONFIG_OBJ);
156
+ if (service_config == nullptr) return;
157
+ const auto* config = static_cast<const RetryGlobalConfig*>(
158
+ service_config->GetGlobalParsedConfig(
159
+ RetryServiceConfigParser::ParserIndex()));
160
+ if (config == nullptr) return;
161
+ // Get server name from target URI.
162
+ const char* server_uri =
163
+ grpc_channel_args_find_string(args, GRPC_ARG_SERVER_URI);
164
+ if (server_uri == nullptr) {
165
+ *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
166
+ "server URI channel arg missing or wrong type in client channel "
167
+ "filter");
168
+ return;
169
+ }
170
+ absl::StatusOr<URI> uri = URI::Parse(server_uri);
171
+ if (!uri.ok() || uri->path().empty()) {
172
+ *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
173
+ "could not extract server name from target URI");
174
+ return;
175
+ }
176
+ std::string server_name(absl::StripPrefix(uri->path(), "/"));
177
+ // Get throttling config for server_name.
178
+ retry_throttle_data_ = internal::ServerRetryThrottleMap::GetDataForServer(
179
+ server_name, config->max_milli_tokens(), config->milli_token_ratio());
180
+ }
181
+
182
+ ClientChannel* client_channel_;
183
+ size_t per_rpc_retry_buffer_size_;
184
+ RefCountedPtr<ServerRetryThrottleData> retry_throttle_data_;
185
+ };
186
+
187
+ //
188
+ // RetryFilter::CallData
189
+ //
190
+
191
+ class RetryFilter::CallData {
192
+ public:
193
+ static grpc_error_handle Init(grpc_call_element* elem,
194
+ const grpc_call_element_args* args);
195
+ static void Destroy(grpc_call_element* elem,
196
+ const grpc_call_final_info* /*final_info*/,
197
+ grpc_closure* then_schedule_closure);
198
+ static void StartTransportStreamOpBatch(
199
+ grpc_call_element* elem, grpc_transport_stream_op_batch* batch);
200
+ static void SetPollent(grpc_call_element* elem, grpc_polling_entity* pollent);
201
+
202
+ private:
203
+ class CallStackDestructionBarrier;
204
+
205
+ // Pending batches stored in call data.
206
+ struct PendingBatch {
207
+ // The pending batch. If nullptr, this slot is empty.
208
+ grpc_transport_stream_op_batch* batch = nullptr;
209
+ // Indicates whether payload for send ops has been cached in CallData.
210
+ bool send_ops_cached = false;
211
+ };
212
+
213
+ // State associated with each call attempt.
214
+ class CallAttempt : public RefCounted<CallAttempt> {
215
+ public:
216
+ explicit CallAttempt(CallData* calld);
217
+ ~CallAttempt() override;
218
+
219
+ // Constructs and starts whatever batches are needed on this call
220
+ // attempt.
221
+ void StartRetriableBatches();
222
+
223
+ // Frees cached send ops that have already been completed after
224
+ // committing the call.
225
+ void FreeCachedSendOpDataAfterCommit();
226
+
227
+ // Cancels the call attempt.
228
+ void CancelFromSurface(grpc_transport_stream_op_batch* cancel_batch);
229
+
230
+ private:
231
+ // State used for starting a retryable batch on the call attempt's LB call.
232
+ // This provides its own grpc_transport_stream_op_batch and other data
233
+ // structures needed to populate the ops in the batch.
234
+ // We allocate one struct on the arena for each attempt at starting a
235
+ // batch on a given LB call.
236
+ class BatchData
237
+ : public RefCounted<BatchData, PolymorphicRefCount, kUnrefCallDtor> {
238
+ public:
239
+ BatchData(RefCountedPtr<CallAttempt> call_attempt, int refcount,
240
+ bool set_on_complete);
241
+ ~BatchData() override;
242
+
243
+ grpc_transport_stream_op_batch* batch() { return &batch_; }
244
+
245
+ // Adds retriable send_initial_metadata op.
246
+ void AddRetriableSendInitialMetadataOp();
247
+ // Adds retriable send_message op.
248
+ void AddRetriableSendMessageOp();
249
+ // Adds retriable send_trailing_metadata op.
250
+ void AddRetriableSendTrailingMetadataOp();
251
+ // Adds retriable recv_initial_metadata op.
252
+ void AddRetriableRecvInitialMetadataOp();
253
+ // Adds retriable recv_message op.
254
+ void AddRetriableRecvMessageOp();
255
+ // Adds retriable recv_trailing_metadata op.
256
+ void AddRetriableRecvTrailingMetadataOp();
257
+ // Adds cancel_stream op.
258
+ void AddCancelStreamOp();
259
+
260
+ private:
261
+ // Frees cached send ops that were completed by the completed batch in
262
+ // batch_data. Used when batches are completed after the call is
263
+ // committed.
264
+ void FreeCachedSendOpDataForCompletedBatch();
265
+
266
+ // Invokes recv_initial_metadata_ready for a batch.
267
+ static void InvokeRecvInitialMetadataCallback(void* arg,
268
+ grpc_error_handle error);
269
+ // Intercepts recv_initial_metadata_ready callback for retries.
270
+ // Commits the call and returns the initial metadata up the stack.
271
+ static void RecvInitialMetadataReady(void* arg, grpc_error_handle error);
272
+
273
+ // Invokes recv_message_ready for a batch.
274
+ static void InvokeRecvMessageCallback(void* arg, grpc_error_handle error);
275
+ // Intercepts recv_message_ready callback for retries.
276
+ // Commits the call and returns the message up the stack.
277
+ static void RecvMessageReady(void* arg, grpc_error_handle error);
278
+
279
+ // Adds recv_trailing_metadata_ready closure to closures.
280
+ void AddClosureForRecvTrailingMetadataReady(
281
+ grpc_error_handle error, CallCombinerClosureList* closures);
282
+ // Adds any necessary closures for deferred batch completion
283
+ // callbacks to closures.
284
+ void AddClosuresForDeferredCompletionCallbacks(
285
+ CallCombinerClosureList* closures);
286
+ // For any pending batch containing an op that has not yet been started,
287
+ // adds the pending batch's completion closures to closures.
288
+ void AddClosuresToFailUnstartedPendingBatches(
289
+ grpc_error_handle error, CallCombinerClosureList* closures);
290
+ // Runs necessary closures upon completion of a call attempt.
291
+ void RunClosuresForCompletedCall(grpc_error_handle error);
292
+ // Intercepts recv_trailing_metadata_ready callback for retries.
293
+ // Commits the call and returns the trailing metadata up the stack.
294
+ static void RecvTrailingMetadataReady(void* arg, grpc_error_handle error);
295
+
296
+ // Adds the on_complete closure for the pending batch completed in
297
+ // batch_data to closures.
298
+ void AddClosuresForCompletedPendingBatch(
299
+ grpc_error_handle error, CallCombinerClosureList* closures);
300
+
301
+ // If there are any cached ops to replay or pending ops to start on the
302
+ // LB call, adds them to closures.
303
+ void AddClosuresForReplayOrPendingSendOps(
304
+ CallCombinerClosureList* closures);
305
+
306
+ // Callback used to intercept on_complete from LB calls.
307
+ static void OnComplete(void* arg, grpc_error_handle error);
308
+
309
+ RefCountedPtr<CallAttempt> call_attempt_;
310
+ // The batch to use in the LB call.
311
+ // Its payload field points to CallAttempt::batch_payload_.
312
+ grpc_transport_stream_op_batch batch_;
313
+ // For intercepting on_complete.
314
+ grpc_closure on_complete_;
315
+ };
316
+
317
+ // Creates a BatchData object on the call's arena with the
318
+ // specified refcount. If set_on_complete is true, the batch's
319
+ // on_complete callback will be set to point to on_complete();
320
+ // otherwise, the batch's on_complete callback will be null.
321
+ BatchData* CreateBatch(int refcount, bool set_on_complete) {
322
+ return calld_->arena_->New<BatchData>(Ref(DEBUG_LOCATION, "CreateBatch"),
323
+ refcount, set_on_complete);
324
+ }
325
+
326
+ // If there are any cached send ops that need to be replayed on this
327
+ // call attempt, creates and returns a new batch to replay those ops.
328
+ // Otherwise, returns nullptr.
329
+ BatchData* MaybeCreateBatchForReplay();
330
+
331
+ // Adds a closure to closures that will execute batch in the call combiner.
332
+ void AddClosureForBatch(grpc_transport_stream_op_batch* batch,
333
+ const char* reason,
334
+ CallCombinerClosureList* closures);
335
+
336
+ // Adds batches for pending batches to closures.
337
+ void AddBatchesForPendingBatches(CallCombinerClosureList* closures);
338
+
339
+ // Adds whatever batches are needed on this attempt to closures.
340
+ void AddRetriableBatches(CallCombinerClosureList* closures);
341
+
342
+ // Returns true if any op in the batch was not yet started on this attempt.
343
+ bool PendingBatchIsUnstarted(PendingBatch* pending);
344
+
345
+ // Returns true if there are cached send ops to replay.
346
+ bool HaveSendOpsToReplay();
347
+
348
+ // If our retry state is no longer needed, switch to fast path by moving
349
+ // our LB call into calld_->committed_call_ and having calld_ drop
350
+ // its ref to us.
351
+ void MaybeSwitchToFastPath();
352
+
353
+ // Helper function used to start a recv_trailing_metadata batch. This
354
+ // is used in the case where a recv_initial_metadata or recv_message
355
+ // op fails in a way that we know the call is over but when the application
356
+ // has not yet started its own recv_trailing_metadata op.
357
+ void StartInternalRecvTrailingMetadata();
358
+
359
+ // Returns true if the call should be retried.
360
+ // If server_pushback_md is non-null, sets *server_pushback_ms.
361
+ bool ShouldRetry(absl::optional<grpc_status_code> status, bool is_lb_drop,
362
+ grpc_mdelem* server_pushback_md,
363
+ grpc_millis* server_pushback_ms);
364
+
365
+ // Cancels the call attempt. Unrefs any deferred batches.
366
+ // Adds a batch to closures to cancel this call attempt.
367
+ void Cancel(CallCombinerClosureList* closures);
368
+
369
+ static void OnPerAttemptRecvTimer(void* arg, grpc_error_handle error);
370
+ static void OnPerAttemptRecvTimerLocked(void* arg, grpc_error_handle error);
371
+ void MaybeCancelPerAttemptRecvTimer();
372
+
373
+ CallData* calld_;
374
+ RefCountedPtr<ClientChannel::LoadBalancedCall> lb_call_;
375
+
376
+ grpc_timer per_attempt_recv_timer_;
377
+ grpc_closure on_per_attempt_recv_timer_;
378
+ bool per_attempt_recv_timer_pending_ = false;
379
+
380
+ // BatchData.batch.payload points to this.
381
+ grpc_transport_stream_op_batch_payload batch_payload_;
382
+ // For send_initial_metadata.
383
+ // Note that we need to make a copy of the initial metadata for each
384
+ // call attempt instead of just referring to the copy in call_data,
385
+ // because filters in the subchannel stack may modify the metadata,
386
+ // so we need to start in a pristine state for each attempt of the call.
387
+ grpc_linked_mdelem* send_initial_metadata_storage_;
388
+ grpc_metadata_batch send_initial_metadata_;
389
+ // For send_message.
390
+ // TODO(roth): Restructure this to eliminate use of ManualConstructor.
391
+ ManualConstructor<ByteStreamCache::CachingByteStream> send_message_;
392
+ // For send_trailing_metadata.
393
+ grpc_linked_mdelem* send_trailing_metadata_storage_;
394
+ grpc_metadata_batch send_trailing_metadata_;
395
+ // For intercepting recv_initial_metadata.
396
+ grpc_metadata_batch recv_initial_metadata_;
397
+ grpc_closure recv_initial_metadata_ready_;
398
+ bool trailing_metadata_available_ = false;
399
+ // For intercepting recv_message.
400
+ grpc_closure recv_message_ready_;
401
+ OrphanablePtr<ByteStream> recv_message_;
402
+ // For intercepting recv_trailing_metadata.
403
+ grpc_metadata_batch recv_trailing_metadata_;
404
+ grpc_transport_stream_stats collect_stats_;
405
+ grpc_closure recv_trailing_metadata_ready_;
406
+ // These fields indicate which ops have been started and completed on
407
+ // this call attempt.
408
+ size_t started_send_message_count_ = 0;
409
+ size_t completed_send_message_count_ = 0;
410
+ size_t started_recv_message_count_ = 0;
411
+ size_t completed_recv_message_count_ = 0;
412
+ bool started_send_initial_metadata_ : 1;
413
+ bool completed_send_initial_metadata_ : 1;
414
+ bool started_send_trailing_metadata_ : 1;
415
+ bool completed_send_trailing_metadata_ : 1;
416
+ bool started_recv_initial_metadata_ : 1;
417
+ bool completed_recv_initial_metadata_ : 1;
418
+ bool started_recv_trailing_metadata_ : 1;
419
+ bool completed_recv_trailing_metadata_ : 1;
420
+ // State for callback processing.
421
+ RefCountedPtr<BatchData> recv_initial_metadata_ready_deferred_batch_;
422
+ grpc_error_handle recv_initial_metadata_error_ = GRPC_ERROR_NONE;
423
+ RefCountedPtr<BatchData> recv_message_ready_deferred_batch_;
424
+ grpc_error_handle recv_message_error_ = GRPC_ERROR_NONE;
425
+ RefCountedPtr<BatchData> on_complete_deferred_batch_;
426
+ grpc_error_handle on_complete_error_ = GRPC_ERROR_NONE;
427
+ RefCountedPtr<BatchData> recv_trailing_metadata_internal_batch_;
428
+ grpc_error_handle recv_trailing_metadata_error_ = GRPC_ERROR_NONE;
429
+ bool seen_recv_trailing_metadata_from_surface_ : 1;
430
+ // NOTE: Do not move this next to the metadata bitfields above. That would
431
+ // save space but will also result in a data race because compiler
432
+ // will generate a 2 byte store which overwrites the meta-data
433
+ // fields upon setting this field.
434
+ bool cancelled_ : 1;
435
+ };
436
+
437
+ CallData(RetryFilter* chand, const grpc_call_element_args& args);
438
+ ~CallData();
439
+
440
+ void StartTransportStreamOpBatch(grpc_transport_stream_op_batch* batch);
441
+
442
+ // Returns the index into pending_batches_ to be used for batch.
443
+ static size_t GetBatchIndex(grpc_transport_stream_op_batch* batch);
444
+ PendingBatch* PendingBatchesAdd(grpc_transport_stream_op_batch* batch);
445
+ void PendingBatchClear(PendingBatch* pending);
446
+ void MaybeClearPendingBatch(PendingBatch* pending);
447
+ static void FailPendingBatchInCallCombiner(void* arg,
448
+ grpc_error_handle error);
449
+ // Fails all pending batches. Does NOT yield call combiner.
450
+ void PendingBatchesFail(grpc_error_handle error);
451
+ // Returns a pointer to the first pending batch for which predicate(batch)
452
+ // returns true, or null if not found.
453
+ template <typename Predicate>
454
+ PendingBatch* PendingBatchFind(const char* log_message, Predicate predicate);
455
+
456
+ // Caches data for send ops so that it can be retried later, if not
457
+ // already cached.
458
+ void MaybeCacheSendOpsForBatch(PendingBatch* pending);
459
+ void FreeCachedSendInitialMetadata();
460
+ // Frees cached send_message at index idx.
461
+ void FreeCachedSendMessage(size_t idx);
462
+ void FreeCachedSendTrailingMetadata();
463
+ void FreeAllCachedSendOpData();
464
+
465
+ // Commits the call so that no further retry attempts will be performed.
466
+ void RetryCommit(CallAttempt* call_attempt);
467
+
468
+ // Starts a timer to retry after appropriate back-off.
469
+ // If server_pushback_ms is -1, retry_backoff_ is used.
470
+ void StartRetryTimer(grpc_millis server_pushback_ms);
471
+
472
+ static void OnRetryTimer(void* arg, grpc_error_handle error);
473
+ static void OnRetryTimerLocked(void* arg, grpc_error_handle error);
474
+
475
+ RefCountedPtr<ClientChannel::LoadBalancedCall> CreateLoadBalancedCall();
476
+
477
+ void CreateCallAttempt();
478
+
479
+ RetryFilter* chand_;
480
+ grpc_polling_entity* pollent_;
481
+ RefCountedPtr<ServerRetryThrottleData> retry_throttle_data_;
482
+ const RetryMethodConfig* retry_policy_ = nullptr;
483
+ BackOff retry_backoff_;
484
+
485
+ grpc_slice path_; // Request path.
486
+ gpr_cycle_counter call_start_time_;
487
+ grpc_millis deadline_;
488
+ Arena* arena_;
489
+ grpc_call_stack* owning_call_;
490
+ CallCombiner* call_combiner_;
491
+ grpc_call_context_element* call_context_;
492
+
493
+ RefCountedPtr<CallStackDestructionBarrier> call_stack_destruction_barrier_;
494
+
495
+ // TODO(roth): As part of implementing hedging, we will need to maintain a
496
+ // list of all pending attempts, so that we can cancel them all if the call
497
+ // gets cancelled.
498
+ RefCountedPtr<CallAttempt> call_attempt_;
499
+
500
+ // LB call used when we've committed to a call attempt and the retry
501
+ // state for that attempt is no longer needed. This provides a fast
502
+ // path for long-running streaming calls that minimizes overhead.
503
+ RefCountedPtr<ClientChannel::LoadBalancedCall> committed_call_;
504
+
505
+ // When are are not yet fully committed to a particular call (i.e.,
506
+ // either we might still retry or we have committed to the call but
507
+ // there are still some cached ops to be replayed on the call),
508
+ // batches received from above will be added to this list, and they
509
+ // will not be removed until we have invoked their completion callbacks.
510
+ size_t bytes_buffered_for_retry_ = 0;
511
+ PendingBatch pending_batches_[MAX_PENDING_BATCHES];
512
+ bool pending_send_initial_metadata_ : 1;
513
+ bool pending_send_message_ : 1;
514
+ bool pending_send_trailing_metadata_ : 1;
515
+
516
+ // Retry state.
517
+ bool retry_committed_ : 1;
518
+ bool retry_timer_pending_ : 1;
519
+ int num_attempts_completed_ = 0;
520
+ grpc_timer retry_timer_;
521
+ grpc_closure retry_closure_;
522
+
523
+ // Cached data for retrying send ops.
524
+ // send_initial_metadata
525
+ bool seen_send_initial_metadata_ = false;
526
+ grpc_linked_mdelem* send_initial_metadata_storage_ = nullptr;
527
+ grpc_metadata_batch send_initial_metadata_;
528
+ uint32_t send_initial_metadata_flags_;
529
+ // TODO(roth): As part of implementing hedging, we'll probably need to
530
+ // have the LB call set a value in CallAttempt and then propagate it
531
+ // from CallAttempt to the parent call when we commit. Otherwise, we
532
+ // may leave this with a value for a peer other than the one we
533
+ // actually commit to. Alternatively, maybe see if there's a way to
534
+ // change the surface API such that the peer isn't available until
535
+ // after initial metadata is received? (Could even change the
536
+ // transport API to return this with the recv_initial_metadata op.)
537
+ gpr_atm* peer_string_;
538
+ // send_message
539
+ // When we get a send_message op, we replace the original byte stream
540
+ // with a CachingByteStream that caches the slices to a local buffer for
541
+ // use in retries.
542
+ // Note: We inline the cache for the first 3 send_message ops and use
543
+ // dynamic allocation after that. This number was essentially picked
544
+ // at random; it could be changed in the future to tune performance.
545
+ // TODO(roth): As part of implementing hedging, we may need some
546
+ // synchronization here, since ByteStreamCache does not provide any
547
+ // synchronization, so it's not safe to have multiple
548
+ // CachingByteStreams read from the same ByteStreamCache concurrently.
549
+ absl::InlinedVector<ByteStreamCache*, 3> send_messages_;
550
+ // send_trailing_metadata
551
+ bool seen_send_trailing_metadata_ = false;
552
+ grpc_linked_mdelem* send_trailing_metadata_storage_ = nullptr;
553
+ grpc_metadata_batch send_trailing_metadata_;
554
+ };
555
+
556
+ //
557
+ // RetryFilter::CallData::CallStackDestructionBarrier
558
+ //
559
+
560
+ // A class to track the existence of LoadBalancedCall call stacks that
561
+ // we've created. We wait until all such call stacks have been
562
+ // destroyed before we return the on_call_stack_destruction closure up
563
+ // to the surface.
564
+ //
565
+ // The parent RetryFilter::CallData object holds a ref to this object.
566
+ // When it is destroyed, it will store the on_call_stack_destruction
567
+ // closure from the surface in this object and then release its ref.
568
+ // We also take a ref to this object for each LB call we create, and
569
+ // those refs are not released until the LB call stack is destroyed.
570
+ // When this object is destroyed, it will invoke the
571
+ // on_call_stack_destruction closure from the surface.
572
+ class RetryFilter::CallData::CallStackDestructionBarrier
573
+ : public RefCounted<CallStackDestructionBarrier, PolymorphicRefCount,
574
+ kUnrefCallDtor> {
575
+ public:
576
+ CallStackDestructionBarrier() {}
577
+
578
+ ~CallStackDestructionBarrier() override {
579
+ // TODO(yashkt) : This can potentially be a Closure::Run
580
+ ExecCtx::Run(DEBUG_LOCATION, on_call_stack_destruction_, GRPC_ERROR_NONE);
581
+ }
582
+
583
+ // Set the closure from the surface. This closure will be invoked
584
+ // when this object is destroyed.
585
+ void set_on_call_stack_destruction(grpc_closure* on_call_stack_destruction) {
586
+ on_call_stack_destruction_ = on_call_stack_destruction;
587
+ }
588
+
589
+ // Invoked to get an on_call_stack_destruction closure for a new LB call.
590
+ grpc_closure* MakeLbCallDestructionClosure(CallData* calld) {
591
+ Ref().release(); // Ref held by callback.
592
+ grpc_closure* on_lb_call_destruction_complete =
593
+ calld->arena_->New<grpc_closure>();
594
+ GRPC_CLOSURE_INIT(on_lb_call_destruction_complete,
595
+ OnLbCallDestructionComplete, this, nullptr);
596
+ return on_lb_call_destruction_complete;
597
+ }
598
+
599
+ private:
600
+ static void OnLbCallDestructionComplete(void* arg,
601
+ grpc_error_handle /*error*/) {
602
+ auto* self = static_cast<CallStackDestructionBarrier*>(arg);
603
+ self->Unref();
604
+ }
605
+
606
+ grpc_closure* on_call_stack_destruction_ = nullptr;
607
+ };
608
+
609
+ //
610
+ // RetryFilter::CallData::CallAttempt
611
+ //
612
+
613
+ RetryFilter::CallData::CallAttempt::CallAttempt(CallData* calld)
614
+ : RefCounted(GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace) ? "CallAttempt"
615
+ : nullptr),
616
+ calld_(calld),
617
+ batch_payload_(calld->call_context_),
618
+ started_send_initial_metadata_(false),
619
+ completed_send_initial_metadata_(false),
620
+ started_send_trailing_metadata_(false),
621
+ completed_send_trailing_metadata_(false),
622
+ started_recv_initial_metadata_(false),
623
+ completed_recv_initial_metadata_(false),
624
+ started_recv_trailing_metadata_(false),
625
+ completed_recv_trailing_metadata_(false),
626
+ seen_recv_trailing_metadata_from_surface_(false),
627
+ cancelled_(false) {
628
+ lb_call_ = calld->CreateLoadBalancedCall();
629
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
630
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: create lb_call=%p",
631
+ calld->chand_, calld, this, lb_call_.get());
632
+ }
633
+ // If per_attempt_recv_timeout is set, start a timer.
634
+ if (calld->retry_policy_ != nullptr &&
635
+ calld->retry_policy_->per_attempt_recv_timeout().has_value()) {
636
+ grpc_millis per_attempt_recv_deadline =
637
+ ExecCtx::Get()->Now() +
638
+ *calld->retry_policy_->per_attempt_recv_timeout();
639
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
640
+ gpr_log(GPR_INFO,
641
+ "chand=%p calld=%p attempt=%p: per-attempt timeout in %" PRId64
642
+ " ms",
643
+ calld->chand_, calld, this,
644
+ *calld->retry_policy_->per_attempt_recv_timeout());
645
+ }
646
+ // Schedule retry after computed delay.
647
+ GRPC_CLOSURE_INIT(&on_per_attempt_recv_timer_, OnPerAttemptRecvTimer, this,
648
+ nullptr);
649
+ GRPC_CALL_STACK_REF(calld->owning_call_, "OnPerAttemptRecvTimer");
650
+ Ref(DEBUG_LOCATION, "OnPerAttemptRecvTimer").release();
651
+ per_attempt_recv_timer_pending_ = true;
652
+ grpc_timer_init(&per_attempt_recv_timer_, per_attempt_recv_deadline,
653
+ &on_per_attempt_recv_timer_);
654
+ }
655
+ }
656
+
657
+ RetryFilter::CallData::CallAttempt::~CallAttempt() {
658
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
659
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: destroying call attempt",
660
+ calld_->chand_, calld_, this);
661
+ }
662
+ }
663
+
664
+ void RetryFilter::CallData::CallAttempt::FreeCachedSendOpDataAfterCommit() {
665
+ // TODO(roth): When we implement hedging, this logic will need to get
666
+ // a bit more complex, because there may be other (now abandoned) call
667
+ // attempts still using this data. We may need to do some sort of
668
+ // ref-counting instead.
669
+ if (completed_send_initial_metadata_) {
670
+ calld_->FreeCachedSendInitialMetadata();
671
+ }
672
+ for (size_t i = 0; i < completed_send_message_count_; ++i) {
673
+ calld_->FreeCachedSendMessage(i);
674
+ }
675
+ if (completed_send_trailing_metadata_) {
676
+ calld_->FreeCachedSendTrailingMetadata();
677
+ }
678
+ }
679
+
680
+ bool RetryFilter::CallData::CallAttempt::PendingBatchIsUnstarted(
681
+ PendingBatch* pending) {
682
+ // Only look at batches containing send ops, since batches containing
683
+ // only recv ops are always started immediately.
684
+ if (pending->batch == nullptr || pending->batch->on_complete == nullptr) {
685
+ return false;
686
+ }
687
+ if (pending->batch->send_initial_metadata &&
688
+ !started_send_initial_metadata_) {
689
+ return true;
690
+ }
691
+ if (pending->batch->send_message &&
692
+ started_send_message_count_ < calld_->send_messages_.size()) {
693
+ return true;
694
+ }
695
+ if (pending->batch->send_trailing_metadata &&
696
+ !started_send_trailing_metadata_) {
697
+ return true;
698
+ }
699
+ return false;
700
+ }
701
+
702
+ bool RetryFilter::CallData::CallAttempt::HaveSendOpsToReplay() {
703
+ // We don't check send_initial_metadata here, because that op will always
704
+ // be started as soon as it is received from the surface, so it will
705
+ // never need to be started at this point.
706
+ return started_send_message_count_ < calld_->send_messages_.size() ||
707
+ (calld_->seen_send_trailing_metadata_ &&
708
+ !started_send_trailing_metadata_);
709
+ }
710
+
711
+ void RetryFilter::CallData::CallAttempt::MaybeSwitchToFastPath() {
712
+ // If we're not yet committed, we can't switch yet.
713
+ // TODO(roth): As part of implementing hedging, this logic needs to
714
+ // check that *this* call attempt is the one that we've committed to.
715
+ // Might need to replace cancelled_ with an enum indicating whether we're
716
+ // in flight, cancelled, or the winning call attempt.
717
+ if (!calld_->retry_committed_) return;
718
+ // If we've already switched to fast path, there's nothing to do here.
719
+ if (calld_->committed_call_ != nullptr) return;
720
+ // If the perAttemptRecvTimeout timer is pending, we can't switch yet.
721
+ if (per_attempt_recv_timer_pending_) return;
722
+ // If there are still send ops to replay, we can't switch yet.
723
+ if (HaveSendOpsToReplay()) return;
724
+ // If we started an internal batch for recv_trailing_metadata but have not
725
+ // yet seen that op from the surface, we can't switch yet.
726
+ if (recv_trailing_metadata_internal_batch_ != nullptr) return;
727
+ // Switch to fast path.
728
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
729
+ gpr_log(GPR_INFO,
730
+ "chand=%p calld=%p attempt=%p: retry state no longer needed; "
731
+ "moving LB call to parent and unreffing the call attempt",
732
+ calld_->chand_, calld_, this);
733
+ }
734
+ calld_->committed_call_ = std::move(lb_call_);
735
+ calld_->call_attempt_.reset(DEBUG_LOCATION, "MaybeSwitchToFastPath");
736
+ }
737
+
738
+ void RetryFilter::CallData::CallAttempt::StartInternalRecvTrailingMetadata() {
739
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
740
+ gpr_log(GPR_INFO,
741
+ "chand=%p calld=%p attempt=%p: call failed but "
742
+ "recv_trailing_metadata not started; starting it internally",
743
+ calld_->chand_, calld_, this);
744
+ }
745
+ // Create batch_data with 2 refs, since this batch will be unreffed twice:
746
+ // once for the recv_trailing_metadata_ready callback when the batch
747
+ // completes, and again when we actually get a recv_trailing_metadata
748
+ // op from the surface.
749
+ BatchData* batch_data = CreateBatch(2, false /* set_on_complete */);
750
+ batch_data->AddRetriableRecvTrailingMetadataOp();
751
+ recv_trailing_metadata_internal_batch_.reset(batch_data);
752
+ // Note: This will release the call combiner.
753
+ lb_call_->StartTransportStreamOpBatch(batch_data->batch());
754
+ }
755
+
756
+ // If there are any cached send ops that need to be replayed on the
757
+ // current call attempt, creates and returns a new batch to replay those ops.
758
+ // Otherwise, returns nullptr.
759
+ RetryFilter::CallData::CallAttempt::BatchData*
760
+ RetryFilter::CallData::CallAttempt::MaybeCreateBatchForReplay() {
761
+ BatchData* replay_batch_data = nullptr;
762
+ // send_initial_metadata.
763
+ if (calld_->seen_send_initial_metadata_ && !started_send_initial_metadata_ &&
764
+ !calld_->pending_send_initial_metadata_) {
765
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
766
+ gpr_log(GPR_INFO,
767
+ "chand=%p calld=%p attempt=%p: replaying previously completed "
768
+ "send_initial_metadata op",
769
+ calld_->chand_, calld_, this);
770
+ }
771
+ replay_batch_data = CreateBatch(1, true /* set_on_complete */);
772
+ replay_batch_data->AddRetriableSendInitialMetadataOp();
773
+ }
774
+ // send_message.
775
+ // Note that we can only have one send_message op in flight at a time.
776
+ if (started_send_message_count_ < calld_->send_messages_.size() &&
777
+ started_send_message_count_ == completed_send_message_count_ &&
778
+ !calld_->pending_send_message_) {
779
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
780
+ gpr_log(GPR_INFO,
781
+ "chand=%p calld=%p attempt=%p: replaying previously completed "
782
+ "send_message op",
783
+ calld_->chand_, calld_, this);
784
+ }
785
+ if (replay_batch_data == nullptr) {
786
+ replay_batch_data = CreateBatch(1, true /* set_on_complete */);
787
+ }
788
+ replay_batch_data->AddRetriableSendMessageOp();
789
+ }
790
+ // send_trailing_metadata.
791
+ // Note that we only add this op if we have no more send_message ops
792
+ // to start, since we can't send down any more send_message ops after
793
+ // send_trailing_metadata.
794
+ if (calld_->seen_send_trailing_metadata_ &&
795
+ started_send_message_count_ == calld_->send_messages_.size() &&
796
+ !started_send_trailing_metadata_ &&
797
+ !calld_->pending_send_trailing_metadata_) {
798
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
799
+ gpr_log(GPR_INFO,
800
+ "chand=%p calld=%p attempt=%p: replaying previously completed "
801
+ "send_trailing_metadata op",
802
+ calld_->chand_, calld_, this);
803
+ }
804
+ if (replay_batch_data == nullptr) {
805
+ replay_batch_data = CreateBatch(1, true /* set_on_complete */);
806
+ }
807
+ replay_batch_data->AddRetriableSendTrailingMetadataOp();
808
+ }
809
+ return replay_batch_data;
810
+ }
811
+
812
+ namespace {
813
+
814
+ void StartBatchInCallCombiner(void* arg, grpc_error_handle /*ignored*/) {
815
+ grpc_transport_stream_op_batch* batch =
816
+ static_cast<grpc_transport_stream_op_batch*>(arg);
817
+ auto* lb_call = static_cast<ClientChannel::LoadBalancedCall*>(
818
+ batch->handler_private.extra_arg);
819
+ // Note: This will release the call combiner.
820
+ lb_call->StartTransportStreamOpBatch(batch);
821
+ }
822
+
823
+ } // namespace
824
+
825
+ void RetryFilter::CallData::CallAttempt::AddClosureForBatch(
826
+ grpc_transport_stream_op_batch* batch, const char* reason,
827
+ CallCombinerClosureList* closures) {
828
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
829
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: adding batch (%s): %s",
830
+ calld_->chand_, calld_, this, reason,
831
+ grpc_transport_stream_op_batch_string(batch).c_str());
832
+ }
833
+ batch->handler_private.extra_arg = lb_call_.get();
834
+ GRPC_CLOSURE_INIT(&batch->handler_private.closure, StartBatchInCallCombiner,
835
+ batch, grpc_schedule_on_exec_ctx);
836
+ closures->Add(&batch->handler_private.closure, GRPC_ERROR_NONE, reason);
837
+ }
838
+
839
+ void RetryFilter::CallData::CallAttempt::AddBatchesForPendingBatches(
840
+ CallCombinerClosureList* closures) {
841
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(calld_->pending_batches_); ++i) {
842
+ PendingBatch* pending = &calld_->pending_batches_[i];
843
+ grpc_transport_stream_op_batch* batch = pending->batch;
844
+ if (batch == nullptr) continue;
845
+ // Skip any batch that either (a) has already been started on this
846
+ // call attempt or (b) we can't start yet because we're still
847
+ // replaying send ops that need to be completed first.
848
+ // TODO(roth): Note that if any one op in the batch can't be sent
849
+ // yet due to ops that we're replaying, we don't start any of the ops
850
+ // in the batch. This is probably okay, but it could conceivably
851
+ // lead to increased latency in some cases -- e.g., we could delay
852
+ // starting a recv op due to it being in the same batch with a send
853
+ // op. If/when we revamp the callback protocol in
854
+ // transport_stream_op_batch, we may be able to fix this.
855
+ if (batch->send_initial_metadata && started_send_initial_metadata_) {
856
+ continue;
857
+ }
858
+ if (batch->send_message &&
859
+ completed_send_message_count_ < started_send_message_count_) {
860
+ continue;
861
+ }
862
+ // Note that we only start send_trailing_metadata if we have no more
863
+ // send_message ops to start, since we can't send down any more
864
+ // send_message ops after send_trailing_metadata.
865
+ if (batch->send_trailing_metadata &&
866
+ (started_send_message_count_ + batch->send_message <
867
+ calld_->send_messages_.size() ||
868
+ started_send_trailing_metadata_)) {
869
+ continue;
870
+ }
871
+ if (batch->recv_initial_metadata && started_recv_initial_metadata_) {
872
+ continue;
873
+ }
874
+ if (batch->recv_message &&
875
+ completed_recv_message_count_ < started_recv_message_count_) {
876
+ continue;
877
+ }
878
+ if (batch->recv_trailing_metadata && started_recv_trailing_metadata_) {
879
+ seen_recv_trailing_metadata_from_surface_ = true;
880
+ // If we previously completed a recv_trailing_metadata op
881
+ // initiated by StartInternalRecvTrailingMetadata(), use the
882
+ // result of that instead of trying to re-start this op.
883
+ if (GPR_UNLIKELY(recv_trailing_metadata_internal_batch_ != nullptr)) {
884
+ // If the batch completed, then trigger the completion callback
885
+ // directly, so that we return the previously returned results to
886
+ // the application. Otherwise, just unref the internally started
887
+ // batch, since we'll propagate the completion when it completes.
888
+ if (completed_recv_trailing_metadata_) {
889
+ closures->Add(
890
+ &recv_trailing_metadata_ready_, recv_trailing_metadata_error_,
891
+ "re-executing recv_trailing_metadata_ready to propagate "
892
+ "internally triggered result");
893
+ // Ref will be released by callback.
894
+ recv_trailing_metadata_internal_batch_.release();
895
+ } else {
896
+ recv_trailing_metadata_internal_batch_.reset(
897
+ DEBUG_LOCATION,
898
+ "internally started recv_trailing_metadata batch pending and "
899
+ "recv_trailing_metadata started from surface");
900
+ GRPC_ERROR_UNREF(recv_trailing_metadata_error_);
901
+ }
902
+ recv_trailing_metadata_error_ = GRPC_ERROR_NONE;
903
+ }
904
+ continue;
905
+ }
906
+ // If we're already committed and these send ops aren't cached, just send
907
+ // the batch as-is.
908
+ if (calld_->retry_committed_ && !pending->send_ops_cached) {
909
+ AddClosureForBatch(
910
+ batch,
911
+ "start non-replayable pending batch on call attempt after commit",
912
+ closures);
913
+ calld_->PendingBatchClear(pending);
914
+ continue;
915
+ }
916
+ // Create batch with the right number of callbacks.
917
+ const bool has_send_ops = batch->send_initial_metadata ||
918
+ batch->send_message ||
919
+ batch->send_trailing_metadata;
920
+ const int num_callbacks = has_send_ops + batch->recv_initial_metadata +
921
+ batch->recv_message +
922
+ batch->recv_trailing_metadata;
923
+ BatchData* batch_data =
924
+ CreateBatch(num_callbacks, has_send_ops /* set_on_complete */);
925
+ // Cache send ops if needed.
926
+ calld_->MaybeCacheSendOpsForBatch(pending);
927
+ // send_initial_metadata.
928
+ if (batch->send_initial_metadata) {
929
+ batch_data->AddRetriableSendInitialMetadataOp();
930
+ }
931
+ // send_message.
932
+ if (batch->send_message) {
933
+ batch_data->AddRetriableSendMessageOp();
934
+ }
935
+ // send_trailing_metadata.
936
+ if (batch->send_trailing_metadata) {
937
+ batch_data->AddRetriableSendTrailingMetadataOp();
938
+ }
939
+ // recv_initial_metadata.
940
+ if (batch->recv_initial_metadata) {
941
+ // recv_flags is only used on the server side.
942
+ GPR_ASSERT(batch->payload->recv_initial_metadata.recv_flags == nullptr);
943
+ batch_data->AddRetriableRecvInitialMetadataOp();
944
+ }
945
+ // recv_message.
946
+ if (batch->recv_message) {
947
+ batch_data->AddRetriableRecvMessageOp();
948
+ }
949
+ // recv_trailing_metadata.
950
+ if (batch->recv_trailing_metadata) {
951
+ batch_data->AddRetriableRecvTrailingMetadataOp();
952
+ }
953
+ AddClosureForBatch(batch_data->batch(),
954
+ "start replayable pending batch on call attempt",
955
+ closures);
956
+ }
957
+ }
958
+
959
+ void RetryFilter::CallData::CallAttempt::AddRetriableBatches(
960
+ CallCombinerClosureList* closures) {
961
+ // Replay previously-returned send_* ops if needed.
962
+ BatchData* replay_batch_data = MaybeCreateBatchForReplay();
963
+ if (replay_batch_data != nullptr) {
964
+ AddClosureForBatch(replay_batch_data->batch(),
965
+ "start replay batch on call attempt", closures);
966
+ }
967
+ // Now add pending batches.
968
+ AddBatchesForPendingBatches(closures);
969
+ }
970
+
971
+ void RetryFilter::CallData::CallAttempt::StartRetriableBatches() {
972
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
973
+ gpr_log(GPR_INFO,
974
+ "chand=%p calld=%p attempt=%p: constructing retriable batches",
975
+ calld_->chand_, calld_, this);
976
+ }
977
+ // Construct list of closures to execute, one for each pending batch.
978
+ CallCombinerClosureList closures;
979
+ AddRetriableBatches(&closures);
980
+ // Note: This will yield the call combiner.
981
+ // Start batches on LB call.
982
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
983
+ gpr_log(GPR_INFO,
984
+ "chand=%p calld=%p attempt=%p: starting %" PRIuPTR
985
+ " retriable batches on lb_call=%p",
986
+ calld_->chand_, calld_, this, closures.size(), lb_call_.get());
987
+ }
988
+ closures.RunClosures(calld_->call_combiner_);
989
+ }
990
+
991
+ void RetryFilter::CallData::CallAttempt::CancelFromSurface(
992
+ grpc_transport_stream_op_batch* cancel_batch) {
993
+ MaybeCancelPerAttemptRecvTimer();
994
+ // Propagate cancellation to LB call.
995
+ lb_call_->StartTransportStreamOpBatch(cancel_batch);
996
+ }
997
+
998
+ bool RetryFilter::CallData::CallAttempt::ShouldRetry(
999
+ absl::optional<grpc_status_code> status, bool is_lb_drop,
1000
+ grpc_mdelem* server_pushback_md, grpc_millis* server_pushback_ms) {
1001
+ // LB drops always inhibit retries.
1002
+ if (is_lb_drop) return false;
1003
+ // TODO(roth): Handle transparent retries here.
1004
+ // If no retry policy, don't retry.
1005
+ if (calld_->retry_policy_ == nullptr) return false;
1006
+ // Check status.
1007
+ if (status.has_value()) {
1008
+ if (GPR_LIKELY(*status == GRPC_STATUS_OK)) {
1009
+ if (calld_->retry_throttle_data_ != nullptr) {
1010
+ calld_->retry_throttle_data_->RecordSuccess();
1011
+ }
1012
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1013
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: call succeeded",
1014
+ calld_->chand_, calld_, this);
1015
+ }
1016
+ return false;
1017
+ }
1018
+ // Status is not OK. Check whether the status is retryable.
1019
+ if (!calld_->retry_policy_->retryable_status_codes().Contains(*status)) {
1020
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1021
+ gpr_log(GPR_INFO,
1022
+ "chand=%p calld=%p attempt=%p: status %s not configured as "
1023
+ "retryable",
1024
+ calld_->chand_, calld_, this,
1025
+ grpc_status_code_to_string(*status));
1026
+ }
1027
+ return false;
1028
+ }
1029
+ }
1030
+ // Record the failure and check whether retries are throttled.
1031
+ // Note that it's important for this check to come after the status
1032
+ // code check above, since we should only record failures whose statuses
1033
+ // match the configured retryable status codes, so that we don't count
1034
+ // things like failures due to malformed requests (INVALID_ARGUMENT).
1035
+ // Conversely, it's important for this to come before the remaining
1036
+ // checks, so that we don't fail to record failures due to other factors.
1037
+ if (calld_->retry_throttle_data_ != nullptr &&
1038
+ !calld_->retry_throttle_data_->RecordFailure()) {
1039
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1040
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: retries throttled",
1041
+ calld_->chand_, calld_, this);
1042
+ }
1043
+ return false;
1044
+ }
1045
+ // Check whether the call is committed.
1046
+ if (calld_->retry_committed_) {
1047
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1048
+ gpr_log(GPR_INFO,
1049
+ "chand=%p calld=%p attempt=%p: retries already committed",
1050
+ calld_->chand_, calld_, this);
1051
+ }
1052
+ return false;
1053
+ }
1054
+ // Check whether we have retries remaining.
1055
+ ++calld_->num_attempts_completed_;
1056
+ if (calld_->num_attempts_completed_ >=
1057
+ calld_->retry_policy_->max_attempts()) {
1058
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1059
+ gpr_log(
1060
+ GPR_INFO, "chand=%p calld=%p attempt=%p: exceeded %d retry attempts",
1061
+ calld_->chand_, calld_, this, calld_->retry_policy_->max_attempts());
1062
+ }
1063
+ return false;
1064
+ }
1065
+ // Check server push-back.
1066
+ if (server_pushback_md != nullptr) {
1067
+ // If the value is "-1" or any other unparseable string, we do not retry.
1068
+ uint32_t ms;
1069
+ if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(*server_pushback_md), &ms)) {
1070
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1071
+ gpr_log(GPR_INFO,
1072
+ "chand=%p calld=%p attempt=%p: not retrying due to server "
1073
+ "push-back",
1074
+ calld_->chand_, calld_, this);
1075
+ }
1076
+ return false;
1077
+ } else {
1078
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1079
+ gpr_log(
1080
+ GPR_INFO,
1081
+ "chand=%p calld=%p attempt=%p: server push-back: retry in %u ms",
1082
+ calld_->chand_, calld_, this, ms);
1083
+ }
1084
+ *server_pushback_ms = static_cast<grpc_millis>(ms);
1085
+ }
1086
+ }
1087
+ // We should retry.
1088
+ return true;
1089
+ }
1090
+
1091
+ void RetryFilter::CallData::CallAttempt::Cancel(
1092
+ CallCombinerClosureList* closures) {
1093
+ // Record that this attempt has been cancelled.
1094
+ cancelled_ = true;
1095
+ // Unref batches for deferred completion callbacks that will now never
1096
+ // be invoked.
1097
+ if (started_recv_trailing_metadata_ &&
1098
+ !seen_recv_trailing_metadata_from_surface_) {
1099
+ recv_trailing_metadata_internal_batch_.reset(
1100
+ DEBUG_LOCATION,
1101
+ "internal recv_trailing_metadata completed before that op was "
1102
+ "started from the surface");
1103
+ }
1104
+ GRPC_ERROR_UNREF(recv_trailing_metadata_error_);
1105
+ recv_trailing_metadata_error_ = GRPC_ERROR_NONE;
1106
+ recv_initial_metadata_ready_deferred_batch_.reset(
1107
+ DEBUG_LOCATION,
1108
+ "unref deferred recv_initial_metadata_ready batch due to retry");
1109
+ GRPC_ERROR_UNREF(recv_initial_metadata_error_);
1110
+ recv_initial_metadata_error_ = GRPC_ERROR_NONE;
1111
+ recv_message_ready_deferred_batch_.reset(
1112
+ DEBUG_LOCATION, "unref deferred recv_message_ready batch due to retry");
1113
+ GRPC_ERROR_UNREF(recv_message_error_);
1114
+ recv_message_error_ = GRPC_ERROR_NONE;
1115
+ on_complete_deferred_batch_.reset(
1116
+ DEBUG_LOCATION, "unref deferred on_complete batch due to retry");
1117
+ GRPC_ERROR_UNREF(on_complete_error_);
1118
+ on_complete_error_ = GRPC_ERROR_NONE;
1119
+ // Start a cancellation op on this call attempt to make sure the
1120
+ // transport knows that this call should be cleaned up, even if it
1121
+ // hasn't received any ops.
1122
+ BatchData* cancel_batch_data = CreateBatch(1, /*set_on_complete=*/true);
1123
+ cancel_batch_data->AddCancelStreamOp();
1124
+ AddClosureForBatch(cancel_batch_data->batch(),
1125
+ "start cancellation batch on call attempt", closures);
1126
+ }
1127
+
1128
+ void RetryFilter::CallData::CallAttempt::OnPerAttemptRecvTimer(
1129
+ void* arg, grpc_error_handle error) {
1130
+ auto* call_attempt = static_cast<CallAttempt*>(arg);
1131
+ GRPC_CLOSURE_INIT(&call_attempt->on_per_attempt_recv_timer_,
1132
+ OnPerAttemptRecvTimerLocked, call_attempt, nullptr);
1133
+ GRPC_CALL_COMBINER_START(call_attempt->calld_->call_combiner_,
1134
+ &call_attempt->on_per_attempt_recv_timer_,
1135
+ GRPC_ERROR_REF(error), "per-attempt timer fired");
1136
+ }
1137
+
1138
+ void RetryFilter::CallData::CallAttempt::OnPerAttemptRecvTimerLocked(
1139
+ void* arg, grpc_error_handle error) {
1140
+ auto* call_attempt = static_cast<CallAttempt*>(arg);
1141
+ auto* calld = call_attempt->calld_;
1142
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1143
+ gpr_log(GPR_INFO,
1144
+ "chand=%p calld=%p attempt=%p: perAttemptRecvTimeout timer fired: "
1145
+ "error=%s, per_attempt_recv_timer_pending_=%d",
1146
+ calld->chand_, calld, call_attempt,
1147
+ grpc_error_std_string(error).c_str(),
1148
+ call_attempt->per_attempt_recv_timer_pending_);
1149
+ }
1150
+ CallCombinerClosureList closures;
1151
+ if (error == GRPC_ERROR_NONE &&
1152
+ call_attempt->per_attempt_recv_timer_pending_) {
1153
+ call_attempt->per_attempt_recv_timer_pending_ = false;
1154
+ // Cancel this attempt.
1155
+ // TODO(roth): When implementing hedging, we should not cancel the
1156
+ // current attempt.
1157
+ call_attempt->Cancel(&closures);
1158
+ // Check whether we should retry.
1159
+ if (call_attempt->ShouldRetry(
1160
+ /*status=*/absl::nullopt, /*is_lb_drop=*/false,
1161
+ /*server_pushback_md=*/nullptr, /*server_pushback_ms=*/nullptr)) {
1162
+ // We are retrying. Start backoff timer.
1163
+ calld->StartRetryTimer(/*server_pushback_ms=*/-1);
1164
+ } else {
1165
+ // Not retrying, so commit the call.
1166
+ calld->RetryCommit(call_attempt);
1167
+ // If retry state is no longer needed, switch to fast path for
1168
+ // subsequent batches.
1169
+ call_attempt->MaybeSwitchToFastPath();
1170
+ }
1171
+ }
1172
+ closures.RunClosures(calld->call_combiner_);
1173
+ call_attempt->Unref(DEBUG_LOCATION, "OnPerAttemptRecvTimer");
1174
+ GRPC_CALL_STACK_UNREF(calld->owning_call_, "OnPerAttemptRecvTimer");
1175
+ }
1176
+
1177
+ void RetryFilter::CallData::CallAttempt::MaybeCancelPerAttemptRecvTimer() {
1178
+ if (per_attempt_recv_timer_pending_) {
1179
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1180
+ gpr_log(GPR_INFO,
1181
+ "chand=%p calld=%p attempt=%p: cancelling "
1182
+ "perAttemptRecvTimeout timer",
1183
+ calld_->chand_, calld_, this);
1184
+ }
1185
+ per_attempt_recv_timer_pending_ = false;
1186
+ grpc_timer_cancel(&per_attempt_recv_timer_);
1187
+ }
1188
+ }
1189
+
1190
+ //
1191
+ // RetryFilter::CallData::CallAttempt::BatchData
1192
+ //
1193
+
1194
+ RetryFilter::CallData::CallAttempt::BatchData::BatchData(
1195
+ RefCountedPtr<CallAttempt> attempt, int refcount, bool set_on_complete)
1196
+ : RefCounted(
1197
+ GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace) ? "BatchData" : nullptr,
1198
+ refcount),
1199
+ call_attempt_(std::move(attempt)) {
1200
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1201
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: creating batch %p",
1202
+ call_attempt_->calld_->chand_, call_attempt_->calld_,
1203
+ call_attempt_.get(), this);
1204
+ }
1205
+ // We hold a ref to the call stack for every batch sent on a call attempt.
1206
+ // This is because some batches on the call attempt may not complete
1207
+ // until after all of the batches are completed at the surface (because
1208
+ // each batch that is pending at the surface holds a ref). This
1209
+ // can happen for replayed send ops, and it can happen for
1210
+ // recv_initial_metadata and recv_message ops on a call attempt that has
1211
+ // been abandoned.
1212
+ GRPC_CALL_STACK_REF(call_attempt_->calld_->owning_call_, "Retry BatchData");
1213
+ batch_.payload = &call_attempt_->batch_payload_;
1214
+ if (set_on_complete) {
1215
+ GRPC_CLOSURE_INIT(&on_complete_, OnComplete, this,
1216
+ grpc_schedule_on_exec_ctx);
1217
+ batch_.on_complete = &on_complete_;
1218
+ }
1219
+ }
1220
+
1221
+ RetryFilter::CallData::CallAttempt::BatchData::~BatchData() {
1222
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1223
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: destroying batch %p",
1224
+ call_attempt_->calld_->chand_, call_attempt_->calld_,
1225
+ call_attempt_.get(), this);
1226
+ }
1227
+ if (batch_.send_initial_metadata) {
1228
+ grpc_metadata_batch_destroy(&call_attempt_->send_initial_metadata_);
1229
+ }
1230
+ if (batch_.send_trailing_metadata) {
1231
+ grpc_metadata_batch_destroy(&call_attempt_->send_trailing_metadata_);
1232
+ }
1233
+ if (batch_.recv_initial_metadata) {
1234
+ grpc_metadata_batch_destroy(&call_attempt_->recv_initial_metadata_);
1235
+ }
1236
+ if (batch_.recv_trailing_metadata) {
1237
+ grpc_metadata_batch_destroy(&call_attempt_->recv_trailing_metadata_);
1238
+ }
1239
+ GRPC_CALL_STACK_UNREF(call_attempt_->calld_->owning_call_, "Retry BatchData");
1240
+ call_attempt_.reset(DEBUG_LOCATION, "~BatchData");
1241
+ }
1242
+
1243
+ void RetryFilter::CallData::CallAttempt::BatchData::
1244
+ FreeCachedSendOpDataForCompletedBatch() {
1245
+ auto* calld = call_attempt_->calld_;
1246
+ // TODO(roth): When we implement hedging, this logic will need to get
1247
+ // a bit more complex, because there may be other (now abandoned) call
1248
+ // attempts still using this data. We may need to do some sort of
1249
+ // ref-counting instead.
1250
+ if (batch_.send_initial_metadata) {
1251
+ calld->FreeCachedSendInitialMetadata();
1252
+ }
1253
+ if (batch_.send_message) {
1254
+ calld->FreeCachedSendMessage(call_attempt_->completed_send_message_count_ -
1255
+ 1);
1256
+ }
1257
+ if (batch_.send_trailing_metadata) {
1258
+ calld->FreeCachedSendTrailingMetadata();
1259
+ }
1260
+ }
1261
+
1262
+ //
1263
+ // recv_initial_metadata callback handling
1264
+ //
1265
+
1266
+ void RetryFilter::CallData::CallAttempt::BatchData::
1267
+ InvokeRecvInitialMetadataCallback(void* arg, grpc_error_handle error) {
1268
+ auto* batch_data = static_cast<BatchData*>(arg);
1269
+ auto* call_attempt = batch_data->call_attempt_.get();
1270
+ // Find pending batch.
1271
+ PendingBatch* pending = call_attempt->calld_->PendingBatchFind(
1272
+ "invoking recv_initial_metadata_ready for",
1273
+ [](grpc_transport_stream_op_batch* batch) {
1274
+ return batch->recv_initial_metadata &&
1275
+ batch->payload->recv_initial_metadata
1276
+ .recv_initial_metadata_ready != nullptr;
1277
+ });
1278
+ GPR_ASSERT(pending != nullptr);
1279
+ // Return metadata.
1280
+ grpc_metadata_batch_move(
1281
+ &call_attempt->recv_initial_metadata_,
1282
+ pending->batch->payload->recv_initial_metadata.recv_initial_metadata);
1283
+ // Update bookkeeping.
1284
+ // Note: Need to do this before invoking the callback, since invoking
1285
+ // the callback will result in yielding the call combiner.
1286
+ grpc_closure* recv_initial_metadata_ready =
1287
+ pending->batch->payload->recv_initial_metadata
1288
+ .recv_initial_metadata_ready;
1289
+ pending->batch->payload->recv_initial_metadata.recv_initial_metadata_ready =
1290
+ nullptr;
1291
+ call_attempt->calld_->MaybeClearPendingBatch(pending);
1292
+ batch_data->Unref();
1293
+ // Invoke callback.
1294
+ Closure::Run(DEBUG_LOCATION, recv_initial_metadata_ready,
1295
+ GRPC_ERROR_REF(error));
1296
+ }
1297
+
1298
+ void RetryFilter::CallData::CallAttempt::BatchData::RecvInitialMetadataReady(
1299
+ void* arg, grpc_error_handle error) {
1300
+ RefCountedPtr<BatchData> batch_data(static_cast<BatchData*>(arg));
1301
+ CallAttempt* call_attempt = batch_data->call_attempt_.get();
1302
+ CallData* calld = call_attempt->calld_;
1303
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1304
+ gpr_log(GPR_INFO,
1305
+ "chand=%p calld=%p attempt=%p: got recv_initial_metadata_ready, "
1306
+ "error=%s",
1307
+ calld->chand_, calld, call_attempt,
1308
+ grpc_error_std_string(error).c_str());
1309
+ }
1310
+ call_attempt->completed_recv_initial_metadata_ = true;
1311
+ // If this attempt has been cancelled, then we're not going to use the
1312
+ // result of this recv_initial_metadata op, so do nothing.
1313
+ if (call_attempt->cancelled_) {
1314
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
1315
+ "recv_initial_metadata_ready after cancellation");
1316
+ return;
1317
+ }
1318
+ // Cancel per-attempt recv timer, if any.
1319
+ call_attempt->MaybeCancelPerAttemptRecvTimer();
1320
+ // If we're not committed, check the response to see if we need to commit.
1321
+ if (!calld->retry_committed_) {
1322
+ // If we got an error or a Trailers-Only response and have not yet gotten
1323
+ // the recv_trailing_metadata_ready callback, then defer propagating this
1324
+ // callback back to the surface. We can evaluate whether to retry when
1325
+ // recv_trailing_metadata comes back.
1326
+ if (GPR_UNLIKELY((call_attempt->trailing_metadata_available_ ||
1327
+ error != GRPC_ERROR_NONE) &&
1328
+ !call_attempt->completed_recv_trailing_metadata_)) {
1329
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1330
+ gpr_log(GPR_INFO,
1331
+ "chand=%p calld=%p attempt=%p: deferring "
1332
+ "recv_initial_metadata_ready (Trailers-Only)",
1333
+ calld->chand_, calld, call_attempt);
1334
+ }
1335
+ call_attempt->recv_initial_metadata_ready_deferred_batch_ =
1336
+ std::move(batch_data);
1337
+ call_attempt->recv_initial_metadata_error_ = GRPC_ERROR_REF(error);
1338
+ if (!call_attempt->started_recv_trailing_metadata_) {
1339
+ // recv_trailing_metadata not yet started by application; start it
1340
+ // ourselves to get status.
1341
+ call_attempt->StartInternalRecvTrailingMetadata();
1342
+ } else {
1343
+ GRPC_CALL_COMBINER_STOP(
1344
+ calld->call_combiner_,
1345
+ "recv_initial_metadata_ready trailers-only or error");
1346
+ }
1347
+ return;
1348
+ }
1349
+ // Received valid initial metadata, so commit the call.
1350
+ calld->RetryCommit(call_attempt);
1351
+ // If retry state is no longer needed, switch to fast path for
1352
+ // subsequent batches.
1353
+ call_attempt->MaybeSwitchToFastPath();
1354
+ }
1355
+ // Invoke the callback to return the result to the surface.
1356
+ // Manually invoking a callback function; it does not take ownership of error.
1357
+ InvokeRecvInitialMetadataCallback(batch_data.release(), error);
1358
+ }
1359
+
1360
+ //
1361
+ // recv_message callback handling
1362
+ //
1363
+
1364
+ void RetryFilter::CallData::CallAttempt::BatchData::InvokeRecvMessageCallback(
1365
+ void* arg, grpc_error_handle error) {
1366
+ auto* batch_data = static_cast<BatchData*>(arg);
1367
+ CallAttempt* call_attempt = batch_data->call_attempt_.get();
1368
+ CallData* calld = call_attempt->calld_;
1369
+ // Find pending op.
1370
+ PendingBatch* pending = calld->PendingBatchFind(
1371
+ "invoking recv_message_ready for",
1372
+ [](grpc_transport_stream_op_batch* batch) {
1373
+ return batch->recv_message &&
1374
+ batch->payload->recv_message.recv_message_ready != nullptr;
1375
+ });
1376
+ GPR_ASSERT(pending != nullptr);
1377
+ // Return payload.
1378
+ *pending->batch->payload->recv_message.recv_message =
1379
+ std::move(call_attempt->recv_message_);
1380
+ // Update bookkeeping.
1381
+ // Note: Need to do this before invoking the callback, since invoking
1382
+ // the callback will result in yielding the call combiner.
1383
+ grpc_closure* recv_message_ready =
1384
+ pending->batch->payload->recv_message.recv_message_ready;
1385
+ pending->batch->payload->recv_message.recv_message_ready = nullptr;
1386
+ calld->MaybeClearPendingBatch(pending);
1387
+ batch_data->Unref();
1388
+ // Invoke callback.
1389
+ Closure::Run(DEBUG_LOCATION, recv_message_ready, GRPC_ERROR_REF(error));
1390
+ }
1391
+
1392
+ void RetryFilter::CallData::CallAttempt::BatchData::RecvMessageReady(
1393
+ void* arg, grpc_error_handle error) {
1394
+ RefCountedPtr<BatchData> batch_data(static_cast<BatchData*>(arg));
1395
+ CallAttempt* call_attempt = batch_data->call_attempt_.get();
1396
+ CallData* calld = call_attempt->calld_;
1397
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1398
+ gpr_log(GPR_INFO,
1399
+ "chand=%p calld=%p attempt=%p: got recv_message_ready, error=%s",
1400
+ calld->chand_, calld, call_attempt,
1401
+ grpc_error_std_string(error).c_str());
1402
+ }
1403
+ ++call_attempt->completed_recv_message_count_;
1404
+ // If this attempt has been cancelled, then we're not going to use the
1405
+ // result of this recv_message op, so do nothing.
1406
+ if (call_attempt->cancelled_) {
1407
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
1408
+ "recv_message_ready after cancellation");
1409
+ return;
1410
+ }
1411
+ // Cancel per-attempt recv timer, if any.
1412
+ call_attempt->MaybeCancelPerAttemptRecvTimer();
1413
+ // If we're not committed, check the response to see if we need to commit.
1414
+ if (!calld->retry_committed_) {
1415
+ // If we got an error or the payload was nullptr and we have not yet gotten
1416
+ // the recv_trailing_metadata_ready callback, then defer propagating this
1417
+ // callback back to the surface. We can evaluate whether to retry when
1418
+ // recv_trailing_metadata comes back.
1419
+ if (GPR_UNLIKELY((call_attempt->recv_message_ == nullptr ||
1420
+ error != GRPC_ERROR_NONE) &&
1421
+ !call_attempt->completed_recv_trailing_metadata_)) {
1422
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1423
+ gpr_log(GPR_INFO,
1424
+ "chand=%p calld=%p attempt=%p: deferring recv_message_ready "
1425
+ "(nullptr message and recv_trailing_metadata pending)",
1426
+ calld->chand_, calld, call_attempt);
1427
+ }
1428
+ call_attempt->recv_message_ready_deferred_batch_ = std::move(batch_data);
1429
+ call_attempt->recv_message_error_ = GRPC_ERROR_REF(error);
1430
+ if (!call_attempt->started_recv_trailing_metadata_) {
1431
+ // recv_trailing_metadata not yet started by application; start it
1432
+ // ourselves to get status.
1433
+ call_attempt->StartInternalRecvTrailingMetadata();
1434
+ } else {
1435
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
1436
+ "recv_message_ready null");
1437
+ }
1438
+ return;
1439
+ }
1440
+ // Received a valid message, so commit the call.
1441
+ calld->RetryCommit(call_attempt);
1442
+ // If retry state is no longer needed, switch to fast path for
1443
+ // subsequent batches.
1444
+ call_attempt->MaybeSwitchToFastPath();
1445
+ }
1446
+ // Invoke the callback to return the result to the surface.
1447
+ // Manually invoking a callback function; it does not take ownership of error.
1448
+ InvokeRecvMessageCallback(batch_data.release(), error);
1449
+ }
1450
+
1451
+ //
1452
+ // recv_trailing_metadata handling
1453
+ //
1454
+
1455
+ namespace {
1456
+
1457
+ // Sets *status, *server_pushback_md, and *is_lb_drop based on md_batch
1458
+ // and error.
1459
+ void GetCallStatus(grpc_millis deadline, grpc_metadata_batch* md_batch,
1460
+ grpc_error_handle error, grpc_status_code* status,
1461
+ grpc_mdelem** server_pushback_md, bool* is_lb_drop) {
1462
+ if (error != GRPC_ERROR_NONE) {
1463
+ grpc_error_get_status(error, deadline, status, nullptr, nullptr, nullptr);
1464
+ intptr_t value = 0;
1465
+ if (grpc_error_get_int(error, GRPC_ERROR_INT_LB_POLICY_DROP, &value) &&
1466
+ value != 0) {
1467
+ *is_lb_drop = true;
1468
+ }
1469
+ } else {
1470
+ GPR_ASSERT(md_batch->idx.named.grpc_status != nullptr);
1471
+ *status =
1472
+ grpc_get_status_code_from_metadata(md_batch->idx.named.grpc_status->md);
1473
+ if (md_batch->idx.named.grpc_retry_pushback_ms != nullptr) {
1474
+ *server_pushback_md = &md_batch->idx.named.grpc_retry_pushback_ms->md;
1475
+ }
1476
+ }
1477
+ GRPC_ERROR_UNREF(error);
1478
+ }
1479
+
1480
+ } // namespace
1481
+
1482
+ void RetryFilter::CallData::CallAttempt::BatchData::
1483
+ AddClosureForRecvTrailingMetadataReady(grpc_error_handle error,
1484
+ CallCombinerClosureList* closures) {
1485
+ auto* calld = call_attempt_->calld_;
1486
+ // Find pending batch.
1487
+ PendingBatch* pending = calld->PendingBatchFind(
1488
+ "invoking recv_trailing_metadata for",
1489
+ [](grpc_transport_stream_op_batch* batch) {
1490
+ return batch->recv_trailing_metadata &&
1491
+ batch->payload->recv_trailing_metadata
1492
+ .recv_trailing_metadata_ready != nullptr;
1493
+ });
1494
+ // If we generated the recv_trailing_metadata op internally via
1495
+ // StartInternalRecvTrailingMetadata(), then there will be no pending batch.
1496
+ if (pending == nullptr) {
1497
+ call_attempt_->recv_trailing_metadata_error_ = error;
1498
+ return;
1499
+ }
1500
+ // Return metadata.
1501
+ grpc_metadata_batch_move(
1502
+ &call_attempt_->recv_trailing_metadata_,
1503
+ pending->batch->payload->recv_trailing_metadata.recv_trailing_metadata);
1504
+ // Add closure.
1505
+ closures->Add(pending->batch->payload->recv_trailing_metadata
1506
+ .recv_trailing_metadata_ready,
1507
+ error, "recv_trailing_metadata_ready for pending batch");
1508
+ // Update bookkeeping.
1509
+ pending->batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready =
1510
+ nullptr;
1511
+ calld->MaybeClearPendingBatch(pending);
1512
+ }
1513
+
1514
+ void RetryFilter::CallData::CallAttempt::BatchData::
1515
+ AddClosuresForDeferredCompletionCallbacks(
1516
+ CallCombinerClosureList* closures) {
1517
+ if (batch_.recv_trailing_metadata) {
1518
+ // Add closure for deferred recv_initial_metadata_ready.
1519
+ if (GPR_UNLIKELY(
1520
+ call_attempt_->recv_initial_metadata_ready_deferred_batch_ !=
1521
+ nullptr)) {
1522
+ GRPC_CLOSURE_INIT(
1523
+ &call_attempt_->recv_initial_metadata_ready_,
1524
+ InvokeRecvInitialMetadataCallback,
1525
+ call_attempt_->recv_initial_metadata_ready_deferred_batch_.release(),
1526
+ grpc_schedule_on_exec_ctx);
1527
+ closures->Add(&call_attempt_->recv_initial_metadata_ready_,
1528
+ call_attempt_->recv_initial_metadata_error_,
1529
+ "resuming recv_initial_metadata_ready");
1530
+ }
1531
+ // Add closure for deferred recv_message_ready.
1532
+ if (GPR_UNLIKELY(call_attempt_->recv_message_ready_deferred_batch_ !=
1533
+ nullptr)) {
1534
+ GRPC_CLOSURE_INIT(
1535
+ &call_attempt_->recv_message_ready_, InvokeRecvMessageCallback,
1536
+ call_attempt_->recv_message_ready_deferred_batch_.release(),
1537
+ grpc_schedule_on_exec_ctx);
1538
+ closures->Add(&call_attempt_->recv_message_ready_,
1539
+ call_attempt_->recv_message_error_,
1540
+ "resuming recv_message_ready");
1541
+ }
1542
+ // Add closure for deferred on_complete.
1543
+ if (GPR_UNLIKELY(call_attempt_->on_complete_deferred_batch_ != nullptr)) {
1544
+ closures->Add(&call_attempt_->on_complete_deferred_batch_->on_complete_,
1545
+ call_attempt_->on_complete_error_, "resuming on_complete");
1546
+ call_attempt_->on_complete_deferred_batch_.release();
1547
+ }
1548
+ }
1549
+ }
1550
+
1551
+ void RetryFilter::CallData::CallAttempt::BatchData::
1552
+ AddClosuresToFailUnstartedPendingBatches(
1553
+ grpc_error_handle error, CallCombinerClosureList* closures) {
1554
+ auto* calld = call_attempt_->calld_;
1555
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches_); ++i) {
1556
+ PendingBatch* pending = &calld->pending_batches_[i];
1557
+ if (call_attempt_->PendingBatchIsUnstarted(pending)) {
1558
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1559
+ gpr_log(GPR_INFO,
1560
+ "chand=%p calld=%p attempt=%p: failing unstarted pending "
1561
+ "batch at index %" PRIuPTR,
1562
+ calld->chand_, calld, call_attempt_.get(), i);
1563
+ }
1564
+ closures->Add(pending->batch->on_complete, GRPC_ERROR_REF(error),
1565
+ "failing on_complete for pending batch");
1566
+ pending->batch->on_complete = nullptr;
1567
+ calld->MaybeClearPendingBatch(pending);
1568
+ }
1569
+ }
1570
+ GRPC_ERROR_UNREF(error);
1571
+ }
1572
+
1573
+ void RetryFilter::CallData::CallAttempt::BatchData::RunClosuresForCompletedCall(
1574
+ grpc_error_handle error) {
1575
+ // Construct list of closures to execute.
1576
+ CallCombinerClosureList closures;
1577
+ // First, add closure for recv_trailing_metadata_ready.
1578
+ AddClosureForRecvTrailingMetadataReady(GRPC_ERROR_REF(error), &closures);
1579
+ // If there are deferred batch completion callbacks, add them to closures.
1580
+ AddClosuresForDeferredCompletionCallbacks(&closures);
1581
+ // Add closures to fail any pending batches that have not yet been started.
1582
+ AddClosuresToFailUnstartedPendingBatches(GRPC_ERROR_REF(error), &closures);
1583
+ // Schedule all of the closures identified above.
1584
+ // Note: This will release the call combiner.
1585
+ closures.RunClosures(call_attempt_->calld_->call_combiner_);
1586
+ GRPC_ERROR_UNREF(error);
1587
+ }
1588
+
1589
+ void RetryFilter::CallData::CallAttempt::BatchData::RecvTrailingMetadataReady(
1590
+ void* arg, grpc_error_handle error) {
1591
+ RefCountedPtr<BatchData> batch_data(static_cast<BatchData*>(arg));
1592
+ CallAttempt* call_attempt = batch_data->call_attempt_.get();
1593
+ CallData* calld = call_attempt->calld_;
1594
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1595
+ gpr_log(GPR_INFO,
1596
+ "chand=%p calld=%p attempt=%p: got recv_trailing_metadata_ready, "
1597
+ "error=%s",
1598
+ calld->chand_, calld, call_attempt,
1599
+ grpc_error_std_string(error).c_str());
1600
+ }
1601
+ call_attempt->completed_recv_trailing_metadata_ = true;
1602
+ // If this attempt has been cancelled, then we're not going to use the
1603
+ // result of this recv_trailing_metadata op, so do nothing.
1604
+ if (call_attempt->cancelled_) {
1605
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
1606
+ "recv_trailing_metadata_ready after cancellation");
1607
+ return;
1608
+ }
1609
+ // Cancel per-attempt recv timer, if any.
1610
+ call_attempt->MaybeCancelPerAttemptRecvTimer();
1611
+ // Get the call's status and check for server pushback metadata.
1612
+ grpc_status_code status = GRPC_STATUS_OK;
1613
+ grpc_mdelem* server_pushback_md = nullptr;
1614
+ grpc_metadata_batch* md_batch =
1615
+ batch_data->batch_.payload->recv_trailing_metadata.recv_trailing_metadata;
1616
+ bool is_lb_drop = false;
1617
+ GetCallStatus(calld->deadline_, md_batch, GRPC_ERROR_REF(error), &status,
1618
+ &server_pushback_md, &is_lb_drop);
1619
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1620
+ gpr_log(
1621
+ GPR_INFO,
1622
+ "chand=%p calld=%p attempt=%p: call finished, status=%s is_lb_drop=%d",
1623
+ calld->chand_, calld, call_attempt, grpc_status_code_to_string(status),
1624
+ is_lb_drop);
1625
+ }
1626
+ // Check if we should retry.
1627
+ grpc_millis server_pushback_ms = -1;
1628
+ if (call_attempt->ShouldRetry(status, is_lb_drop, server_pushback_md,
1629
+ &server_pushback_ms)) {
1630
+ // Start retry timer.
1631
+ calld->StartRetryTimer(server_pushback_ms);
1632
+ // Cancel call attempt.
1633
+ CallCombinerClosureList closures;
1634
+ call_attempt->Cancel(&closures);
1635
+ // Yields call combiner.
1636
+ closures.RunClosures(calld->call_combiner_);
1637
+ return;
1638
+ }
1639
+ // Not retrying, so commit the call.
1640
+ calld->RetryCommit(call_attempt);
1641
+ // If retry state is no longer needed, switch to fast path for
1642
+ // subsequent batches.
1643
+ call_attempt->MaybeSwitchToFastPath();
1644
+ // Run any necessary closures.
1645
+ batch_data->RunClosuresForCompletedCall(GRPC_ERROR_REF(error));
1646
+ }
1647
+
1648
+ //
1649
+ // on_complete callback handling
1650
+ //
1651
+
1652
+ void RetryFilter::CallData::CallAttempt::BatchData::
1653
+ AddClosuresForCompletedPendingBatch(grpc_error_handle error,
1654
+ CallCombinerClosureList* closures) {
1655
+ auto* calld = call_attempt_->calld_;
1656
+ PendingBatch* pending = calld->PendingBatchFind(
1657
+ "completed", [this](grpc_transport_stream_op_batch* batch) {
1658
+ // Match the pending batch with the same set of send ops as the
1659
+ // batch we've just completed.
1660
+ return batch->on_complete != nullptr &&
1661
+ batch_.send_initial_metadata == batch->send_initial_metadata &&
1662
+ batch_.send_message == batch->send_message &&
1663
+ batch_.send_trailing_metadata == batch->send_trailing_metadata;
1664
+ });
1665
+ // If batch_data is a replay batch, then there will be no pending
1666
+ // batch to complete.
1667
+ if (pending == nullptr) {
1668
+ GRPC_ERROR_UNREF(error);
1669
+ return;
1670
+ }
1671
+ // Add closure.
1672
+ closures->Add(pending->batch->on_complete, error,
1673
+ "on_complete for pending batch");
1674
+ pending->batch->on_complete = nullptr;
1675
+ calld->MaybeClearPendingBatch(pending);
1676
+ }
1677
+
1678
+ void RetryFilter::CallData::CallAttempt::BatchData::
1679
+ AddClosuresForReplayOrPendingSendOps(CallCombinerClosureList* closures) {
1680
+ auto* calld = call_attempt_->calld_;
1681
+ bool have_pending_send_ops = call_attempt_->HaveSendOpsToReplay();
1682
+ // We don't check send_initial_metadata here, because that op will always
1683
+ // be started as soon as it is received from the surface, so it will
1684
+ // never need to be started at this point.
1685
+ if (!have_pending_send_ops) {
1686
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches_); ++i) {
1687
+ PendingBatch* pending = &calld->pending_batches_[i];
1688
+ grpc_transport_stream_op_batch* batch = pending->batch;
1689
+ if (batch == nullptr || pending->send_ops_cached) continue;
1690
+ if (batch->send_message || batch->send_trailing_metadata) {
1691
+ have_pending_send_ops = true;
1692
+ break;
1693
+ }
1694
+ }
1695
+ }
1696
+ if (have_pending_send_ops) {
1697
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1698
+ gpr_log(GPR_INFO,
1699
+ "chand=%p calld=%p attempt=%p: starting next batch for pending "
1700
+ "send op(s)",
1701
+ calld->chand_, calld, call_attempt_.get());
1702
+ }
1703
+ call_attempt_->AddRetriableBatches(closures);
1704
+ }
1705
+ }
1706
+
1707
+ void RetryFilter::CallData::CallAttempt::BatchData::OnComplete(
1708
+ void* arg, grpc_error_handle error) {
1709
+ RefCountedPtr<BatchData> batch_data(static_cast<BatchData*>(arg));
1710
+ CallAttempt* call_attempt = batch_data->call_attempt_.get();
1711
+ CallData* calld = call_attempt->calld_;
1712
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1713
+ gpr_log(GPR_INFO,
1714
+ "chand=%p calld=%p attempt=%p: got on_complete, error=%s, batch=%s",
1715
+ calld->chand_, calld, call_attempt,
1716
+ grpc_error_std_string(error).c_str(),
1717
+ grpc_transport_stream_op_batch_string(&batch_data->batch_).c_str());
1718
+ }
1719
+ // If this attempt has been cancelled, then we're not going to propagate
1720
+ // the completion of this batch, so do nothing.
1721
+ if (call_attempt->cancelled_) {
1722
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
1723
+ "on_complete after cancellation");
1724
+ return;
1725
+ }
1726
+ // If we got an error and have not yet gotten the
1727
+ // recv_trailing_metadata_ready callback, then defer propagating this
1728
+ // callback back to the surface. We can evaluate whether to retry when
1729
+ // recv_trailing_metadata comes back.
1730
+ if (GPR_UNLIKELY(!calld->retry_committed_ && error != GRPC_ERROR_NONE &&
1731
+ !call_attempt->completed_recv_trailing_metadata_)) {
1732
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1733
+ gpr_log(GPR_INFO, "chand=%p calld=%p attempt=%p: deferring on_complete",
1734
+ calld->chand_, calld, call_attempt);
1735
+ }
1736
+ call_attempt->on_complete_deferred_batch_ = std::move(batch_data);
1737
+ call_attempt->on_complete_error_ = GRPC_ERROR_REF(error);
1738
+ if (!call_attempt->started_recv_trailing_metadata_) {
1739
+ // recv_trailing_metadata not yet started by application; start it
1740
+ // ourselves to get status.
1741
+ call_attempt->StartInternalRecvTrailingMetadata();
1742
+ } else {
1743
+ GRPC_CALL_COMBINER_STOP(
1744
+ calld->call_combiner_,
1745
+ "on_complete failure before recv_trailing_metadata_ready");
1746
+ }
1747
+ return;
1748
+ }
1749
+ // Update bookkeeping in call_attempt.
1750
+ if (batch_data->batch_.send_initial_metadata) {
1751
+ call_attempt->completed_send_initial_metadata_ = true;
1752
+ }
1753
+ if (batch_data->batch_.send_message) {
1754
+ ++call_attempt->completed_send_message_count_;
1755
+ }
1756
+ if (batch_data->batch_.send_trailing_metadata) {
1757
+ call_attempt->completed_send_trailing_metadata_ = true;
1758
+ }
1759
+ // If the call is committed, free cached data for send ops that we've just
1760
+ // completed.
1761
+ if (calld->retry_committed_) {
1762
+ batch_data->FreeCachedSendOpDataForCompletedBatch();
1763
+ }
1764
+ // Construct list of closures to execute.
1765
+ CallCombinerClosureList closures;
1766
+ // Add closure for the completed pending batch, if any.
1767
+ batch_data->AddClosuresForCompletedPendingBatch(GRPC_ERROR_REF(error),
1768
+ &closures);
1769
+ // If needed, add a callback to start any replay or pending send ops on
1770
+ // the LB call.
1771
+ if (!call_attempt->completed_recv_trailing_metadata_) {
1772
+ batch_data->AddClosuresForReplayOrPendingSendOps(&closures);
1773
+ }
1774
+ // If retry state is no longer needed (i.e., we're committed and there
1775
+ // are no more send ops to replay), switch to fast path for subsequent
1776
+ // batches.
1777
+ call_attempt->MaybeSwitchToFastPath();
1778
+ // Schedule all of the closures identified above.
1779
+ // Note: This yields the call combiner.
1780
+ closures.RunClosures(calld->call_combiner_);
1781
+ }
1782
+
1783
+ //
1784
+ // retriable batch construction
1785
+ //
1786
+
1787
+ void RetryFilter::CallData::CallAttempt::BatchData::
1788
+ AddRetriableSendInitialMetadataOp() {
1789
+ auto* calld = call_attempt_->calld_;
1790
+ // Maps the number of retries to the corresponding metadata value slice.
1791
+ const grpc_slice* retry_count_strings[] = {&GRPC_MDSTR_1, &GRPC_MDSTR_2,
1792
+ &GRPC_MDSTR_3, &GRPC_MDSTR_4};
1793
+ // We need to make a copy of the metadata batch for each attempt, since
1794
+ // the filters in the subchannel stack may modify this batch, and we don't
1795
+ // want those modifications to be passed forward to subsequent attempts.
1796
+ //
1797
+ // If we've already completed one or more attempts, add the
1798
+ // grpc-retry-attempts header.
1799
+ call_attempt_->send_initial_metadata_storage_ =
1800
+ static_cast<grpc_linked_mdelem*>(
1801
+ calld->arena_->Alloc(sizeof(grpc_linked_mdelem) *
1802
+ (calld->send_initial_metadata_.list.count +
1803
+ (calld->num_attempts_completed_ > 0))));
1804
+ grpc_metadata_batch_copy(&calld->send_initial_metadata_,
1805
+ &call_attempt_->send_initial_metadata_,
1806
+ call_attempt_->send_initial_metadata_storage_);
1807
+ if (GPR_UNLIKELY(call_attempt_->send_initial_metadata_.idx.named
1808
+ .grpc_previous_rpc_attempts != nullptr)) {
1809
+ grpc_metadata_batch_remove(&call_attempt_->send_initial_metadata_,
1810
+ GRPC_BATCH_GRPC_PREVIOUS_RPC_ATTEMPTS);
1811
+ }
1812
+ if (GPR_UNLIKELY(calld->num_attempts_completed_ > 0)) {
1813
+ grpc_mdelem retry_md = grpc_mdelem_create(
1814
+ GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS,
1815
+ *retry_count_strings[calld->num_attempts_completed_ - 1], nullptr);
1816
+ grpc_error_handle error = grpc_metadata_batch_add_tail(
1817
+ &call_attempt_->send_initial_metadata_,
1818
+ &call_attempt_->send_initial_metadata_storage_
1819
+ [calld->send_initial_metadata_.list.count],
1820
+ retry_md, GRPC_BATCH_GRPC_PREVIOUS_RPC_ATTEMPTS);
1821
+ if (GPR_UNLIKELY(error != GRPC_ERROR_NONE)) {
1822
+ gpr_log(GPR_ERROR, "error adding retry metadata: %s",
1823
+ grpc_error_std_string(error).c_str());
1824
+ GPR_ASSERT(false);
1825
+ }
1826
+ }
1827
+ call_attempt_->started_send_initial_metadata_ = true;
1828
+ batch_.send_initial_metadata = true;
1829
+ batch_.payload->send_initial_metadata.send_initial_metadata =
1830
+ &call_attempt_->send_initial_metadata_;
1831
+ batch_.payload->send_initial_metadata.send_initial_metadata_flags =
1832
+ calld->send_initial_metadata_flags_;
1833
+ batch_.payload->send_initial_metadata.peer_string = calld->peer_string_;
1834
+ }
1835
+
1836
+ void RetryFilter::CallData::CallAttempt::BatchData::
1837
+ AddRetriableSendMessageOp() {
1838
+ auto* calld = call_attempt_->calld_;
1839
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1840
+ gpr_log(
1841
+ GPR_INFO,
1842
+ "chand=%p calld=%p attempt=%p: starting calld->send_messages[%" PRIuPTR
1843
+ "]",
1844
+ calld->chand_, calld, call_attempt_.get(),
1845
+ call_attempt_->started_send_message_count_);
1846
+ }
1847
+ ByteStreamCache* cache =
1848
+ calld->send_messages_[call_attempt_->started_send_message_count_];
1849
+ ++call_attempt_->started_send_message_count_;
1850
+ call_attempt_->send_message_.Init(cache);
1851
+ batch_.send_message = true;
1852
+ batch_.payload->send_message.send_message.reset(
1853
+ call_attempt_->send_message_.get());
1854
+ }
1855
+
1856
+ void RetryFilter::CallData::CallAttempt::BatchData::
1857
+ AddRetriableSendTrailingMetadataOp() {
1858
+ auto* calld = call_attempt_->calld_;
1859
+ // We need to make a copy of the metadata batch for each attempt, since
1860
+ // the filters in the subchannel stack may modify this batch, and we don't
1861
+ // want those modifications to be passed forward to subsequent attempts.
1862
+ call_attempt_->send_trailing_metadata_storage_ =
1863
+ static_cast<grpc_linked_mdelem*>(
1864
+ calld->arena_->Alloc(sizeof(grpc_linked_mdelem) *
1865
+ calld->send_trailing_metadata_.list.count));
1866
+ grpc_metadata_batch_copy(&calld->send_trailing_metadata_,
1867
+ &call_attempt_->send_trailing_metadata_,
1868
+ call_attempt_->send_trailing_metadata_storage_);
1869
+ call_attempt_->started_send_trailing_metadata_ = true;
1870
+ batch_.send_trailing_metadata = true;
1871
+ batch_.payload->send_trailing_metadata.send_trailing_metadata =
1872
+ &call_attempt_->send_trailing_metadata_;
1873
+ }
1874
+
1875
+ void RetryFilter::CallData::CallAttempt::BatchData::
1876
+ AddRetriableRecvInitialMetadataOp() {
1877
+ call_attempt_->started_recv_initial_metadata_ = true;
1878
+ batch_.recv_initial_metadata = true;
1879
+ grpc_metadata_batch_init(&call_attempt_->recv_initial_metadata_);
1880
+ batch_.payload->recv_initial_metadata.recv_initial_metadata =
1881
+ &call_attempt_->recv_initial_metadata_;
1882
+ batch_.payload->recv_initial_metadata.trailing_metadata_available =
1883
+ &call_attempt_->trailing_metadata_available_;
1884
+ GRPC_CLOSURE_INIT(&call_attempt_->recv_initial_metadata_ready_,
1885
+ RecvInitialMetadataReady, this, grpc_schedule_on_exec_ctx);
1886
+ batch_.payload->recv_initial_metadata.recv_initial_metadata_ready =
1887
+ &call_attempt_->recv_initial_metadata_ready_;
1888
+ }
1889
+
1890
+ void RetryFilter::CallData::CallAttempt::BatchData::
1891
+ AddRetriableRecvMessageOp() {
1892
+ ++call_attempt_->started_recv_message_count_;
1893
+ batch_.recv_message = true;
1894
+ batch_.payload->recv_message.recv_message = &call_attempt_->recv_message_;
1895
+ batch_.payload->recv_message.call_failed_before_recv_message = nullptr;
1896
+ GRPC_CLOSURE_INIT(&call_attempt_->recv_message_ready_, RecvMessageReady, this,
1897
+ grpc_schedule_on_exec_ctx);
1898
+ batch_.payload->recv_message.recv_message_ready =
1899
+ &call_attempt_->recv_message_ready_;
1900
+ }
1901
+
1902
+ void RetryFilter::CallData::CallAttempt::BatchData::
1903
+ AddRetriableRecvTrailingMetadataOp() {
1904
+ call_attempt_->started_recv_trailing_metadata_ = true;
1905
+ batch_.recv_trailing_metadata = true;
1906
+ grpc_metadata_batch_init(&call_attempt_->recv_trailing_metadata_);
1907
+ batch_.payload->recv_trailing_metadata.recv_trailing_metadata =
1908
+ &call_attempt_->recv_trailing_metadata_;
1909
+ batch_.payload->recv_trailing_metadata.collect_stats =
1910
+ &call_attempt_->collect_stats_;
1911
+ GRPC_CLOSURE_INIT(&call_attempt_->recv_trailing_metadata_ready_,
1912
+ RecvTrailingMetadataReady, this, grpc_schedule_on_exec_ctx);
1913
+ batch_.payload->recv_trailing_metadata.recv_trailing_metadata_ready =
1914
+ &call_attempt_->recv_trailing_metadata_ready_;
1915
+ }
1916
+
1917
+ void RetryFilter::CallData::CallAttempt::BatchData::AddCancelStreamOp() {
1918
+ batch_.cancel_stream = true;
1919
+ batch_.payload->cancel_stream.cancel_error =
1920
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING("retry attempt abandoned");
1921
+ }
1922
+
1923
+ //
1924
+ // CallData vtable functions
1925
+ //
1926
+
1927
+ grpc_error_handle RetryFilter::CallData::Init(
1928
+ grpc_call_element* elem, const grpc_call_element_args* args) {
1929
+ auto* chand = static_cast<RetryFilter*>(elem->channel_data);
1930
+ new (elem->call_data) CallData(chand, *args);
1931
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
1932
+ gpr_log(GPR_INFO, "chand=%p calld=%p: created call", chand,
1933
+ elem->call_data);
1934
+ }
1935
+ return GRPC_ERROR_NONE;
1936
+ }
1937
+
1938
+ void RetryFilter::CallData::Destroy(grpc_call_element* elem,
1939
+ const grpc_call_final_info* /*final_info*/,
1940
+ grpc_closure* then_schedule_closure) {
1941
+ auto* calld = static_cast<CallData*>(elem->call_data);
1942
+ // Save our ref to the CallStackDestructionBarrier until after our
1943
+ // dtor is invoked.
1944
+ RefCountedPtr<CallStackDestructionBarrier> call_stack_destruction_barrier =
1945
+ std::move(calld->call_stack_destruction_barrier_);
1946
+ calld->~CallData();
1947
+ // Now set the callback in the CallStackDestructionBarrier object,
1948
+ // right before we release our ref to it (implicitly upon returning).
1949
+ // The callback will be invoked when the CallStackDestructionBarrier
1950
+ // is destroyed.
1951
+ call_stack_destruction_barrier->set_on_call_stack_destruction(
1952
+ then_schedule_closure);
1953
+ }
1954
+
1955
+ void RetryFilter::CallData::StartTransportStreamOpBatch(
1956
+ grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
1957
+ auto* calld = static_cast<CallData*>(elem->call_data);
1958
+ calld->StartTransportStreamOpBatch(batch);
1959
+ }
1960
+
1961
+ void RetryFilter::CallData::SetPollent(grpc_call_element* elem,
1962
+ grpc_polling_entity* pollent) {
1963
+ auto* calld = static_cast<CallData*>(elem->call_data);
1964
+ calld->pollent_ = pollent;
1965
+ }
1966
+
1967
+ //
1968
+ // CallData implementation
1969
+ //
1970
+
1971
+ const RetryMethodConfig* GetRetryPolicy(
1972
+ const grpc_call_context_element* context) {
1973
+ if (context == nullptr) return nullptr;
1974
+ auto* svc_cfg_call_data = static_cast<ServiceConfigCallData*>(
1975
+ context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value);
1976
+ if (svc_cfg_call_data == nullptr) return nullptr;
1977
+ return static_cast<const RetryMethodConfig*>(
1978
+ svc_cfg_call_data->GetMethodParsedConfig(
1979
+ RetryServiceConfigParser::ParserIndex()));
1980
+ }
1981
+
1982
+ RetryFilter::CallData::CallData(RetryFilter* chand,
1983
+ const grpc_call_element_args& args)
1984
+ : chand_(chand),
1985
+ retry_throttle_data_(chand->retry_throttle_data_),
1986
+ retry_policy_(GetRetryPolicy(args.context)),
1987
+ retry_backoff_(
1988
+ BackOff::Options()
1989
+ .set_initial_backoff(retry_policy_ == nullptr
1990
+ ? 0
1991
+ : retry_policy_->initial_backoff())
1992
+ .set_multiplier(retry_policy_ == nullptr
1993
+ ? 0
1994
+ : retry_policy_->backoff_multiplier())
1995
+ .set_jitter(RETRY_BACKOFF_JITTER)
1996
+ .set_max_backoff(
1997
+ retry_policy_ == nullptr ? 0 : retry_policy_->max_backoff())),
1998
+ path_(grpc_slice_ref_internal(args.path)),
1999
+ call_start_time_(args.start_time),
2000
+ deadline_(args.deadline),
2001
+ arena_(args.arena),
2002
+ owning_call_(args.call_stack),
2003
+ call_combiner_(args.call_combiner),
2004
+ call_context_(args.context),
2005
+ call_stack_destruction_barrier_(
2006
+ arena_->New<CallStackDestructionBarrier>()),
2007
+ pending_send_initial_metadata_(false),
2008
+ pending_send_message_(false),
2009
+ pending_send_trailing_metadata_(false),
2010
+ retry_committed_(false),
2011
+ retry_timer_pending_(false) {}
2012
+
2013
+ RetryFilter::CallData::~CallData() {
2014
+ grpc_slice_unref_internal(path_);
2015
+ // Make sure there are no remaining pending batches.
2016
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(pending_batches_); ++i) {
2017
+ GPR_ASSERT(pending_batches_[i].batch == nullptr);
2018
+ }
2019
+ }
2020
+
2021
+ void RetryFilter::CallData::StartTransportStreamOpBatch(
2022
+ grpc_transport_stream_op_batch* batch) {
2023
+ // If we have an LB call, delegate to the LB call.
2024
+ if (committed_call_ != nullptr) {
2025
+ // Note: This will release the call combiner.
2026
+ committed_call_->StartTransportStreamOpBatch(batch);
2027
+ return;
2028
+ }
2029
+ // Handle cancellation.
2030
+ if (GPR_UNLIKELY(batch->cancel_stream)) {
2031
+ grpc_error_handle cancel_error = batch->payload->cancel_stream.cancel_error;
2032
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2033
+ gpr_log(GPR_INFO, "chand=%p calld=%p: cancelled from surface: %s", chand_,
2034
+ this, grpc_error_std_string(cancel_error).c_str());
2035
+ }
2036
+ // If we have a current call attempt, commit the call, then send
2037
+ // the cancellation down to that attempt. When the call fails, it
2038
+ // will not be retried, because we have committed it here.
2039
+ if (call_attempt_ != nullptr) {
2040
+ RetryCommit(call_attempt_.get());
2041
+ // TODO(roth): When implementing hedging, this will get more
2042
+ // complex, because instead of just passing the batch down to a
2043
+ // single call attempt, we'll need to cancel multiple call
2044
+ // attempts and wait for the cancellation on_complete from each call
2045
+ // attempt before we propagate the on_complete from this batch
2046
+ // back to the surface.
2047
+ // Note: This will release the call combiner.
2048
+ call_attempt_->CancelFromSurface(batch);
2049
+ return;
2050
+ }
2051
+ // Cancel retry timer.
2052
+ if (retry_timer_pending_) {
2053
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2054
+ gpr_log(GPR_INFO, "chand=%p calld=%p: cancelling retry timer", chand_,
2055
+ this);
2056
+ }
2057
+ retry_timer_pending_ = false; // Lame timer callback.
2058
+ grpc_timer_cancel(&retry_timer_);
2059
+ FreeAllCachedSendOpData();
2060
+ }
2061
+ // Fail pending batches.
2062
+ PendingBatchesFail(GRPC_ERROR_REF(cancel_error));
2063
+ // Note: This will release the call combiner.
2064
+ grpc_transport_stream_op_batch_finish_with_failure(
2065
+ batch, GRPC_ERROR_REF(cancel_error), call_combiner_);
2066
+ return;
2067
+ }
2068
+ // Add the batch to the pending list.
2069
+ PendingBatch* pending = PendingBatchesAdd(batch);
2070
+ // If the timer is pending, yield the call combiner and wait for it to
2071
+ // run, since we don't want to start another call attempt until it does.
2072
+ if (retry_timer_pending_) {
2073
+ GRPC_CALL_COMBINER_STOP(call_combiner_,
2074
+ "added pending batch while retry timer pending");
2075
+ return;
2076
+ }
2077
+ // If we do not yet have a call attempt, create one.
2078
+ if (call_attempt_ == nullptr) {
2079
+ // If this is the first batch and retries are already committed
2080
+ // (e.g., if this batch put the call above the buffer size limit), then
2081
+ // immediately create an LB call and delegate the batch to it. This
2082
+ // avoids the overhead of unnecessarily allocating a CallAttempt
2083
+ // object or caching any of the send op data.
2084
+ // Note that we would ideally like to do this also on subsequent
2085
+ // attempts (e.g., if a batch puts the call above the buffer size
2086
+ // limit since the last attempt was complete), but in practice that's
2087
+ // not really worthwhile, because we will almost always have cached and
2088
+ // completed at least the send_initial_metadata op on the previous
2089
+ // attempt, which means that we'd need special logic to replay the
2090
+ // batch anyway, which is exactly what the CallAttempt object provides.
2091
+ // We also skip this optimization if perAttemptRecvTimeout is set in the
2092
+ // retry policy, because we need the code in CallAttempt to handle
2093
+ // the associated timer.
2094
+ if (num_attempts_completed_ == 0 && retry_committed_ &&
2095
+ (retry_policy_ == nullptr ||
2096
+ !retry_policy_->per_attempt_recv_timeout().has_value())) {
2097
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2098
+ gpr_log(GPR_INFO,
2099
+ "chand=%p calld=%p: retry committed before first attempt; "
2100
+ "creating LB call",
2101
+ chand_, this);
2102
+ }
2103
+ PendingBatchClear(pending);
2104
+ committed_call_ = CreateLoadBalancedCall();
2105
+ committed_call_->StartTransportStreamOpBatch(batch);
2106
+ return;
2107
+ }
2108
+ // Otherwise, create a call attempt.
2109
+ // The attempt will automatically start any necessary replays or
2110
+ // pending batches.
2111
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2112
+ gpr_log(GPR_INFO, "chand=%p calld=%p: creating call attempt", chand_,
2113
+ this);
2114
+ }
2115
+ CreateCallAttempt();
2116
+ return;
2117
+ }
2118
+ // Send batches to call attempt.
2119
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2120
+ gpr_log(GPR_INFO, "chand=%p calld=%p: starting batch on attempt=%p", chand_,
2121
+ this, call_attempt_.get());
2122
+ }
2123
+ call_attempt_->StartRetriableBatches();
2124
+ }
2125
+
2126
+ RefCountedPtr<ClientChannel::LoadBalancedCall>
2127
+ RetryFilter::CallData::CreateLoadBalancedCall() {
2128
+ grpc_call_element_args args = {owning_call_, nullptr, call_context_,
2129
+ path_, call_start_time_, deadline_,
2130
+ arena_, call_combiner_};
2131
+ return chand_->client_channel_->CreateLoadBalancedCall(
2132
+ args, pollent_,
2133
+ // This callback holds a ref to the CallStackDestructionBarrier
2134
+ // object until the LB call is destroyed.
2135
+ call_stack_destruction_barrier_->MakeLbCallDestructionClosure(this));
2136
+ }
2137
+
2138
+ void RetryFilter::CallData::CreateCallAttempt() {
2139
+ call_attempt_ = MakeRefCounted<CallAttempt>(this);
2140
+ call_attempt_->StartRetriableBatches();
2141
+ }
2142
+
2143
+ //
2144
+ // send op data caching
2145
+ //
2146
+
2147
+ void RetryFilter::CallData::MaybeCacheSendOpsForBatch(PendingBatch* pending) {
2148
+ if (pending->send_ops_cached) return;
2149
+ pending->send_ops_cached = true;
2150
+ grpc_transport_stream_op_batch* batch = pending->batch;
2151
+ // Save a copy of metadata for send_initial_metadata ops.
2152
+ if (batch->send_initial_metadata) {
2153
+ seen_send_initial_metadata_ = true;
2154
+ GPR_ASSERT(send_initial_metadata_storage_ == nullptr);
2155
+ grpc_metadata_batch* send_initial_metadata =
2156
+ batch->payload->send_initial_metadata.send_initial_metadata;
2157
+ send_initial_metadata_storage_ =
2158
+ static_cast<grpc_linked_mdelem*>(arena_->Alloc(
2159
+ sizeof(grpc_linked_mdelem) * send_initial_metadata->list.count));
2160
+ grpc_metadata_batch_copy(send_initial_metadata, &send_initial_metadata_,
2161
+ send_initial_metadata_storage_);
2162
+ send_initial_metadata_flags_ =
2163
+ batch->payload->send_initial_metadata.send_initial_metadata_flags;
2164
+ peer_string_ = batch->payload->send_initial_metadata.peer_string;
2165
+ }
2166
+ // Set up cache for send_message ops.
2167
+ if (batch->send_message) {
2168
+ ByteStreamCache* cache = arena_->New<ByteStreamCache>(
2169
+ std::move(batch->payload->send_message.send_message));
2170
+ send_messages_.push_back(cache);
2171
+ }
2172
+ // Save metadata batch for send_trailing_metadata ops.
2173
+ if (batch->send_trailing_metadata) {
2174
+ seen_send_trailing_metadata_ = true;
2175
+ GPR_ASSERT(send_trailing_metadata_storage_ == nullptr);
2176
+ grpc_metadata_batch* send_trailing_metadata =
2177
+ batch->payload->send_trailing_metadata.send_trailing_metadata;
2178
+ send_trailing_metadata_storage_ =
2179
+ static_cast<grpc_linked_mdelem*>(arena_->Alloc(
2180
+ sizeof(grpc_linked_mdelem) * send_trailing_metadata->list.count));
2181
+ grpc_metadata_batch_copy(send_trailing_metadata, &send_trailing_metadata_,
2182
+ send_trailing_metadata_storage_);
2183
+ }
2184
+ }
2185
+
2186
+ void RetryFilter::CallData::FreeCachedSendInitialMetadata() {
2187
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2188
+ gpr_log(GPR_INFO, "chand=%p calld=%p: destroying send_initial_metadata",
2189
+ chand_, this);
2190
+ }
2191
+ grpc_metadata_batch_destroy(&send_initial_metadata_);
2192
+ }
2193
+
2194
+ void RetryFilter::CallData::FreeCachedSendMessage(size_t idx) {
2195
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2196
+ gpr_log(GPR_INFO,
2197
+ "chand=%p calld=%p: destroying send_messages[%" PRIuPTR "]", chand_,
2198
+ this, idx);
2199
+ }
2200
+ send_messages_[idx]->Destroy();
2201
+ }
2202
+
2203
+ void RetryFilter::CallData::FreeCachedSendTrailingMetadata() {
2204
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2205
+ gpr_log(GPR_INFO, "chand=%p calld=%p: destroying send_trailing_metadata",
2206
+ chand_, this);
2207
+ }
2208
+ grpc_metadata_batch_destroy(&send_trailing_metadata_);
2209
+ }
2210
+
2211
+ void RetryFilter::CallData::FreeAllCachedSendOpData() {
2212
+ if (seen_send_initial_metadata_) {
2213
+ FreeCachedSendInitialMetadata();
2214
+ }
2215
+ for (size_t i = 0; i < send_messages_.size(); ++i) {
2216
+ FreeCachedSendMessage(i);
2217
+ }
2218
+ if (seen_send_trailing_metadata_) {
2219
+ FreeCachedSendTrailingMetadata();
2220
+ }
2221
+ }
2222
+
2223
+ //
2224
+ // pending_batches management
2225
+ //
2226
+
2227
+ size_t RetryFilter::CallData::GetBatchIndex(
2228
+ grpc_transport_stream_op_batch* batch) {
2229
+ if (batch->send_initial_metadata) return 0;
2230
+ if (batch->send_message) return 1;
2231
+ if (batch->send_trailing_metadata) return 2;
2232
+ if (batch->recv_initial_metadata) return 3;
2233
+ if (batch->recv_message) return 4;
2234
+ if (batch->recv_trailing_metadata) return 5;
2235
+ GPR_UNREACHABLE_CODE(return (size_t)-1);
2236
+ }
2237
+
2238
+ // This is called via the call combiner, so access to calld is synchronized.
2239
+ RetryFilter::CallData::PendingBatch* RetryFilter::CallData::PendingBatchesAdd(
2240
+ grpc_transport_stream_op_batch* batch) {
2241
+ const size_t idx = GetBatchIndex(batch);
2242
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2243
+ gpr_log(GPR_INFO,
2244
+ "chand=%p calld=%p: adding pending batch at index %" PRIuPTR,
2245
+ chand_, this, idx);
2246
+ }
2247
+ PendingBatch* pending = &pending_batches_[idx];
2248
+ GPR_ASSERT(pending->batch == nullptr);
2249
+ pending->batch = batch;
2250
+ pending->send_ops_cached = false;
2251
+ // Update state in calld about pending batches.
2252
+ // Also check if the batch takes us over the retry buffer limit.
2253
+ // Note: We don't check the size of trailing metadata here, because
2254
+ // gRPC clients do not send trailing metadata.
2255
+ if (batch->send_initial_metadata) {
2256
+ pending_send_initial_metadata_ = true;
2257
+ bytes_buffered_for_retry_ += grpc_metadata_batch_size(
2258
+ batch->payload->send_initial_metadata.send_initial_metadata);
2259
+ }
2260
+ if (batch->send_message) {
2261
+ pending_send_message_ = true;
2262
+ bytes_buffered_for_retry_ +=
2263
+ batch->payload->send_message.send_message->length();
2264
+ }
2265
+ if (batch->send_trailing_metadata) {
2266
+ pending_send_trailing_metadata_ = true;
2267
+ }
2268
+ // TODO(roth): When we implement hedging, if there are currently attempts
2269
+ // in flight, we will need to pick the one on which the max number of send
2270
+ // ops have already been sent, and we commit to that attempt.
2271
+ if (GPR_UNLIKELY(bytes_buffered_for_retry_ >
2272
+ chand_->per_rpc_retry_buffer_size_)) {
2273
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2274
+ gpr_log(GPR_INFO,
2275
+ "chand=%p calld=%p: exceeded retry buffer size, committing",
2276
+ chand_, this);
2277
+ }
2278
+ RetryCommit(call_attempt_.get());
2279
+ }
2280
+ return pending;
2281
+ }
2282
+
2283
+ void RetryFilter::CallData::PendingBatchClear(PendingBatch* pending) {
2284
+ if (pending->batch->send_initial_metadata) {
2285
+ pending_send_initial_metadata_ = false;
2286
+ }
2287
+ if (pending->batch->send_message) {
2288
+ pending_send_message_ = false;
2289
+ }
2290
+ if (pending->batch->send_trailing_metadata) {
2291
+ pending_send_trailing_metadata_ = false;
2292
+ }
2293
+ pending->batch = nullptr;
2294
+ }
2295
+
2296
+ void RetryFilter::CallData::MaybeClearPendingBatch(PendingBatch* pending) {
2297
+ grpc_transport_stream_op_batch* batch = pending->batch;
2298
+ // We clear the pending batch if all of its callbacks have been
2299
+ // scheduled and reset to nullptr.
2300
+ if (batch->on_complete == nullptr &&
2301
+ (!batch->recv_initial_metadata ||
2302
+ batch->payload->recv_initial_metadata.recv_initial_metadata_ready ==
2303
+ nullptr) &&
2304
+ (!batch->recv_message ||
2305
+ batch->payload->recv_message.recv_message_ready == nullptr) &&
2306
+ (!batch->recv_trailing_metadata ||
2307
+ batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready ==
2308
+ nullptr)) {
2309
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2310
+ gpr_log(GPR_INFO, "chand=%p calld=%p: clearing pending batch", chand_,
2311
+ this);
2312
+ }
2313
+ PendingBatchClear(pending);
2314
+ }
2315
+ }
2316
+
2317
+ // This is called via the call combiner, so access to calld is synchronized.
2318
+ void RetryFilter::CallData::FailPendingBatchInCallCombiner(
2319
+ void* arg, grpc_error_handle error) {
2320
+ grpc_transport_stream_op_batch* batch =
2321
+ static_cast<grpc_transport_stream_op_batch*>(arg);
2322
+ CallData* call = static_cast<CallData*>(batch->handler_private.extra_arg);
2323
+ // Note: This will release the call combiner.
2324
+ grpc_transport_stream_op_batch_finish_with_failure(
2325
+ batch, GRPC_ERROR_REF(error), call->call_combiner_);
2326
+ }
2327
+
2328
+ // This is called via the call combiner, so access to calld is synchronized.
2329
+ void RetryFilter::CallData::PendingBatchesFail(grpc_error_handle error) {
2330
+ GPR_ASSERT(error != GRPC_ERROR_NONE);
2331
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2332
+ size_t num_batches = 0;
2333
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(pending_batches_); ++i) {
2334
+ if (pending_batches_[i].batch != nullptr) ++num_batches;
2335
+ }
2336
+ gpr_log(GPR_INFO,
2337
+ "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s",
2338
+ chand_, this, num_batches, grpc_error_std_string(error).c_str());
2339
+ }
2340
+ CallCombinerClosureList closures;
2341
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(pending_batches_); ++i) {
2342
+ PendingBatch* pending = &pending_batches_[i];
2343
+ grpc_transport_stream_op_batch* batch = pending->batch;
2344
+ if (batch != nullptr) {
2345
+ batch->handler_private.extra_arg = this;
2346
+ GRPC_CLOSURE_INIT(&batch->handler_private.closure,
2347
+ FailPendingBatchInCallCombiner, batch,
2348
+ grpc_schedule_on_exec_ctx);
2349
+ closures.Add(&batch->handler_private.closure, GRPC_ERROR_REF(error),
2350
+ "PendingBatchesFail");
2351
+ PendingBatchClear(pending);
2352
+ }
2353
+ }
2354
+ closures.RunClosuresWithoutYielding(call_combiner_);
2355
+ GRPC_ERROR_UNREF(error);
2356
+ }
2357
+
2358
+ template <typename Predicate>
2359
+ RetryFilter::CallData::PendingBatch* RetryFilter::CallData::PendingBatchFind(
2360
+ const char* log_message, Predicate predicate) {
2361
+ for (size_t i = 0; i < GPR_ARRAY_SIZE(pending_batches_); ++i) {
2362
+ PendingBatch* pending = &pending_batches_[i];
2363
+ grpc_transport_stream_op_batch* batch = pending->batch;
2364
+ if (batch != nullptr && predicate(batch)) {
2365
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2366
+ gpr_log(GPR_INFO,
2367
+ "chand=%p calld=%p: %s pending batch at index %" PRIuPTR,
2368
+ chand_, this, log_message, i);
2369
+ }
2370
+ return pending;
2371
+ }
2372
+ }
2373
+ return nullptr;
2374
+ }
2375
+
2376
+ //
2377
+ // retry code
2378
+ //
2379
+
2380
+ void RetryFilter::CallData::RetryCommit(CallAttempt* call_attempt) {
2381
+ if (retry_committed_) return;
2382
+ retry_committed_ = true;
2383
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2384
+ gpr_log(GPR_INFO, "chand=%p calld=%p: committing retries", chand_, this);
2385
+ }
2386
+ if (call_attempt != nullptr) {
2387
+ call_attempt->FreeCachedSendOpDataAfterCommit();
2388
+ }
2389
+ }
2390
+
2391
+ void RetryFilter::CallData::StartRetryTimer(grpc_millis server_pushback_ms) {
2392
+ // Reset call attempt.
2393
+ call_attempt_.reset(DEBUG_LOCATION, "StartRetryTimer");
2394
+ // Compute backoff delay.
2395
+ grpc_millis next_attempt_time;
2396
+ if (server_pushback_ms >= 0) {
2397
+ next_attempt_time = ExecCtx::Get()->Now() + server_pushback_ms;
2398
+ retry_backoff_.Reset();
2399
+ } else {
2400
+ next_attempt_time = retry_backoff_.NextAttemptTime();
2401
+ }
2402
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_retry_trace)) {
2403
+ gpr_log(GPR_INFO,
2404
+ "chand=%p calld=%p: retrying failed call in %" PRId64 " ms", chand_,
2405
+ this, next_attempt_time - ExecCtx::Get()->Now());
2406
+ }
2407
+ // Schedule retry after computed delay.
2408
+ GRPC_CLOSURE_INIT(&retry_closure_, OnRetryTimer, this, nullptr);
2409
+ GRPC_CALL_STACK_REF(owning_call_, "OnRetryTimer");
2410
+ retry_timer_pending_ = true;
2411
+ grpc_timer_init(&retry_timer_, next_attempt_time, &retry_closure_);
2412
+ }
2413
+
2414
+ void RetryFilter::CallData::OnRetryTimer(void* arg, grpc_error_handle error) {
2415
+ auto* calld = static_cast<CallData*>(arg);
2416
+ GRPC_CLOSURE_INIT(&calld->retry_closure_, OnRetryTimerLocked, calld, nullptr);
2417
+ GRPC_CALL_COMBINER_START(calld->call_combiner_, &calld->retry_closure_,
2418
+ GRPC_ERROR_REF(error), "retry timer fired");
2419
+ }
2420
+
2421
+ void RetryFilter::CallData::OnRetryTimerLocked(void* arg,
2422
+ grpc_error_handle error) {
2423
+ auto* calld = static_cast<CallData*>(arg);
2424
+ if (error == GRPC_ERROR_NONE && calld->retry_timer_pending_) {
2425
+ calld->retry_timer_pending_ = false;
2426
+ calld->CreateCallAttempt();
2427
+ } else {
2428
+ GRPC_CALL_COMBINER_STOP(calld->call_combiner_, "retry timer cancelled");
2429
+ }
2430
+ GRPC_CALL_STACK_UNREF(calld->owning_call_, "OnRetryTimer");
2431
+ }
2432
+
2433
+ } // namespace
2434
+
2435
+ const grpc_channel_filter kRetryFilterVtable = {
2436
+ RetryFilter::CallData::StartTransportStreamOpBatch,
2437
+ RetryFilter::StartTransportOp,
2438
+ sizeof(RetryFilter::CallData),
2439
+ RetryFilter::CallData::Init,
2440
+ RetryFilter::CallData::SetPollent,
2441
+ RetryFilter::CallData::Destroy,
2442
+ sizeof(RetryFilter),
2443
+ RetryFilter::Init,
2444
+ RetryFilter::Destroy,
2445
+ RetryFilter::GetChannelInfo,
2446
+ "retry_filter",
2447
+ };
2448
+
2449
+ } // namespace grpc_core