Skip to content

Commit 5e2799b

Browse files
authored
Fix S3 cache considering .tar.gz and .tar in the cache (#203)
1 parent 43a34cc commit 5e2799b

File tree

3 files changed

+502
-10
lines changed

3 files changed

+502
-10
lines changed

pkg/leeway/build.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,15 @@ func Build(pkg *Package, opts ...BuildOption) (err error) {
435435

436436
pkgsInRemoteCache, err := ctx.RemoteCache.ExistingPackages(context.Background(), toPackageInterface(pkgsToCheckRemoteCache))
437437
if err != nil {
438-
return err
438+
log.WithError(err).Warn("failed to check remote cache, proceeding with local build")
439+
pkgsInRemoteCache = make(map[cache.Package]struct{})
440+
}
441+
442+
// Log packages that will be built locally
443+
for _, p := range pkgsToCheckRemoteCache {
444+
if _, exists := pkgsInRemoteCache[p]; !exists {
445+
log.WithField("package", p.FullName()).Debug("package not found in remote cache, will build locally")
446+
}
439447
}
440448

441449
pkgsWillBeDownloaded := make(map[*Package]struct{})

pkg/leeway/cache/remote/s3.go

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,58 @@ func (s *S3Cache) ExistingPackages(ctx context.Context, pkgs []cache.Package) (m
127127
return fmt.Errorf("failed to get version: %w", err)
128128
}
129129

130-
// Check for .tar.gz first
130+
// Try .tar.gz first
131131
gzKey := fmt.Sprintf("%s.tar.gz", version)
132132
exists, err := s.storage.HasObject(ctx, gzKey)
133133
if err != nil {
134-
return fmt.Errorf("failed to check object %s: %w", gzKey, err)
135-
}
136-
137-
if exists {
134+
log.WithFields(log.Fields{
135+
"package": p.FullName(),
136+
"key": gzKey,
137+
"error": err,
138+
}).Debug("failed to check .tar.gz in remote cache, will try .tar")
139+
} else if exists {
140+
log.WithFields(log.Fields{
141+
"package": p.FullName(),
142+
"key": gzKey,
143+
}).Debug("found package in remote cache (.tar.gz)")
138144
mu.Lock()
139145
result[p] = struct{}{}
140146
mu.Unlock()
141147
return nil
142148
}
143149

144-
// Fall back to .tar
150+
// Fall back to .tar if .tar.gz doesn't exist or had error
145151
tarKey := fmt.Sprintf("%s.tar", version)
146152
exists, err = s.storage.HasObject(ctx, tarKey)
147153
if err != nil {
148-
return fmt.Errorf("failed to check object %s: %w", tarKey, err)
154+
log.WithFields(log.Fields{
155+
"package": p.FullName(),
156+
"key": tarKey,
157+
"error": err,
158+
}).Debug("failed to check .tar in remote cache")
159+
return nil // Continue with next package, will trigger local build
149160
}
150161

151162
if exists {
163+
log.WithFields(log.Fields{
164+
"package": p.FullName(),
165+
"key": tarKey,
166+
}).Debug("found package in remote cache (.tar)")
152167
mu.Lock()
153168
result[p] = struct{}{}
154169
mu.Unlock()
170+
} else {
171+
log.WithFields(log.Fields{
172+
"package": p.FullName(),
173+
"version": version,
174+
}).Debug("package not found in remote cache, will build locally")
155175
}
156176

157177
return nil
158178
})
159179

160180
if err != nil {
161-
log.WithError(err).Error("failed to check existing packages")
181+
log.WithError(err).Error("failed to check existing packages in remote cache")
162182
// Return partial results even if some checks failed
163183
return result, err
164184
}
@@ -183,16 +203,34 @@ func (s *S3Cache) Download(ctx context.Context, dst cache.LocalCache, pkgs []cac
183203
gzKey := fmt.Sprintf("%s.tar.gz", version)
184204
_, err = s.storage.GetObject(ctx, gzKey, localPath)
185205
if err == nil {
206+
log.WithFields(log.Fields{
207+
"package": p.FullName(),
208+
"key": gzKey,
209+
}).Debug("successfully downloaded package from remote cache (.tar.gz)")
186210
return nil
187211
}
212+
log.WithFields(log.Fields{
213+
"package": p.FullName(),
214+
"key": gzKey,
215+
"error": err,
216+
}).Debug("failed to download .tar.gz from remote cache, trying .tar")
188217

189218
// Try .tar if .tar.gz fails
190219
tarKey := fmt.Sprintf("%s.tar", version)
191220
_, err = s.storage.GetObject(ctx, tarKey, localPath)
192221
if err != nil {
222+
log.WithFields(log.Fields{
223+
"package": p.FullName(),
224+
"key": tarKey,
225+
"error": err,
226+
}).Debug("failed to download package from remote cache, will build locally")
193227
return fmt.Errorf("failed to download package %s: %w", p.FullName(), err)
194228
}
195229

230+
log.WithFields(log.Fields{
231+
"package": p.FullName(),
232+
"key": tarKey,
233+
}).Debug("successfully downloaded package from remote cache (.tar)")
196234
return nil
197235
})
198236
}
@@ -214,9 +252,21 @@ func (s *S3Cache) Upload(ctx context.Context, src cache.LocalCache, pkgs []cache
214252
})
215253
}
216254

255+
// s3ClientAPI is a subset of the S3 client interface we need
256+
type s3ClientAPI interface {
257+
HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
258+
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
259+
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
260+
AbortMultipartUpload(ctx context.Context, params *s3.AbortMultipartUploadInput, optFns ...func(*s3.Options)) (*s3.AbortMultipartUploadOutput, error)
261+
CompleteMultipartUpload(ctx context.Context, params *s3.CompleteMultipartUploadInput, optFns ...func(*s3.Options)) (*s3.CompleteMultipartUploadOutput, error)
262+
CreateMultipartUpload(ctx context.Context, params *s3.CreateMultipartUploadInput, optFns ...func(*s3.Options)) (*s3.CreateMultipartUploadOutput, error)
263+
UploadPart(ctx context.Context, params *s3.UploadPartInput, optFns ...func(*s3.Options)) (*s3.UploadPartOutput, error)
264+
ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
265+
}
266+
217267
// S3Storage implements ObjectStorage using AWS S3
218268
type S3Storage struct {
219-
client *s3.Client
269+
client s3ClientAPI
220270
bucketName string
221271
}
222272

0 commit comments

Comments
 (0)