Skip to content

Commit

Permalink
Add flag to avoid lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-sorint committed Feb 26, 2021
1 parent dabfce5 commit 2d0b8e5
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 34 deletions.
43 changes: 25 additions & 18 deletions cmd/keeper/cmd/keeper.go
Expand Up @@ -115,6 +115,7 @@ type config struct {

canBeMaster bool
canBeSynchronousReplica bool
disableDataDirLocking bool
}

var cfg config
Expand Down Expand Up @@ -142,6 +143,7 @@ func init() {

CmdKeeper.PersistentFlags().BoolVar(&cfg.canBeMaster, "can-be-master", true, "prevent keeper from being elected as master")
CmdKeeper.PersistentFlags().BoolVar(&cfg.canBeSynchronousReplica, "can-be-synchronous-replica", true, "prevent keeper from being chosen as synchronous replica")
CmdKeeper.PersistentFlags().BoolVar(&cfg.disableDataDirLocking, "disable-data-dir-locking", false, "disable locking on data dir. Warning! It'll cause data corruptions if two keepers are concurrently running with the same data dir.")

if err := CmdKeeper.PersistentFlags().MarkDeprecated("id", "please use --uid"); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -2032,26 +2034,29 @@ func keeper(c *cobra.Command, args []string) {
// Open (and create if needed) the lock file.
// There is no need to clean up this file since we don't use the file as an actual lock. We get a lock
// on the file. So the lock get released when our process stops (or log.Fatalfs).
lockFileName := filepath.Join(cfg.dataDir, "lock")
lockFile, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
}
var lockFile *os.File
if !cfg.disableDataDirLocking {
lockFileName := filepath.Join(cfg.dataDir, "lock")
lockFile, err = os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
}

// Get a lock on our lock file.
ft := &syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: int16(io.SeekStart),
Start: 0,
Len: 0, // Entire file.
}
// Get a lock on our lock file.
ft := &syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: int16(io.SeekStart),
Start: 0,
Len: 0, // Entire file.
}

err = syscall.FcntlFlock(lockFile.Fd(), syscall.F_SETLK, ft)
if err != nil {
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
}
err = syscall.FcntlFlock(lockFile.Fd(), syscall.F_SETLK, ft)
if err != nil {
log.Fatalf("cannot take exclusive lock on data dir %q: %v", lockFileName, err)
}

log.Infow("exclusive lock on data dir taken")
log.Infow("exclusive lock on data dir taken")
}

if cfg.uid != "" {
if !pg.IsValidReplSlotName(cfg.uid) {
Expand Down Expand Up @@ -2084,5 +2089,7 @@ func keeper(c *cobra.Command, args []string) {

<-end

lockFile.Close()
if !cfg.disableDataDirLocking {
lockFile.Close()
}
}
5 changes: 4 additions & 1 deletion doc/commands/stolon-keeper.md
Expand Up @@ -13,8 +13,11 @@ stolon-keeper [flags]
### Options

```
--can-be-master prevent keeper from being elected as master (default true)
--can-be-synchronous-replica prevent keeper from being chosen as synchronous replica (default true)
--cluster-name string cluster name
--data-dir string data directory
--disable-data-dir-locking disable locking on data dir. Warning! It'll cause data corruptions if two keepers are concurrently running with the same data dir.
-h, --help help for stolon-keeper
--kube-resource-kind string the k8s resource kind to be used to store stolon clusterdata and do sentinel leader election (only "configmap" is currently supported)
--log-color enable color in log output (default if attached to a terminal)
Expand Down Expand Up @@ -44,4 +47,4 @@ stolon-keeper [flags]
--uid string keeper uid (must be unique in the cluster and can contain only lower-case letters, numbers and the underscore character). If not provided a random uid will be generated.
```

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolon-proxy.md
Expand Up @@ -35,4 +35,4 @@ stolon-proxy [flags]
--tcp-keepalive-interval int set tcp keepalive interval (seconds)
```

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolon-sentinel.md
Expand Up @@ -30,4 +30,4 @@ stolon-sentinel [flags]
--store-timeout duration store request timeout (default 5s)
```

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl.md
Expand Up @@ -44,4 +44,4 @@ stolonctl [flags]
* [stolonctl update](stolonctl_update.md) - Update a cluster specification
* [stolonctl version](stolonctl_version.md) - Display the version

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_clusterdata.md
Expand Up @@ -38,4 +38,4 @@ Manage current cluster data
* [stolonctl clusterdata read](stolonctl_clusterdata_read.md) - Retrieve the current cluster data
* [stolonctl clusterdata write](stolonctl_clusterdata_write.md) - Write cluster data

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_clusterdata_read.md
Expand Up @@ -41,4 +41,4 @@ stolonctl clusterdata read [flags]

* [stolonctl clusterdata](stolonctl_clusterdata.md) - Manage current cluster data

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_clusterdata_write.md
Expand Up @@ -42,4 +42,4 @@ stolonctl clusterdata write [flags]

* [stolonctl clusterdata](stolonctl_clusterdata.md) - Manage current cluster data

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_failkeeper.md
Expand Up @@ -40,4 +40,4 @@ stolonctl failkeeper [keeper uid] [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_init.md
Expand Up @@ -42,4 +42,4 @@ stolonctl init [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_promote.md
Expand Up @@ -41,4 +41,4 @@ stolonctl promote [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_register.md
Expand Up @@ -51,4 +51,4 @@ stolonctl register [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_removekeeper.md
Expand Up @@ -40,4 +40,4 @@ stolonctl removekeeper [keeper uid] [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_spec.md
Expand Up @@ -41,4 +41,4 @@ stolonctl spec [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_status.md
Expand Up @@ -41,4 +41,4 @@ stolonctl status [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_update.md
Expand Up @@ -42,4 +42,4 @@ stolonctl update [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021
2 changes: 1 addition & 1 deletion doc/commands/stolonctl_version.md
Expand Up @@ -40,4 +40,4 @@ stolonctl version [flags]

* [stolonctl](stolonctl.md) - stolon command line client

###### Auto generated by spf13/cobra on 6-Mar-2020
###### Auto generated by spf13/cobra on 24-Feb-2021

0 comments on commit 2d0b8e5

Please sign in to comment.