etcdserver: remove temp files in snap dir when etcdServer starting

When etcd exits abnormally, tmp files will remain in snap dir, so clean up tmp files in snap dir when etcdserver starting.

Fixes #12837
This commit is contained in:
pyiyun
2021-04-09 09:58:56 +08:00
parent bad0b4d513
commit 28a490b09c
3 changed files with 88 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import (
"path"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
@@ -338,6 +339,17 @@ func NewServer(cfg config.ServerConfig) (srv *EtcdServer, err error) {
zap.Error(err),
)
}
if err = fileutil.RemoveMatchFile(cfg.Logger, cfg.SnapDir(), func(fileName string) bool {
return strings.HasPrefix(fileName, "tmp")
}); err != nil {
cfg.Logger.Error(
"failed to remove temp file(s) in snapshot directory",
zap.String("path", cfg.SnapDir()),
zap.Error(err),
)
}
ss := snap.New(cfg.Logger, cfg.SnapDir())
bepath := cfg.BackendPath()