Skip to content

Commit 9e3d80f

Browse files
committed
Fix wrong use of hello command in server monitoring
1 parent 55b8675 commit 9e3d80f

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct mongoc_topology_scanner_node {
5454
int64_t last_used;
5555
int64_t last_failed;
5656
bool has_auth;
57+
bool hello_ok;
5758
mongoc_host_list_t host;
5859
struct mongoc_topology_scanner *ts;
5960

@@ -131,7 +132,8 @@ mongoc_topology_scanner_valid (mongoc_topology_scanner_t *ts);
131132
void
132133
mongoc_topology_scanner_add (mongoc_topology_scanner_t *ts,
133134
const mongoc_host_list_t *host,
134-
uint32_t id);
135+
uint32_t id,
136+
bool hello_ok);
135137

136138
void
137139
mongoc_topology_scanner_scan (mongoc_topology_scanner_t *ts, uint32_t id);

src/libmongoc/src/mongoc/mongoc-topology-scanner.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ _begin_hello_cmd (mongoc_topology_scanner_node_t *node,
327327

328328
if (node->last_used != -1 && node->last_failed == -1) {
329329
/* The node's been used before and not failed recently */
330-
bson_copy_to (&ts->hello_cmd, &cmd);
330+
bson_copy_to (node->hello_ok ? &ts->hello_cmd : &ts->legacy_hello_cmd,
331+
&cmd);
331332
} else {
332333
bson_copy_to (_mongoc_topology_scanner_get_handshake_cmd (ts), &cmd);
333334
}
@@ -457,7 +458,8 @@ mongoc_topology_scanner_valid (mongoc_topology_scanner_t *ts)
457458
void
458459
mongoc_topology_scanner_add (mongoc_topology_scanner_t *ts,
459460
const mongoc_host_list_t *host,
460-
uint32_t id)
461+
uint32_t id,
462+
bool hello_ok)
461463
{
462464
mongoc_topology_scanner_node_t *node;
463465

@@ -469,6 +471,7 @@ mongoc_topology_scanner_add (mongoc_topology_scanner_t *ts,
469471
node->ts = ts;
470472
node->last_failed = -1;
471473
node->last_used = -1;
474+
node->hello_ok = hello_ok;
472475
bson_init (&node->speculative_auth_response);
473476

474477
DL_APPEND (ts->nodes, node);

src/libmongoc/src/mongoc/mongoc-topology.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,19 @@ _mongoc_topology_reconcile_add_nodes (mongoc_server_description_t *sd,
4343
mongoc_topology_t *topology)
4444
{
4545
mongoc_topology_scanner_t *scanner = topology->scanner;
46+
mongoc_topology_scanner_node_t *node;
47+
48+
/* Search by ID and update hello_ok */
49+
node = mongoc_topology_scanner_get_node (scanner, sd->id);
50+
if (node) {
51+
node->hello_ok = sd->hello_ok;
52+
53+
return true;
54+
}
4655

47-
/* quickly search by id, then check if a node for this host was retired in
48-
* this scan. */
49-
if (!mongoc_topology_scanner_get_node (scanner, sd->id) &&
50-
!mongoc_topology_scanner_has_node_for_host (scanner, &sd->host)) {
51-
mongoc_topology_scanner_add (scanner, &sd->host, sd->id);
56+
/* Check if a node for this host was retired in this scan. */
57+
if (!mongoc_topology_scanner_has_node_for_host (scanner, &sd->host)) {
58+
mongoc_topology_scanner_add (scanner, &sd->host, sd->id, sd->hello_ok);
5259
mongoc_topology_scanner_scan (scanner, sd->id);
5360
}
5461

@@ -189,9 +196,9 @@ _mongoc_topology_scanner_cb (uint32_t id,
189196
_mongoc_topology_update_no_lock (
190197
id, hello_response, rtt_msec, topology, error);
191198

192-
/* The processing of the hello results above may have added/removed
193-
* server descriptions. We need to reconcile that with our monitoring
194-
* agents
199+
/* The processing of the hello results above may have added, changed, or
200+
* removed server descriptions. We need to reconcile that with our
201+
* monitoring agents
195202
*/
196203
mongoc_topology_reconcile (topology);
197204

@@ -442,7 +449,7 @@ mongoc_topology_new (const mongoc_uri_t *uri, bool single_threaded)
442449
while (hl) {
443450
mongoc_topology_description_add_server (
444451
&topology->description, hl->host_and_port, &id);
445-
mongoc_topology_scanner_add (topology->scanner, hl, id);
452+
mongoc_topology_scanner_add (topology->scanner, hl, id, false);
446453

447454
hl = hl->next;
448455
}

src/libmongoc/tests/test-happy-eyeballs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ _testcase_run (he_testcase_t *testcase)
304304
testcase->state.start = bson_get_monotonic_time ();
305305

306306
mongoc_topology_scanner_add (
307-
ts, &testcase->state.host, 1 /* any server id is ok. */);
307+
ts, &testcase->state.host, 1 /* any server id is ok. */, false);
308308
mongoc_topology_scanner_scan (ts, 1);
309309
/* how many commands should we have initially? */
310310
ASSERT_CMPINT ((int) (ts->async->ncmds), ==, expected->initial_acmds);

src/libmongoc/tests/test-mongoc-topology-scanner.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ _test_topology_scanner (bool with_ssl)
9292
mongoc_topology_scanner_add (
9393
topology_scanner,
9494
mongoc_uri_get_hosts (mock_server_get_uri (servers[i])),
95-
(uint32_t) i);
95+
(uint32_t) i,
96+
false);
9697
}
9798

9899
for (i = 0; i < 3; i++) {
@@ -498,7 +499,7 @@ test_topology_scanner_dns_testcase (dns_testcase_t *testcase)
498499
BSON_ASSERT (!host.next);
499500
bson_free (host_str);
500501

501-
mongoc_topology_scanner_add (ts, &host, 1);
502+
mongoc_topology_scanner_add (ts, &host, 1, false);
502503
mongoc_topology_scanner_scan (ts, 1 /* any server id is ok. */);
503504
ASSERT_CMPINT ((int) (ts->async->ncmds), ==, testcase->expected_ncmds);
504505
mongoc_topology_scanner_work (ts);
@@ -603,7 +604,7 @@ test_topology_retired_fails_to_initiate (void)
603604
BSON_ASSERT (_mongoc_host_list_from_string (
604605
&host_list, mock_server_get_host_and_port (server)));
605606

606-
mongoc_topology_scanner_add (scanner, &host_list, 1);
607+
mongoc_topology_scanner_add (scanner, &host_list, 1, false);
607608
mongoc_topology_scanner_start (scanner, false);
608609
BSON_ASSERT (scanner->async->ncmds > 0);
609610
/* retire the node */

src/libmongoc/tests/test-mongoc-topology.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ _test_topology_invalidate_server (bool pooled)
529529
fake_sd->type = MONGOC_SERVER_STANDALONE;
530530
mongoc_set_add (td->servers, fake_id, fake_sd);
531531
mongoc_topology_scanner_add (
532-
client->topology->scanner, &fake_host_list, fake_id);
532+
client->topology->scanner, &fake_host_list, fake_id, false);
533533
BSON_ASSERT (!mongoc_cluster_stream_for_server (
534534
&client->cluster, fake_id, true, NULL, NULL, &error));
535535
bson_mutex_lock (&client->topology->mutex);

0 commit comments

Comments
 (0)