channelz.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. *
  3. * Copyright 2015 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. #include <stdio.h>
  19. #include <string.h>
  20. #include <grpc/byte_buffer.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/time.h>
  25. #include "src/core/lib/channel/channelz_registry.h"
  26. #include "src/core/lib/gpr/string.h"
  27. #include "src/core/lib/surface/channel.h"
  28. #include "src/core/lib/surface/server.h"
  29. #include "test/core/end2end/cq_verifier.h"
  30. #include "test/core/end2end/end2end_tests.h"
  31. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  32. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  33. const char* test_name,
  34. grpc_channel_args* client_args,
  35. grpc_channel_args* server_args) {
  36. grpc_end2end_test_fixture f;
  37. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  38. f = config.create_fixture(client_args, server_args);
  39. config.init_server(&f, server_args);
  40. config.init_client(&f, client_args);
  41. return f;
  42. }
  43. static gpr_timespec n_seconds_from_now(int n) {
  44. return grpc_timeout_seconds_to_deadline(n);
  45. }
  46. static gpr_timespec five_seconds_from_now(void) {
  47. return n_seconds_from_now(5);
  48. }
  49. static void drain_cq(grpc_completion_queue* cq) {
  50. grpc_event ev;
  51. do {
  52. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  53. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  54. }
  55. static void shutdown_server(grpc_end2end_test_fixture* f) {
  56. if (!f->server) return;
  57. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  58. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  59. grpc_timeout_seconds_to_deadline(5),
  60. nullptr)
  61. .type == GRPC_OP_COMPLETE);
  62. grpc_server_destroy(f->server);
  63. f->server = nullptr;
  64. }
  65. static void shutdown_client(grpc_end2end_test_fixture* f) {
  66. if (!f->client) return;
  67. grpc_channel_destroy(f->client);
  68. f->client = nullptr;
  69. }
  70. static void end_test(grpc_end2end_test_fixture* f) {
  71. shutdown_server(f);
  72. shutdown_client(f);
  73. grpc_completion_queue_shutdown(f->cq);
  74. drain_cq(f->cq);
  75. grpc_completion_queue_destroy(f->cq);
  76. grpc_completion_queue_destroy(f->shutdown_cq);
  77. }
  78. static void run_one_request(grpc_end2end_test_config /*config*/,
  79. grpc_end2end_test_fixture f,
  80. bool request_is_success) {
  81. grpc_call* c;
  82. grpc_call* s;
  83. cq_verifier* cqv = cq_verifier_create(f.cq);
  84. grpc_op ops[6];
  85. grpc_op* op;
  86. grpc_metadata_array initial_metadata_recv;
  87. grpc_metadata_array trailing_metadata_recv;
  88. grpc_metadata_array request_metadata_recv;
  89. grpc_call_details call_details;
  90. grpc_status_code status;
  91. grpc_call_error error;
  92. grpc_slice details;
  93. int was_cancelled = 2;
  94. gpr_timespec deadline = five_seconds_from_now();
  95. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  96. grpc_slice_from_static_string("/foo"), nullptr,
  97. deadline, nullptr);
  98. GPR_ASSERT(c);
  99. grpc_metadata_array_init(&initial_metadata_recv);
  100. grpc_metadata_array_init(&trailing_metadata_recv);
  101. grpc_metadata_array_init(&request_metadata_recv);
  102. grpc_call_details_init(&call_details);
  103. memset(ops, 0, sizeof(ops));
  104. op = ops;
  105. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  106. op->data.send_initial_metadata.count = 0;
  107. op->flags = 0;
  108. op->reserved = nullptr;
  109. op++;
  110. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  111. op->flags = 0;
  112. op->reserved = nullptr;
  113. op++;
  114. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  115. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  116. op->flags = 0;
  117. op->reserved = nullptr;
  118. op++;
  119. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  120. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  121. op->data.recv_status_on_client.status = &status;
  122. op->data.recv_status_on_client.status_details = &details;
  123. op->data.recv_status_on_client.error_string = nullptr;
  124. op->flags = 0;
  125. op->reserved = nullptr;
  126. op++;
  127. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  128. nullptr);
  129. GPR_ASSERT(GRPC_CALL_OK == error);
  130. error =
  131. grpc_server_request_call(f.server, &s, &call_details,
  132. &request_metadata_recv, f.cq, f.cq, tag(101));
  133. GPR_ASSERT(GRPC_CALL_OK == error);
  134. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  135. cq_verify(cqv);
  136. memset(ops, 0, sizeof(ops));
  137. op = ops;
  138. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  139. op->data.send_initial_metadata.count = 0;
  140. op->flags = 0;
  141. op->reserved = nullptr;
  142. op++;
  143. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  144. op->data.send_status_from_server.trailing_metadata_count = 0;
  145. op->data.send_status_from_server.status =
  146. request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED;
  147. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  148. op->data.send_status_from_server.status_details = &status_details;
  149. op->flags = 0;
  150. op->reserved = nullptr;
  151. op++;
  152. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  153. op->data.recv_close_on_server.cancelled = &was_cancelled;
  154. op->flags = 0;
  155. op->reserved = nullptr;
  156. op++;
  157. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  158. nullptr);
  159. GPR_ASSERT(GRPC_CALL_OK == error);
  160. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  161. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  162. cq_verify(cqv);
  163. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  164. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  165. GPR_ASSERT(0 == call_details.flags);
  166. grpc_slice_unref(details);
  167. grpc_metadata_array_destroy(&initial_metadata_recv);
  168. grpc_metadata_array_destroy(&trailing_metadata_recv);
  169. grpc_metadata_array_destroy(&request_metadata_recv);
  170. grpc_call_details_destroy(&call_details);
  171. grpc_call_unref(c);
  172. grpc_call_unref(s);
  173. cq_verifier_destroy(cqv);
  174. }
  175. static void test_channelz(grpc_end2end_test_config config) {
  176. grpc_end2end_test_fixture f;
  177. grpc_arg arg[] = {
  178. grpc_channel_arg_integer_create(
  179. const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
  180. 0),
  181. grpc_channel_arg_integer_create(
  182. const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
  183. grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
  184. f = begin_test(config, "test_channelz", &args, &args);
  185. grpc_core::channelz::ChannelNode* channelz_channel =
  186. grpc_channel_get_channelz_node(f.client);
  187. GPR_ASSERT(channelz_channel != nullptr);
  188. grpc_core::channelz::ServerNode* channelz_server =
  189. grpc_core::Server::FromC(f.server)->channelz_node();
  190. GPR_ASSERT(channelz_server != nullptr);
  191. std::string json = channelz_channel->RenderJsonString();
  192. // nothing is present yet
  193. GPR_ASSERT(json.find("\"callsStarted\"") == json.npos);
  194. GPR_ASSERT(json.find("\"callsFailed\"") == json.npos);
  195. GPR_ASSERT(json.find("\"callsSucceeded\"") == json.npos);
  196. // one successful request
  197. run_one_request(config, f, true);
  198. json = channelz_channel->RenderJsonString();
  199. GPR_ASSERT(json.find("\"callsStarted\":\"1\"") != json.npos);
  200. GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
  201. // one failed request
  202. run_one_request(config, f, false);
  203. json = channelz_channel->RenderJsonString();
  204. GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
  205. GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
  206. GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
  207. // channel tracing is not enabled, so these should not be preset.
  208. GPR_ASSERT(json.find("\"trace\"") == json.npos);
  209. GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
  210. GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
  211. json = channelz_server->RenderJsonString();
  212. GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
  213. GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
  214. GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
  215. // channel tracing is not enabled, so these should not be preset.
  216. GPR_ASSERT(json.find("\"trace\"") == json.npos);
  217. GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
  218. GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
  219. json = channelz_server->RenderServerSockets(0, 100);
  220. GPR_ASSERT(json.find("\"end\":true") != json.npos);
  221. end_test(&f);
  222. config.tear_down_data(&f);
  223. }
  224. static void test_channelz_with_channel_trace(grpc_end2end_test_config config) {
  225. grpc_end2end_test_fixture f;
  226. grpc_arg arg[] = {
  227. grpc_channel_arg_integer_create(
  228. const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
  229. 1024 * 1024),
  230. grpc_channel_arg_integer_create(
  231. const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
  232. grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
  233. f = begin_test(config, "test_channelz_with_channel_trace", &args, &args);
  234. grpc_core::channelz::ChannelNode* channelz_channel =
  235. grpc_channel_get_channelz_node(f.client);
  236. GPR_ASSERT(channelz_channel != nullptr);
  237. grpc_core::channelz::ServerNode* channelz_server =
  238. grpc_core::Server::FromC(f.server)->channelz_node();
  239. GPR_ASSERT(channelz_server != nullptr);
  240. run_one_request(config, f, true);
  241. std::string json = channelz_channel->RenderJsonString();
  242. GPR_ASSERT(json.find("\"trace\"") != json.npos);
  243. GPR_ASSERT(json.find("\"description\":\"Channel created\"") != json.npos);
  244. GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
  245. json = channelz_server->RenderJsonString();
  246. GPR_ASSERT(json.find("\"trace\"") != json.npos);
  247. GPR_ASSERT(json.find("\"description\":\"Server created\"") != json.npos);
  248. GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
  249. end_test(&f);
  250. config.tear_down_data(&f);
  251. }
  252. static void test_channelz_disabled(grpc_end2end_test_config config) {
  253. grpc_end2end_test_fixture f;
  254. grpc_arg arg[] = {
  255. grpc_channel_arg_integer_create(
  256. const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
  257. 0),
  258. grpc_channel_arg_integer_create(
  259. const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), false)};
  260. grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
  261. f = begin_test(config, "test_channelz_disabled", &args, &args);
  262. grpc_core::channelz::ChannelNode* channelz_channel =
  263. grpc_channel_get_channelz_node(f.client);
  264. GPR_ASSERT(channelz_channel == nullptr);
  265. // one successful request
  266. run_one_request(config, f, true);
  267. GPR_ASSERT(channelz_channel == nullptr);
  268. end_test(&f);
  269. config.tear_down_data(&f);
  270. }
  271. void channelz(grpc_end2end_test_config config) {
  272. test_channelz(config);
  273. test_channelz_with_channel_trace(config);
  274. test_channelz_disabled(config);
  275. }
  276. void channelz_pre_init(void) {}