Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather REPLICA tablet types from Vitess only #118

Merged
merged 10 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed // indirect
vitess.io/vitess v2.1.1+incompatible
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
Expand Down Expand Up @@ -45,6 +46,9 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
vitess.io/vitess v2.1.1+incompatible h1:nuuGHiWYWpudD3gOCLeGzol2EJ25e/u5Wer2wV1O130=
vitess.io/vitess v2.1.1+incompatible/go.mod h1:h4qvkyNYTOC0xI+vcidSWoka0gQAZc9ZPHbkHo48gP0=
5 changes: 3 additions & 2 deletions pkg/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,14 @@ func (throttler *Throttler) refreshMySQLInventory() error {
if err != nil {
return log.Errorf("Unable to get vitess hosts from %s, %s/%s: %+v", clusterSettings.VitessSettings.API, keyspace, shard, err)
}
log.Debugf("Read %+v hosts from vitess %s, %s/%s", len(tablets), clusterSettings.VitessSettings.API, keyspace, shard)
replicas := vitess.FilterReplicaTablets(tablets)
log.Debugf("Read %+v replica hosts from vitess %s, %s/%s", len(replicas), clusterSettings.VitessSettings.API, keyspace, shard)
clusterProbes := &mysql.ClusterProbes{
ClusterName: clusterName,
IgnoreHostsCount: clusterSettings.IgnoreHostsCount,
InstanceProbes: mysql.NewProbes(),
}
for _, tablet := range tablets {
for _, tablet := range replicas {
key := mysql.InstanceKey{Hostname: tablet.MysqlHostname, Port: int(tablet.MysqlPort)}
addInstanceKey(&key, clusterName, clusterSettings, clusterProbes.InstanceProbes)
}
Expand Down
24 changes: 21 additions & 3 deletions pkg/vitess/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"net/http"
"strings"
"time"

"vitess.io/vitess/go/vt/proto/topodata"
)

// Tablet represents information about a running instance of vttablet.
type Tablet struct {
MysqlHostname string `json:"mysql_hostname,omitempty"`
MysqlPort int32 `json:"mysql_port,omitempty"`
MysqlHostname string `json:"mysql_hostname,omitempty"`
MysqlPort int32 `json:"mysql_port,omitempty"`
Type topodata.TabletType `json:"type,omitempty"`
}

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

// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
// tblet (mysql_hostname, mysql_port) listing
// tablet (mysql_hostname, mysql_port) listing
func ParseTablets(api string, keyspace string, shard string) (tablets []Tablet, err error) {
url := constructAPIURL(api, keyspace, shard)
resp, err := httpClient.Get(url)
Expand All @@ -47,3 +50,18 @@ func ParseTablets(api string, keyspace string, shard string) (tablets []Tablet,
err = json.Unmarshal(body, &tablets)
return tablets, err
}

func isReplicaTablet(tablet Tablet) bool {
return tablet.Type == topodata.TabletType_REPLICA
}

// FilterReplicaTablets parses a list of tablets, returning replica tablets only
func FilterReplicaTablets(tablets []Tablet) []Tablet {
replicas := make([]Tablet, 0)
for _, tablet := range tablets {
if isReplicaTablet(tablet) {
replicas = append(replicas, tablet)
}
}
return replicas
}
35 changes: 35 additions & 0 deletions pkg/vitess/api_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package vitess

import (
"testing"

"vitess.io/vitess/go/vt/proto/topodata"
)

func TestFilterReplicaTablets(t *testing.T) {
replicas := FilterReplicaTablets([]Tablet{
{
MysqlHostname: t.Name() + "1",
Type: topodata.TabletType_MASTER,
},
{
MysqlHostname: t.Name() + "2", // this node is valid
Type: topodata.TabletType_REPLICA,
},
{
MysqlHostname: t.Name() + "3",
Type: topodata.TabletType_BACKUP,
},
{

MysqlHostname: t.Name() + "4",
Type: topodata.TabletType_RESTORE,
},
})
if len(replicas) != 1 {
t.Fatalf("Expected 1 replica, got %v", replicas)
}
if replicas[0].MysqlHostname != t.Name()+"2" {
t.Fatalf("Expected hostname %q, got %q", t.Name()+"2", replicas[0].MysqlHostname)
}
}