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.
func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) {
r, err := Open(path, nocache)
if err != nil {
return nil, err
}
r := MustOpen(path, nocache)
n, err := r.f.Seek(offset, io.SeekStart)
if err != nil {
r.MustClose()
@ -80,13 +77,13 @@ func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) {
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.
func Open(path string, nocache bool) (*Reader, error) {
func MustOpen(path string, nocache bool) *Reader {
f, err := os.Open(path)
if err != nil {
return nil, err
logger.Panicf("FATAL: cannot open file: %s", err)
}
r := &Reader{
f: f,
@ -96,10 +93,10 @@ func Open(path string, nocache bool) (*Reader, error) {
r.st.fd = f.Fd()
}
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() {
if err := r.st.close(); err != nil {
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()
r, err := Open("./nocache_test.txt", nocache)
if err != nil {
t.Fatalf("cannot open file: %s", err)
}
r := MustOpen("./nocache_test.txt", nocache)
buf := make([]byte, len(testStr))
if _, err := io.ReadFull(r, buf); err != nil {
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)
metaindexFile, err := filestream.Open(metaindexPath, true)
if err != nil {
return fmt.Errorf("cannot open metaindex file in stream mode: %w", err)
}
metaindexFile := filestream.MustOpen(metaindexPath, true)
var err error
bsr.mrs, err = unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile)
metaindexFile.MustClose()
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)
indexFile, err := filestream.Open(indexPath, true)
if err != nil {
return fmt.Errorf("cannot open index file in stream mode: %w", err)
}
indexFile := filestream.MustOpen(indexPath, true)
itemsPath := filepath.Join(path, itemsFilename)
itemsFile, err := filestream.Open(itemsPath, true)
if err != nil {
indexFile.MustClose()
return fmt.Errorf("cannot open items file in stream mode: %w", err)
}
itemsFile := filestream.MustOpen(itemsPath, true)
lensPath := filepath.Join(path, lensFilename)
lensFile, err := filestream.Open(lensPath, true)
if err != nil {
indexFile.MustClose()
itemsFile.MustClose()
return fmt.Errorf("cannot open lens file in stream mode: %w", err)
}
lensFile := filestream.MustOpen(lensPath, true)
bsr.path = path
bsr.indexReader = indexFile

View file

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

View file

@ -100,10 +100,7 @@ func (q *queue) mustResetFiles() {
q.writer = w
q.readerPath = q.writerPath
r, err := filestream.Open(q.readerPath, true)
if err != nil {
logger.Panicf("FATAL: cannot open chunk file %q: %s", q.readerPath, err)
}
r := filestream.MustOpen(q.readerPath, true)
q.reader = r
if err := q.flushMetainfo(); err != nil {
@ -559,10 +556,7 @@ func (q *queue) nextChunkFileForRead() error {
}
q.readerLocalOffset = 0
q.readerPath = q.chunkFilePath(q.readerOffset)
r, err := filestream.Open(q.readerPath, true)
if err != nil {
return fmt.Errorf("cannot open chunk file %q: %w", q.readerPath, err)
}
r := filestream.MustOpen(q.readerPath, true)
q.reader = r
if err := q.flushMetainfo(); err != nil {
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)
timestampsFile, err := filestream.Open(timestampsPath, true)
if err != nil {
return fmt.Errorf("cannot open timestamps file in stream mode: %w", err)
}
timestampsFile := filestream.MustOpen(timestampsPath, true)
valuesPath := filepath.Join(path, valuesFilename)
valuesFile, err := filestream.Open(valuesPath, true)
if err != nil {
timestampsFile.MustClose()
return fmt.Errorf("cannot open values file in stream mode: %w", err)
}
valuesFile := filestream.MustOpen(valuesPath, true)
indexPath := filepath.Join(path, indexFilename)
indexFile, err := filestream.Open(indexPath, true)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
return fmt.Errorf("cannot open index file in stream mode: %w", err)
}
indexFile := filestream.MustOpen(indexPath, true)
metaindexPath := filepath.Join(path, metaindexFilename)
metaindexFile, err := filestream.Open(metaindexPath, true)
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
indexFile.MustClose()
return fmt.Errorf("cannot open metaindex file in stream mode: %w", err)
}
metaindexFile := filestream.MustOpen(metaindexPath, true)
mrs, err := unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile)
metaindexFile.MustClose()
if err != nil {
timestampsFile.MustClose()
valuesFile.MustClose()
indexFile.MustClose()
return fmt.Errorf("cannot unmarshal metaindex rows from file part %q: %w", metaindexPath, err)
logger.Panicf("FATAL: cannot unmarshal metaindex rows from file part %q: %s", metaindexPath, err)
}
bsr.path = path

View file

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