Skip to content

Commit 52d2a09

Browse files
committed
CDRIVER-696: Aggregate the scanner node errors when Server Selection fails
1 parent c8cb7ea commit 52d2a09

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ bool
113113
mongoc_topology_scanner_work (mongoc_topology_scanner_t *ts,
114114
int32_t timeout_msec);
115115

116+
void
117+
mongoc_topology_scanner_sum_errors (mongoc_topology_scanner_t *ts,
118+
bson_error_t *error);
119+
116120
void
117121
mongoc_topology_scanner_reset (mongoc_topology_scanner_t *ts);
118122

src/mongoc/mongoc-topology-scanner.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,45 @@ mongoc_topology_scanner_work (mongoc_topology_scanner_t *ts,
535535
return r;
536536
}
537537

538+
/*
539+
*--------------------------------------------------------------------------
540+
*
541+
* mongoc_topology_scanner_sum_errors --
542+
*
543+
* Summarizes all scanner node errors into one error message
544+
*
545+
*--------------------------------------------------------------------------
546+
*/
547+
548+
void
549+
mongoc_topology_scanner_sum_errors (mongoc_topology_scanner_t *ts,
550+
bson_error_t *error)
551+
{
552+
mongoc_topology_scanner_node_t *node, *tmp;
553+
554+
DL_FOREACH_SAFE (ts->nodes, node, tmp) {
555+
if (node->last_error.code) {
556+
char *msg = NULL;
557+
558+
if (error->code) {
559+
msg = bson_strdup(error->message);
560+
}
561+
562+
bson_set_error(error,
563+
MONGOC_ERROR_SERVER_SELECTION,
564+
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
565+
"%s[%s] ",
566+
msg ? msg : "", node->last_error.message);
567+
if (msg) {
568+
bson_free (msg);
569+
}
570+
}
571+
}
572+
if (error->code) {
573+
error->message[strlen(error->message)-1] = '\0';
574+
}
575+
}
576+
538577
/*
539578
*--------------------------------------------------------------------------
540579
*
@@ -557,3 +596,4 @@ mongoc_topology_scanner_reset (mongoc_topology_scanner_t *ts)
557596
}
558597
}
559598
}
599+

src/mongoc/mongoc-topology.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,16 @@ _mongoc_topology_run_scanner (mongoc_topology_t *topology,
321321
*--------------------------------------------------------------------------
322322
*/
323323
static void
324-
_mongoc_topology_do_blocking_scan (mongoc_topology_t *topology) {
324+
_mongoc_topology_do_blocking_scan (mongoc_topology_t *topology, bson_error_t *error) {
325325
mongoc_topology_scanner_start (topology->scanner,
326326
topology->connect_timeout_msec,
327327
true);
328328

329329
while (_mongoc_topology_run_scanner (topology,
330330
topology->connect_timeout_msec)) {}
331331

332+
/* Aggregate all scanner errors, if any */
333+
mongoc_topology_scanner_sum_errors (topology->scanner, error);
332334
/* "retired" nodes can be checked again in the next scan */
333335
mongoc_topology_scanner_reset (topology->scanner);
334336
topology->last_scan = bson_get_monotonic_time ();
@@ -378,6 +380,7 @@ mongoc_topology_select (mongoc_topology_t *topology,
378380
bool try_once;
379381
int64_t sleep_usec;
380382
bool tried_once;
383+
bson_error_t scanner_error = { 0 };
381384

382385
/* These names come from the Server Selection Spec pseudocode */
383386
int64_t loop_start; /* when we entered this function */
@@ -424,7 +427,7 @@ mongoc_topology_select (mongoc_topology_t *topology,
424427
}
425428

426429
/* takes up to connectTimeoutMS. sets "last_scan", clears "stale" */
427-
_mongoc_topology_do_blocking_scan (topology);
430+
_mongoc_topology_do_blocking_scan (topology, &scanner_error);
428431
tried_once = true;
429432
}
430433

@@ -441,11 +444,19 @@ mongoc_topology_select (mongoc_topology_t *topology,
441444

442445
if (try_once) {
443446
if (tried_once) {
444-
bson_set_error(error,
445-
MONGOC_ERROR_SERVER_SELECTION,
446-
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
447-
"No suitable servers found: "
448-
"`serverselectiontryonce` set");
447+
if (scanner_error.code) {
448+
bson_set_error(error,
449+
MONGOC_ERROR_SERVER_SELECTION,
450+
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
451+
"No suitable servers found "
452+
"(`serverselectiontryonce` set): %s", scanner_error.message);
453+
} else {
454+
bson_set_error(error,
455+
MONGOC_ERROR_SERVER_SELECTION,
456+
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
457+
"No suitable servers found "
458+
"(`serverselectiontryonce` set)");
459+
}
449460
goto FAIL;
450461
}
451462
} else {

tests/test-mongoc-topology.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ _test_server_selection (bool try_once)
198198
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_SERVER_SELECTION_FAILURE);
199199

200200
if (try_once) {
201-
ASSERT_CMPSTR ("No suitable servers found: `serverselectiontryonce` set", error.message);
201+
ASSERT_CMPSTR ("No suitable servers found (`serverselectiontryonce` set)", error.message);
202202
} else {
203203
ASSERT_CMPSTR ("No suitable servers found: `minheartbeatfrequencyms` not reached yet", error.message);
204204
}

0 commit comments

Comments
 (0)