Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cachewd cachew.hcl **/*.go !**/*_test.go !state/**/* debounce=2s ready=http:8080/_readiness=200: CACHEW_URL=http://localhost:8080 CACHEW_LOG_LEVEL=debug cachewd
cachewd cachew.hcl **/*.go !**/*_test.go !state/**/* debounce=2s ready=http:8080/_readiness=200 timeout=600s: CACHEW_URL=http://localhost:8080 CACHEW_LOG_LEVEL=debug cachewd
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/proctor
11 changes: 10 additions & 1 deletion cmd/cachew/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -162,7 +163,15 @@ func (c *GitRestoreCmd) streamFetchAndExtract(ctx context.Context, api *client.C
// WriteAt so it cannot stream into extraction; the temp file is removed on
// return.
func (c *GitRestoreCmd) parallelFetchAndExtract(ctx context.Context, api *client.Client) (string, string, error) {
tmp, err := os.CreateTemp("", "cachew-snapshot-*.tar.zst")
// Stage the temp snapshot on the same filesystem as the restore target so a
// small or separate /tmp can't fail a restore the target directory has room
// for. The parent of c.Directory shares its filesystem and is created by
// extraction anyway.
tmpDir := filepath.Dir(c.Directory)
if err := os.MkdirAll(tmpDir, 0o750); err != nil {
return "", "", errors.Wrap(err, "create snapshot temp dir")
}
tmp, err := os.CreateTemp(tmpDir, ".cachew-snapshot-*.tar.zst")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid requiring writes to the restore parent

When Directory already exists and is writable but its parent is not (for example a CI workspace pre-created/chowned for the job under a root-owned parent), snapshot.Extract can succeed because it only needs to write inside c.Directory, but this new CreateTemp writes into filepath.Dir(c.Directory) and fails the parallel restore before extraction with permission denied. Consider staging in the existing target directory, or falling back when the parent cannot be written, while still keeping the temp file on the target filesystem.

Useful? React with 👍 / 👎.

if err != nil {
return "", "", errors.Wrap(err, "create snapshot temp file")
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/cachew/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,54 @@ func TestGitRestoreSnapshot(t *testing.T) {
assert.Equal(t, "nested content", string(content))
}

func TestGitRestoreSnapshotParallel(t *testing.T) {
srcDir := t.TempDir()
initGitRepo(t, srcDir, map[string]string{
"hello.txt": "hello world",
"subdir/nested.txt": "nested content",
})
snapshotData := createTarZst(t, srcDir)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/snapshot.tar.zst") {
w.Header().Set("Content-Type", "application/zstd")
w.Header().Set("ETag", `"snap-v1"`)
// ServeContent honours Range/If-Range against the ETag, so ParallelGet
// fetches the snapshot in concurrent chunks.
http.ServeContent(w, r, "snapshot.tar.zst", time.Time{}, bytes.NewReader(snapshotData))
return
}
http.NotFound(w, r)
}))
defer srv.Close()

// A nested, not-yet-existing target exercises temp-dir creation on the
// target filesystem.
dstDir := filepath.Join(t.TempDir(), "nested", "restored")
cmd := &GitRestoreCmd{
RepoURL: "https://github.com/test/repo",
Directory: dstDir,
DownloadConcurrency: 4,
DownloadChunkSizeMB: 8,
}
api := client.NewWithHTTPClient(srv.URL, srv.Client())
assert.NoError(t, cmd.Run(context.Background(), api))

content, err := os.ReadFile(filepath.Join(dstDir, "hello.txt"))
assert.NoError(t, err)
assert.Equal(t, "hello world", string(content))
content, err = os.ReadFile(filepath.Join(dstDir, "subdir", "nested.txt"))
assert.NoError(t, err)
assert.Equal(t, "nested content", string(content))

// The temp snapshot is staged on the target filesystem and cleaned up.
entries, err := os.ReadDir(filepath.Dir(dstDir))
assert.NoError(t, err)
for _, e := range entries {
assert.False(t, strings.HasPrefix(e.Name(), ".cachew-snapshot-"), "temp snapshot left behind: %s", e.Name())
}
}

func TestGitRestoreWithBundle(t *testing.T) {
srcDir := t.TempDir()
initGitRepo(t, srcDir, map[string]string{"file.txt": "v1"})
Expand Down