-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export app image and cache image in parallel #1247
Merged
natalieparellano
merged 6 commits into
buildpacks:main
from
kritkasahni-google:parallelize
Dec 14, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2422101
Export app image and cache image in parallel
kritkasahni-google 23d828e
Update platform/resolve_inputs.go
kritkasahni-google 15b91a1
Fix warning message for parallel export without cache image.
kritkasahni-google c81dca6
Add test for parallel export enabled.
kritkasahni-google 560df95
Fix test for parallel export without cache image.
kritkasahni-google 687b27a
Fix test message for parallel export without cache image.
kritkasahni-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package layers | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
|
||
|
@@ -20,14 +23,15 @@ const ( | |
ProcessTypesLayerName = "Buildpacks Process Types" | ||
SBOMLayerName = "Software Bill-of-Materials" | ||
SliceLayerName = "Application Slice: %d" | ||
processing = "processing" | ||
) | ||
|
||
type Factory struct { | ||
ArtifactsDir string // ArtifactsDir is the directory where layer files are written | ||
UID, GID int // UID and GID are used to normalize layer entries | ||
Logger log.Logger | ||
|
||
tarHashes map[string]string // tarHases Stores hashes of layer tarballs for reuse between the export and cache steps. | ||
Ctx context.Context | ||
tarHashes sync.Map // tarHases Stores hashes of layer tarballs for reuse between the export and cache steps. | ||
} | ||
|
||
type Layer struct { | ||
|
@@ -38,44 +42,66 @@ type Layer struct { | |
} | ||
|
||
func (f *Factory) writeLayer(id, createdBy string, addEntries func(tw *archive.NormalizingTarWriter) error) (layer Layer, err error) { | ||
if f.Ctx == nil { | ||
f.Ctx = context.TODO() | ||
} | ||
tarPath := filepath.Join(f.ArtifactsDir, escape(id)+".tar") | ||
if f.tarHashes == nil { | ||
f.tarHashes = make(map[string]string) | ||
for { | ||
sha, loaded := f.tarHashes.LoadOrStore(tarPath, processing) | ||
if loaded { | ||
select { | ||
case <-f.Ctx.Done(): | ||
return Layer{}, f.Ctx.Err() | ||
default: | ||
shaString := sha.(string) | ||
if shaString == processing { | ||
// another goroutine is processing this layer, wait and try again | ||
time.Sleep(500 * time.Millisecond) | ||
continue | ||
} | ||
|
||
f.Logger.Debugf("Reusing tarball for layer %q with SHA: %s\n", id, shaString) | ||
return Layer{ | ||
ID: id, | ||
TarPath: tarPath, | ||
Digest: shaString, | ||
History: v1.History{CreatedBy: createdBy}, | ||
}, nil | ||
} | ||
} | ||
break | ||
} | ||
if sha, ok := f.tarHashes[tarPath]; ok { | ||
f.Logger.Debugf("Reusing tarball for layer %q with SHA: %s\n", id, sha) | ||
|
||
select { | ||
case <-f.Ctx.Done(): | ||
return Layer{}, f.Ctx.Err() | ||
default: | ||
lw, err := newFileLayerWriter(tarPath) | ||
if err != nil { | ||
return Layer{}, err | ||
} | ||
defer func() { | ||
if closeErr := lw.Close(); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't |
||
err = closeErr | ||
} | ||
}() | ||
tw := tarWriter(lw) | ||
if err := addEntries(tw); err != nil { | ||
return Layer{}, err | ||
} | ||
|
||
if err := tw.Close(); err != nil { | ||
return Layer{}, err | ||
} | ||
digest := lw.Digest() | ||
f.tarHashes.Store(tarPath, digest) | ||
return Layer{ | ||
ID: id, | ||
Digest: digest, | ||
TarPath: tarPath, | ||
Digest: sha, | ||
History: v1.History{CreatedBy: createdBy}, | ||
}, nil | ||
} | ||
lw, err := newFileLayerWriter(tarPath) | ||
if err != nil { | ||
return Layer{}, err | ||
} | ||
defer func() { | ||
if closeErr := lw.Close(); err == nil { | ||
err = closeErr | ||
} | ||
}() | ||
tw := tarWriter(lw) | ||
if err := addEntries(tw); err != nil { | ||
return Layer{}, err | ||
} | ||
|
||
if err := tw.Close(); err != nil { | ||
return Layer{}, err | ||
}, err | ||
} | ||
digest := lw.Digest() | ||
f.tarHashes[tarPath] = digest | ||
return Layer{ | ||
ID: id, | ||
Digest: digest, | ||
TarPath: tarPath, | ||
History: v1.History{CreatedBy: createdBy}, | ||
}, err | ||
} | ||
|
||
func escape(id string) string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we enable parallel export in this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking a look on how to enable that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be passing "PASS: TestExporter/acceptance-exporter/0.10/registry_case/first_build/cache/cache_image_case/is_created_with_parallel_export_enabled" -> is this this test enough @natalieparellano ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think latest test-windows is failing due to an unrelated error. It should ideally succeed on retry - I don't have option to retry workflow @natalieparellano . I have added test and fixed warning message for -parallel export option. I will wait for end user testing results to see if we need to make more changes.