Don't validate GLM state tx if there is no previous state

This commit is contained in:
Brian Picciano 2025-01-07 16:05:18 +01:00
parent 92802da2dd
commit 8073a05415

View File

@ -81,18 +81,20 @@ type glmState struct {
ActiveAllocations []daecommon.ConfigStorageAllocation ActiveAllocations []daecommon.ConfigStorageAllocation
} }
func (glm *garageLayoutManager) get() (glmState, error) { func (glm *garageLayoutManager) get() (glmState, bool, error) {
var ( var (
path = filepath.Join(glm.dir.Path, glmStateFile) path = filepath.Join(glm.dir.Path, glmStateFile)
state glmState state glmState
) )
err := jsonutil.LoadFile(&state, path) err := jsonutil.LoadFile(&state, path)
if err != nil && !errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
return glmState{}, err return glmState{}, false, nil
} else if err != nil && !errors.Is(err, fs.ErrNotExist) {
return glmState{}, false, err
} }
return state, nil return state, true, nil
} }
func (glm *garageLayoutManager) set(state glmState) error { func (glm *garageLayoutManager) set(state glmState) error {
@ -103,7 +105,7 @@ func (glm *garageLayoutManager) set(state glmState) error {
func (glm *garageLayoutManager) GetActiveAllocations(context.Context) ( func (glm *garageLayoutManager) GetActiveAllocations(context.Context) (
[]daecommon.ConfigStorageAllocation, error, []daecommon.ConfigStorageAllocation, error,
) { ) {
state, err := glm.get() state, _, err := glm.get()
return state.ActiveAllocations, err return state.ActiveAllocations, err
} }
@ -117,9 +119,13 @@ func (glm *garageLayoutManager) SetActiveAllocations(
func (glm *garageLayoutManager) Validate( func (glm *garageLayoutManager) Validate(
_ context.Context, targetAllocs []daecommon.ConfigStorageAllocation, _ context.Context, targetAllocs []daecommon.ConfigStorageAllocation,
) error { ) error {
state, err := glm.get() state, ok, err := glm.get()
if err != nil { if err != nil {
return fmt.Errorf("reading state: %w", err) return fmt.Errorf("reading state: %w", err)
} else if !ok {
// If there is no previously state then we assume the new state can't
// conflict with it.
return nil
} }
return validateTargetAllocs( return validateTargetAllocs(
@ -135,17 +141,21 @@ func (glm *garageLayoutManager) CalculateStateTransition(
) ( ) (
StateTransition, error, StateTransition, error,
) { ) {
state, err := glm.get() state, ok, err := glm.get()
if err != nil { if err != nil {
return StateTransition{}, fmt.Errorf("reading state: %w", err) return StateTransition{}, fmt.Errorf("reading state: %w", err)
} }
if ok {
// If there is no previously state then we assume the new state can't
// conflict with it.
err = validateTargetAllocs(state.ActiveAllocations, targetAllocs) err = validateTargetAllocs(state.ActiveAllocations, targetAllocs)
if err != nil { if err != nil {
return StateTransition{}, fmt.Errorf( return StateTransition{}, fmt.Errorf(
"validating target allocations: %w", err, "validating target allocations: %w", err,
) )
} }
}
allKnownNodes, knownNodes := knownNodes, knownNodes[:0] allKnownNodes, knownNodes := knownNodes, knownNodes[:0]
for _, node := range allKnownNodes { for _, node := range allKnownNodes {