1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/*
* Copyright 2015 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <bson.h>
#include "mock-rs.h"
#include "sync-queue.h"
#include "../test-libmongoc.h"
struct _mock_rs_t {
bool has_primary;
int n_secondaries;
int n_arbiters;
int32_t max_wire_version;
int64_t request_timeout_msec;
bool verbose;
mock_server_t *primary;
mongoc_array_t secondaries;
mongoc_array_t arbiters;
mongoc_array_t servers;
char *hosts_str;
mongoc_uri_t *uri;
sync_queue_t *q;
};
mock_server_t *
get_server (mongoc_array_t *servers, int i)
{
return _mongoc_array_index (servers, mock_server_t *, i);
}
void
append_array (mongoc_array_t *dst, mongoc_array_t *src)
{
_mongoc_array_append_vals (dst, src->data, (uint32_t) src->len);
}
/* a string like: "localhost:1","localhost:2","localhost:3" */
char *
hosts (mongoc_array_t *servers)
{
int i;
const char *host_and_port;
bson_string_t *hosts_str = bson_string_new ("");
for (i = 0; i < servers->len; i++) {
host_and_port = mock_server_get_host_and_port (get_server (servers, i));
bson_string_append_printf (hosts_str, "\"%s\"", host_and_port);
if (i < servers->len - 1) {
bson_string_append_printf (hosts_str, ", ");
}
}
return bson_string_free (hosts_str, false); /* detach buffer */
}
mongoc_uri_t *
make_uri (mongoc_array_t *servers)
{
int i;
const char *host_and_port;
bson_string_t *uri_str = bson_string_new ("mongodb://");
mongoc_uri_t *uri;
for (i = 0; i < servers->len; i++) {
host_and_port = mock_server_get_host_and_port (get_server (servers, i));
bson_string_append_printf (uri_str, "%s", host_and_port);
if (i < servers->len - 1) {
bson_string_append_printf (uri_str, ",");
}
}
bson_string_append_printf (uri_str, "/?replicaSet=rs");
uri = mongoc_uri_new (uri_str->str);
bson_string_free (uri_str, true);
return uri;
}
/*--------------------------------------------------------------------------
*
* mock_rs_with_autoismaster --
*
* A new mock replica set. Each member autoresponds to ismaster.
* Call mock_rs_run to start it, then mock_rs_get_uri to connect.
*
* Returns:
* A replica set you must mock_rs_destroy.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
mock_rs_t *
mock_rs_with_autoismaster (int32_t max_wire_version,
bool has_primary,
int n_secondaries,
int n_arbiters)
{
mock_rs_t *rs = (mock_rs_t *)bson_malloc0 (sizeof (mock_rs_t));
rs->max_wire_version = max_wire_version;
rs->has_primary = has_primary;
rs->n_secondaries = n_secondaries;
rs->n_arbiters = n_arbiters;
rs->request_timeout_msec = 10 * 1000;
_mongoc_array_init (&rs->secondaries, sizeof (mock_server_t *));
_mongoc_array_init (&rs->arbiters, sizeof (mock_server_t *));
_mongoc_array_init (&rs->servers, sizeof (mock_server_t *));
rs->q = q_new ();
rs->verbose = test_framework_getenv_bool ("MONGOC_TEST_SERVER_VERBOSE");
return rs;
}
/*--------------------------------------------------------------------------
*
* mock_rs_set_verbose --
*
* Tell the replica set whether to log during normal operation.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_set_verbose (mock_rs_t *rs,
bool verbose)
{
int i;
rs->verbose = true;
for (i = 0; i < rs->servers.len; i++) {
mock_server_set_verbose (get_server (&rs->servers, i), verbose);
}
}
/*--------------------------------------------------------------------------
*
* mock_rs_get_request_timeout_msec --
*
* How long mock_rs_receives_* functions wait for a client
* request before giving up and returning NULL.
*
*--------------------------------------------------------------------------
*/
int64_t
mock_rs_get_request_timeout_msec (mock_rs_t *rs)
{
return rs->request_timeout_msec;
}
/*--------------------------------------------------------------------------
*
* mock_rs_set_request_timeout_msec --
*
* How long mock_rs_receives_* functions wait for a client
* request before giving up and returning NULL.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_set_request_timeout_msec (mock_rs_t *rs,
int64_t request_timeout_msec)
{
rs->request_timeout_msec = request_timeout_msec;
}
static bool
rs_q_append (request_t *request,
void *data)
{
mock_rs_t *rs = (mock_rs_t *)data;
q_put (rs->q, (void *)request);
return true; /* handled */
}
/*--------------------------------------------------------------------------
*
* mock_rs_run --
*
* Start each member listening on an unused port. After this, call
* mock_rs_get_uri to connect.
*
* Returns:
* None.
*
* Side effects:
* The replica set's URI is set.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_run (mock_rs_t *rs)
{
int i;
mock_server_t *server;
char *hosts_str;
char *ismaster_json;
if (rs->has_primary) {
/* start primary */
rs->primary = mock_server_new ();
mock_server_run (rs->primary);
}
/* start secondaries */
_mongoc_array_init (&rs->secondaries, sizeof(mock_server_t *));
for (i = 0; i < rs->n_secondaries; i++) {
server = mock_server_new ();
mock_server_run (server);
_mongoc_array_append_val (&rs->secondaries, server);
}
/* start arbiters */
_mongoc_array_init (&rs->arbiters, sizeof(mock_server_t *));
for (i = 0; i < rs->n_arbiters; i++) {
server = mock_server_new ();
mock_server_run (server);
_mongoc_array_append_val (&rs->arbiters, server);
}
/* add all servers to replica set */
if (rs->has_primary) {
_mongoc_array_append_val (&rs->servers, rs->primary);
}
append_array (&rs->servers, &rs->secondaries);
append_array (&rs->servers, &rs->arbiters);
/* enqueue unhandled requests in rs->q, they're retrieved with
* mock_rs_receives_query() &co. rs_q_append is added first so it
* runs last, after auto_ismaster.
*/
for (i = 0; i < rs->servers.len; i++) {
mock_server_autoresponds (get_server (&rs->servers, i),
rs_q_append,
(void *) rs,
NULL);
}
/* now we know all servers' ports and we have them in one array */
rs->hosts_str = hosts_str = hosts (&rs->servers);
rs->uri = make_uri (&rs->servers);
if (rs->has_primary) {
/* primary's ismaster response */
ismaster_json = bson_strdup_printf (
"{'ok': 1, 'ismaster': true, 'secondary': false, 'maxWireVersion': %d, "
"'setName': 'rs', 'hosts': [%s]}", rs->max_wire_version, hosts_str);
mock_server_auto_ismaster (rs->primary, ismaster_json);
bson_free (ismaster_json);
}
/* secondaries' ismaster response */
ismaster_json = bson_strdup_printf (
"{'ok': 1, 'ismaster': false, 'secondary': true, 'maxWireVersion': %d, "
"'setName': 'rs', 'hosts': [%s]}", rs->max_wire_version, hosts_str);
for (i = 0; i < rs->n_secondaries; i++) {
mock_server_auto_ismaster (get_server (&rs->secondaries, i), ismaster_json);
}
bson_free (ismaster_json);
/* arbiters' ismaster response */
ismaster_json = bson_strdup_printf (
"{'ok': 1, 'ismaster': true, 'arbiterOnly': true, 'maxWireVersion': %d, "
"'setName': 'rs', 'hosts': [%s]}", rs->max_wire_version, hosts_str);
for (i = 0; i < rs->n_arbiters; i++) {
mock_server_auto_ismaster (get_server (&rs->arbiters, i), ismaster_json);
}
for (i = 0; i < rs->servers.len; i++) {
mock_server_set_verbose (get_server (&rs->servers, i), rs->verbose);
}
bson_free (ismaster_json);
}
/*--------------------------------------------------------------------------
*
* mock_rs_get_uri --
*
* Call after mock_rs_run to get the connection string.
*
* Returns:
* A const URI.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
const mongoc_uri_t *
mock_rs_get_uri (mock_rs_t *rs)
{
return rs->uri;
}
/*--------------------------------------------------------------------------
*
* mock_rs_receives_query --
*
* Pop a client request if one is enqueued, or wait up to
* request_timeout_ms for the client to send a request.
*
* Returns:
* A request you must request_destroy, or NULL if the request does
* not match.
*
* Side effects:
* Logs if the current request is not a query matching ns, flags,
* skip, n_return, query_json, and fields_json.
*
*--------------------------------------------------------------------------
*/
/* TODO: refactor with mock_server_receives_query, etc.? */
request_t *
mock_rs_receives_query (mock_rs_t *rs,
const char *ns,
mongoc_query_flags_t flags,
uint32_t skip,
uint32_t n_return,
const char *query_json,
const char *fields_json)
{
request_t *request;
request = (request_t *) q_get (rs->q, rs->request_timeout_msec);
if (request && !request_matches_query (request,
ns,
flags,
skip,
n_return,
query_json,
fields_json,
false)) {
request_destroy (request);
return NULL;
}
return request;
}
/*--------------------------------------------------------------------------
*
* mock_rs_receives_getmore --
*
* Pop a client request if one is enqueued, or wait up to
* request_timeout_ms for the client to send a request.
*
* Returns:
* A request you must request_destroy, or NULL if the request does
* not match.
*
* Side effects:
* Logs if the current request is not a getmore matching n_return
* and cursor_id.
*
*--------------------------------------------------------------------------
*/
request_t *
mock_rs_receives_getmore (mock_rs_t *rs,
const char *ns,
uint32_t n_return,
int64_t cursor_id)
{
request_t *request;
request = (request_t *) q_get (rs->q, rs->request_timeout_msec);
if (request && !request_matches_getmore (request,
ns,
n_return,
cursor_id)) {
request_destroy (request);
return NULL;
}
return request;
}
/*--------------------------------------------------------------------------
*
* mock_rs_hangs_up --
*
* Hang up on a client request.
*
* Returns:
* None.
*
* Side effects:
* Causes a network error on the client side.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_hangs_up (request_t *request)
{
mock_server_hangs_up (request);
}
/*--------------------------------------------------------------------------
*
* mock_rs_receives_kill_cursors --
*
* Pop a client request if one is enqueued, or wait up to
* request_timeout_ms for the client to send a request.
*
* Real-life OP_KILLCURSORS can take multiple ids, but that is
* not yet supported here.
*
* Returns:
* A request you must request_destroy, or NULL if the request
* does not match.
*
* Side effects:
* Logs if the current request is not an OP_KILLCURSORS with the
* expected cursor_id.
*
*--------------------------------------------------------------------------
*/
request_t *
mock_rs_receives_kill_cursors (mock_rs_t *rs,
int64_t cursor_id)
{
request_t *request;
request = (request_t *) q_get (rs->q, rs->request_timeout_msec);
if (request && !request_matches_kill_cursors (request, cursor_id)) {
request_destroy (request);
return NULL;
}
return request;
}
/*--------------------------------------------------------------------------
*
* mock_rs_replies --
*
* Respond to a client request.
*
* Returns:
* None.
*
* Side effects:
* Sends an OP_REPLY to the client.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_replies (request_t *request,
uint32_t flags,
int64_t cursor_id,
int32_t starting_from,
int32_t number_returned,
const char *docs_json)
{
mock_server_replies (request, flags, cursor_id, starting_from,
number_returned, docs_json);
}
/*--------------------------------------------------------------------------
*
* mock_rs_request_is_to_primary --
*
* Check that the request is non-NULL and sent to a
* primary in this replica set.
*
* Returns:
* True if so.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
mock_rs_request_is_to_primary (mock_rs_t *rs,
request_t *request)
{
uint16_t port;
assert (request);
port = request_get_server_port (request);
return (rs->primary && port == mock_server_get_port (rs->primary));
}
/*--------------------------------------------------------------------------
*
* mock_rs_request_is_to_secondary --
*
* Check that the request is non-NULL and sent to a
* secondary in this replica set.
*
* Returns:
* True if so.
*
* Side effects:
* None.
*
*--------------------------------------------------------------------------
*/
bool
mock_rs_request_is_to_secondary (mock_rs_t *rs,
request_t *request)
{
uint16_t port;
int i;
assert (request);
port = request_get_server_port (request);
for (i = 0; i < rs->secondaries.len; i++) {
if (port == mock_server_get_port (get_server (&rs->secondaries, i))) {
return true;
}
}
return false;
}
/*--------------------------------------------------------------------------
*
* mock_rs_destroy --
*
* Free a mock_rs_t.
*
* Returns:
* None.
*
* Side effects:
* Destroys each member mock_server_t, closes sockets, joins threads.
*
*--------------------------------------------------------------------------
*/
void
mock_rs_destroy (mock_rs_t *rs)
{
int i;
for (i = 0; i < rs->servers.len; i++) {
mock_server_destroy (get_server (&rs->servers, i));
}
_mongoc_array_destroy (&rs->secondaries);
_mongoc_array_destroy (&rs->arbiters);
_mongoc_array_destroy (&rs->servers);
bson_free (rs->hosts_str);
mongoc_uri_destroy (rs->uri);
q_destroy (rs->q);
bson_free (rs);
}
|