forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbagecollector.go
78 lines (68 loc) · 2.11 KB
/
garbagecollector.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
package main
import "fmt"
// A resource is something that can be deleted. Rating provides an indication
// of how "valuable" it is. A higher value means it should be preserved in
// favour of a resource with a lower rating.
type Resource interface {
Rating() float64
Expunge(task *TaskRun) error
}
// Resources is a type that can be sorted in order to establish in which order
// resources should be expunged.
type Resources []Resource
func (r Resources) Empty() bool {
return len(r) == 0
}
func (r *Resources) ExpungeNext() error {
err := (*r)[0].Expunge(nil)
if err != nil {
return err
}
*r = (*r)[1:]
return nil
}
// Implement sort.Interface to sort by deletion order.
func (r Resources) Len() int {
return len(r)
}
func (r Resources) Less(i, j int) bool {
return r[i].Rating() < r[j].Rating()
}
func (r Resources) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// Note ideally this would run in an independent thread, but since we have one
// job at a time, we can sequence it between task runs. Also it should be
// independent of mounts feature, but let's go with it here as currently that
// is the only feature that uses it.
func runGarbageCollection(r Resources) error {
currentFreeSpace, err := freeDiskSpaceBytes(taskContext.TaskDir)
if err != nil {
return fmt.Errorf("Could not calculate free disk space in dir %v due to error %#v", taskContext.TaskDir, err)
}
requiredFreeSpace := requiredSpaceBytes()
for currentFreeSpace < requiredFreeSpace {
// need to free up space
if r.Empty() {
break
}
err = r.ExpungeNext()
if err != nil {
return err
}
currentFreeSpace, err = freeDiskSpaceBytes(taskContext.TaskDir)
if err != nil {
return err
}
}
if currentFreeSpace < requiredFreeSpace {
return fmt.Errorf("Not able to free up enough disk space - require %v bytes, but only have %v bytes - and nothing left to delete", requiredFreeSpace, currentFreeSpace)
}
return nil
}
func requiredSpaceBytes() uint64 {
// note it used to be:
// uint64(config.RequiredDiskSpaceMegabytes * 1024 * 1024)
// but then it overflows on 32 bit systems
return uint64(config.RequiredDiskSpaceMegabytes) * 1024 * 1024
}