Skip to content
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

gha: add support for batch checking existing keys #4788

Merged
merged 1 commit into from
Mar 26, 2024
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
96 changes: 75 additions & 21 deletions cache/remotecache/gha/gha.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ func init() {
}

const (
attrScope = "scope"
attrTimeout = "timeout"
attrToken = "token"
attrURL = "url"
version = "1"
attrScope = "scope"
attrTimeout = "timeout"
attrToken = "token"
attrURL = "url"
attrRepository = "repository"
attrGHToken = "ghtoken"
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

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

version = "1"

defaultTimeout = 10 * time.Minute
)

type Config struct {
Scope string
URL string
Token string
Timeout time.Duration
Scope string
URL string
Token string // token for the Github Cache runtime API
GHToken string // token for the Github REST API
Repository string
Timeout time.Duration
}

func getConfig(attrs map[string]string) (*Config, error) {
Expand All @@ -62,6 +66,7 @@ func getConfig(attrs map[string]string) (*Config, error) {
if !ok {
return nil, errors.Errorf("token not set for github actions cache")
}

timeout := defaultTimeout
if v, ok := attrs[attrTimeout]; ok {
var err error
Expand All @@ -71,10 +76,12 @@ func getConfig(attrs map[string]string) (*Config, error) {
}
}
return &Config{
Scope: scope,
URL: url,
Token: token,
Timeout: timeout,
Scope: scope,
URL: url,
Token: token,
Timeout: timeout,
GHToken: attrs[attrGHToken],
Repository: attrs[attrRepository],
}, nil
}

Expand All @@ -91,9 +98,11 @@ func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc {

type exporter struct {
solver.CacheExporterTarget
chains *v1.CacheChains
cache *actionscache.Cache
config *Config
chains *v1.CacheChains
cache *actionscache.Cache
config *Config
keyMapOnce sync.Once
keyMap map[string]struct{}
}

func NewExporter(c *Config) (remotecache.Exporter, error) {
Expand All @@ -118,8 +127,12 @@ func (ce *exporter) Config() remotecache.Config {
}
}

func (ce *exporter) blobKeyPrefix() string {
return "buildkit-blob-" + version + "-"
}
Comment on lines +130 to +132
Copy link
Member

Choose a reason for hiding this comment

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

nit: could just be a const


func (ce *exporter) blobKey(dgst digest.Digest) string {
return "buildkit-blob-" + version + "-" + dgst.String()
return ce.blobKeyPrefix() + dgst.String()
}

func (ce *exporter) indexKey() string {
Expand All @@ -133,6 +146,35 @@ func (ce *exporter) indexKey() string {
return "index-" + ce.config.Scope + "-" + version + "-" + scope
}

func (ce *exporter) initActiveKeyMap(ctx context.Context) {
ce.keyMapOnce.Do(func() {
if ce.config.Repository == "" || ce.config.GHToken == "" {
return
}
m, err := ce.initActiveKeyMapOnce(ctx)
if err != nil {
bklog.G(ctx).Errorf("error initializing active key map: %v", err)
return
}
ce.keyMap = m
})
}

func (ce *exporter) initActiveKeyMapOnce(ctx context.Context) (map[string]struct{}, error) {
api, err := actionscache.NewRestAPI(ce.config.Repository, ce.config.GHToken, actionscache.Opt{
Client: tracing.DefaultClient,
Timeout: ce.config.Timeout,
})
if err != nil {
return nil, err
}
keys, err := ce.cache.AllKeys(ctx, api, ce.blobKeyPrefix())
if err != nil {
return nil, err
}
return keys, nil
}

func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
// res := make(map[string]string)
config, descs, err := ce.chains.Marshal(ctx)
Expand All @@ -159,13 +201,25 @@ func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
return nil, errors.Wrapf(err, "failed to parse uncompressed annotation")
}
diffID = dgst
ce.initActiveKeyMap(ctx)

key := ce.blobKey(dgstPair.Descriptor.Digest)
b, err := ce.cache.Load(ctx, key)
if err != nil {
return nil, err

exists := false
if ce.keyMap != nil {
if _, ok := ce.keyMap[key]; ok {
exists = true
}
} else {
b, err := ce.cache.Load(ctx, key)
if err != nil {
return nil, err
}
if b != nil {
exists = true
}
}
if b == nil {
if !exists {
layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob))
ra, err := dgstPair.Provider.ReaderAt(ctx, dgstPair.Descriptor)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require (
github.com/spdx/tools-golang v0.5.3
github.com/stretchr/testify v1.8.4
github.com/tonistiigi/fsutil v0.0.0-20240301111122-7525a1af2bb5
github.com/tonistiigi/go-actions-cache v0.0.0-20240227172821-a0b64f338598
github.com/tonistiigi/go-actions-cache v0.0.0-20240320205438-9794bdbb2fb4
github.com/tonistiigi/go-archvariant v1.0.0
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea
github.com/tonistiigi/vt100 v0.0.0-20230623042737-f9a4f7ef6531
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tonistiigi/fsutil v0.0.0-20240301111122-7525a1af2bb5 h1:oZS8KCqAg62sxJkEq/Ppzqrb6EooqzWtL8Oaex7bc5c=
github.com/tonistiigi/fsutil v0.0.0-20240301111122-7525a1af2bb5/go.mod h1:vbbYqJlnswsbJqWUcJN8fKtBhnEgldDrcagTgnBVKKM=
github.com/tonistiigi/go-actions-cache v0.0.0-20240227172821-a0b64f338598 h1:DA/NDC0YbMdnfcOSUzAnbUZE6dSM54d+0hrBqG+bOfs=
github.com/tonistiigi/go-actions-cache v0.0.0-20240227172821-a0b64f338598/go.mod h1:anhKd3mnC1shAbQj1Q4IJ+w6xqezxnyDYlx/yKa7IXM=
github.com/tonistiigi/go-actions-cache v0.0.0-20240320205438-9794bdbb2fb4 h1:R0lM8Jo3aZL95yjQHWQti7nszllleKBxCs9uyFbykII=
github.com/tonistiigi/go-actions-cache v0.0.0-20240320205438-9794bdbb2fb4/go.mod h1:anhKd3mnC1shAbQj1Q4IJ+w6xqezxnyDYlx/yKa7IXM=
github.com/tonistiigi/go-archvariant v1.0.0 h1:5LC1eDWiBNflnTF1prCiX09yfNHIxDC/aukdhCdTyb0=
github.com/tonistiigi/go-archvariant v1.0.0/go.mod h1:TxFmO5VS6vMq2kvs3ht04iPXtu2rUT/erOnGFYfk5Ho=
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea h1:SXhTLE6pb6eld/v/cCndK0AMpt1wiVFb/YYmqB3/QG0=
Expand Down
49 changes: 39 additions & 10 deletions vendor/github.com/tonistiigi/go-actions-cache/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions vendor/github.com/tonistiigi/go-actions-cache/rest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ github.com/stretchr/testify/require
github.com/tonistiigi/fsutil
github.com/tonistiigi/fsutil/copy
github.com/tonistiigi/fsutil/types
# github.com/tonistiigi/go-actions-cache v0.0.0-20240227172821-a0b64f338598
# github.com/tonistiigi/go-actions-cache v0.0.0-20240320205438-9794bdbb2fb4
## explicit; go 1.20
github.com/tonistiigi/go-actions-cache
# github.com/tonistiigi/go-archvariant v1.0.0
Expand Down