Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/advancer/advancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ func (s *Service) createSnapshot(ctx context.Context, app *Application, machine
"input", input.Index,
"path", snapshotPath)

// Guard against partial store by cleaning up before a retry
if _, err := os.Stat(snapshotPath); err == nil {
if err := s.removeSnapshot(snapshotPath, app.Name); err != nil {
return fmt.Errorf("failed to clean stale snapshot directory: %w", err)
}
}

// Ensure the parent directory exists
if err := os.MkdirAll(s.snapshotsDir, 0755); err != nil { //nolint: mnd
return fmt.Errorf("failed to create snapshots directory: %w", err)
Expand Down
22 changes: 22 additions & 0 deletions internal/advancer/advancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
mrand "math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -937,6 +938,27 @@ func (s *AdvancerSuite) TestCreateSnapshot() {
require.True(os.IsNotExist(statErr))
})

s.Run("OverwritesPartialPriorAttempt", func() {

require := s.Require()
env, machine, tmpDir := setupCreateSnapshot()

prevPath := filepath.Join(tmpDir, "testapp_epoch0_input0")
require.Nil(os.MkdirAll(prevPath, 0755))

input := repotest.NewInputBuilder().WithIndex(0).WithEpochIndex(0).Build()
input.EpochApplicationID = env.app.Application.ID

err := env.service.createSnapshot(context.Background(), env.app.Application, machine, input)
require.Nil(err)
require.NotNil(input.SnapshotURI)
require.Equal(prevPath, *input.SnapshotURI)

// This works because the machine mock doesn't create directories
_, statErr := os.Stat(prevPath)
require.True(errors.Is(statErr, fs.ErrNotExist))
})
Comment thread
mpolitzer marked this conversation as resolved.

s.Run("CreateSnapshotError", func() {
require := s.Require()
env, machine, _ := setupCreateSnapshot()
Expand Down
Loading