16
16
17
17
#include "mongoc-error.h"
18
18
#include "mongoc-topology-private.h"
19
- #include "mongoc-uri-private.h"
20
19
#include "mongoc-util-private.h"
21
20
22
21
#include "utlist.h"
@@ -326,22 +325,46 @@ _mongoc_topology_run_scanner (mongoc_topology_t *topology,
326
325
*--------------------------------------------------------------------------
327
326
*/
328
327
static void
329
- _mongoc_topology_do_blocking_scan (mongoc_topology_t * topology , bson_error_t * error ) {
330
- mongoc_topology_scanner_start (topology -> scanner ,
331
- topology -> connect_timeout_msec ,
328
+ _mongoc_topology_do_blocking_scan (mongoc_topology_t * topology ,
329
+ bson_error_t * error )
330
+ {
331
+ mongoc_topology_scanner_t * scanner ;
332
+
333
+ scanner = topology -> scanner ;
334
+ mongoc_topology_scanner_start (scanner ,
335
+ (int32_t ) topology -> connect_timeout_msec ,
332
336
true);
333
337
334
338
while (_mongoc_topology_run_scanner (topology ,
335
339
topology -> connect_timeout_msec )) {}
336
340
337
- /* Aggregate all scanner errors, if any */
338
- mongoc_topology_scanner_sum_errors ( topology -> scanner , error );
341
+ mongoc_topology_scanner_get_error ( scanner , error );
342
+
339
343
/* "retired" nodes can be checked again in the next scan */
340
- mongoc_topology_scanner_reset (topology -> scanner );
344
+ mongoc_topology_scanner_reset (scanner );
341
345
topology -> last_scan = bson_get_monotonic_time ();
342
346
topology -> stale = false;
343
347
}
344
348
349
+ static void
350
+ _mongoc_server_selection_error (const char * msg ,
351
+ const bson_error_t * scanner_error ,
352
+ bson_error_t * error )
353
+ {
354
+ if (scanner_error && scanner_error -> code ) {
355
+ bson_set_error (error ,
356
+ MONGOC_ERROR_SERVER_SELECTION ,
357
+ MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
358
+ "%s: %s" ,
359
+ msg , scanner_error -> message );
360
+ } else {
361
+ bson_set_error (error ,
362
+ MONGOC_ERROR_SERVER_SELECTION ,
363
+ MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
364
+ "%s" , msg );
365
+ }
366
+ }
367
+
345
368
/*
346
369
*-------------------------------------------------------------------------
347
370
*
@@ -376,6 +399,8 @@ mongoc_topology_select (mongoc_topology_t *topology,
376
399
const mongoc_read_prefs_t * read_prefs ,
377
400
bson_error_t * error )
378
401
{
402
+ static const char * timeout_msg =
403
+ "No suitable servers found: `serverSelectionTimeoutMS` expired" ;
379
404
int r ;
380
405
int64_t local_threshold_ms ;
381
406
mongoc_server_description_t * selected_server = NULL ;
@@ -416,11 +441,10 @@ mongoc_topology_select (mongoc_topology_t *topology,
416
441
417
442
if (scan_ready > expire_at && !try_once ) {
418
443
/* selection timeout will expire before min heartbeat passes */
419
- bson_set_error (error ,
420
- MONGOC_ERROR_SERVER_SELECTION ,
421
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
422
- "No suitable servers found: "
423
- "`minheartbeatfrequencyms` not reached yet" );
444
+ _mongoc_server_selection_error (
445
+ "No suitable servers found: "
446
+ "(`minHeartbeatFrequencyMS` not reached yet)" ,
447
+ & scanner_error , error );
424
448
goto FAIL ;
425
449
}
426
450
@@ -447,31 +471,20 @@ mongoc_topology_select (mongoc_topology_t *topology,
447
471
448
472
if (try_once ) {
449
473
if (tried_once ) {
450
- if (scanner_error .code ) {
451
- bson_set_error (error ,
452
- MONGOC_ERROR_SERVER_SELECTION ,
453
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
454
- "No suitable servers found "
455
- "(`serverselectiontryonce` set): %s" , scanner_error .message );
456
- } else {
457
- bson_set_error (error ,
458
- MONGOC_ERROR_SERVER_SELECTION ,
459
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
460
- "No suitable servers found "
461
- "(`serverselectiontryonce` set)" );
462
- }
474
+ _mongoc_server_selection_error (
475
+ "No suitable servers found (`serverSelectionTryOnce` set)" ,
476
+ & scanner_error , error );
477
+
463
478
goto FAIL ;
464
479
}
465
480
} else {
466
481
loop_end = bson_get_monotonic_time ();
467
482
468
483
if (loop_end > expire_at ) {
469
484
/* no time left in server_selection_timeout_msec */
470
- bson_set_error (error ,
471
- MONGOC_ERROR_SERVER_SELECTION ,
472
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
473
- "No suitable servers found: "
474
- "`serverselectiontimeoutms` timed out" );
485
+ _mongoc_server_selection_error (timeout_msg ,
486
+ & scanner_error , error );
487
+
475
488
goto FAIL ;
476
489
}
477
490
}
@@ -493,6 +506,7 @@ mongoc_topology_select (mongoc_topology_t *topology,
493
506
r = mongoc_cond_timedwait (& topology -> cond_client , & topology -> mutex ,
494
507
(expire_at - loop_start ) / 1000 );
495
508
509
+ mongoc_topology_scanner_get_error (topology -> scanner , & scanner_error );
496
510
mongoc_mutex_unlock (& topology -> mutex );
497
511
498
512
#ifdef _WIN32
@@ -501,27 +515,25 @@ mongoc_topology_select (mongoc_topology_t *topology,
501
515
if (r == ETIMEDOUT ) {
502
516
#endif
503
517
/* handle timeouts */
504
- bson_set_error (error ,
505
- MONGOC_ERROR_SERVER_SELECTION ,
506
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
507
- "Timed out trying to select a server" );
518
+ _mongoc_server_selection_error (timeout_msg ,
519
+ & scanner_error , error );
520
+
508
521
goto FAIL ;
509
522
} else if (r ) {
510
523
bson_set_error (error ,
511
524
MONGOC_ERROR_SERVER_SELECTION ,
512
525
MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
513
- "Unknown error '%d' received while waiting on thread condition" ,
514
- r );
526
+ "Unknown error '%d' received while waiting on "
527
+ "thread condition" , r );
515
528
goto FAIL ;
516
529
}
517
530
518
531
loop_start = bson_get_monotonic_time ();
519
532
520
533
if (loop_start > expire_at ) {
521
- bson_set_error (error ,
522
- MONGOC_ERROR_SERVER_SELECTION ,
523
- MONGOC_ERROR_SERVER_SELECTION_FAILURE ,
524
- "Timed out trying to select a server" );
534
+ _mongoc_server_selection_error (timeout_msg ,
535
+ & scanner_error , error );
536
+
525
537
goto FAIL ;
526
538
}
527
539
} else {
0 commit comments