timeout after 5 seconds when client opens a data directory (#6348)

This commit is contained in:
Jasmine Dahilig 2019-09-24 16:28:21 -07:00 committed by GitHub
parent 0d09b564fa
commit 0780adfa7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,8 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/boltdb/bolt"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
trstate "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state" trstate "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
dmstate "github.com/hashicorp/nomad/client/devicemanager/state" dmstate "github.com/hashicorp/nomad/client/devicemanager/state"
@ -120,9 +122,14 @@ func NewBoltStateDB(logger hclog.Logger, stateDir string) (StateDB, error) {
} }
firstRun := fi == nil firstRun := fi == nil
// Timeout to force failure when accessing a data dir that is already in use
timeout := &bolt.Options{Timeout: 5 * time.Second}
// Create or open the boltdb state database // Create or open the boltdb state database
db, err := boltdd.Open(fn, 0600, nil) db, err := boltdd.Open(fn, 0600, timeout)
if err != nil { if err == bolt.ErrTimeout {
return nil, fmt.Errorf("timed out while opening database, is another Nomad process accessing data_dir %s?", stateDir)
} else if err != nil {
return nil, fmt.Errorf("failed to create state database: %v", err) return nil, fmt.Errorf("failed to create state database: %v", err)
} }