-
Notifications
You must be signed in to change notification settings - Fork 2
/
prune.go
62 lines (58 loc) · 1.79 KB
/
prune.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
/* See the file "LICENSE.txt" for the full license governing this code. */
// Snapshot pruning ("aging")
// This is the core functionality: keep the number of snapshots within the
// user selected schedule's limits or maximum disk usage or other constraints
package main
import (
"log"
)
// Sieves snapshots according to schedule and marks them as obsolete. Also,
// enqueue them in the buffered channel q for later reuse or deletion.
func prune(q chan *snapshot, cl clock) {
intervals := schedules[config.Schedule]
// interval 0 does not need pruning, start with 1
for i := len(intervals) - 2; i > 0; i-- {
snapshots, err := findSnapshots(cl)
if err != nil {
log.Println(err)
return
}
if len(snapshots) < 2 {
log.Println("less than 2 snapshots found, not pruning")
return
}
iv := snapshots.interval(intervals, i, cl).state(stateComplete, stateObsolete)
pruneAgain := false
if len(iv) > 2 {
// prune highest interval by maximum number
if (i == len(intervals)-2) &&
(len(iv) > config.MaxKeep) &&
(config.MaxKeep != 0) {
debugf("%d snapshots in oldest interval", len(iv))
log.Printf("mark oldest as obsolete: %s", iv[0])
err := iv[0].transObsolete()
if err != nil {
log.Printf("could not transition snapshot: %s", err)
}
q <- iv[0]
pruneAgain = true
}
// regularly prune by sieving
youngest := len(iv) - 1
secondYoungest := youngest - 1
dist := iv[youngest].startTime.Sub(iv[secondYoungest].startTime)
if dist.Seconds() < intervals[i].Seconds() {
log.Printf("mark as obsolete: %s", iv[youngest].Name())
err := iv[youngest].transObsolete()
if err != nil {
log.Printf("could not transition snapshot: %s", err)
}
q <- iv[youngest]
pruneAgain = true
}
if pruneAgain {
prune(q, cl)
}
}
}
}