Skip to content

Commit 4d4316e

Browse files
Gather REPLICA tablet types from Vitess only
1 parent 32efe2d commit 4d4316e

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ require (
1313
github.com/patrickmn/go-cache v2.1.0+incompatible
1414
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563
1515
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed // indirect
16+
vitess.io/vitess v2.1.1+incompatible
1617
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1212
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
1313
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
14+
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
1415
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1516
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
1617
github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
@@ -45,6 +46,9 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
4546
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4647
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
4748
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
49+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
4850
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4951
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
5052
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
53+
vitess.io/vitess v2.1.1+incompatible h1:nuuGHiWYWpudD3gOCLeGzol2EJ25e/u5Wer2wV1O130=
54+
vitess.io/vitess v2.1.1+incompatible/go.mod h1:h4qvkyNYTOC0xI+vcidSWoka0gQAZc9ZPHbkHo48gP0=

pkg/throttle/throttler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,14 @@ func (throttler *Throttler) refreshMySQLInventory() error {
327327
if err != nil {
328328
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
329329
}
330-
log.Debugf("Read %+v hosts from vitess %s, %s/%s", len(tablets), clusterSettings.VitessSettings.API, keyspace, shard)
330+
replicas := vitess.FilterReplicaTablets(tablets)
331+
log.Debugf("Read %+v replica hosts from vitess %s, %s/%s", len(replicas), clusterSettings.VitessSettings.API, keyspace, shard)
331332
clusterProbes := &mysql.ClusterProbes{
332333
ClusterName: clusterName,
333334
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
334335
InstanceProbes: mysql.NewProbes(),
335336
}
336-
for _, tablet := range tablets {
337+
for _, tablet := range replicas {
337338
key := mysql.InstanceKey{Hostname: tablet.MysqlHostname, Port: int(tablet.MysqlPort)}
338339
addInstanceKey(&key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
339340
}

pkg/vitess/api_client.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"net/http"
88
"strings"
99
"time"
10+
11+
"vitess.io/vitess/go/vt/proto/topodata"
1012
)
1113

1214
// Tablet represents information about a running instance of vttablet.
1315
type Tablet struct {
14-
MysqlHostname string `json:"mysql_hostname,omitempty"`
15-
MysqlPort int32 `json:"mysql_port,omitempty"`
16+
MysqlHostname string `json:"mysql_hostname,omitempty"`
17+
MysqlPort int32 `json:"mysql_port,omitempty"`
18+
Type topodata.TabletType `json:"type,omitempty"`
1619
}
1720

1821
var httpClient = http.Client{
@@ -30,7 +33,7 @@ func constructAPIURL(api string, keyspace string, shard string) (url string) {
3033
}
3134

3235
// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
33-
// tblet (mysql_hostname, mysql_port) listing
36+
// tablet (mysql_hostname, mysql_port) listing
3437
func ParseTablets(api string, keyspace string, shard string) (tablets []Tablet, err error) {
3538
url := constructAPIURL(api, keyspace, shard)
3639
resp, err := httpClient.Get(url)
@@ -47,3 +50,18 @@ func ParseTablets(api string, keyspace string, shard string) (tablets []Tablet,
4750
err = json.Unmarshal(body, &tablets)
4851
return tablets, err
4952
}
53+
54+
func isReplicaTablet(tablet Tablet) bool {
55+
return tablet.Type == topodata.TabletType_REPLICA
56+
}
57+
58+
// FilterReplicaTablets parses a list of tablets, returning replica tablets only
59+
func FilterReplicaTablets(tablets []Tablet) []Tablet {
60+
replicas := make([]Tablet, 0)
61+
for _, tablet := range tablets {
62+
if isReplicaTablet(tablet) {
63+
replicas = append(replicas, tablet)
64+
}
65+
}
66+
return replicas
67+
}

pkg/vitess/api_client_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package vitess
2+
3+
import (
4+
"testing"
5+
6+
"vitess.io/vitess/go/vt/proto/topodata"
7+
)
8+
9+
func TestFilterReplicaTablets(t *testing.T) {
10+
replicas := FilterReplicaTablets([]Tablet{
11+
{
12+
MysqlHostname: t.Name() + "1",
13+
Type: topodata.TabletType_MASTER,
14+
},
15+
{
16+
MysqlHostname: t.Name() + "2", // this node is valid
17+
Type: topodata.TabletType_REPLICA,
18+
},
19+
{
20+
MysqlHostname: t.Name() + "3",
21+
Type: topodata.TabletType_BACKUP,
22+
},
23+
{
24+
25+
MysqlHostname: t.Name() + "4",
26+
Type: topodata.TabletType_RESTORE,
27+
},
28+
})
29+
if len(replicas) != 1 {
30+
t.Fatalf("Expected 1 replica, got %v", replicas)
31+
}
32+
if replicas[0].MysqlHostname != t.Name()+"2" {
33+
t.Fatalf("Expected hostname %q, got %q", t.Name()+"2", replicas[0].MysqlHostname)
34+
}
35+
}

0 commit comments

Comments
 (0)