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