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
1 changed files with 9 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import (
"path/filepath"
"time"
"github.com/boltdb/bolt"
hclog "github.com/hashicorp/go-hclog"
trstate "github.com/hashicorp/nomad/client/allocrunner/taskrunner/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
// 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
db, err := boltdd.Open(fn, 0600, nil)
if err != nil {
db, err := boltdd.Open(fn, 0600, timeout)
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)
}