Skip to content

Commit

Permalink
Obtain a lock on filePathPrefix on dbnode startup. Close #641 (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
edkolev authored and richardartoul committed Feb 21, 2019
1 parent e0c976b commit 667f864
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/dbnode/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"net/http"
"os"
"os/signal"
"path"
"runtime"
"runtime/debug"
"syscall"
Expand Down Expand Up @@ -67,6 +68,7 @@ import (
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/postings/roaring"
xdocs "github.com/m3db/m3/src/x/docs"
"github.com/m3db/m3/src/x/lockfile"
"github.com/m3db/m3/src/x/mmap"
"github.com/m3db/m3/src/x/serialize"
xconfig "github.com/m3db/m3x/config"
Expand All @@ -87,6 +89,7 @@ const (
serverGracefulCloseTimeout = 10 * time.Second
bgProcessLimitInterval = 10 * time.Second
maxBgProcessLimitMonitorDuration = 5 * time.Minute
filePathPrefixLockFile = ".lock"
)

// RunOptions provides options for running the server
Expand Down Expand Up @@ -144,6 +147,29 @@ func Run(runOpts RunOptions) {
os.Exit(1)
}

newFileMode, err := cfg.Filesystem.ParseNewFileMode()
if err != nil {
logger.Fatalf("could not parse new file mode: %v", err)
}

newDirectoryMode, err := cfg.Filesystem.ParseNewDirectoryMode()
if err != nil {
logger.Fatalf("could not parse new directory mode: %v", err)
}

// Obtain a lock on `filePathPrefix`, or exit if another process already has it.
// The lock consists of a lock file (on the file system) and a lock in memory.
// When the process exits gracefully, both the lock file and the lock will be removed.
// If the process exits ungracefully, only the lock in memory will be removed, the lock
// file will remain on the file system. When a dbnode starts after an ungracefully stop,
// it will be able to acquire the lock despite the fact the the lock file exists.
lockPath := path.Join(cfg.Filesystem.FilePathPrefixOrDefault(), filePathPrefixLockFile)
fslock, err := lockfile.CreateAndAcquire(lockPath, newDirectoryMode)
if err != nil {
logger.Fatalf("could not acquire lock on %s: %v", lockPath, err)
}
defer fslock.Release()

go bgValidateProcessLimits(logger)
debug.SetGCPercent(cfg.GCPercentage)

Expand Down Expand Up @@ -290,16 +316,6 @@ func Run(runOpts RunOptions) {

opts = opts.SetRuntimeOptionsManager(runtimeOptsMgr)

newFileMode, err := cfg.Filesystem.ParseNewFileMode()
if err != nil {
logger.Fatalf("could not parse new file mode: %v", err)
}

newDirectoryMode, err := cfg.Filesystem.ParseNewDirectoryMode()
if err != nil {
logger.Fatalf("could not parse new directory mode: %v", err)
}

mmapCfg := cfg.Filesystem.MmapConfigurationOrDefault()
shouldUseHugeTLB := mmapCfg.HugeTLB.Enabled
if shouldUseHugeTLB {
Expand Down

0 comments on commit 667f864

Please sign in to comment.