@@ -13,6 +13,8 @@ var defaults = require('./defaults')
13
13
var Connection = require ( './connection' )
14
14
const dns = require ( 'dns' )
15
15
const YB_SERVERS_QUERY = 'SELECT * FROM yb_servers()'
16
+ const DEFAULT_FAILED_HOST_TTL_SECONDS = 5
17
+
16
18
class ServerInfo {
17
19
constructor ( hostName , port , placementInfo , public_ip ) {
18
20
this . hostName = hostName
@@ -122,6 +124,8 @@ class Client extends EventEmitter {
122
124
static connectionMap = new Map ( )
123
125
// Map of failedHost -> ServerInfo of host
124
126
static failedHosts = new Map ( )
127
+ // Map of failedHost -> Time at which host was added to failedHosts Map
128
+ static failedHostsTime = new Map ( )
125
129
// Map of placementInfoOfHost -> list of Hosts
126
130
static placementInfoHostMap = new Map ( )
127
131
// Map of Host -> ServerInfo
@@ -155,8 +159,7 @@ class Client extends EventEmitter {
155
159
let leastLoadedHosts = [ ]
156
160
for ( var i = 1 ; i <= Client . topologyKeyMap . size ; i ++ ) {
157
161
let hosts = hostsList . keys ( )
158
- for ( let value of hosts ) {
159
- let host = value
162
+ for ( let host of hosts ) {
160
163
let placementInfoOfHost
161
164
if ( Client . hostServerInfo . has ( host ) ) {
162
165
placementInfoOfHost = Client . hostServerInfo . get ( host ) . placementInfo
@@ -255,6 +258,7 @@ class Client extends EventEmitter {
255
258
let serverInfo = Client . failedHosts . get ( host )
256
259
Client . hostServerInfo . set ( host , serverInfo )
257
260
Client . failedHosts . delete ( host )
261
+ Client . failedHostsTime . delete ( host )
258
262
}
259
263
Client . connectionMap . set ( host , prevCount + 1 )
260
264
}
@@ -359,6 +363,8 @@ class Client extends EventEmitter {
359
363
client . on ( 'error' , ( ) => {
360
364
if ( Client . hostServerInfo . has ( client . host ) ) {
361
365
Client . failedHosts . set ( client . host , Client . hostServerInfo . get ( client . host ) )
366
+ let start = new Date ( ) . getTime ( ) ;
367
+ Client . failedHostsTime . set ( client . host , start )
362
368
Client . connectionMap . delete ( client . host )
363
369
Client . hostServerInfo . delete ( client . host )
364
370
}
@@ -479,9 +485,23 @@ class Client extends EventEmitter {
479
485
}
480
486
481
487
createConnectionMap ( data ) {
488
+ const currConnectionMap = new Map ( Client . connectionMap )
482
489
Client . connectionMap . clear ( )
483
490
data . forEach ( ( eachServer ) => {
484
- Client . connectionMap . set ( eachServer . host , 0 )
491
+ if ( ! Client . failedHosts . has ( eachServer . host ) ) {
492
+ if ( currConnectionMap . has ( eachServer . host ) ) {
493
+ Client . connectionMap . set ( eachServer . host , currConnectionMap . get ( eachServer . host ) )
494
+ } else {
495
+ Client . connectionMap . set ( eachServer . host , 0 )
496
+ }
497
+ } else {
498
+ let start = new Date ( ) . getTime ( ) ;
499
+ if ( start - Client . failedHostsTime . get ( eachServer . host ) > ( DEFAULT_FAILED_HOST_TTL_SECONDS * 1000 ) ) {
500
+ Client . connectionMap . set ( eachServer . host , 0 )
501
+ Client . failedHosts . delete ( eachServer . host )
502
+ Client . failedHostsTime . delete ( eachServer . host )
503
+ }
504
+ }
485
505
} )
486
506
}
487
507
@@ -525,10 +545,13 @@ class Client extends EventEmitter {
525
545
if ( this . connectionParameters . loadBalance ) {
526
546
if ( Client . hostServerInfo . has ( this . host ) ) {
527
547
Client . failedHosts . set ( this . host , Client . hostServerInfo . get ( this . host ) )
548
+ let start = new Date ( ) . getTime ( ) ;
549
+ Client . failedHostsTime . set ( this . host , start )
528
550
Client . connectionMap . delete ( this . host )
529
551
Client . hostServerInfo . delete ( this . host )
530
552
} else if ( Client . failedHosts . has ( this . host ) ) {
531
553
Client . failedHosts . delete ( this . host )
554
+ Client . failedHostsTime . delete ( this . host )
532
555
}
533
556
lock . release ( )
534
557
this . connect ( callback )
@@ -557,10 +580,13 @@ class Client extends EventEmitter {
557
580
if ( this . connectionParameters . loadBalance ) {
558
581
if ( Client . hostServerInfo . has ( this . host ) ) {
559
582
Client . failedHosts . set ( this . host , Client . hostServerInfo . get ( this . host ) )
583
+ let start = new Date ( ) . getTime ( ) ;
584
+ Client . failedHostsTime . set ( this . host , start )
560
585
Client . connectionMap . delete ( this . host )
561
586
Client . hostServerInfo . delete ( this . host )
562
587
} else if ( Client . failedHosts . has ( this . host ) ) {
563
588
Client . failedHosts . delete ( this . host )
589
+ Client . failedHostsTime . delete ( this . host )
564
590
}
565
591
lock . release ( )
566
592
this . connect ( callback )
@@ -580,15 +606,22 @@ class Client extends EventEmitter {
580
606
581
607
updateConnectionMapAfterRefresh ( ) {
582
608
let hostsInfoList = Client . hostServerInfo . keys ( )
583
- for ( let value of hostsInfoList ) {
584
- let eachHost = value
609
+ for ( let eachHost of hostsInfoList ) {
585
610
if ( ! Client . connectionMap . has ( eachHost ) ) {
586
- Client . connectionMap . set ( eachHost , 0 )
611
+ if ( ! Client . failedHosts . has ( eachHost ) ) {
612
+ Client . connectionMap . set ( eachHost , 0 )
613
+ } else {
614
+ let start = new Date ( ) . getTime ( ) ;
615
+ if ( start - Client . failedHostsTime . get ( eachHost ) > ( DEFAULT_FAILED_HOST_TTL_SECONDS * 1000 ) ) {
616
+ Client . connectionMap . set ( eachHost , 0 )
617
+ Client . failedHosts . delete ( eachHost )
618
+ Client . failedHostsTime . delete ( eachHost )
619
+ }
620
+ }
587
621
}
588
622
}
589
623
let connectionMapHostList = Client . connectionMap . keys ( )
590
- for ( let value of connectionMapHostList ) {
591
- let eachHost = value
624
+ for ( let eachHost of connectionMapHostList ) {
592
625
if ( ! Client . hostServerInfo . has ( eachHost ) ) {
593
626
Client . connectionMap . delete ( eachHost )
594
627
}
0 commit comments