Refresh state outside of grabbing the lock; only grab the lock on provisioning if the state file doesn't exist; this is similar to the GCS backend

This commit is contained in:
Sean Teeling 2020-10-12 21:45:25 -07:00
parent 5677978eb0
commit e1f1b84d67
1 changed files with 10 additions and 15 deletions

View File

@ -95,9 +95,13 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
stateMgr := &remote.State{Client: client} stateMgr := &remote.State{Client: client}
// Grab the value
if err := stateMgr.RefreshState(); err != nil {
return nil, err
}
//if this isn't the default state name, we need to create the object so //if this isn't the default state name, we need to create the object so
//it's listed by States. //it's listed by States.
if name != backend.DefaultStateName { if v := stateMgr.State(); v == nil {
// take a lock on this state while we write it // take a lock on this state while we write it
lockInfo := statemgr.NewLockInfo() lockInfo := statemgr.NewLockInfo()
lockInfo.Operation = "init" lockInfo.Operation = "init"
@ -114,29 +118,20 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
return parent return parent
} }
// Grab the value // If we have no state, we have to create an empty state
if err := stateMgr.RefreshState(); err != nil { if err := stateMgr.WriteState(states.NewState()); err != nil {
err = lockUnlock(err) err = lockUnlock(err)
return nil, err return nil, err
} }
if err := stateMgr.PersistState(); err != nil {
// If we have no state, we have to create an empty state err = lockUnlock(err)
if v := stateMgr.State(); v == nil { return nil, err
if err := stateMgr.WriteState(states.NewState()); err != nil {
err = lockUnlock(err)
return nil, err
}
if err := stateMgr.PersistState(); err != nil {
err = lockUnlock(err)
return nil, err
}
} }
// Unlock, the state should now be initialized // Unlock, the state should now be initialized
if err := lockUnlock(nil); err != nil { if err := lockUnlock(nil); err != nil {
return nil, err return nil, err
} }
} }
return stateMgr, nil return stateMgr, nil