grpc 1.32.0 → 1.33.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 (214) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +175 -376
  3. data/include/grpc/grpc.h +0 -5
  4. data/include/grpc/grpc_security.h +16 -0
  5. data/include/grpc/impl/codegen/grpc_types.h +0 -5
  6. data/src/core/ext/filters/client_channel/client_channel.cc +204 -170
  7. data/src/core/ext/filters/client_channel/config_selector.cc +0 -4
  8. data/src/core/ext/filters/client_channel/config_selector.h +34 -5
  9. data/src/core/ext/filters/client_channel/lb_policy.h +1 -1
  10. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.cc +48 -35
  11. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.h +7 -5
  12. data/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc +3 -2
  13. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +106 -106
  14. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +2 -2
  15. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +3 -3
  16. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +3 -3
  17. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +9 -32
  18. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +3 -3
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +198 -126
  20. data/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc +439 -249
  21. data/src/core/ext/filters/client_channel/lb_policy/xds/eds_drop.cc +571 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +727 -0
  23. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +8 -1
  24. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +1 -1
  25. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +553 -358
  26. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h +28 -0
  27. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +8 -39
  28. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +4 -2
  29. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +44 -43
  30. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +5 -9
  31. data/src/core/ext/filters/client_channel/server_address.cc +80 -0
  32. data/src/core/ext/filters/client_channel/server_address.h +25 -36
  33. data/src/core/ext/filters/client_channel/service_config.cc +16 -13
  34. data/src/core/ext/filters/client_channel/service_config.h +7 -4
  35. data/src/core/ext/filters/client_channel/service_config_channel_arg_filter.cc +2 -2
  36. data/src/core/ext/filters/client_channel/service_config_parser.cc +8 -6
  37. data/src/core/ext/filters/client_channel/service_config_parser.h +8 -5
  38. data/src/core/ext/filters/client_channel/subchannel_interface.h +44 -0
  39. data/src/core/ext/filters/message_size/message_size_filter.cc +2 -1
  40. data/src/core/ext/filters/message_size/message_size_filter.h +2 -1
  41. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +17 -10
  42. data/src/core/ext/transport/chttp2/transport/flow_control.cc +10 -2
  43. data/src/core/ext/transport/chttp2/transport/flow_control.h +10 -0
  44. data/src/core/ext/transport/chttp2/transport/internal.h +5 -0
  45. data/src/core/ext/transport/chttp2/transport/parsing.cc +16 -2
  46. data/src/core/ext/upb-generated/envoy/config/accesslog/v3/accesslog.upb.c +29 -9
  47. data/src/core/ext/upb-generated/envoy/config/accesslog/v3/accesslog.upb.h +66 -0
  48. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.c +123 -45
  49. data/src/core/ext/upb-generated/envoy/config/cluster/v3/cluster.upb.h +310 -53
  50. data/src/core/ext/upb-generated/envoy/config/core/v3/address.upb.c +17 -5
  51. data/src/core/ext/upb-generated/envoy/config/core/v3/address.upb.h +45 -0
  52. data/src/core/ext/upb-generated/envoy/config/core/v3/base.upb.c +1 -0
  53. data/src/core/ext/upb-generated/envoy/config/core/v3/config_source.upb.c +16 -9
  54. data/src/core/ext/upb-generated/envoy/config/core/v3/config_source.upb.h +38 -15
  55. data/src/core/ext/upb-generated/envoy/config/core/v3/extension.upb.c +53 -0
  56. data/src/core/ext/upb-generated/envoy/config/core/v3/extension.upb.h +133 -0
  57. data/src/core/ext/upb-generated/envoy/config/core/v3/grpc_service.upb.c +54 -8
  58. data/src/core/ext/upb-generated/envoy/config/core/v3/grpc_service.upb.h +123 -5
  59. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.c +40 -16
  60. data/src/core/ext/upb-generated/envoy/config/core/v3/protocol.upb.h +114 -5
  61. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.c +36 -0
  62. data/src/core/ext/upb-generated/envoy/config/core/v3/substitution_format_string.upb.h +85 -0
  63. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.c +36 -16
  64. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener.upb.h +86 -20
  65. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.c +23 -6
  66. data/src/core/ext/upb-generated/envoy/config/listener/v3/listener_components.upb.h +54 -5
  67. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.c +10 -6
  68. data/src/core/ext/upb-generated/envoy/config/rbac/v3/rbac.upb.h +28 -11
  69. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.c +184 -57
  70. data/src/core/ext/upb-generated/envoy/config/route/v3/route_components.upb.h +504 -69
  71. data/src/core/ext/upb-generated/envoy/config/route/v3/scoped_route.upb.c +6 -5
  72. data/src/core/ext/upb-generated/envoy/config/route/v3/scoped_route.upb.h +11 -7
  73. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.c +78 -26
  74. data/src/core/ext/upb-generated/envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.upb.h +236 -25
  75. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.c +8 -9
  76. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/common.upb.h +19 -33
  77. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/secret.upb.c +7 -3
  78. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/secret.upb.h +16 -0
  79. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/tls.upb.c +65 -23
  80. data/src/core/ext/upb-generated/envoy/extensions/transport_sockets/tls/v3/tls.upb.h +229 -47
  81. data/src/core/ext/upb-generated/envoy/service/discovery/v3/discovery.upb.c +20 -10
  82. data/src/core/ext/upb-generated/envoy/service/discovery/v3/discovery.upb.h +67 -4
  83. data/src/core/ext/upb-generated/envoy/type/matcher/v3/string.upb.c +3 -2
  84. data/src/core/ext/upb-generated/envoy/type/matcher/v3/string.upb.h +6 -0
  85. data/src/core/ext/upb-generated/google/api/expr/v1alpha1/checked.upb.c +242 -0
  86. data/src/core/ext/upb-generated/google/api/expr/v1alpha1/checked.upb.h +753 -0
  87. data/src/core/ext/upb-generated/udpa/annotations/security.upb.c +31 -0
  88. data/src/core/ext/upb-generated/udpa/annotations/security.upb.h +57 -0
  89. data/src/core/ext/upb-generated/udpa/core/v1/authority.upb.c +28 -0
  90. data/src/core/ext/upb-generated/udpa/core/v1/authority.upb.h +53 -0
  91. data/src/core/ext/upb-generated/udpa/core/v1/collection_entry.upb.c +52 -0
  92. data/src/core/ext/upb-generated/udpa/core/v1/collection_entry.upb.h +129 -0
  93. data/src/core/ext/upb-generated/udpa/core/v1/context_params.upb.c +42 -0
  94. data/src/core/ext/upb-generated/udpa/core/v1/context_params.upb.h +77 -0
  95. data/src/core/ext/upb-generated/udpa/core/v1/resource.upb.c +36 -0
  96. data/src/core/ext/upb-generated/udpa/core/v1/resource.upb.h +85 -0
  97. data/src/core/ext/upb-generated/udpa/core/v1/resource_locator.upb.c +54 -0
  98. data/src/core/ext/upb-generated/udpa/core/v1/resource_locator.upb.h +160 -0
  99. data/src/core/ext/upb-generated/udpa/core/v1/resource_name.upb.c +36 -0
  100. data/src/core/ext/upb-generated/udpa/core/v1/resource_name.upb.h +84 -0
  101. data/src/core/ext/xds/certificate_provider_factory.h +59 -0
  102. data/src/core/ext/xds/certificate_provider_registry.cc +103 -0
  103. data/src/core/ext/xds/certificate_provider_registry.h +57 -0
  104. data/src/core/ext/xds/certificate_provider_store.h +50 -0
  105. data/src/core/ext/xds/google_mesh_ca_certificate_provider_factory.cc +377 -0
  106. data/src/core/ext/xds/google_mesh_ca_certificate_provider_factory.h +102 -0
  107. data/src/core/ext/xds/xds_api.cc +301 -93
  108. data/src/core/ext/xds/xds_api.h +129 -92
  109. data/src/core/ext/xds/xds_channel_args.h +6 -3
  110. data/src/core/ext/xds/xds_client.cc +498 -410
  111. data/src/core/ext/xds/xds_client.h +105 -51
  112. data/src/core/ext/xds/xds_client_stats.cc +18 -12
  113. data/src/core/ext/xds/xds_client_stats.h +33 -5
  114. data/src/core/lib/channel/channel_args.h +0 -1
  115. data/src/core/lib/channel/channelz.cc +10 -45
  116. data/src/core/lib/channel/channelz.h +11 -19
  117. data/src/core/lib/channel/channelz_registry.cc +12 -11
  118. data/src/core/lib/channel/channelz_registry.h +3 -0
  119. data/src/core/lib/gpr/time_precise.cc +2 -0
  120. data/src/core/lib/gpr/time_precise.h +6 -2
  121. data/src/core/lib/gprpp/dual_ref_counted.h +336 -0
  122. data/src/core/lib/gprpp/ref_counted.h +51 -22
  123. data/src/core/lib/gprpp/ref_counted_ptr.h +153 -0
  124. data/src/core/lib/iomgr/endpoint_cfstream.cc +9 -5
  125. data/src/core/lib/iomgr/exec_ctx.h +10 -8
  126. data/src/core/lib/json/json_util.cc +58 -0
  127. data/src/core/lib/json/json_util.h +37 -0
  128. data/src/core/lib/security/certificate_provider.h +60 -0
  129. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.cc +321 -0
  130. data/src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h +214 -0
  131. data/src/core/lib/security/credentials/xds/xds_credentials.cc +45 -0
  132. data/src/core/lib/security/credentials/xds/xds_credentials.h +51 -0
  133. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +6 -10
  134. data/src/core/lib/security/security_connector/ssl_utils.h +5 -0
  135. data/src/core/lib/surface/channel.cc +9 -31
  136. data/src/core/lib/surface/channel.h +6 -1
  137. data/src/core/lib/surface/init.cc +26 -9
  138. data/src/core/lib/surface/version.cc +2 -2
  139. data/src/core/lib/transport/bdp_estimator.h +2 -1
  140. data/src/core/lib/transport/connectivity_state.h +2 -2
  141. data/src/core/lib/transport/metadata.cc +11 -1
  142. data/src/core/plugin_registry/grpc_plugin_registry.cc +35 -20
  143. data/src/core/tsi/ssl_transport_security.cc +2 -2
  144. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -2
  145. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +3 -3
  146. data/src/ruby/lib/grpc/version.rb +1 -1
  147. data/third_party/boringssl-with-bazel/err_data.c +465 -463
  148. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_lib.c +0 -6
  149. data/third_party/boringssl-with-bazel/src/crypto/dsa/dsa.c +9 -43
  150. data/third_party/boringssl-with-bazel/src/crypto/dsa/dsa_asn1.c +55 -4
  151. data/third_party/boringssl-with-bazel/src/crypto/dsa/internal.h +34 -0
  152. data/third_party/boringssl-with-bazel/src/crypto/evp/evp.c +4 -0
  153. data/third_party/boringssl-with-bazel/src/crypto/evp/p_dsa_asn1.c +6 -2
  154. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digest.c +2 -0
  155. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/internal.h +4 -0
  156. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +30 -10
  157. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +10 -15
  158. data/third_party/boringssl-with-bazel/src/crypto/hpke/hpke.c +98 -11
  159. data/third_party/boringssl-with-bazel/src/crypto/hpke/internal.h +51 -6
  160. data/third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h +44 -2
  161. data/third_party/boringssl-with-bazel/src/crypto/trust_token/pmbtoken.c +221 -49
  162. data/third_party/boringssl-with-bazel/src/crypto/trust_token/trust_token.c +64 -20
  163. data/third_party/boringssl-with-bazel/src/crypto/x509/a_strex.c +3 -3
  164. data/third_party/boringssl-with-bazel/src/crypto/x509/algorithm.c +0 -8
  165. data/third_party/boringssl-with-bazel/src/crypto/x509/t_crl.c +3 -3
  166. data/third_party/boringssl-with-bazel/src/crypto/x509/t_x509.c +1 -1
  167. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +7 -2
  168. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_ext.c +21 -18
  169. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_obj.c +1 -1
  170. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +24 -3
  171. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +3 -3
  172. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_txt.c +67 -67
  173. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_v3.c +3 -3
  174. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +29 -35
  175. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +13 -2
  176. data/third_party/boringssl-with-bazel/src/crypto/x509/x509name.c +9 -8
  177. data/third_party/boringssl-with-bazel/src/crypto/x509/x_all.c +10 -10
  178. data/third_party/boringssl-with-bazel/src/crypto/x509/x_crl.c +2 -2
  179. data/third_party/boringssl-with-bazel/src/crypto/x509/x_name.c +28 -40
  180. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509.c +3 -1
  181. data/third_party/boringssl-with-bazel/src/crypto/x509v3/ext_dat.h +1 -4
  182. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_conf.c +7 -3
  183. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_genn.c +2 -2
  184. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_info.c +1 -1
  185. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +55 -8
  186. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_skey.c +1 -1
  187. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +0 -1
  188. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +1 -1
  189. data/third_party/boringssl-with-bazel/src/include/openssl/cipher.h +6 -0
  190. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +1 -1
  191. data/third_party/boringssl-with-bazel/src/include/openssl/dh.h +12 -0
  192. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +9 -0
  193. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +4 -1
  194. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +9 -2
  195. data/third_party/boringssl-with-bazel/src/include/openssl/trust_token.h +26 -6
  196. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +188 -78
  197. data/third_party/boringssl-with-bazel/src/include/openssl/x509v3.h +52 -43
  198. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +18 -18
  199. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +2 -3
  200. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +1 -1
  201. data/third_party/boringssl-with-bazel/src/ssl/internal.h +9 -9
  202. data/third_party/boringssl-with-bazel/src/ssl/ssl_cipher.cc +8 -9
  203. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +1 -2
  204. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +4 -8
  205. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +2 -2
  206. metadata +72 -42
  207. data/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc +0 -537
  208. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc +0 -1141
  209. data/src/core/ext/upb-generated/gogoproto/gogo.upb.c +0 -17
  210. data/src/core/ext/upb-generated/gogoproto/gogo.upb.h +0 -29
  211. data/src/core/ext/xds/xds_channel.h +0 -46
  212. data/src/core/ext/xds/xds_channel_secure.cc +0 -103
  213. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_pku.c +0 -110
  214. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_sxnet.c +0 -274
@@ -23,6 +23,7 @@
23
23
 
24
24
  #include <grpc/grpc.h>
25
25
 
26
+ #include <set>
26
27
  #include <string>
27
28
 
28
29
  #include "absl/container/inlined_vector.h"
@@ -42,8 +43,9 @@
42
43
  // Channel arg key for channelz node.
43
44
  #define GRPC_ARG_CHANNELZ_CHANNEL_NODE "grpc.channelz_channel_node"
44
45
 
45
- // Channel arg key to encode the channelz uuid of the channel's parent.
46
- #define GRPC_ARG_CHANNELZ_PARENT_UUID "grpc.channelz_parent_uuid"
46
+ // Channel arg key for indicating an internal channel.
47
+ #define GRPC_ARG_CHANNELZ_IS_INTERNAL_CHANNEL \
48
+ "grpc.channelz_is_internal_channel"
47
49
 
48
50
  /** This is the default value for whether or not to enable channelz. If
49
51
  * GRPC_ARG_ENABLE_CHANNELZ is set, it will override this default value. */
@@ -59,10 +61,6 @@ namespace grpc_core {
59
61
 
60
62
  namespace channelz {
61
63
 
62
- // Helpers for getting and setting GRPC_ARG_CHANNELZ_PARENT_UUID.
63
- grpc_arg MakeParentUuidArg(intptr_t parent_uuid);
64
- intptr_t GetParentUuidFromArgs(const grpc_channel_args& args);
65
-
66
64
  class SocketNode;
67
65
  class ListenSocketNode;
68
66
 
@@ -176,14 +174,12 @@ class CallCountingHelper {
176
174
  class ChannelNode : public BaseNode {
177
175
  public:
178
176
  ChannelNode(std::string target, size_t channel_tracer_max_nodes,
179
- intptr_t parent_uuid);
177
+ bool is_internal_channel);
180
178
 
181
179
  // Returns the string description of the given connectivity state.
182
180
  static const char* GetChannelConnectivityStateChangeString(
183
181
  grpc_connectivity_state state);
184
182
 
185
- intptr_t parent_uuid() const { return parent_uuid_; }
186
-
187
183
  Json RenderJson() override;
188
184
 
189
185
  // proxy methods to composed classes.
@@ -213,26 +209,22 @@ class ChannelNode : public BaseNode {
213
209
  void RemoveChildSubchannel(intptr_t child_uuid);
214
210
 
215
211
  private:
216
- void PopulateChildRefs(Json::Object* json);
217
-
218
- // to allow the channel trace test to access trace_.
212
+ // Allows the channel trace test to access trace_.
219
213
  friend class testing::ChannelNodePeer;
220
214
 
215
+ void PopulateChildRefs(Json::Object* json);
216
+
221
217
  std::string target_;
222
218
  CallCountingHelper call_counter_;
223
219
  ChannelTrace trace_;
224
- const intptr_t parent_uuid_;
225
220
 
226
221
  // Least significant bit indicates whether the value is set. Remaining
227
222
  // bits are a grpc_connectivity_state value.
228
223
  Atomic<int> connectivity_state_{0};
229
224
 
230
- Mutex child_mu_; // Guards child maps below.
231
- // TODO(roth): We don't actually use the values here, only the keys, so
232
- // these should be sets instead of maps, but we don't currently have a set
233
- // implementation. Change this if/when we have one.
234
- std::map<intptr_t, bool> child_channels_;
235
- std::map<intptr_t, bool> child_subchannels_;
225
+ Mutex child_mu_; // Guards sets below.
226
+ std::set<intptr_t> child_channels_;
227
+ std::set<intptr_t> child_subchannels_;
236
228
  };
237
229
 
238
230
  // Handles channelz bookkeeping for servers
@@ -78,8 +78,7 @@ RefCountedPtr<BaseNode> ChannelzRegistry::InternalGet(intptr_t uuid) {
78
78
  // Found node. Return only if its refcount is not zero (i.e., when we
79
79
  // know that there is no other thread about to destroy it).
80
80
  BaseNode* node = it->second;
81
- if (!node->RefIfNonZero()) return nullptr;
82
- return RefCountedPtr<BaseNode>(node);
81
+ return node->RefIfNonZero();
83
82
  }
84
83
 
85
84
  std::string ChannelzRegistry::InternalGetTopChannels(
@@ -91,8 +90,9 @@ std::string ChannelzRegistry::InternalGetTopChannels(
91
90
  for (auto it = node_map_.lower_bound(start_channel_id);
92
91
  it != node_map_.end(); ++it) {
93
92
  BaseNode* node = it->second;
93
+ RefCountedPtr<BaseNode> node_ref;
94
94
  if (node->type() == BaseNode::EntityType::kTopLevelChannel &&
95
- node->RefIfNonZero()) {
95
+ (node_ref = node->RefIfNonZero()) != nullptr) {
96
96
  // Check if we are over pagination limit to determine if we need to set
97
97
  // the "end" element. If we don't go through this block, we know that
98
98
  // when the loop terminates, we have <= to kPaginationLimit.
@@ -100,10 +100,10 @@ std::string ChannelzRegistry::InternalGetTopChannels(
100
100
  // refcount, we need to decrease it, but we can't unref while
101
101
  // holding the lock, because this may lead to a deadlock.
102
102
  if (top_level_channels.size() == kPaginationLimit) {
103
- node_after_pagination_limit.reset(node);
103
+ node_after_pagination_limit = std::move(node_ref);
104
104
  break;
105
105
  }
106
- top_level_channels.emplace_back(node);
106
+ top_level_channels.emplace_back(std::move(node_ref));
107
107
  }
108
108
  }
109
109
  }
@@ -129,8 +129,9 @@ std::string ChannelzRegistry::InternalGetServers(intptr_t start_server_id) {
129
129
  for (auto it = node_map_.lower_bound(start_server_id);
130
130
  it != node_map_.end(); ++it) {
131
131
  BaseNode* node = it->second;
132
+ RefCountedPtr<BaseNode> node_ref;
132
133
  if (node->type() == BaseNode::EntityType::kServer &&
133
- node->RefIfNonZero()) {
134
+ (node_ref = node->RefIfNonZero()) != nullptr) {
134
135
  // Check if we are over pagination limit to determine if we need to set
135
136
  // the "end" element. If we don't go through this block, we know that
136
137
  // when the loop terminates, we have <= to kPaginationLimit.
@@ -138,10 +139,10 @@ std::string ChannelzRegistry::InternalGetServers(intptr_t start_server_id) {
138
139
  // refcount, we need to decrease it, but we can't unref while
139
140
  // holding the lock, because this may lead to a deadlock.
140
141
  if (servers.size() == kPaginationLimit) {
141
- node_after_pagination_limit.reset(node);
142
+ node_after_pagination_limit = std::move(node_ref);
142
143
  break;
143
144
  }
144
- servers.emplace_back(node);
145
+ servers.emplace_back(std::move(node_ref));
145
146
  }
146
147
  }
147
148
  }
@@ -164,9 +165,9 @@ void ChannelzRegistry::InternalLogAllEntities() {
164
165
  {
165
166
  MutexLock lock(&mu_);
166
167
  for (auto& p : node_map_) {
167
- BaseNode* node = p.second;
168
- if (node->RefIfNonZero()) {
169
- nodes.emplace_back(node);
168
+ RefCountedPtr<BaseNode> node = p.second->RefIfNonZero();
169
+ if (node != nullptr) {
170
+ nodes.emplace_back(std::move(node));
170
171
  }
171
172
  }
172
173
  }
@@ -23,6 +23,9 @@
23
23
 
24
24
  #include <stdint.h>
25
25
 
26
+ #include <map>
27
+ #include <string>
28
+
26
29
  #include "src/core/lib/channel/channel_trace.h"
27
30
  #include "src/core/lib/channel/channelz.h"
28
31
  #include "src/core/lib/gprpp/map.h"
@@ -31,6 +31,7 @@
31
31
 
32
32
  #include "src/core/lib/gpr/time_precise.h"
33
33
 
34
+ #ifndef GPR_CYCLE_COUNTER_CUSTOM
34
35
  #if GPR_CYCLE_COUNTER_RDTSC_32 || GPR_CYCLE_COUNTER_RDTSC_64
35
36
  #if GPR_LINUX
36
37
  static bool read_freq_from_kernel(double* freq) {
@@ -163,3 +164,4 @@ gpr_timespec gpr_cycle_counter_sub(gpr_cycle_counter a, gpr_cycle_counter b) {
163
164
  gpr_cycle_counter_to_time(b));
164
165
  }
165
166
  #endif /* GPR_CYCLE_COUNTER_FALLBACK */
167
+ #endif /* !GPR_CYCLE_COUNTER_CUSTOM */
@@ -31,7 +31,10 @@
31
31
  // Using gpr_get_cycle_counter() is preferred to using ExecCtx::Get()->Now()
32
32
  // whenever possible.
33
33
 
34
- #if GPR_CYCLE_COUNTER_RDTSC_32
34
+ #if GPR_CYCLE_COUNTER_CUSTOM
35
+ typedef int64_t gpr_cycle_counter;
36
+ gpr_cycle_counter gpr_get_cycle_counter();
37
+ #elif GPR_CYCLE_COUNTER_RDTSC_32
35
38
  typedef int64_t gpr_cycle_counter;
36
39
  inline gpr_cycle_counter gpr_get_cycle_counter() {
37
40
  int64_t ret;
@@ -54,7 +57,8 @@ gpr_cycle_counter gpr_get_cycle_counter();
54
57
  #else
55
58
  #error Must define exactly one of \
56
59
  GPR_CYCLE_COUNTER_RDTSC_32, \
57
- GPR_CYCLE_COUNTER_RDTSC_64, or \
60
+ GPR_CYCLE_COUNTER_RDTSC_64, \
61
+ GPR_CYCLE_COUNTER_CUSTOM, or \
58
62
  GPR_CYCLE_COUNTER_FALLBACK
59
63
  #endif
60
64
 
@@ -0,0 +1,336 @@
1
+ //
2
+ // Copyright 2020 gRPC authors.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+
17
+ #ifndef GRPC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H
18
+ #define GRPC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H
19
+
20
+ #include <grpc/support/port_platform.h>
21
+
22
+ #include <grpc/support/atm.h>
23
+ #include <grpc/support/log.h>
24
+ #include <grpc/support/sync.h>
25
+
26
+ #include <atomic>
27
+ #include <cassert>
28
+ #include <cinttypes>
29
+
30
+ #include "src/core/lib/debug/trace.h"
31
+ #include "src/core/lib/gprpp/atomic.h"
32
+ #include "src/core/lib/gprpp/debug_location.h"
33
+ #include "src/core/lib/gprpp/orphanable.h"
34
+ #include "src/core/lib/gprpp/ref_counted_ptr.h"
35
+
36
+ namespace grpc_core {
37
+
38
+ // DualRefCounted is an interface for reference-counted objects with two
39
+ // classes of refs: strong refs (usually just called "refs") and weak refs.
40
+ // This supports cases where an object needs to start shutting down when
41
+ // all external callers are done with it (represented by strong refs) but
42
+ // cannot be destroyed until all internal callbacks are complete
43
+ // (represented by weak refs).
44
+ //
45
+ // Each class of refs can be incremented and decremented independently.
46
+ // Objects start with 1 strong ref and 0 weak refs at instantiation.
47
+ // When the strong refcount reaches 0, the object's Orphan() method is called.
48
+ // When the weak refcount reaches 0, the object is destroyed.
49
+ //
50
+ // This will be used by CRTP (curiously-recurring template pattern), e.g.:
51
+ // class MyClass : public RefCounted<MyClass> { ... };
52
+ template <typename Child>
53
+ class DualRefCounted : public Orphanable {
54
+ public:
55
+ virtual ~DualRefCounted() = default;
56
+
57
+ RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
58
+ IncrementRefCount();
59
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
60
+ }
61
+
62
+ RefCountedPtr<Child> Ref(const DebugLocation& location,
63
+ const char* reason) GRPC_MUST_USE_RESULT {
64
+ IncrementRefCount(location, reason);
65
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
66
+ }
67
+
68
+ void Unref() {
69
+ // Convert strong ref to weak ref.
70
+ const uint64_t prev_ref_pair =
71
+ refs_.FetchAdd(MakeRefPair(-1, 1), MemoryOrder::ACQ_REL);
72
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
73
+ #ifndef NDEBUG
74
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
75
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
76
+ gpr_log(GPR_INFO, "%s:%p unref %d -> %d, weak_ref %d -> %d",
77
+ trace_flag_->name(), this, strong_refs, strong_refs - 1,
78
+ weak_refs, weak_refs + 1);
79
+ }
80
+ GPR_ASSERT(strong_refs > 0);
81
+ #endif
82
+ if (GPR_UNLIKELY(strong_refs == 1)) {
83
+ Orphan();
84
+ }
85
+ // Now drop the weak ref.
86
+ WeakUnref();
87
+ }
88
+ void Unref(const DebugLocation& location, const char* reason) {
89
+ const uint64_t prev_ref_pair =
90
+ refs_.FetchAdd(MakeRefPair(-1, 1), MemoryOrder::ACQ_REL);
91
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
92
+ #ifndef NDEBUG
93
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
94
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
95
+ gpr_log(GPR_INFO, "%s:%p %s:%d unref %d -> %d, weak_ref %d -> %d) %s",
96
+ trace_flag_->name(), this, location.file(), location.line(),
97
+ strong_refs, strong_refs - 1, weak_refs, weak_refs + 1, reason);
98
+ }
99
+ GPR_ASSERT(strong_refs > 0);
100
+ #else
101
+ // Avoid unused-parameter warnings for debug-only parameters
102
+ (void)location;
103
+ (void)reason;
104
+ #endif
105
+ if (GPR_UNLIKELY(strong_refs == 1)) {
106
+ Orphan();
107
+ }
108
+ // Now drop the weak ref.
109
+ WeakUnref(location, reason);
110
+ }
111
+
112
+ RefCountedPtr<Child> RefIfNonZero() GRPC_MUST_USE_RESULT {
113
+ uint64_t prev_ref_pair = refs_.Load(MemoryOrder::ACQUIRE);
114
+ do {
115
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
116
+ #ifndef NDEBUG
117
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
118
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
119
+ gpr_log(GPR_INFO, "%s:%p ref_if_non_zero %d -> %d (weak_refs=%d)",
120
+ trace_flag_->name(), this, strong_refs, strong_refs + 1,
121
+ weak_refs);
122
+ }
123
+ #endif
124
+ if (strong_refs == 0) return nullptr;
125
+ } while (!refs_.CompareExchangeWeak(
126
+ &prev_ref_pair, prev_ref_pair + MakeRefPair(1, 0), MemoryOrder::ACQ_REL,
127
+ MemoryOrder::ACQUIRE));
128
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
129
+ }
130
+
131
+ RefCountedPtr<Child> RefIfNonZero(const DebugLocation& location,
132
+ const char* reason) GRPC_MUST_USE_RESULT {
133
+ uint64_t prev_ref_pair = refs_.Load(MemoryOrder::ACQUIRE);
134
+ do {
135
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
136
+ #ifndef NDEBUG
137
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
138
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
139
+ gpr_log(GPR_INFO,
140
+ "%s:%p %s:%d ref_if_non_zero %d -> %d (weak_refs=%d) %s",
141
+ trace_flag_->name(), this, location.file(), location.line(),
142
+ strong_refs, strong_refs + 1, weak_refs, reason);
143
+ }
144
+ #else
145
+ // Avoid unused-parameter warnings for debug-only parameters
146
+ (void)location;
147
+ (void)reason;
148
+ #endif
149
+ if (strong_refs == 0) return nullptr;
150
+ } while (!refs_.CompareExchangeWeak(
151
+ &prev_ref_pair, prev_ref_pair + MakeRefPair(1, 0), MemoryOrder::ACQ_REL,
152
+ MemoryOrder::ACQUIRE));
153
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
154
+ }
155
+
156
+ WeakRefCountedPtr<Child> WeakRef() GRPC_MUST_USE_RESULT {
157
+ IncrementWeakRefCount();
158
+ return WeakRefCountedPtr<Child>(static_cast<Child*>(this));
159
+ }
160
+
161
+ WeakRefCountedPtr<Child> WeakRef(const DebugLocation& location,
162
+ const char* reason) GRPC_MUST_USE_RESULT {
163
+ IncrementWeakRefCount(location, reason);
164
+ return WeakRefCountedPtr<Child>(static_cast<Child*>(this));
165
+ }
166
+
167
+ void WeakUnref() {
168
+ #ifndef NDEBUG
169
+ // Grab a copy of the trace flag before the atomic change, since we
170
+ // can't safely access it afterwards if we're going to be freed.
171
+ auto* trace_flag = trace_flag_;
172
+ #endif
173
+ const uint64_t prev_ref_pair =
174
+ refs_.FetchSub(MakeRefPair(0, 1), MemoryOrder::ACQ_REL);
175
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
176
+ #ifndef NDEBUG
177
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
178
+ if (trace_flag != nullptr && trace_flag->enabled()) {
179
+ gpr_log(GPR_INFO, "%s:%p weak_unref %d -> %d (refs=%d)",
180
+ trace_flag->name(), this, weak_refs, weak_refs - 1, strong_refs);
181
+ }
182
+ GPR_ASSERT(weak_refs > 0);
183
+ #endif
184
+ if (GPR_UNLIKELY(prev_ref_pair == MakeRefPair(0, 1))) {
185
+ delete static_cast<Child*>(this);
186
+ }
187
+ }
188
+ void WeakUnref(const DebugLocation& location, const char* reason) {
189
+ #ifndef NDEBUG
190
+ // Grab a copy of the trace flag before the atomic change, since we
191
+ // can't safely access it afterwards if we're going to be freed.
192
+ auto* trace_flag = trace_flag_;
193
+ #endif
194
+ const uint64_t prev_ref_pair =
195
+ refs_.FetchSub(MakeRefPair(0, 1), MemoryOrder::ACQ_REL);
196
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
197
+ #ifndef NDEBUG
198
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
199
+ if (trace_flag != nullptr && trace_flag->enabled()) {
200
+ gpr_log(GPR_INFO, "%s:%p %s:%d weak_unref %d -> %d (refs=%d) %s",
201
+ trace_flag->name(), this, location.file(), location.line(),
202
+ weak_refs, weak_refs - 1, strong_refs, reason);
203
+ }
204
+ GPR_ASSERT(weak_refs > 0);
205
+ #else
206
+ // Avoid unused-parameter warnings for debug-only parameters
207
+ (void)location;
208
+ (void)reason;
209
+ #endif
210
+ if (GPR_UNLIKELY(prev_ref_pair == MakeRefPair(0, 1))) {
211
+ delete static_cast<Child*>(this);
212
+ }
213
+ }
214
+
215
+ // Not copyable nor movable.
216
+ DualRefCounted(const DualRefCounted&) = delete;
217
+ DualRefCounted& operator=(const DualRefCounted&) = delete;
218
+
219
+ protected:
220
+ // TraceFlagT is defined to accept both DebugOnlyTraceFlag and TraceFlag.
221
+ // Note: RefCount tracing is only enabled on debug builds, even when a
222
+ // TraceFlag is used.
223
+ template <typename TraceFlagT = TraceFlag>
224
+ explicit DualRefCounted(
225
+ TraceFlagT*
226
+ #ifndef NDEBUG
227
+ // Leave unnamed if NDEBUG to avoid unused parameter warning
228
+ trace_flag
229
+ #endif
230
+ = nullptr,
231
+ int32_t initial_refcount = 1)
232
+ :
233
+ #ifndef NDEBUG
234
+ trace_flag_(trace_flag),
235
+ #endif
236
+ refs_(MakeRefPair(initial_refcount, 0)) {
237
+ }
238
+
239
+ private:
240
+ // Allow RefCountedPtr<> to access IncrementRefCount().
241
+ template <typename T>
242
+ friend class RefCountedPtr;
243
+ // Allow WeakRefCountedPtr<> to access IncrementWeakRefCount().
244
+ template <typename T>
245
+ friend class WeakRefCountedPtr;
246
+
247
+ // First 32 bits are strong refs, next 32 bits are weak refs.
248
+ static uint64_t MakeRefPair(uint32_t strong, uint32_t weak) {
249
+ return (static_cast<uint64_t>(strong) << 32) + static_cast<int64_t>(weak);
250
+ }
251
+ static uint32_t GetStrongRefs(uint64_t ref_pair) {
252
+ return static_cast<uint32_t>(ref_pair >> 32);
253
+ }
254
+ static uint32_t GetWeakRefs(uint64_t ref_pair) {
255
+ return static_cast<uint32_t>(ref_pair & 0xffffffffu);
256
+ }
257
+
258
+ void IncrementRefCount() {
259
+ #ifndef NDEBUG
260
+ const uint64_t prev_ref_pair =
261
+ refs_.FetchAdd(MakeRefPair(1, 0), MemoryOrder::RELAXED);
262
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
263
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
264
+ GPR_ASSERT(strong_refs != 0);
265
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
266
+ gpr_log(GPR_INFO, "%s:%p ref %d -> %d; (weak_refs=%d)",
267
+ trace_flag_->name(), this, strong_refs, strong_refs + 1,
268
+ weak_refs);
269
+ }
270
+ #else
271
+ refs_.FetchAdd(MakeRefPair(1, 0), MemoryOrder::RELAXED);
272
+ #endif
273
+ }
274
+ void IncrementRefCount(const DebugLocation& location, const char* reason) {
275
+ #ifndef NDEBUG
276
+ const uint64_t prev_ref_pair =
277
+ refs_.FetchAdd(MakeRefPair(1, 0), MemoryOrder::RELAXED);
278
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
279
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
280
+ GPR_ASSERT(strong_refs != 0);
281
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
282
+ gpr_log(GPR_INFO, "%s:%p %s:%d ref %d -> %d (weak_refs=%d) %s",
283
+ trace_flag_->name(), this, location.file(), location.line(),
284
+ strong_refs, strong_refs + 1, weak_refs, reason);
285
+ }
286
+ #else
287
+ // Use conditionally-important parameters
288
+ (void)location;
289
+ (void)reason;
290
+ refs_.FetchAdd(MakeRefPair(1, 0), MemoryOrder::RELAXED);
291
+ #endif
292
+ }
293
+
294
+ void IncrementWeakRefCount() {
295
+ #ifndef NDEBUG
296
+ const uint64_t prev_ref_pair =
297
+ refs_.FetchAdd(MakeRefPair(0, 1), MemoryOrder::RELAXED);
298
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
299
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
300
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
301
+ gpr_log(GPR_INFO, "%s:%p weak_ref %d -> %d; (refs=%d)",
302
+ trace_flag_->name(), this, weak_refs, weak_refs + 1, strong_refs);
303
+ }
304
+ #else
305
+ refs_.FetchAdd(MakeRefPair(0, 1), MemoryOrder::RELAXED);
306
+ #endif
307
+ }
308
+ void IncrementWeakRefCount(const DebugLocation& location,
309
+ const char* reason) {
310
+ #ifndef NDEBUG
311
+ const uint64_t prev_ref_pair =
312
+ refs_.FetchAdd(MakeRefPair(0, 1), MemoryOrder::RELAXED);
313
+ const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
314
+ const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
315
+ if (trace_flag_ != nullptr && trace_flag_->enabled()) {
316
+ gpr_log(GPR_INFO, "%s:%p %s:%d weak_ref %d -> %d (refs=%d) %s",
317
+ trace_flag_->name(), this, location.file(), location.line(),
318
+ weak_refs, weak_refs + 1, strong_refs, reason);
319
+ }
320
+ #else
321
+ // Use conditionally-important parameters
322
+ (void)location;
323
+ (void)reason;
324
+ refs_.FetchAdd(MakeRefPair(0, 1), MemoryOrder::RELAXED);
325
+ #endif
326
+ }
327
+
328
+ #ifndef NDEBUG
329
+ TraceFlag* trace_flag_;
330
+ #endif
331
+ Atomic<uint64_t> refs_;
332
+ };
333
+
334
+ } // namespace grpc_core
335
+
336
+ #endif /* GRPC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H */