Skip to content

Commit fb1031b

Browse files
committed
CDRIVER-2218 Only topology scanner performs MongoDB Handshake
1 parent 86eddf9 commit fb1031b

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
668668
const char *address,
669669
uint32_t server_id)
670670
{
671-
bson_t command = BSON_INITIALIZER;
671+
const bson_t *command;
672672
mongoc_cmd_parts_t parts;
673673
bson_t reply;
674674
bson_error_t error = {0};
@@ -682,8 +682,10 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
682682
BSON_ASSERT (cluster);
683683
BSON_ASSERT (stream);
684684

685-
bson_append_int32 (&command, "ismaster", 8, 1);
686-
mongoc_cmd_parts_init (&parts, "admin", MONGOC_QUERY_SLAVE_OK, &command);
685+
command = _mongoc_topology_scanner_get_ismaster (
686+
cluster->client->topology->scanner);
687+
688+
mongoc_cmd_parts_init (&parts, "admin", MONGOC_QUERY_SLAVE_OK, command);
687689

688690
start = bson_get_monotonic_time ();
689691
mongoc_cluster_run_command_private (
@@ -698,7 +700,6 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
698700
/* send the error from run_command IN to handle_ismaster */
699701
mongoc_server_description_handle_ismaster (sd, &reply, rtt_msec, &error);
700702

701-
bson_destroy (&command);
702703
bson_destroy (&reply);
703704

704705
r = _mongoc_topology_update_from_handshake (cluster->client->topology, sd);

src/mongoc/mongoc-topology-scanner-private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ mongoc_topology_scanner_node_setup (mongoc_topology_scanner_node_t *node,
150150
mongoc_topology_scanner_node_t *
151151
mongoc_topology_scanner_get_node (mongoc_topology_scanner_t *ts, uint32_t id);
152152

153+
bson_t *
154+
_mongoc_topology_scanner_get_ismaster (mongoc_topology_scanner_t *ts);
155+
153156
bool
154157
mongoc_topology_scanner_has_node_for_host (mongoc_topology_scanner_t *ts,
155158
mongoc_host_list_t *host);

src/mongoc/mongoc-topology-scanner.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,9 @@ _build_ismaster_with_handshake (mongoc_topology_scanner_t *ts)
106106
return res;
107107
}
108108

109-
static bson_t *
110-
_get_ismaster_doc (mongoc_topology_scanner_t *ts,
111-
mongoc_topology_scanner_node_t *node)
109+
bson_t *
110+
_mongoc_topology_scanner_get_ismaster (mongoc_topology_scanner_t *ts)
112111
{
113-
if (node->last_used != -1 && node->last_failed == -1) {
114-
/* The node's been used before and not failed recently */
115-
return &ts->ismaster_cmd;
116-
}
117-
118112
/* If this is the first time using the node or if it's the first time
119113
* using it after a failure, build handshake doc */
120114
if (bson_empty (&ts->ismaster_cmd_with_handshake)) {
@@ -137,14 +131,22 @@ _begin_ismaster_cmd (mongoc_topology_scanner_t *ts,
137131
mongoc_topology_scanner_node_t *node,
138132
int64_t timeout_msec)
139133
{
140-
const bson_t *ismaster_cmd_to_send = _get_ismaster_doc (ts, node);
134+
const bson_t *command;
135+
136+
if (node->last_used != -1 && node->last_failed == -1) {
137+
/* The node's been used before and not failed recently */
138+
command = &ts->ismaster_cmd;
139+
} else {
140+
command = _mongoc_topology_scanner_get_ismaster (ts);
141+
}
142+
141143

142144
node->cmd = mongoc_async_cmd_new (ts->async,
143145
node->stream,
144146
ts->setup,
145147
node->host.host,
146148
"admin",
147-
ismaster_cmd_to_send,
149+
command,
148150
&mongoc_topology_scanner_ismaster_handler,
149151
node,
150152
timeout_msec);

tests/test-mongoc-client.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,6 @@ test_mongoc_client_select_server_retry_fail (void)
23712371
}
23722372

23732373

2374-
23752374
/* CDRIVER-2172: in single mode, if the selected server has a socket that's been
23762375
* idle for socketCheckIntervalMS, check it with ping. If it fails, retry once.
23772376
*/
@@ -2728,6 +2727,56 @@ _respond_to_ping (future_t *future, mock_server_t *server)
27282727
request_destroy (request);
27292728
}
27302729

2730+
static void
2731+
test_mongoc_handshake_pool (void)
2732+
{
2733+
mock_server_t *server;
2734+
request_t *request1;
2735+
request_t *request2;
2736+
mongoc_uri_t *uri;
2737+
mongoc_client_t *client1;
2738+
mongoc_client_t *client2;
2739+
mongoc_client_pool_t *pool;
2740+
const char *const server_reply = "{'ok': 1, 'ismaster': true}";
2741+
future_t *future;
2742+
2743+
server = mock_server_new ();
2744+
mock_server_run (server);
2745+
2746+
uri = mongoc_uri_copy (mock_server_get_uri (server));
2747+
mongoc_uri_set_appname (uri, BSON_FUNC);
2748+
2749+
pool = mongoc_client_pool_new (uri);
2750+
2751+
client1 = mongoc_client_pool_pop (pool);
2752+
request1 = mock_server_receives_ismaster (server);
2753+
_assert_ismaster_valid (request1, true);
2754+
mock_server_replies_simple (request1, server_reply);
2755+
request_destroy (request1);
2756+
2757+
client2 = mongoc_client_pool_pop (pool);
2758+
future = future_client_command_simple (
2759+
client2, "test", tmp_bson ("{'ping': 1}"), NULL, NULL, NULL);
2760+
2761+
request2 = mock_server_receives_ismaster (server);
2762+
_assert_ismaster_valid (request2, true);
2763+
mock_server_replies_simple (request2, server_reply);
2764+
request_destroy (request2);
2765+
2766+
request2 = mock_server_receives_command (
2767+
server, "test", MONGOC_QUERY_SLAVE_OK, NULL);
2768+
mock_server_replies_ok_and_destroys (request2);
2769+
ASSERT (future_get_bool (future));
2770+
future_destroy (future);
2771+
2772+
mongoc_client_pool_push (pool, client1);
2773+
mongoc_client_pool_push (pool, client2);
2774+
2775+
mongoc_client_pool_destroy (pool);
2776+
mongoc_uri_destroy (uri);
2777+
mock_server_destroy (server);
2778+
}
2779+
27312780
static void
27322781
_test_client_sends_handshake (bool pooled)
27332782
{
@@ -2762,9 +2811,8 @@ _test_client_sends_handshake (bool pooled)
27622811

27632812
request = mock_server_receives_ismaster (server);
27642813

2765-
/* Make sure the isMaster request has a "meta" field: */
2814+
/* Make sure the isMaster request has a "client" field: */
27662815
_assert_ismaster_valid (request, true);
2767-
27682816
mock_server_replies_simple (request, server_reply);
27692817
request_destroy (request);
27702818

@@ -3179,6 +3227,8 @@ test_client_install (TestSuite *suite)
31793227
TestSuite_AddMockServerTest (
31803228
suite, "/Client/mismatched_me", test_mongoc_client_mismatched_me);
31813229

3230+
TestSuite_AddMockServerTest (
3231+
suite, "/Client/handshake/pool", test_mongoc_handshake_pool);
31823232
TestSuite_Add (suite,
31833233
"/Client/application_handshake",
31843234
test_mongoc_client_application_handshake);

0 commit comments

Comments
 (0)