wal: lock WAL file while repairing

This commit is contained in:
Anthony Romano 2016-05-04 10:46:16 -07:00
parent 774030e1b2
commit cd9e6a1d4f
2 changed files with 7 additions and 7 deletions

View File

@ -76,7 +76,7 @@ func Repair(dirpath string) bool {
plog.Errorf("could not repair %v, failed to truncate file", f.Name())
return false
}
if err = fileutil.Fsync(f); err != nil {
if err = fileutil.Fsync(f.File); err != nil {
plog.Errorf("could not repair %v, failed to sync file", f.Name())
return false
}
@ -89,7 +89,7 @@ func Repair(dirpath string) bool {
}
// openLast opens the last wal file for read and write.
func openLast(dirpath string) (*os.File, error) {
func openLast(dirpath string) (*fileutil.LockedFile, error) {
names, err := fileutil.ReadDir(dirpath)
if err != nil {
return nil, err
@ -99,5 +99,5 @@ func openLast(dirpath string) (*os.File, error) {
return nil, ErrFileNotFound
}
last := path.Join(dirpath, names[len(names)-1])
return os.OpenFile(last, os.O_RDWR, 0)
return fileutil.LockFile(last, os.O_RDWR, 0600)
}

View File

@ -34,10 +34,8 @@ func TestRepairTruncate(t *testing.T) {
if err != nil {
return err
}
if terr := f.Truncate(offset - 4); terr != nil {
return terr
}
return nil
defer f.Close()
return f.Truncate(offset - 4)
}
testRepair(t, makeEnts(10), corruptf, 9)
@ -140,6 +138,7 @@ func TestRepairWriteTearLast(t *testing.T) {
if err != nil {
return err
}
defer f.Close()
// 512 bytes perfectly aligns the last record, so use 1024
if offset < 1024 {
return fmt.Errorf("got offset %d, expected >1024", offset)
@ -163,6 +162,7 @@ func TestRepairWriteTearMiddle(t *testing.T) {
if err != nil {
return err
}
defer f.Close()
// corrupt middle of 2nd record
_, werr := f.WriteAt(make([]byte, 512), 4096+512)
return werr