Skip to content

Commit 42d5227

Browse files
Support polling a specific Vitess cell
1 parent 5f8f6db commit 42d5227

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

pkg/config/mysql_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type MySQLConfigurationSettings struct {
5858
HttpCheckPort int // port for HTTP check. -1 to disable.
5959
HttpCheckPath string // If non-empty, requires HttpCheckPort
6060
IgnoreHosts []string // If non empty, substrings to indicate hosts to be ignored/skipped
61+
VitessCell string // Name of the Vitess cell for polling tablet hosts
6162

6263
Clusters map[string](*MySQLClusterConfigurationSettings) // cluster name -> cluster config
6364
}
@@ -114,6 +115,9 @@ func (settings *MySQLConfigurationSettings) postReadAdjustments() error {
114115
if len(clusterSettings.IgnoreHosts) == 0 {
115116
clusterSettings.IgnoreHosts = settings.IgnoreHosts
116117
}
118+
if !clusterSettings.VitessSettings.IsEmpty() && clusterSettings.VitessSettings.Cell == "" {
119+
clusterSettings.VitessSettings.Cell = settings.VitessCell
120+
}
117121
}
118122
return nil
119123
}

pkg/config/vitess_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package config
66

77
type VitessConfigurationSettings struct {
88
API string
9+
Cell string
910
Keyspace string
1011
Shard string
1112
TimeoutSecs uint

pkg/vitess/api_client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const defaultTimeout = time.Duration(5) * time.Second
1616

1717
// Tablet represents information about a running instance of vttablet.
1818
type Tablet struct {
19-
MysqlHostname string `json:"mysql_hostname,omitempty"`
20-
MysqlPort int32 `json:"mysql_port,omitempty"`
21-
Type topodata.TabletType `json:"type,omitempty"`
19+
Alias *topodata.TabletAlias `json:"alias,omitempty"`
20+
MysqlHostname string `json:"mysql_hostname,omitempty"`
21+
MysqlPort int32 `json:"mysql_port,omitempty"`
22+
Type topodata.TabletType `json:"type,omitempty"`
2223
}
2324

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

4344
// filterReplicaTablets parses a list of tablets, returning replica tablets only
44-
func filterReplicaTablets(tablets []Tablet) (replicas []Tablet) {
45+
func filterReplicaTablets(settings config.VitessConfigurationSettings, tablets []Tablet) (replicas []Tablet) {
4546
for _, tablet := range tablets {
47+
if settings.Cell != "" && tablet.Alias.Cell != settings.Cell {
48+
continue
49+
}
4650
if tablet.IsValidReplica() {
4751
replicas = append(replicas, tablet)
4852
}
@@ -72,5 +76,5 @@ func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet
7276
}
7377

7478
err = json.Unmarshal(body, &tablets)
75-
return filterReplicaTablets(tablets), err
79+
return filterReplicaTablets(settings, tablets), err
7680
}

pkg/vitess/api_client_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,37 @@ func TestParseTablets(t *testing.T) {
1818
case "/api/keyspace/test/tablets/00":
1919
data, _ := json.Marshal([]Tablet{
2020
{
21+
Alias: &topodata.TabletAlias{Cell: "ash1"},
2122
MysqlHostname: "master",
2223
Type: topodata.TabletType_MASTER,
2324
},
2425
{
26+
Alias: &topodata.TabletAlias{Cell: "ac4"},
2527
MysqlHostname: "replica",
2628
Type: topodata.TabletType_REPLICA,
2729
},
2830
{
31+
Alias: &topodata.TabletAlias{Cell: "va3"},
32+
MysqlHostname: "replica",
33+
Type: topodata.TabletType_REPLICA,
34+
},
35+
{
36+
Alias: &topodata.TabletAlias{Cell: "ac4"},
2937
MysqlHostname: "spare",
3038
Type: topodata.TabletType_SPARE,
3139
},
3240
{
41+
Alias: &topodata.TabletAlias{Cell: "va3"},
3342
MysqlHostname: "batch",
3443
Type: topodata.TabletType_BATCH,
3544
},
3645
{
46+
Alias: &topodata.TabletAlias{Cell: "ac4"},
3747
MysqlHostname: "backup",
3848
Type: topodata.TabletType_BACKUP,
3949
},
4050
{
41-
51+
Alias: &topodata.TabletAlias{Cell: "ash1"},
4252
MysqlHostname: "restore",
4353
Type: topodata.TabletType_RESTORE,
4454
},
@@ -62,8 +72,8 @@ func TestParseTablets(t *testing.T) {
6272
t.Fatalf("Expected no error, got %q", err)
6373
}
6474

65-
if len(tablets) != 1 {
66-
t.Fatalf("Expected 1 tablet, got %d", len(tablets))
75+
if len(tablets) != 2 {
76+
t.Fatalf("Expected 2 tablets, got %d", len(tablets))
6777
}
6878

6979
if tablets[0].MysqlHostname != "replica" {
@@ -75,6 +85,26 @@ func TestParseTablets(t *testing.T) {
7585
}
7686
})
7787

88+
t.Run("with-cell", func(t *testing.T) {
89+
tablets, err := ParseTablets(config.VitessConfigurationSettings{
90+
API: vitessApi.URL,
91+
Cell: "ac4",
92+
Keyspace: "test",
93+
Shard: "00",
94+
})
95+
if err != nil {
96+
t.Fatalf("Expected no error, got %q", err)
97+
}
98+
99+
if len(tablets) > 1 {
100+
t.Fatalf("Expected 1 tablet, got %d", len(tablets))
101+
}
102+
103+
if tablets[0].Alias.Cell != "ac4" {
104+
t.Fatalf("Expected vitess cell %s, got %s", "ac4", tablets[0].Alias.Cell)
105+
}
106+
})
107+
78108
t.Run("not-found", func(t *testing.T) {
79109
tablets, err := ParseTablets(config.VitessConfigurationSettings{
80110
API: vitessApi.URL,

0 commit comments

Comments
 (0)