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

/healthz should not report ok when vttablet is not connected to mysql #8238

Merged
merged 1 commit into from
Jun 2, 2021
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
28 changes: 15 additions & 13 deletions go/vt/vttablet/tabletserver/tabletserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,19 +1534,21 @@ var okMessage = []byte("ok\n")
// Health check
// Returns ok if we are in the desired serving state
func (tsv *TabletServer) registerHealthzHealthHandler() {
tsv.exporter.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.MONITORING); err != nil {
acl.SendError(w, err)
return
}
if tsv.sm.wantState == StateServing && !tsv.sm.IsServing() {
http.Error(w, "500 internal server error: vttablet is not serving", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%v", len(okMessage)))
w.WriteHeader(http.StatusOK)
w.Write(okMessage)
})
tsv.exporter.HandleFunc("/healthz", tsv.healthzHandler)
}

func (tsv *TabletServer) healthzHandler(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.MONITORING); err != nil {
acl.SendError(w, err)
return
}
if (tsv.sm.wantState == StateServing || tsv.sm.wantState == StateNotConnected) && !tsv.sm.IsServing() {
http.Error(w, "500 internal server error: vttablet is not serving", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%v", len(okMessage)))
w.WriteHeader(http.StatusOK)
w.Write(okMessage)
}

// Query service health check
Expand Down
58 changes: 58 additions & 0 deletions go/vt/vttablet/tabletserver/tabletserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -56,6 +58,62 @@ import (
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)

func TestTabletServerHealthz(t *testing.T) {
db, tsv := setupTabletServerTest(t, "")
defer tsv.StopService()
defer db.Close()

req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(tsv.healthzHandler)
handler.ServeHTTP(rr, req)

expectedCode := http.StatusOK
if status := rr.Code; status != expectedCode {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expectedCode)
}

expected := "ok\n"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}

func TestTabletServerHealthzNotConnected(t *testing.T) {
db, tsv := setupTabletServerTest(t, "")
defer tsv.StopService()
defer db.Close()

tsv.sm.SetServingType(topodatapb.TabletType_MASTER, time.Time{}, StateNotConnected, "test disconnected")

req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(tsv.healthzHandler)
handler.ServeHTTP(rr, req)

expectedCode := http.StatusInternalServerError
if status := rr.Code; status != expectedCode {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expectedCode)
}

expected := "500 internal server error: vttablet is not serving\n"
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}

func TestBeginOnReplica(t *testing.T) {
db, tsv := setupTabletServerTest(t, "")
defer tsv.StopService()
Expand Down