From c238ce9737f7e59d3aeb9ebf354aa877d09deb36 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Mon, 20 Jan 2025 18:25:42 +0000 Subject: [PATCH] add comment to clarify the etcd shutting down workflow Signed-off-by: Benjamin Wang --- server/embed/etcd.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/server/embed/etcd.go b/server/embed/etcd.go index 5f8f7e33d960..9b4bccdaba72 100644 --- a/server/embed/etcd.go +++ b/server/embed/etcd.go @@ -388,6 +388,24 @@ func (e *Etcd) Config() Config { // Close gracefully shuts down all servers/listeners. // Client requests will be terminated with request timeout. // After timeout, enforce remaning requests be closed immediately. +// +// The rough workflow to shut down etcd: +// 1. close the `stopc` channel, so that all error handlers (child +// goroutines) won't send back any errors anymore; +// 2. close all client and metrics listeners, so that etcd server +// stops receiving any new connection immediately; +// 3. stop the http and grpc servers gracefully, within request timeout; +// 4. call the cancel function to close the gateway context, so that +// all gateway connections are closed. +// 5. stop etcd server gracefully, and ensure the main raft loop +// goroutine is stopped; +// 6. stop all peer listeners, so that it stops receives peer connections +// and messages (wait up to 1-second); +// 7. wait for all child goroutines (i.e. client handlers, peer handlers +// and metrics handlers) to exit; +// 8. close the `errc` channel to release the resource. Note that it's only +// safe to close the `errc` after step 6 above is done, otherwise the +// child goroutines may send errors back to already closed `errc` channel. func (e *Etcd) Close() { fields := []zap.Field{ zap.String("name", e.cfg.Name), @@ -407,10 +425,14 @@ func (e *Etcd) Close() { lg.Sync() }() + // 1. close the `stopc` channel, so that all error handlers (child + // goroutines) won't send back any errors anymore; e.closeOnce.Do(func() { close(e.stopc) }) + // 2. close all client and metrics listeners, so that etcd server + // stops receiving any new connection immediately; for i := range e.Clients { if e.Clients[i] != nil { e.Clients[i].Close() @@ -421,7 +443,7 @@ func (e *Etcd) Close() { e.metricsListeners[i].Close() } - // close client requests with request timeout + // 3. stop the http and grpc servers gracefully, within request timeout; timeout := 2 * time.Second if e.Server != nil { timeout = e.Server.Cfg.ReqTimeout() @@ -434,6 +456,8 @@ func (e *Etcd) Close() { } } + // 4. call the cancel function to close the gateway context, so that + // all gateway connections are closed. for _, sctx := range e.sctxs { sctx.cancel() } @@ -443,12 +467,14 @@ func (e *Etcd) Close() { e.tracingExporterShutdown() } - // close rafthttp transports + // 5. stop etcd server gracefully, and ensure the main raft loop + // goroutine is stopped; if e.Server != nil { e.Server.Stop() } - // close all idle connections in peer handler (wait up to 1-second) + // 6. stop all peer listeners, so that it stops receives peer connections + // and messages (wait up to 1-second); for i := range e.Peers { if e.Peers[i] != nil && e.Peers[i].close != nil { ctx, cancel := context.WithTimeout(context.Background(), time.Second) @@ -457,7 +483,13 @@ func (e *Etcd) Close() { } } if e.errc != nil { + // 7. wait for all child goroutines (i.e. client handlers, peer handlers + // and metrics handlers) to exit; e.wg.Wait() + + // 8. close the `errc` channel to release the resource. Note that it's only + // safe to close the `errc` after step 6 above is done, otherwise the + // child goroutines may send errors back to already closed `errc` channel. close(e.errc) } }