mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdserver: wait purge file loop during shutdown
To prevent the purge file loop from accidentally acquiring the file lock and remove the files during server shutdowm.
This commit is contained in:
parent
fb5e7bfe00
commit
e2cb1a417c
@ -602,12 +602,13 @@ func (s *EtcdServer) start() {
|
|||||||
|
|
||||||
func (s *EtcdServer) purgeFile() {
|
func (s *EtcdServer) purgeFile() {
|
||||||
var dberrc, serrc, werrc <-chan error
|
var dberrc, serrc, werrc <-chan error
|
||||||
|
var dbdonec, sdonec, wdonec <-chan struct{}
|
||||||
if s.Cfg.MaxSnapFiles > 0 {
|
if s.Cfg.MaxSnapFiles > 0 {
|
||||||
dberrc = fileutil.PurgeFile(s.Cfg.SnapDir(), "snap.db", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
|
dbdonec, dberrc = fileutil.PurgeFileWithDoneNotify(s.Cfg.SnapDir(), "snap.db", s.Cfg.MaxSnapFiles, purgeFileInterval, s.stopping)
|
||||||
serrc = fileutil.PurgeFile(s.Cfg.SnapDir(), "snap", s.Cfg.MaxSnapFiles, purgeFileInterval, s.done)
|
sdonec, serrc = fileutil.PurgeFileWithDoneNotify(s.Cfg.SnapDir(), "snap", s.Cfg.MaxSnapFiles, purgeFileInterval, s.stopping)
|
||||||
}
|
}
|
||||||
if s.Cfg.MaxWALFiles > 0 {
|
if s.Cfg.MaxWALFiles > 0 {
|
||||||
werrc = fileutil.PurgeFile(s.Cfg.WALDir(), "wal", s.Cfg.MaxWALFiles, purgeFileInterval, s.done)
|
wdonec, werrc = fileutil.PurgeFileWithDoneNotify(s.Cfg.WALDir(), "wal", s.Cfg.MaxWALFiles, purgeFileInterval, s.stopping)
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case e := <-dberrc:
|
case e := <-dberrc:
|
||||||
@ -617,6 +618,15 @@ func (s *EtcdServer) purgeFile() {
|
|||||||
case e := <-werrc:
|
case e := <-werrc:
|
||||||
plog.Fatalf("failed to purge wal file %v", e)
|
plog.Fatalf("failed to purge wal file %v", e)
|
||||||
case <-s.stopping:
|
case <-s.stopping:
|
||||||
|
if dbdonec != nil {
|
||||||
|
<-dbdonec
|
||||||
|
}
|
||||||
|
if sdonec != nil {
|
||||||
|
<-sdonec
|
||||||
|
}
|
||||||
|
if wdonec != nil {
|
||||||
|
<-wdonec
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,13 +23,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func PurgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
|
func PurgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
|
||||||
return purgeFile(dirname, suffix, max, interval, stop, nil)
|
return purgeFile(dirname, suffix, max, interval, stop, nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PurgeFileWithDoneNotify(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) (<-chan struct{}, <-chan error) {
|
||||||
|
doneC := make(chan struct{})
|
||||||
|
errC := purgeFile(dirname, suffix, max, interval, stop, nil, doneC)
|
||||||
|
return doneC, errC
|
||||||
}
|
}
|
||||||
|
|
||||||
// purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil.
|
// purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil.
|
||||||
func purgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error {
|
// if donec is non-nil, the function closes it to notify its exit.
|
||||||
|
func purgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string, donec chan<- struct{}) <-chan error {
|
||||||
errC := make(chan error, 1)
|
errC := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
if donec != nil {
|
||||||
|
defer close(donec)
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
fnames, err := ReadDir(dirname)
|
fnames, err := ReadDir(dirname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -43,7 +43,7 @@ func TestPurgeFile(t *testing.T) {
|
|||||||
stop, purgec := make(chan struct{}), make(chan string, 10)
|
stop, purgec := make(chan struct{}), make(chan string, 10)
|
||||||
|
|
||||||
// keep 3 most recent files
|
// keep 3 most recent files
|
||||||
errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec)
|
errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec, nil)
|
||||||
select {
|
select {
|
||||||
case f := <-purgec:
|
case f := <-purgec:
|
||||||
t.Errorf("unexpected purge on %q", f)
|
t.Errorf("unexpected purge on %q", f)
|
||||||
@ -114,7 +114,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stop, purgec := make(chan struct{}), make(chan string, 10)
|
stop, purgec := make(chan struct{}), make(chan string, 10)
|
||||||
errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec)
|
errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec, nil)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
select {
|
select {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user