-
Notifications
You must be signed in to change notification settings - Fork 19
/
clean.go
604 lines (506 loc) · 17 KB
/
clean.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* Copyright 2022 Aspect Build Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package clean
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/osutils/filesystem"
)
const (
unstructuredArgsBEPKey = "unstructuredCommandLine"
)
var diskCacheRegex = regexp.MustCompile(`--disk_cache.+?(\/.+?)"`)
type SelectRunner interface {
Run() (int, string, error)
}
type PromptRunner interface {
Run() (string, error)
}
type bazelDirInfo struct {
path string
size float64
humanReadableSize float64
unit string
workspaceName string
comparator float64
isCurrentWorkspace bool
accessTime time.Duration
processed bool
isCache bool
}
// Clean represents the aspect clean command.
type Clean struct {
ioutils.Streams
bzl bazel.Bazel
Filesystem filesystem.Filesystem
}
// New creates a Clean command.
func New(
streams ioutils.Streams,
bzl bazel.Bazel) *Clean {
return &Clean{
Streams: streams,
bzl: bzl,
}
}
func NewDefault(streams ioutils.Streams, bzl bazel.Bazel) *Clean {
runner := New(streams, bzl)
runner.Filesystem = filesystem.NewDefault()
return runner
}
// Run runs the aspect build command.
func (runner *Clean) Run(cmd *cobra.Command, args []string) error {
cleanAll := false
// TODO: move separation of flags and arguments to a high level of abstraction
flags := make([]string, 0)
for i := 0; i < len(args); i++ {
if args[i] == "all" {
cleanAll = true
continue
}
flags = append(flags, args[i])
}
if cleanAll {
return runner.reclaimAll()
}
bazelCmd := []string{"clean"}
bazelCmd = append(bazelCmd, flags...)
if exitCode, err := runner.bzl.RunCommand(runner.Streams, nil, bazelCmd...); exitCode != 0 {
err = &aspecterrors.ExitError{
Err: err,
ExitCode: exitCode,
}
return err
}
return nil
}
func (runner *Clean) reclaimAll() error {
sizeCalcQueue := make(chan bazelDirInfo, 64)
confirmationQueue := make(chan bazelDirInfo, 64)
deleteQueue := make(chan bazelDirInfo, 128)
sizeQueue := make(chan float64, 64)
errorQueue := make(chan error, 64)
var sizeCalcWaitGroup sync.WaitGroup
var confirmationWaitGroup sync.WaitGroup
var deleteWaitGroup sync.WaitGroup
var sizeWaitGroup sync.WaitGroup
var errorWaitGroup sync.WaitGroup
errors := errorSet{nodes: make(map[errorNode]struct{})}
// Goroutine for processing errors from the other threads.
go runner.errorProcessor(errorQueue, &errorWaitGroup, &errors)
// Goroutines for calculating sizes of directories.
for i := 0; i < 5; i++ {
go runner.sizeCalculator(sizeCalcQueue, confirmationQueue, &sizeCalcWaitGroup)
}
// Goroutines for deleting directories.
// We dont add to the wait group here as deleteProcessor will add to the deleteWaitGroup itself.
// This is due to deleteProcessor breaking up its deletes and adding subdirectories to the
// deleteQueue as it does the deleting. Where the other wait groups ensure all their goroutines
// have finished processing, deleteWaitGroup ensures deleteQueue is empty before the program exits.
for i := 0; i < 8; i++ {
go runner.deleteProcessor(deleteQueue, &deleteWaitGroup, sizeQueue, errorQueue)
}
go runner.sizePrinter(sizeQueue, &sizeWaitGroup)
// Goroutine for prompting the user to confirm deletion.
go runner.confirmationActor(confirmationQueue, deleteQueue, &confirmationWaitGroup, &deleteWaitGroup)
// Find disk caches and add them to the sizeCalculator queue.
runner.findDiskCaches(sizeCalcQueue, errorQueue)
// Find bazel workspaces and add them to the sizeCalculator queue.
runner.findBazelWorkspaces(sizeCalcQueue, errorQueue)
// Since the directories are added to sizeCalcQueue synchronously, so we can
// close the channel before waiting for the calculations to complete.
close(sizeCalcQueue)
sizeCalcWaitGroup.Wait()
// Since the confirmationQueue will be filled before sizeCalcWaitGroup completes,
// we can close the channel before waiting.
close(confirmationQueue)
confirmationWaitGroup.Wait()
// Since the deleteQueue can add to itself to improve speeds. We need to wait for
// all deletes to complete before we can close the channel.
deleteWaitGroup.Wait()
close(deleteQueue)
// Since the sizeQueue will contain all the sizes once the deletes are completed,
// we can close the chan before we wait.
close(sizeQueue)
sizeWaitGroup.Wait()
// All the errors we will receive will be in the errorQueue at this point,
// so we can close the queue before waiting.
close(errorQueue)
errorWaitGroup.Wait()
if errors.size > 0 {
return errors.generateError()
}
return nil
}
func (runner *Clean) confirmationActor(
directories <-chan bazelDirInfo,
deleteQueue chan<- bazelDirInfo,
confirmationWaitGroup *sync.WaitGroup,
deleteWaitGroup *sync.WaitGroup,
) {
confirmationWaitGroup.Add(1)
for bazelDir := range directories {
var label string
if bazelDir.isCache {
label = fmt.Sprintf("Cache: %s, Age: %s, Size: %.2f %s. Would you like to remove?", bazelDir.workspaceName, bazelDir.accessTime, bazelDir.humanReadableSize, bazelDir.unit)
} else {
label = fmt.Sprintf("Workspace: %s, Age: %s, Size: %.2f %s. Would you like to remove?", bazelDir.workspaceName, bazelDir.accessTime, bazelDir.humanReadableSize, bazelDir.unit)
}
promptRemove := &promptui.Prompt{
Label: label,
IsConfirm: true,
}
if _, err := promptRemove.Run(); err == nil {
fmt.Fprintf(runner.Streams.Stdout, "%s added to the delete queue\n", bazelDir.workspaceName)
deleteWaitGroup.Add(1)
deleteQueue <- bazelDir
} else {
// promptui.ErrInterrupt is the error returned when SIGINT is received.
// This is most likely due to the user hitting ctrl+runner in the terminal.
if errors.Is(err, promptui.ErrInterrupt) {
// We allow the program to gracefully exit in such case.
break
}
}
promptContinue := &promptui.Prompt{
Label: "Would you like to continue?",
IsConfirm: true,
}
if _, err := promptContinue.Run(); err != nil || errors.Is(err, promptui.ErrInterrupt) {
break
}
}
confirmationWaitGroup.Done()
}
func (runner *Clean) findDiskCaches(
sizeCalcQueue chan<- bazelDirInfo,
errors chan<- error,
) {
tempDir, err := os.MkdirTemp("", "tmp_bazel_output")
if err != nil {
errors <- fmt.Errorf("failed to find disk caches: failed to create tmp dir: %w", err)
return
}
defer os.RemoveAll(tempDir)
bepLocation := filepath.Join(tempDir, "bep.json")
streams := ioutils.Streams{
Stdin: nil,
Stdout: nil,
Stderr: nil,
}
// Running an invalid query should ensure that repository rules are not executed.
// However, bazel will still emit its BEP containing the flag that we are interested in.
// This will ensure it returns quickly and allows us to easily access said flag.
runner.bzl.RunCommand(
streams,
nil,
"query",
"//",
"--build_event_json_file="+bepLocation,
// We dont want bazel to print anything to the command line.
// We are only interested in the BEP output
"--ui_event_filters=-fatal,-error,-warning,-info,-progress,-debug,-start,-finish,-subcommand,-stdout,-stderr,-pass,-fail,-timeout,-cancelled,-depchecker",
"--noshow_progress",
)
file, err := os.Open(bepLocation)
if err != nil {
errors <- fmt.Errorf("failed to find disk caches: failed to open BEP file: %w", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if strings.Contains(text, unstructuredArgsBEPKey) {
result := diskCacheRegex.FindAllStringSubmatch(text, -1)
for i := range result {
cachePath := result[i][1]
cacheInfo := bazelDirInfo{
path: cachePath,
isCurrentWorkspace: false,
isCache: true,
}
fileStat, err := os.Stat(cachePath)
if err != nil {
errors <- fmt.Errorf("failed to find disk caches: failed to stat potential cache: %w", err)
return
}
cacheInfo.accessTime = runner.Filesystem.GetAccessTime(fileStat)
cacheInfo.workspaceName = cachePath
sizeCalcQueue <- cacheInfo
}
}
}
if err := scanner.Err(); err != nil {
errors <- fmt.Errorf("failed to find disk caches: failed to read BEP file: %w", err)
return
}
}
func (runner *Clean) findBazelWorkspaces(
sizeCalcQueue chan<- bazelDirInfo,
errors chan<- error,
) {
bazelBaseDir, currentWorkingBase, err := runner.findBazelBaseDir()
if err != nil {
errors <- fmt.Errorf("failed to find bazel workspaces: failed to find bazel base directory: %w", err)
return
}
bazelWorkspaces, err := os.ReadDir(bazelBaseDir)
if err != nil {
errors <- fmt.Errorf("failed to find bazel workspaces: failed to read bazel base directory: %w", err)
return
}
// Find bazel workspaces and start processing.
for _, workspace := range bazelWorkspaces {
workspaceInfo := bazelDirInfo{
path: filepath.Join(bazelBaseDir, workspace.Name()),
isCurrentWorkspace: workspace.Name() == currentWorkingBase,
isCache: false,
}
wkFileInfo, err := workspace.Info()
if err != nil {
errors <- fmt.Errorf("failed to find bazel workspaces: failed to retrieve file info: %w", err)
return
}
workspaceInfo.accessTime = runner.Filesystem.GetAccessTime(wkFileInfo)
execrootFiles, readDirErr := os.ReadDir(filepath.Join(bazelBaseDir, workspace.Name(), "execroot"))
if readDirErr != nil {
// The install and cache directories will end up here.We must not remove these
continue
}
// We expect these directories to have up to 2 files / directories:
// - Firstly, a file named "DO_NOT_BUILD_HERE".
// - Secondly, a directory named after the given workspace.
// We can use the given second directory to determine the name of the workspace. We want this
// so that we can ask the user if they want to remove a given workspace.
if (len(execrootFiles) == 1 && execrootFiles[0].Name() == "DO_NOT_BUILD_HERE") || len(execrootFiles) > 2 {
// TODO: Only ask the user if they want to remove unknown workspace once.
// https://github.com/aspect-build/aspect-cli/issues/208
workspaceInfo.workspaceName = "Unknown Workspace"
} else {
for _, execrootFile := range execrootFiles {
if execrootFile.Name() != "DO_NOT_BUILD_HERE" {
workspaceInfo.workspaceName = execrootFile.Name()
}
}
}
sizeCalcQueue <- workspaceInfo
}
}
func (runner *Clean) sizeCalculator(
in <-chan bazelDirInfo,
out chan<- bazelDirInfo,
waitGroup *sync.WaitGroup,
) {
waitGroup.Add(1)
for bazelDir := range in {
size, humanReadableSize, unit := runner.getDirSize(bazelDir.path)
bazelDir.size = size
bazelDir.humanReadableSize = humanReadableSize
bazelDir.unit = unit
bazelDir.processed = true
comparator := bazelDir.accessTime.Hours() * float64(size)
if bazelDir.isCurrentWorkspace {
// If we can avoid cleaning the current working directory then maybe we want to do so?
// If the user has selected this mode then they just want to reclaim resources.
// Keeping the bazel workspace for the current repo will mean faster build times for that repo.
// Dividing by 2 so that the current workspace will be listed later to the user.
comparator = comparator / 2
}
bazelDir.comparator = comparator
out <- bazelDir
}
waitGroup.Done()
}
func (runner *Clean) deleteProcessor(
deleteQueue chan bazelDirInfo,
waitGroup *sync.WaitGroup,
sizeQueue chan<- float64,
errors chan<- error,
) {
for bazelDir := range deleteQueue {
// We know that there will be an "external" directory that could be deleted in parallel.
// So we can move those directories to a tmp filepath and add them as seperate deletes that will
// therefore happen in parallel.
externalDirectories, _ := os.ReadDir(filepath.Join(bazelDir.path, "external"))
for _, directory := range externalDirectories {
newPath, err := runner.Filesystem.MoveDirectoryToTmp(bazelDir.path, directory.Name())
if err != nil {
errors <- fmt.Errorf("failed to delete %q: failed to move directory to tmp: %w", bazelDir.path, err)
continue
}
if newPath != "" {
waitGroup.Add(1)
deleteQueue <- bazelDirInfo{
path: newPath,
// Size is used to calculate how much space has been reclaimed.
// This bazelDirInfo is for a subdirectory we want to delete in parallel.
// Rather than calculate the size of each subdirectory we can just use the
// already calculate size of the parent.
size: 0,
}
}
}
// The permissions set in the directories being removed don't allow write access,
// so we change the permissions before removing those directories.
if _, err := runner.Filesystem.ChangeDirectoryPermissions(bazelDir.path, "0777"); err != nil {
waitGroup.Done()
errors <- fmt.Errorf("failed to delete %q: failed to change permissions: %w", bazelDir.path, err)
continue
}
// Remove the entire directory tree.
if err := os.RemoveAll(bazelDir.path); err != nil {
waitGroup.Done()
errors <- fmt.Errorf("failed to delete %q: %w", bazelDir.path, err)
continue
}
sizeQueue <- bazelDir.size
waitGroup.Done()
}
}
func (runner *Clean) errorProcessor(errorQueue <-chan error, waitGroup *sync.WaitGroup, errors *errorSet) {
waitGroup.Add(1)
for err := range errorQueue {
errors.insert(err)
}
waitGroup.Done()
}
func (runner *Clean) sizePrinter(sizeQueue <-chan float64, waitGroup *sync.WaitGroup) {
waitGroup.Add(1)
var totalSize float64 = 0
for size := range sizeQueue {
totalSize = totalSize + size
}
_, hRSpaceReclaimed, unit := runner.makeBytesHumanReadable(totalSize)
fmt.Fprintf(runner.Streams.Stdout, "Space reclaimed: %.2f%s\n", hRSpaceReclaimed, unit)
waitGroup.Done()
}
func (runner *Clean) findBazelBaseDir() (string, string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", "", fmt.Errorf("failed to find Bazel base directory: failed to get current working directory: %w", err)
}
files, err := os.ReadDir(cwd)
if err != nil {
return "", "", fmt.Errorf("failed to find Bazel base directory: failed to read current working directory %w", err)
}
for _, dirEntry := range files {
file, err := dirEntry.Info()
if err != nil {
return "", "", err
}
// bazel-bin, bazel-out, etc... will be symlinks, so we can eliminate non-symlinks immediately.
if file.Mode()&os.ModeSymlink != 0 {
actualPath, err := os.Readlink(filepath.Join(cwd, file.Name()))
if err != nil {
return "", "", fmt.Errorf("failed to find Bazel base directory: failed to follow symlink: %w", err)
}
normalizedPath := filepath.ToSlash(actualPath)
if strings.Contains(normalizedPath, "bazel") && strings.Contains(normalizedPath, "/execroot/") {
execrootBase := strings.Split(normalizedPath, "/execroot/")[0]
execrootSplit := strings.Split(execrootBase, "/")
currentWorkingBase := execrootSplit[len(execrootSplit)-1]
bazelOutputBase := strings.Join(execrootSplit[:len(execrootSplit)-1], "/")
return bazelOutputBase, currentWorkingBase, nil
}
}
}
return "", "", fmt.Errorf("failed to find Bazel base directory: bazel output symlinks not found in directory")
}
func (runner *Clean) getDirSize(path string) (float64, float64, string) {
var size float64
filepath.Walk(path, func(path string, file os.FileInfo, err error) error {
if !file.IsDir() {
size += float64(file.Size())
}
return nil
})
return runner.makeBytesHumanReadable(size)
}
func (runner *Clean) makeBytesHumanReadable(bytes float64) (float64, float64, string) {
humanReadable, unit := runner.makeBytesHumanReadableInternal(bytes, "bytes")
return bytes, humanReadable, unit
}
func (runner *Clean) makeBytesHumanReadableInternal(bytes float64, unit string) (float64, string) {
if bytes < 1024 {
return bytes, unit
}
bytes = bytes / 1024
switch unit {
case "bytes":
unit = "KB"
case "KB":
unit = "MB"
case "MB":
unit = "GB"
case "GB":
unit = "TB"
case "TB":
unit = "PB"
}
if bytes >= 1024 {
return runner.makeBytesHumanReadableInternal(bytes, unit)
}
return bytes, unit
}
type errorSet struct {
head *errorNode
tail *errorNode
nodes map[errorNode]struct{}
size int
}
func (s *errorSet) generateError() error {
var err error
for node := s.head; node != nil; node = node.next {
if err == nil {
err = node.err
} else {
err = fmt.Errorf("%s, %w", err, node.err)
}
}
return err
}
func (s *errorSet) insert(err error) {
node := errorNode{
err: err,
}
if _, exists := s.nodes[node]; !exists {
s.nodes[node] = struct{}{}
if s.head == nil {
s.head = &node
} else {
s.tail.next = &node
}
s.tail = &node
s.size++
}
}
type errorNode struct {
next *errorNode
err error
}