From 0967683ae96e8aebf3c0d7b25f22b580920bfaf7 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 13 Aug 2019 01:45:22 +0300 Subject: [PATCH] lib: move common code for creating flock.lock file into fs.CreateFlockFile --- lib/fs/fs.go | 15 +++++++++++++++ lib/mergeset/table.go | 8 ++------ lib/storage/storage.go | 10 +++------- lib/storage/table.go | 10 +++------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/fs/fs.go b/lib/fs/fs.go index ddc65390f..617c835a6 100644 --- a/lib/fs/fs.go +++ b/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 +} diff --git a/lib/mergeset/table.go b/lib/mergeset/table.go index 06110b395..5daa9de2b 100644 --- a/lib/mergeset/table.go +++ b/lib/mergeset/table.go @@ -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. diff --git a/lib/storage/storage.go b/lib/storage/storage.go index b077a2c0e..84d6880be 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -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 diff --git a/lib/storage/table.go b/lib/storage/table.go index 4acc27b0c..9a0cc3d6e 100644 --- a/lib/storage/table.go +++ b/lib/storage/table.go @@ -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.