2018-11-20 19:30:15 +01:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-17 21:06:19 +01:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2018-11-20 19:30:15 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"github.com/zrepl/zrepl/daemon/filters"
|
|
|
|
"github.com/zrepl/zrepl/daemon/job/wakeup"
|
|
|
|
"github.com/zrepl/zrepl/daemon/logging"
|
|
|
|
"github.com/zrepl/zrepl/daemon/pruner"
|
|
|
|
"github.com/zrepl/zrepl/daemon/snapper"
|
|
|
|
"github.com/zrepl/zrepl/endpoint"
|
2019-03-17 21:06:19 +01:00
|
|
|
"github.com/zrepl/zrepl/replication/logic/pdu"
|
2018-11-20 19:30:15 +01:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
)
|
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
type SnapJob struct {
|
2018-11-21 14:02:21 +01:00
|
|
|
name string
|
|
|
|
fsfilter zfs.DatasetFilter
|
|
|
|
snapper *snapper.PeriodicOrManual
|
2018-11-20 19:30:15 +01:00
|
|
|
|
2019-03-17 21:18:25 +01:00
|
|
|
prunerFactory *pruner.LocalPrunerFactory
|
2018-11-20 19:30:15 +01:00
|
|
|
|
2019-03-17 21:07:42 +01:00
|
|
|
promPruneSecs *prometheus.HistogramVec // labels: prune_side
|
2018-11-20 19:30:15 +01:00
|
|
|
|
|
|
|
pruner *pruner.Pruner
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
func (j *SnapJob) Name() string { return j.name }
|
2018-11-20 19:30:15 +01:00
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
func (j *SnapJob) Type() Type { return TypeSnap }
|
2018-11-20 19:30:15 +01:00
|
|
|
|
2018-11-21 14:37:03 +01:00
|
|
|
func snapJobFromConfig(g *config.Global, in *config.SnapJob) (j *SnapJob, err error) {
|
2018-11-21 01:54:56 +01:00
|
|
|
j = &SnapJob{}
|
2018-11-21 14:02:21 +01:00
|
|
|
fsf, err := filters.DatasetMapFilterFromConfig(in.Filesystems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannnot build filesystem filter")
|
|
|
|
}
|
|
|
|
j.fsfilter = fsf
|
|
|
|
|
|
|
|
if j.snapper, err = snapper.FromConfig(g, fsf, in.Snapshotting); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot build snapper")
|
|
|
|
}
|
2018-11-20 19:30:15 +01:00
|
|
|
j.name = in.Name
|
|
|
|
j.promPruneSecs = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Namespace: "zrepl",
|
|
|
|
Subsystem: "pruning",
|
|
|
|
Name: "time",
|
|
|
|
Help: "seconds spent in pruner",
|
2018-11-21 14:02:21 +01:00
|
|
|
ConstLabels: prometheus.Labels{"zrepl_job": j.name},
|
2018-11-21 04:26:03 +01:00
|
|
|
}, []string{"prune_side"})
|
2019-03-17 21:18:25 +01:00
|
|
|
j.prunerFactory, err = pruner.NewLocalPrunerFactory(in.Pruning, j.promPruneSecs)
|
2018-11-20 19:30:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot build snapjob pruning rules")
|
|
|
|
}
|
|
|
|
return j, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
func (j *SnapJob) RegisterMetrics(registerer prometheus.Registerer) {
|
2018-11-20 19:30:15 +01:00
|
|
|
registerer.MustRegister(j.promPruneSecs)
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:08:39 +01:00
|
|
|
type SnapJobStatus struct {
|
|
|
|
Pruning *pruner.Report
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
func (j *SnapJob) Status() *Status {
|
2018-11-21 02:08:39 +01:00
|
|
|
s := &SnapJobStatus{}
|
2018-11-21 01:54:56 +01:00
|
|
|
t := j.Type()
|
2018-11-21 02:42:13 +01:00
|
|
|
if j.pruner != nil {
|
|
|
|
s.Pruning = j.pruner.Report()
|
2018-11-20 19:30:15 +01:00
|
|
|
}
|
|
|
|
return &Status{Type: t, JobSpecific: s}
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:54:56 +01:00
|
|
|
func (j *SnapJob) Run(ctx context.Context) {
|
2018-11-20 19:30:15 +01:00
|
|
|
log := GetLogger(ctx)
|
|
|
|
ctx = logging.WithSubsystemLoggers(ctx, log)
|
|
|
|
|
|
|
|
defer log.Info("job exiting")
|
|
|
|
|
|
|
|
periodicDone := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2018-11-21 21:44:48 +01:00
|
|
|
go j.snapper.Run(ctx, periodicDone)
|
2018-11-20 19:30:15 +01:00
|
|
|
|
|
|
|
invocationCount := 0
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
log.Info("wait for wakeups")
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.WithError(ctx.Err()).Info("context")
|
|
|
|
break outer
|
|
|
|
|
|
|
|
case <-wakeup.Wait(ctx):
|
|
|
|
case <-periodicDone:
|
|
|
|
}
|
|
|
|
invocationCount++
|
|
|
|
invLog := log.WithField("invocation", invocationCount)
|
2018-11-21 02:42:13 +01:00
|
|
|
j.doPrune(WithLogger(ctx, invLog))
|
2018-11-20 19:30:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:06:19 +01:00
|
|
|
// Adaptor that implements pruner.History around a pruner.Target.
|
|
|
|
// The ReplicationCursor method is Get-op only and always returns
|
|
|
|
// the filesystem's most recent version's GUID.
|
|
|
|
//
|
|
|
|
// TODO:
|
|
|
|
// This is a work-around for the current package daemon/pruner
|
|
|
|
// and package pruning.Snapshot limitation: they require the
|
|
|
|
// `Replicated` getter method be present, but obviously,
|
|
|
|
// a local job like SnapJob can't deliver on that.
|
|
|
|
// But the pruner.Pruner gives up on an FS if no replication
|
|
|
|
// cursor is present, which is why this pruner returns the
|
|
|
|
// most recent filesystem version.
|
|
|
|
type alwaysUpToDateReplicationCursorHistory struct {
|
2019-03-17 21:18:25 +01:00
|
|
|
// the Target passed as Target to BuildLocalPruner
|
2019-03-17 21:06:19 +01:00
|
|
|
target pruner.Target
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ pruner.History = (*alwaysUpToDateReplicationCursorHistory)(nil)
|
|
|
|
|
|
|
|
func (h alwaysUpToDateReplicationCursorHistory) ReplicationCursor(ctx context.Context, req *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error) {
|
|
|
|
if req.GetGet() == nil {
|
|
|
|
return nil, fmt.Errorf("unsupported ReplicationCursor request: SnapJob only supports GETting a (faked) cursor")
|
|
|
|
}
|
|
|
|
fsvReq := &pdu.ListFilesystemVersionsReq{
|
|
|
|
Filesystem: req.GetFilesystem(),
|
|
|
|
}
|
|
|
|
res, err := h.target.ListFilesystemVersions(ctx, fsvReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fsvs := res.GetVersions()
|
|
|
|
if len(fsvs) <= 0 {
|
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Notexist{Notexist: true}}, nil
|
|
|
|
}
|
|
|
|
// always return must recent version
|
|
|
|
sort.Slice(fsvs, func(i, j int) bool {
|
|
|
|
return fsvs[i].CreateTXG < fsvs[j].CreateTXG
|
|
|
|
})
|
|
|
|
mostRecent := fsvs[len(fsvs)-1]
|
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Guid{Guid: mostRecent.GetGuid()}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h alwaysUpToDateReplicationCursorHistory) ListFilesystems(ctx context.Context, req *pdu.ListFilesystemReq) (*pdu.ListFilesystemRes, error) {
|
|
|
|
return h.target.ListFilesystems(ctx, req)
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:42:13 +01:00
|
|
|
func (j *SnapJob) doPrune(ctx context.Context) {
|
2018-11-20 19:30:15 +01:00
|
|
|
log := GetLogger(ctx)
|
|
|
|
ctx = logging.WithSubsystemLoggers(ctx, log)
|
2018-11-21 14:35:40 +01:00
|
|
|
sender := endpoint.NewSender(j.fsfilter)
|
2019-03-17 21:18:25 +01:00
|
|
|
j.pruner = j.prunerFactory.BuildLocalPruner(ctx, sender, alwaysUpToDateReplicationCursorHistory{sender})
|
2018-11-21 02:42:13 +01:00
|
|
|
log.Info("start pruning")
|
|
|
|
j.pruner.Prune()
|
|
|
|
log.Info("finished pruning")
|
2018-11-20 19:30:15 +01:00
|
|
|
}
|