Skip to content

Commit 59187a0

Browse files
authored
Fix S3 cache (#206)
* Fix S3 cache * Check for 404
1 parent 9d772e6 commit 59187a0

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

Diff for: pkg/leeway/cache/remote/s3.go

+60-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"strings"
910
"sync"
1011

1112
"github.com/aws/aws-sdk-go-v2/aws"
@@ -136,6 +137,7 @@ func (s *S3Cache) ExistingPackages(ctx context.Context, pkgs []cache.Package) (m
136137
"key": gzKey,
137138
"error": err,
138139
}).Debug("failed to check .tar.gz in remote cache, will try .tar")
140+
// Continue to check .tar format - don't return error here
139141
} else if exists {
140142
log.WithFields(log.Fields{
141143
"package": p.FullName(),
@@ -156,6 +158,7 @@ func (s *S3Cache) ExistingPackages(ctx context.Context, pkgs []cache.Package) (m
156158
"key": tarKey,
157159
"error": err,
158160
}).Debug("failed to check .tar in remote cache")
161+
// Don't return error for missing objects - this is expected
159162
return nil // Continue with next package, will trigger local build
160163
}
161164

@@ -178,17 +181,17 @@ func (s *S3Cache) ExistingPackages(ctx context.Context, pkgs []cache.Package) (m
178181
})
179182

180183
if err != nil {
181-
log.WithError(err).Error("failed to check existing packages in remote cache")
184+
log.WithError(err).Warn("failed to check existing packages in remote cache")
182185
// Return partial results even if some checks failed
183-
return result, err
186+
return result, nil
184187
}
185188

186189
return result, nil
187190
}
188191

189192
// Download implements RemoteCache
190193
func (s *S3Cache) Download(ctx context.Context, dst cache.LocalCache, pkgs []cache.Package) error {
191-
return s.processPackages(ctx, pkgs, func(ctx context.Context, p cache.Package) error {
194+
err := s.processPackages(ctx, pkgs, func(ctx context.Context, p cache.Package) error {
192195
version, err := p.Version()
193196
if err != nil {
194197
return fmt.Errorf("failed to get version for package %s: %w", p.FullName(), err)
@@ -209,22 +212,40 @@ func (s *S3Cache) Download(ctx context.Context, dst cache.LocalCache, pkgs []cac
209212
}).Debug("successfully downloaded package from remote cache (.tar.gz)")
210213
return nil
211214
}
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")
215+
216+
// Check if this is a "not found" error
217+
if strings.Contains(err.Error(), "NotFound") || strings.Contains(err.Error(), "404") {
218+
log.WithFields(log.Fields{
219+
"package": p.FullName(),
220+
"key": gzKey,
221+
}).Debug("package not found in remote cache (.tar.gz), trying .tar")
222+
} else {
223+
log.WithFields(log.Fields{
224+
"package": p.FullName(),
225+
"key": gzKey,
226+
"error": err,
227+
}).Debug("failed to download .tar.gz from remote cache, trying .tar")
228+
}
217229

218230
// Try .tar if .tar.gz fails
219231
tarKey := fmt.Sprintf("%s.tar", version)
220232
_, err = s.storage.GetObject(ctx, tarKey, localPath)
221233
if err != nil {
234+
// Check if this is a "not found" error
235+
if strings.Contains(err.Error(), "NotFound") || strings.Contains(err.Error(), "404") {
236+
log.WithFields(log.Fields{
237+
"package": p.FullName(),
238+
"key": tarKey,
239+
}).Debug("package not found in remote cache (.tar), will build locally")
240+
return nil // Not an error, just not found
241+
}
242+
222243
log.WithFields(log.Fields{
223244
"package": p.FullName(),
224245
"key": tarKey,
225246
"error": err,
226247
}).Debug("failed to download package from remote cache, will build locally")
227-
return fmt.Errorf("failed to download package %s: %w", p.FullName(), err)
248+
return nil // Continue with local build
228249
}
229250

230251
log.WithFields(log.Fields{
@@ -233,6 +254,14 @@ func (s *S3Cache) Download(ctx context.Context, dst cache.LocalCache, pkgs []cac
233254
}).Debug("successfully downloaded package from remote cache (.tar)")
234255
return nil
235256
})
257+
258+
if err != nil {
259+
log.WithError(err).Warn("errors occurred during download from remote cache")
260+
// Continue with local builds for packages that couldn't be downloaded
261+
return nil
262+
}
263+
264+
return nil
236265
}
237266

238267
// Upload implements RemoteCache
@@ -287,10 +316,17 @@ func (s *S3Storage) HasObject(ctx context.Context, key string) (bool, error) {
287316
})
288317

289318
if err != nil {
319+
// Check for various "not found" error types
290320
var nsk *types.NoSuchKey
291321
if errors.As(err, &nsk) {
292322
return false, nil
293323
}
324+
325+
// Also handle 404 NotFound errors which might not be properly wrapped as NoSuchKey
326+
if strings.Contains(err.Error(), "NotFound") || strings.Contains(err.Error(), "404") {
327+
return false, nil
328+
}
329+
294330
return false, err
295331
}
296332

@@ -318,7 +354,22 @@ func (s *S3Storage) GetObject(ctx context.Context, key string, dest string) (int
318354
Bucket: aws.String(s.bucketName),
319355
Key: aws.String(key),
320356
})
357+
321358
if err != nil {
359+
// Remove the partially downloaded file if there was an error
360+
os.Remove(dest)
361+
362+
// Check for various "not found" error types
363+
var nsk *types.NoSuchKey
364+
if errors.As(err, &nsk) {
365+
return 0, fmt.Errorf("object not found: %w", err)
366+
}
367+
368+
// Also handle 404 NotFound errors which might not be properly wrapped
369+
if strings.Contains(err.Error(), "NotFound") || strings.Contains(err.Error(), "404") {
370+
return 0, fmt.Errorf("object not found: %w", err)
371+
}
372+
322373
return 0, fmt.Errorf("failed to download object: %w", err)
323374
}
324375

Diff for: pkg/leeway/cache/remote/s3_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestS3Cache_ExistingPackages(t *testing.T) {
129129
return &s3.HeadObjectOutput{}, nil
130130
},
131131
expectedResults: map[string]struct{}{},
132-
expectError: true,
132+
expectError: false,
133133
},
134134
}
135135

@@ -254,7 +254,8 @@ func TestS3Cache_Download(t *testing.T) {
254254
"v1": filepath.Join(tmpDir, "pkg1.tar.gz"),
255255
},
256256
},
257-
expectError: true,
257+
expectError: false,
258+
expectedFiles: []string{},
258259
},
259260
}
260261

0 commit comments

Comments
 (0)