Skip to content

Vitess: handle non-200 HTTP status in ParseTablets() #125

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

Merged
merged 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions pkg/vitess/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func filterReplicaTablets(settings config.VitessConfigurationSettings, tablets [
return replicas
}

// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
// ParseTablets reads from vitess /api/keyspace/<keyspace>/tablets/[shard] and returns a
// listing (mysql_hostname, mysql_port, type) of REPLICA tablets
func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet, err error) {
if settings.TimeoutSecs == 0 {
Expand All @@ -90,13 +90,16 @@ func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet
if err != nil {
return tablets, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return tablets, fmt.Errorf("%v", resp.Status)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return tablets, err
}

err = json.Unmarshal(body, &tablets)
return filterReplicaTablets(settings, tablets), err
}
7 changes: 3 additions & 4 deletions pkg/vitess/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestParseTablets(t *testing.T) {
fmt.Fprint(w, string(data))
default:
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "[]")
}
}))
defer vitessApi.Close()
Expand Down Expand Up @@ -125,11 +124,11 @@ func TestParseTablets(t *testing.T) {
Keyspace: "not-found",
Shard: "40-80",
})
if err != nil {
t.Fatalf("Expected no error, got %q", err)
if err == nil || err.Error() != "404 Not Found" {
t.Fatalf("Expected %q error, got %q", "404 Not Found", err)
}

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

Expand Down