@@ -121,7 +121,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
121
121
this . forgetWriter ( address , database || DEFAULT_DB_NAME )
122
122
return newError (
123
123
'No longer possible to write to server at ' + address ,
124
- SESSION_EXPIRED
124
+ SESSION_EXPIRED ,
125
+ error
125
126
)
126
127
}
127
128
@@ -345,7 +346,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
345
346
) {
346
347
// we start with seed router, no routers were probed before
347
348
const seenRouters = [ ]
348
- let newRoutingTable = await this . _fetchRoutingTableUsingSeedRouter (
349
+ let [ newRoutingTable , error ] = await this . _fetchRoutingTableUsingSeedRouter (
349
350
seenRouters ,
350
351
this . _seedRouter ,
351
352
currentRoutingTable ,
@@ -357,18 +358,21 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
357
358
this . _useSeedRouter = false
358
359
} else {
359
360
// seed router did not return a valid routing table - try to use other known routers
360
- newRoutingTable = await this . _fetchRoutingTableUsingKnownRouters (
361
+ const [ newRoutingTable2 , error2 ] = await this . _fetchRoutingTableUsingKnownRouters (
361
362
knownRouters ,
362
363
currentRoutingTable ,
363
364
bookmarks ,
364
365
impersonatedUser
365
366
)
367
+ newRoutingTable = newRoutingTable2
368
+ error = error2 || error
366
369
}
367
370
368
371
return await this . _applyRoutingTableIfPossible (
369
372
currentRoutingTable ,
370
373
newRoutingTable ,
371
- onDatabaseNameResolved
374
+ onDatabaseNameResolved ,
375
+ error
372
376
)
373
377
}
374
378
@@ -379,7 +383,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
379
383
impersonatedUser ,
380
384
onDatabaseNameResolved
381
385
) {
382
- let newRoutingTable = await this . _fetchRoutingTableUsingKnownRouters (
386
+ let [ newRoutingTable , error ] = await this . _fetchRoutingTableUsingKnownRouters (
383
387
knownRouters ,
384
388
currentRoutingTable ,
385
389
bookmarks ,
@@ -388,7 +392,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
388
392
389
393
if ( ! newRoutingTable ) {
390
394
// none of the known routers returned a valid routing table - try to use seed router address for rediscovery
391
- newRoutingTable = await this . _fetchRoutingTableUsingSeedRouter (
395
+ [ newRoutingTable , error ] = await this . _fetchRoutingTableUsingSeedRouter (
392
396
knownRouters ,
393
397
this . _seedRouter ,
394
398
currentRoutingTable ,
@@ -400,7 +404,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
400
404
return await this . _applyRoutingTableIfPossible (
401
405
currentRoutingTable ,
402
406
newRoutingTable ,
403
- onDatabaseNameResolved
407
+ onDatabaseNameResolved ,
408
+ error
404
409
)
405
410
}
406
411
@@ -410,7 +415,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
410
415
bookmarks ,
411
416
impersonatedUser
412
417
) {
413
- const newRoutingTable = await this . _fetchRoutingTable (
418
+ const [ newRoutingTable , error ] = await this . _fetchRoutingTable (
414
419
knownRouters ,
415
420
currentRoutingTable ,
416
421
bookmarks ,
@@ -419,7 +424,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
419
424
420
425
if ( newRoutingTable ) {
421
426
// one of the known routers returned a valid routing table - use it
422
- return newRoutingTable
427
+ return [ newRoutingTable , null ]
423
428
}
424
429
425
430
// returned routing table was undefined, this means a connection error happened and the last known
@@ -431,7 +436,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
431
436
lastRouterIndex
432
437
)
433
438
434
- return null
439
+ return [ null , error ]
435
440
}
436
441
437
442
async _fetchRoutingTableUsingSeedRouter (
@@ -460,14 +465,14 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
460
465
return [ ] . concat . apply ( [ ] , dnsResolvedAddresses )
461
466
}
462
467
463
- _fetchRoutingTable ( routerAddresses , routingTable , bookmarks , impersonatedUser ) {
468
+ async _fetchRoutingTable ( routerAddresses , routingTable , bookmarks , impersonatedUser ) {
464
469
return routerAddresses . reduce (
465
470
async ( refreshedTablePromise , currentRouter , currentIndex ) => {
466
- const newRoutingTable = await refreshedTablePromise
471
+ const [ newRoutingTable ] = await refreshedTablePromise
467
472
468
473
if ( newRoutingTable ) {
469
474
// valid routing table was fetched - just return it, try next router otherwise
470
- return newRoutingTable
475
+ return [ newRoutingTable , null ]
471
476
} else {
472
477
// returned routing table was undefined, this means a connection error happened and we need to forget the
473
478
// previous router and try the next one
@@ -480,19 +485,19 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
480
485
}
481
486
482
487
// try next router
483
- const session = await this . _createSessionForRediscovery (
488
+ const [ session , error ] = await this . _createSessionForRediscovery (
484
489
currentRouter ,
485
490
bookmarks ,
486
491
impersonatedUser
487
492
)
488
493
if ( session ) {
489
494
try {
490
- return await this . _rediscovery . lookupRoutingTableOnRouter (
495
+ return [ await this . _rediscovery . lookupRoutingTableOnRouter (
491
496
session ,
492
497
routingTable . database ,
493
498
currentRouter ,
494
499
impersonatedUser
495
- )
500
+ ) , null ]
496
501
} catch ( error ) {
497
502
return this . _handleRediscoveryError ( error , currentRouter )
498
503
} finally {
@@ -501,10 +506,10 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
501
506
} else {
502
507
// unable to acquire connection and create session towards the current router
503
508
// return null to signal that the next router should be tried
504
- return null
509
+ return [ null , error ]
505
510
}
506
511
} ,
507
- Promise . resolve ( null )
512
+ Promise . resolve ( [ null , null ] )
508
513
)
509
514
}
510
515
@@ -522,20 +527,20 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
522
527
523
528
const protocolVersion = connection . protocol ( ) . version
524
529
if ( protocolVersion < 4.0 ) {
525
- return new Session ( {
530
+ return [ new Session ( {
526
531
mode : WRITE ,
527
532
bookmarks : Bookmarks . empty ( ) ,
528
533
connectionProvider
529
- } )
534
+ } ) , null ]
530
535
}
531
536
532
- return new Session ( {
537
+ return [ new Session ( {
533
538
mode : READ ,
534
539
database : SYSTEM_DB_NAME ,
535
540
bookmarks,
536
541
connectionProvider,
537
542
impersonatedUser
538
- } )
543
+ } ) , null ]
539
544
} catch ( error ) {
540
545
return this . _handleRediscoveryError ( error , routerAddress )
541
546
}
@@ -548,21 +553,23 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
548
553
// throw when getServers procedure not found because this is clearly a configuration issue
549
554
throw newError (
550
555
`Server at ${ routerAddress . asHostPort ( ) } can't perform routing. Make sure you are connecting to a causal cluster` ,
551
- SERVICE_UNAVAILABLE
556
+ SERVICE_UNAVAILABLE ,
557
+ error
552
558
)
553
559
}
554
560
this . _log . warn (
555
561
`unable to fetch routing table because of an error ${ error } `
556
562
)
557
- return null
563
+ return [ null , error ]
558
564
}
559
565
560
- async _applyRoutingTableIfPossible ( currentRoutingTable , newRoutingTable , onDatabaseNameResolved ) {
566
+ async _applyRoutingTableIfPossible ( currentRoutingTable , newRoutingTable , onDatabaseNameResolved , error ) {
561
567
if ( ! newRoutingTable ) {
562
568
// none of routing servers returned valid routing table, throw exception
563
569
throw newError (
564
570
`Could not perform discovery. No routing servers available. Known routing table: ${ currentRoutingTable } ` ,
565
- SERVICE_UNAVAILABLE
571
+ SERVICE_UNAVAILABLE ,
572
+ error
566
573
)
567
574
}
568
575
0 commit comments