| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 <signal.h> |
| 20 |
#include <stdint.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <time.h> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <string> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/base/attributes.h" |
| 29 |
#include "absl/flags/flag.h" |
| 30 |
#include "absl/flags/parse.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
|
| 33 |
#include <grpc/byte_buffer.h> |
| 34 |
#include <grpc/grpc.h> |
| 35 |
#include <grpc/grpc_security.h> |
| 36 |
#include <grpc/slice.h> |
| 37 |
#include <grpc/status.h> |
| 38 |
|
| 39 |
#include "test/core/memory_usage/memstats.h" |
| 40 |
#ifndef _WIN32 |
| 41 |
/* This is for _exit() below, which is temporary. */ |
| 42 |
#include <unistd.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <grpc/support/alloc.h> |
| 46 |
#include <grpc/support/log.h> |
| 47 |
#include <grpc/support/time.h> |
| 48 |
|
| 49 |
#include "src/core/lib/channel/channel_args.h" |
| 50 |
#include "src/core/lib/gprpp/host_port.h" |
| 51 |
#include "test/core/end2end/data/ssl_test_data.h" |
| 52 |
#include "test/core/util/port.h" |
| 53 |
#include "test/core/util/test_config.h" |
| 54 |
|
| 55 |
static grpc_completion_queue* cq; |
| 56 |
static grpc_server* server; |
| 57 |
static grpc_op metadata_ops[2]; |
| 58 |
static grpc_op snapshot_ops[5]; |
| 59 |
static grpc_op status_op; |
| 60 |
static int got_sigint = 0; |
| 61 |
static grpc_byte_buffer* payload_buffer = nullptr; |
| 62 |
static grpc_byte_buffer* terminal_buffer = nullptr; |
| 63 |
static int was_cancelled = 2; |
| 64 |
|
| 65 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 66 |
|
| 67 |
typedef enum { |
| 68 |
FLING_SERVER_NEW_REQUEST = 1, |
| 69 |
FLING_SERVER_SEND_INIT_METADATA, |
| 70 |
FLING_SERVER_WAIT_FOR_DESTROY, |
| 71 |
FLING_SERVER_SEND_STATUS_FLING_CALL, |
| 72 |
FLING_SERVER_SEND_STATUS_SNAPSHOT, |
| 73 |
FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL |
| 74 |
} fling_server_tags; |
| 75 |
|
| 76 |
typedef struct { |
| 77 |
fling_server_tags state; |
| 78 |
grpc_call* call; |
| 79 |
grpc_call_details call_details; |
| 80 |
grpc_metadata_array request_metadata_recv; |
| 81 |
grpc_metadata_array initial_metadata_send; |
| 82 |
} fling_call; |
| 83 |
|
| 84 |
// hold up to 100000 calls and 6 snaphost calls |
| 85 |
static fling_call calls[1000006]; |
| 86 |
|
| 87 |
static void request_call_unary(int call_idx) { |
| 88 |
if (call_idx == static_cast<int>(sizeof(calls) / sizeof(fling_call))) { |
| 89 |
gpr_log(GPR_INFO, "Used all call slots (10000) on server. Server exit."); |
| 90 |
_exit(0); |
| 91 |
} |
| 92 |
grpc_metadata_array_init(&calls[call_idx].request_metadata_recv); |
| 93 |
grpc_server_request_call( |
| 94 |
server, &calls[call_idx].call, &calls[call_idx].call_details, |
| 95 |
&calls[call_idx].request_metadata_recv, cq, cq, &calls[call_idx]); |
| 96 |
} |
| 97 |
|
| 98 |
static void send_initial_metadata_unary(void* tag) { |
| 99 |
grpc_metadata_array_init( |
| 100 |
&(*static_cast<fling_call*>(tag)).initial_metadata_send); |
| 101 |
metadata_ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
| 102 |
metadata_ops[0].data.send_initial_metadata.count = 0; |
| 103 |
|
| 104 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch((*(fling_call*)tag).call, |
| 105 |
metadata_ops, 1, tag, |
| 106 |
nullptr)); |
| 107 |
} |
| 108 |
|
| 109 |
static void send_status(void* tag) { |
| 110 |
status_op.op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 111 |
status_op.data.send_status_from_server.status = GRPC_STATUS_OK; |
| 112 |
status_op.data.send_status_from_server.trailing_metadata_count = 0; |
| 113 |
grpc_slice details = grpc_slice_from_static_string(""); |
| 114 |
status_op.data.send_status_from_server.status_details = &details; |
| 115 |
|
| 116 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch((*(fling_call*)tag).call, |
| 117 |
&status_op, 1, tag, |
| 118 |
nullptr)); |
| 119 |
} |
| 120 |
|
| 121 |
static void send_snapshot(void* tag, MemStats* snapshot) { |
| 122 |
grpc_op* op; |
| 123 |
|
| 124 |
grpc_slice snapshot_slice = |
| 125 |
grpc_slice_new(snapshot, sizeof(*snapshot), gpr_free); |
| 126 |
payload_buffer = grpc_raw_byte_buffer_create(&snapshot_slice, 1); |
| 127 |
grpc_metadata_array_init( |
| 128 |
&(*static_cast<fling_call*>(tag)).initial_metadata_send); |
| 129 |
|
| 130 |
op = snapshot_ops; |
| 131 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 132 |
op->data.send_initial_metadata.count = 0; |
| 133 |
op++; |
| 134 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 135 |
op->data.recv_message.recv_message = &terminal_buffer; |
| 136 |
op++; |
| 137 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 138 |
if (payload_buffer == nullptr) { |
| 139 |
gpr_log(GPR_INFO, "NULL payload buffer !!!"); |
| 140 |
} |
| 141 |
op->data.send_message.send_message = payload_buffer; |
| 142 |
op++; |
| 143 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 144 |
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
| 145 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 146 |
grpc_slice details = grpc_slice_from_static_string(""); |
| 147 |
op->data.send_status_from_server.status_details = &details; |
| 148 |
op++; |
| 149 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 150 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 151 |
op++; |
| 152 |
|
| 153 |
GPR_ASSERT(GRPC_CALL_OK == |
| 154 |
grpc_call_start_batch((*(fling_call*)tag).call, snapshot_ops, |
| 155 |
(size_t)(op - snapshot_ops), tag, nullptr)); |
| 156 |
} |
| 157 |
/* We have some sort of deadlock, so let's not exit gracefully for now. |
| 158 |
When that is resolved, please remove the #include <unistd.h> above. */ |
| 159 |
static void sigint_handler(int /*x*/) { _exit(0); } |
| 160 |
|
| 161 |
ABSL_FLAG(std::string, bind, "", "Bind host:port"); |
| 162 |
ABSL_FLAG(bool, secure, false, "Use security"); |
| 163 |
ABSL_FLAG(bool, minstack, false, "Use minimal stack"); |
| 164 |
|
| 165 |
int main(int argc, char** argv) { |
| 166 |
absl::ParseCommandLine(argc, argv); |
| 167 |
|
| 168 |
grpc_event ev; |
| 169 |
grpc_completion_queue* shutdown_cq; |
| 170 |
int shutdown_started = 0; |
| 171 |
int shutdown_finished = 0; |
| 172 |
|
| 173 |
char* fake_argv[1]; |
| 174 |
|
| 175 |
GPR_ASSERT(argc >= 1); |
| 176 |
fake_argv[0] = argv[0]; |
| 177 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 178 |
|
| 179 |
grpc_init(); |
| 180 |
srand(static_cast<unsigned>(clock())); |
| 181 |
|
| 182 |
std::string addr = absl::GetFlag(FLAGS_bind); |
| 183 |
if (addr.empty()) { |
| 184 |
addr = grpc_core::JoinHostPort("::", grpc_pick_unused_port_or_die()); |
| 185 |
} |
| 186 |
gpr_log(GPR_INFO, "creating server on: %s", addr.c_str()); |
| 187 |
|
| 188 |
cq = grpc_completion_queue_create_for_next(nullptr); |
| 189 |
|
| 190 |
std::vector<grpc_arg> args_vec; |
| 191 |
if (absl::GetFlag(FLAGS_minstack)) { |
| 192 |
args_vec.push_back(grpc_channel_arg_integer_create( |
| 193 |
const_cast<char*>(GRPC_ARG_MINIMAL_STACK), 1)); |
| 194 |
} |
| 195 |
grpc_channel_args args = {args_vec.size(), args_vec.data()}; |
| 196 |
|
| 197 |
MemStats before_server_create = MemStats::Snapshot(); |
| 198 |
if (absl::GetFlag(FLAGS_secure)) { |
| 199 |
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key, |
| 200 |
test_server1_cert}; |
| 201 |
grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create( |
| 202 |
nullptr, &pem_key_cert_pair, 1, 0, nullptr); |
| 203 |
server = grpc_server_create(&args, nullptr); |
| 204 |
GPR_ASSERT(grpc_server_add_http2_port(server, addr.c_str(), ssl_creds)); |
| 205 |
grpc_server_credentials_release(ssl_creds); |
| 206 |
} else { |
| 207 |
server = grpc_server_create(&args, nullptr); |
| 208 |
GPR_ASSERT(grpc_server_add_http2_port( |
| 209 |
server, addr.c_str(), grpc_insecure_server_credentials_create())); |
| 210 |
} |
| 211 |
|
| 212 |
grpc_server_register_completion_queue(server, cq, nullptr); |
| 213 |
grpc_server_start(server); |
| 214 |
|
| 215 |
MemStats after_server_create = MemStats::Snapshot(); |
| 216 |
|
| 217 |
// initialize call instances |
| 218 |
for (int i = 0; i < static_cast<int>(sizeof(calls) / sizeof(fling_call)); |
| 219 |
i++) { |
| 220 |
grpc_call_details_init(&calls[i].call_details); |
| 221 |
calls[i].state = FLING_SERVER_NEW_REQUEST; |
| 222 |
} |
| 223 |
|
| 224 |
int next_call_idx = 0; |
| 225 |
MemStats current_snapshot; |
| 226 |
|
| 227 |
request_call_unary(next_call_idx); |
| 228 |
|
| 229 |
signal(SIGINT, sigint_handler); |
| 230 |
|
| 231 |
while (!shutdown_finished) { |
| 232 |
if (got_sigint && !shutdown_started) { |
| 233 |
gpr_log(GPR_INFO, "Shutting down due to SIGINT"); |
| 234 |
|
| 235 |
shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr); |
| 236 |
grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000)); |
| 237 |
GPR_ASSERT(grpc_completion_queue_pluck( |
| 238 |
shutdown_cq, tag(1000), |
| 239 |
grpc_timeout_seconds_to_deadline(5), nullptr) |
| 240 |
.type == GRPC_OP_COMPLETE); |
| 241 |
grpc_completion_queue_destroy(shutdown_cq); |
| 242 |
grpc_completion_queue_shutdown(cq); |
| 243 |
shutdown_started = 1; |
| 244 |
} |
| 245 |
ev = grpc_completion_queue_next( |
| 246 |
cq, |
| 247 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 248 |
gpr_time_from_micros(1000000, GPR_TIMESPAN)), |
| 249 |
nullptr); |
| 250 |
fling_call* s = static_cast<fling_call*>(ev.tag); |
| 251 |
switch (ev.type) { |
| 252 |
case GRPC_OP_COMPLETE: |
| 253 |
switch (s->state) { |
| 254 |
case FLING_SERVER_NEW_REQUEST: |
| 255 |
request_call_unary(++next_call_idx); |
| 256 |
if (0 == grpc_slice_str_cmp(s->call_details.method, |
| 257 |
"/Reflector/reflectUnary")) { |
| 258 |
s->state = FLING_SERVER_SEND_INIT_METADATA; |
| 259 |
send_initial_metadata_unary(s); |
| 260 |
} else if (0 == |
| 261 |
grpc_slice_str_cmp(s->call_details.method, |
| 262 |
"Reflector/GetBeforeSvrCreation")) { |
| 263 |
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
| 264 |
send_snapshot(s, &before_server_create); |
| 265 |
} else if (0 == |
| 266 |
grpc_slice_str_cmp(s->call_details.method, |
| 267 |
"Reflector/GetAfterSvrCreation")) { |
| 268 |
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
| 269 |
send_snapshot(s, &after_server_create); |
| 270 |
} else if (0 == grpc_slice_str_cmp(s->call_details.method, |
| 271 |
"Reflector/SimpleSnapshot")) { |
| 272 |
s->state = FLING_SERVER_SEND_STATUS_SNAPSHOT; |
| 273 |
current_snapshot = MemStats::Snapshot(); |
| 274 |
send_snapshot(s, ¤t_snapshot); |
| 275 |
} else if (0 == grpc_slice_str_cmp(s->call_details.method, |
| 276 |
"Reflector/DestroyCalls")) { |
| 277 |
s->state = FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL; |
| 278 |
current_snapshot = MemStats::Snapshot(); |
| 279 |
send_snapshot(s, ¤t_snapshot); |
| 280 |
} else { |
| 281 |
gpr_log(GPR_ERROR, "Wrong call method"); |
| 282 |
} |
| 283 |
break; |
| 284 |
case FLING_SERVER_SEND_INIT_METADATA: |
| 285 |
s->state = FLING_SERVER_WAIT_FOR_DESTROY; |
| 286 |
break; |
| 287 |
case FLING_SERVER_WAIT_FOR_DESTROY: |
| 288 |
break; |
| 289 |
case FLING_SERVER_SEND_STATUS_FLING_CALL: |
| 290 |
grpc_call_unref(s->call); |
| 291 |
grpc_call_details_destroy(&s->call_details); |
| 292 |
grpc_metadata_array_destroy(&s->initial_metadata_send); |
| 293 |
grpc_metadata_array_destroy(&s->request_metadata_recv); |
| 294 |
break; |
| 295 |
case FLING_SERVER_BATCH_SEND_STATUS_FLING_CALL: |
| 296 |
for (int k = 0; |
| 297 |
k < static_cast<int>(sizeof(calls) / sizeof(fling_call)); |
| 298 |
++k) { |
| 299 |
if (calls[k].state == FLING_SERVER_WAIT_FOR_DESTROY) { |
| 300 |
calls[k].state = FLING_SERVER_SEND_STATUS_FLING_CALL; |
| 301 |
send_status(&calls[k]); |
| 302 |
} |
| 303 |
} |
| 304 |
ABSL_FALLTHROUGH_INTENDED; |
| 305 |
// no break here since we want to continue to case |
| 306 |
// FLING_SERVER_SEND_STATUS_SNAPSHOT to destroy the snapshot call |
| 307 |
case FLING_SERVER_SEND_STATUS_SNAPSHOT: |
| 308 |
grpc_byte_buffer_destroy(payload_buffer); |
| 309 |
grpc_byte_buffer_destroy(terminal_buffer); |
| 310 |
grpc_call_unref(s->call); |
| 311 |
grpc_call_details_destroy(&s->call_details); |
| 312 |
grpc_metadata_array_destroy(&s->initial_metadata_send); |
| 313 |
grpc_metadata_array_destroy(&s->request_metadata_recv); |
| 314 |
terminal_buffer = nullptr; |
| 315 |
payload_buffer = nullptr; |
| 316 |
break; |
| 317 |
} |
| 318 |
break; |
| 319 |
case GRPC_QUEUE_SHUTDOWN: |
| 320 |
GPR_ASSERT(shutdown_started); |
| 321 |
shutdown_finished = 1; |
| 322 |
break; |
| 323 |
case GRPC_QUEUE_TIMEOUT: |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
grpc_server_destroy(server); |
| 329 |
grpc_completion_queue_destroy(cq); |
| 330 |
grpc_shutdown_blocking(); |
| 331 |
return 0; |
| 332 |
} |