Skip to content

Commit

Permalink
Recovery fixes
Browse files Browse the repository at this point in the history
- Don't create files when reading them
- Don't remove lock on db.Open errors
  • Loading branch information
akrylysov committed Nov 13, 2023
1 parent 689f110 commit 90db0cc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion datalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func parseSegmentName(name string) (uint16, uint64, error) {
}

func (dl *datalog) openSegment(name string, id uint16, seqID uint64) (*segment, error) {
f, err := openFile(dl.opts.FileSystem, name, false)
f, err := openFile(dl.opts.FileSystem, name, openFileFlags{})
if err != nil {
return nil, err
}
Expand Down
7 changes: 0 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ func Open(path string, opts *Options) (*DB, error) {
}
return nil, errors.Wrap(err, "creating lock file")
}
clean := lock.Unlock
defer func() {
if clean != nil {
_ = clean()
}
}()

if acquiredExistingLock {
// Lock file already existed, but the process managed to acquire it.
Expand Down Expand Up @@ -121,7 +115,6 @@ func Open(path string, opts *Options) (*DB, error) {
db.startBackgroundWorker()
}

clean = nil
return db, nil
}

Expand Down
18 changes: 14 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ type file struct {
size int64
}

func openFile(fsyst fs.FileSystem, name string, truncate bool) (*file, error) {
flag := os.O_CREATE | os.O_RDWR
if truncate {
flag |= os.O_TRUNC
type openFileFlags struct {
truncate bool
readOnly bool
}

func openFile(fsyst fs.FileSystem, name string, flags openFileFlags) (*file, error) {
var flag int
if flags.readOnly {
flag = os.O_RDONLY
} else {
flag = os.O_CREATE | os.O_RDWR
if flags.truncate {
flag |= os.O_TRUNC
}
}
fi, err := fsyst.OpenFile(name, flag, os.FileMode(0640))
f := &file{}
Expand Down
4 changes: 2 additions & 2 deletions gobfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func readGobFile(fsys fs.FileSystem, name string, v interface{}) error {
f, err := openFile(fsys, name, false)
f, err := openFile(fsys, name, openFileFlags{readOnly: true})
if err != nil {
return err
}
Expand All @@ -17,7 +17,7 @@ func readGobFile(fsys fs.FileSystem, name string, v interface{}) error {
}

func writeGobFile(fsys fs.FileSystem, name string, v interface{}) error {
f, err := openFile(fsys, name, true)
f, err := openFile(fsys, name, openFileFlags{truncate: true})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type indexMeta struct {
type matchKeyFunc func(slot) (bool, error)

func openIndex(opts *Options) (*index, error) {
main, err := openFile(opts.FileSystem, indexMainName, false)
main, err := openFile(opts.FileSystem, indexMainName, openFileFlags{})
if err != nil {
return nil, errors.Wrap(err, "opening main index")
}
overflow, err := openFile(opts.FileSystem, indexOverflowName, false)
overflow, err := openFile(opts.FileSystem, indexOverflowName, openFileFlags{})
if err != nil {
_ = main.Close()
return nil, errors.Wrap(err, "opening overflow index")
Expand Down

0 comments on commit 90db0cc

Please sign in to comment.