2017-09-13 23:27:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntervalAutosnap struct {
|
2017-12-26 19:36:27 +01:00
|
|
|
task *Task
|
2017-09-13 23:27:18 +02:00
|
|
|
DatasetFilter zfs.DatasetFilter
|
|
|
|
Prefix string
|
|
|
|
SnapshotInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
func (a *IntervalAutosnap) filterFilesystems() (fss []*zfs.DatasetPath, stop bool) {
|
|
|
|
a.task.Enter("filter_filesystems")
|
|
|
|
defer a.task.Finish()
|
|
|
|
fss, err := zfs.ZFSListMapping(a.DatasetFilter)
|
|
|
|
stop = err != nil || len(fss) == 0
|
2017-09-13 23:27:18 +02:00
|
|
|
if err != nil {
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().WithError(err).Error("cannot list datasets")
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-12-26 19:46:44 +01:00
|
|
|
if len(fss) == 0 {
|
|
|
|
a.task.Log().Warn("no filesystem matching filesystem filter")
|
|
|
|
}
|
|
|
|
return fss, stop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IntervalAutosnap) findSyncPoint(fss []*zfs.DatasetPath) (syncPoint time.Time, err error) {
|
|
|
|
a.task.Enter("find_sync_point")
|
|
|
|
defer a.task.Finish()
|
|
|
|
type snapTime struct {
|
|
|
|
ds *zfs.DatasetPath
|
|
|
|
time time.Time
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
snaptimes := make([]snapTime, 0, len(fss))
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().Debug("examine filesystem state")
|
|
|
|
for _, d := range fss {
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
l := a.task.Log().WithField(logFSField, d.ToString())
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-11-12 23:02:23 +01:00
|
|
|
fsvs, err := zfs.ZFSListFilesystemVersions(d, NewTypedPrefixFilter(a.Prefix, zfs.Snapshot))
|
2017-09-13 23:27:18 +02:00
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
l.WithError(err).Error("cannot list filesystem versions")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(fsvs) <= 0 {
|
2017-09-22 14:13:58 +02:00
|
|
|
l.WithField("prefix", a.Prefix).Info("no filesystem versions with prefix")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort versions by creation
|
|
|
|
sort.SliceStable(fsvs, func(i, j int) bool {
|
|
|
|
return fsvs[i].CreateTXG < fsvs[j].CreateTXG
|
|
|
|
})
|
|
|
|
|
|
|
|
latest := fsvs[len(fsvs)-1]
|
2017-09-22 14:13:58 +02:00
|
|
|
l.WithField("creation", latest.Creation).
|
|
|
|
Debug("found latest snapshot")
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
since := now.Sub(latest.Creation)
|
|
|
|
if since < 0 {
|
2017-09-22 14:13:58 +02:00
|
|
|
l.WithField("snapshot", latest.Name).
|
|
|
|
WithField("creation", latest.Creation).
|
|
|
|
Error("snapshot is from the future")
|
2017-09-13 23:27:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
next := now
|
|
|
|
if since < a.SnapshotInterval {
|
|
|
|
next = latest.Creation.Add(a.SnapshotInterval)
|
|
|
|
}
|
2017-12-26 19:46:44 +01:00
|
|
|
snaptimes = append(snaptimes, snapTime{d, next})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(snaptimes) == 0 {
|
|
|
|
snaptimes = append(snaptimes, snapTime{nil, now})
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
sort.Slice(snaptimes, func(i, j int) bool {
|
|
|
|
return snaptimes[i].time.Before(snaptimes[j].time)
|
2017-09-13 23:27:18 +02:00
|
|
|
})
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
return snaptimes[0].time, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IntervalAutosnap) waitForSyncPoint(ctx context.Context, syncPoint time.Time) {
|
|
|
|
a.task.Enter("wait_sync_point")
|
|
|
|
defer a.task.Finish()
|
|
|
|
|
|
|
|
const LOG_TIME_FMT string = time.ANSIC
|
|
|
|
|
|
|
|
a.task.Log().WithField("sync_point", syncPoint.Format(LOG_TIME_FMT)).
|
2017-09-22 14:13:58 +02:00
|
|
|
Info("wait for sync point")
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().WithError(ctx.Err()).Info("context done")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
2017-12-26 19:46:44 +01:00
|
|
|
case <-time.After(syncPoint.Sub(time.Now())):
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
func (a *IntervalAutosnap) syncUpRun(ctx context.Context, didSnaps chan struct{}) (stop bool) {
|
|
|
|
a.task.Enter("sync_up")
|
|
|
|
defer a.task.Finish()
|
|
|
|
|
|
|
|
fss, stop := a.filterFilesystems()
|
|
|
|
if stop {
|
|
|
|
return true
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
syncPoint, err := a.findSyncPoint(fss)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
a.waitForSyncPoint(ctx, syncPoint)
|
|
|
|
|
|
|
|
a.task.Log().Debug("snapshot all filesystems to enable further snaps in lockstep")
|
|
|
|
a.doSnapshots(didSnaps)
|
|
|
|
return false
|
|
|
|
}
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
|
|
|
|
|
|
|
if a.syncUpRun(ctx, didSnaps) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// task drops back to idle here
|
|
|
|
|
|
|
|
a.task.Log().Debug("setting up ticker in SnapshotInterval")
|
|
|
|
ticker := time.NewTicker(a.SnapshotInterval)
|
2017-09-13 23:27:18 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ticker.Stop()
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().WithError(ctx.Err()).Info("context done")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2017-09-16 21:12:26 +02:00
|
|
|
a.doSnapshots(didSnaps)
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
func (a *IntervalAutosnap) doSnapshots(didSnaps chan struct{}) {
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Enter("do_snapshots")
|
|
|
|
defer a.task.Finish()
|
|
|
|
|
|
|
|
// don't cache the result from previous run in case the user added
|
|
|
|
// a new dataset in the meantime
|
|
|
|
ds, stop := a.filterFilesystems()
|
|
|
|
if stop {
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO channel programs -> allow a little jitter?
|
|
|
|
for _, d := range ds {
|
|
|
|
suffix := time.Now().In(time.UTC).Format("20060102_150405_000")
|
|
|
|
snapname := fmt.Sprintf("%s%s", a.Prefix, suffix)
|
|
|
|
|
2017-12-26 19:46:44 +01:00
|
|
|
l := a.task.Log().WithField(logFSField, d.ToString()).
|
2017-11-12 23:05:18 +01:00
|
|
|
WithField("snapname", snapname)
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2017-11-12 23:05:18 +01:00
|
|
|
l.Info("create snapshot")
|
2017-09-13 23:27:18 +02:00
|
|
|
err := zfs.ZFSSnapshot(d, snapname, false)
|
|
|
|
if err != nil {
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().WithError(err).Error("cannot create snapshot")
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-11-12 23:05:18 +01:00
|
|
|
|
|
|
|
l.Info("create corresponding bookmark")
|
|
|
|
err = zfs.ZFSBookmark(d, snapname, snapname)
|
|
|
|
if err != nil {
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().WithError(err).Error("cannot create bookmark")
|
2017-11-12 23:05:18 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
select {
|
|
|
|
case didSnaps <- struct{}{}:
|
|
|
|
default:
|
2017-12-26 19:46:44 +01:00
|
|
|
a.task.Log().Error("warning: callback channel is full, discarding")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|