lib/fs: improve error logging inside MustWriteData

Log the path to file on errors inside MustWriteData().
This improves debuggability of errors, which may occur inside MustWriteData().
This commit is contained in:
Aliaksandr Valialkin 2023-04-14 14:32:43 -07:00
parent 4b43c91f8c
commit bd6de6406a
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
4 changed files with 32 additions and 23 deletions

View file

@ -1,6 +1,7 @@
package bytesutil
import (
"fmt"
"io"
"sync"
@ -25,6 +26,11 @@ type ByteBuffer struct {
B []byte
}
// Path returns an unique id for bb.
func (bb *ByteBuffer) Path() string {
return fmt.Sprintf("ByteBuffer(%p)", bb)
}
// Reset resets bb.
func (bb *ByteBuffer) Reset() {
bb.B = bb.B[:0]
@ -92,6 +98,11 @@ type reader struct {
readOffset int
}
// Path returns an unique id for the underlying ByteBuffer.
func (r *reader) Path() string {
return r.bb.Path()
}
// Read reads up to len(p) bytes from bb.
func (r *reader) Read(p []byte) (int, error) {
var err error

View file

@ -17,12 +17,14 @@ const dontNeedBlockSize = 16 * 1024 * 1024
// ReadCloser is a standard interface for filestream Reader.
type ReadCloser interface {
Path() string
Read(p []byte) (int, error)
MustClose()
}
// WriteCloser is a standard interface for filestream Writer.
type WriteCloser interface {
Path() string
Write(p []byte) (int, error)
MustClose()
}
@ -53,6 +55,11 @@ type Reader struct {
st streamTracker
}
// Path returns the path to r
func (r *Reader) Path() string {
return r.f.Name()
}
// OpenReaderAt opens the file at the given path in nocache mode at the given offset.
//
// If nocache is set, then the reader doesn't pollute OS page cache.
@ -171,6 +178,11 @@ type Writer struct {
st streamTracker
}
// Path returns the path to r
func (w *Writer) Path() string {
return w.f.Name()
}
// OpenWriterAt opens the file at path in nocache mode for writing at the given offset.
//
// The file at path is created if it is missing.

View file

@ -353,16 +353,16 @@ func ReadFullData(r io.Reader, data []byte) error {
}
// MustWriteData writes data to w.
func MustWriteData(w io.Writer, data []byte) {
func MustWriteData(w filestream.WriteCloser, data []byte) {
if len(data) == 0 {
return
}
n, err := w.Write(data)
if err != nil {
logger.Panicf("FATAL: cannot write %d bytes: %s", len(data), err)
logger.Panicf("FATAL: cannot write %d bytes to %s: %s", len(data), w.Path(), err)
}
if n != len(data) {
logger.Panicf("BUG: writer wrote %d bytes instead of %d bytes", n, len(data))
logger.Panicf("BUG: writer wrote %d bytes instead of %d bytes to %s", n, len(data), w.Path())
}
}

View file

@ -3,7 +3,6 @@ package storage
import (
"bytes"
"fmt"
"io"
"path/filepath"
"sync"
"sync/atomic"
@ -18,14 +17,10 @@ import (
type blockStreamWriter struct {
compressLevel int
// Use io.Writer type for timestampsWriter and valuesWriter
// in order to remove I2I conversion in WriteExternalBlock
// when passing them to fs.MustWriteData
timestampsWriter io.Writer
valuesWriter io.Writer
indexWriter filestream.WriteCloser
metaindexWriter filestream.WriteCloser
timestampsWriter filestream.WriteCloser
valuesWriter filestream.WriteCloser
indexWriter filestream.WriteCloser
metaindexWriter filestream.WriteCloser
mr metaindexRow
@ -47,11 +42,6 @@ type blockStreamWriter struct {
prevTimestampsBlockOffset uint64
}
func (bsw *blockStreamWriter) assertWriteClosers() {
_ = bsw.timestampsWriter.(filestream.WriteCloser)
_ = bsw.valuesWriter.(filestream.WriteCloser)
}
// Init initializes bsw with the given writers.
func (bsw *blockStreamWriter) reset() {
bsw.compressLevel = 0
@ -86,8 +76,6 @@ func (bsw *blockStreamWriter) InitFromInmemoryPart(mp *inmemoryPart, compressLev
bsw.valuesWriter = &mp.valuesData
bsw.indexWriter = &mp.indexData
bsw.metaindexWriter = &mp.metaindexData
bsw.assertWriteClosers()
}
// InitFromFilePart initializes bsw from a file-based part on the given path.
@ -144,8 +132,6 @@ func (bsw *blockStreamWriter) InitFromFilePart(path string, nocache bool, compre
bsw.indexWriter = indexFile
bsw.metaindexWriter = metaindexFile
bsw.assertWriteClosers()
return nil
}
@ -161,8 +147,8 @@ func (bsw *blockStreamWriter) MustClose() {
fs.MustWriteData(bsw.metaindexWriter, bsw.compressedMetaindexData)
// Close writers.
bsw.timestampsWriter.(filestream.WriteCloser).MustClose()
bsw.valuesWriter.(filestream.WriteCloser).MustClose()
bsw.timestampsWriter.MustClose()
bsw.valuesWriter.MustClose()
bsw.indexWriter.MustClose()
bsw.metaindexWriter.MustClose()