-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_balance.go
572 lines (531 loc) · 16.7 KB
/
load_balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package pgx
import (
"context"
"errors"
"fmt"
"log"
"math"
"math/rand"
"net"
"regexp"
"strings"
"time"
)
const NO_SERVERS_MSG = "could not find a server to connect to"
const MAX_RETRIES = 20
const REFRESH_INTERVAL_SECONDS = 300
const DEFAULT_FAILED_HOST_RECONNECT_DELAY_SECS = 5
const MAX_FAILED_HOST_RECONNECT_DELAY_SECS = 60
const MAX_INTERVAL_SECONDS = 600
const MAX_PREFERENCE_VALUE = 10
const CONTROL_CONN_TIMEOUT = 15 * time.Second
var ErrFallbackToOriginalBehaviour = errors.New("no preferred server available, fallback-to-topology-keys-only is set to true so falling back to original behaviour")
// -- Values for ClusterLoadInfo.flags --
// Use private address (host) of tservers to create a connection
const USE_HOSTS byte = 0
// Use public address (public_ip) of tservers to create a connection
const USE_PUBLIC_IP byte = 1
// Try both the addresses (host, public_ip) of tservers to create a connection
const TRY_HOSTS_PUBLIC_IP byte = 2
// Both the addresses (host, public_ip) of tserver to be tried, but no success with private addresses to create a connection
const HOSTS_EXHAUSTED byte = 3
// Indicate to the Go routine processing the requestChan that it should return a least loaded tserver host, port
const GET_LB_CONN byte = 4
// Indicate to the Go routine processing the requestChan that it should decrease the connection count for the given host by one
const DECREMENT_COUNT byte = 5
type ClusterLoadInfo struct {
clusterName string
ctx context.Context
config *ConnConfig
controlConn *Conn
ctrlCtx context.Context
lastRefresh time.Time
// map of host -> connection count
hostLoad map[string]int
// map of host -> port
hostPort map[string]uint16
// map of "cloud.region.zone" -> slice of hostnames
zoneList map[string][]string
// map of host -> int
unavailableHosts map[string]int64
// map of (private -> public) address of a node.
hostPairs map[string]string
flags byte
}
type lbHost struct {
hostname string
port uint16
err error
}
var clustersLoadInfo map[string]*ClusterLoadInfo
const LB_QUERY = "SELECT host,port,num_connections,node_type,cloud,region,zone,public_ip FROM yb_servers()"
// Only the Go routine spawned in init() reads this channel. Based on the flag, it
// - returns the least loaded tserver's host/port (GET_LB_CONN)
// - decrements connection count by one for closed connection (DECREMENT_COUNT)
var requestChan chan *ClusterLoadInfo
// Only the Go routine spawned in init() writes to this channel.
// It returns the least loaded tserver's host/port if successful else err
var hostChan chan *lbHost
func NewClusterLoadInfo(ctx context.Context, config *ConnConfig) *ClusterLoadInfo {
info := new(ClusterLoadInfo)
info.clusterName = LookupIP(config.Host)
info.ctx = ctx
info.config = config
info.flags = GET_LB_CONN
return info
}
func LookupIP(host string) string {
addrs, err := net.LookupHost(host)
if err == nil {
for _, addr := range addrs {
if strings.Contains(addr, ".") {
return addr
}
}
if len(addrs) > 0 {
return addrs[0]
}
}
return host
}
func init() {
clustersLoadInfo = make(map[string]*ClusterLoadInfo)
requestChan = make(chan *ClusterLoadInfo)
hostChan = make(chan *lbHost)
go produceHostName(requestChan, hostChan)
}
func replaceHostString(connString string, newHost string, port uint16) string {
newConnString := connString
if strings.HasPrefix(connString, "postgres://") || strings.HasPrefix(connString, "postgresql://") {
if strings.Contains(connString, "@") {
pattern := regexp.MustCompile("@([^/]*)/")
// todo IPv6 handling
newConnString = pattern.ReplaceAllString(connString, fmt.Sprintf("@%s:%d/", newHost, port))
} else {
pattern := regexp.MustCompile("://([^/]*)/")
newConnString = pattern.ReplaceAllString(connString, fmt.Sprintf("://%s:%d/", newHost, port))
}
} else { // key = value (DSN style)
pattern := regexp.MustCompile("host=([^ ]*) ")
newConnString = pattern.ReplaceAllString(connString, fmt.Sprintf("host=%s ", newHost))
pattern = regexp.MustCompile("port=([^ ]*) ")
newConnString = pattern.ReplaceAllString(newConnString, fmt.Sprintf("port=%d ", port))
}
return newConnString
}
func produceHostName(in chan *ClusterLoadInfo, out chan *lbHost) {
for {
new, present := <-in
if !present {
log.Println("The requestChannel is closed, load_balance feature will not work")
break
}
if new.flags == DECREMENT_COUNT {
names := strings.Split(new.clusterName, ",")
if len(names) != 2 {
log.Printf("cannot parse names to update connection count: %s", new.clusterName)
} else {
cli, ok := clustersLoadInfo[LookupIP(names[0])]
if ok {
cnt := cli.hostLoad[names[1]]
if cnt == 0 {
log.Printf("connection count for %s going negative!", names[1])
}
cli.hostLoad[names[1]] = cnt - 1
}
}
continue
}
old, present := clustersLoadInfo[new.clusterName]
if !present {
// There is no loadInfo available for this config. Create one.
err := refreshLoadInfo(new)
if err != nil {
lb := &lbHost{
hostname: "",
err: err,
}
out <- lb
continue
}
publicIpAvailable := false
for k, v := range new.hostPairs {
if v != "" {
publicIpAvailable = true
}
if new.clusterName == k {
new.flags = USE_HOSTS
break
} else if new.clusterName == v {
new.flags = USE_PUBLIC_IP
break
} else {
new.flags = TRY_HOSTS_PUBLIC_IP
}
}
if !publicIpAvailable {
new.flags = USE_HOSTS
}
clustersLoadInfo[new.clusterName] = new
out <- getHostWithLeastConns(new)
// continue
} else {
old.config.topologyKeys = new.config.topologyKeys // Use the provided topology-keys.
old.config.fallbackToTopologyKeysOnly = new.config.fallbackToTopologyKeysOnly
old.config.failedHostReconnectDelaySecs = new.config.failedHostReconnectDelaySecs
old.config.connString = new.config.connString
out <- refreshAndGetLeastLoadedHost(old, new.unavailableHosts)
// continue
}
}
}
func connectLoadBalanced(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
newLoadInfo := NewClusterLoadInfo(ctx, config)
requestChan <- newLoadInfo
lbHost := <-hostChan
if lbHost.err != nil {
return connect(ctx, config) // fallback to original behaviour
}
if lbHost.hostname == config.Host {
return connectWithRetries(ctx, config.controlHost, config, newLoadInfo, lbHost)
} else {
newConnString := replaceHostString(config.connString, lbHost.hostname, lbHost.port)
newConfig, err := ParseConfig(newConnString)
if err != nil {
return nil, err
}
newConfig.Port = lbHost.port
newConfig.controlHost = config.controlHost
newConfig.TLSConfig = config.TLSConfig
return connectWithRetries(ctx, config.controlHost, newConfig, newLoadInfo, lbHost)
}
}
func connectWithRetries(ctx context.Context, controlHost string, newConfig *ConnConfig,
newLoadInfo *ClusterLoadInfo, lbHost *lbHost) (c *Conn, er error) {
var timeout time.Duration = 0
if ctxDeadline, ok := ctx.Deadline(); ok {
timeout = time.Until(ctxDeadline)
}
conn, err := connect(ctx, newConfig)
for i := 0; i < MAX_RETRIES && err != nil; i++ {
decrementConnCount(newConfig.controlHost + "," + newConfig.Host)
log.Printf("Adding %s to unavailableHosts due to %s", newConfig.Host, err.Error())
newLoadInfo.unavailableHosts = map[string]int64{lbHost.hostname: time.Now().Unix()}
requestChan <- newLoadInfo
lbHost = <-hostChan
if lbHost.err != nil {
return nil, lbHost.err
}
if timeout > 0 {
ctx, _ = context.WithTimeout(context.Background(), timeout)
} else {
ctx = context.Background()
}
if lbHost.hostname == newConfig.Host {
conn, err = connect(ctx, newConfig)
} else {
newConnString := strings.Replace(newConfig.connString, newConfig.Host, lbHost.hostname, -1)
oldTLSConfig := newConfig.TLSConfig
newConfig, err = ParseConfig(newConnString)
if err != nil {
return nil, err
}
newConfig.Port = lbHost.port
newConfig.controlHost = controlHost
newConfig.TLSConfig = oldTLSConfig
conn, err = connect(ctx, newConfig)
}
}
if err != nil {
decrementConnCount(newConfig.controlHost + "," + newConfig.Host)
}
return conn, err
}
func decrementConnCount(str string) {
requestChan <- &ClusterLoadInfo{
clusterName: str,
flags: DECREMENT_COUNT,
}
}
func markHostAway(li *ClusterLoadInfo, h string) {
log.Printf("Marking host %s as unreachable", h)
delete(li.hostLoad, h)
delete(li.hostPairs, h)
if li.unavailableHosts == nil {
li.unavailableHosts = make(map[string]int64)
}
li.unavailableHosts[h] = time.Now().Unix()
}
func refreshLoadInfo(li *ClusterLoadInfo) error {
li.ctrlCtx, _ = context.WithTimeout(context.Background(), CONTROL_CONN_TIMEOUT)
if li.controlConn == nil || li.controlConn.IsClosed() {
var err error
ctrlConfig, err := ParseConfig(li.config.connString)
if err != nil {
log.Printf("refreshLoadInfo(): ParseConfig for control connection failed, %s", err.Error())
return err
}
li.config = ctrlConfig
li.config.Host = LookupIP(li.config.Host)
li.config.ConnectTimeout = CONTROL_CONN_TIMEOUT
li.controlConn, err = connect(li.ctrlCtx, li.config)
if err != nil {
log.Printf("Could not create control connection to %s\n", li.config.Host)
// remove its hostLoad entry
markHostAway(li, li.config.Host)
li.controlConn = nil
// Attempt connection to other servers which are already fetched in cli.
if len(li.hostPairs) > 0 {
log.Printf("Attempting control connection to %d other servers ...\n", len(li.hostPairs))
}
for h := range li.hostPairs {
newConnString := replaceHostString(li.config.connString, h, li.hostPort[h])
if li.config, err = ParseConfig(newConnString); err == nil {
li.ctrlCtx, _ = context.WithTimeout(context.Background(), CONTROL_CONN_TIMEOUT)
if li.controlConn, err = connect(li.ctrlCtx, li.config); err == nil {
log.Printf("Created control connection to host %s", h)
break
}
log.Printf("Could not create control connection to host %s", h)
markHostAway(li, li.config.Host)
li.controlConn = nil
}
}
if err != nil {
log.Printf("Failed to create control connection")
return err
}
}
}
// defer li.controlConn.Close(li.ctrlCtx)
rows, err := li.controlConn.Query(li.ctrlCtx, LB_QUERY)
if err != nil {
log.Printf("Could not query load information: %s", err.Error())
markHostAway(li, li.config.Host)
li.controlConn = nil
return refreshLoadInfo(li)
}
defer rows.Close()
var host, nodeType, cloud, region, zone, publicIP string
var port, numConns int
newHostLoad := make(map[string]int)
newHostPort := make(map[string]uint16)
newZoneList := make(map[string][]string)
newHostPairs := make(map[string]string)
if li.unavailableHosts == nil {
li.unavailableHosts = make(map[string]int64)
}
for rows.Next() {
err := rows.Scan(&host, &port, &numConns, &nodeType, &cloud, ®ion, &zone, &publicIP)
if err != nil {
log.Printf("Could not read load information: %s", err.Error())
markHostAway(li, li.config.Host)
li.controlConn = nil
return refreshLoadInfo(li)
} else {
host = LookupIP(host)
publicIP = LookupIP(publicIP)
newHostPairs[host] = publicIP
tk := cloud + "." + region + "." + zone
tk_star := cloud + "." + region // Used for topology_keys of type: cloud.region.*
hosts, ok := newZoneList[tk]
if !ok {
hosts = make([]string, 0)
}
hosts_star, ok_star := newZoneList[tk_star]
if !ok_star {
hosts_star = make([]string, 0)
}
hosts = append(hosts, host)
hosts_star = append(hosts_star, host)
newZoneList[tk] = hosts
newZoneList[tk_star] = hosts_star
cnt := li.hostLoad[host]
newHostLoad[host] = cnt
newHostPort[host] = uint16(port)
}
}
rsError := rows.Err()
if rsError != nil {
log.Printf("refreshLoadInfo(): Could not read load information, Rows.Err(): %s", rsError.Error())
markHostAway(li, li.config.Host)
li.controlConn = nil
return refreshLoadInfo(li)
}
li.hostPort = newHostPort
li.zoneList = newZoneList
li.hostPairs = newHostPairs
li.hostLoad = newHostLoad
li.lastRefresh = time.Now()
for uh, t := range li.unavailableHosts {
if time.Now().Unix()-t > li.config.failedHostReconnectDelaySecs {
// clear the unavailable-hosts list
log.Printf("Removing %s from unavailableHosts Map", uh)
li.hostLoad[uh] = 0
delete(li.unavailableHosts, uh)
}
}
return nil
}
func getHostWithLeastConns(li *ClusterLoadInfo) *lbHost {
leastCnt := int(math.MaxInt64)
leastLoaded := ""
leastLoadedservers := make([]string, 0)
if li.config.topologyKeys != nil {
for i := 0; i < len(li.config.topologyKeys); i++ {
var servers []string
for _, tk := range li.config.topologyKeys[i] {
toCheckStar := strings.Split(tk, ".")
if toCheckStar[2] == "*" {
tk = toCheckStar[0] + "." + toCheckStar[1]
}
servers = append(servers, li.zoneList[tk]...)
}
for _, h := range servers {
if !isHostAway(li, h) {
if li.hostLoad[h] < leastCnt {
leastLoadedservers = nil
leastLoadedservers = append(leastLoadedservers, h)
leastCnt = li.hostLoad[h]
} else if li.hostLoad[h] == leastCnt {
leastLoadedservers = append(leastLoadedservers, h)
}
}
}
if leastCnt != int(math.MaxInt64) && len(leastLoadedservers) != 0 {
break
}
}
}
if leastCnt == int(math.MaxInt64) && len(leastLoadedservers) == 0 {
if li.config.topologyKeys == nil || !li.config.fallbackToTopologyKeysOnly {
for h := range li.hostLoad {
if !isHostAway(li, h) {
if li.hostLoad[h] < leastCnt {
leastLoadedservers = nil
leastLoadedservers = append(leastLoadedservers, h)
leastCnt = li.hostLoad[h]
} else if li.hostLoad[h] == leastCnt {
leastLoadedservers = append(leastLoadedservers, h)
}
}
}
} else {
lbh := &lbHost{
err: ErrFallbackToOriginalBehaviour,
}
return lbh
}
}
if len(leastLoadedservers) != 0 {
rand.Seed(time.Now().UnixNano())
leastLoaded = leastLoadedservers[rand.Intn(len(leastLoadedservers))]
}
if leastLoaded == "" {
if li.flags == TRY_HOSTS_PUBLIC_IP {
// remove all host (private ips) from unavailable list
for h := range li.hostPairs {
delete(li.unavailableHosts, h)
}
li.flags = HOSTS_EXHAUSTED
return getHostWithLeastConns(li)
}
lbh := &lbHost{
hostname: "",
err: errors.New(NO_SERVERS_MSG),
}
log.Println("No hosts found, returning with NO_SERVERS_MSG")
return lbh
}
leastLoadedToUse := leastLoaded
if li.flags == USE_PUBLIC_IP || li.flags == HOSTS_EXHAUSTED {
leastLoadedToUse = li.hostPairs[leastLoaded]
if leastLoadedToUse == "" {
lbh := &lbHost{
hostname: "",
err: errors.New(NO_SERVERS_MSG),
}
log.Println("No hosts and public ip found, returning with NO_SERVERS_MSG")
return lbh
}
}
lbh := &lbHost{
hostname: leastLoadedToUse,
port: li.hostPort[leastLoaded],
err: nil,
}
li.hostLoad[leastLoadedToUse] = leastCnt + 1
return lbh
}
func isHostAway(li *ClusterLoadInfo, h string) bool {
for awayHost := range li.unavailableHosts {
if h == awayHost || h == li.hostPairs[awayHost] {
return true
}
}
return false
}
func refreshAndGetLeastLoadedHost(li *ClusterLoadInfo, awayHosts map[string]int64) *lbHost {
if time.Now().Unix()-li.lastRefresh.Unix() > li.config.refreshInterval {
err := refreshLoadInfo(li)
if err != nil {
return &lbHost{
hostname: "",
err: err,
}
}
}
for h := range awayHosts {
li.unavailableHosts[h] = awayHosts[h]
}
return getHostWithLeastConns(li)
}
// expects the toplogykeys in the format 'cloud1.region1.zone1,cloud1.region1.zone2,...'
func validateTopologyKeys(s string) ([]string, error) {
tkeys := strings.Split(s, ",")
for _, tk := range tkeys {
zones1 := strings.Split(tk, ".")
zones2 := strings.Split(tk, ":")
if len(zones1) != 3 || len(zones2) > 2 {
return nil, errors.New("toplogy_keys '" + s +
"' not in correct format, should be specified as '<cloud>.<region>.<zone>,...'")
}
}
return tkeys, nil
}
// For test purpose
func GetHostLoad() map[string]map[string]int {
hl := make(map[string]map[string]int)
for cluster := range clustersLoadInfo {
hl[cluster] = make(map[string]int)
for host, cnt := range clustersLoadInfo[cluster].hostLoad {
hl[cluster][host] = cnt
}
}
return hl
}
// For test purpose
func GetAZInfo() map[string]map[string][]string {
az := make(map[string]map[string][]string)
for n, cli := range clustersLoadInfo {
az[n] = make(map[string][]string)
for z, hosts := range cli.zoneList {
q := strings.Split(z, ".")
if len(q) == 3 {
newzl := make([]string, len(hosts))
copy(newzl, hosts)
az[n][z] = newzl
}
}
}
return az
}
// For test purpose
func EmptyHostLoad() map[string]map[string]int {
for cluster := range clustersLoadInfo {
for host := range clustersLoadInfo[cluster].hostLoad {
delete(clustersLoadInfo[cluster].hostLoad, host)
}
}
return nil
}