Skip to content

Commit

Permalink
etcdserver: wait purge file loop during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyih committed Oct 30, 2019
1 parent 84e2788 commit f428fea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 13 additions & 3 deletions etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,12 +807,13 @@ func (s *EtcdServer) start() {

func (s *EtcdServer) purgeFile() {
var dberrc, serrc, werrc <-chan error
var dbdonec, sdonec, wdonec <-chan struct{}
if s.Cfg.MaxSnapFiles > 0 {
dberrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap.db", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
serrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
dbdonec, dberrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap.db", s.Cfg.MaxSnapFiles, purgeFileInterval, s.stopping)
sdonec, serrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.SnapDir(), "snap", s.Cfg.MaxSnapFiles, purgeFileInterval, s.stopping)
}
if s.Cfg.MaxWALFiles > 0 {
werrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.WALDir(), "wal", s.Cfg.MaxWALFiles, purgeFileInterval, s.done)
wdonec, werrc = fileutil.PurgeFile(s.getLogger(), s.Cfg.WALDir(), "wal", s.Cfg.MaxWALFiles, purgeFileInterval, s.stopping)
}

lg := s.getLogger()
Expand All @@ -836,6 +837,15 @@ func (s *EtcdServer) purgeFile() {
plog.Fatalf("failed to purge wal file %v", e)
}
case <-s.stopping:
if dbdonec != nil {
<-dbdonec
}
if sdonec != nil {
<-sdonec
}
if wdonec != nil {
<-wdonec
}
return
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/fileutil/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
"go.uber.org/zap"
)

func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) (<-chan struct{}, <-chan error) {
return purgeFile(lg, dirname, suffix, max, interval, stop, nil)
}

// purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil.
func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error {
func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) (<-chan struct{}, <-chan error) {
errC := make(chan error, 1)
doneC := make(chan struct{})
go func() {
defer close(doneC)
for {
fnames, err := ReadDir(dirname)
if err != nil {
Expand Down Expand Up @@ -84,5 +86,5 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval
}
}
}()
return errC
return doneC, errC
}

0 comments on commit f428fea

Please sign in to comment.