mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-29 10:21:01 -06:00
4d53eaa6df
Previously we relied on a constellation of coincidences for everything to work out correctly with state serials. In particular, callers needed to be very careful about mutating states (or not) because many different bits of code shared pointers to the same objects. Here we move to a model where all of the state managers always use distinct instances of state, copied when WriteState is called. This means that they are truly a snapshot of the state as it was at that call, even if the caller goes on mutating the state that was passed. We also adjust the handling of serials so that the state managers ignore any serials in incoming states and instead just treat each Persist as the next version after what was most recently Refreshed. (An exception exists for when nothing has been refreshed, e.g. because we are writing a state to a location for the first time. In that case we _do_ trust the caller, since the given state is either a new state or it's a copy of something we're migrating from elsewhere with its state and lineage intact.) The intent here is to allow the rest of Terraform to not worry about serials and state identity, and instead just treat the state as a mutable structure. We'll just snapshot it occasionally, when WriteState is called, and deal with serials _only_ at persist time. This is intended as a more robust version of #15423, which was a quick hotfix to an issue that resulted from our previous slopping handling of state serials but arguably makes the problem worse by depending on an additional coincidental behavior of the local backend's apply implementation.
109 lines
1.7 KiB
Go
109 lines
1.7 KiB
Go
package state
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
// InmemState is an in-memory state storage.
|
|
type InmemState struct {
|
|
mu sync.Mutex
|
|
state *terraform.State
|
|
}
|
|
|
|
func (s *InmemState) State() *terraform.State {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return s.state.DeepCopy()
|
|
}
|
|
|
|
func (s *InmemState) RefreshState() error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *InmemState) WriteState(state *terraform.State) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
state = state.DeepCopy()
|
|
|
|
if s.state != nil {
|
|
state.Serial = s.state.Serial
|
|
|
|
if !s.state.MarshalEqual(state) {
|
|
state.Serial++
|
|
}
|
|
}
|
|
|
|
s.state = state
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *InmemState) PersistState() error {
|
|
return nil
|
|
}
|
|
|
|
func (s *InmemState) Lock(*LockInfo) (string, error) {
|
|
return "", nil
|
|
}
|
|
|
|
func (s *InmemState) Unlock(string) error {
|
|
return nil
|
|
}
|
|
|
|
// inmemLocker is an in-memory State implementation for testing locks.
|
|
type inmemLocker struct {
|
|
*InmemState
|
|
|
|
mu sync.Mutex
|
|
lockInfo *LockInfo
|
|
// count the calls to Lock
|
|
lockCounter int
|
|
}
|
|
|
|
func (s *inmemLocker) Lock(info *LockInfo) (string, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.lockCounter++
|
|
|
|
lockErr := &LockError{
|
|
Info: &LockInfo{},
|
|
}
|
|
|
|
if s.lockInfo != nil {
|
|
lockErr.Err = errors.New("state locked")
|
|
*lockErr.Info = *s.lockInfo
|
|
return "", lockErr
|
|
}
|
|
|
|
info.Created = time.Now().UTC()
|
|
s.lockInfo = info
|
|
return s.lockInfo.ID, nil
|
|
}
|
|
|
|
func (s *inmemLocker) Unlock(id string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
lockErr := &LockError{
|
|
Info: &LockInfo{},
|
|
}
|
|
|
|
if id != s.lockInfo.ID {
|
|
lockErr.Err = errors.New("invalid lock id")
|
|
*lockErr.Info = *s.lockInfo
|
|
return lockErr
|
|
}
|
|
|
|
s.lockInfo = nil
|
|
return nil
|
|
}
|