Skip to content

Commit 02858e2

Browse files
Added Failed-host-ttl-seconds support (#3)
* Added Failed-host-ttl-seconds support * Update as per review
1 parent 990e27a commit 02858e2

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

packages/pg/lib/client.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var defaults = require('./defaults')
1313
var Connection = require('./connection')
1414
const dns = require('dns')
1515
const YB_SERVERS_QUERY = 'SELECT * FROM yb_servers()'
16+
const DEFAULT_FAILED_HOST_TTL_SECONDS = 5
17+
1618
class ServerInfo {
1719
constructor(hostName, port, placementInfo, public_ip) {
1820
this.hostName = hostName
@@ -122,6 +124,8 @@ class Client extends EventEmitter {
122124
static connectionMap = new Map()
123125
// Map of failedHost -> ServerInfo of host
124126
static failedHosts = new Map()
127+
// Map of failedHost -> Time at which host was added to failedHosts Map
128+
static failedHostsTime = new Map()
125129
// Map of placementInfoOfHost -> list of Hosts
126130
static placementInfoHostMap = new Map()
127131
// Map of Host -> ServerInfo
@@ -155,8 +159,7 @@ class Client extends EventEmitter {
155159
let leastLoadedHosts = []
156160
for (var i = 1; i <= Client.topologyKeyMap.size; i++) {
157161
let hosts = hostsList.keys()
158-
for (let value of hosts) {
159-
let host = value
162+
for (let host of hosts) {
160163
let placementInfoOfHost
161164
if (Client.hostServerInfo.has(host)) {
162165
placementInfoOfHost = Client.hostServerInfo.get(host).placementInfo
@@ -255,6 +258,7 @@ class Client extends EventEmitter {
255258
let serverInfo = Client.failedHosts.get(host)
256259
Client.hostServerInfo.set(host, serverInfo)
257260
Client.failedHosts.delete(host)
261+
Client.failedHostsTime.delete(host)
258262
}
259263
Client.connectionMap.set(host, prevCount + 1)
260264
}
@@ -359,6 +363,8 @@ class Client extends EventEmitter {
359363
client.on('error', () => {
360364
if (Client.hostServerInfo.has(client.host)) {
361365
Client.failedHosts.set(client.host, Client.hostServerInfo.get(client.host))
366+
let start = new Date().getTime();
367+
Client.failedHostsTime.set(client.host, start)
362368
Client.connectionMap.delete(client.host)
363369
Client.hostServerInfo.delete(client.host)
364370
}
@@ -479,9 +485,23 @@ class Client extends EventEmitter {
479485
}
480486

481487
createConnectionMap(data) {
488+
const currConnectionMap = new Map(Client.connectionMap)
482489
Client.connectionMap.clear()
483490
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+
}
485505
})
486506
}
487507

@@ -525,10 +545,13 @@ class Client extends EventEmitter {
525545
if (this.connectionParameters.loadBalance) {
526546
if (Client.hostServerInfo.has(this.host)) {
527547
Client.failedHosts.set(this.host, Client.hostServerInfo.get(this.host))
548+
let start = new Date().getTime();
549+
Client.failedHostsTime.set(this.host, start)
528550
Client.connectionMap.delete(this.host)
529551
Client.hostServerInfo.delete(this.host)
530552
} else if (Client.failedHosts.has(this.host)) {
531553
Client.failedHosts.delete(this.host)
554+
Client.failedHostsTime.delete(this.host)
532555
}
533556
lock.release()
534557
this.connect(callback)
@@ -557,10 +580,13 @@ class Client extends EventEmitter {
557580
if (this.connectionParameters.loadBalance) {
558581
if (Client.hostServerInfo.has(this.host)) {
559582
Client.failedHosts.set(this.host, Client.hostServerInfo.get(this.host))
583+
let start = new Date().getTime();
584+
Client.failedHostsTime.set(this.host, start)
560585
Client.connectionMap.delete(this.host)
561586
Client.hostServerInfo.delete(this.host)
562587
} else if (Client.failedHosts.has(this.host)) {
563588
Client.failedHosts.delete(this.host)
589+
Client.failedHostsTime.delete(this.host)
564590
}
565591
lock.release()
566592
this.connect(callback)
@@ -580,15 +606,22 @@ class Client extends EventEmitter {
580606

581607
updateConnectionMapAfterRefresh() {
582608
let hostsInfoList = Client.hostServerInfo.keys()
583-
for (let value of hostsInfoList) {
584-
let eachHost = value
609+
for (let eachHost of hostsInfoList) {
585610
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+
}
587621
}
588622
}
589623
let connectionMapHostList = Client.connectionMap.keys()
590-
for (let value of connectionMapHostList) {
591-
let eachHost = value
624+
for (let eachHost of connectionMapHostList) {
592625
if (!Client.hostServerInfo.has(eachHost)) {
593626
Client.connectionMap.delete(eachHost)
594627
}

0 commit comments

Comments
 (0)