Skip to content

Commit 76d5c86

Browse files
committed
Lock cache directory during download
To solve the races during concurrent downloads and avoid unneeded work and bandwidth, we allow one concurrent download of the same image. When a limactl process try to access the cache, it takes a lock on the file cache directory. If multiple processes try to get the lock in the same time, only one will take the lock, and the other will block. The process that took the lock tries to get the file from the cache. This is the fast path and the common case. This can fail if the file is not in the cache, the digest does not match, or the cached last modified time does not match the last modified returned from the server. If the process cannot get the file from the cache, it downloads the file from the remote server, and update the cached data and metadata files. Finally the process release the lock on the cache directory. Other limactl processes waiting on the lock wake up and take the lock. In the common case they will find the image in the cache and will release the lock quickly. Since we have exactly one concurrent download, updating the metadata files is trivial and we don't need the writeFirst() helper. Fixes: #2902 Fixes: #2732 Signed-off-by: Nir Soffer <[email protected]>
1 parent 8b90a0a commit 76d5c86

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

pkg/downloader/downloader.go

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,25 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
229229
return res, nil
230230
}
231231

232-
res, err := getCached(ctx, localPath, remote, o)
233-
if err != nil {
232+
shad := cacheDirectoryPath(o.cacheDir, remote)
233+
if err := os.MkdirAll(shad, 0o700); err != nil {
234234
return nil, err
235235
}
236-
if res != nil {
237-
return res, nil
238-
}
239-
return fetch(ctx, localPath, remote, o)
236+
237+
var res *Result
238+
err := lockutil.WithDirLock(shad, func() error {
239+
var err error
240+
res, err = getCached(ctx, localPath, remote, o)
241+
if err != nil {
242+
return err
243+
}
244+
if res != nil {
245+
return nil
246+
}
247+
res, err = fetch(ctx, localPath, remote, o)
248+
return err
249+
})
250+
return res, err
240251
}
241252

242253
// getCached tries to copy the file from the cache to local path. Return result,
@@ -298,18 +309,15 @@ func fetch(ctx context.Context, localPath, remote string, o options) (*Result, e
298309
return nil, err
299310
}
300311
ext := path.Ext(remote)
301-
if err := os.MkdirAll(shad, 0o700); err != nil {
302-
return nil, err
303-
}
304312
shadURL := filepath.Join(shad, "url")
305-
if err := writeFirst(shadURL, []byte(remote), 0o644); err != nil {
313+
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
306314
return nil, err
307315
}
308316
if err := downloadHTTP(ctx, shadData, shadTime, shadType, remote, o.description, o.expectedDigest); err != nil {
309317
return nil, err
310318
}
311319
if shadDigest != "" && o.expectedDigest != "" {
312-
if err := writeFirst(shadDigest, []byte(o.expectedDigest.String()), 0o644); err != nil {
320+
if err := os.WriteFile(shadDigest, []byte(o.expectedDigest.String()), 0o644); err != nil {
313321
return nil, err
314322
}
315323
}
@@ -638,13 +646,13 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url
638646
}
639647
if lastModified != "" {
640648
lm := resp.Header.Get("Last-Modified")
641-
if err := writeFirst(lastModified, []byte(lm), 0o644); err != nil {
649+
if err := os.WriteFile(lastModified, []byte(lm), 0o644); err != nil {
642650
return err
643651
}
644652
}
645653
if contentType != "" {
646654
ct := resp.Header.Get("Content-Type")
647-
if err := writeFirst(contentType, []byte(ct), 0o644); err != nil {
655+
if err := os.WriteFile(contentType, []byte(ct), 0o644); err != nil {
648656
return err
649657
}
650658
}
@@ -705,19 +713,7 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url
705713
return err
706714
}
707715

708-
// If localPath was created by a parallel download keep it. Replacing it
709-
// while another process is copying it to the destination may fail the
710-
// clonefile syscall. We use a lock to ensure that only one process updates
711-
// data, and when we return data file exists.
712-
713-
return lockutil.WithDirLock(filepath.Dir(localPath), func() error {
714-
if _, err := os.Stat(localPath); err == nil {
715-
return nil
716-
} else if !errors.Is(err, os.ErrNotExist) {
717-
return err
718-
}
719-
return os.Rename(localPathTmp, localPath)
720-
})
716+
return os.Rename(localPathTmp, localPath)
721717
}
722718

723719
var tempfileCount atomic.Uint64
@@ -732,18 +728,6 @@ func perProcessTempfile(path string) string {
732728
return fmt.Sprintf("%s.tmp.%d.%d", path, os.Getpid(), tempfileCount.Add(1))
733729
}
734730

735-
// writeFirst writes data to path unless path already exists.
736-
func writeFirst(path string, data []byte, perm os.FileMode) error {
737-
return lockutil.WithDirLock(filepath.Dir(path), func() error {
738-
if _, err := os.Stat(path); err == nil {
739-
return nil
740-
} else if !errors.Is(err, os.ErrNotExist) {
741-
return err
742-
}
743-
return os.WriteFile(path, data, perm)
744-
})
745-
}
746-
747731
// CacheEntries returns a map of cache entries.
748732
// The key is the SHA256 of the URL.
749733
// The value is the path to the cache entry.

0 commit comments

Comments
 (0)