This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.go
511 lines (447 loc) · 16.6 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package executor
import (
"bufio"
"context"
"crypto/sha256"
"fmt"
"github.com/bmatcuk/doublestar"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-ci-agent/internal/environment"
"github.com/cirruslabs/cirrus-ci-agent/internal/hasher"
"github.com/cirruslabs/cirrus-ci-agent/internal/http_cache"
"github.com/cirruslabs/cirrus-ci-agent/internal/targz"
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type Cache struct {
Name string
Key string
BaseFolder string
PartiallyExpandedFolders []string
FileHasher *hasher.Hasher
SkipUpload bool
CacheAvailable bool
}
var caches = make([]Cache, 0)
var httpClient = &http.Client{
Timeout: 10 * time.Minute,
}
func (executor *Executor) DownloadCache(
ctx context.Context,
logUploader *LogUploader,
commandName string,
cacheHost string,
instruction *api.CacheInstruction,
custom_env *environment.Environment,
) bool {
cacheKey, ok := executor.generateCacheKey(ctx, logUploader, commandName, instruction, custom_env)
if !ok {
return false
}
// Partially expand cache folders without and keep them for further re-evaluation in UploadCache()
//
// Once in UploadCache(), the cache will be populated, and the globbing may yield a different result.
var partiallyExpandedFolders []string
for _, folder := range instruction.Folders {
folder = custom_env.ExpandText(folder)
folder, err := filepath.Abs(folder)
if err != nil {
message := fmt.Sprintf("\nFailed to compute absolute path for cache folder '%s': %v\n", folder, err)
executor.cacheAttempts.Failed(cacheKey, message)
logUploader.Write([]byte(message))
return false
}
partiallyExpandedFolders = append(partiallyExpandedFolders, folder)
}
// Determine the base folder
baseFolder := custom_env.Get("CIRRUS_WORKING_DIR")
if len(partiallyExpandedFolders) == 1 && !pathLooksLikeGlob(partiallyExpandedFolders[0]) {
baseFolder = partiallyExpandedFolders[0]
}
// Perform a sanity check against the base folder
//
// When we're dealing with multiple cache folders, the semantics is
// clearly defined only when all folders are scoped to the current
// working directory, otherwise it's impossible to make paths inside
// of the archive portable (i.e. independent on the location of the
// working directory).
//
// Note: this is not a security stop-gap but merely a hint to the users
// that they are doing something wrong.
if len(partiallyExpandedFolders) > 1 {
for _, partiallyExpandedFolder := range partiallyExpandedFolders {
terminatedWorkingDir := baseFolder
if !strings.HasSuffix(terminatedWorkingDir, string(os.PathSeparator)) {
terminatedWorkingDir += string(os.PathSeparator)
}
if !strings.HasPrefix(partiallyExpandedFolder, terminatedWorkingDir) {
message := fmt.Sprintf("\nWhen using globs or multiple cache folders, all folders should be relative to "+
"the current working directory, yet, folder '%s' points above the current working directory '%s'\n",
partiallyExpandedFolder, terminatedWorkingDir)
executor.cacheAttempts.Failed(cacheKey, message)
logUploader.Write([]byte(message))
return false
}
}
}
cachePopulated, cacheAvailable := executor.tryToDownloadAndPopulateCache(ctx, logUploader, commandName, cacheHost, cacheKey, baseFolder)
// Expand cache folders in case they contain potential globs,
// so we can calculate the hashes for directories that already exist
foldersToCache, message := executor.expandAndDeduplicateGlobs(partiallyExpandedFolders)
if message != "" {
executor.cacheAttempts.Failed(cacheKey, message)
logUploader.Write([]byte(message))
return false
}
fileHasher := hasher.New()
if cachePopulated {
for _, folderToCache := range foldersToCache {
if err := fileHasher.AddFolder(baseFolder, folderToCache); err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to calculate hash of %s! %s", folderToCache, err)))
}
}
}
if !cachePopulated && len(instruction.PopulateScripts) > 0 {
populateStartTime := time.Now()
logUploader.Write([]byte(fmt.Sprintf("\nCache miss for %s! Populating...\n", cacheKey)))
cmd, err := ShellCommandsAndWait(ctx, instruction.PopulateScripts, custom_env, func(bytes []byte) (int, error) {
return logUploader.Write(bytes)
}, executor.shouldKillProcesses())
if err != nil || cmd == nil || cmd.ProcessState == nil || !cmd.ProcessState.Success() {
message := fmt.Sprintf("\nFailed to execute populate script for %s cache!", commandName)
executor.cacheAttempts.Failed(cacheKey, message)
logUploader.Write([]byte(message))
return false
}
executor.cacheAttempts.PopulatedIn(cacheKey, time.Since(populateStartTime))
} else if !cachePopulated {
logUploader.Write([]byte(fmt.Sprintf("\nCache miss for %s! No script to populate with.", cacheKey)))
}
caches = append(
caches,
Cache{
Name: commandName,
Key: cacheKey,
BaseFolder: baseFolder,
PartiallyExpandedFolders: partiallyExpandedFolders,
FileHasher: fileHasher,
SkipUpload: cacheAvailable && !instruction.ReuploadOnChanges,
CacheAvailable: cacheAvailable,
},
)
return true
}
func (executor *Executor) generateCacheKey(
ctx context.Context,
logUploader *LogUploader,
commandName string,
instruction *api.CacheInstruction,
custom_env *environment.Environment,
) (string, bool) {
if instruction.FingerprintKey != "" {
return instruction.FingerprintKey, true
}
cacheKeyHash := sha256.New()
if len(instruction.FingerprintScripts) > 0 {
cmd, err := ShellCommandsAndWait(ctx, instruction.FingerprintScripts, custom_env, func(bytes []byte) (int, error) {
cacheKeyHash.Write(bytes)
return logUploader.Write(bytes)
}, executor.shouldKillProcesses())
if err != nil || !cmd.ProcessState.Success() {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to execute fingerprint script for %s cache!", commandName)))
return "", false
}
} else {
cacheKeyHash.Write([]byte(custom_env.Get("CIRRUS_TASK_NAME")))
cacheKeyHash.Write([]byte(custom_env.Get("CI_NODE_INDEX")))
}
return fmt.Sprintf("%s-%x", commandName, cacheKeyHash.Sum(nil)), true
}
func (executor *Executor) expandAndDeduplicateGlobs(folders []string) ([]string, string) {
var result []string
for _, folder := range folders {
if pathLooksLikeGlob(folder) {
expandedGlob, err := doublestar.Glob(folder)
if err != nil {
return nil, fmt.Sprintf("\nCannot expand cache folder glob '%s': %v\n", folder, err)
}
result = append(result, expandedGlob...)
} else {
result = append(result, folder)
}
}
// Deduplicate paths to improve UX
result = DeduplicatePaths(result)
return result, ""
}
func pathLooksLikeGlob(path string) bool {
return strings.Contains(path, "*")
}
func (executor *Executor) tryToDownloadAndPopulateCache(
ctx context.Context,
logUploader *LogUploader,
commandName string,
cacheHost string,
cacheKey string,
folderToCache string,
) (bool, bool) { // successfully populated, available remotely
cacheFile, fetchDuration, err := FetchCache(ctx, logUploader, commandName, cacheHost, cacheKey)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to fetch archive for %s cache: %s!", commandName, err)))
if err, ok := err.(net.Error); ok && err.Timeout() {
return false, true
} else {
return false, false
}
}
if cacheFile == nil {
return false, false
}
cacheFileInfo, statErr := os.Stat(cacheFile.Name())
if statErr != nil {
executor.cacheAttempts.Failed(cacheKey, fmt.Sprintf("failed to determine cache file size: %v", statErr))
}
_, _ = logUploader.Write([]byte(fmt.Sprintf("\nCache hit for %s!", cacheKey)))
unarchiveStartTime := time.Now()
err = unarchiveCache(cacheFile, folderToCache)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to unarchive %s cache because of %s! Retrying...\n", commandName, err)))
os.RemoveAll(folderToCache)
cacheFile, fetchDuration, err = FetchCache(ctx, logUploader, commandName, cacheHost, cacheKey)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to fetch archive for %s cache: %s!", commandName, err)))
if err, ok := err.(net.Error); ok && err.Timeout() {
return false, true
} else {
return false, false
}
}
if cacheFile == nil {
return false, true
}
err = unarchiveCache(cacheFile, folderToCache)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed again to unarchive %s cache because of %s!\n", commandName, err)))
logUploader.Write([]byte(fmt.Sprintf("\nTreating this failure as a cache miss but won't try to re-upload! Cleaning up %s...\n", folderToCache)))
os.RemoveAll(folderToCache)
return false, true
}
} else {
unarchiveDuration := time.Since(unarchiveStartTime)
if unarchiveDuration > 10*time.Second {
logUploader.Write([]byte(fmt.Sprintf("\nUnarchived %s cache entry in %f seconds!\n", commandName, unarchiveDuration.Seconds())))
}
}
if statErr == nil {
executor.cacheAttempts.Hit(cacheKey, uint64(cacheFileInfo.Size()), fetchDuration, time.Since(unarchiveStartTime))
}
return true, true
}
func unarchiveCache(
cacheFile *os.File,
folderToCache string,
) error {
defer os.Remove(cacheFile.Name())
EnsureFolderExists(folderToCache)
return targz.Unarchive(cacheFile.Name(), folderToCache)
}
func FetchCache(
ctx context.Context,
logUploader *LogUploader,
commandName string,
cacheHost string,
cacheKey string,
) (*os.File, time.Duration, error) {
cacheFile, err := os.CreateTemp(os.TempDir(), commandName)
if err != nil {
log.Printf("Failed to create a temp file %s: %v\n", commandName, err)
logUploader.Write([]byte(fmt.Sprintf("\nCache miss for %s!", commandName)))
return nil, 0, err
}
defer cacheFile.Close()
downloadStartTime := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://%s/%s", cacheHost, cacheKey), nil)
if err != nil {
log.Printf("Failed to create a cache request for %s: %v\n", commandName, err)
return nil, 0, err
}
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("HTTP cache request for %s failed: %v\n", commandName, err)
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("HTTP cache request for %s status: %s\n", commandName, resp.Status)
return nil, 0, nil
}
bufferedFileWriter := bufio.NewWriter(cacheFile)
bytesDownloaded, err := bufferedFileWriter.ReadFrom(bufio.NewReader(resp.Body))
if err != nil {
log.Printf("Failed to finish downloading %s cache: %v\n", commandName, err)
return nil, 0, err
}
err = bufferedFileWriter.Flush()
if err != nil {
log.Printf("Failed to flush %s cache: %v\n", commandName, err)
return nil, 0, err
}
downloadDuration := time.Since(downloadStartTime)
if bytesDownloaded < 1024 {
logUploader.Write([]byte(fmt.Sprintf("\nDownloaded %d bytes.", bytesDownloaded)))
} else if bytesDownloaded < 1024*1024 {
logUploader.Write([]byte(fmt.Sprintf("\nDownloaded %dKb.", bytesDownloaded/1024)))
} else {
logUploader.Write([]byte(fmt.Sprintf("\nDownloaded %dMb in %fs.", bytesDownloaded/1024/1024, downloadDuration.Seconds())))
}
return cacheFile, downloadDuration, nil
}
func (executor *Executor) UploadCache(
ctx context.Context,
logUploader *LogUploader,
commandName string,
cacheHost string,
instruction *api.UploadCacheInstruction,
) bool {
var err error
cache := FindCache(instruction.CacheName)
if cache == nil {
logUploader.Write([]byte(fmt.Sprintf("No cache found for %s!", instruction.CacheName)))
return false // cache record should always exists
}
if cache.SkipUpload {
logUploader.Write([]byte(fmt.Sprintf("Skipping change detection for %s cache!", instruction.CacheName)))
return true
}
foldersToCache, message := executor.expandAndDeduplicateGlobs(cache.PartiallyExpandedFolders)
if message != "" {
logUploader.Write([]byte(message))
return false
}
commaSeparatedFolders := strings.Join(foldersToCache, ", ")
if allDirsEmpty(foldersToCache) {
logUploader.Write([]byte(fmt.Sprintf("All cache folders (%s) are empty! Skipping uploading ...", commaSeparatedFolders)))
return true
}
fileHasher := hasher.New()
for _, folder := range foldersToCache {
if err := fileHasher.AddFolder(cache.BaseFolder, folder); err != nil {
logUploader.Write([]byte(fmt.Sprintf("Failed to calculate hash of %s! %s", folder, err)))
logUploader.Write([]byte("Skipping uploading of cache!"))
return true
}
}
logUploader.Write([]byte(fmt.Sprintf("SHA for cache folders (%s) is '%s'\n", commaSeparatedFolders, fileHasher.SHA())))
if fileHasher.SHA() == cache.FileHasher.SHA() {
logUploader.Write([]byte(fmt.Sprintf("Cache %s hasn't changed! Skipping uploading...", cache.Name)))
return true
}
if cache.FileHasher.Len() != 0 {
logUploader.Write([]byte(fmt.Sprintf("Cache %s has changed!", cache.Name)))
logUploader.Write([]byte(fmt.Sprintf("\nList of changes for cache folders (%s):", commaSeparatedFolders)))
for _, diffEntry := range cache.FileHasher.DiffWithNewer(fileHasher) {
logUploader.Write([]byte(fmt.Sprintf("\n%s: %s", diffEntry.Type.String(), diffEntry.Path)))
}
}
cacheFile, err := os.CreateTemp("", "")
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to create temporary cache file: %v", err)))
return false
}
defer os.Remove(cacheFile.Name())
archiveStartTime := time.Now()
err = targz.Archive(cache.BaseFolder, foldersToCache, cacheFile.Name())
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to tar caches for %s with %s!", commandName, err)))
return false
}
archivingDuration := time.Since(archiveStartTime)
fi, err := cacheFile.Stat()
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to create caches archive for %s with %s!", commandName, err)))
return false
}
bytesToUpload := fi.Size()
if bytesToUpload < 1024 {
logUploader.Write([]byte(fmt.Sprintf("\n%s cache size is %d bytes.", instruction.CacheName, bytesToUpload)))
} else if bytesToUpload < 1024*1024 {
logUploader.Write([]byte(fmt.Sprintf("\n%s cache size is %dKb.", instruction.CacheName, bytesToUpload/1024)))
} else {
logUploader.Write([]byte(fmt.Sprintf("\n%s cache size is %dMb.", instruction.CacheName, bytesToUpload/1024/1024)))
}
cacheURL := fmt.Sprintf("http://%s/%s", cacheHost, url.PathEscape(cache.Key))
if !cache.CacheAvailable {
// check if some other task has uploaded the cache already
req, err := http.NewRequestWithContext(ctx, http.MethodHead, cacheURL, nil)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to create cache check request to URL %s!", cacheURL)))
return false
}
response, _ := httpClient.Do(req)
if response != nil && response.StatusCode == http.StatusOK {
createdByTaskId := response.Header.Get(http_cache.CirrusHeaderCreatedBy)
if createdByTaskId != "" {
logUploader.Write([]byte(fmt.Sprintf("\nTask '%s' has already uploaded cache entry %s! Skipping upload...", createdByTaskId, cache.Key)))
} else {
logUploader.Write([]byte(fmt.Sprintf("\nSome other task has already uploaded cache entry %s! Skipping upload...", cache.Key)))
}
return true
}
}
logUploader.Write([]byte(fmt.Sprintf("\nUploading cache %s...", instruction.CacheName)))
uploadStartTime := time.Now()
err = UploadCacheFile(ctx, cacheURL, cacheFile)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to upload cache '%s': %s!", commandName, err)))
logUploader.Write([]byte("\nIgnoring the error..."))
return true
}
executor.cacheAttempts.Miss(cache.Key, uint64(bytesToUpload), archivingDuration, time.Since(uploadStartTime))
return true
}
func UploadCacheFile(ctx context.Context, cacheURL string, cacheFile *os.File) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cacheURL, cacheFile)
if err != nil {
return err
}
fileStat, err := cacheFile.Stat()
if err != nil {
return err
}
req.ContentLength = fileStat.Size()
req.Header.Set("Content-Type", "application/octet-stream")
response, err := httpClient.Do(req)
if err != nil {
return err
}
if response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusOK {
return fmt.Errorf("bad response status from HTTP cache %d: %s", response.StatusCode, response.Status)
}
return nil
}
func FindCache(cacheName string) *Cache {
for i := 0; i < len(caches); i++ {
if caches[i].Name == cacheName {
return &caches[i]
}
}
return nil
}
func DeduplicatePaths(paths []string) (result []string) {
sort.Strings(paths)
var previous string
for _, path := range paths {
if previous == "" || !strings.Contains(path, previous+string(os.PathSeparator)) {
result = append(result, path)
previous = path
}
}
return
}