mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-10 15:14:09 +00:00
lib: move common code for creating flock.lock file into fs.CreateFlockFile
This commit is contained in:
parent
ad8a43b4e1
commit
0967683ae9
4 changed files with 23 additions and 20 deletions
15
lib/fs/fs.go
15
lib/fs/fs.go
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/filestream"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
"github.com/VictoriaMetrics/metrics"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// ReadAtCloser is rand-access read interface.
|
||||
|
@ -385,3 +386,17 @@ func MustWriteData(w io.Writer, data []byte) {
|
|||
logger.Panicf("BUG: writer wrote %d bytes instead of %d bytes", n, len(data))
|
||||
}
|
||||
}
|
||||
|
||||
// CreateFlockFile creates flock.lock file in the directory dir
|
||||
// and returns the handler to the file.
|
||||
func CreateFlockFile(dir string) (*os.File, error) {
|
||||
flockFile := dir + "/flock.lock"
|
||||
flockF, err := os.Create(flockFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create lock file %q: %s", flockFile, err)
|
||||
}
|
||||
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
return nil, fmt.Errorf("cannot acquire lock on file %q: %s", flockFile, err)
|
||||
}
|
||||
return flockF, nil
|
||||
}
|
||||
|
|
|
@ -134,13 +134,9 @@ func OpenTable(path string) (*Table, error) {
|
|||
}
|
||||
|
||||
// Protect from concurrent opens.
|
||||
flockFile := path + "/flock.lock"
|
||||
flockF, err := os.Create(flockFile)
|
||||
flockF, err := fs.CreateFlockFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create lock file %q: %s", flockFile, err)
|
||||
}
|
||||
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
return nil, fmt.Errorf("cannot acquire lock on file %q: %s", flockFile, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Open table parts.
|
||||
|
|
|
@ -21,7 +21,6 @@ import (
|
|||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/timerpool"
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const maxRetentionMonths = 12 * 100
|
||||
|
@ -103,13 +102,10 @@ func OpenStorage(path string, retentionMonths int) (*Storage, error) {
|
|||
return nil, fmt.Errorf("cannot create %q: %s", snapshotsPath, err)
|
||||
}
|
||||
|
||||
flockFile := path + "/flock.lock"
|
||||
flockF, err := os.Create(flockFile)
|
||||
// Protect from concurrent opens.
|
||||
flockF, err := fs.CreateFlockFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create lock file %q: %s", flockFile, err)
|
||||
}
|
||||
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
return nil, fmt.Errorf("cannot acquire lock on file %q: %s", flockFile, err)
|
||||
return nil, err
|
||||
}
|
||||
s.flockF = flockF
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// table represents a single table with time series data.
|
||||
|
@ -84,13 +83,10 @@ func openTable(path string, retentionMonths int, getDeletedMetricIDs func() map[
|
|||
return nil, fmt.Errorf("cannot create directory for table %q: %s", path, err)
|
||||
}
|
||||
|
||||
flockFile := path + "/flock.lock"
|
||||
flockF, err := os.Create(flockFile)
|
||||
// Protect from concurrent opens.
|
||||
flockF, err := fs.CreateFlockFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create lock file %q: %s", flockFile, err)
|
||||
}
|
||||
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
return nil, fmt.Errorf("cannot acquire lock on file %q: %s", flockFile, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create directories for small and big partitions if they don't exist yet.
|
||||
|
|
Loading…
Reference in a new issue