Fix spelling & re-add immutable state struct

This commit is contained in:
Michael Schurter 2017-06-23 13:01:39 -07:00
parent d359d3b554
commit cff8546035

View file

@ -30,7 +30,8 @@ const (
var (
// The following are the key paths written to the state database
allocRunnreStateAllocKey = []byte("alloc")
allocRunnerStateAllocKey = []byte("alloc")
allocRunnerStateImmutableKey = []byte("immutable")
allocRunnerStateMutableKey = []byte("mutable")
allocRunnerStateAllocDirKey = []byte("alloc-dir")
)
@ -87,9 +88,10 @@ type AllocRunner struct {
persistedEvalLock sync.Mutex
persistedEval string
// allocDirPersisted is used to track whether the alloc dir has been
// persisted. Once persisted we can lower write volume by not
// re-writing it
// immutablePersisted and allocDirPersisted are used to track whether the
// immutable data and the alloc dir have been persisted. Once persisted we
// can lower write volume by not re-writing these values
immutablePersisted bool
allocDirPersisted bool
}
@ -119,10 +121,14 @@ type allocRunnerState struct {
// allocRunnerAllocState is state that only has to be written when the alloc
// changes.
type allocRunnerAllocState struct {
Version string
Alloc *structs.Allocation
}
// allocRunnerImmutableState is state that only has to be written once.
type allocRunnerImmutableState struct {
Version string
}
// allocRunnerMutableState is state that has to be written on each save as it
// changes over the life-cycle of the alloc_runner.
type allocRunnerMutableState struct {
@ -212,10 +218,14 @@ func (r *AllocRunner) RestoreState() error {
// Get the state objects
var mutable allocRunnerMutableState
var immutable allocRunnerImmutableState
var allocState allocRunnerAllocState
var allocDir allocdir.AllocDir
if err := getObject(bkt, allocRunnreStateAllocKey, &allocState); err != nil {
if err := getObject(bkt, allocRunnerStateAllocKey, &allocState); err != nil {
return fmt.Errorf("failed to read alloc runner alloc state: %v", err)
}
if err := getObject(bkt, allocRunnerStateImmutableKey, &immutable); err != nil {
return fmt.Errorf("failed to read alloc runner immutable state: %v", err)
}
if err := getObject(bkt, allocRunnerStateMutableKey, &mutable); err != nil {
@ -354,11 +364,10 @@ func (r *AllocRunner) saveAllocRunnerState() error {
if alloc.EvalID != lastPersisted {
allocState := &allocRunnerAllocState{
Alloc: alloc,
Version: r.config.Version,
}
if err := putObject(allocBkt, allocRunnreStateAllocKey, &allocState); err != nil {
return fmt.Errorf("failed to write alloc_runner immutable state: %v", err)
if err := putObject(allocBkt, allocRunnerStateAllocKey, &allocState); err != nil {
return fmt.Errorf("failed to write alloc_runner alloc state: %v", err)
}
tx.OnCommit(func() {
@ -368,6 +377,21 @@ func (r *AllocRunner) saveAllocRunnerState() error {
})
}
// Write immutable data iff it hasn't been written yet
if !r.immutablePersisted {
immutable := &allocRunnerImmutableState{
Version: r.config.Version,
}
if err := putObject(allocBkt, allocRunnerStateImmutableKey, &immutable); err != nil {
return fmt.Errorf("failed to write alloc_runner immutable state: %v", err)
}
tx.OnCommit(func() {
r.immutablePersisted = true
})
}
// Write the alloc dir data if it hasn't been written before and it exists.
if !r.allocDirPersisted && r.allocDir != nil {
if err := putObject(allocBkt, allocRunnerStateAllocDirKey, allocDir); err != nil {