lib/fs: convert CreateFlockFile to MustCreateFlockFile

Callers of CreateFlockFile log the returned err and exit.
It is better to log the error inside the MustCreateFlockFile together with the path
to the specified directory and the call stack. This simplifies
the code at the callers' side while leaving the debuggability at the same level.
This commit is contained in:
Aliaksandr Valialkin 2023-04-14 19:49:54 -07:00
parent c0b852d50d
commit 2a3b19e1d2
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
6 changed files with 14 additions and 34 deletions

View file

@ -46,10 +46,7 @@ func (r *Restore) Run() error {
// Make sure VictoriaMetrics doesn't run during the restore process.
fs.MustMkdirIfNotExist(r.Dst.Dir)
flockF, err := fs.CreateFlockFile(r.Dst.Dir)
if err != nil {
return fmt.Errorf("cannot create lock file in %q; make sure VictoriaMetrics doesn't use the dir; error: %w", r.Dst.Dir, err)
}
flockF := fs.MustCreateFlockFile(r.Dst.Dir)
defer fs.MustClose(flockF)
if err := createRestoreLock(r.Dst.Dir); err != nil {

View file

@ -362,14 +362,18 @@ func MustWriteData(w filestream.WriteCloser, data []byte) {
}
}
// CreateFlockFile creates FlockFilename file in the directory dir
// MustCreateFlockFile creates FlockFilename file in the directory dir
// and returns the handler to the file.
func CreateFlockFile(dir string) (*os.File, error) {
flockFile := filepath.Join(dir, FlockFilename)
return createFlockFile(flockFile)
func MustCreateFlockFile(dir string) *os.File {
flockFilepath := filepath.Join(dir, FlockFilename)
f, err := createFlockFile(flockFilepath)
if err != nil {
logger.Panicf("FATAL: cannot create lock file: %s; make sure a single process has exclusive access to %q", err, dir)
}
return f
}
// FlockFilename is the filename for the file created by CreateFlockFile().
// FlockFilename is the filename for the file created by MustCreateFlockFile().
const FlockFilename = "flock.lock"
// MustGetFreeSpace returns free space for the given directory path.

View file

@ -331,11 +331,7 @@ func OpenTable(path string, flushCallback func(), prepareBlock PrepareBlockCallb
fs.MustMkdirIfNotExist(path)
// Protect from concurrent opens.
flockF, err := fs.CreateFlockFile(path)
if err != nil {
return nil, fmt.Errorf("cannot create lock file in %q; "+
"make sure the dir isn't used by other processes or manually delete the file if you recover from abrupt VictoriaMetrics crash; error: %w", path, err)
}
flockF := fs.MustCreateFlockFile(path)
// Open table parts.
pws, err := openParts(path)

View file

@ -144,14 +144,6 @@ func mustOpenInternal(path, name string, chunkFileSize, maxBlockSize, maxPending
return q
}
func mustCreateFlockFile(path string) *os.File {
f, err := fs.CreateFlockFile(path)
if err != nil {
logger.Panicf("FATAL: %s", err)
}
return f
}
func tryOpeningQueue(path, name string, chunkFileSize, maxBlockSize, maxPendingBytes uint64) (*queue, error) {
// Protect from concurrent opens.
var q queue
@ -178,7 +170,7 @@ func tryOpeningQueue(path, name string, chunkFileSize, maxBlockSize, maxPendingB
}
fs.MustMkdirIfNotExist(path)
q.flockF = mustCreateFlockFile(path)
q.flockF = fs.MustCreateFlockFile(path)
mustCloseFlockF := true
defer func() {
if mustCloseFlockF {

View file

@ -167,12 +167,7 @@ func OpenStorage(path string, retentionMsecs int64, maxHourlySeries, maxDailySer
}
// Protect from concurrent opens.
flockF, err := fs.CreateFlockFile(path)
if err != nil {
return nil, fmt.Errorf("cannot create lock file in %q; "+
"make sure the dir isn't used by other processes or manually delete the file if you recover from abrupt VictoriaMetrics crash; error: %w", path, err)
}
s.flockF = flockF
s.flockF = fs.MustCreateFlockFile(path)
// Check whether restore process finished successfully
restoreLockF := filepath.Join(path, backupnames.RestoreInProgressFilename)

View file

@ -86,11 +86,7 @@ func openTable(path string, s *Storage) (*table, error) {
fs.MustMkdirIfNotExist(path)
// Protect from concurrent opens.
flockF, err := fs.CreateFlockFile(path)
if err != nil {
return nil, fmt.Errorf("cannot create lock file in %q; "+
"make sure the dir isn't used by other processes or manually delete the file if you recover from abrupt VictoriaMetrics crash; error: %w", path, err)
}
flockF := fs.MustCreateFlockFile(path)
// Create directories for small and big partitions if they don't exist yet.
smallPartitionsPath := filepath.Join(path, smallDirname)