grpc 1.16.0 → 1.17.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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +299 -133
  3. data/include/grpc/grpc.h +11 -1
  4. data/include/grpc/grpc_posix.h +0 -8
  5. data/include/grpc/impl/codegen/grpc_types.h +3 -0
  6. data/src/core/ext/filters/client_channel/client_channel.cc +336 -345
  7. data/src/core/ext/filters/client_channel/client_channel.h +6 -2
  8. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +3 -1
  9. data/src/core/ext/filters/client_channel/client_channel_channelz.h +0 -7
  10. data/src/core/ext/filters/client_channel/health/health.pb.c +23 -0
  11. data/src/core/ext/filters/client_channel/health/health.pb.h +73 -0
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +652 -0
  13. data/src/core/ext/filters/client_channel/health/health_check_client.h +173 -0
  14. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +2 -1
  15. data/src/core/ext/filters/client_channel/http_proxy.cc +1 -1
  16. data/src/core/ext/filters/client_channel/lb_policy.h +17 -14
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +15 -11
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +21 -15
  19. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +18 -10
  20. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +12 -9
  21. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +19 -8
  22. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +1832 -0
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.h +36 -0
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h +36 -0
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +107 -0
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.cc +85 -0
  27. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h +72 -0
  28. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.cc +307 -0
  29. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +89 -0
  30. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  31. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +5 -0
  32. data/src/core/ext/filters/client_channel/lb_policy_registry.h +4 -0
  33. data/src/core/ext/filters/client_channel/parse_address.h +1 -1
  34. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +19 -22
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +41 -39
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +3 -2
  37. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +4 -1
  38. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +15 -2
  39. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +5 -1
  40. data/src/core/ext/filters/client_channel/resolver_factory.h +1 -1
  41. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +384 -0
  42. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +146 -0
  43. data/src/core/ext/filters/client_channel/subchannel.cc +361 -103
  44. data/src/core/ext/filters/client_channel/subchannel.h +14 -8
  45. data/src/core/ext/filters/deadline/deadline_filter.cc +19 -23
  46. data/src/core/ext/filters/deadline/deadline_filter.h +9 -13
  47. data/src/core/ext/filters/http/client/http_client_filter.cc +29 -19
  48. data/src/core/ext/filters/http/client_authority_filter.cc +2 -3
  49. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +28 -16
  50. data/src/core/ext/filters/http/server/http_server_filter.cc +31 -20
  51. data/src/core/ext/filters/message_size/message_size_filter.cc +50 -45
  52. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +13 -6
  53. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +1 -1
  54. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +58 -8
  55. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +1 -1
  56. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +175 -173
  57. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +2 -1
  58. data/src/core/ext/transport/chttp2/transport/frame_data.cc +4 -10
  59. data/src/core/ext/transport/chttp2/transport/frame_data.h +10 -12
  60. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +1 -1
  61. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +28 -25
  62. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +0 -12
  63. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +12 -9
  64. data/src/core/ext/transport/chttp2/transport/internal.h +109 -94
  65. data/src/core/ext/transport/chttp2/transport/parsing.cc +4 -2
  66. data/src/core/ext/transport/inproc/inproc_transport.cc +280 -300
  67. data/src/core/lib/channel/channel_stack.cc +5 -4
  68. data/src/core/lib/channel/channel_stack.h +4 -4
  69. data/src/core/lib/channel/channel_stack_builder.cc +14 -2
  70. data/src/core/lib/channel/channel_stack_builder.h +8 -0
  71. data/src/core/lib/channel/channel_trace.cc +6 -2
  72. data/src/core/lib/channel/channelz.cc +137 -5
  73. data/src/core/lib/channel/channelz.h +32 -6
  74. data/src/core/lib/channel/channelz_registry.cc +134 -28
  75. data/src/core/lib/channel/channelz_registry.h +25 -3
  76. data/src/core/lib/channel/context.h +4 -4
  77. data/src/core/lib/channel/handshaker.cc +7 -6
  78. data/src/core/lib/channel/handshaker.h +7 -8
  79. data/src/core/lib/channel/handshaker_factory.cc +3 -2
  80. data/src/core/lib/channel/handshaker_factory.h +2 -0
  81. data/src/core/lib/channel/handshaker_registry.cc +6 -2
  82. data/src/core/lib/channel/handshaker_registry.h +1 -0
  83. data/src/core/lib/gpr/arena.cc +84 -37
  84. data/src/core/lib/gpr/arena.h +2 -0
  85. data/src/core/lib/gpr/mpscq.h +4 -2
  86. data/src/core/lib/gprpp/inlined_vector.h +8 -0
  87. data/src/core/lib/gprpp/ref_counted.h +105 -18
  88. data/src/core/lib/gprpp/ref_counted_ptr.h +11 -0
  89. data/src/core/lib/http/httpcli_security_connector.cc +7 -4
  90. data/src/core/lib/iomgr/call_combiner.cc +2 -0
  91. data/src/core/lib/iomgr/call_combiner.h +2 -2
  92. data/src/core/lib/iomgr/closure.h +1 -0
  93. data/src/core/lib/iomgr/error.cc +16 -31
  94. data/src/core/lib/iomgr/error.h +29 -4
  95. data/src/core/lib/iomgr/error_internal.h +0 -2
  96. data/src/core/lib/iomgr/ev_epoll1_linux.cc +7 -3
  97. data/src/core/lib/iomgr/ev_posix.cc +0 -2
  98. data/src/core/lib/iomgr/polling_entity.h +4 -4
  99. data/src/core/lib/iomgr/resource_quota.cc +64 -10
  100. data/src/core/lib/iomgr/resource_quota.h +21 -6
  101. data/src/core/lib/iomgr/socket_utils_common_posix.cc +11 -5
  102. data/src/core/lib/iomgr/tcp_client_custom.cc +14 -3
  103. data/src/core/lib/iomgr/tcp_client_posix.cc +2 -0
  104. data/src/core/lib/iomgr/tcp_posix.cc +4 -2
  105. data/src/core/lib/iomgr/timer_manager.cc +1 -1
  106. data/src/core/lib/iomgr/wakeup_fd_eventfd.cc +3 -4
  107. data/src/core/lib/security/context/security_context.cc +20 -13
  108. data/src/core/lib/security/context/security_context.h +27 -19
  109. data/src/core/lib/security/credentials/alts/alts_credentials.cc +1 -1
  110. data/src/core/lib/security/credentials/credentials.h +2 -2
  111. data/src/core/lib/security/credentials/fake/fake_credentials.cc +1 -0
  112. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +39 -54
  113. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +3 -2
  114. data/src/core/lib/security/credentials/local/local_credentials.cc +1 -1
  115. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +1 -2
  116. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +2 -0
  117. data/src/core/lib/security/security_connector/{alts_security_connector.cc → alts/alts_security_connector.cc} +10 -9
  118. data/src/core/lib/security/security_connector/{alts_security_connector.h → alts/alts_security_connector.h} +3 -3
  119. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +310 -0
  120. data/src/core/lib/security/security_connector/fake/fake_security_connector.h +42 -0
  121. data/src/core/lib/security/security_connector/{local_security_connector.cc → local/local_security_connector.cc} +4 -3
  122. data/src/core/lib/security/security_connector/{local_security_connector.h → local/local_security_connector.h} +3 -3
  123. data/src/core/lib/security/security_connector/security_connector.cc +4 -1039
  124. data/src/core/lib/security/security_connector/security_connector.h +6 -114
  125. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +474 -0
  126. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +77 -0
  127. data/src/core/lib/security/security_connector/ssl_utils.cc +345 -0
  128. data/src/core/lib/security/security_connector/ssl_utils.h +93 -0
  129. data/src/core/lib/security/transport/client_auth_filter.cc +28 -17
  130. data/src/core/lib/security/transport/secure_endpoint.cc +51 -41
  131. data/src/core/lib/security/transport/security_handshaker.cc +6 -7
  132. data/src/core/lib/security/transport/server_auth_filter.cc +39 -31
  133. data/src/core/lib/surface/call.cc +100 -80
  134. data/src/core/lib/surface/call.h +4 -0
  135. data/src/core/lib/surface/channel.cc +27 -13
  136. data/src/core/lib/surface/channel.h +4 -3
  137. data/src/core/lib/surface/completion_queue.cc +8 -1
  138. data/src/core/lib/surface/init.cc +1 -0
  139. data/src/core/lib/surface/server.cc +111 -46
  140. data/src/core/lib/surface/server.h +16 -2
  141. data/src/core/lib/surface/version.cc +2 -2
  142. data/src/core/lib/transport/error_utils.cc +4 -2
  143. data/src/core/lib/transport/metadata.cc +3 -2
  144. data/src/core/lib/transport/metadata.h +3 -2
  145. data/src/core/lib/transport/metadata_batch.cc +1 -0
  146. data/src/core/lib/transport/metadata_batch.h +4 -2
  147. data/src/core/lib/transport/static_metadata.cc +225 -221
  148. data/src/core/lib/transport/static_metadata.h +74 -71
  149. data/src/core/lib/transport/transport.h +44 -26
  150. data/src/core/{ext/filters/client_channel → lib/uri}/uri_parser.cc +1 -1
  151. data/src/core/{ext/filters/client_channel → lib/uri}/uri_parser.h +3 -3
  152. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -4
  153. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +356 -77
  154. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +46 -36
  155. data/src/core/tsi/alts/handshaker/alts_shared_resource.cc +83 -0
  156. data/src/core/tsi/alts/handshaker/alts_shared_resource.h +73 -0
  157. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +122 -175
  158. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +33 -22
  159. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +38 -10
  160. data/src/core/tsi/transport_security.cc +18 -1
  161. data/src/core/tsi/transport_security.h +2 -1
  162. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +4 -2
  163. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +6 -3
  164. data/src/ruby/lib/grpc/version.rb +1 -1
  165. data/src/ruby/spec/pb/codegen/grpc/testing/package_options.proto +28 -0
  166. data/src/ruby/spec/pb/codegen/package_option_spec.rb +2 -3
  167. metadata +58 -40
  168. data/src/core/ext/filters/client_channel/method_params.cc +0 -178
  169. data/src/core/ext/filters/client_channel/method_params.h +0 -78
  170. data/src/core/tsi/alts/handshaker/alts_tsi_event.cc +0 -75
  171. data/src/core/tsi/alts/handshaker/alts_tsi_event.h +0 -93
  172. data/src/core/tsi/alts_transport_security.cc +0 -65
  173. data/src/core/tsi/alts_transport_security.h +0 -47
@@ -0,0 +1,36 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_H
20
+ #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ /** Channel arg indicating if a target corresponding to the address is grpclb
25
+ * loadbalancer. The type of this arg is an integer and the value is treated as
26
+ * a bool. */
27
+ #define GRPC_ARG_ADDRESS_IS_XDS_LOAD_BALANCER \
28
+ "grpc.address_is_xds_load_balancer"
29
+ /** Channel arg indicating if a target corresponding to the address is a backend
30
+ * received from a balancer. The type of this arg is an integer and the value is
31
+ * treated as a bool. */
32
+ #define GRPC_ARG_ADDRESS_IS_BACKEND_FROM_XDS_LOAD_BALANCER \
33
+ "grpc.address_is_backend_from_xds_load_balancer"
34
+
35
+ #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_H \
36
+ */
@@ -0,0 +1,36 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CHANNEL_H
20
+ #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CHANNEL_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include "src/core/ext/filters/client_channel/lb_policy_factory.h"
25
+
26
+ /// Makes any necessary modifications to \a args for use in the xds
27
+ /// balancer channel.
28
+ ///
29
+ /// Takes ownership of \a args.
30
+ ///
31
+ /// Caller takes ownership of the returned args.
32
+ grpc_channel_args* grpc_lb_policy_xds_modify_lb_channel_args(
33
+ grpc_channel_args* args);
34
+
35
+ #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CHANNEL_H \
36
+ */
@@ -0,0 +1,107 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #include <grpc/support/port_platform.h>
20
+
21
+ #include "src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h"
22
+
23
+ #include <grpc/support/alloc.h>
24
+ #include <grpc/support/string_util.h>
25
+ #include <string.h>
26
+
27
+ #include "src/core/ext/filters/client_channel/client_channel.h"
28
+ #include "src/core/lib/channel/channel_args.h"
29
+ #include "src/core/lib/gpr/string.h"
30
+ #include "src/core/lib/iomgr/sockaddr_utils.h"
31
+ #include "src/core/lib/security/credentials/credentials.h"
32
+ #include "src/core/lib/security/transport/target_authority_table.h"
33
+ #include "src/core/lib/slice/slice_internal.h"
34
+
35
+ namespace grpc_core {
36
+ namespace {
37
+
38
+ int BalancerNameCmp(const grpc_core::UniquePtr<char>& a,
39
+ const grpc_core::UniquePtr<char>& b) {
40
+ return strcmp(a.get(), b.get());
41
+ }
42
+
43
+ RefCountedPtr<TargetAuthorityTable> CreateTargetAuthorityTable(
44
+ grpc_lb_addresses* addresses) {
45
+ TargetAuthorityTable::Entry* target_authority_entries =
46
+ static_cast<TargetAuthorityTable::Entry*>(gpr_zalloc(
47
+ sizeof(*target_authority_entries) * addresses->num_addresses));
48
+ for (size_t i = 0; i < addresses->num_addresses; ++i) {
49
+ char* addr_str;
50
+ GPR_ASSERT(grpc_sockaddr_to_string(
51
+ &addr_str, &addresses->addresses[i].address, true) > 0);
52
+ target_authority_entries[i].key = grpc_slice_from_copied_string(addr_str);
53
+ target_authority_entries[i].value.reset(
54
+ gpr_strdup(addresses->addresses[i].balancer_name));
55
+ gpr_free(addr_str);
56
+ }
57
+ RefCountedPtr<TargetAuthorityTable> target_authority_table =
58
+ TargetAuthorityTable::Create(addresses->num_addresses,
59
+ target_authority_entries, BalancerNameCmp);
60
+ gpr_free(target_authority_entries);
61
+ return target_authority_table;
62
+ }
63
+
64
+ } // namespace
65
+ } // namespace grpc_core
66
+
67
+ grpc_channel_args* grpc_lb_policy_xds_modify_lb_channel_args(
68
+ grpc_channel_args* args) {
69
+ const char* args_to_remove[1];
70
+ size_t num_args_to_remove = 0;
71
+ grpc_arg args_to_add[2];
72
+ size_t num_args_to_add = 0;
73
+ // Add arg for targets info table.
74
+ const grpc_arg* arg = grpc_channel_args_find(args, GRPC_ARG_LB_ADDRESSES);
75
+ GPR_ASSERT(arg != nullptr);
76
+ GPR_ASSERT(arg->type == GRPC_ARG_POINTER);
77
+ grpc_lb_addresses* addresses =
78
+ static_cast<grpc_lb_addresses*>(arg->value.pointer.p);
79
+ grpc_core::RefCountedPtr<grpc_core::TargetAuthorityTable>
80
+ target_authority_table = grpc_core::CreateTargetAuthorityTable(addresses);
81
+ args_to_add[num_args_to_add++] =
82
+ grpc_core::CreateTargetAuthorityTableChannelArg(
83
+ target_authority_table.get());
84
+ // Substitute the channel credentials with a version without call
85
+ // credentials: the load balancer is not necessarily trusted to handle
86
+ // bearer token credentials.
87
+ grpc_channel_credentials* channel_credentials =
88
+ grpc_channel_credentials_find_in_args(args);
89
+ grpc_channel_credentials* creds_sans_call_creds = nullptr;
90
+ if (channel_credentials != nullptr) {
91
+ creds_sans_call_creds =
92
+ grpc_channel_credentials_duplicate_without_call_credentials(
93
+ channel_credentials);
94
+ GPR_ASSERT(creds_sans_call_creds != nullptr);
95
+ args_to_remove[num_args_to_remove++] = GRPC_ARG_CHANNEL_CREDENTIALS;
96
+ args_to_add[num_args_to_add++] =
97
+ grpc_channel_credentials_to_arg(creds_sans_call_creds);
98
+ }
99
+ grpc_channel_args* result = grpc_channel_args_copy_and_add_and_remove(
100
+ args, args_to_remove, num_args_to_remove, args_to_add, num_args_to_add);
101
+ // Clean up.
102
+ grpc_channel_args_destroy(args);
103
+ if (creds_sans_call_creds != nullptr) {
104
+ grpc_channel_credentials_unref(creds_sans_call_creds);
105
+ }
106
+ return result;
107
+ }
@@ -0,0 +1,85 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #include <grpc/support/port_platform.h>
20
+
21
+ #include "src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h"
22
+
23
+ #include <grpc/support/atm.h>
24
+ #include <grpc/support/string_util.h>
25
+ #include <string.h>
26
+
27
+ namespace grpc_core {
28
+
29
+ void XdsLbClientStats::AddCallStarted() {
30
+ gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
31
+ }
32
+
33
+ void XdsLbClientStats::AddCallFinished(bool finished_with_client_failed_to_send,
34
+ bool finished_known_received) {
35
+ gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
36
+ if (finished_with_client_failed_to_send) {
37
+ gpr_atm_full_fetch_add(&num_calls_finished_with_client_failed_to_send_,
38
+ (gpr_atm)1);
39
+ }
40
+ if (finished_known_received) {
41
+ gpr_atm_full_fetch_add(&num_calls_finished_known_received_, (gpr_atm)1);
42
+ }
43
+ }
44
+
45
+ void XdsLbClientStats::AddCallDroppedLocked(char* token) {
46
+ // Increment num_calls_started and num_calls_finished.
47
+ gpr_atm_full_fetch_add(&num_calls_started_, (gpr_atm)1);
48
+ gpr_atm_full_fetch_add(&num_calls_finished_, (gpr_atm)1);
49
+ // Record the drop.
50
+ if (drop_token_counts_ == nullptr) {
51
+ drop_token_counts_.reset(New<DroppedCallCounts>());
52
+ }
53
+ for (size_t i = 0; i < drop_token_counts_->size(); ++i) {
54
+ if (strcmp((*drop_token_counts_)[i].token.get(), token) == 0) {
55
+ ++(*drop_token_counts_)[i].count;
56
+ return;
57
+ }
58
+ }
59
+ // Not found, so add a new entry.
60
+ drop_token_counts_->emplace_back(UniquePtr<char>(gpr_strdup(token)), 1);
61
+ }
62
+
63
+ namespace {
64
+
65
+ void AtomicGetAndResetCounter(int64_t* value, gpr_atm* counter) {
66
+ *value = static_cast<int64_t>(gpr_atm_full_xchg(counter, (gpr_atm)0));
67
+ }
68
+
69
+ } // namespace
70
+
71
+ void XdsLbClientStats::GetLocked(
72
+ int64_t* num_calls_started, int64_t* num_calls_finished,
73
+ int64_t* num_calls_finished_with_client_failed_to_send,
74
+ int64_t* num_calls_finished_known_received,
75
+ UniquePtr<DroppedCallCounts>* drop_token_counts) {
76
+ AtomicGetAndResetCounter(num_calls_started, &num_calls_started_);
77
+ AtomicGetAndResetCounter(num_calls_finished, &num_calls_finished_);
78
+ AtomicGetAndResetCounter(num_calls_finished_with_client_failed_to_send,
79
+ &num_calls_finished_with_client_failed_to_send_);
80
+ AtomicGetAndResetCounter(num_calls_finished_known_received,
81
+ &num_calls_finished_known_received_);
82
+ *drop_token_counts = std::move(drop_token_counts_);
83
+ }
84
+
85
+ } // namespace grpc_core
@@ -0,0 +1,72 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CLIENT_STATS_H
20
+ #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CLIENT_STATS_H
21
+
22
+ #include <grpc/support/port_platform.h>
23
+
24
+ #include <grpc/support/atm.h>
25
+
26
+ #include "src/core/lib/gprpp/inlined_vector.h"
27
+ #include "src/core/lib/gprpp/memory.h"
28
+ #include "src/core/lib/gprpp/ref_counted.h"
29
+
30
+ namespace grpc_core {
31
+
32
+ class XdsLbClientStats : public RefCounted<XdsLbClientStats> {
33
+ public:
34
+ struct DropTokenCount {
35
+ UniquePtr<char> token;
36
+ int64_t count;
37
+
38
+ DropTokenCount(UniquePtr<char> token, int64_t count)
39
+ : token(std::move(token)), count(count) {}
40
+ };
41
+
42
+ typedef InlinedVector<DropTokenCount, 10> DroppedCallCounts;
43
+
44
+ XdsLbClientStats() {}
45
+
46
+ void AddCallStarted();
47
+ void AddCallFinished(bool finished_with_client_failed_to_send,
48
+ bool finished_known_received);
49
+
50
+ // This method is not thread-safe; caller must synchronize.
51
+ void AddCallDroppedLocked(char* token);
52
+
53
+ // This method is not thread-safe; caller must synchronize.
54
+ void GetLocked(int64_t* num_calls_started, int64_t* num_calls_finished,
55
+ int64_t* num_calls_finished_with_client_failed_to_send,
56
+ int64_t* num_calls_finished_known_received,
57
+ UniquePtr<DroppedCallCounts>* drop_token_counts);
58
+
59
+ private:
60
+ // This field must only be accessed via *_locked() methods.
61
+ UniquePtr<DroppedCallCounts> drop_token_counts_;
62
+ // These fields may be accessed from multiple threads at a time.
63
+ gpr_atm num_calls_started_ = 0;
64
+ gpr_atm num_calls_finished_ = 0;
65
+ gpr_atm num_calls_finished_with_client_failed_to_send_ = 0;
66
+ gpr_atm num_calls_finished_known_received_ = 0;
67
+ };
68
+
69
+ } // namespace grpc_core
70
+
71
+ #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_XDS_XDS_CLIENT_STATS_H \
72
+ */
@@ -0,0 +1,307 @@
1
+ /*
2
+ *
3
+ * Copyright 2018 gRPC authors.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ *
17
+ */
18
+
19
+ #include <grpc/support/port_platform.h>
20
+
21
+ #include "pb_decode.h"
22
+ #include "pb_encode.h"
23
+ #include "src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h"
24
+
25
+ #include <grpc/support/alloc.h>
26
+
27
+ /* invoked once for every Server in ServerList */
28
+ static bool count_serverlist(pb_istream_t* stream, const pb_field_t* field,
29
+ void** arg) {
30
+ xds_grpclb_serverlist* sl = static_cast<xds_grpclb_serverlist*>(*arg);
31
+ xds_grpclb_server server;
32
+ if (GPR_UNLIKELY(!pb_decode(stream, grpc_lb_v1_Server_fields, &server))) {
33
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream));
34
+ return false;
35
+ }
36
+ ++sl->num_servers;
37
+ return true;
38
+ }
39
+
40
+ typedef struct decode_serverlist_arg {
41
+ /* The decoding callback is invoked once per server in serverlist. Remember
42
+ * which index of the serverlist are we currently decoding */
43
+ size_t decoding_idx;
44
+ /* The decoded serverlist */
45
+ xds_grpclb_serverlist* serverlist;
46
+ } decode_serverlist_arg;
47
+
48
+ /* invoked once for every Server in ServerList */
49
+ static bool decode_serverlist(pb_istream_t* stream, const pb_field_t* field,
50
+ void** arg) {
51
+ decode_serverlist_arg* dec_arg = static_cast<decode_serverlist_arg*>(*arg);
52
+ GPR_ASSERT(dec_arg->serverlist->num_servers >= dec_arg->decoding_idx);
53
+ xds_grpclb_server* server =
54
+ static_cast<xds_grpclb_server*>(gpr_zalloc(sizeof(xds_grpclb_server)));
55
+ if (GPR_UNLIKELY(!pb_decode(stream, grpc_lb_v1_Server_fields, server))) {
56
+ gpr_free(server);
57
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream));
58
+ return false;
59
+ }
60
+ dec_arg->serverlist->servers[dec_arg->decoding_idx++] = server;
61
+ return true;
62
+ }
63
+
64
+ xds_grpclb_request* xds_grpclb_request_create(const char* lb_service_name) {
65
+ xds_grpclb_request* req =
66
+ static_cast<xds_grpclb_request*>(gpr_malloc(sizeof(xds_grpclb_request)));
67
+ req->has_client_stats = false;
68
+ req->has_initial_request = true;
69
+ req->initial_request.has_name = true;
70
+ strncpy(req->initial_request.name, lb_service_name,
71
+ XDS_SERVICE_NAME_MAX_LENGTH);
72
+ return req;
73
+ }
74
+
75
+ static void populate_timestamp(gpr_timespec timestamp,
76
+ xds_grpclb_timestamp* timestamp_pb) {
77
+ timestamp_pb->has_seconds = true;
78
+ timestamp_pb->seconds = timestamp.tv_sec;
79
+ timestamp_pb->has_nanos = true;
80
+ timestamp_pb->nanos = timestamp.tv_nsec;
81
+ }
82
+
83
+ static bool encode_string(pb_ostream_t* stream, const pb_field_t* field,
84
+ void* const* arg) {
85
+ char* str = static_cast<char*>(*arg);
86
+ if (!pb_encode_tag_for_field(stream, field)) return false;
87
+ return pb_encode_string(stream, reinterpret_cast<uint8_t*>(str), strlen(str));
88
+ }
89
+
90
+ static bool encode_drops(pb_ostream_t* stream, const pb_field_t* field,
91
+ void* const* arg) {
92
+ grpc_core::XdsLbClientStats::DroppedCallCounts* drop_entries =
93
+ static_cast<grpc_core::XdsLbClientStats::DroppedCallCounts*>(*arg);
94
+ if (drop_entries == nullptr) return true;
95
+ for (size_t i = 0; i < drop_entries->size(); ++i) {
96
+ if (!pb_encode_tag_for_field(stream, field)) return false;
97
+ grpc_lb_v1_ClientStatsPerToken drop_message;
98
+ drop_message.load_balance_token.funcs.encode = encode_string;
99
+ drop_message.load_balance_token.arg = (*drop_entries)[i].token.get();
100
+ drop_message.has_num_calls = true;
101
+ drop_message.num_calls = (*drop_entries)[i].count;
102
+ if (!pb_encode_submessage(stream, grpc_lb_v1_ClientStatsPerToken_fields,
103
+ &drop_message)) {
104
+ return false;
105
+ }
106
+ }
107
+ return true;
108
+ }
109
+
110
+ xds_grpclb_request* xds_grpclb_load_report_request_create_locked(
111
+ grpc_core::XdsLbClientStats* client_stats) {
112
+ xds_grpclb_request* req =
113
+ static_cast<xds_grpclb_request*>(gpr_zalloc(sizeof(xds_grpclb_request)));
114
+ req->has_client_stats = true;
115
+ req->client_stats.has_timestamp = true;
116
+ populate_timestamp(gpr_now(GPR_CLOCK_REALTIME), &req->client_stats.timestamp);
117
+ req->client_stats.has_num_calls_started = true;
118
+ req->client_stats.has_num_calls_finished = true;
119
+ req->client_stats.has_num_calls_finished_with_client_failed_to_send = true;
120
+ req->client_stats.has_num_calls_finished_with_client_failed_to_send = true;
121
+ req->client_stats.has_num_calls_finished_known_received = true;
122
+ req->client_stats.calls_finished_with_drop.funcs.encode = encode_drops;
123
+ grpc_core::UniquePtr<grpc_core::XdsLbClientStats::DroppedCallCounts>
124
+ drop_counts;
125
+ client_stats->GetLocked(
126
+ &req->client_stats.num_calls_started,
127
+ &req->client_stats.num_calls_finished,
128
+ &req->client_stats.num_calls_finished_with_client_failed_to_send,
129
+ &req->client_stats.num_calls_finished_known_received, &drop_counts);
130
+ // Will be deleted in xds_grpclb_request_destroy().
131
+ req->client_stats.calls_finished_with_drop.arg = drop_counts.release();
132
+ return req;
133
+ }
134
+
135
+ grpc_slice xds_grpclb_request_encode(const xds_grpclb_request* request) {
136
+ size_t encoded_length;
137
+ pb_ostream_t sizestream;
138
+ pb_ostream_t outputstream;
139
+ grpc_slice slice;
140
+ memset(&sizestream, 0, sizeof(pb_ostream_t));
141
+ pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request);
142
+ encoded_length = sizestream.bytes_written;
143
+
144
+ slice = GRPC_SLICE_MALLOC(encoded_length);
145
+ outputstream =
146
+ pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length);
147
+ GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields,
148
+ request) != 0);
149
+ return slice;
150
+ }
151
+
152
+ void xds_grpclb_request_destroy(xds_grpclb_request* request) {
153
+ if (request->has_client_stats) {
154
+ grpc_core::XdsLbClientStats::DroppedCallCounts* drop_entries =
155
+ static_cast<grpc_core::XdsLbClientStats::DroppedCallCounts*>(
156
+ request->client_stats.calls_finished_with_drop.arg);
157
+ grpc_core::Delete(drop_entries);
158
+ }
159
+ gpr_free(request);
160
+ }
161
+
162
+ typedef grpc_lb_v1_LoadBalanceResponse xds_grpclb_response;
163
+ xds_grpclb_initial_response* xds_grpclb_initial_response_parse(
164
+ grpc_slice encoded_xds_grpclb_response) {
165
+ pb_istream_t stream =
166
+ pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_xds_grpclb_response),
167
+ GRPC_SLICE_LENGTH(encoded_xds_grpclb_response));
168
+ xds_grpclb_response res;
169
+ memset(&res, 0, sizeof(xds_grpclb_response));
170
+ if (GPR_UNLIKELY(
171
+ !pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res))) {
172
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
173
+ return nullptr;
174
+ }
175
+
176
+ if (!res.has_initial_response) return nullptr;
177
+
178
+ xds_grpclb_initial_response* initial_res =
179
+ static_cast<xds_grpclb_initial_response*>(
180
+ gpr_malloc(sizeof(xds_grpclb_initial_response)));
181
+ memcpy(initial_res, &res.initial_response,
182
+ sizeof(xds_grpclb_initial_response));
183
+
184
+ return initial_res;
185
+ }
186
+
187
+ xds_grpclb_serverlist* xds_grpclb_response_parse_serverlist(
188
+ grpc_slice encoded_xds_grpclb_response) {
189
+ pb_istream_t stream =
190
+ pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_xds_grpclb_response),
191
+ GRPC_SLICE_LENGTH(encoded_xds_grpclb_response));
192
+ pb_istream_t stream_at_start = stream;
193
+ xds_grpclb_serverlist* sl = static_cast<xds_grpclb_serverlist*>(
194
+ gpr_zalloc(sizeof(xds_grpclb_serverlist)));
195
+ xds_grpclb_response res;
196
+ memset(&res, 0, sizeof(xds_grpclb_response));
197
+ // First pass: count number of servers.
198
+ res.server_list.servers.funcs.decode = count_serverlist;
199
+ res.server_list.servers.arg = sl;
200
+ bool status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res);
201
+ if (GPR_UNLIKELY(!status)) {
202
+ gpr_free(sl);
203
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
204
+ return nullptr;
205
+ }
206
+ // Second pass: populate servers.
207
+ if (sl->num_servers > 0) {
208
+ sl->servers = static_cast<xds_grpclb_server**>(
209
+ gpr_zalloc(sizeof(xds_grpclb_server*) * sl->num_servers));
210
+ decode_serverlist_arg decode_arg;
211
+ memset(&decode_arg, 0, sizeof(decode_arg));
212
+ decode_arg.serverlist = sl;
213
+ res.server_list.servers.funcs.decode = decode_serverlist;
214
+ res.server_list.servers.arg = &decode_arg;
215
+ status = pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields,
216
+ &res);
217
+ if (GPR_UNLIKELY(!status)) {
218
+ xds_grpclb_destroy_serverlist(sl);
219
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
220
+ return nullptr;
221
+ }
222
+ }
223
+ return sl;
224
+ }
225
+
226
+ void xds_grpclb_destroy_serverlist(xds_grpclb_serverlist* serverlist) {
227
+ if (serverlist == nullptr) {
228
+ return;
229
+ }
230
+ for (size_t i = 0; i < serverlist->num_servers; i++) {
231
+ gpr_free(serverlist->servers[i]);
232
+ }
233
+ gpr_free(serverlist->servers);
234
+ gpr_free(serverlist);
235
+ }
236
+
237
+ xds_grpclb_serverlist* xds_grpclb_serverlist_copy(
238
+ const xds_grpclb_serverlist* sl) {
239
+ xds_grpclb_serverlist* copy = static_cast<xds_grpclb_serverlist*>(
240
+ gpr_zalloc(sizeof(xds_grpclb_serverlist)));
241
+ copy->num_servers = sl->num_servers;
242
+ copy->servers = static_cast<xds_grpclb_server**>(
243
+ gpr_malloc(sizeof(xds_grpclb_server*) * sl->num_servers));
244
+ for (size_t i = 0; i < sl->num_servers; i++) {
245
+ copy->servers[i] =
246
+ static_cast<xds_grpclb_server*>(gpr_malloc(sizeof(xds_grpclb_server)));
247
+ memcpy(copy->servers[i], sl->servers[i], sizeof(xds_grpclb_server));
248
+ }
249
+ return copy;
250
+ }
251
+
252
+ bool xds_grpclb_serverlist_equals(const xds_grpclb_serverlist* lhs,
253
+ const xds_grpclb_serverlist* rhs) {
254
+ if (lhs == nullptr || rhs == nullptr) {
255
+ return false;
256
+ }
257
+ if (lhs->num_servers != rhs->num_servers) {
258
+ return false;
259
+ }
260
+ for (size_t i = 0; i < lhs->num_servers; i++) {
261
+ if (!xds_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) {
262
+ return false;
263
+ }
264
+ }
265
+ return true;
266
+ }
267
+
268
+ bool xds_grpclb_server_equals(const xds_grpclb_server* lhs,
269
+ const xds_grpclb_server* rhs) {
270
+ return memcmp(lhs, rhs, sizeof(xds_grpclb_server)) == 0;
271
+ }
272
+
273
+ int xds_grpclb_duration_compare(const xds_grpclb_duration* lhs,
274
+ const xds_grpclb_duration* rhs) {
275
+ GPR_ASSERT(lhs && rhs);
276
+ if (lhs->has_seconds && rhs->has_seconds) {
277
+ if (lhs->seconds < rhs->seconds) return -1;
278
+ if (lhs->seconds > rhs->seconds) return 1;
279
+ } else if (lhs->has_seconds) {
280
+ return 1;
281
+ } else if (rhs->has_seconds) {
282
+ return -1;
283
+ }
284
+
285
+ GPR_ASSERT(lhs->seconds == rhs->seconds);
286
+ if (lhs->has_nanos && rhs->has_nanos) {
287
+ if (lhs->nanos < rhs->nanos) return -1;
288
+ if (lhs->nanos > rhs->nanos) return 1;
289
+ } else if (lhs->has_nanos) {
290
+ return 1;
291
+ } else if (rhs->has_nanos) {
292
+ return -1;
293
+ }
294
+
295
+ return 0;
296
+ }
297
+
298
+ grpc_millis xds_grpclb_duration_to_millis(xds_grpclb_duration* duration_pb) {
299
+ return static_cast<grpc_millis>(
300
+ (duration_pb->has_seconds ? duration_pb->seconds : 0) * GPR_MS_PER_SEC +
301
+ (duration_pb->has_nanos ? duration_pb->nanos : 0) / GPR_NS_PER_MS);
302
+ }
303
+
304
+ void xds_grpclb_initial_response_destroy(
305
+ xds_grpclb_initial_response* response) {
306
+ gpr_free(response);
307
+ }