Merge pull request #28128 from hashicorp/jbardin/etcdv3

StateMgr must be able to return with locked state
This commit is contained in:
James Bardin 2021-07-20 14:27:58 -04:00 committed by GitHub
commit 017633f000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 9 deletions

View File

@ -58,16 +58,8 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
lockInfo := statemgr.NewLockInfo() lockInfo := statemgr.NewLockInfo()
lockInfo.Operation = "init" lockInfo.Operation = "init"
lockId, err := stateMgr.Lock(lockInfo)
if err != nil {
return nil, fmt.Errorf("Failed to lock state in etcd: %s.", err)
}
lockUnlock := func(parent error) error { lockUnlock := func(parent error) error {
if err := stateMgr.Unlock(lockId); err != nil { return nil
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
}
return parent
} }
if err := stateMgr.RefreshState(); err != nil { if err := stateMgr.RefreshState(); err != nil {
@ -76,6 +68,18 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
} }
if v := stateMgr.State(); v == nil { if v := stateMgr.State(); v == nil {
lockId, err := stateMgr.Lock(lockInfo)
if err != nil {
return nil, fmt.Errorf("Failed to lock state in etcd: %s.", err)
}
lockUnlock = func(parent error) error {
if err := stateMgr.Unlock(lockId); err != nil {
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
}
return parent
}
if err := stateMgr.WriteState(states.NewState()); err != nil { if err := stateMgr.WriteState(states.NewState()); err != nil {
err = lockUnlock(err) err = lockUnlock(err)
return nil, err return nil, err