Skip to content

Commit

Permalink
Merge pull request #28128 from hashicorp/jbardin/etcdv3
Browse files Browse the repository at this point in the history
StateMgr must be able to return with locked state
  • Loading branch information
jbardin authored Jul 20, 2021
2 parents 6d65c19 + 9c20ed6 commit 017633f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions internal/backend/remote-state/etcdv3/backend_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,8 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {

lockInfo := statemgr.NewLockInfo()
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 {
if err := stateMgr.Unlock(lockId); err != nil {
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
}
return parent
return nil
}

if err := stateMgr.RefreshState(); err != nil {
Expand All @@ -76,6 +68,18 @@ func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
}

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 {
err = lockUnlock(err)
return nil, err
Expand Down

0 comments on commit 017633f

Please sign in to comment.