server: Cover V3 health with tests

Signed-off-by: Siyuan Zhang <sizhang@google.com>
This commit is contained in:
Siyuan Zhang
2023-12-12 15:39:09 -08:00
parent f009772c84
commit cc44646a2e
6 changed files with 69 additions and 21 deletions

View File

@@ -773,6 +773,7 @@ func (e *Etcd) serveClients() (err error) {
} else { } else {
mux := http.NewServeMux() mux := http.NewServeMux()
etcdhttp.HandleBasic(mux, e.Server) etcdhttp.HandleBasic(mux, e.Server)
etcdhttp.HandleMetricsHealth(mux, e.Server)
h = mux h = mux
} }

View File

@@ -51,7 +51,6 @@ func HandleBasic(mux *http.ServeMux, server etcdserver.ServerPeer) {
// TODO: deprecate '/config/local/log' in v3.5 // TODO: deprecate '/config/local/log' in v3.5
mux.HandleFunc(configPath+"/local/log", logHandleFunc) mux.HandleFunc(configPath+"/local/log", logHandleFunc)
HandleMetricsHealth(mux, server)
mux.HandleFunc(versionPath, versionHandler(server.Cluster(), serveVersion)) mux.HandleFunc(versionPath, versionHandler(server.Cluster(), serveVersion))
} }

View File

@@ -23,7 +23,8 @@ import (
"go.etcd.io/etcd/auth" "go.etcd.io/etcd/auth"
"go.etcd.io/etcd/etcdserver" "go.etcd.io/etcd/etcdserver"
"go.etcd.io/etcd/etcdserver/etcdserverpb" pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
"go.etcd.io/etcd/pkg/types"
"go.etcd.io/etcd/raft" "go.etcd.io/etcd/raft"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@@ -35,8 +36,19 @@ const (
PathHealth = "/health" PathHealth = "/health"
) )
// HandleMetricsHealth registers metrics and health handlers. type ServerHealth interface {
func HandleMetricsHealth(mux *http.ServeMux, srv etcdserver.ServerV2) { serverHealthV2V3
Range(context.Context, *pb.RangeRequest) (*pb.RangeResponse, error)
Config() etcdserver.ServerConfig
}
type serverHealthV2V3 interface {
Alarms() []*pb.AlarmMember
Leader() types.ID
}
// HandleMetricsHealthForV2 registers metrics and health handlers for v2.
func HandleMetricsHealthForV2(mux *http.ServeMux, srv etcdserver.ServerV2) {
mux.Handle(PathMetrics, promhttp.Handler()) mux.Handle(PathMetrics, promhttp.Handler())
mux.Handle(PathHealth, NewHealthHandler(func(excludedAlarms AlarmSet, serializable bool) Health { mux.Handle(PathHealth, NewHealthHandler(func(excludedAlarms AlarmSet, serializable bool) Health {
if h := checkAlarms(srv, excludedAlarms); h.Health != "true" { if h := checkAlarms(srv, excludedAlarms); h.Health != "true" {
@@ -49,9 +61,9 @@ func HandleMetricsHealth(mux *http.ServeMux, srv etcdserver.ServerV2) {
})) }))
} }
// HandleMetricsHealthForV3 registers metrics and health handlers. it checks health by using v3 range request // HandleMetricsHealth registers metrics and health handlers. it checks health by using v3 range request
// and its corresponding timeout. // and its corresponding timeout.
func HandleMetricsHealthForV3(mux *http.ServeMux, srv *etcdserver.EtcdServer) { func HandleMetricsHealth(mux *http.ServeMux, srv ServerHealth) {
mux.Handle(PathMetrics, promhttp.Handler()) mux.Handle(PathMetrics, promhttp.Handler())
mux.Handle(PathHealth, NewHealthHandler(func(excludedAlarms AlarmSet, serializable bool) Health { mux.Handle(PathHealth, NewHealthHandler(func(excludedAlarms AlarmSet, serializable bool) Health {
if h := checkAlarms(srv, excludedAlarms); h.Health != "true" { if h := checkAlarms(srv, excludedAlarms); h.Health != "true" {
@@ -60,7 +72,7 @@ func HandleMetricsHealthForV3(mux *http.ServeMux, srv *etcdserver.EtcdServer) {
if h := checkLeader(srv, serializable); h.Health != "true" { if h := checkLeader(srv, serializable); h.Health != "true" {
return h return h
} }
return checkV3API(srv, serializable) return checkAPI(srv, serializable)
})) }))
} }
@@ -152,7 +164,7 @@ func getSerializableFlag(r *http.Request) bool {
// TODO: etcdserver.ErrNoLeader in health API // TODO: etcdserver.ErrNoLeader in health API
func checkAlarms(srv etcdserver.ServerV2, excludedAlarms AlarmSet) Health { func checkAlarms(srv serverHealthV2V3, excludedAlarms AlarmSet) Health {
h := Health{Health: "true"} h := Health{Health: "true"}
as := srv.Alarms() as := srv.Alarms()
if len(as) > 0 { if len(as) > 0 {
@@ -165,9 +177,9 @@ func checkAlarms(srv etcdserver.ServerV2, excludedAlarms AlarmSet) Health {
h.Health = "false" h.Health = "false"
switch v.Alarm { switch v.Alarm {
case etcdserverpb.AlarmType_NOSPACE: case pb.AlarmType_NOSPACE:
h.Reason = "ALARM NOSPACE" h.Reason = "ALARM NOSPACE"
case etcdserverpb.AlarmType_CORRUPT: case pb.AlarmType_CORRUPT:
h.Reason = "ALARM CORRUPT" h.Reason = "ALARM CORRUPT"
default: default:
h.Reason = "ALARM UNKNOWN" h.Reason = "ALARM UNKNOWN"
@@ -180,7 +192,7 @@ func checkAlarms(srv etcdserver.ServerV2, excludedAlarms AlarmSet) Health {
return h return h
} }
func checkLeader(srv etcdserver.ServerV2, serializable bool) Health { func checkLeader(srv serverHealthV2V3, serializable bool) Health {
h := Health{Health: "true"} h := Health{Health: "true"}
if !serializable && (uint64(srv.Leader()) == raft.None) { if !serializable && (uint64(srv.Leader()) == raft.None) {
h.Health = "false" h.Health = "false"
@@ -193,7 +205,7 @@ func checkLeader(srv etcdserver.ServerV2, serializable bool) Health {
func checkV2API(srv etcdserver.ServerV2) Health { func checkV2API(srv etcdserver.ServerV2) Health {
h := Health{Health: "true"} h := Health{Health: "true"}
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"}) _, err := srv.Do(ctx, pb.Request{Method: "QGET"})
cancel() cancel()
if err != nil { if err != nil {
h.Health = "false" h.Health = "false"
@@ -204,10 +216,11 @@ func checkV2API(srv etcdserver.ServerV2) Health {
return h return h
} }
func checkV3API(srv *etcdserver.EtcdServer, serializable bool) Health { func checkAPI(srv ServerHealth, serializable bool) Health {
h := Health{Health: "true"} h := Health{Health: "true"}
ctx, cancel := context.WithTimeout(context.Background(), srv.Cfg.ReqTimeout()) cfg := srv.Config()
_, err := srv.Range(ctx, &etcdserverpb.RangeRequest{KeysOnly: true, Limit: 1, Serializable: serializable}) ctx, cancel := context.WithTimeout(context.Background(), cfg.ReqTimeout())
_, err := srv.Range(ctx, &pb.RangeRequest{KeysOnly: true, Limit: 1, Serializable: serializable})
cancel() cancel()
if err != nil && err != auth.ErrUserEmpty && err != auth.ErrPermissionDenied { if err != nil && err != auth.ErrUserEmpty && err != auth.ErrPermissionDenied {
h.Health = "false" h.Health = "false"

View File

@@ -24,6 +24,7 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"go.etcd.io/etcd/auth"
"go.etcd.io/etcd/etcdserver" "go.etcd.io/etcd/etcdserver"
stats "go.etcd.io/etcd/etcdserver/api/v2stats" stats "go.etcd.io/etcd/etcdserver/api/v2stats"
pb "go.etcd.io/etcd/etcdserver/etcdserverpb" pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
@@ -38,25 +39,34 @@ func (s *fakeStats) SelfStats() []byte { return nil }
func (s *fakeStats) LeaderStats() []byte { return nil } func (s *fakeStats) LeaderStats() []byte { return nil }
func (s *fakeStats) StoreStats() []byte { return nil } func (s *fakeStats) StoreStats() []byte { return nil }
type fakeServerV2 struct { type fakeHealthServer struct {
fakeServer fakeServer
stats.Stats stats.Stats
health string health string
apiError error
} }
func (s *fakeServerV2) Leader() types.ID { func (s *fakeHealthServer) Range(ctx context.Context, request *pb.RangeRequest) (*pb.RangeResponse, error) {
return nil, s.apiError
}
func (s *fakeHealthServer) Config() etcdserver.ServerConfig {
return etcdserver.ServerConfig{}
}
func (s *fakeHealthServer) Leader() types.ID {
if s.health == "true" { if s.health == "true" {
return 1 return 1
} }
return types.ID(raft.None) return types.ID(raft.None)
} }
func (s *fakeServerV2) Do(ctx context.Context, r pb.Request) (etcdserver.Response, error) { func (s *fakeHealthServer) Do(ctx context.Context, r pb.Request) (etcdserver.Response, error) {
if s.health == "true" { if s.health == "true" {
return etcdserver.Response{}, nil return etcdserver.Response{}, nil
} }
return etcdserver.Response{}, fmt.Errorf("fail health check") return etcdserver.Response{}, fmt.Errorf("fail health check")
} }
func (s *fakeServerV2) ClientCertAuthEnabled() bool { return false } func (s *fakeHealthServer) ClientCertAuthEnabled() bool { return false }
func TestHealthHandler(t *testing.T) { func TestHealthHandler(t *testing.T) {
// define the input and expected output // define the input and expected output
@@ -65,6 +75,7 @@ func TestHealthHandler(t *testing.T) {
name string name string
alarms []*pb.AlarmMember alarms []*pb.AlarmMember
healthCheckURL string healthCheckURL string
apiError error
expectStatusCode int expectStatusCode int
expectHealth string expectHealth string
@@ -118,15 +129,34 @@ func TestHealthHandler(t *testing.T) {
expectStatusCode: http.StatusOK, expectStatusCode: http.StatusOK,
expectHealth: "true", expectHealth: "true",
}, },
{
healthCheckURL: "/health",
apiError: auth.ErrUserEmpty,
expectStatusCode: http.StatusOK,
expectHealth: "true",
},
{
healthCheckURL: "/health",
apiError: auth.ErrPermissionDenied,
expectStatusCode: http.StatusOK,
expectHealth: "true",
},
{
healthCheckURL: "/health",
apiError: fmt.Errorf("Unexpected error"),
expectStatusCode: http.StatusServiceUnavailable,
expectHealth: "false",
},
} }
for i, tt := range tests { for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mux := http.NewServeMux() mux := http.NewServeMux()
HandleMetricsHealth(mux, &fakeServerV2{ HandleMetricsHealth(mux, &fakeHealthServer{
fakeServer: fakeServer{alarms: tt.alarms}, fakeServer: fakeServer{alarms: tt.alarms},
Stats: &fakeStats{}, Stats: &fakeStats{},
health: tt.expectHealth, health: tt.expectHealth,
apiError: tt.apiError,
}) })
ts := httptest.NewServer(mux) ts := httptest.NewServer(mux)
defer ts.Close() defer ts.Close()

View File

@@ -55,6 +55,7 @@ const (
func NewClientHandler(lg *zap.Logger, server etcdserver.ServerPeer, timeout time.Duration) http.Handler { func NewClientHandler(lg *zap.Logger, server etcdserver.ServerPeer, timeout time.Duration) http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
etcdhttp.HandleBasic(mux, server) etcdhttp.HandleBasic(mux, server)
etcdhttp.HandleMetricsHealthForV2(mux, server)
handleV2(lg, mux, server, timeout) handleV2(lg, mux, server, timeout)
return requestLogger(lg, mux) return requestLogger(lg, mux)
} }

View File

@@ -667,6 +667,10 @@ func (s *EtcdServer) getLogger() *zap.Logger {
return l return l
} }
func (s *EtcdServer) Config() ServerConfig {
return s.Cfg
}
func tickToDur(ticks int, tickMs uint) string { func tickToDur(ticks int, tickMs uint) string {
return fmt.Sprintf("%v", time.Duration(ticks)*time.Duration(tickMs)*time.Millisecond) return fmt.Sprintf("%v", time.Duration(ticks)*time.Duration(tickMs)*time.Millisecond)
} }