lib/fs: replace fs.OpenReaderAt with fs.MustOpenReaderAt

All the callers for fs.OpenReaderAt expect that the file will be opened.
So it is better to log fatal error inside fs.MustOpenReaderAt instead of leaving this to the caller.
This commit is contained in:
Aliaksandr Valialkin 2020-11-23 09:55:38 +02:00
parent 1dcb438c3b
commit f4fd917e4f
6 changed files with 16 additions and 51 deletions

View file

@ -133,10 +133,7 @@ func (tbf *tmpBlocksFile) Finalize() error {
return fmt.Errorf("cannot write the remaining %d bytes to %q: %w", len(tbf.buf), fname, err) return fmt.Errorf("cannot write the remaining %d bytes to %q: %w", len(tbf.buf), fname, err)
} }
tbf.buf = tbf.buf[:0] tbf.buf = tbf.buf[:0]
r, err := fs.OpenReaderAt(fname) r := fs.MustOpenReaderAt(fname)
if err != nil {
logger.Panicf("FATAL: cannot open %q: %s", fname, err)
}
// Hint the OS that the file is read almost sequentiallly. // Hint the OS that the file is read almost sequentiallly.
// This should reduce the number of disk seeks, which is important // This should reduce the number of disk seeks, which is important
// for HDDs. // for HDDs.

View file

@ -158,13 +158,13 @@ func (r *ReaderAt) MustFadviseSequentialRead(prefetch bool) {
} }
} }
// OpenReaderAt opens ReaderAt for reading from filename. // MustOpenReaderAt opens ReaderAt for reading from filename.
// //
// MustClose must be called on the returned ReaderAt when it is no longer needed. // MustClose must be called on the returned ReaderAt when it is no longer needed.
func OpenReaderAt(path string) (*ReaderAt, error) { func MustOpenReaderAt(path string) *ReaderAt {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot open file %q for reader: %w", path, err) logger.Panicf("FATAL: cannot open file %q for reading: %s", path, err)
} }
var r ReaderAt var r ReaderAt
r.f = f r.f = f
@ -172,7 +172,8 @@ func OpenReaderAt(path string) (*ReaderAt, error) {
if !*disableMmap { if !*disableMmap {
fi, err := f.Stat() fi, err := f.Stat()
if err != nil { if err != nil {
return nil, fmt.Errorf("error in stat: %w", err) MustClose(f)
logger.Panicf("FATAL: error in fstat(%q): %s", path, err)
} }
size := fi.Size() size := fi.Size()
bm := &pageCacheBitmap{ bm := &pageCacheBitmap{
@ -188,12 +189,12 @@ func OpenReaderAt(path string) (*ReaderAt, error) {
data, err := mmapFile(f, size) data, err := mmapFile(f, size)
if err != nil { if err != nil {
MustClose(f) MustClose(f)
return nil, fmt.Errorf("cannot init reader for %q: %w", path, err) logger.Panicf("FATAL: cannot mmap %q: %s", path, err)
} }
r.mmapData = data r.mmapData = data
} }
readersCount.Inc() readersCount.Inc()
return &r, nil return &r
} }
func pageCacheBitmapCleaner(pcbm *atomic.Value, stopCh <-chan struct{}) { func pageCacheBitmapCleaner(pcbm *atomic.Value, stopCh <-chan struct{}) {

View file

@ -22,10 +22,7 @@ func testReaderAt(t *testing.T, bufSize int) {
t.Fatalf("cannot create %q: %s", path, err) t.Fatalf("cannot create %q: %s", path, err)
} }
defer MustRemoveAll(path) defer MustRemoveAll(path)
r, err := OpenReaderAt(path) r := MustOpenReaderAt(path)
if err != nil {
t.Fatalf("error in OpenReaderAt(%q): %s", path, err)
}
defer r.MustClose() defer r.MustClose()
buf := make([]byte, bufSize) buf := make([]byte, bufSize)

View file

@ -29,10 +29,7 @@ func benchmarkReaderAtMustReadAt(b *testing.B, isMmap bool) {
b.Fatalf("cannot create %q: %s", path, err) b.Fatalf("cannot create %q: %s", path, err)
} }
defer MustRemoveAll(path) defer MustRemoveAll(path)
r, err := OpenReaderAt(path) r := MustOpenReaderAt(path)
if err != nil {
b.Fatalf("error in OpenReaderAt(%q): %s", path, err)
}
defer r.MustClose() defer r.MustClose()
b.ResetTimer() b.ResetTimer()

View file

@ -78,30 +78,15 @@ func openFilePart(path string) (*part, error) {
metaindexSize := fs.MustFileSize(metaindexPath) metaindexSize := fs.MustFileSize(metaindexPath)
indexPath := path + "/index.bin" indexPath := path + "/index.bin"
indexFile, err := fs.OpenReaderAt(indexPath) indexFile := fs.MustOpenReaderAt(indexPath)
if err != nil {
metaindexFile.MustClose()
return nil, fmt.Errorf("cannot open %q: %w", indexPath, err)
}
indexSize := fs.MustFileSize(indexPath) indexSize := fs.MustFileSize(indexPath)
itemsPath := path + "/items.bin" itemsPath := path + "/items.bin"
itemsFile, err := fs.OpenReaderAt(itemsPath) itemsFile := fs.MustOpenReaderAt(itemsPath)
if err != nil {
metaindexFile.MustClose()
indexFile.MustClose()
return nil, fmt.Errorf("cannot open %q: %w", itemsPath, err)
}
itemsSize := fs.MustFileSize(itemsPath) itemsSize := fs.MustFileSize(itemsPath)
lensPath := path + "/lens.bin" lensPath := path + "/lens.bin"
lensFile, err := fs.OpenReaderAt(lensPath) lensFile := fs.MustOpenReaderAt(lensPath)
if err != nil {
metaindexFile.MustClose()
indexFile.MustClose()
itemsFile.MustClose()
return nil, fmt.Errorf("cannot open %q: %w", lensPath, err)
}
lensSize := fs.MustFileSize(lensPath) lensSize := fs.MustFileSize(lensPath)
size := metaindexSize + indexSize + itemsSize + lensSize size := metaindexSize + indexSize + itemsSize + lensSize

View file

@ -60,27 +60,15 @@ func openFilePart(path string) (*part, error) {
} }
timestampsPath := path + "/timestamps.bin" timestampsPath := path + "/timestamps.bin"
timestampsFile, err := fs.OpenReaderAt(timestampsPath) timestampsFile := fs.MustOpenReaderAt(timestampsPath)
if err != nil {
return nil, fmt.Errorf("cannot open timestamps file: %w", err)
}
timestampsSize := fs.MustFileSize(timestampsPath) timestampsSize := fs.MustFileSize(timestampsPath)
valuesPath := path + "/values.bin" valuesPath := path + "/values.bin"
valuesFile, err := fs.OpenReaderAt(valuesPath) valuesFile := fs.MustOpenReaderAt(valuesPath)
if err != nil {
timestampsFile.MustClose()
return nil, fmt.Errorf("cannot open values file: %w", err)
}
valuesSize := fs.MustFileSize(valuesPath) valuesSize := fs.MustFileSize(valuesPath)
indexPath := path + "/index.bin" indexPath := path + "/index.bin"
indexFile, err := fs.OpenReaderAt(indexPath) indexFile := fs.MustOpenReaderAt(indexPath)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
return nil, fmt.Errorf("cannot open index file: %w", err)
}
indexSize := fs.MustFileSize(indexPath) indexSize := fs.MustFileSize(indexPath)
metaindexPath := path + "/metaindex.bin" metaindexPath := path + "/metaindex.bin"