From 8073a05415e2127eb12a0996463c1a8a4a0fe03f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 7 Jan 2025 16:05:18 +0100 Subject: [PATCH] Don't validate GLM state tx if there is no previous state --- go/daemon/network/glm/glm.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/go/daemon/network/glm/glm.go b/go/daemon/network/glm/glm.go index 5d246b3..be7b4b1 100644 --- a/go/daemon/network/glm/glm.go +++ b/go/daemon/network/glm/glm.go @@ -81,18 +81,20 @@ type glmState struct { ActiveAllocations []daecommon.ConfigStorageAllocation } -func (glm *garageLayoutManager) get() (glmState, error) { +func (glm *garageLayoutManager) get() (glmState, bool, error) { var ( path = filepath.Join(glm.dir.Path, glmStateFile) state glmState ) err := jsonutil.LoadFile(&state, path) - if err != nil && !errors.Is(err, fs.ErrNotExist) { - return glmState{}, err + if errors.Is(err, fs.ErrNotExist) { + 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 { @@ -103,7 +105,7 @@ func (glm *garageLayoutManager) set(state glmState) error { func (glm *garageLayoutManager) GetActiveAllocations(context.Context) ( []daecommon.ConfigStorageAllocation, error, ) { - state, err := glm.get() + state, _, err := glm.get() return state.ActiveAllocations, err } @@ -117,9 +119,13 @@ func (glm *garageLayoutManager) SetActiveAllocations( func (glm *garageLayoutManager) Validate( _ context.Context, targetAllocs []daecommon.ConfigStorageAllocation, ) error { - state, err := glm.get() + state, ok, err := glm.get() if err != nil { 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( @@ -135,16 +141,20 @@ func (glm *garageLayoutManager) CalculateStateTransition( ) ( StateTransition, error, ) { - state, err := glm.get() + state, ok, err := glm.get() if err != nil { return StateTransition{}, fmt.Errorf("reading state: %w", err) } - err = validateTargetAllocs(state.ActiveAllocations, targetAllocs) - if err != nil { - return StateTransition{}, fmt.Errorf( - "validating target allocations: %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) + if err != nil { + return StateTransition{}, fmt.Errorf( + "validating target allocations: %w", err, + ) + } } allKnownNodes, knownNodes := knownNodes, knownNodes[:0]