2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-01-18 22:47:56 -06:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2023-02-13 19:38:24 -06:00
|
|
|
"log"
|
2017-01-18 22:47:56 -06:00
|
|
|
"sync"
|
2023-02-13 19:38:24 -06:00
|
|
|
"time"
|
2017-01-18 22:47:56 -06:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
|
|
|
"github.com/opentofu/opentofu/internal/states/statemgr"
|
2023-09-20 07:16:53 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/tofu"
|
2017-01-18 22:47:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// StateHook is a hook that continuously updates the state by calling
|
2020-08-11 10:43:01 -05:00
|
|
|
// WriteState on a statemgr.Full.
|
2017-01-18 22:47:56 -06:00
|
|
|
type StateHook struct {
|
2023-09-20 07:16:53 -05:00
|
|
|
tofu.NilHook
|
2017-01-18 22:47:56 -06:00
|
|
|
sync.Mutex
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
StateMgr statemgr.Writer
|
2023-02-13 19:38:24 -06:00
|
|
|
|
|
|
|
// If PersistInterval is nonzero then for any new state update after
|
|
|
|
// the duration has elapsed we'll try to persist a state snapshot
|
|
|
|
// to the persistent backend too.
|
|
|
|
// That's only possible if field Schemas is valid, because the
|
|
|
|
// StateMgr.PersistState function for some backends needs schemas.
|
|
|
|
PersistInterval time.Duration
|
|
|
|
|
|
|
|
// Schemas are the schemas to use when persisting state due to
|
|
|
|
// PersistInterval. This is ignored if PersistInterval is zero,
|
|
|
|
// and PersistInterval is ignored if this is nil.
|
2023-09-20 07:16:53 -05:00
|
|
|
Schemas *tofu.Schemas
|
2023-02-13 19:38:24 -06:00
|
|
|
|
2023-04-04 19:04:56 -05:00
|
|
|
intermediatePersist IntermediateStatePersistInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntermediateStatePersistInfo struct {
|
|
|
|
// RequestedPersistInterval is the persist interval requested by whatever
|
|
|
|
// instantiated the StateHook.
|
|
|
|
//
|
|
|
|
// Implementations of [IntermediateStateConditionalPersister] should ideally
|
|
|
|
// respect this, but may ignore it if they use something other than the
|
|
|
|
// passage of time to make their decision.
|
|
|
|
RequestedPersistInterval time.Duration
|
|
|
|
|
|
|
|
// LastPersist is the time when the last intermediate state snapshot was
|
2023-09-21 07:38:46 -05:00
|
|
|
// persisted, or the time of the first report for OpenTofu Core if there
|
2023-04-04 19:04:56 -05:00
|
|
|
// hasn't yet been a persisted snapshot.
|
|
|
|
LastPersist time.Time
|
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
// ForcePersist is true when OpenTofu CLI has received an interrupt
|
2023-04-04 19:04:56 -05:00
|
|
|
// signal and is therefore trying to create snapshots more aggressively
|
|
|
|
// in anticipation of possibly being terminated ungracefully.
|
|
|
|
// [IntermediateStateConditionalPersister] implementations should ideally
|
|
|
|
// persist every snapshot they get when this flag is set, unless they have
|
|
|
|
// some external information that implies this shouldn't be necessary.
|
|
|
|
ForcePersist bool
|
2017-01-18 22:47:56 -06:00
|
|
|
}
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
var _ tofu.Hook = (*StateHook)(nil)
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
func (h *StateHook) PostStateUpdate(new *states.State) (tofu.HookAction, error) {
|
2017-01-18 22:47:56 -06:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
2023-04-04 19:04:56 -05:00
|
|
|
h.intermediatePersist.RequestedPersistInterval = h.PersistInterval
|
|
|
|
|
|
|
|
if h.intermediatePersist.LastPersist.IsZero() {
|
2023-02-13 19:38:24 -06:00
|
|
|
// The first PostStateUpdate starts the clock for intermediate
|
|
|
|
// calls to PersistState.
|
2023-04-04 19:04:56 -05:00
|
|
|
h.intermediatePersist.LastPersist = time.Now()
|
2023-02-13 19:38:24 -06:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
if h.StateMgr != nil {
|
|
|
|
if err := h.StateMgr.WriteState(new); err != nil {
|
2023-09-20 07:16:53 -05:00
|
|
|
return tofu.HookActionHalt, err
|
2017-01-18 22:47:56 -06:00
|
|
|
}
|
2023-02-13 19:38:24 -06:00
|
|
|
if mgrPersist, ok := h.StateMgr.(statemgr.Persister); ok && h.PersistInterval != 0 && h.Schemas != nil {
|
2023-04-04 19:04:56 -05:00
|
|
|
if h.shouldPersist() {
|
2023-11-08 15:09:14 -06:00
|
|
|
err := mgrPersist.PersistState(h.Schemas)
|
2023-02-13 19:38:24 -06:00
|
|
|
if err != nil {
|
2023-09-20 07:16:53 -05:00
|
|
|
return tofu.HookActionHalt, err
|
2023-02-13 19:38:24 -06:00
|
|
|
}
|
2023-04-04 19:04:56 -05:00
|
|
|
h.intermediatePersist.LastPersist = time.Now()
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] State storage %T declined to persist a state snapshot", h.StateMgr)
|
2023-02-13 19:38:24 -06:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 22:47:56 -06:00
|
|
|
}
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
return tofu.HookActionContinue, nil
|
2017-01-18 22:47:56 -06:00
|
|
|
}
|
2023-02-13 19:38:24 -06:00
|
|
|
|
|
|
|
func (h *StateHook) Stopping() {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
// If OpenTofu has been asked to stop then that might mean that a hard
|
|
|
|
// kill signal will follow shortly in case OpenTofu doesn't stop
|
2023-02-13 19:38:24 -06:00
|
|
|
// quickly enough, and so we'll try to persist the latest state
|
|
|
|
// snapshot in the hope that it'll give the user less recovery work to
|
2023-09-21 07:38:46 -05:00
|
|
|
// do if they _do_ subsequently hard-kill OpenTofu during an apply.
|
2023-02-13 19:38:24 -06:00
|
|
|
|
|
|
|
if mgrPersist, ok := h.StateMgr.(statemgr.Persister); ok && h.Schemas != nil {
|
|
|
|
// While we're in the stopping phase we'll try to persist every
|
|
|
|
// new state update to maximize every opportunity we get to avoid
|
|
|
|
// losing track of objects that have been created or updated.
|
2023-09-21 07:38:46 -05:00
|
|
|
// OpenTofu Core won't start any new operations after it's been
|
2023-02-13 19:38:24 -06:00
|
|
|
// stopped, so at most we should see one more PostStateUpdate
|
|
|
|
// call per already-active request.
|
2023-04-04 19:04:56 -05:00
|
|
|
h.intermediatePersist.ForcePersist = true
|
|
|
|
|
|
|
|
if h.shouldPersist() {
|
2023-11-08 15:09:14 -06:00
|
|
|
err := mgrPersist.PersistState(h.Schemas)
|
2023-04-04 19:04:56 -05:00
|
|
|
if err != nil {
|
2023-09-21 07:38:46 -05:00
|
|
|
// This hook can't affect OpenTofu Core's ongoing behavior,
|
2023-08-22 04:49:16 -05:00
|
|
|
// but it's a best effort thing anyway, so we'll just emit a
|
2023-04-04 19:04:56 -05:00
|
|
|
// log to aid with debugging.
|
|
|
|
log.Printf("[ERROR] Failed to persist state after interruption: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] State storage %T declined to persist a state snapshot", h.StateMgr)
|
|
|
|
}
|
2023-02-13 19:38:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-04-04 19:04:56 -05:00
|
|
|
|
|
|
|
func (h *StateHook) shouldPersist() bool {
|
|
|
|
if m, ok := h.StateMgr.(IntermediateStateConditionalPersister); ok {
|
|
|
|
return m.ShouldPersistIntermediateState(&h.intermediatePersist)
|
|
|
|
}
|
|
|
|
return DefaultIntermediateStatePersistRule(&h.intermediatePersist)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultIntermediateStatePersistRule is the default implementation of
|
|
|
|
// [IntermediateStateConditionalPersister.ShouldPersistIntermediateState] used
|
|
|
|
// when the selected state manager doesn't implement that interface.
|
|
|
|
//
|
|
|
|
// Implementers of that interface can optionally wrap a call to this function
|
|
|
|
// if they want to combine the default behavior with some logic of their own.
|
|
|
|
func DefaultIntermediateStatePersistRule(info *IntermediateStatePersistInfo) bool {
|
|
|
|
return info.ForcePersist || time.Since(info.LastPersist) >= info.RequestedPersistInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntermediateStateConditionalPersister is an optional extension of
|
|
|
|
// [statemgr.Persister] that allows an implementation to tailor the rules for
|
2023-09-21 07:38:46 -05:00
|
|
|
// whether to create intermediate state snapshots when OpenTofu Core emits
|
2023-04-04 19:04:56 -05:00
|
|
|
// events reporting that the state might have changed.
|
|
|
|
//
|
|
|
|
// For state managers that don't implement this interface, [StateHook] uses
|
|
|
|
// a default set of rules that aim to be a good compromise between how long
|
|
|
|
// a state change can be active before it gets committed as a snapshot vs.
|
|
|
|
// how many intermediate snapshots will get created. That compromise is subject
|
|
|
|
// to change over time, but a state manager can implement this interface to
|
|
|
|
// exert full control over those rules.
|
|
|
|
type IntermediateStateConditionalPersister interface {
|
2023-09-21 07:38:46 -05:00
|
|
|
// ShouldPersistIntermediateState will be called each time OpenTofu Core
|
2023-04-04 19:04:56 -05:00
|
|
|
// emits an intermediate state event that is potentially eligible to be
|
|
|
|
// persisted.
|
|
|
|
//
|
2024-08-29 12:20:33 -05:00
|
|
|
// The implementation should return true to signal that the state snapshot
|
2023-04-04 19:04:56 -05:00
|
|
|
// most recently provided to the object's WriteState should be persisted,
|
|
|
|
// or false if it should not be persisted. If this function returns true
|
|
|
|
// then the receiver will see a subsequent call to
|
|
|
|
// [statemgr.Persister.PersistState] to request persistence.
|
|
|
|
//
|
|
|
|
// The implementation must not modify anything reachable through the
|
|
|
|
// arguments, and must not retain pointers to anything reachable through
|
|
|
|
// them after the function returns. However, implementers can assume that
|
|
|
|
// nothing will write to anything reachable through the arguments while
|
|
|
|
// this function is active.
|
|
|
|
ShouldPersistIntermediateState(info *IntermediateStatePersistInfo) bool
|
|
|
|
}
|