Skip to content

Commit

Permalink
Install livez and readyz in embed server.
Browse files Browse the repository at this point in the history
Tested successfully with: curl -k '127.0.0.1:2379/readyz?verbose'
  • Loading branch information
siyuanfoundation committed Sep 25, 2023
1 parent 2adfac8 commit 043b6c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ func (e *Etcd) serveClients() (err error) {
etcdhttp.HandleVersion(mux, e.Server)
etcdhttp.HandleMetrics(mux)
etcdhttp.HandleHealth(e.cfg.logger, mux, e.Server)
e.Server.InstallLivezReadyz(e.cfg.logger, mux)

var gopts []grpc.ServerOption
if e.cfg.GRPCKeepAliveMinTime > time.Duration(0) {
Expand Down
32 changes: 21 additions & 11 deletions server/etcdserver/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ type EtcdServerHealth interface {
Range(context.Context, *etcdserverpb.RangeRequest) (*etcdserverpb.RangeResponse, error)
}

// HealthChecker is a named healthz checker.
type HealthChecker interface {
Name() string
Check(req *http.Request) error
}

func (s *EtcdServer) InstallLivezReadyz(lg *zap.Logger, mux mux) {
s.healthHandler.installLivez(lg, mux)
s.healthHandler.installReadyz(lg, mux)
s.healthHandler.installHealthz(lg, mux)
}

func (s *EtcdServer) AddHealthCheck(check HealthChecker, isLivez bool, isReadyz bool) error {
return s.healthHandler.addHealthCheck(check, isLivez, isReadyz)
}

type HealthHandler struct {
server EtcdServerHealth
// lock for health check related functions.
Expand Down Expand Up @@ -78,12 +94,6 @@ func (s stringSet) List() []string {
return keys
}

// HealthChecker is a named healthz checker.
type HealthChecker interface {
Name() string
Check(req *http.Request) error
}

// PingHealthz returns true automatically when checked
var PingHealthz HealthChecker = ping{}

Expand Down Expand Up @@ -134,7 +144,7 @@ func checkAlarm(srv EtcdServerHealth, at etcdserverpb.AlarmType) error {

func (h *HealthHandler) addDefaultHealthChecks() error {
// Checks that should be included both in livez and readyz.
h.AddHealthCheck(PingHealthz, true, true)
h.addHealthCheck(PingHealthz, true, true)
serializableReadCheck := NamedCheck("serializable_read", func(r *http.Request) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err := h.server.Range(ctx, &etcdserverpb.RangeRequest{KeysOnly: true, Limit: 1, Serializable: true})
Expand All @@ -144,18 +154,18 @@ func (h *HealthHandler) addDefaultHealthChecks() error {
}
return nil
})
h.AddHealthCheck(serializableReadCheck, true, true)
h.addHealthCheck(serializableReadCheck, true, true)
// Checks that should be included only in livez.
// Checks that should be included only in readyz.
corruptionAlarmCheck := NamedCheck("data_corruption", func(r *http.Request) error {
return checkAlarm(h.server, etcdserverpb.AlarmType_CORRUPT)
})
h.AddHealthCheck(corruptionAlarmCheck, false, true)
h.addHealthCheck(corruptionAlarmCheck, false, true)
return nil
}

// AddHealthCheck allows you to add a HealthCheck to livez or readyz or both.
func (h *HealthHandler) AddHealthCheck(check HealthChecker, isLivez bool, isReadyz bool) error {
// addHealthCheck allows you to add a HealthCheck to livez or readyz or both.
func (h *HealthHandler) addHealthCheck(check HealthChecker, isLivez bool, isReadyz bool) error {
h.healthMux.Lock()
defer h.healthMux.Unlock()
if _, found := h.healthCheckStore[check.Name()]; found {
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/healthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestHealthHandler(t *testing.T) {
failedFunc := func(r *http.Request) error { return fmt.Errorf("Failed") }
succeededFunc := func(r *http.Request) error { return nil }
ableToAddCheck := func(expectSuccess bool, chk HealthChecker, isLivez bool, isReadyz bool) {
err := handler.AddHealthCheck(chk, isLivez, isReadyz)
err := handler.addHealthCheck(chk, isLivez, isReadyz)
if expectSuccess && err != nil {
t.Errorf("Expect being able to add check %s", chk.Name())
}
Expand Down

0 comments on commit 043b6c4

Please sign in to comment.