grpc 1.19.0 → 1.20.0.pre1

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 (224) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +4131 -7903
  3. data/include/grpc/grpc.h +11 -6
  4. data/include/grpc/grpc_security.h +51 -9
  5. data/include/grpc/impl/codegen/byte_buffer.h +13 -0
  6. data/include/grpc/impl/codegen/grpc_types.h +4 -0
  7. data/include/grpc/impl/codegen/port_platform.h +37 -6
  8. data/include/grpc/impl/codegen/sync_posix.h +18 -0
  9. data/src/core/ext/filters/client_channel/client_channel.cc +560 -236
  10. data/src/core/ext/filters/client_channel/client_channel_channelz.h +2 -2
  11. data/src/core/ext/filters/client_channel/client_channel_factory.cc +22 -34
  12. data/src/core/ext/filters/client_channel/client_channel_factory.h +19 -38
  13. data/src/core/ext/filters/client_channel/global_subchannel_pool.cc +7 -4
  14. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +2 -2
  15. data/src/core/ext/filters/client_channel/lb_policy.cc +105 -28
  16. data/src/core/ext/filters/client_channel/lb_policy.h +259 -141
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +29 -32
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +789 -803
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h +3 -1
  20. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +2 -6
  21. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.cc +1 -1
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +7 -1
  23. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +8 -8
  24. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +2 -2
  25. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +127 -219
  26. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +103 -282
  27. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +4 -10
  28. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +709 -906
  29. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +0 -43
  30. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.cc +8 -8
  31. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +2 -2
  32. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -6
  33. data/src/core/ext/filters/client_channel/resolver.cc +54 -1
  34. data/src/core/ext/filters/client_channel/resolver.h +51 -22
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +34 -86
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +29 -41
  37. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +32 -78
  38. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +109 -72
  39. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +13 -8
  40. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +28 -63
  41. data/src/core/ext/filters/client_channel/resolver_factory.h +3 -1
  42. data/src/core/ext/filters/client_channel/resolver_registry.cc +5 -2
  43. data/src/core/ext/filters/client_channel/resolver_registry.h +5 -4
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +69 -49
  45. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +11 -8
  46. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +568 -0
  47. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +141 -0
  48. data/src/core/ext/filters/client_channel/server_address.cc +0 -48
  49. data/src/core/ext/filters/client_channel/server_address.h +0 -10
  50. data/src/core/{lib/transport → ext/filters/client_channel}/service_config.cc +10 -5
  51. data/src/core/{lib/transport → ext/filters/client_channel}/service_config.h +16 -12
  52. data/src/core/ext/filters/client_channel/subchannel.cc +11 -16
  53. data/src/core/ext/filters/client_channel/subchannel.h +3 -0
  54. data/src/core/ext/filters/max_age/max_age_filter.cc +4 -1
  55. data/src/core/ext/filters/message_size/message_size_filter.cc +2 -2
  56. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +45 -45
  57. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +133 -134
  58. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +4 -4
  59. data/src/core/ext/transport/chttp2/transport/bin_decoder.h +4 -4
  60. data/src/core/ext/transport/chttp2/transport/bin_encoder.cc +7 -6
  61. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +4 -3
  62. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +37 -29
  63. data/src/core/ext/transport/chttp2/transport/flow_control.cc +1 -1
  64. data/src/core/ext/transport/chttp2/transport/frame_data.cc +2 -1
  65. data/src/core/ext/transport/chttp2/transport/frame_data.h +1 -1
  66. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +6 -5
  67. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +3 -2
  68. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +5 -4
  69. data/src/core/ext/transport/chttp2/transport/frame_ping.h +1 -1
  70. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +5 -4
  71. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +2 -1
  72. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +2 -1
  73. data/src/core/ext/transport/chttp2/transport/frame_settings.h +2 -1
  74. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +4 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +1 -1
  76. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +7 -6
  77. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +3 -2
  78. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +9 -5
  79. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +6 -1
  80. data/src/core/ext/transport/chttp2/transport/internal.h +5 -4
  81. data/src/core/ext/transport/chttp2/transport/parsing.cc +9 -9
  82. data/src/core/ext/transport/chttp2/transport/writing.cc +1 -1
  83. data/src/core/ext/transport/inproc/inproc_transport.cc +8 -0
  84. data/src/core/lib/channel/channel_args.cc +2 -0
  85. data/src/core/lib/channel/channel_args.h +3 -0
  86. data/src/core/lib/channel/channel_stack.h +1 -1
  87. data/src/core/lib/channel/channel_trace.cc +4 -4
  88. data/src/core/lib/channel/channel_trace.h +4 -4
  89. data/src/core/lib/channel/channelz.cc +32 -19
  90. data/src/core/lib/channel/channelz.h +4 -4
  91. data/src/core/lib/channel/channelz_registry.cc +1 -1
  92. data/src/core/lib/channel/context.h +0 -3
  93. data/src/core/lib/channel/handshaker_registry.cc +7 -3
  94. data/src/core/lib/compression/algorithm_metadata.h +3 -3
  95. data/src/core/lib/compression/compression.cc +1 -1
  96. data/src/core/lib/compression/compression_internal.cc +2 -2
  97. data/src/core/lib/compression/stream_compression_gzip.cc +1 -1
  98. data/src/core/lib/debug/trace.h +2 -1
  99. data/src/core/lib/gpr/cpu_posix.cc +5 -3
  100. data/src/core/lib/gpr/sync_posix.cc +65 -4
  101. data/src/core/lib/gprpp/atomic.h +75 -5
  102. data/src/core/lib/gprpp/fork.cc +0 -2
  103. data/src/core/lib/gprpp/orphanable.h +3 -2
  104. data/src/core/lib/gprpp/ref_counted.h +9 -11
  105. data/src/core/lib/gprpp/thd.h +42 -7
  106. data/src/core/lib/gprpp/thd_posix.cc +31 -13
  107. data/src/core/lib/gprpp/thd_windows.cc +47 -34
  108. data/src/core/lib/http/httpcli.cc +3 -2
  109. data/src/core/lib/http/httpcli_security_connector.cc +0 -1
  110. data/src/core/lib/http/parser.cc +2 -1
  111. data/src/core/lib/http/parser.h +2 -1
  112. data/src/core/lib/iomgr/buffer_list.h +1 -1
  113. data/src/core/lib/iomgr/endpoint.cc +2 -2
  114. data/src/core/lib/iomgr/endpoint.h +3 -2
  115. data/src/core/lib/iomgr/error.cc +9 -9
  116. data/src/core/lib/iomgr/error.h +4 -3
  117. data/src/core/lib/iomgr/ev_epoll1_linux.cc +6 -0
  118. data/src/core/lib/iomgr/ev_epollex_linux.cc +14 -9
  119. data/src/core/lib/iomgr/ev_poll_posix.cc +7 -481
  120. data/src/core/lib/iomgr/ev_posix.cc +7 -3
  121. data/src/core/lib/iomgr/ev_posix.h +8 -0
  122. data/src/core/lib/iomgr/executor.cc +13 -0
  123. data/src/core/lib/iomgr/executor.h +2 -1
  124. data/src/core/lib/iomgr/internal_errqueue.cc +2 -4
  125. data/src/core/lib/iomgr/iomgr.cc +5 -0
  126. data/src/core/lib/iomgr/iomgr.h +7 -0
  127. data/src/core/lib/iomgr/iomgr_custom.cc +9 -2
  128. data/src/core/lib/iomgr/iomgr_internal.cc +6 -0
  129. data/src/core/lib/iomgr/iomgr_internal.h +9 -1
  130. data/src/core/lib/iomgr/iomgr_posix.cc +10 -2
  131. data/src/core/lib/iomgr/iomgr_windows.cc +10 -2
  132. data/src/core/lib/iomgr/port.h +19 -0
  133. data/src/core/lib/iomgr/tcp_client_windows.cc +6 -4
  134. data/src/core/lib/iomgr/tcp_custom.cc +1 -1
  135. data/src/core/lib/iomgr/tcp_posix.cc +158 -54
  136. data/src/core/lib/iomgr/tcp_windows.cc +1 -1
  137. data/src/core/lib/iomgr/wakeup_fd_posix.cc +1 -19
  138. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +10 -6
  139. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +2 -1
  140. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +3 -6
  141. data/src/core/lib/security/credentials/tls/spiffe_credentials.cc +129 -0
  142. data/src/core/lib/security/credentials/tls/spiffe_credentials.h +62 -0
  143. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +7 -2
  144. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +28 -17
  145. data/src/core/lib/security/security_connector/ssl_utils.cc +134 -0
  146. data/src/core/lib/security/security_connector/ssl_utils.h +32 -0
  147. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +426 -0
  148. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +122 -0
  149. data/src/core/lib/security/transport/auth_filters.h +2 -2
  150. data/src/core/lib/security/transport/client_auth_filter.cc +35 -39
  151. data/src/core/lib/security/transport/secure_endpoint.cc +2 -2
  152. data/src/core/lib/security/transport/security_handshaker.cc +4 -3
  153. data/src/core/lib/slice/percent_encoding.cc +3 -3
  154. data/src/core/lib/slice/percent_encoding.h +3 -3
  155. data/src/core/lib/slice/slice.cc +27 -30
  156. data/src/core/lib/slice/slice_hash_table.h +2 -2
  157. data/src/core/lib/slice/slice_intern.cc +1 -1
  158. data/src/core/lib/slice/slice_internal.h +14 -3
  159. data/src/core/lib/slice/slice_weak_hash_table.h +4 -4
  160. data/src/core/lib/surface/byte_buffer_reader.cc +17 -0
  161. data/src/core/lib/surface/call.cc +8 -3
  162. data/src/core/lib/surface/completion_queue.cc +134 -148
  163. data/src/core/lib/surface/init.cc +78 -30
  164. data/src/core/lib/surface/init.h +1 -0
  165. data/src/core/lib/surface/lame_client.cc +4 -6
  166. data/src/core/lib/surface/version.cc +1 -1
  167. data/src/core/lib/transport/metadata.cc +66 -33
  168. data/src/core/lib/transport/metadata_batch.cc +1 -1
  169. data/src/core/lib/transport/metadata_batch.h +1 -1
  170. data/src/core/lib/transport/timeout_encoding.cc +1 -1
  171. data/src/core/lib/transport/timeout_encoding.h +1 -1
  172. data/src/core/lib/transport/transport.h +4 -3
  173. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +3 -3
  174. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +1 -1
  175. data/src/core/tsi/alts/handshaker/transport_security_common_api.cc +4 -3
  176. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +1 -1
  177. data/src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.cc +1 -1
  178. data/src/core/tsi/ssl_transport_security.cc +1 -5
  179. data/src/core/tsi/ssl_transport_security.h +24 -4
  180. data/src/ruby/bin/math_pb.rb +18 -16
  181. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +4 -0
  182. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +6 -0
  183. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  184. data/src/ruby/lib/grpc/version.rb +1 -1
  185. data/src/ruby/pb/README.md +1 -1
  186. data/src/ruby/pb/grpc/health/v1/health_pb.rb +13 -10
  187. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +18 -0
  188. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +3 -1
  189. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +58 -56
  190. data/src/ruby/pb/src/proto/grpc/testing/test_pb.rb +2 -0
  191. data/third_party/cares/cares/ares.h +12 -0
  192. data/third_party/cares/cares/ares_create_query.c +5 -1
  193. data/third_party/cares/cares/ares_data.c +74 -73
  194. data/third_party/cares/cares/ares_destroy.c +6 -1
  195. data/third_party/cares/cares/ares_gethostbyaddr.c +5 -5
  196. data/third_party/cares/cares/ares_gethostbyname.c +15 -4
  197. data/third_party/cares/cares/ares_getnameinfo.c +11 -0
  198. data/third_party/cares/cares/ares_init.c +274 -173
  199. data/third_party/cares/cares/ares_library_init.c +21 -3
  200. data/third_party/cares/cares/ares_options.c +6 -2
  201. data/third_party/cares/cares/ares_parse_naptr_reply.c +7 -6
  202. data/third_party/cares/cares/ares_parse_ptr_reply.c +4 -2
  203. data/third_party/cares/cares/ares_platform.c +7 -0
  204. data/third_party/cares/cares/ares_private.h +19 -11
  205. data/third_party/cares/cares/ares_process.c +27 -2
  206. data/third_party/cares/cares/ares_rules.h +1 -1
  207. data/third_party/cares/cares/ares_search.c +7 -0
  208. data/third_party/cares/cares/ares_send.c +6 -0
  209. data/third_party/cares/cares/ares_strsplit.c +174 -0
  210. data/third_party/cares/cares/ares_strsplit.h +43 -0
  211. data/third_party/cares/cares/ares_version.h +4 -4
  212. data/third_party/cares/cares/config-win32.h +1 -1
  213. data/third_party/cares/cares/inet_ntop.c +2 -3
  214. data/third_party/cares/config_darwin/ares_config.h +3 -0
  215. data/third_party/cares/config_freebsd/ares_config.h +3 -0
  216. data/third_party/cares/config_linux/ares_config.h +3 -0
  217. data/third_party/cares/config_openbsd/ares_config.h +3 -0
  218. metadata +39 -37
  219. data/src/core/ext/filters/client_channel/request_routing.cc +0 -946
  220. data/src/core/ext/filters/client_channel/request_routing.h +0 -181
  221. data/src/core/lib/gprpp/atomic_with_atm.h +0 -57
  222. data/src/core/lib/gprpp/atomic_with_std.h +0 -35
  223. data/src/core/lib/iomgr/wakeup_fd_cv.cc +0 -107
  224. data/src/core/lib/iomgr/wakeup_fd_cv.h +0 -69
@@ -71,11 +71,11 @@ class SubchannelNode : public BaseNode {
71
71
  grpc_json* RenderJson() override;
72
72
 
73
73
  // proxy methods to composed classes.
74
- void AddTraceEvent(ChannelTrace::Severity severity, grpc_slice data) {
74
+ void AddTraceEvent(ChannelTrace::Severity severity, const grpc_slice& data) {
75
75
  trace_.AddTraceEvent(severity, data);
76
76
  }
77
77
  void AddTraceEventWithReference(ChannelTrace::Severity severity,
78
- grpc_slice data,
78
+ const grpc_slice& data,
79
79
  RefCountedPtr<BaseNode> referenced_channel) {
80
80
  trace_.AddTraceEventWithReference(severity, data,
81
81
  std::move(referenced_channel));
@@ -21,47 +21,35 @@
21
21
  #include "src/core/ext/filters/client_channel/client_channel_factory.h"
22
22
  #include "src/core/lib/channel/channel_args.h"
23
23
 
24
- void grpc_client_channel_factory_ref(grpc_client_channel_factory* factory) {
25
- factory->vtable->ref(factory);
26
- }
24
+ // Channel arg key for client channel factory.
25
+ #define GRPC_ARG_CLIENT_CHANNEL_FACTORY "grpc.client_channel_factory"
27
26
 
28
- void grpc_client_channel_factory_unref(grpc_client_channel_factory* factory) {
29
- factory->vtable->unref(factory);
30
- }
27
+ namespace grpc_core {
31
28
 
32
- grpc_core::Subchannel* grpc_client_channel_factory_create_subchannel(
33
- grpc_client_channel_factory* factory, const grpc_channel_args* args) {
34
- return factory->vtable->create_subchannel(factory, args);
35
- }
29
+ namespace {
36
30
 
37
- grpc_channel* grpc_client_channel_factory_create_channel(
38
- grpc_client_channel_factory* factory, const char* target,
39
- grpc_client_channel_type type, const grpc_channel_args* args) {
40
- return factory->vtable->create_client_channel(factory, target, type, args);
31
+ void* factory_arg_copy(void* f) { return f; }
32
+ void factory_arg_destroy(void* f) {}
33
+ int factory_arg_cmp(void* factory1, void* factory2) {
34
+ return GPR_ICMP(factory1, factory2);
41
35
  }
36
+ const grpc_arg_pointer_vtable factory_arg_vtable = {
37
+ factory_arg_copy, factory_arg_destroy, factory_arg_cmp};
42
38
 
43
- static void* factory_arg_copy(void* factory) {
44
- grpc_client_channel_factory_ref(
45
- static_cast<grpc_client_channel_factory*>(factory));
46
- return factory;
47
- }
39
+ } // namespace
48
40
 
49
- static void factory_arg_destroy(void* factory) {
50
- grpc_client_channel_factory_unref(
51
- static_cast<grpc_client_channel_factory*>(factory));
41
+ grpc_arg ClientChannelFactory::CreateChannelArg(ClientChannelFactory* factory) {
42
+ return grpc_channel_arg_pointer_create(
43
+ const_cast<char*>(GRPC_ARG_CLIENT_CHANNEL_FACTORY), factory,
44
+ &factory_arg_vtable);
52
45
  }
53
46
 
54
- static int factory_arg_cmp(void* factory1, void* factory2) {
55
- if (factory1 < factory2) return -1;
56
- if (factory1 > factory2) return 1;
57
- return 0;
47
+ ClientChannelFactory* ClientChannelFactory::GetFromChannelArgs(
48
+ const grpc_channel_args* args) {
49
+ const grpc_arg* arg =
50
+ grpc_channel_args_find(args, GRPC_ARG_CLIENT_CHANNEL_FACTORY);
51
+ if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr;
52
+ return static_cast<ClientChannelFactory*>(arg->value.pointer.p);
58
53
  }
59
54
 
60
- static const grpc_arg_pointer_vtable factory_arg_vtable = {
61
- factory_arg_copy, factory_arg_destroy, factory_arg_cmp};
62
-
63
- grpc_arg grpc_client_channel_factory_create_channel_arg(
64
- grpc_client_channel_factory* factory) {
65
- return grpc_channel_arg_pointer_create((char*)GRPC_ARG_CLIENT_CHANNEL_FACTORY,
66
- factory, &factory_arg_vtable);
67
- }
55
+ } // namespace grpc_core
@@ -24,51 +24,32 @@
24
24
  #include <grpc/impl/codegen/grpc_types.h>
25
25
 
26
26
  #include "src/core/ext/filters/client_channel/subchannel.h"
27
- #include "src/core/lib/channel/channel_stack.h"
27
+ #include "src/core/lib/gprpp/abstract.h"
28
28
 
29
- // Channel arg key for client channel factory.
30
- #define GRPC_ARG_CLIENT_CHANNEL_FACTORY "grpc.client_channel_factory"
29
+ namespace grpc_core {
31
30
 
32
- typedef struct grpc_client_channel_factory grpc_client_channel_factory;
33
- typedef struct grpc_client_channel_factory_vtable
34
- grpc_client_channel_factory_vtable;
31
+ class ClientChannelFactory {
32
+ public:
33
+ virtual ~ClientChannelFactory() = default;
35
34
 
36
- typedef enum {
37
- GRPC_CLIENT_CHANNEL_TYPE_REGULAR, /** for the user-level regular calls */
38
- GRPC_CLIENT_CHANNEL_TYPE_LOAD_BALANCING, /** for communication with a load
39
- balancing service */
40
- } grpc_client_channel_type;
35
+ // Creates a subchannel with the specified args.
36
+ virtual Subchannel* CreateSubchannel(const grpc_channel_args* args)
37
+ GRPC_ABSTRACT;
41
38
 
42
- /** Constructor for new configured channels.
43
- Creating decorators around this type is encouraged to adapt behavior. */
44
- struct grpc_client_channel_factory {
45
- const grpc_client_channel_factory_vtable* vtable;
46
- };
47
-
48
- struct grpc_client_channel_factory_vtable {
49
- void (*ref)(grpc_client_channel_factory* factory);
50
- void (*unref)(grpc_client_channel_factory* factory);
51
- grpc_core::Subchannel* (*create_subchannel)(
52
- grpc_client_channel_factory* factory, const grpc_channel_args* args);
53
- grpc_channel* (*create_client_channel)(grpc_client_channel_factory* factory,
54
- const char* target,
55
- grpc_client_channel_type type,
56
- const grpc_channel_args* args);
57
- };
39
+ // Creates a channel for the specified target with the specified args.
40
+ virtual grpc_channel* CreateChannel(
41
+ const char* target, const grpc_channel_args* args) GRPC_ABSTRACT;
58
42
 
59
- void grpc_client_channel_factory_ref(grpc_client_channel_factory* factory);
60
- void grpc_client_channel_factory_unref(grpc_client_channel_factory* factory);
43
+ // Returns a channel arg containing the specified factory.
44
+ static grpc_arg CreateChannelArg(ClientChannelFactory* factory);
61
45
 
62
- /** Create a new grpc_subchannel */
63
- grpc_core::Subchannel* grpc_client_channel_factory_create_subchannel(
64
- grpc_client_channel_factory* factory, const grpc_channel_args* args);
46
+ // Returns the factory from args, or null if not found.
47
+ static ClientChannelFactory* GetFromChannelArgs(
48
+ const grpc_channel_args* args);
65
49
 
66
- /** Create a new grpc_channel */
67
- grpc_channel* grpc_client_channel_factory_create_channel(
68
- grpc_client_channel_factory* factory, const char* target,
69
- grpc_client_channel_type type, const grpc_channel_args* args);
50
+ GRPC_ABSTRACT_BASE_CLASS
51
+ };
70
52
 
71
- grpc_arg grpc_client_channel_factory_create_channel_arg(
72
- grpc_client_channel_factory* factory);
53
+ } // namespace grpc_core
73
54
 
74
55
  #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_CLIENT_CHANNEL_FACTORY_H */
@@ -66,10 +66,13 @@ Subchannel* GlobalSubchannelPool::RegisterSubchannel(SubchannelKey* key,
66
66
  // Check to see if a subchannel already exists.
67
67
  c = static_cast<Subchannel*>(grpc_avl_get(old_map, key, nullptr));
68
68
  if (c != nullptr) {
69
- // The subchannel already exists. Reuse it.
69
+ // The subchannel already exists. Try to reuse it.
70
70
  c = GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(c, "subchannel_register+reuse");
71
- GRPC_SUBCHANNEL_UNREF(constructed, "subchannel_register+found_existing");
72
- // Exit the CAS loop without modifying the shared map.
71
+ if (c != nullptr) {
72
+ GRPC_SUBCHANNEL_UNREF(constructed,
73
+ "subchannel_register+found_existing");
74
+ // Exit the CAS loop without modifying the shared map.
75
+ } // Else, reuse failed, so retry CAS loop.
73
76
  } else {
74
77
  // There hasn't been such subchannel. Add one.
75
78
  // Note that we should ref the old map first because grpc_avl_add() will
@@ -128,7 +131,7 @@ Subchannel* GlobalSubchannelPool::FindSubchannel(SubchannelKey* key) {
128
131
  grpc_avl index = grpc_avl_ref(subchannel_map_, nullptr);
129
132
  gpr_mu_unlock(&mu_);
130
133
  Subchannel* c = static_cast<Subchannel*>(grpc_avl_get(index, key, nullptr));
131
- if (c != nullptr) GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(c, "found_from_pool");
134
+ if (c != nullptr) c = GRPC_SUBCHANNEL_REF_FROM_WEAK_REF(c, "found_from_pool");
132
135
  grpc_avl_unref(index, nullptr);
133
136
  return c;
134
137
  }
@@ -144,7 +144,7 @@ void HttpConnectHandshaker::OnWriteDone(void* arg, grpc_error* error) {
144
144
  // The read callback inherits our ref to the handshaker.
145
145
  grpc_endpoint_read(handshaker->args_->endpoint,
146
146
  handshaker->args_->read_buffer,
147
- &handshaker->response_read_closure_);
147
+ &handshaker->response_read_closure_, /*urgent=*/true);
148
148
  gpr_mu_unlock(&handshaker->mu_);
149
149
  }
150
150
  }
@@ -207,7 +207,7 @@ void HttpConnectHandshaker::OnReadDone(void* arg, grpc_error* error) {
207
207
  grpc_slice_buffer_reset_and_unref_internal(handshaker->args_->read_buffer);
208
208
  grpc_endpoint_read(handshaker->args_->endpoint,
209
209
  handshaker->args_->read_buffer,
210
- &handshaker->response_read_closure_);
210
+ &handshaker->response_read_closure_, /*urgent=*/true);
211
211
  gpr_mu_unlock(&handshaker->mu_);
212
212
  return;
213
213
  }
@@ -23,11 +23,45 @@
23
23
  #include "src/core/ext/filters/client_channel/lb_policy_registry.h"
24
24
  #include "src/core/lib/iomgr/combiner.h"
25
25
 
26
- grpc_core::DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(
27
- false, "lb_policy_refcount");
28
-
29
26
  namespace grpc_core {
30
27
 
28
+ DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(false, "lb_policy_refcount");
29
+
30
+ //
31
+ // LoadBalancingPolicy
32
+ //
33
+
34
+ LoadBalancingPolicy::LoadBalancingPolicy(Args args, intptr_t initial_refcount)
35
+ : InternallyRefCounted(&grpc_trace_lb_policy_refcount, initial_refcount),
36
+ combiner_(GRPC_COMBINER_REF(args.combiner, "lb_policy")),
37
+ interested_parties_(grpc_pollset_set_create()),
38
+ channel_control_helper_(std::move(args.channel_control_helper)) {}
39
+
40
+ LoadBalancingPolicy::~LoadBalancingPolicy() {
41
+ grpc_pollset_set_destroy(interested_parties_);
42
+ GRPC_COMBINER_UNREF(combiner_, "lb_policy");
43
+ }
44
+
45
+ void LoadBalancingPolicy::Orphan() {
46
+ // Invoke ShutdownAndUnrefLocked() inside of the combiner.
47
+ // TODO(roth): Is this actually needed? We should already be in the
48
+ // combiner here. Note that if we directly call ShutdownLocked(),
49
+ // then we can probably remove the hack whereby the helper is
50
+ // destroyed at shutdown instead of at destruction.
51
+ GRPC_CLOSURE_SCHED(
52
+ GRPC_CLOSURE_CREATE(&LoadBalancingPolicy::ShutdownAndUnrefLocked, this,
53
+ grpc_combiner_scheduler(combiner_)),
54
+ GRPC_ERROR_NONE);
55
+ }
56
+
57
+ void LoadBalancingPolicy::ShutdownAndUnrefLocked(void* arg,
58
+ grpc_error* ignored) {
59
+ LoadBalancingPolicy* policy = static_cast<LoadBalancingPolicy*>(arg);
60
+ policy->ShutdownLocked();
61
+ policy->channel_control_helper_.reset();
62
+ policy->Unref();
63
+ }
64
+
31
65
  grpc_json* LoadBalancingPolicy::ParseLoadBalancingConfig(
32
66
  const grpc_json* lb_config_array) {
33
67
  if (lb_config_array == nullptr || lb_config_array->type != GRPC_JSON_ARRAY) {
@@ -54,35 +88,78 @@ grpc_json* LoadBalancingPolicy::ParseLoadBalancingConfig(
54
88
  return nullptr;
55
89
  }
56
90
 
57
- LoadBalancingPolicy::LoadBalancingPolicy(Args args)
58
- : InternallyRefCounted(&grpc_trace_lb_policy_refcount),
59
- combiner_(GRPC_COMBINER_REF(args.combiner, "lb_policy")),
60
- client_channel_factory_(args.client_channel_factory),
61
- subchannel_pool_(std::move(args.subchannel_pool)),
62
- interested_parties_(grpc_pollset_set_create()),
63
- request_reresolution_(nullptr) {}
91
+ //
92
+ // LoadBalancingPolicy::UpdateArgs
93
+ //
64
94
 
65
- LoadBalancingPolicy::~LoadBalancingPolicy() {
66
- grpc_pollset_set_destroy(interested_parties_);
67
- GRPC_COMBINER_UNREF(combiner_, "lb_policy");
95
+ LoadBalancingPolicy::UpdateArgs::UpdateArgs(const UpdateArgs& other) {
96
+ addresses = other.addresses;
97
+ config = other.config;
98
+ args = grpc_channel_args_copy(other.args);
68
99
  }
69
100
 
70
- void LoadBalancingPolicy::TryReresolutionLocked(
71
- grpc_core::TraceFlag* grpc_lb_trace, grpc_error* error) {
72
- if (request_reresolution_ != nullptr) {
73
- GRPC_CLOSURE_SCHED(request_reresolution_, error);
74
- request_reresolution_ = nullptr;
75
- if (grpc_lb_trace->enabled()) {
76
- gpr_log(GPR_INFO,
77
- "%s %p: scheduling re-resolution closure with error=%s.",
78
- grpc_lb_trace->name(), this, grpc_error_string(error));
79
- }
80
- } else {
81
- if (grpc_lb_trace->enabled()) {
82
- gpr_log(GPR_INFO, "%s %p: no available re-resolution closure.",
83
- grpc_lb_trace->name(), this);
84
- }
101
+ LoadBalancingPolicy::UpdateArgs::UpdateArgs(UpdateArgs&& other) {
102
+ addresses = std::move(other.addresses);
103
+ config = std::move(other.config);
104
+ // TODO(roth): Use std::move() once channel args is converted to C++.
105
+ args = other.args;
106
+ other.args = nullptr;
107
+ }
108
+
109
+ LoadBalancingPolicy::UpdateArgs& LoadBalancingPolicy::UpdateArgs::operator=(
110
+ const UpdateArgs& other) {
111
+ addresses = other.addresses;
112
+ config = other.config;
113
+ grpc_channel_args_destroy(args);
114
+ args = grpc_channel_args_copy(other.args);
115
+ return *this;
116
+ }
117
+
118
+ LoadBalancingPolicy::UpdateArgs& LoadBalancingPolicy::UpdateArgs::operator=(
119
+ UpdateArgs&& other) {
120
+ addresses = std::move(other.addresses);
121
+ config = std::move(other.config);
122
+ // TODO(roth): Use std::move() once channel args is converted to C++.
123
+ grpc_channel_args_destroy(args);
124
+ args = other.args;
125
+ other.args = nullptr;
126
+ return *this;
127
+ }
128
+
129
+ //
130
+ // LoadBalancingPolicy::QueuePicker
131
+ //
132
+
133
+ LoadBalancingPolicy::PickResult LoadBalancingPolicy::QueuePicker::Pick(
134
+ PickArgs* pick, grpc_error** error) {
135
+ // We invoke the parent's ExitIdleLocked() via a closure instead
136
+ // of doing it directly here, for two reasons:
137
+ // 1. ExitIdleLocked() may cause the policy's state to change and
138
+ // a new picker to be delivered to the channel. If that new
139
+ // picker is delivered before ExitIdleLocked() returns, then by
140
+ // the time this function returns, the pick will already have
141
+ // been processed, and we'll be trying to re-process the same
142
+ // pick again, leading to a crash.
143
+ // 2. In a subsequent PR, we will split the data plane and control
144
+ // plane synchronization into separate combiners, at which
145
+ // point this will need to hop from the data plane combiner into
146
+ // the control plane combiner.
147
+ if (!exit_idle_called_) {
148
+ exit_idle_called_ = true;
149
+ parent_->Ref().release(); // ref held by closure.
150
+ GRPC_CLOSURE_SCHED(
151
+ GRPC_CLOSURE_CREATE(&CallExitIdle, parent_.get(),
152
+ grpc_combiner_scheduler(parent_->combiner())),
153
+ GRPC_ERROR_NONE);
85
154
  }
155
+ return PICK_QUEUE;
156
+ }
157
+
158
+ void LoadBalancingPolicy::QueuePicker::CallExitIdle(void* arg,
159
+ grpc_error* error) {
160
+ LoadBalancingPolicy* parent = static_cast<LoadBalancingPolicy*>(arg);
161
+ parent->ExitIdleLocked();
162
+ parent->Unref();
86
163
  }
87
164
 
88
165
  } // namespace grpc_core
@@ -22,9 +22,9 @@
22
22
  #include <grpc/support/port_platform.h>
23
23
 
24
24
  #include "src/core/ext/filters/client_channel/client_channel_channelz.h"
25
- #include "src/core/ext/filters/client_channel/client_channel_factory.h"
25
+ #include "src/core/ext/filters/client_channel/server_address.h"
26
+ #include "src/core/ext/filters/client_channel/service_config.h"
26
27
  #include "src/core/ext/filters/client_channel/subchannel.h"
27
- #include "src/core/ext/filters/client_channel/subchannel_pool_interface.h"
28
28
  #include "src/core/lib/gprpp/abstract.h"
29
29
  #include "src/core/lib/gprpp/orphanable.h"
30
30
  #include "src/core/lib/gprpp/ref_counted_ptr.h"
@@ -38,13 +38,199 @@ namespace grpc_core {
38
38
 
39
39
  /// Interface for load balancing policies.
40
40
  ///
41
+ /// The following concepts are used here:
42
+ ///
43
+ /// Channel: An abstraction that manages connections to backend servers
44
+ /// on behalf of a client application. The application creates a channel
45
+ /// for a given server name and then sends RPCs on it, and the channel
46
+ /// figures out which backend server to send each RPC to. A channel
47
+ /// contains a resolver, a load balancing policy (or a tree of LB policies),
48
+ /// and a set of one or more subchannels.
49
+ ///
50
+ /// Subchannel: A subchannel represents a connection to one backend server.
51
+ /// The LB policy decides which subchannels to create, manages the
52
+ /// connectivity state of those subchannels, and decides which subchannel
53
+ /// to send any given RPC to.
54
+ ///
55
+ /// Resolver: A plugin that takes a gRPC server URI and resolves it to a
56
+ /// list of one or more addresses and a service config, as described
57
+ /// in https://github.com/grpc/grpc/blob/master/doc/naming.md. See
58
+ /// resolver.h for the resolver API.
59
+ ///
60
+ /// Load Balancing (LB) Policy: A plugin that takes a list of addresses
61
+ /// from the resolver, maintains and manages a subchannel for each
62
+ /// backend address, and decides which subchannel to send each RPC on.
63
+ /// An LB policy has two parts:
64
+ /// - A LoadBalancingPolicy, which deals with the control plane work of
65
+ /// managing subchannels.
66
+ /// - A SubchannelPicker, which handles the data plane work of
67
+ /// determining which subchannel a given RPC should be sent on.
68
+
69
+ /// LoadBalacingPolicy API.
70
+ ///
41
71
  /// Note: All methods with a "Locked" suffix must be called from the
42
72
  /// combiner passed to the constructor.
43
73
  ///
44
74
  /// Any I/O done by the LB policy should be done under the pollset_set
45
75
  /// returned by \a interested_parties().
76
+ // TODO(roth): Once we move to EventManager-based polling, remove the
77
+ // interested_parties() hooks from the API.
46
78
  class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
47
79
  public:
80
+ /// Arguments used when picking a subchannel for an RPC.
81
+ struct PickArgs {
82
+ ///
83
+ /// Input parameters.
84
+ ///
85
+ /// Initial metadata associated with the picking call.
86
+ /// The LB policy may use the existing metadata to influence its routing
87
+ /// decision, and it may add new metadata elements to be sent with the
88
+ /// call to the chosen backend.
89
+ // TODO(roth): Provide a more generic metadata API here.
90
+ grpc_metadata_batch* initial_metadata = nullptr;
91
+ /// Storage for LB token in \a initial_metadata, or nullptr if not used.
92
+ // TODO(roth): Remove this from the API. Maybe have the LB policy
93
+ // allocate this on the arena instead?
94
+ grpc_linked_mdelem lb_token_mdelem_storage;
95
+ ///
96
+ /// Output parameters.
97
+ ///
98
+ /// Will be set to the selected subchannel, or nullptr on failure or when
99
+ /// the LB policy decides to drop the call.
100
+ RefCountedPtr<ConnectedSubchannel> connected_subchannel;
101
+ /// Callback set by lb policy to be notified of trailing metadata.
102
+ /// The callback must be scheduled on grpc_schedule_on_exec_ctx.
103
+ // TODO(roth): Provide a cleaner callback API.
104
+ grpc_closure* recv_trailing_metadata_ready = nullptr;
105
+ /// The address that will be set to point to the original
106
+ /// recv_trailing_metadata_ready callback, to be invoked by the LB
107
+ /// policy's recv_trailing_metadata_ready callback when complete.
108
+ /// Must be non-null if recv_trailing_metadata_ready is non-null.
109
+ // TODO(roth): Consider making the recv_trailing_metadata closure a
110
+ // synchronous callback, in which case it is not responsible for
111
+ // chaining to the next callback, so this can be removed from the API.
112
+ grpc_closure** original_recv_trailing_metadata_ready = nullptr;
113
+ /// If this is not nullptr, then the client channel will point it to the
114
+ /// call's trailing metadata before invoking recv_trailing_metadata_ready.
115
+ /// If this is nullptr, then the callback will still be called.
116
+ /// The lb does not have ownership of the metadata.
117
+ // TODO(roth): If we make this a synchronous callback, then this can
118
+ // be passed to the callback as a parameter and can be removed from
119
+ // the API here.
120
+ grpc_metadata_batch** recv_trailing_metadata = nullptr;
121
+ };
122
+
123
+ /// The result of picking a subchannel for an RPC.
124
+ enum PickResult {
125
+ // Pick complete. If connected_subchannel is non-null, client channel
126
+ // can immediately proceed with the call on connected_subchannel;
127
+ // otherwise, call should be dropped.
128
+ PICK_COMPLETE,
129
+ // Pick cannot be completed until something changes on the control
130
+ // plane. Client channel will queue the pick and try again the
131
+ // next time the picker is updated.
132
+ PICK_QUEUE,
133
+ // LB policy is in transient failure. If the pick is wait_for_ready,
134
+ // client channel will wait for the next picker and try again;
135
+ // otherwise, the call will be failed immediately (although it may
136
+ // be retried if the client channel is configured to do so).
137
+ // The Pick() method will set its error parameter if this value is
138
+ // returned.
139
+ PICK_TRANSIENT_FAILURE,
140
+ };
141
+
142
+ /// A subchannel picker is the object used to pick the subchannel to
143
+ /// use for a given RPC.
144
+ ///
145
+ /// Pickers are intended to encapsulate all of the state and logic
146
+ /// needed on the data plane (i.e., to actually process picks for
147
+ /// individual RPCs sent on the channel) while excluding all of the
148
+ /// state and logic needed on the control plane (i.e., resolver
149
+ /// updates, connectivity state notifications, etc); the latter should
150
+ /// live in the LB policy object itself.
151
+ ///
152
+ /// Currently, pickers are always accessed from within the
153
+ /// client_channel combiner, so they do not have to be thread-safe.
154
+ // TODO(roth): In a subsequent PR, split the data plane work (i.e.,
155
+ // the interaction with the picker) and the control plane work (i.e.,
156
+ // the interaction with the LB policy) into two different
157
+ // synchronization mechanisms, to avoid lock contention between the two.
158
+ class SubchannelPicker {
159
+ public:
160
+ SubchannelPicker() = default;
161
+ virtual ~SubchannelPicker() = default;
162
+
163
+ virtual PickResult Pick(PickArgs* pick, grpc_error** error) GRPC_ABSTRACT;
164
+
165
+ GRPC_ABSTRACT_BASE_CLASS
166
+ };
167
+
168
+ /// A proxy object used by the LB policy to communicate with the client
169
+ /// channel.
170
+ class ChannelControlHelper {
171
+ public:
172
+ ChannelControlHelper() = default;
173
+ virtual ~ChannelControlHelper() = default;
174
+
175
+ /// Creates a new subchannel with the specified channel args.
176
+ virtual Subchannel* CreateSubchannel(const grpc_channel_args& args)
177
+ GRPC_ABSTRACT;
178
+
179
+ /// Creates a channel with the specified target and channel args.
180
+ /// This can be used in cases where the LB policy needs to create a
181
+ /// channel for its own use (e.g., to talk to an external load balancer).
182
+ virtual grpc_channel* CreateChannel(
183
+ const char* target, const grpc_channel_args& args) GRPC_ABSTRACT;
184
+
185
+ /// Sets the connectivity state and returns a new picker to be used
186
+ /// by the client channel.
187
+ virtual void UpdateState(grpc_connectivity_state state,
188
+ grpc_error* state_error,
189
+ UniquePtr<SubchannelPicker>) GRPC_ABSTRACT;
190
+
191
+ /// Requests that the resolver re-resolve.
192
+ virtual void RequestReresolution() GRPC_ABSTRACT;
193
+
194
+ GRPC_ABSTRACT_BASE_CLASS
195
+ };
196
+
197
+ /// Configuration for an LB policy instance.
198
+ // TODO(roth): Find a better JSON representation for this API.
199
+ class Config : public RefCounted<Config> {
200
+ public:
201
+ Config(const grpc_json* lb_config,
202
+ RefCountedPtr<ServiceConfig> service_config)
203
+ : json_(lb_config), service_config_(std::move(service_config)) {}
204
+
205
+ const char* name() const { return json_->key; }
206
+ const grpc_json* config() const { return json_->child; }
207
+ RefCountedPtr<ServiceConfig> service_config() const {
208
+ return service_config_;
209
+ }
210
+
211
+ private:
212
+ const grpc_json* json_;
213
+ RefCountedPtr<ServiceConfig> service_config_;
214
+ };
215
+
216
+ /// Data passed to the UpdateLocked() method when new addresses and
217
+ /// config are available.
218
+ struct UpdateArgs {
219
+ ServerAddressList addresses;
220
+ RefCountedPtr<Config> config;
221
+ const grpc_channel_args* args = nullptr;
222
+
223
+ // TODO(roth): Remove everything below once channel args is
224
+ // converted to a copyable and movable C++ object.
225
+ UpdateArgs() = default;
226
+ ~UpdateArgs() { grpc_channel_args_destroy(args); }
227
+ UpdateArgs(const UpdateArgs& other);
228
+ UpdateArgs(UpdateArgs&& other);
229
+ UpdateArgs& operator=(const UpdateArgs& other);
230
+ UpdateArgs& operator=(UpdateArgs&& other);
231
+ };
232
+
233
+ /// Args used to instantiate an LB policy.
48
234
  struct Args {
49
235
  /// The combiner under which all LB policy calls will be run.
50
236
  /// Policy does NOT take ownership of the reference to the combiner.
@@ -52,53 +238,17 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
52
238
  // API should change to take a smart pointer that does pass ownership
53
239
  // of a reference.
54
240
  grpc_combiner* combiner = nullptr;
55
- /// Used to create channels and subchannels.
56
- grpc_client_channel_factory* client_channel_factory = nullptr;
57
- /// Subchannel pool.
58
- RefCountedPtr<SubchannelPoolInterface> subchannel_pool;
59
- /// Channel args from the resolver.
60
- /// Note that the LB policy gets the set of addresses from the
61
- /// GRPC_ARG_SERVER_ADDRESS_LIST channel arg.
62
- grpc_channel_args* args = nullptr;
63
- /// Load balancing config from the resolver.
64
- grpc_json* lb_config = nullptr;
241
+ /// Channel control helper.
242
+ /// Note: LB policies MUST NOT call any method on the helper from
243
+ /// their constructor.
244
+ UniquePtr<ChannelControlHelper> channel_control_helper;
245
+ /// Channel args.
246
+ // TODO(roth): Find a better channel args representation for this API.
247
+ const grpc_channel_args* args = nullptr;
65
248
  };
66
249
 
67
- /// State used for an LB pick.
68
- struct PickState {
69
- /// Initial metadata associated with the picking call.
70
- grpc_metadata_batch* initial_metadata = nullptr;
71
- /// Pointer to bitmask used for selective cancelling. See
72
- /// \a CancelMatchingPicksLocked() and \a GRPC_INITIAL_METADATA_* in
73
- /// grpc_types.h.
74
- uint32_t* initial_metadata_flags = nullptr;
75
- /// Storage for LB token in \a initial_metadata, or nullptr if not used.
76
- grpc_linked_mdelem lb_token_mdelem_storage;
77
- /// Closure to run when pick is complete, if not completed synchronously.
78
- /// If null, pick will fail if a result is not available synchronously.
79
- grpc_closure* on_complete = nullptr;
80
- // Callback set by lb policy to be notified of trailing metadata.
81
- // The callback must be scheduled on grpc_schedule_on_exec_ctx.
82
- grpc_closure* recv_trailing_metadata_ready = nullptr;
83
- // The address that will be set to point to the original
84
- // recv_trailing_metadata_ready callback, to be invoked by the LB
85
- // policy's recv_trailing_metadata_ready callback when complete.
86
- // Must be non-null if recv_trailing_metadata_ready is non-null.
87
- grpc_closure** original_recv_trailing_metadata_ready = nullptr;
88
- // If this is not nullptr, then the client channel will point it to the
89
- // call's trailing metadata before invoking recv_trailing_metadata_ready.
90
- // If this is nullptr, then the callback will still be called.
91
- // The lb does not have ownership of the metadata.
92
- grpc_metadata_batch** recv_trailing_metadata = nullptr;
93
- /// Will be set to the selected subchannel, or nullptr on failure or when
94
- /// the LB policy decides to drop the call.
95
- RefCountedPtr<ConnectedSubchannel> connected_subchannel;
96
- /// Will be populated with context to pass to the subchannel call, if
97
- /// needed.
98
- grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT] = {};
99
- /// Next pointer. For internal use by LB policy.
100
- PickState* next = nullptr;
101
- };
250
+ explicit LoadBalancingPolicy(Args args, intptr_t initial_refcount = 1);
251
+ virtual ~LoadBalancingPolicy();
102
252
 
103
253
  // Not copyable nor movable.
104
254
  LoadBalancingPolicy(const LoadBalancingPolicy&) = delete;
@@ -107,135 +257,103 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
107
257
  /// Returns the name of the LB policy.
108
258
  virtual const char* name() const GRPC_ABSTRACT;
109
259
 
110
- /// Updates the policy with a new set of \a args and a new \a lb_config from
111
- /// the resolver. Note that the LB policy gets the set of addresses from the
112
- /// GRPC_ARG_SERVER_ADDRESS_LIST channel arg.
113
- virtual void UpdateLocked(const grpc_channel_args& args,
114
- grpc_json* lb_config) GRPC_ABSTRACT;
115
-
116
- /// Finds an appropriate subchannel for a call, based on data in \a pick.
117
- /// \a pick must remain alive until the pick is complete.
118
- ///
119
- /// If a result is known immediately, returns true, setting \a *error
120
- /// upon failure. Otherwise, \a pick->on_complete will be invoked once
121
- /// the pick is complete with its error argument set to indicate success
122
- /// or failure.
123
- ///
124
- /// If \a pick->on_complete is null and no result is known immediately,
125
- /// a synchronous failure will be returned (i.e., \a *error will be
126
- /// set and true will be returned).
127
- virtual bool PickLocked(PickState* pick, grpc_error** error) GRPC_ABSTRACT;
128
-
129
- /// Cancels \a pick.
130
- /// The \a on_complete callback of the pending pick will be invoked with
131
- /// \a pick->connected_subchannel set to null.
132
- virtual void CancelPickLocked(PickState* pick,
133
- grpc_error* error) GRPC_ABSTRACT;
134
-
135
- /// Cancels all pending picks for which their \a initial_metadata_flags (as
136
- /// given in the call to \a PickLocked()) matches
137
- /// \a initial_metadata_flags_eq when ANDed with
138
- /// \a initial_metadata_flags_mask.
139
- virtual void CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask,
140
- uint32_t initial_metadata_flags_eq,
141
- grpc_error* error) GRPC_ABSTRACT;
142
-
143
- /// Requests a notification when the connectivity state of the policy
144
- /// changes from \a *state. When that happens, sets \a *state to the
145
- /// new state and schedules \a closure.
146
- virtual void NotifyOnStateChangeLocked(grpc_connectivity_state* state,
147
- grpc_closure* closure) GRPC_ABSTRACT;
148
-
149
- /// Returns the policy's current connectivity state. Sets \a error to
150
- /// the associated error, if any.
151
- virtual grpc_connectivity_state CheckConnectivityLocked(
152
- grpc_error** connectivity_error) GRPC_ABSTRACT;
153
-
154
- /// Hands off pending picks to \a new_policy.
155
- virtual void HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy)
156
- GRPC_ABSTRACT;
260
+ /// Updates the policy with new data from the resolver. Will be invoked
261
+ /// immediately after LB policy is constructed, and then again whenever
262
+ /// the resolver returns a new result.
263
+ virtual void UpdateLocked(UpdateArgs) GRPC_ABSTRACT; // NOLINT
157
264
 
158
265
  /// Tries to enter a READY connectivity state.
159
- /// TODO(roth): As part of restructuring how we handle IDLE state,
160
- /// consider whether this method is still needed.
161
- virtual void ExitIdleLocked() GRPC_ABSTRACT;
266
+ /// This is a no-op by default, since most LB policies never go into
267
+ /// IDLE state.
268
+ virtual void ExitIdleLocked() {}
162
269
 
163
270
  /// Resets connection backoff.
164
271
  virtual void ResetBackoffLocked() GRPC_ABSTRACT;
165
272
 
166
273
  /// Populates child_subchannels and child_channels with the uuids of this
167
- /// LB policy's referenced children. This is not invoked from the
168
- /// client_channel's combiner. The implementation is responsible for
169
- /// providing its own synchronization.
274
+ /// LB policy's referenced children.
275
+ ///
276
+ /// This is not invoked from the client_channel's combiner. The
277
+ /// implementation is responsible for providing its own synchronization.
170
278
  virtual void FillChildRefsForChannelz(
171
279
  channelz::ChildRefsList* child_subchannels,
172
280
  channelz::ChildRefsList* child_channels) GRPC_ABSTRACT;
173
281
 
174
- void Orphan() override {
175
- // Invoke ShutdownAndUnrefLocked() inside of the combiner.
176
- GRPC_CLOSURE_SCHED(
177
- GRPC_CLOSURE_CREATE(&LoadBalancingPolicy::ShutdownAndUnrefLocked, this,
178
- grpc_combiner_scheduler(combiner_)),
179
- GRPC_ERROR_NONE);
282
+ void set_channelz_node(
283
+ RefCountedPtr<channelz::ClientChannelNode> channelz_node) {
284
+ channelz_node_ = std::move(channelz_node);
180
285
  }
181
286
 
287
+ grpc_pollset_set* interested_parties() const { return interested_parties_; }
288
+
289
+ void Orphan() override;
290
+
182
291
  /// Returns the JSON node of policy (with both policy name and config content)
183
292
  /// given the JSON node of a LoadBalancingConfig array.
184
293
  static grpc_json* ParseLoadBalancingConfig(const grpc_json* lb_config_array);
185
294
 
186
- /// Sets the re-resolution closure to \a request_reresolution.
187
- void SetReresolutionClosureLocked(grpc_closure* request_reresolution) {
188
- GPR_ASSERT(request_reresolution_ == nullptr);
189
- request_reresolution_ = request_reresolution;
190
- }
295
+ // A picker that returns PICK_QUEUE for all picks.
296
+ // Also calls the parent LB policy's ExitIdleLocked() method when the
297
+ // first pick is seen.
298
+ class QueuePicker : public SubchannelPicker {
299
+ public:
300
+ explicit QueuePicker(RefCountedPtr<LoadBalancingPolicy> parent)
301
+ : parent_(std::move(parent)) {}
191
302
 
192
- grpc_pollset_set* interested_parties() const { return interested_parties_; }
303
+ PickResult Pick(PickArgs* pick, grpc_error** error) override;
193
304
 
194
- // Callers that need their own reference can call the returned
195
- // object's Ref() method.
196
- SubchannelPoolInterface* subchannel_pool() const {
197
- return subchannel_pool_.get();
198
- }
305
+ private:
306
+ static void CallExitIdle(void* arg, grpc_error* error);
307
+
308
+ RefCountedPtr<LoadBalancingPolicy> parent_;
309
+ bool exit_idle_called_ = false;
310
+ };
311
+
312
+ // A picker that returns PICK_TRANSIENT_FAILURE for all picks.
313
+ class TransientFailurePicker : public SubchannelPicker {
314
+ public:
315
+ explicit TransientFailurePicker(grpc_error* error) : error_(error) {}
316
+ ~TransientFailurePicker() override { GRPC_ERROR_UNREF(error_); }
317
+
318
+ PickResult Pick(PickArgs* pick, grpc_error** error) override {
319
+ *error = GRPC_ERROR_REF(error_);
320
+ return PICK_TRANSIENT_FAILURE;
321
+ }
322
+
323
+ private:
324
+ grpc_error* error_;
325
+ };
199
326
 
200
327
  GRPC_ABSTRACT_BASE_CLASS
201
328
 
202
329
  protected:
203
- GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
330
+ grpc_combiner* combiner() const { return combiner_; }
204
331
 
205
- explicit LoadBalancingPolicy(Args args);
206
- virtual ~LoadBalancingPolicy();
332
+ // Note: LB policies MUST NOT call any method on the helper from their
333
+ // constructor.
334
+ // Note: This will return null after ShutdownLocked() has been called.
335
+ ChannelControlHelper* channel_control_helper() const {
336
+ return channel_control_helper_.get();
337
+ }
207
338
 
208
- grpc_combiner* combiner() const { return combiner_; }
209
- grpc_client_channel_factory* client_channel_factory() const {
210
- return client_channel_factory_;
339
+ channelz::ClientChannelNode* channelz_node() const {
340
+ return channelz_node_.get();
211
341
  }
212
342
 
213
- /// Shuts down the policy. Any pending picks that have not been
214
- /// handed off to a new policy via HandOffPendingPicksLocked() will be
215
- /// failed.
343
+ /// Shuts down the policy.
216
344
  virtual void ShutdownLocked() GRPC_ABSTRACT;
217
345
 
218
- /// Tries to request a re-resolution.
219
- void TryReresolutionLocked(grpc_core::TraceFlag* grpc_lb_trace,
220
- grpc_error* error);
221
-
222
346
  private:
223
- static void ShutdownAndUnrefLocked(void* arg, grpc_error* ignored) {
224
- LoadBalancingPolicy* policy = static_cast<LoadBalancingPolicy*>(arg);
225
- policy->ShutdownLocked();
226
- policy->Unref();
227
- }
347
+ static void ShutdownAndUnrefLocked(void* arg, grpc_error* ignored);
228
348
 
229
349
  /// Combiner under which LB policy actions take place.
230
350
  grpc_combiner* combiner_;
231
- /// Client channel factory, used to create channels and subchannels.
232
- grpc_client_channel_factory* client_channel_factory_;
233
- /// Subchannel pool.
234
- RefCountedPtr<SubchannelPoolInterface> subchannel_pool_;
235
351
  /// Owned pointer to interested parties in load balancing decisions.
236
352
  grpc_pollset_set* interested_parties_;
237
- /// Callback to force a re-resolution.
238
- grpc_closure* request_reresolution_;
353
+ /// Channel control helper.
354
+ UniquePtr<ChannelControlHelper> channel_control_helper_;
355
+ /// Channelz node.
356
+ RefCountedPtr<channelz::ClientChannelNode> channelz_node_;
239
357
  };
240
358
 
241
359
  } // namespace grpc_core