terraform/state/inmem.go

109 lines
1.7 KiB
Go
Raw Normal View History

2015-02-23 18:53:20 +01:00
package state
import (
2017-04-03 17:00:45 +02:00
"errors"
"sync"
"time"
2015-02-23 18:53:20 +01:00
"github.com/hashicorp/terraform/terraform"
)
// InmemState is an in-memory state storage.
type InmemState struct {
mu sync.Mutex
2015-02-23 18:53:20 +01:00
state *terraform.State
}
func (s *InmemState) State() *terraform.State {
s.mu.Lock()
defer s.mu.Unlock()
2015-02-24 06:36:35 +01:00
return s.state.DeepCopy()
2015-02-23 18:53:20 +01:00
}
func (s *InmemState) RefreshState() error {
s.mu.Lock()
defer s.mu.Unlock()
2015-02-23 18:53:20 +01:00
return nil
}
func (s *InmemState) WriteState(state *terraform.State) error {
s.mu.Lock()
defer s.mu.Unlock()
state: more robust handling of state Serial 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.
2017-07-05 21:34:30 +02:00
state = state.DeepCopy()
if s.state != nil {
state.Serial = s.state.Serial
if !s.state.MarshalEqual(state) {
state.Serial++
}
}
2015-02-23 18:53:20 +01:00
s.state = state
state: more robust handling of state Serial 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.
2017-07-05 21:34:30 +02:00
2015-02-23 18:53:20 +01:00
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
}
2017-04-03 17:00:45 +02:00
// 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
}