Skip to content

Commit 7247c02

Browse files
authored
observer: fix TiProxy reports unmarshal body in healthy check failed" when TiDB is graceful shutdown (#569)
1 parent 7c6ad51 commit 7247c02

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pkg/balance/observer/health_check.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,29 @@ func (dhc *DefaultHealthCheck) checkSqlPort(ctx context.Context, addr string, bh
100100
func (dhc *DefaultHealthCheck) backendStatusCheck(httpCli *http.Client, url string, bh *BackendHealth) error {
101101
resp, err := httpCli.Get(url)
102102
if err == nil {
103+
defer func() {
104+
if ignoredErr := resp.Body.Close(); ignoredErr != nil {
105+
dhc.logger.Warn("close http response in health check failed", zap.Error(ignoredErr))
106+
}
107+
}()
103108

104109
if resp.StatusCode != http.StatusOK {
105-
err = backoff.Permanent(errors.Errorf("http status %d", resp.StatusCode))
110+
return backoff.Permanent(errors.Errorf("http status %d", resp.StatusCode))
106111
}
107112

108113
body, err := io.ReadAll(resp.Body)
109114
if err != nil {
110-
dhc.logger.Error("read response body in healthy check failed ", zap.Error(err))
115+
dhc.logger.Error("read response body in healthy check failed", zap.String("url", url), zap.Error(err))
111116
return err
112117
}
113118

114119
var respBody backendHttpStatusRespBody
115120
err = json.Unmarshal(body, &respBody)
116121
if err != nil {
117-
dhc.logger.Error("unmarshal body in healthy check failed", zap.String("resp body", string(body)), zap.Error(err))
122+
dhc.logger.Error("unmarshal body in healthy check failed", zap.String("url", url), zap.String("resp body", string(body)), zap.Error(err))
118123
return err
119124
}
120125

121-
if ignoredErr := resp.Body.Close(); ignoredErr != nil {
122-
dhc.logger.Warn("close http response in health check failed", zap.Error(ignoredErr))
123-
}
124-
125126
bh.ServerVersion = respBody.Version
126127
}
127128
return err

pkg/balance/observer/health_check_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ func TestReadServerVersion(t *testing.T) {
2323
hc := NewDefaultHealthCheck(nil, newHealthCheckConfigForTest(), lg)
2424
backend, info := newBackendServer(t)
2525
backend.setServerVersion("1.0")
26-
//backend.serverVersion.Store("1.0")
2726
health := hc.Check(context.Background(), backend.sqlAddr, info)
2827
require.Equal(t, "1.0", health.ServerVersion)
2928
backend.stopSQLServer()
30-
//backend.serverVersion.Store("2.0")
3129
backend.setServerVersion("2.0")
3230
backend.startSQLServer()
3331
health = hc.Check(context.Background(), backend.sqlAddr, info)
@@ -47,7 +45,7 @@ func TestReadServerVersion(t *testing.T) {
4745

4846
// Test that the backend status is correct when the backend starts or shuts down.
4947
func TestHealthCheck(t *testing.T) {
50-
lg, _ := logger.CreateLoggerForTest(t)
48+
lg, text := logger.CreateLoggerForTest(t)
5149
cfg := newHealthCheckConfigForTest()
5250
hc := NewDefaultHealthCheck(nil, cfg, lg)
5351
backend, info := newBackendServer(t)
@@ -65,6 +63,7 @@ func TestHealthCheck(t *testing.T) {
6563
backend.setHTTPResp(false)
6664
health = hc.Check(context.Background(), backend.sqlAddr, info)
6765
require.False(t, health.Healthy)
66+
require.NotContains(t, text.String(), "unmarshal body")
6867
backend.setHTTPResp(true)
6968
health = hc.Check(context.Background(), backend.sqlAddr, info)
7069
require.True(t, health.Healthy)
@@ -113,6 +112,7 @@ func (srv *backendServer) setServerVersion(version string) {
113112
body, _ := json.Marshal(resp)
114113
srv.mockHttpHandler.setHTTPRespBody(string(body))
115114
}
115+
116116
func (srv *backendServer) startHTTPServer() {
117117
if srv.mockHttpHandler == nil {
118118
srv.mockHttpHandler = &mockHttpHandler{

0 commit comments

Comments
 (0)