Skip to content

Commit 3adca08

Browse files
Replace tag-to-digest cache fanciness in oci.go with lru.LRU (#11591)
1 parent ea941cb commit 3adca08

File tree

2 files changed

+16
-40
lines changed

2 files changed

+16
-40
lines changed

enterprise/server/util/oci/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ go_library(
3131
"@com_github_google_go_containerregistry//pkg/v1/remote",
3232
"@com_github_google_go_containerregistry//pkg/v1/remote/transport",
3333
"@com_github_google_go_containerregistry//pkg/v1/types",
34-
"@com_github_jonboulle_clockwork//:clockwork",
3534
"@org_golang_google_genproto_googleapis_bytestream//:bytestream",
3635
],
3736
)

enterprise/server/util/oci/oci.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515
"github.com/buildbuddy-io/buildbuddy/enterprise/server/oci/ocifetcher"
1616
"github.com/buildbuddy-io/buildbuddy/enterprise/server/util/ocicache"
1717
"github.com/buildbuddy-io/buildbuddy/enterprise/server/util/ocimanifest"
18+
ofpb "github.com/buildbuddy-io/buildbuddy/proto/oci_fetcher"
19+
rgpb "github.com/buildbuddy-io/buildbuddy/proto/registry"
20+
repb "github.com/buildbuddy-io/buildbuddy/proto/remote_execution"
1821
"github.com/buildbuddy-io/buildbuddy/server/environment"
1922
"github.com/buildbuddy-io/buildbuddy/server/http/httpclient"
2023
"github.com/buildbuddy-io/buildbuddy/server/util/authutil"
@@ -27,17 +30,12 @@ import (
2730
"github.com/buildbuddy-io/buildbuddy/server/util/tracing"
2831
"github.com/distribution/reference"
2932
"github.com/google/go-containerregistry/pkg/authn"
33+
gcrname "github.com/google/go-containerregistry/pkg/name"
34+
gcr "github.com/google/go-containerregistry/pkg/v1"
3035
"github.com/google/go-containerregistry/pkg/v1/partial"
3136
"github.com/google/go-containerregistry/pkg/v1/remote"
3237
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
3338
"github.com/google/go-containerregistry/pkg/v1/types"
34-
"github.com/jonboulle/clockwork"
35-
36-
ofpb "github.com/buildbuddy-io/buildbuddy/proto/oci_fetcher"
37-
rgpb "github.com/buildbuddy-io/buildbuddy/proto/registry"
38-
repb "github.com/buildbuddy-io/buildbuddy/proto/remote_execution"
39-
gcrname "github.com/google/go-containerregistry/pkg/name"
40-
gcr "github.com/google/go-containerregistry/pkg/v1"
4139
bspb "google.golang.org/genproto/googleapis/bytestream"
4240
)
4341

@@ -198,29 +196,24 @@ func (c Credentials) Equals(o Credentials) bool {
198196
return c.Username == o.Username && c.Password == o.Password
199197
}
200198

201-
type tagToDigestEntry struct {
202-
nameWithDigest string
203-
expiration time.Time
204-
}
205-
206199
type Resolver struct {
207200
env environment.Env
208201

209-
allowedPrivateIPs []*net.IPNet
210-
211-
mu sync.Mutex
212-
imageTagToDigestLRU lru.LRU[tagToDigestEntry]
213-
clock clockwork.Clock
202+
allowedPrivateIPs []*net.IPNet
203+
imageTagToDigestLRU lru.LRU[string]
214204
}
215205

216206
func NewResolver(env environment.Env) (*Resolver, error) {
217207
allowedPrivateIPNets, err := ocifetcher.ParseAllowedPrivateIPs()
218208
if err != nil {
219209
return nil, err
220210
}
221-
imageTagToDigestLRU, err := lru.New[tagToDigestEntry](&lru.Config[tagToDigestEntry]{
222-
SizeFn: func(_ tagToDigestEntry) int64 { return 1 },
223-
MaxSize: int64(resolveImageDigestLRUMaxEntries),
211+
imageTagToDigestLRU, err := lru.New[string](&lru.Config[string]{
212+
SizeFn: func(_ string) int64 { return 1 },
213+
MaxSize: int64(resolveImageDigestLRUMaxEntries),
214+
TTL: resolveImageDigestLRUDuration,
215+
Clock: env.GetClock(),
216+
ThreadSafe: true,
224217
})
225218
if err != nil {
226219
return nil, err
@@ -229,7 +222,6 @@ func NewResolver(env environment.Env) (*Resolver, error) {
229222
env: env,
230223
imageTagToDigestLRU: imageTagToDigestLRU,
231224
allowedPrivateIPs: allowedPrivateIPNets,
232-
clock: env.GetClock(),
233225
}, nil
234226
}
235227

@@ -276,17 +268,8 @@ func (r *Resolver) ResolveImageDigest(ctx context.Context, imageName string, pla
276268
return "", status.InvalidArgumentErrorf("invalid image name %q", imageName)
277269
}
278270

279-
r.mu.Lock()
280-
entry, ok := r.imageTagToDigestLRU.Get(tagRef.String())
281-
r.mu.Unlock()
282-
if ok {
283-
if entry.expiration.After(r.clock.Now()) {
284-
return entry.nameWithDigest, nil
285-
}
286-
// The entry has expired. Evict it!
287-
r.mu.Lock()
288-
r.imageTagToDigestLRU.Remove(tagRef.String())
289-
r.mu.Unlock()
271+
if nameWithDigest, ok := r.imageTagToDigestLRU.Get(tagRef.String()); ok {
272+
return nameWithDigest, nil
290273
}
291274

292275
remoteOpts := r.getRemoteOpts(ctx, platform, credentials)
@@ -298,13 +281,7 @@ func (r *Resolver) ResolveImageDigest(ctx context.Context, imageName string, pla
298281
return "", status.UnavailableErrorf("could not fetch manifest metadata from remote registry: %s", err)
299282
}
300283
imageNameWithDigest := tagRef.Context().Digest(desc.Digest.String()).String()
301-
entryToAdd := tagToDigestEntry{
302-
nameWithDigest: imageNameWithDigest,
303-
expiration: r.clock.Now().Add(resolveImageDigestLRUDuration),
304-
}
305-
r.mu.Lock()
306-
r.imageTagToDigestLRU.Add(tagRef.String(), entryToAdd)
307-
r.mu.Unlock()
284+
r.imageTagToDigestLRU.Add(tagRef.String(), imageNameWithDigest)
308285
return imageNameWithDigest, nil
309286
}
310287

0 commit comments

Comments
 (0)