Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
enable lockfile by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav Paul committed Aug 9, 2019
1 parent 50c26b9 commit 871c19a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,18 @@ It is expected that the CO will connect to the plugin through the unix socket an

### Locking

If the `-lockfile` parameter is specified or the `CSILVM_LOCKFILE_PATH` environment variable is set,
an exclusive `flock()` is held on that file whenever any lvm2 command-line utility is invoked. This prevents
multiple CSILVM processes from performing concurrent lvm operations that can
lead to deadlocks in the underlying lvm2 implementation. For example,
Any command-line invocations executed by the `csilvm` process first acquires a
lock on a lockfile. The path to the lockfile can be overridden using the
`-lockfile=` option or the `CSILVM_LOCKFILE_PATH` environment variable. The
locking behaviour can be disabled by setting the `-lockfile=` option to the
empty string. The purpose of locking around command-line invocations is to
prevent multiple `csilvm` processes from executing concurrent `lvm2` commands.
This works around deadlocks in lvm2. For example,
https://jira.mesosphere.com/browse/DCOS_OSS-5434 and
https://github.com/lvmteam/lvm2/issues/23.

By default no lock file is specified and no inter-process synchronization is
performed. You are encouraged to enable locking by setting the `-lockfile=`
option, for example `-lockfile=/run/csilvm.lock`.
By default the lock file is created at `/run/csilvm.lock` so it is assumed that
the `/run` directory exists and is writable by the `csilvm` process.


### Logging
Expand Down
4 changes: 2 additions & 2 deletions cmd/csilvm/csilvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
var probeModulesF stringsFlag
flag.Var(&probeModulesF, "probe-module", "Probe checks that the kernel module is loaded")
nodeIDF := flag.String("node-id", "", "The node ID reported via the CSI Node gRPC service")
lockFilePathF := flag.String("lockfile", "", "The path to the lock file used to prevent concurrent lvm invocation by multiple csilvm instances")
lockFilePathF := flag.String("lockfile", "/run/csilvm.lock", "The path to the lock file used to prevent concurrent lvm invocation by multiple csilvm instances")
// Metrics-related flags
statsdUDPHostEnvVarF := flag.String("statsd-udp-host-env-var", "", "The name of the environment variable containing the host where a statsd service is listening for stats over UDP")
statsdUDPPortEnvVarF := flag.String("statsd-udp-port-env-var", "", "The name of the environment variable containing the port where a statsd service is listening for stats over UDP")
Expand Down Expand Up @@ -95,7 +95,7 @@ func main() {
// Unlink the domain socket in case it is left lying around from a
// previous run. err return is not really interesting because it is
// normal for this to fail if the process is starting for the first time.
logger.Printf("Unlinking %s", sock)
logger.Printf("Unlinking socket file: %q", sock)
syscall.Unlink(sock)
// Setup socket listener
lis, err := net.Listen("unix", sock)
Expand Down
13 changes: 13 additions & 0 deletions pkg/lvm/lock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lvm

import (
"fmt"

"github.com/gofrs/flock"
)

Expand All @@ -13,5 +15,16 @@ var lvmlock *flock.Flock
// - https://jira.mesosphere.com/browse/DCOS_OSS-5434
// - https://github.com/lvmteam/lvm2/issues/23
func SetLockFilePath(filepath string) {
log.Printf("using lock file at %q", filepath)
lvmlock = flock.New(filepath)
log.Printf("checking if lock can be acquired")
err := lvmlock.Lock()
if err != nil {
panic(fmt.Sprintf("cannot acquire lock: %v", err))
}
log.Printf("lock acquired, releasing lock")
if err := lvmlock.Unlock(); err != nil {
panic(fmt.Sprintf("cannot release lock: %v", err))
}
log.Printf("configured lock file")
}

0 comments on commit 871c19a

Please sign in to comment.