mirror of
https://github.com/docker/compose.git
synced 2026-11-17 13:45:39 +00:00
fix(oci): put back only a complete entry when the replacement of a cache entry fails
A run whose replacement of a directory that is not an entry failed put that directory back unconditionally. Another run having set aside the entry that took its place in the meantime, the directory put back stood in the way of the final rename of that run, whose deferred removal then deleted the complete entry it had set aside: the cache ended with the incomplete directory after a run returned success. Only a complete entry is worth restoring, so the restore is guarded. A run that loses to another may still return an error: the concurrency test now pins the path of the entry for the runs that succeed, and the state of the cache once all of them are over. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
ff9622c5c5
commit
2fcf79b3c1
2 changed files with 36 additions and 10 deletions
|
|
@ -232,7 +232,9 @@ func cacheEntryIsComplete(local string) bool {
|
|||
// it out of the cache in one step, and only then removed: a run that replaces
|
||||
// it at the same time does not remove the entry that another one has put in
|
||||
// place in the meantime, and the cache is left with a complete entry whichever
|
||||
// of them is last.
|
||||
// of them is last. A run that loses to another may still return an error: what
|
||||
// it gets is the state of the cache when it is over, not the result of its own
|
||||
// move.
|
||||
func moveCacheEntry(tmp, local string) error {
|
||||
err := os.Rename(tmp, local)
|
||||
if err == nil || cacheEntryIsComplete(local) {
|
||||
|
|
@ -252,9 +254,13 @@ func moveCacheEntry(tmp, local string) error {
|
|||
if cacheEntryIsComplete(local) {
|
||||
return nil
|
||||
}
|
||||
// nothing took the place of what was set aside: put it back rather than
|
||||
// leave the cache without it
|
||||
_ = os.Rename(stale, local)
|
||||
// nothing took the place of what was set aside: put it back, if it is a
|
||||
// complete entry, rather than leave the cache without it. One that is
|
||||
// not stays aside: another run may have set aside the entry that took
|
||||
// its place, and would then find this one in its way.
|
||||
if cacheEntryIsComplete(stale) {
|
||||
_ = os.Rename(stale, local)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -183,10 +183,11 @@ func TestOCILoaderFailedPullLeavesNothingBehind(t *testing.T) {
|
|||
}
|
||||
|
||||
// Runs that load the same artifact at the same time, whether the cache has the
|
||||
// entry already, has none or has a directory that is not one, all succeed and
|
||||
// leave a single complete entry, and nothing aside. What a run gets is the path
|
||||
// of the entry, which is read once the runs are over: a run that replaces a
|
||||
// directory which is not an entry takes it out of the way for a moment.
|
||||
// entry already, has none or has a directory that is not one, leave a single
|
||||
// complete entry, and nothing aside. A run that loses to another while it
|
||||
// replaces a directory which is not an entry may return an error, so what is
|
||||
// pinned for every run is the path of the entry when it succeeds, and the state
|
||||
// of the cache once the runs are over.
|
||||
func TestOCILoaderConcurrentRunsLeaveOneCompleteEntry(t *testing.T) {
|
||||
registry := newCacheTestRegistry(t)
|
||||
host := registry.serve(t)
|
||||
|
|
@ -218,8 +219,9 @@ func TestOCILoaderConcurrentRunsLeaveOneCompleteEntry(t *testing.T) {
|
|||
wg.Wait()
|
||||
|
||||
for i := range runs {
|
||||
assert.NilError(t, errs[i])
|
||||
assert.Equal(t, files[i], filepath.Join(entry, "compose.yaml"))
|
||||
if errs[i] == nil {
|
||||
assert.Equal(t, files[i], filepath.Join(entry, "compose.yaml"))
|
||||
}
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(entry, "compose.yaml"))
|
||||
assert.NilError(t, err)
|
||||
|
|
@ -233,3 +235,21 @@ func TestOCILoaderConcurrentRunsLeaveOneCompleteEntry(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A directory which is not an entry that is replaced, and whose replacement
|
||||
// then fails, is not put back: only a complete entry is worth restoring. Put
|
||||
// back, it would be in the way of the run that set aside the entry which took
|
||||
// its place (see moveCacheEntry), and would leave the cache with it instead.
|
||||
func TestMoveCacheEntryDoesNotRestoreADirectoryWhichIsNotAnEntry(t *testing.T) {
|
||||
cache := t.TempDir()
|
||||
local := filepath.Join(cache, "entry")
|
||||
assert.NilError(t, os.MkdirAll(local, 0o700))
|
||||
assert.NilError(t, os.WriteFile(filepath.Join(local, "compose.yaml"), []byte("services:\n app:\n ima"), 0o600))
|
||||
|
||||
err := moveCacheEntry(filepath.Join(cache, "missing"), local)
|
||||
assert.Assert(t, err != nil)
|
||||
|
||||
entries, err := os.ReadDir(cache)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(entries), 0, "neither the directory nor anything aside is left")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue