2017-09-10 16:13:05 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-11 15:37:45 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-09-10 16:13:05 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-09-11 15:37:45 +02:00
|
|
|
"github.com/zrepl/zrepl/util"
|
2017-09-10 16:13:05 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2018-02-17 20:48:31 +01:00
|
|
|
"math"
|
2017-09-11 15:37:45 +02:00
|
|
|
"regexp"
|
2017-09-10 16:13:05 +02:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-11 15:37:45 +02:00
|
|
|
"time"
|
2017-09-10 16:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type GridPrunePolicy struct {
|
2017-09-11 15:37:45 +02:00
|
|
|
RetentionGrid *util.RetentionGrid
|
2018-02-17 20:48:31 +01:00
|
|
|
MaxBookmarks int
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
const GridPrunePolicyMaxBookmarksKeepAll = -1
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
type retentionGridAdaptor struct {
|
|
|
|
zfs.FilesystemVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a retentionGridAdaptor) Date() time.Time {
|
|
|
|
return a.Creation
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:37:45 +02:00
|
|
|
func (a retentionGridAdaptor) LessThan(b util.RetentionGridEntry) bool {
|
2017-09-10 16:13:05 +02:00
|
|
|
return a.CreateTXG < b.(retentionGridAdaptor).CreateTXG
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
// Prune filters snapshots with the retention grid.
|
|
|
|
// Bookmarks are deleted such that KeepBookmarks are kept in the end.
|
|
|
|
// The oldest bookmarks are removed first.
|
2017-09-13 23:46:57 +02:00
|
|
|
func (p *GridPrunePolicy) Prune(_ *zfs.DatasetPath, versions []zfs.FilesystemVersion) (keep, remove []zfs.FilesystemVersion, err error) {
|
2018-02-17 20:48:31 +01:00
|
|
|
skeep, sremove := p.pruneSnapshots(versions)
|
|
|
|
keep, remove = p.pruneBookmarks(skeep)
|
|
|
|
remove = append(remove, sremove...)
|
|
|
|
return keep, remove, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GridPrunePolicy) pruneSnapshots(versions []zfs.FilesystemVersion) (keep, remove []zfs.FilesystemVersion) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
// Build adaptors for retention grid
|
2018-02-17 20:48:31 +01:00
|
|
|
keep = []zfs.FilesystemVersion{}
|
|
|
|
adaptors := make([]util.RetentionGridEntry, 0)
|
2017-09-10 16:13:05 +02:00
|
|
|
for fsv := range versions {
|
2018-02-17 20:48:31 +01:00
|
|
|
if versions[fsv].Type != zfs.Snapshot {
|
|
|
|
keep = append(keep, versions[fsv])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
adaptors = append(adaptors, retentionGridAdaptor{versions[fsv]})
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(adaptors, func(i, j int) bool {
|
|
|
|
return adaptors[i].LessThan(adaptors[j])
|
|
|
|
})
|
|
|
|
now := adaptors[len(adaptors)-1].Date()
|
|
|
|
|
|
|
|
// Evaluate retention grid
|
|
|
|
keepa, removea := p.RetentionGrid.FitEntries(now, adaptors)
|
|
|
|
|
|
|
|
// Revert adaptors
|
|
|
|
for i := range keepa {
|
2018-02-17 20:48:31 +01:00
|
|
|
keep = append(keep, keepa[i].(retentionGridAdaptor).FilesystemVersion)
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
remove = make([]zfs.FilesystemVersion, len(removea))
|
|
|
|
for i := range removea {
|
|
|
|
remove[i] = removea[i].(retentionGridAdaptor).FilesystemVersion
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
func (p *GridPrunePolicy) pruneBookmarks(versions []zfs.FilesystemVersion) (keep, remove []zfs.FilesystemVersion) {
|
|
|
|
|
|
|
|
if p.MaxBookmarks == GridPrunePolicyMaxBookmarksKeepAll {
|
|
|
|
return versions, []zfs.FilesystemVersion{}
|
|
|
|
}
|
|
|
|
|
|
|
|
keep = []zfs.FilesystemVersion{}
|
|
|
|
bookmarks := make([]zfs.FilesystemVersion, 0)
|
|
|
|
for fsv := range versions {
|
|
|
|
if versions[fsv].Type != zfs.Bookmark {
|
|
|
|
keep = append(keep, versions[fsv])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bookmarks = append(bookmarks, versions[fsv])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bookmarks) == 0 {
|
|
|
|
return keep, []zfs.FilesystemVersion{}
|
|
|
|
}
|
|
|
|
if len(bookmarks) < p.MaxBookmarks {
|
|
|
|
keep = append(keep, bookmarks...)
|
|
|
|
return keep, []zfs.FilesystemVersion{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: sorting descending by descending by createtxg <=> sorting ascending wrt creation time
|
|
|
|
sort.SliceStable(bookmarks, func(i, j int) bool {
|
|
|
|
return (bookmarks[i].CreateTXG > bookmarks[j].CreateTXG)
|
|
|
|
})
|
|
|
|
|
|
|
|
keep = append(keep, bookmarks[:p.MaxBookmarks]...)
|
|
|
|
remove = bookmarks[p.MaxBookmarks:]
|
|
|
|
|
|
|
|
return keep, remove
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGridPrunePolicy(e map[string]interface{}, willSeeBookmarks bool) (p *GridPrunePolicy, err error) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
const KeepBookmarksAllString = "all"
|
2017-09-10 16:13:05 +02:00
|
|
|
var i struct {
|
2018-02-17 20:48:31 +01:00
|
|
|
Grid string
|
|
|
|
KeepBookmarks string `mapstructure:"keep_bookmarks"`
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{Result: &i, WeaklyTypedInput: true})
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "mapstructure error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = dec.Decode(e); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
err = errors.Wrapf(err, "mapstructure error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
// Parse grid
|
2017-09-10 16:13:05 +02:00
|
|
|
intervals, err := parseRetentionGridIntervalsString(i.Grid)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("cannot parse retention grid: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Assert intervals are of increasing length (not necessarily required, but indicates config mistake)
|
|
|
|
lastDuration := time.Duration(0)
|
|
|
|
for i := range intervals {
|
2017-10-05 20:34:35 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
if intervals[i].Length < lastDuration {
|
2017-10-05 20:34:35 +02:00
|
|
|
// If all intervals before were keep=all, this is ok
|
|
|
|
allPrevKeepCountAll := true
|
|
|
|
for j := i - 1; allPrevKeepCountAll && j >= 0; j-- {
|
|
|
|
allPrevKeepCountAll = intervals[j].KeepCount == util.RetentionGridKeepCountAll
|
|
|
|
}
|
|
|
|
if allPrevKeepCountAll {
|
|
|
|
goto isMonotonicIncrease
|
|
|
|
}
|
|
|
|
err = errors.New("retention grid interval length must be monotonically increasing")
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
2017-10-05 20:34:35 +02:00
|
|
|
isMonotonicIncrease:
|
|
|
|
lastDuration = intervals[i].Length
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
// Parse KeepBookmarks
|
|
|
|
keepBookmarks := 0
|
|
|
|
if i.KeepBookmarks == KeepBookmarksAllString || (i.KeepBookmarks == "" && !willSeeBookmarks) {
|
|
|
|
keepBookmarks = GridPrunePolicyMaxBookmarksKeepAll
|
|
|
|
} else {
|
|
|
|
i, err := strconv.ParseInt(i.KeepBookmarks, 10, 32)
|
|
|
|
if err != nil || i <= 0 || i > math.MaxInt32 {
|
|
|
|
return nil, errors.Errorf("keep_bookmarks must be positive integer or 'all'")
|
|
|
|
}
|
|
|
|
keepBookmarks = int(i)
|
|
|
|
}
|
|
|
|
return &GridPrunePolicy{
|
|
|
|
util.NewRetentionGrid(intervals),
|
|
|
|
keepBookmarks,
|
|
|
|
}, nil
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var retentionStringIntervalRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*x\s*([^\(]+)\s*(\((.*)\))?\s*$`)
|
|
|
|
|
2017-09-11 15:37:45 +02:00
|
|
|
func parseRetentionGridIntervalString(e string) (intervals []util.RetentionInterval, err error) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
comps := retentionStringIntervalRegex.FindStringSubmatch(e)
|
|
|
|
if comps == nil {
|
|
|
|
err = fmt.Errorf("retention string does not match expected format")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
times, err := strconv.Atoi(comps[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if times <= 0 {
|
|
|
|
return nil, fmt.Errorf("contains factor <= 0")
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:12:50 +02:00
|
|
|
duration, err := parsePostitiveDuration(comps[2])
|
2017-09-10 16:13:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keepCount := 1
|
|
|
|
if comps[3] != "" {
|
|
|
|
// Decompose key=value, comma separated
|
|
|
|
// For now, only keep_count is supported
|
|
|
|
re := regexp.MustCompile(`^\s*keep=(.+)\s*$`)
|
|
|
|
res := re.FindStringSubmatch(comps[4])
|
|
|
|
if res == nil || len(res) != 2 {
|
|
|
|
err = fmt.Errorf("interval parameter contains unknown parameters")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if res[1] == "all" {
|
2017-09-11 15:37:45 +02:00
|
|
|
keepCount = util.RetentionGridKeepCountAll
|
2017-09-10 16:13:05 +02:00
|
|
|
} else {
|
|
|
|
keepCount, err = strconv.Atoi(res[1])
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("cannot parse keep_count value")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:37:45 +02:00
|
|
|
intervals = make([]util.RetentionInterval, times)
|
2017-09-10 16:13:05 +02:00
|
|
|
for i := range intervals {
|
2017-09-11 15:37:45 +02:00
|
|
|
intervals[i] = util.RetentionInterval{
|
2017-09-10 16:13:05 +02:00
|
|
|
Length: duration,
|
|
|
|
KeepCount: keepCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:37:45 +02:00
|
|
|
func parseRetentionGridIntervalsString(s string) (intervals []util.RetentionInterval, err error) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
ges := strings.Split(s, "|")
|
2017-09-11 15:37:45 +02:00
|
|
|
intervals = make([]util.RetentionInterval, 0, 7*len(ges))
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
for intervalIdx, e := range ges {
|
|
|
|
parsed, err := parseRetentionGridIntervalString(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse interval %d of %d: %s: %s", intervalIdx+1, len(ges), err, strings.TrimSpace(e))
|
|
|
|
}
|
|
|
|
intervals = append(intervals, parsed...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-09-11 15:37:45 +02:00
|
|
|
}
|