2017-09-13 23:27:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntervalAutosnap struct {
|
|
|
|
DatasetFilter zfs.DatasetFilter
|
|
|
|
Prefix string
|
|
|
|
SnapshotInterval time.Duration
|
|
|
|
|
|
|
|
log Logger
|
|
|
|
snaptimes []snapTime
|
|
|
|
}
|
|
|
|
|
|
|
|
type snapTime struct {
|
|
|
|
ds *zfs.DatasetPath
|
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
a.log = ctx.Value(contextKeyLog).(Logger)
|
|
|
|
|
|
|
|
const LOG_TIME_FMT string = time.ANSIC
|
|
|
|
|
|
|
|
ds, err := zfs.ZFSListMapping(a.DatasetFilter)
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.WithError(err).Error("cannot list datasets")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ds) == 0 {
|
2017-10-05 21:22:17 +02:00
|
|
|
a.log.Warn("no filesystem matching filesystem filter")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.snaptimes = make([]snapTime, len(ds))
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.Debug("examine filesystem state")
|
2017-09-13 23:27:18 +02:00
|
|
|
for i, d := range ds {
|
|
|
|
|
2017-09-23 11:24:36 +02:00
|
|
|
l := a.log.WithField(logFSField, d.ToString())
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
fsvs, err := zfs.ZFSListFilesystemVersions(d, &PrefixSnapshotFilter{a.Prefix})
|
|
|
|
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
|
|
|
a.snaptimes[i] = snapTime{d, now}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
a.snaptimes[i] = snapTime{d, next}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(a.snaptimes, func(i, j int) bool {
|
|
|
|
return a.snaptimes[i].time.Before(a.snaptimes[j].time)
|
|
|
|
})
|
|
|
|
|
|
|
|
syncPoint := a.snaptimes[0]
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.WithField("sync_point", syncPoint.time.Format(LOG_TIME_FMT)).
|
|
|
|
Info("wait for sync point")
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.WithError(ctx.Err()).Info("context done")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-time.After(syncPoint.time.Sub(now)):
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.Debug("snapshot all filesystems to enable further snaps in lockstep")
|
2017-09-16 21:12:26 +02:00
|
|
|
a.doSnapshots(didSnaps)
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(a.SnapshotInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ticker.Stop()
|
2017-09-22 14:13:58 +02:00
|
|
|
a.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
|
|
|
|
|
|
|
// fetch new dataset list in case user added new dataset
|
|
|
|
ds, err := zfs.ZFSListMapping(a.DatasetFilter)
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.WithError(err).Error("cannot list datasets")
|
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-09-23 11:24:36 +02:00
|
|
|
a.log.WithField(logFSField, d.ToString()).
|
2017-09-22 14:13:58 +02:00
|
|
|
WithField("snapname", snapname).
|
|
|
|
Info("create snapshot")
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
err := zfs.ZFSSnapshot(d, snapname, false)
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
a.log.WithError(err).Error("cannot create snapshot")
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
select {
|
|
|
|
case didSnaps <- struct{}{}:
|
|
|
|
default:
|
2017-09-23 17:52:29 +02:00
|
|
|
a.log.Error("warning: callback channel is full, discarding")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|