Skip to content

Commit def45d7

Browse files
committed
add num_backup_conns option to force discovery of non-zone-local vtgates
1 parent 1b6038a commit def45d7

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

go/vt/vtgateproxy/discovery.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type JSONGateResolverBuilder struct {
8484
affinityField string
8585
affinityValue string
8686
numConnections int
87+
numBackupConns int
8788

8889
mu sync.RWMutex
8990
targets map[string][]targetHost
@@ -115,6 +116,7 @@ func RegisterJSONGateResolver(
115116
affinityField string,
116117
affinityValue string,
117118
numConnections int,
119+
numBackupConns int,
118120
) (*JSONGateResolverBuilder, error) {
119121
jsonDiscovery := &JSONGateResolverBuilder{
120122
targets: map[string][]targetHost{},
@@ -125,6 +127,7 @@ func RegisterJSONGateResolver(
125127
affinityField: affinityField,
126128
affinityValue: affinityValue,
127129
numConnections: numConnections,
130+
numBackupConns: numBackupConns,
128131
sorter: newShuffleSorter(),
129132
}
130133

@@ -265,7 +268,7 @@ func (b *JSONGateResolverBuilder) parse() (bool, error) {
265268
return false, fmt.Errorf("error parsing JSON discovery file %s: %v", b.jsonPath, err)
266269
}
267270

268-
var targets = map[string][]targetHost{}
271+
var allTargets = map[string][]targetHost{}
269272
for _, host := range hosts {
270273
hostname, hasHostname := host["host"]
271274
address, hasAddress := host[b.addressField]
@@ -312,7 +315,7 @@ func (b *JSONGateResolverBuilder) parse() (bool, error) {
312315
}
313316

314317
target := targetHost{hostname.(string), fmt.Sprintf("%s:%s", address, port), poolType.(string), affinity.(string), affinity == b.affinityValue}
315-
targets[target.PoolType] = append(targets[target.PoolType], target)
318+
allTargets[target.PoolType] = append(allTargets[target.PoolType], target)
316319
}
317320

318321
// If a pool disappears, the metric will not record this unless all counts
@@ -322,16 +325,25 @@ func (b *JSONGateResolverBuilder) parse() (bool, error) {
322325
// targets and only resetting pools which disappear.
323326
targetCount.ResetAll()
324327

325-
for poolType := range targets {
326-
b.sorter.shuffleSort(targets[poolType])
327-
if len(targets[poolType]) > *numConnections {
328-
targets[poolType] = targets[poolType][:b.numConnections]
328+
var selected = map[string][]targetHost{}
329+
330+
for poolType := range allTargets {
331+
b.sorter.shuffleSort(allTargets[poolType])
332+
333+
// try to pick numConnections from the front of the list (local zone) and numBackupConnections
334+
// from the tail (remote zone). if that's not possible, just take the whole set
335+
if len(allTargets[poolType]) >= b.numConnections+b.numBackupConns {
336+
remoteOffset := len(allTargets[poolType]) - b.numBackupConns
337+
selected[poolType] = append(allTargets[poolType][:b.numConnections], allTargets[poolType][remoteOffset:]...)
338+
} else {
339+
selected[poolType] = allTargets[poolType]
329340
}
330-
targetCount.Set(poolType, int64(len(targets[poolType])))
341+
342+
targetCount.Set(poolType, int64(len(selected[poolType])))
331343
}
332344

333345
b.mu.Lock()
334-
b.targets = targets
346+
b.targets = selected
335347
b.mu.Unlock()
336348

337349
return true, nil

go/vt/vtgateproxy/vtgateproxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const (
5353
var (
5454
vtgateHostsFile = flag.String("vtgate_hosts_file", "", "json file describing the host list to use for vtgate:// resolution")
5555
numConnections = flag.Int("num_connections", 4, "number of outbound GPRC connections to maintain")
56+
numBackupConns = flag.Int("num_backup_conns", 1, "number of backup remote-zone GPRC connections to maintain")
5657
poolTypeField = flag.String("pool_type_field", "", "Field name used to specify the target vtgate type and filter the hosts")
5758
affinityField = flag.String("affinity_field", "", "Attribute (JSON file) used to specify the routing affinity , e.g. 'az_id'")
5859
affinityValue = flag.String("affinity_value", "", "Value to match for routing affinity , e.g. 'use-az1'")
@@ -233,6 +234,7 @@ func Init() {
233234
*affinityField,
234235
*affinityValue,
235236
*numConnections,
237+
*numBackupConns,
236238
)
237239

238240
if err != nil {

0 commit comments

Comments
 (0)