Skip to content

Commit

Permalink
wal: lock WAL file while repairing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Romano committed May 6, 2016
1 parent 774030e commit cd9e6a1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions wal/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions wal/repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit cd9e6a1

Please sign in to comment.