mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
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:
parent
d544bfd73e
commit
36559dfec2
4 changed files with 32 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
||||||
package bytesutil
|
package bytesutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -25,6 +26,11 @@ type ByteBuffer struct {
|
||||||
B []byte
|
B []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path returns an unique id for bb.
|
||||||
|
func (bb *ByteBuffer) Path() string {
|
||||||
|
return fmt.Sprintf("ByteBuffer(%p)", bb)
|
||||||
|
}
|
||||||
|
|
||||||
// Reset resets bb.
|
// Reset resets bb.
|
||||||
func (bb *ByteBuffer) Reset() {
|
func (bb *ByteBuffer) Reset() {
|
||||||
bb.B = bb.B[:0]
|
bb.B = bb.B[:0]
|
||||||
|
@ -92,6 +98,11 @@ type reader struct {
|
||||||
readOffset int
|
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.
|
// Read reads up to len(p) bytes from bb.
|
||||||
func (r *reader) Read(p []byte) (int, error) {
|
func (r *reader) Read(p []byte) (int, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -17,12 +17,14 @@ const dontNeedBlockSize = 16 * 1024 * 1024
|
||||||
|
|
||||||
// ReadCloser is a standard interface for filestream Reader.
|
// ReadCloser is a standard interface for filestream Reader.
|
||||||
type ReadCloser interface {
|
type ReadCloser interface {
|
||||||
|
Path() string
|
||||||
Read(p []byte) (int, error)
|
Read(p []byte) (int, error)
|
||||||
MustClose()
|
MustClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteCloser is a standard interface for filestream Writer.
|
// WriteCloser is a standard interface for filestream Writer.
|
||||||
type WriteCloser interface {
|
type WriteCloser interface {
|
||||||
|
Path() string
|
||||||
Write(p []byte) (int, error)
|
Write(p []byte) (int, error)
|
||||||
MustClose()
|
MustClose()
|
||||||
}
|
}
|
||||||
|
@ -53,6 +55,11 @@ type Reader struct {
|
||||||
st streamTracker
|
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.
|
// 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.
|
// If nocache is set, then the reader doesn't pollute OS page cache.
|
||||||
|
@ -171,6 +178,11 @@ type Writer struct {
|
||||||
st streamTracker
|
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.
|
// 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.
|
// The file at path is created if it is missing.
|
||||||
|
|
|
@ -353,16 +353,16 @@ func ReadFullData(r io.Reader, data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustWriteData writes data to w.
|
// MustWriteData writes data to w.
|
||||||
func MustWriteData(w io.Writer, data []byte) {
|
func MustWriteData(w filestream.WriteCloser, data []byte) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n, err := w.Write(data)
|
n, err := w.Write(data)
|
||||||
if err != nil {
|
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) {
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@ package storage
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -18,12 +17,8 @@ import (
|
||||||
type blockStreamWriter struct {
|
type blockStreamWriter struct {
|
||||||
compressLevel int
|
compressLevel int
|
||||||
|
|
||||||
// Use io.Writer type for timestampsWriter and valuesWriter
|
timestampsWriter filestream.WriteCloser
|
||||||
// in order to remove I2I conversion in WriteExternalBlock
|
valuesWriter filestream.WriteCloser
|
||||||
// when passing them to fs.MustWriteData
|
|
||||||
timestampsWriter io.Writer
|
|
||||||
valuesWriter io.Writer
|
|
||||||
|
|
||||||
indexWriter filestream.WriteCloser
|
indexWriter filestream.WriteCloser
|
||||||
metaindexWriter filestream.WriteCloser
|
metaindexWriter filestream.WriteCloser
|
||||||
|
|
||||||
|
@ -47,11 +42,6 @@ type blockStreamWriter struct {
|
||||||
prevTimestampsBlockOffset uint64
|
prevTimestampsBlockOffset uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bsw *blockStreamWriter) assertWriteClosers() {
|
|
||||||
_ = bsw.timestampsWriter.(filestream.WriteCloser)
|
|
||||||
_ = bsw.valuesWriter.(filestream.WriteCloser)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes bsw with the given writers.
|
// Init initializes bsw with the given writers.
|
||||||
func (bsw *blockStreamWriter) reset() {
|
func (bsw *blockStreamWriter) reset() {
|
||||||
bsw.compressLevel = 0
|
bsw.compressLevel = 0
|
||||||
|
@ -86,8 +76,6 @@ func (bsw *blockStreamWriter) InitFromInmemoryPart(mp *inmemoryPart, compressLev
|
||||||
bsw.valuesWriter = &mp.valuesData
|
bsw.valuesWriter = &mp.valuesData
|
||||||
bsw.indexWriter = &mp.indexData
|
bsw.indexWriter = &mp.indexData
|
||||||
bsw.metaindexWriter = &mp.metaindexData
|
bsw.metaindexWriter = &mp.metaindexData
|
||||||
|
|
||||||
bsw.assertWriteClosers()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitFromFilePart initializes bsw from a file-based part on the given path.
|
// 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.indexWriter = indexFile
|
||||||
bsw.metaindexWriter = metaindexFile
|
bsw.metaindexWriter = metaindexFile
|
||||||
|
|
||||||
bsw.assertWriteClosers()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,8 +147,8 @@ func (bsw *blockStreamWriter) MustClose() {
|
||||||
fs.MustWriteData(bsw.metaindexWriter, bsw.compressedMetaindexData)
|
fs.MustWriteData(bsw.metaindexWriter, bsw.compressedMetaindexData)
|
||||||
|
|
||||||
// Close writers.
|
// Close writers.
|
||||||
bsw.timestampsWriter.(filestream.WriteCloser).MustClose()
|
bsw.timestampsWriter.MustClose()
|
||||||
bsw.valuesWriter.(filestream.WriteCloser).MustClose()
|
bsw.valuesWriter.MustClose()
|
||||||
bsw.indexWriter.MustClose()
|
bsw.indexWriter.MustClose()
|
||||||
bsw.metaindexWriter.MustClose()
|
bsw.metaindexWriter.MustClose()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue