grpc 1.17.1 → 1.18.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 (166) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1228 -988
  3. data/etc/roots.pem +242 -30
  4. data/include/grpc/grpc.h +2 -1
  5. data/include/grpc/grpc_security_constants.h +3 -3
  6. data/include/grpc/impl/codegen/atm_gcc_sync.h +2 -0
  7. data/include/grpc/impl/codegen/atm_windows.h +2 -0
  8. data/include/grpc/impl/codegen/compression_types.h +2 -1
  9. data/include/grpc/impl/codegen/grpc_types.h +1 -1
  10. data/include/grpc/impl/codegen/port_platform.h +9 -0
  11. data/src/core/ext/filters/client_channel/client_channel.cc +163 -882
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -4
  13. data/src/core/ext/filters/client_channel/health/health_check_client.h +2 -3
  14. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -1
  15. data/src/core/ext/filters/client_channel/lb_policy.h +8 -17
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +176 -216
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h +1 -1
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +20 -23
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +1 -1
  20. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +49 -52
  21. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +13 -35
  22. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +31 -30
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +69 -225
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h +1 -1
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +20 -23
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +1 -1
  27. data/src/core/ext/filters/client_channel/lb_policy_factory.h +2 -84
  28. data/src/core/ext/filters/client_channel/request_routing.cc +936 -0
  29. data/src/core/ext/filters/client_channel/request_routing.h +177 -0
  30. data/src/core/ext/filters/client_channel/resolver.cc +1 -1
  31. data/src/core/ext/filters/client_channel/resolver.h +1 -1
  32. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +37 -26
  33. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +30 -18
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +119 -100
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +8 -5
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +5 -4
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_posix.cc +2 -1
  38. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +12 -14
  39. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +5 -9
  40. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +2 -1
  41. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +1 -2
  42. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +17 -17
  43. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +45 -52
  44. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +13 -17
  45. data/src/core/ext/filters/client_channel/server_address.cc +103 -0
  46. data/src/core/ext/filters/client_channel/server_address.h +108 -0
  47. data/src/core/ext/filters/client_channel/subchannel.cc +10 -8
  48. data/src/core/ext/filters/client_channel/subchannel.h +9 -6
  49. data/src/core/ext/filters/client_channel/subchannel_index.cc +20 -27
  50. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -2
  51. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +8 -9
  52. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  53. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +1 -1
  54. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +8 -11
  55. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +24 -54
  56. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +3 -1
  57. data/src/core/ext/transport/chttp2/transport/context_list.cc +67 -0
  58. data/src/core/ext/transport/chttp2/transport/context_list.h +53 -0
  59. data/src/core/ext/transport/chttp2/transport/internal.h +38 -11
  60. data/src/core/ext/transport/chttp2/transport/writing.cc +5 -0
  61. data/src/core/ext/transport/inproc/inproc_transport.cc +1 -1
  62. data/src/core/lib/channel/channelz.cc +19 -18
  63. data/src/core/lib/channel/channelz.h +7 -1
  64. data/src/core/lib/channel/channelz_registry.cc +3 -2
  65. data/src/core/lib/debug/trace.cc +3 -0
  66. data/src/core/lib/debug/trace.h +5 -3
  67. data/src/core/lib/gpr/sync_posix.cc +96 -4
  68. data/src/core/lib/gprpp/inlined_vector.h +25 -19
  69. data/src/core/lib/gprpp/memory.h +2 -11
  70. data/src/core/lib/gprpp/orphanable.h +18 -82
  71. data/src/core/lib/gprpp/ref_counted.h +75 -84
  72. data/src/core/lib/gprpp/ref_counted_ptr.h +22 -17
  73. data/src/core/lib/http/httpcli_security_connector.cc +101 -94
  74. data/src/core/lib/http/parser.h +5 -5
  75. data/src/core/lib/iomgr/buffer_list.cc +16 -5
  76. data/src/core/lib/iomgr/buffer_list.h +10 -3
  77. data/src/core/lib/iomgr/call_combiner.cc +50 -2
  78. data/src/core/lib/iomgr/call_combiner.h +29 -2
  79. data/src/core/lib/iomgr/dynamic_annotations.h +67 -0
  80. data/src/core/lib/iomgr/endpoint.cc +4 -0
  81. data/src/core/lib/iomgr/endpoint.h +3 -0
  82. data/src/core/lib/iomgr/endpoint_pair_posix.cc +2 -2
  83. data/src/core/lib/iomgr/ev_epoll1_linux.cc +4 -0
  84. data/src/core/lib/iomgr/ev_epollex_linux.cc +4 -0
  85. data/src/core/lib/iomgr/ev_poll_posix.cc +4 -0
  86. data/src/core/lib/iomgr/ev_posix.cc +15 -7
  87. data/src/core/lib/iomgr/ev_posix.h +10 -0
  88. data/src/core/lib/iomgr/exec_ctx.cc +13 -0
  89. data/src/core/lib/iomgr/fork_posix.cc +1 -1
  90. data/src/core/lib/iomgr/internal_errqueue.cc +36 -3
  91. data/src/core/lib/iomgr/internal_errqueue.h +7 -1
  92. data/src/core/lib/iomgr/iomgr.cc +7 -0
  93. data/src/core/lib/iomgr/iomgr.h +4 -0
  94. data/src/core/lib/iomgr/iomgr_custom.cc +3 -1
  95. data/src/core/lib/iomgr/iomgr_internal.cc +4 -0
  96. data/src/core/lib/iomgr/iomgr_internal.h +4 -0
  97. data/src/core/lib/iomgr/iomgr_posix.cc +6 -1
  98. data/src/core/lib/iomgr/iomgr_windows.cc +4 -1
  99. data/src/core/lib/iomgr/port.h +1 -2
  100. data/src/core/lib/iomgr/resource_quota.cc +1 -0
  101. data/src/core/lib/iomgr/sockaddr_utils.cc +1 -0
  102. data/src/core/lib/iomgr/tcp_custom.cc +4 -1
  103. data/src/core/lib/iomgr/tcp_posix.cc +95 -35
  104. data/src/core/lib/iomgr/tcp_windows.cc +4 -1
  105. data/src/core/lib/iomgr/timer_manager.cc +6 -0
  106. data/src/core/lib/security/context/security_context.cc +75 -108
  107. data/src/core/lib/security/context/security_context.h +59 -35
  108. data/src/core/lib/security/credentials/alts/alts_credentials.cc +36 -48
  109. data/src/core/lib/security/credentials/alts/alts_credentials.h +37 -10
  110. data/src/core/lib/security/credentials/composite/composite_credentials.cc +97 -157
  111. data/src/core/lib/security/credentials/composite/composite_credentials.h +60 -24
  112. data/src/core/lib/security/credentials/credentials.cc +18 -142
  113. data/src/core/lib/security/credentials/credentials.h +119 -95
  114. data/src/core/lib/security/credentials/fake/fake_credentials.cc +46 -71
  115. data/src/core/lib/security/credentials/fake/fake_credentials.h +23 -5
  116. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +144 -51
  117. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +28 -5
  118. data/src/core/lib/security/credentials/iam/iam_credentials.cc +27 -35
  119. data/src/core/lib/security/credentials/iam/iam_credentials.h +18 -4
  120. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +60 -69
  121. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +29 -10
  122. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +2 -0
  123. data/src/core/lib/security/credentials/local/local_credentials.cc +19 -32
  124. data/src/core/lib/security/credentials/local/local_credentials.h +32 -11
  125. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +130 -149
  126. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +74 -29
  127. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +59 -77
  128. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +40 -17
  129. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +66 -83
  130. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +58 -15
  131. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +152 -177
  132. data/src/core/lib/security/security_connector/alts/alts_security_connector.h +12 -10
  133. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +210 -215
  134. data/src/core/lib/security/security_connector/fake/fake_security_connector.h +9 -6
  135. data/src/core/lib/security/security_connector/local/local_security_connector.cc +176 -169
  136. data/src/core/lib/security/security_connector/local/local_security_connector.h +10 -9
  137. data/src/core/lib/security/security_connector/security_connector.cc +41 -124
  138. data/src/core/lib/security/security_connector/security_connector.h +102 -105
  139. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +348 -370
  140. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +14 -12
  141. data/src/core/lib/security/security_connector/ssl_utils.cc +13 -9
  142. data/src/core/lib/security/security_connector/ssl_utils.h +3 -1
  143. data/src/core/lib/security/transport/client_auth_filter.cc +50 -50
  144. data/src/core/lib/security/transport/secure_endpoint.cc +7 -1
  145. data/src/core/lib/security/transport/security_handshaker.cc +82 -66
  146. data/src/core/lib/security/transport/server_auth_filter.cc +15 -13
  147. data/src/core/lib/surface/init.cc +1 -0
  148. data/src/core/lib/surface/server.cc +13 -11
  149. data/src/core/lib/surface/server.h +6 -6
  150. data/src/core/lib/surface/version.cc +2 -2
  151. data/src/core/lib/transport/metadata.cc +1 -0
  152. data/src/core/lib/transport/static_metadata.cc +228 -221
  153. data/src/core/lib/transport/static_metadata.h +75 -71
  154. data/src/core/lib/transport/transport.cc +2 -1
  155. data/src/core/lib/transport/transport.h +5 -1
  156. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +9 -2
  157. data/src/core/tsi/ssl_transport_security.cc +35 -24
  158. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +1 -1
  159. data/src/ruby/lib/grpc/generic/rpc_server.rb +61 -0
  160. data/src/ruby/lib/grpc/generic/service.rb +1 -1
  161. data/src/ruby/lib/grpc/version.rb +1 -1
  162. data/src/ruby/pb/grpc/health/checker.rb +2 -3
  163. data/src/ruby/spec/generic/rpc_server_spec.rb +22 -0
  164. data/src/ruby/spec/support/services.rb +1 -0
  165. metadata +37 -32
  166. data/src/core/ext/filters/client_channel/lb_policy_factory.cc +0 -163
@@ -51,8 +51,7 @@ HealthCheckClient::HealthCheckClient(
51
51
  RefCountedPtr<ConnectedSubchannel> connected_subchannel,
52
52
  grpc_pollset_set* interested_parties,
53
53
  grpc_core::RefCountedPtr<grpc_core::channelz::SubchannelNode> channelz_node)
54
- : InternallyRefCountedWithTracing<HealthCheckClient>(
55
- &grpc_health_check_client_trace),
54
+ : InternallyRefCounted<HealthCheckClient>(&grpc_health_check_client_trace),
56
55
  service_name_(service_name),
57
56
  connected_subchannel_(std::move(connected_subchannel)),
58
57
  interested_parties_(interested_parties),
@@ -281,8 +280,7 @@ bool DecodeResponse(grpc_slice_buffer* slice_buffer, grpc_error** error) {
281
280
  HealthCheckClient::CallState::CallState(
282
281
  RefCountedPtr<HealthCheckClient> health_check_client,
283
282
  grpc_pollset_set* interested_parties)
284
- : InternallyRefCountedWithTracing<CallState>(
285
- &grpc_health_check_client_trace),
283
+ : InternallyRefCounted<CallState>(&grpc_health_check_client_trace),
286
284
  health_check_client_(std::move(health_check_client)),
287
285
  pollent_(grpc_polling_entity_create_from_pollset_set(interested_parties)),
288
286
  arena_(gpr_arena_create(health_check_client_->connected_subchannel_
@@ -41,8 +41,7 @@
41
41
 
42
42
  namespace grpc_core {
43
43
 
44
- class HealthCheckClient
45
- : public InternallyRefCountedWithTracing<HealthCheckClient> {
44
+ class HealthCheckClient : public InternallyRefCounted<HealthCheckClient> {
46
45
  public:
47
46
  HealthCheckClient(const char* service_name,
48
47
  RefCountedPtr<ConnectedSubchannel> connected_subchannel,
@@ -61,7 +60,7 @@ class HealthCheckClient
61
60
 
62
61
  private:
63
62
  // Contains a call to the backend and all the data related to the call.
64
- class CallState : public InternallyRefCountedWithTracing<CallState> {
63
+ class CallState : public InternallyRefCounted<CallState> {
65
64
  public:
66
65
  CallState(RefCountedPtr<HealthCheckClient> health_check_client,
67
66
  grpc_pollset_set* interested_parties_);
@@ -27,7 +27,7 @@ grpc_core::DebugOnlyTraceFlag grpc_trace_lb_policy_refcount(
27
27
  namespace grpc_core {
28
28
 
29
29
  LoadBalancingPolicy::LoadBalancingPolicy(const Args& args)
30
- : InternallyRefCountedWithTracing(&grpc_trace_lb_policy_refcount),
30
+ : InternallyRefCounted(&grpc_trace_lb_policy_refcount),
31
31
  combiner_(GRPC_COMBINER_REF(args.combiner, "lb_policy")),
32
32
  client_channel_factory_(args.client_channel_factory),
33
33
  interested_parties_(grpc_pollset_set_create()),
@@ -42,8 +42,7 @@ namespace grpc_core {
42
42
  ///
43
43
  /// Any I/O done by the LB policy should be done under the pollset_set
44
44
  /// returned by \a interested_parties().
45
- class LoadBalancingPolicy
46
- : public InternallyRefCountedWithTracing<LoadBalancingPolicy> {
45
+ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
47
46
  public:
48
47
  struct Args {
49
48
  /// The combiner under which all LB policy calls will be run.
@@ -56,7 +55,7 @@ class LoadBalancingPolicy
56
55
  grpc_client_channel_factory* client_channel_factory = nullptr;
57
56
  /// Channel args from the resolver.
58
57
  /// Note that the LB policy gets the set of addresses from the
59
- /// GRPC_ARG_LB_ADDRESSES channel arg.
58
+ /// GRPC_ARG_SERVER_ADDRESS_LIST channel arg.
60
59
  grpc_channel_args* args = nullptr;
61
60
  /// Load balancing config from the resolver.
62
61
  grpc_json* lb_config = nullptr;
@@ -66,10 +65,10 @@ class LoadBalancingPolicy
66
65
  struct PickState {
67
66
  /// Initial metadata associated with the picking call.
68
67
  grpc_metadata_batch* initial_metadata = nullptr;
69
- /// Bitmask used for selective cancelling. See
68
+ /// Pointer to bitmask used for selective cancelling. See
70
69
  /// \a CancelMatchingPicksLocked() and \a GRPC_INITIAL_METADATA_* in
71
70
  /// grpc_types.h.
72
- uint32_t initial_metadata_flags = 0;
71
+ uint32_t* initial_metadata_flags = nullptr;
73
72
  /// Storage for LB token in \a initial_metadata, or nullptr if not used.
74
73
  grpc_linked_mdelem lb_token_mdelem_storage;
75
74
  /// Closure to run when pick is complete, if not completed synchronously.
@@ -81,11 +80,6 @@ class LoadBalancingPolicy
81
80
  /// Will be populated with context to pass to the subchannel call, if
82
81
  /// needed.
83
82
  grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT] = {};
84
- /// Upon success, \a *user_data will be set to whatever opaque information
85
- /// may need to be propagated from the LB policy, or nullptr if not needed.
86
- // TODO(roth): As part of revamping our metadata APIs, try to find a
87
- // way to clean this up and C++-ify it.
88
- void** user_data = nullptr;
89
83
  /// Next pointer. For internal use by LB policy.
90
84
  PickState* next = nullptr;
91
85
  };
@@ -94,9 +88,12 @@ class LoadBalancingPolicy
94
88
  LoadBalancingPolicy(const LoadBalancingPolicy&) = delete;
95
89
  LoadBalancingPolicy& operator=(const LoadBalancingPolicy&) = delete;
96
90
 
91
+ /// Returns the name of the LB policy.
92
+ virtual const char* name() const GRPC_ABSTRACT;
93
+
97
94
  /// Updates the policy with a new set of \a args and a new \a lb_config from
98
95
  /// the resolver. Note that the LB policy gets the set of addresses from the
99
- /// GRPC_ARG_LB_ADDRESSES channel arg.
96
+ /// GRPC_ARG_SERVER_ADDRESS_LIST channel arg.
100
97
  virtual void UpdateLocked(const grpc_channel_args& args,
101
98
  grpc_json* lb_config) GRPC_ABSTRACT;
102
99
 
@@ -211,12 +208,6 @@ class LoadBalancingPolicy
211
208
  grpc_pollset_set* interested_parties_;
212
209
  /// Callback to force a re-resolution.
213
210
  grpc_closure* request_reresolution_;
214
-
215
- // Dummy classes needed for alignment issues.
216
- // See https://github.com/grpc/grpc/issues/16032 for context.
217
- // TODO(ncteisen): remove this as soon as the issue is resolved.
218
- channelz::ChildRefsList dummy_list_foo;
219
- channelz::ChildRefsList dummy_list_bar;
220
211
  };
221
212
 
222
213
  } // namespace grpc_core
@@ -84,6 +84,7 @@
84
84
  #include "src/core/ext/filters/client_channel/lb_policy_registry.h"
85
85
  #include "src/core/ext/filters/client_channel/parse_address.h"
86
86
  #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
87
+ #include "src/core/ext/filters/client_channel/server_address.h"
87
88
  #include "src/core/ext/filters/client_channel/subchannel_index.h"
88
89
  #include "src/core/lib/backoff/backoff.h"
89
90
  #include "src/core/lib/channel/channel_args.h"
@@ -113,15 +114,21 @@
113
114
  #define GRPC_GRPCLB_RECONNECT_JITTER 0.2
114
115
  #define GRPC_GRPCLB_DEFAULT_FALLBACK_TIMEOUT_MS 10000
115
116
 
117
+ #define GRPC_ARG_GRPCLB_ADDRESS_LB_TOKEN "grpc.grpclb_address_lb_token"
118
+
116
119
  namespace grpc_core {
117
120
 
118
121
  TraceFlag grpc_lb_glb_trace(false, "glb");
119
122
 
120
123
  namespace {
121
124
 
125
+ constexpr char kGrpclb[] = "grpclb";
126
+
122
127
  class GrpcLb : public LoadBalancingPolicy {
123
128
  public:
124
- GrpcLb(const grpc_lb_addresses* addresses, const Args& args);
129
+ explicit GrpcLb(const Args& args);
130
+
131
+ const char* name() const override { return kGrpclb; }
125
132
 
126
133
  void UpdateLocked(const grpc_channel_args& args,
127
134
  grpc_json* lb_config) override;
@@ -161,9 +168,6 @@ class GrpcLb : public LoadBalancingPolicy {
161
168
  // Our on_complete closure and the original one.
162
169
  grpc_closure on_complete;
163
170
  grpc_closure* original_on_complete;
164
- // The LB token associated with the pick. This is set via user_data in
165
- // the pick.
166
- grpc_mdelem lb_token;
167
171
  // Stats for client-side load reporting.
168
172
  RefCountedPtr<GrpcLbClientStats> client_stats;
169
173
  // Next pending pick.
@@ -171,8 +175,7 @@ class GrpcLb : public LoadBalancingPolicy {
171
175
  };
172
176
 
173
177
  /// Contains a call to the LB server and all the data related to the call.
174
- class BalancerCallState
175
- : public InternallyRefCountedWithTracing<BalancerCallState> {
178
+ class BalancerCallState : public InternallyRefCounted<BalancerCallState> {
176
179
  public:
177
180
  explicit BalancerCallState(
178
181
  RefCountedPtr<LoadBalancingPolicy> parent_grpclb_policy);
@@ -330,7 +333,7 @@ class GrpcLb : public LoadBalancingPolicy {
330
333
  // 0 means not using fallback.
331
334
  int lb_fallback_timeout_ms_ = 0;
332
335
  // The backend addresses from the resolver.
333
- grpc_lb_addresses* fallback_backend_addresses_ = nullptr;
336
+ UniquePtr<ServerAddressList> fallback_backend_addresses_;
334
337
  // Fallback timer.
335
338
  bool fallback_timer_callback_pending_ = false;
336
339
  grpc_timer lb_fallback_timer_;
@@ -350,7 +353,7 @@ class GrpcLb : public LoadBalancingPolicy {
350
353
  // serverlist parsing code
351
354
  //
352
355
 
353
- // vtable for LB tokens in grpc_lb_addresses
356
+ // vtable for LB token channel arg.
354
357
  void* lb_token_copy(void* token) {
355
358
  return token == nullptr
356
359
  ? nullptr
@@ -362,38 +365,13 @@ void lb_token_destroy(void* token) {
362
365
  }
363
366
  }
364
367
  int lb_token_cmp(void* token1, void* token2) {
365
- if (token1 > token2) return 1;
366
- if (token1 < token2) return -1;
368
+ // Always indicate a match, since we don't want this channel arg to
369
+ // affect the subchannel's key in the index.
367
370
  return 0;
368
371
  }
369
- const grpc_lb_user_data_vtable lb_token_vtable = {
372
+ const grpc_arg_pointer_vtable lb_token_arg_vtable = {
370
373
  lb_token_copy, lb_token_destroy, lb_token_cmp};
371
374
 
372
- // Returns the backend addresses extracted from the given addresses.
373
- grpc_lb_addresses* ExtractBackendAddresses(const grpc_lb_addresses* addresses) {
374
- // First pass: count the number of backend addresses.
375
- size_t num_backends = 0;
376
- for (size_t i = 0; i < addresses->num_addresses; ++i) {
377
- if (!addresses->addresses[i].is_balancer) {
378
- ++num_backends;
379
- }
380
- }
381
- // Second pass: actually populate the addresses and (empty) LB tokens.
382
- grpc_lb_addresses* backend_addresses =
383
- grpc_lb_addresses_create(num_backends, &lb_token_vtable);
384
- size_t num_copied = 0;
385
- for (size_t i = 0; i < addresses->num_addresses; ++i) {
386
- if (addresses->addresses[i].is_balancer) continue;
387
- const grpc_resolved_address* addr = &addresses->addresses[i].address;
388
- grpc_lb_addresses_set_address(backend_addresses, num_copied, &addr->addr,
389
- addr->len, false /* is_balancer */,
390
- nullptr /* balancer_name */,
391
- (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload);
392
- ++num_copied;
393
- }
394
- return backend_addresses;
395
- }
396
-
397
375
  bool IsServerValid(const grpc_grpclb_server* server, size_t idx, bool log) {
398
376
  if (server->drop) return false;
399
377
  const grpc_grpclb_ip_address* ip = &server->ip_address;
@@ -441,30 +419,16 @@ void ParseServer(const grpc_grpclb_server* server,
441
419
  }
442
420
 
443
421
  // Returns addresses extracted from \a serverlist.
444
- grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
445
- size_t num_valid = 0;
446
- /* first pass: count how many are valid in order to allocate the necessary
447
- * memory in a single block */
422
+ ServerAddressList ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
423
+ ServerAddressList addresses;
448
424
  for (size_t i = 0; i < serverlist->num_servers; ++i) {
449
- if (IsServerValid(serverlist->servers[i], i, true)) ++num_valid;
450
- }
451
- grpc_lb_addresses* lb_addresses =
452
- grpc_lb_addresses_create(num_valid, &lb_token_vtable);
453
- /* second pass: actually populate the addresses and LB tokens (aka user data
454
- * to the outside world) to be read by the RR policy during its creation.
455
- * Given that the validity tests are very cheap, they are performed again
456
- * instead of marking the valid ones during the first pass, as this would
457
- * incurr in an allocation due to the arbitrary number of server */
458
- size_t addr_idx = 0;
459
- for (size_t sl_idx = 0; sl_idx < serverlist->num_servers; ++sl_idx) {
460
- const grpc_grpclb_server* server = serverlist->servers[sl_idx];
461
- if (!IsServerValid(serverlist->servers[sl_idx], sl_idx, false)) continue;
462
- GPR_ASSERT(addr_idx < num_valid);
463
- /* address processing */
425
+ const grpc_grpclb_server* server = serverlist->servers[i];
426
+ if (!IsServerValid(serverlist->servers[i], i, false)) continue;
427
+ // Address processing.
464
428
  grpc_resolved_address addr;
465
429
  ParseServer(server, &addr);
466
- /* lb token processing */
467
- void* user_data;
430
+ // LB token processing.
431
+ grpc_mdelem lb_token;
468
432
  if (server->has_load_balance_token) {
469
433
  const size_t lb_token_max_length =
470
434
  GPR_ARRAY_SIZE(server->load_balance_token);
@@ -472,9 +436,7 @@ grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
472
436
  strnlen(server->load_balance_token, lb_token_max_length);
473
437
  grpc_slice lb_token_mdstr = grpc_slice_from_copied_buffer(
474
438
  server->load_balance_token, lb_token_length);
475
- user_data =
476
- (void*)grpc_mdelem_from_slices(GRPC_MDSTR_LB_TOKEN, lb_token_mdstr)
477
- .payload;
439
+ lb_token = grpc_mdelem_from_slices(GRPC_MDSTR_LB_TOKEN, lb_token_mdstr);
478
440
  } else {
479
441
  char* uri = grpc_sockaddr_to_uri(&addr);
480
442
  gpr_log(GPR_INFO,
@@ -482,15 +444,18 @@ grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
482
444
  "be used instead",
483
445
  uri);
484
446
  gpr_free(uri);
485
- user_data = (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload;
447
+ lb_token = GRPC_MDELEM_LB_TOKEN_EMPTY;
486
448
  }
487
- grpc_lb_addresses_set_address(lb_addresses, addr_idx, &addr.addr, addr.len,
488
- false /* is_balancer */,
489
- nullptr /* balancer_name */, user_data);
490
- ++addr_idx;
491
- }
492
- GPR_ASSERT(addr_idx == num_valid);
493
- return lb_addresses;
449
+ // Add address.
450
+ grpc_arg arg = grpc_channel_arg_pointer_create(
451
+ const_cast<char*>(GRPC_ARG_GRPCLB_ADDRESS_LB_TOKEN),
452
+ (void*)lb_token.payload, &lb_token_arg_vtable);
453
+ grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
454
+ addresses.emplace_back(addr, args);
455
+ // Clean up.
456
+ GRPC_MDELEM_UNREF(lb_token);
457
+ }
458
+ return addresses;
494
459
  }
495
460
 
496
461
  //
@@ -499,7 +464,7 @@ grpc_lb_addresses* ProcessServerlist(const grpc_grpclb_serverlist* serverlist) {
499
464
 
500
465
  GrpcLb::BalancerCallState::BalancerCallState(
501
466
  RefCountedPtr<LoadBalancingPolicy> parent_grpclb_policy)
502
- : InternallyRefCountedWithTracing<BalancerCallState>(&grpc_lb_glb_trace),
467
+ : InternallyRefCounted<BalancerCallState>(&grpc_lb_glb_trace),
503
468
  grpclb_policy_(std::move(parent_grpclb_policy)) {
504
469
  GPR_ASSERT(grpclb_policy_ != nullptr);
505
470
  GPR_ASSERT(!grpclb_policy()->shutting_down_);
@@ -566,8 +531,7 @@ void GrpcLb::BalancerCallState::Orphan() {
566
531
  void GrpcLb::BalancerCallState::StartQuery() {
567
532
  GPR_ASSERT(lb_call_ != nullptr);
568
533
  if (grpc_lb_glb_trace.enabled()) {
569
- gpr_log(GPR_INFO,
570
- "[grpclb %p] Starting LB call (lb_calld: %p, lb_call: %p)",
534
+ gpr_log(GPR_INFO, "[grpclb %p] lb_calld=%p: Starting LB call %p",
571
535
  grpclb_policy_.get(), this, lb_call_);
572
536
  }
573
537
  // Create the ops.
@@ -711,8 +675,9 @@ void GrpcLb::BalancerCallState::SendClientLoadReportLocked() {
711
675
  grpc_call_error call_error = grpc_call_start_batch_and_execute(
712
676
  lb_call_, &op, 1, &client_load_report_closure_);
713
677
  if (GPR_UNLIKELY(call_error != GRPC_CALL_OK)) {
714
- gpr_log(GPR_ERROR, "[grpclb %p] call_error=%d", grpclb_policy_.get(),
715
- call_error);
678
+ gpr_log(GPR_ERROR,
679
+ "[grpclb %p] lb_calld=%p call_error=%d sending client load report",
680
+ grpclb_policy_.get(), this, call_error);
716
681
  GPR_ASSERT(GRPC_CALL_OK == call_error);
717
682
  }
718
683
  }
@@ -749,7 +714,7 @@ void GrpcLb::BalancerCallState::OnBalancerMessageReceivedLocked(
749
714
  void* arg, grpc_error* error) {
750
715
  BalancerCallState* lb_calld = static_cast<BalancerCallState*>(arg);
751
716
  GrpcLb* grpclb_policy = lb_calld->grpclb_policy();
752
- // Empty payload means the LB call was cancelled.
717
+ // Null payload means the LB call was cancelled.
753
718
  if (lb_calld != grpclb_policy->lb_calld_.get() ||
754
719
  lb_calld->recv_message_payload_ == nullptr) {
755
720
  lb_calld->Unref(DEBUG_LOCATION, "on_message_received");
@@ -773,15 +738,17 @@ void GrpcLb::BalancerCallState::OnBalancerMessageReceivedLocked(
773
738
  &initial_response->client_stats_report_interval));
774
739
  if (grpc_lb_glb_trace.enabled()) {
775
740
  gpr_log(GPR_INFO,
776
- "[grpclb %p] Received initial LB response message; "
777
- "client load reporting interval = %" PRId64 " milliseconds",
778
- grpclb_policy, lb_calld->client_stats_report_interval_);
741
+ "[grpclb %p] lb_calld=%p: Received initial LB response "
742
+ "message; client load reporting interval = %" PRId64
743
+ " milliseconds",
744
+ grpclb_policy, lb_calld,
745
+ lb_calld->client_stats_report_interval_);
779
746
  }
780
747
  } else if (grpc_lb_glb_trace.enabled()) {
781
748
  gpr_log(GPR_INFO,
782
- "[grpclb %p] Received initial LB response message; client load "
783
- "reporting NOT enabled",
784
- grpclb_policy);
749
+ "[grpclb %p] lb_calld=%p: Received initial LB response message; "
750
+ "client load reporting NOT enabled",
751
+ grpclb_policy, lb_calld);
785
752
  }
786
753
  grpc_grpclb_initial_response_destroy(initial_response);
787
754
  lb_calld->seen_initial_response_ = true;
@@ -791,74 +758,67 @@ void GrpcLb::BalancerCallState::OnBalancerMessageReceivedLocked(
791
758
  GPR_ASSERT(lb_calld->lb_call_ != nullptr);
792
759
  if (grpc_lb_glb_trace.enabled()) {
793
760
  gpr_log(GPR_INFO,
794
- "[grpclb %p] Serverlist with %" PRIuPTR " servers received",
795
- grpclb_policy, serverlist->num_servers);
761
+ "[grpclb %p] lb_calld=%p: Serverlist with %" PRIuPTR
762
+ " servers received",
763
+ grpclb_policy, lb_calld, serverlist->num_servers);
796
764
  for (size_t i = 0; i < serverlist->num_servers; ++i) {
797
765
  grpc_resolved_address addr;
798
766
  ParseServer(serverlist->servers[i], &addr);
799
767
  char* ipport;
800
768
  grpc_sockaddr_to_string(&ipport, &addr, false);
801
- gpr_log(GPR_INFO, "[grpclb %p] Serverlist[%" PRIuPTR "]: %s",
802
- grpclb_policy, i, ipport);
769
+ gpr_log(GPR_INFO,
770
+ "[grpclb %p] lb_calld=%p: Serverlist[%" PRIuPTR "]: %s",
771
+ grpclb_policy, lb_calld, i, ipport);
803
772
  gpr_free(ipport);
804
773
  }
805
774
  }
806
- /* update serverlist */
807
- if (serverlist->num_servers > 0) {
808
- // Start sending client load report only after we start using the
809
- // serverlist returned from the current LB call.
810
- if (lb_calld->client_stats_report_interval_ > 0 &&
811
- lb_calld->client_stats_ == nullptr) {
812
- lb_calld->client_stats_.reset(New<GrpcLbClientStats>());
813
- // TODO(roth): We currently track this ref manually. Once the
814
- // ClosureRef API is ready, we should pass the RefCountedPtr<> along
815
- // with the callback.
816
- auto self = lb_calld->Ref(DEBUG_LOCATION, "client_load_report");
817
- self.release();
818
- lb_calld->ScheduleNextClientLoadReportLocked();
819
- }
820
- if (grpc_grpclb_serverlist_equals(grpclb_policy->serverlist_,
821
- serverlist)) {
822
- if (grpc_lb_glb_trace.enabled()) {
823
- gpr_log(GPR_INFO,
824
- "[grpclb %p] Incoming server list identical to current, "
825
- "ignoring.",
826
- grpclb_policy);
827
- }
828
- grpc_grpclb_destroy_serverlist(serverlist);
829
- } else { /* new serverlist */
830
- if (grpclb_policy->serverlist_ != nullptr) {
831
- /* dispose of the old serverlist */
832
- grpc_grpclb_destroy_serverlist(grpclb_policy->serverlist_);
833
- } else {
834
- /* or dispose of the fallback */
835
- grpc_lb_addresses_destroy(grpclb_policy->fallback_backend_addresses_);
836
- grpclb_policy->fallback_backend_addresses_ = nullptr;
837
- if (grpclb_policy->fallback_timer_callback_pending_) {
838
- grpc_timer_cancel(&grpclb_policy->lb_fallback_timer_);
839
- }
840
- }
841
- // and update the copy in the GrpcLb instance. This
842
- // serverlist instance will be destroyed either upon the next
843
- // update or when the GrpcLb instance is destroyed.
844
- grpclb_policy->serverlist_ = serverlist;
845
- grpclb_policy->serverlist_index_ = 0;
846
- grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked();
847
- }
848
- } else {
775
+ // Start sending client load report only after we start using the
776
+ // serverlist returned from the current LB call.
777
+ if (lb_calld->client_stats_report_interval_ > 0 &&
778
+ lb_calld->client_stats_ == nullptr) {
779
+ lb_calld->client_stats_.reset(New<GrpcLbClientStats>());
780
+ // TODO(roth): We currently track this ref manually. Once the
781
+ // ClosureRef API is ready, we should pass the RefCountedPtr<> along
782
+ // with the callback.
783
+ auto self = lb_calld->Ref(DEBUG_LOCATION, "client_load_report");
784
+ self.release();
785
+ lb_calld->ScheduleNextClientLoadReportLocked();
786
+ }
787
+ // Check if the serverlist differs from the previous one.
788
+ if (grpc_grpclb_serverlist_equals(grpclb_policy->serverlist_, serverlist)) {
849
789
  if (grpc_lb_glb_trace.enabled()) {
850
- gpr_log(GPR_INFO, "[grpclb %p] Received empty server list, ignoring.",
851
- grpclb_policy);
790
+ gpr_log(GPR_INFO,
791
+ "[grpclb %p] lb_calld=%p: Incoming server list identical to "
792
+ "current, ignoring.",
793
+ grpclb_policy, lb_calld);
852
794
  }
853
795
  grpc_grpclb_destroy_serverlist(serverlist);
796
+ } else { // New serverlist.
797
+ if (grpclb_policy->serverlist_ != nullptr) {
798
+ // Dispose of the old serverlist.
799
+ grpc_grpclb_destroy_serverlist(grpclb_policy->serverlist_);
800
+ } else {
801
+ // Dispose of the fallback.
802
+ grpclb_policy->fallback_backend_addresses_.reset();
803
+ if (grpclb_policy->fallback_timer_callback_pending_) {
804
+ grpc_timer_cancel(&grpclb_policy->lb_fallback_timer_);
805
+ }
806
+ }
807
+ // Update the serverlist in the GrpcLb instance. This serverlist
808
+ // instance will be destroyed either upon the next update or when the
809
+ // GrpcLb instance is destroyed.
810
+ grpclb_policy->serverlist_ = serverlist;
811
+ grpclb_policy->serverlist_index_ = 0;
812
+ grpclb_policy->CreateOrUpdateRoundRobinPolicyLocked();
854
813
  }
855
814
  } else {
856
815
  // No valid initial response or serverlist found.
857
816
  char* response_slice_str =
858
817
  grpc_dump_slice(response_slice, GPR_DUMP_ASCII | GPR_DUMP_HEX);
859
818
  gpr_log(GPR_ERROR,
860
- "[grpclb %p] Invalid LB response received: '%s'. Ignoring.",
861
- grpclb_policy, response_slice_str);
819
+ "[grpclb %p] lb_calld=%p: Invalid LB response received: '%s'. "
820
+ "Ignoring.",
821
+ grpclb_policy, lb_calld, response_slice_str);
862
822
  gpr_free(response_slice_str);
863
823
  }
864
824
  grpc_slice_unref_internal(response_slice);
@@ -889,9 +849,9 @@ void GrpcLb::BalancerCallState::OnBalancerStatusReceivedLocked(
889
849
  char* status_details =
890
850
  grpc_slice_to_c_string(lb_calld->lb_call_status_details_);
891
851
  gpr_log(GPR_INFO,
892
- "[grpclb %p] Status from LB server received. Status = %d, details "
893
- "= '%s', (lb_calld: %p, lb_call: %p), error '%s'",
894
- grpclb_policy, lb_calld->lb_call_status_, status_details, lb_calld,
852
+ "[grpclb %p] lb_calld=%p: Status from LB server received. "
853
+ "Status = %d, details = '%s', (lb_call: %p), error '%s'",
854
+ grpclb_policy, lb_calld, lb_calld->lb_call_status_, status_details,
895
855
  lb_calld->lb_call_, grpc_error_string(error));
896
856
  gpr_free(status_details);
897
857
  }
@@ -920,31 +880,25 @@ void GrpcLb::BalancerCallState::OnBalancerStatusReceivedLocked(
920
880
  // helper code for creating balancer channel
921
881
  //
922
882
 
923
- grpc_lb_addresses* ExtractBalancerAddresses(
924
- const grpc_lb_addresses* addresses) {
925
- size_t num_grpclb_addrs = 0;
926
- for (size_t i = 0; i < addresses->num_addresses; ++i) {
927
- if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs;
928
- }
929
- // There must be at least one balancer address, or else the
930
- // client_channel would not have chosen this LB policy.
931
- GPR_ASSERT(num_grpclb_addrs > 0);
932
- grpc_lb_addresses* lb_addresses =
933
- grpc_lb_addresses_create(num_grpclb_addrs, nullptr);
934
- size_t lb_addresses_idx = 0;
935
- for (size_t i = 0; i < addresses->num_addresses; ++i) {
936
- if (!addresses->addresses[i].is_balancer) continue;
937
- if (GPR_UNLIKELY(addresses->addresses[i].user_data != nullptr)) {
938
- gpr_log(GPR_ERROR,
939
- "This LB policy doesn't support user data. It will be ignored");
883
+ ServerAddressList ExtractBalancerAddresses(const ServerAddressList& addresses) {
884
+ ServerAddressList balancer_addresses;
885
+ for (size_t i = 0; i < addresses.size(); ++i) {
886
+ if (addresses[i].IsBalancer()) {
887
+ // Strip out the is_balancer channel arg, since we don't want to
888
+ // recursively use the grpclb policy in the channel used to talk to
889
+ // the balancers. Note that we do NOT strip out the balancer_name
890
+ // channel arg, since we need that to set the authority correctly
891
+ // to talk to the balancers.
892
+ static const char* args_to_remove[] = {
893
+ GRPC_ARG_ADDRESS_IS_BALANCER,
894
+ };
895
+ balancer_addresses.emplace_back(
896
+ addresses[i].address(),
897
+ grpc_channel_args_copy_and_remove(addresses[i].args(), args_to_remove,
898
+ GPR_ARRAY_SIZE(args_to_remove)));
940
899
  }
941
- grpc_lb_addresses_set_address(
942
- lb_addresses, lb_addresses_idx++, addresses->addresses[i].address.addr,
943
- addresses->addresses[i].address.len, false /* is balancer */,
944
- addresses->addresses[i].balancer_name, nullptr /* user data */);
945
900
  }
946
- GPR_ASSERT(num_grpclb_addrs == lb_addresses_idx);
947
- return lb_addresses;
901
+ return balancer_addresses;
948
902
  }
949
903
 
950
904
  /* Returns the channel args for the LB channel, used to create a bidirectional
@@ -956,10 +910,10 @@ grpc_lb_addresses* ExtractBalancerAddresses(
956
910
  * above the grpclb policy.
957
911
  * - \a args: other args inherited from the grpclb policy. */
958
912
  grpc_channel_args* BuildBalancerChannelArgs(
959
- const grpc_lb_addresses* addresses,
913
+ const ServerAddressList& addresses,
960
914
  FakeResolverResponseGenerator* response_generator,
961
915
  const grpc_channel_args* args) {
962
- grpc_lb_addresses* lb_addresses = ExtractBalancerAddresses(addresses);
916
+ ServerAddressList balancer_addresses = ExtractBalancerAddresses(addresses);
963
917
  // Channel args to remove.
964
918
  static const char* args_to_remove[] = {
965
919
  // LB policy name, since we want to use the default (pick_first) in
@@ -977,7 +931,7 @@ grpc_channel_args* BuildBalancerChannelArgs(
977
931
  // is_balancer=true. We need the LB channel to return addresses with
978
932
  // is_balancer=false so that it does not wind up recursively using the
979
933
  // grpclb LB policy, as per the special case logic in client_channel.c.
980
- GRPC_ARG_LB_ADDRESSES,
934
+ GRPC_ARG_SERVER_ADDRESS_LIST,
981
935
  // The fake resolver response generator, because we are replacing it
982
936
  // with the one from the grpclb policy, used to propagate updates to
983
937
  // the LB channel.
@@ -993,10 +947,10 @@ grpc_channel_args* BuildBalancerChannelArgs(
993
947
  };
994
948
  // Channel args to add.
995
949
  const grpc_arg args_to_add[] = {
996
- // New LB addresses.
950
+ // New address list.
997
951
  // Note that we pass these in both when creating the LB channel
998
952
  // and via the fake resolver. The latter is what actually gets used.
999
- grpc_lb_addresses_create_channel_arg(lb_addresses),
953
+ CreateServerAddressListChannelArg(&balancer_addresses),
1000
954
  // The fake resolver response generator, which we use to inject
1001
955
  // address updates into the LB channel.
1002
956
  grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
@@ -1014,18 +968,14 @@ grpc_channel_args* BuildBalancerChannelArgs(
1014
968
  args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), args_to_add,
1015
969
  GPR_ARRAY_SIZE(args_to_add));
1016
970
  // Make any necessary modifications for security.
1017
- new_args = grpc_lb_policy_grpclb_modify_lb_channel_args(new_args);
1018
- // Clean up.
1019
- grpc_lb_addresses_destroy(lb_addresses);
1020
- return new_args;
971
+ return grpc_lb_policy_grpclb_modify_lb_channel_args(new_args);
1021
972
  }
1022
973
 
1023
974
  //
1024
975
  // ctor and dtor
1025
976
  //
1026
977
 
1027
- GrpcLb::GrpcLb(const grpc_lb_addresses* addresses,
1028
- const LoadBalancingPolicy::Args& args)
978
+ GrpcLb::GrpcLb(const LoadBalancingPolicy::Args& args)
1029
979
  : LoadBalancingPolicy(args),
1030
980
  response_generator_(MakeRefCounted<FakeResolverResponseGenerator>()),
1031
981
  lb_call_backoff_(
@@ -1082,9 +1032,6 @@ GrpcLb::~GrpcLb() {
1082
1032
  if (serverlist_ != nullptr) {
1083
1033
  grpc_grpclb_destroy_serverlist(serverlist_);
1084
1034
  }
1085
- if (fallback_backend_addresses_ != nullptr) {
1086
- grpc_lb_addresses_destroy(fallback_backend_addresses_);
1087
- }
1088
1035
  grpc_subchannel_index_unref();
1089
1036
  }
1090
1037
 
@@ -1132,7 +1079,6 @@ void GrpcLb::HandOffPendingPicksLocked(LoadBalancingPolicy* new_policy) {
1132
1079
  while ((pp = pending_picks_) != nullptr) {
1133
1080
  pending_picks_ = pp->next;
1134
1081
  pp->pick->on_complete = pp->original_on_complete;
1135
- pp->pick->user_data = nullptr;
1136
1082
  grpc_error* error = GRPC_ERROR_NONE;
1137
1083
  if (new_policy->PickLocked(pp->pick, &error)) {
1138
1084
  // Synchronous return; schedule closure.
@@ -1194,7 +1140,7 @@ void GrpcLb::CancelMatchingPicksLocked(uint32_t initial_metadata_flags_mask,
1194
1140
  pending_picks_ = nullptr;
1195
1141
  while (pp != nullptr) {
1196
1142
  PendingPick* next = pp->next;
1197
- if ((pp->pick->initial_metadata_flags & initial_metadata_flags_mask) ==
1143
+ if ((*pp->pick->initial_metadata_flags & initial_metadata_flags_mask) ==
1198
1144
  initial_metadata_flags_eq) {
1199
1145
  // Note: pp is deleted in this callback.
1200
1146
  GRPC_CLOSURE_SCHED(&pp->on_complete,
@@ -1286,9 +1232,27 @@ void GrpcLb::NotifyOnStateChangeLocked(grpc_connectivity_state* current,
1286
1232
  notify);
1287
1233
  }
1288
1234
 
1235
+ // Returns the backend addresses extracted from the given addresses.
1236
+ UniquePtr<ServerAddressList> ExtractBackendAddresses(
1237
+ const ServerAddressList& addresses) {
1238
+ void* lb_token = (void*)GRPC_MDELEM_LB_TOKEN_EMPTY.payload;
1239
+ grpc_arg arg = grpc_channel_arg_pointer_create(
1240
+ const_cast<char*>(GRPC_ARG_GRPCLB_ADDRESS_LB_TOKEN), lb_token,
1241
+ &lb_token_arg_vtable);
1242
+ auto backend_addresses = MakeUnique<ServerAddressList>();
1243
+ for (size_t i = 0; i < addresses.size(); ++i) {
1244
+ if (!addresses[i].IsBalancer()) {
1245
+ backend_addresses->emplace_back(
1246
+ addresses[i].address(),
1247
+ grpc_channel_args_copy_and_add(addresses[i].args(), &arg, 1));
1248
+ }
1249
+ }
1250
+ return backend_addresses;
1251
+ }
1252
+
1289
1253
  void GrpcLb::ProcessChannelArgsLocked(const grpc_channel_args& args) {
1290
- const grpc_arg* arg = grpc_channel_args_find(&args, GRPC_ARG_LB_ADDRESSES);
1291
- if (GPR_UNLIKELY(arg == nullptr || arg->type != GRPC_ARG_POINTER)) {
1254
+ const ServerAddressList* addresses = FindServerAddressListChannelArg(&args);
1255
+ if (addresses == nullptr) {
1292
1256
  // Ignore this update.
1293
1257
  gpr_log(
1294
1258
  GPR_ERROR,
@@ -1296,13 +1260,8 @@ void GrpcLb::ProcessChannelArgsLocked(const grpc_channel_args& args) {
1296
1260
  this);
1297
1261
  return;
1298
1262
  }
1299
- const grpc_lb_addresses* addresses =
1300
- static_cast<const grpc_lb_addresses*>(arg->value.pointer.p);
1301
1263
  // Update fallback address list.
1302
- if (fallback_backend_addresses_ != nullptr) {
1303
- grpc_lb_addresses_destroy(fallback_backend_addresses_);
1304
- }
1305
- fallback_backend_addresses_ = ExtractBackendAddresses(addresses);
1264
+ fallback_backend_addresses_ = ExtractBackendAddresses(*addresses);
1306
1265
  // Make sure that GRPC_ARG_LB_POLICY_NAME is set in channel args,
1307
1266
  // since we use this to trigger the client_load_reporting filter.
1308
1267
  static const char* args_to_remove[] = {GRPC_ARG_LB_POLICY_NAME};
@@ -1313,7 +1272,7 @@ void GrpcLb::ProcessChannelArgsLocked(const grpc_channel_args& args) {
1313
1272
  &args, args_to_remove, GPR_ARRAY_SIZE(args_to_remove), &new_arg, 1);
1314
1273
  // Construct args for balancer channel.
1315
1274
  grpc_channel_args* lb_channel_args =
1316
- BuildBalancerChannelArgs(addresses, response_generator_.get(), &args);
1275
+ BuildBalancerChannelArgs(*addresses, response_generator_.get(), &args);
1317
1276
  // Create balancer channel if needed.
1318
1277
  if (lb_channel_ == nullptr) {
1319
1278
  char* uri_str;
@@ -1519,12 +1478,17 @@ void DestroyClientStats(void* arg) {
1519
1478
  }
1520
1479
 
1521
1480
  void GrpcLb::PendingPickSetMetadataAndContext(PendingPick* pp) {
1522
- /* if connected_subchannel is nullptr, no pick has been made by the RR
1523
- * policy (e.g., all addresses failed to connect). There won't be any
1524
- * user_data/token available */
1481
+ // If connected_subchannel is nullptr, no pick has been made by the RR
1482
+ // policy (e.g., all addresses failed to connect). There won't be any
1483
+ // LB token available.
1525
1484
  if (pp->pick->connected_subchannel != nullptr) {
1526
- if (GPR_LIKELY(!GRPC_MDISNULL(pp->lb_token))) {
1527
- AddLbTokenToInitialMetadata(GRPC_MDELEM_REF(pp->lb_token),
1485
+ const grpc_arg* arg =
1486
+ grpc_channel_args_find(pp->pick->connected_subchannel->args(),
1487
+ GRPC_ARG_GRPCLB_ADDRESS_LB_TOKEN);
1488
+ if (arg != nullptr) {
1489
+ grpc_mdelem lb_token = {
1490
+ reinterpret_cast<uintptr_t>(arg->value.pointer.p)};
1491
+ AddLbTokenToInitialMetadata(GRPC_MDELEM_REF(lb_token),
1528
1492
  &pp->pick->lb_token_mdelem_storage,
1529
1493
  pp->pick->initial_metadata);
1530
1494
  } else {
@@ -1583,7 +1547,7 @@ void GrpcLb::AddPendingPick(PendingPick* pp) {
1583
1547
  bool GrpcLb::PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp,
1584
1548
  grpc_error** error) {
1585
1549
  // Check for drops if we are not using fallback backend addresses.
1586
- if (serverlist_ != nullptr) {
1550
+ if (serverlist_ != nullptr && serverlist_->num_servers > 0) {
1587
1551
  // Look at the index into the serverlist to see if we should drop this call.
1588
1552
  grpc_grpclb_server* server = serverlist_->servers[serverlist_index_++];
1589
1553
  if (serverlist_index_ == serverlist_->num_servers) {
@@ -1608,12 +1572,10 @@ bool GrpcLb::PickFromRoundRobinPolicyLocked(bool force_async, PendingPick* pp,
1608
1572
  return true;
1609
1573
  }
1610
1574
  }
1611
- // Set client_stats and user_data.
1575
+ // Set client_stats.
1612
1576
  if (lb_calld_ != nullptr && lb_calld_->client_stats() != nullptr) {
1613
1577
  pp->client_stats = lb_calld_->client_stats()->Ref();
1614
1578
  }
1615
- GPR_ASSERT(pp->pick->user_data == nullptr);
1616
- pp->pick->user_data = (void**)&pp->lb_token;
1617
1579
  // Pick via the RR policy.
1618
1580
  bool pick_done = rr_policy_->PickLocked(pp->pick, error);
1619
1581
  if (pick_done) {
@@ -1641,6 +1603,10 @@ void GrpcLb::CreateRoundRobinPolicyLocked(const Args& args) {
1641
1603
  this);
1642
1604
  return;
1643
1605
  }
1606
+ if (grpc_lb_glb_trace.enabled()) {
1607
+ gpr_log(GPR_INFO, "[grpclb %p] Created new RR policy %p", this,
1608
+ rr_policy_.get());
1609
+ }
1644
1610
  // TODO(roth): We currently track this ref manually. Once the new
1645
1611
  // ClosureRef API is done, pass the RefCountedPtr<> along with the closure.
1646
1612
  auto self = Ref(DEBUG_LOCATION, "on_rr_reresolution_requested");
@@ -1678,11 +1644,11 @@ void GrpcLb::CreateRoundRobinPolicyLocked(const Args& args) {
1678
1644
  }
1679
1645
 
1680
1646
  grpc_channel_args* GrpcLb::CreateRoundRobinPolicyArgsLocked() {
1681
- grpc_lb_addresses* addresses;
1647
+ ServerAddressList tmp_addresses;
1648
+ ServerAddressList* addresses = &tmp_addresses;
1682
1649
  bool is_backend_from_grpclb_load_balancer = false;
1683
1650
  if (serverlist_ != nullptr) {
1684
- GPR_ASSERT(serverlist_->num_servers > 0);
1685
- addresses = ProcessServerlist(serverlist_);
1651
+ tmp_addresses = ProcessServerlist(serverlist_);
1686
1652
  is_backend_from_grpclb_load_balancer = true;
1687
1653
  } else {
1688
1654
  // If CreateOrUpdateRoundRobinPolicyLocked() is invoked when we haven't
@@ -1691,14 +1657,14 @@ grpc_channel_args* GrpcLb::CreateRoundRobinPolicyArgsLocked() {
1691
1657
  // empty, in which case the new round_robin policy will keep the requested
1692
1658
  // picks pending.
1693
1659
  GPR_ASSERT(fallback_backend_addresses_ != nullptr);
1694
- addresses = grpc_lb_addresses_copy(fallback_backend_addresses_);
1660
+ addresses = fallback_backend_addresses_.get();
1695
1661
  }
1696
1662
  GPR_ASSERT(addresses != nullptr);
1697
- // Replace the LB addresses in the channel args that we pass down to
1663
+ // Replace the server address list in the channel args that we pass down to
1698
1664
  // the subchannel.
1699
- static const char* keys_to_remove[] = {GRPC_ARG_LB_ADDRESSES};
1665
+ static const char* keys_to_remove[] = {GRPC_ARG_SERVER_ADDRESS_LIST};
1700
1666
  grpc_arg args_to_add[3] = {
1701
- grpc_lb_addresses_create_channel_arg(addresses),
1667
+ CreateServerAddressListChannelArg(addresses),
1702
1668
  // A channel arg indicating if the target is a backend inferred from a
1703
1669
  // grpclb load balancer.
1704
1670
  grpc_channel_arg_integer_create(
@@ -1715,7 +1681,6 @@ grpc_channel_args* GrpcLb::CreateRoundRobinPolicyArgsLocked() {
1715
1681
  grpc_channel_args* args = grpc_channel_args_copy_and_add_and_remove(
1716
1682
  args_, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), args_to_add,
1717
1683
  num_args_to_add);
1718
- grpc_lb_addresses_destroy(addresses);
1719
1684
  return args;
1720
1685
  }
1721
1686
 
@@ -1735,10 +1700,6 @@ void GrpcLb::CreateOrUpdateRoundRobinPolicyLocked() {
1735
1700
  lb_policy_args.client_channel_factory = client_channel_factory();
1736
1701
  lb_policy_args.args = args;
1737
1702
  CreateRoundRobinPolicyLocked(lb_policy_args);
1738
- if (grpc_lb_glb_trace.enabled()) {
1739
- gpr_log(GPR_INFO, "[grpclb %p] Created new RR policy %p", this,
1740
- rr_policy_.get());
1741
- }
1742
1703
  }
1743
1704
  grpc_channel_args_destroy(args);
1744
1705
  }
@@ -1848,22 +1809,21 @@ class GrpcLbFactory : public LoadBalancingPolicyFactory {
1848
1809
  OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
1849
1810
  const LoadBalancingPolicy::Args& args) const override {
1850
1811
  /* Count the number of gRPC-LB addresses. There must be at least one. */
1851
- const grpc_arg* arg =
1852
- grpc_channel_args_find(args.args, GRPC_ARG_LB_ADDRESSES);
1853
- if (arg == nullptr || arg->type != GRPC_ARG_POINTER) {
1854
- return nullptr;
1855
- }
1856
- grpc_lb_addresses* addresses =
1857
- static_cast<grpc_lb_addresses*>(arg->value.pointer.p);
1858
- size_t num_grpclb_addrs = 0;
1859
- for (size_t i = 0; i < addresses->num_addresses; ++i) {
1860
- if (addresses->addresses[i].is_balancer) ++num_grpclb_addrs;
1812
+ const ServerAddressList* addresses =
1813
+ FindServerAddressListChannelArg(args.args);
1814
+ if (addresses == nullptr) return nullptr;
1815
+ bool found_balancer = false;
1816
+ for (size_t i = 0; i < addresses->size(); ++i) {
1817
+ if ((*addresses)[i].IsBalancer()) {
1818
+ found_balancer = true;
1819
+ break;
1820
+ }
1861
1821
  }
1862
- if (num_grpclb_addrs == 0) return nullptr;
1863
- return OrphanablePtr<LoadBalancingPolicy>(New<GrpcLb>(addresses, args));
1822
+ if (!found_balancer) return nullptr;
1823
+ return OrphanablePtr<LoadBalancingPolicy>(New<GrpcLb>(args));
1864
1824
  }
1865
1825
 
1866
- const char* name() const override { return "grpclb"; }
1826
+ const char* name() const override { return kGrpclb; }
1867
1827
  };
1868
1828
 
1869
1829
  } // namespace