From cee181d1abd666f435e531c0a378797af4bb8a72 Mon Sep 17 00:00:00 2001 From: Thomas Jungblut Date: Mon, 6 May 2024 10:03:08 +0200 Subject: [PATCH] v3rpc: run health notifier to listen on online defrag state change Backport from 3.6 in #16836 Co-authored-by: Chao Chen Signed-off-by: Thomas Jungblut --- server/etcdserver/api/v3rpc/grpc.go | 3 +- server/etcdserver/api/v3rpc/health.go | 33 +++++++++++++-------- server/etcdserver/api/v3rpc/maintenance.go | 13 ++++----- tests/e2e/failover_test.go | 34 ++++++++++++---------- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/server/etcdserver/api/v3rpc/grpc.go b/server/etcdserver/api/v3rpc/grpc.go index 8bb4acaad..6947903a3 100644 --- a/server/etcdserver/api/v3rpc/grpc.go +++ b/server/etcdserver/api/v3rpc/grpc.go @@ -78,7 +78,8 @@ func Server(s *etcdserver.EtcdServer, tls *tls.Config, interceptor grpc.UnarySer pb.RegisterAuthServer(grpcServer, NewAuthServer(s)) hsrv := health.NewServer() - pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s, NewHealthNotifier(hsrv, s.Logger()))) + healthNotifier := newHealthNotifier(hsrv, s) + pb.RegisterMaintenanceServer(grpcServer, NewMaintenanceServer(s, healthNotifier)) healthpb.RegisterHealthServer(grpcServer, hsrv) // set zero values for metrics registered for this grpc server diff --git a/server/etcdserver/api/v3rpc/health.go b/server/etcdserver/api/v3rpc/health.go index 5e84b9b40..f58396d1c 100644 --- a/server/etcdserver/api/v3rpc/health.go +++ b/server/etcdserver/api/v3rpc/health.go @@ -15,6 +15,7 @@ package v3rpc import ( + "go.etcd.io/etcd/server/v3/etcdserver" "go.uber.org/zap" "google.golang.org/grpc/health" healthpb "google.golang.org/grpc/health/grpc_health_v1" @@ -24,31 +25,39 @@ const ( allGRPCServices = "" ) -type HealthNotifier interface { - StartServe() - StopServe(reason string) +type notifier interface { + defragStarted() + defragFinished() } -func NewHealthNotifier(hs *health.Server, lg *zap.Logger) HealthNotifier { +func newHealthNotifier(hs *health.Server, s *etcdserver.EtcdServer) notifier { if hs == nil { panic("unexpected nil gRPC health server") } - if lg == nil { - lg = zap.NewNop() - } - hc := &healthChecker{hs: hs, lg: lg} + hc := &healthNotifier{hs: hs, lg: s.Logger(), stopGRPCServiceOnDefrag: s.Cfg.ExperimentalStopGRPCServiceOnDefrag} // set grpc health server as serving status blindly since // the grpc server will serve iff s.ReadyNotify() is closed. - hc.StartServe() + hc.startServe() return hc } -type healthChecker struct { +type healthNotifier struct { hs *health.Server lg *zap.Logger + + stopGRPCServiceOnDefrag bool } -func (hc *healthChecker) StartServe() { +func (hc *healthNotifier) defragStarted() { + if !hc.stopGRPCServiceOnDefrag { + return + } + hc.stopServe("defrag is active") +} + +func (hc *healthNotifier) defragFinished() { hc.startServe() } + +func (hc *healthNotifier) startServe() { hc.lg.Info( "grpc service status changed", zap.String("service", allGRPCServices), @@ -57,7 +66,7 @@ func (hc *healthChecker) StartServe() { hc.hs.SetServingStatus(allGRPCServices, healthpb.HealthCheckResponse_SERVING) } -func (hc *healthChecker) StopServe(reason string) { +func (hc *healthNotifier) stopServe(reason string) { hc.lg.Warn( "grpc service status changed", zap.String("service", allGRPCServices), diff --git a/server/etcdserver/api/v3rpc/maintenance.go b/server/etcdserver/api/v3rpc/maintenance.go index 5b42ba901..7db17e1a0 100644 --- a/server/etcdserver/api/v3rpc/maintenance.go +++ b/server/etcdserver/api/v3rpc/maintenance.go @@ -76,13 +76,12 @@ type maintenanceServer struct { hdr header cs ClusterStatusGetter d Downgrader - hn HealthNotifier - stopServingOnDefrag bool + healthNotifier notifier } -func NewMaintenanceServer(s *etcdserver.EtcdServer, hn HealthNotifier) pb.MaintenanceServer { - srv := &maintenanceServer{lg: s.Cfg.Logger, rg: s, hasher: s.KV().HashStorage(), kg: s, bg: s, a: s, lt: s, hdr: newHeader(s), cs: s, d: s, hn: hn, stopServingOnDefrag: s.Cfg.ExperimentalStopGRPCServiceOnDefrag} +func NewMaintenanceServer(s *etcdserver.EtcdServer, healthNotifier notifier) pb.MaintenanceServer { + srv := &maintenanceServer{lg: s.Cfg.Logger, rg: s, hasher: s.KV().HashStorage(), kg: s, bg: s, a: s, lt: s, hdr: newHeader(s), cs: s, d: s, healthNotifier: healthNotifier} if srv.lg == nil { srv.lg = zap.NewNop() } @@ -91,10 +90,8 @@ func NewMaintenanceServer(s *etcdserver.EtcdServer, hn HealthNotifier) pb.Mainte func (ms *maintenanceServer) Defragment(ctx context.Context, sr *pb.DefragmentRequest) (*pb.DefragmentResponse, error) { ms.lg.Info("starting defragment") - if ms.stopServingOnDefrag { - ms.hn.StopServe("defrag is active") - defer ms.hn.StartServe() - } + ms.healthNotifier.defragStarted() + defer ms.healthNotifier.defragFinished() err := ms.bg.Backend().Defrag() if err != nil { ms.lg.Warn("failed to defragment", zap.Error(err)) diff --git a/tests/e2e/failover_test.go b/tests/e2e/failover_test.go index 06218067d..505cef01e 100644 --- a/tests/e2e/failover_test.go +++ b/tests/e2e/failover_test.go @@ -49,11 +49,11 @@ func TestFailoverOnDefrag(t *testing.T) { gRPCDialOptions []grpc.DialOption // common assertion - expectedMinTotalRequestsCount int + expectedMinQPS float64 // happy case assertion - expectedMaxFailedRequestsCount int + expectedMaxFailureRate float64 // negative case assertion - expectedMinFailedRequestsCount int + expectedMinFailureRate float64 }{ { name: "defrag failover happy case", @@ -62,8 +62,8 @@ func TestFailoverOnDefrag(t *testing.T) { grpc.WithDisableServiceConfig(), grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin", "healthCheckConfig": {"serviceName": ""}}`), }, - expectedMinTotalRequestsCount: 300, - expectedMaxFailedRequestsCount: 5, + expectedMinQPS: 20, + expectedMaxFailureRate: 0.01, }, { name: "defrag blocks one-third of requests with stopGRPCServiceOnDefrag set to false", @@ -72,14 +72,14 @@ func TestFailoverOnDefrag(t *testing.T) { grpc.WithDisableServiceConfig(), grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin", "healthCheckConfig": {"serviceName": ""}}`), }, - expectedMinTotalRequestsCount: 300, - expectedMinFailedRequestsCount: 90, + expectedMinQPS: 20, + expectedMinFailureRate: 0.25, }, { name: "defrag blocks one-third of requests with stopGRPCServiceOnDefrag set to true and client health check disabled", experimentalStopGRPCServiceOnDefragEnabled: true, - expectedMinTotalRequestsCount: 300, - expectedMinFailedRequestsCount: 90, + expectedMinQPS: 20, + expectedMinFailureRate: 0.25, }, } @@ -98,6 +98,7 @@ func TestFailoverOnDefrag(t *testing.T) { endpoints := clus.EndpointsGRPC() requestVolume, successfulRequestCount := 0, 0 + start := time.Now() g := new(errgroup.Group) g.Go(func() (lastErr error) { clusterClient, cerr := clientv3.New(clientv3.Config{ @@ -137,15 +138,16 @@ func TestFailoverOnDefrag(t *testing.T) { if err != nil { t.Logf("etcd client failed to fail over, error (%v)", err) } - t.Logf("request failure rate is %.2f%%, traffic volume successfulRequestCount %d requests, total %d requests", (1-float64(successfulRequestCount)/float64(requestVolume))*100, successfulRequestCount, requestVolume) + qps := float64(requestVolume) / float64(time.Since(start)) * float64(time.Second) + failureRate := 1 - float64(successfulRequestCount)/float64(requestVolume) + t.Logf("request failure rate is %.2f%%, qps is %.2f requests/second", failureRate*100, qps) - require.GreaterOrEqual(t, requestVolume, tc.expectedMinTotalRequestsCount) - failedRequestCount := requestVolume - successfulRequestCount - if tc.expectedMaxFailedRequestsCount != 0 { - require.LessOrEqual(t, failedRequestCount, tc.expectedMaxFailedRequestsCount) + require.GreaterOrEqual(t, qps, tc.expectedMinQPS) + if tc.expectedMaxFailureRate != 0.0 { + require.LessOrEqual(t, failureRate, tc.expectedMaxFailureRate) } - if tc.expectedMinFailedRequestsCount != 0 { - require.GreaterOrEqual(t, failedRequestCount, tc.expectedMinFailedRequestsCount) + if tc.expectedMinFailureRate != 0.0 { + require.GreaterOrEqual(t, failureRate, tc.expectedMinFailureRate) } }) }