diff --git a/lib/bytesutil/bytebuffer.go b/lib/bytesutil/bytebuffer.go index 7a1351fb36..1fccdb034d 100644 --- a/lib/bytesutil/bytebuffer.go +++ b/lib/bytesutil/bytebuffer.go @@ -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 diff --git a/lib/filestream/filestream.go b/lib/filestream/filestream.go index 2f0e4e5953..6b5b3d2332 100644 --- a/lib/filestream/filestream.go +++ b/lib/filestream/filestream.go @@ -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. diff --git a/lib/fs/fs.go b/lib/fs/fs.go index 11e46d80f6..e7fe8450a6 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -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()) } } diff --git a/lib/storage/block_stream_writer.go b/lib/storage/block_stream_writer.go index b60662c6e9..a43725f3cb 100644 --- a/lib/storage/block_stream_writer.go +++ b/lib/storage/block_stream_writer.go @@ -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()