zrepl/pruning/retentiongrid/retentiongrid.go

143 lines
3.1 KiB
Go
Raw Normal View History

2018-08-26 19:20:08 +02:00
package retentiongrid
2017-06-21 20:29:20 +02:00
import (
"sort"
"time"
)
2018-09-24 17:30:03 +02:00
type Interval interface {
2018-08-26 19:20:08 +02:00
Length() time.Duration
KeepCount() int
2017-06-21 20:29:20 +02:00
}
const RetentionGridKeepCountAll int = -1
2018-09-24 17:30:03 +02:00
type Grid struct {
intervals []Interval
2017-06-21 20:29:20 +02:00
}
2018-09-24 17:30:03 +02:00
type Entry interface {
2017-06-21 20:29:20 +02:00
Date() time.Time
}
2018-09-24 17:30:03 +02:00
func NewGrid(l []Interval) *Grid {
if len(l) == 0 {
panic("must specify at least one interval")
}
2017-06-21 20:29:20 +02:00
// TODO Maybe check for ascending interval lengths here, although the algorithm
// itself doesn't care about that.
2018-09-24 17:30:03 +02:00
return &Grid{l}
2017-06-21 20:29:20 +02:00
}
func (g Grid) FitEntries(entries []Entry) (keep, remove []Entry) {
if len(entries) == 0 {
return
2017-06-21 20:29:20 +02:00
}
// determine 'now' based on youngest snapshot
// => sort youngest-to-oldest
sort.SliceStable(entries, func(i, j int) bool {
return entries[i].Date().After(entries[j].Date())
})
now := entries[0].Date()
return g.fitEntriesWithNow(now, entries)
}
type bucket struct {
keepCount int
youngerThan time.Time
olderThanOrEq time.Time
entries []Entry
}
2017-06-21 20:29:20 +02:00
func makeBucketFromInterval(olderThanOrEq time.Time, i Interval) bucket {
var b bucket
kc := i.KeepCount()
if kc == 0 {
panic("keep count 0 is not allowed")
}
if (kc < 0) && kc != RetentionGridKeepCountAll {
panic("negative keep counts are not allowed")
2017-06-21 20:29:20 +02:00
}
b.keepCount = kc
b.olderThanOrEq = olderThanOrEq
b.youngerThan = b.olderThanOrEq.Add(-i.Length())
return b
}
2017-06-21 20:29:20 +02:00
func (b *bucket) Contains(e Entry) bool {
d := e.Date()
olderThan := d.Before(b.olderThanOrEq)
eq := d.Equal(b.olderThanOrEq)
youngerThan := d.After(b.youngerThan)
return (olderThan || eq) && youngerThan
}
2017-06-21 20:29:20 +02:00
func (b *bucket) AddIfContains(e Entry) (added bool) {
added = b.Contains(e)
if added {
b.entries = append(b.entries, e)
}
return
}
2017-06-21 20:29:20 +02:00
func (b *bucket) RemoveYoungerSnapsExceedingKeepCount() (removed []Entry) {
2017-06-21 20:29:20 +02:00
if b.keepCount == RetentionGridKeepCountAll {
return nil
}
removeCount := len(b.entries) - b.keepCount
if removeCount <= 0 {
return nil
2017-06-21 20:29:20 +02:00
}
// sort youngest-to-oldest
sort.SliceStable(b.entries, func(i, j int) bool {
return b.entries[i].Date().After(b.entries[j].Date())
})
return b.entries[:removeCount]
}
func (g Grid) fitEntriesWithNow(now time.Time, entries []Entry) (keep, remove []Entry) {
buckets := make([]bucket, len(g.intervals))
2017-06-21 20:29:20 +02:00
buckets[0] = makeBucketFromInterval(now, g.intervals[0])
for i := 1; i < len(g.intervals); i++ {
buckets[i] = makeBucketFromInterval(buckets[i-1].youngerThan, g.intervals[i])
}
2017-06-21 20:29:20 +02:00
keep = make([]Entry, 0)
remove = make([]Entry, 0)
2017-06-21 20:29:20 +02:00
assignEntriesToBuckets:
for ei := 0; ei < len(entries); ei++ {
e := entries[ei]
// unconditionally keep entries that are in the future
if now.Before(e.Date()) {
keep = append(keep, e)
continue assignEntriesToBuckets
2017-06-21 20:29:20 +02:00
}
// add to matching bucket, if any
for bi := range buckets {
if buckets[bi].AddIfContains(e) {
continue assignEntriesToBuckets
}
2017-06-21 20:29:20 +02:00
}
// unconditionally remove entries older than the oldest bucket
remove = append(remove, e)
2017-06-21 20:29:20 +02:00
}
// now apply the `KeepCount` per bucket
for _, b := range buckets {
destroy := b.RemoveYoungerSnapsExceedingKeepCount()
remove = append(remove, destroy...)
keep = append(keep, b.entries...)
}
2017-06-21 20:29:20 +02:00
return
}