Skip to content
4 changes: 4 additions & 0 deletions pkg/config/mysql_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type MySQLConfigurationSettings struct {
HttpCheckPort int // port for HTTP check. -1 to disable.
HttpCheckPath string // If non-empty, requires HttpCheckPort
IgnoreHosts []string // If non empty, substrings to indicate hosts to be ignored/skipped
VitessCell string // Name of the Vitess cell for polling tablet hosts

Clusters map[string](*MySQLClusterConfigurationSettings) // cluster name -> cluster config
}
Expand Down Expand Up @@ -114,6 +115,9 @@ func (settings *MySQLConfigurationSettings) postReadAdjustments() error {
if len(clusterSettings.IgnoreHosts) == 0 {
clusterSettings.IgnoreHosts = settings.IgnoreHosts
}
if !clusterSettings.VitessSettings.IsEmpty() && clusterSettings.VitessSettings.Cell == "" {
clusterSettings.VitessSettings.Cell = settings.VitessCell
}
}
return nil
}
1 change: 1 addition & 0 deletions pkg/config/vitess_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config

type VitessConfigurationSettings struct {
API string
Cell string
Keyspace string
Shard string
TimeoutSecs uint
Expand Down
14 changes: 9 additions & 5 deletions pkg/vitess/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const defaultTimeout = time.Duration(5) * time.Second

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

// IsValidReplica returns a bool reflecting if a tablet type is REPLICA
Expand All @@ -41,8 +42,11 @@ func constructAPIURL(settings config.VitessConfigurationSettings) (url string) {
}

// filterReplicaTablets parses a list of tablets, returning replica tablets only
func filterReplicaTablets(tablets []Tablet) (replicas []Tablet) {
func filterReplicaTablets(settings config.VitessConfigurationSettings, tablets []Tablet) (replicas []Tablet) {
for _, tablet := range tablets {
if settings.Cell != "" && tablet.Alias.Cell != settings.Cell {
continue
}
if tablet.IsValidReplica() {
replicas = append(replicas, tablet)
}
Expand Down Expand Up @@ -72,5 +76,5 @@ func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet
}

err = json.Unmarshal(body, &tablets)
return filterReplicaTablets(tablets), err
return filterReplicaTablets(settings, tablets), err
}
49 changes: 43 additions & 6 deletions pkg/vitess/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,37 @@ func TestParseTablets(t *testing.T) {
case "/api/keyspace/test/tablets/00":
data, _ := json.Marshal([]Tablet{
{
Alias: &topodata.TabletAlias{Cell: "ash1"},
MysqlHostname: "master",
Type: topodata.TabletType_MASTER,
},
{
MysqlHostname: "replica",
Alias: &topodata.TabletAlias{Cell: "ac4"},
MysqlHostname: "replica1",
Type: topodata.TabletType_REPLICA,
},
{
Alias: &topodata.TabletAlias{Cell: "va3"},
MysqlHostname: "replica2",
Type: topodata.TabletType_REPLICA,
},
{
Alias: &topodata.TabletAlias{Cell: "ac4"},
MysqlHostname: "spare",
Type: topodata.TabletType_SPARE,
},
{
Alias: &topodata.TabletAlias{Cell: "va3"},
MysqlHostname: "batch",
Type: topodata.TabletType_BATCH,
},
{
Alias: &topodata.TabletAlias{Cell: "ac4"},
MysqlHostname: "backup",
Type: topodata.TabletType_BACKUP,
},
{

Alias: &topodata.TabletAlias{Cell: "ash1"},
MysqlHostname: "restore",
Type: topodata.TabletType_RESTORE,
},
Expand All @@ -62,19 +72,46 @@ func TestParseTablets(t *testing.T) {
t.Fatalf("Expected no error, got %q", err)
}

if len(tablets) != 1 {
t.Fatalf("Expected 1 tablet, got %d", len(tablets))
if len(tablets) != 2 {
t.Fatalf("Expected 2 tablets, got %d", len(tablets))
}

if tablets[0].MysqlHostname != "replica" {
t.Fatalf("Expected hostname %q, got %q", "replica", tablets[0].MysqlHostname)
if tablets[0].MysqlHostname != "replica1" {
t.Fatalf("Expected hostname %q, got %q", "replica1", tablets[0].MysqlHostname)
}
if tablets[1].MysqlHostname != "replica2" {
t.Fatalf("Expected hostname %q, got %q", "replica2", tablets[1].MysqlHostname)
}

if httpClient.Timeout != time.Second {
t.Fatalf("Expected vitess client timeout of %v, got %v", time.Second, httpClient.Timeout)
}
})

t.Run("with-cell", func(t *testing.T) {
tablets, err := ParseTablets(config.VitessConfigurationSettings{
API: vitessApi.URL,
Cell: "ac4",
Keyspace: "test",
Shard: "00",
})
if err != nil {
t.Fatalf("Expected no error, got %q", err)
}

if len(tablets) != 1 {
t.Fatalf("Expected 1 tablet, got %d", len(tablets))
}

if tablets[0].MysqlHostname != "replica1" {
t.Fatalf("Expected hostname %q, got %q", "replica1", tablets[0].MysqlHostname)
}
if tablets[0].Alias.Cell != "ac4" {
t.Fatalf("Expected vitess cell %s, got %s", "ac4", tablets[0].Alias.Cell)
}

})

t.Run("not-found", func(t *testing.T) {
tablets, err := ParseTablets(config.VitessConfigurationSettings{
API: vitessApi.URL,
Expand Down