lib/filestream: transform Open() -> MustOpen()

Callers of this function log the returned error and exit.
Let's log the error with the path to the filename and call stack
inside the function. This simplifies the code at callers' side
without reducing the level of debuggability.
This commit is contained in:
Aliaksandr Valialkin 2023-04-14 15:03:39 -07:00
parent fda1a54343
commit 5eb163a08a
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
7 changed files with 23 additions and 78 deletions

View file

@ -64,10 +64,7 @@ func (r *Reader) Path() string {
// //
// If nocache is set, then the reader doesn't pollute OS page cache. // If nocache is set, then the reader doesn't pollute OS page cache.
func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) { func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) {
r, err := Open(path, nocache) r := MustOpen(path, nocache)
if err != nil {
return nil, err
}
n, err := r.f.Seek(offset, io.SeekStart) n, err := r.f.Seek(offset, io.SeekStart)
if err != nil { if err != nil {
r.MustClose() r.MustClose()
@ -80,13 +77,13 @@ func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) {
return r, nil return r, nil
} }
// Open opens the file from the given path in nocache mode. // MustOpen opens the file from the given path in nocache mode.
// //
// If nocache is set, then the reader doesn't pollute OS page cache. // If nocache is set, then the reader doesn't pollute OS page cache.
func Open(path string, nocache bool) (*Reader, error) { func MustOpen(path string, nocache bool) *Reader {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, err logger.Panicf("FATAL: cannot open file: %s", err)
} }
r := &Reader{ r := &Reader{
f: f, f: f,
@ -96,10 +93,10 @@ func Open(path string, nocache bool) (*Reader, error) {
r.st.fd = f.Fd() r.st.fd = f.Fd()
} }
readersCount.Inc() readersCount.Inc()
return r, nil return r
} }
// MustClose closes the underlying file passed to Open. // MustClose closes the underlying file passed to MustOpen.
func (r *Reader) MustClose() { func (r *Reader) MustClose() {
if err := r.st.close(); err != nil { if err := r.st.close(); err != nil {
logger.Panicf("FATAL: cannot close streamTracker for file %q: %s", r.f.Name(), err) logger.Panicf("FATAL: cannot close streamTracker for file %q: %s", r.f.Name(), err)

View file

@ -42,10 +42,7 @@ func testWriteRead(t *testing.T, nocache bool, testStr string) {
} }
w.MustClose() w.MustClose()
r, err := Open("./nocache_test.txt", nocache) r := MustOpen("./nocache_test.txt", nocache)
if err != nil {
t.Fatalf("cannot open file: %s", err)
}
buf := make([]byte, len(testStr)) buf := make([]byte, len(testStr))
if _, err := io.ReadFull(r, buf); err != nil { if _, err := io.ReadFull(r, buf); err != nil {
t.Fatalf("unexpected error when reading: %s", err) t.Fatalf("unexpected error when reading: %s", err)

View file

@ -148,36 +148,23 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error {
} }
metaindexPath := filepath.Join(path, metaindexFilename) metaindexPath := filepath.Join(path, metaindexFilename)
metaindexFile, err := filestream.Open(metaindexPath, true) metaindexFile := filestream.MustOpen(metaindexPath, true)
if err != nil {
return fmt.Errorf("cannot open metaindex file in stream mode: %w", err) var err error
}
bsr.mrs, err = unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile) bsr.mrs, err = unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile)
metaindexFile.MustClose() metaindexFile.MustClose()
if err != nil { if err != nil {
return fmt.Errorf("cannot unmarshal metaindex rows from file %q: %w", metaindexPath, err) logger.Panicf("FATAL: cannot unmarshal metaindex rows from file %q: %s", metaindexPath, err)
} }
indexPath := filepath.Join(path, indexFilename) indexPath := filepath.Join(path, indexFilename)
indexFile, err := filestream.Open(indexPath, true) indexFile := filestream.MustOpen(indexPath, true)
if err != nil {
return fmt.Errorf("cannot open index file in stream mode: %w", err)
}
itemsPath := filepath.Join(path, itemsFilename) itemsPath := filepath.Join(path, itemsFilename)
itemsFile, err := filestream.Open(itemsPath, true) itemsFile := filestream.MustOpen(itemsPath, true)
if err != nil {
indexFile.MustClose()
return fmt.Errorf("cannot open items file in stream mode: %w", err)
}
lensPath := filepath.Join(path, lensFilename) lensPath := filepath.Join(path, lensFilename)
lensFile, err := filestream.Open(lensPath, true) lensFile := filestream.MustOpen(lensPath, true)
if err != nil {
indexFile.MustClose()
itemsFile.MustClose()
return fmt.Errorf("cannot open lens file in stream mode: %w", err)
}
bsr.path = path bsr.path = path
bsr.indexReader = indexFile bsr.indexReader = indexFile

View file

@ -74,10 +74,7 @@ func openFilePart(path string) (*part, error) {
} }
metaindexPath := filepath.Join(path, metaindexFilename) metaindexPath := filepath.Join(path, metaindexFilename)
metaindexFile, err := filestream.Open(metaindexPath, true) metaindexFile := filestream.MustOpen(metaindexPath, true)
if err != nil {
return nil, fmt.Errorf("cannot open %q: %w", metaindexPath, err)
}
metaindexSize := fs.MustFileSize(metaindexPath) metaindexSize := fs.MustFileSize(metaindexPath)
indexPath := filepath.Join(path, indexFilename) indexPath := filepath.Join(path, indexFilename)

View file

@ -100,10 +100,7 @@ func (q *queue) mustResetFiles() {
q.writer = w q.writer = w
q.readerPath = q.writerPath q.readerPath = q.writerPath
r, err := filestream.Open(q.readerPath, true) r := filestream.MustOpen(q.readerPath, true)
if err != nil {
logger.Panicf("FATAL: cannot open chunk file %q: %s", q.readerPath, err)
}
q.reader = r q.reader = r
if err := q.flushMetainfo(); err != nil { if err := q.flushMetainfo(); err != nil {
@ -559,10 +556,7 @@ func (q *queue) nextChunkFileForRead() error {
} }
q.readerLocalOffset = 0 q.readerLocalOffset = 0
q.readerPath = q.chunkFilePath(q.readerOffset) q.readerPath = q.chunkFilePath(q.readerOffset)
r, err := filestream.Open(q.readerPath, true) r := filestream.MustOpen(q.readerPath, true)
if err != nil {
return fmt.Errorf("cannot open chunk file %q: %w", q.readerPath, err)
}
q.reader = r q.reader = r
if err := q.flushMetainfo(); err != nil { if err := q.flushMetainfo(); err != nil {
return fmt.Errorf("cannot flush metainfo: %w", err) return fmt.Errorf("cannot flush metainfo: %w", err)

View file

@ -135,41 +135,20 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error {
} }
timestampsPath := filepath.Join(path, timestampsFilename) timestampsPath := filepath.Join(path, timestampsFilename)
timestampsFile, err := filestream.Open(timestampsPath, true) timestampsFile := filestream.MustOpen(timestampsPath, true)
if err != nil {
return fmt.Errorf("cannot open timestamps file in stream mode: %w", err)
}
valuesPath := filepath.Join(path, valuesFilename) valuesPath := filepath.Join(path, valuesFilename)
valuesFile, err := filestream.Open(valuesPath, true) valuesFile := filestream.MustOpen(valuesPath, true)
if err != nil {
timestampsFile.MustClose()
return fmt.Errorf("cannot open values file in stream mode: %w", err)
}
indexPath := filepath.Join(path, indexFilename) indexPath := filepath.Join(path, indexFilename)
indexFile, err := filestream.Open(indexPath, true) indexFile := filestream.MustOpen(indexPath, true)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
return fmt.Errorf("cannot open index file in stream mode: %w", err)
}
metaindexPath := filepath.Join(path, metaindexFilename) metaindexPath := filepath.Join(path, metaindexFilename)
metaindexFile, err := filestream.Open(metaindexPath, true) metaindexFile := filestream.MustOpen(metaindexPath, true)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
indexFile.MustClose()
return fmt.Errorf("cannot open metaindex file in stream mode: %w", err)
}
mrs, err := unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile) mrs, err := unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile)
metaindexFile.MustClose() metaindexFile.MustClose()
if err != nil { if err != nil {
timestampsFile.MustClose() logger.Panicf("FATAL: cannot unmarshal metaindex rows from file part %q: %s", metaindexPath, err)
valuesFile.MustClose()
indexFile.MustClose()
return fmt.Errorf("cannot unmarshal metaindex rows from file part %q: %w", metaindexPath, err)
} }
bsr.path = path bsr.path = path

View file

@ -67,13 +67,7 @@ func openFilePart(path string) (*part, error) {
indexSize := fs.MustFileSize(indexPath) indexSize := fs.MustFileSize(indexPath)
metaindexPath := filepath.Join(path, metaindexFilename) metaindexPath := filepath.Join(path, metaindexFilename)
metaindexFile, err := filestream.Open(metaindexPath, true) metaindexFile := filestream.MustOpen(metaindexPath, true)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
indexFile.MustClose()
return nil, fmt.Errorf("cannot open metaindex file: %w", err)
}
metaindexSize := fs.MustFileSize(metaindexPath) metaindexSize := fs.MustFileSize(metaindexPath)
size := timestampsSize + valuesSize + indexSize + metaindexSize size := timestampsSize + valuesSize + indexSize + metaindexSize