2017-09-10 16:13:05 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
"context"
|
2017-09-10 16:13:05 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
2017-09-16 21:12:26 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"sync"
|
2018-08-22 00:19:03 +02:00
|
|
|
"github.com/zrepl/zrepl/replication"
|
2018-08-22 00:43:58 +02:00
|
|
|
"github.com/zrepl/zrepl/cmd/endpoint"
|
2017-09-10 16:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LocalJob struct {
|
|
|
|
Name string
|
|
|
|
Mapping *DatasetMapFilter
|
2017-09-16 20:24:46 +02:00
|
|
|
SnapshotPrefix string
|
2017-09-10 16:13:05 +02:00
|
|
|
Interval time.Duration
|
2018-08-22 10:11:14 +02:00
|
|
|
InitialReplPolicy replication.InitialReplPolicy
|
2017-09-10 16:13:05 +02:00
|
|
|
PruneLHS PrunePolicy
|
|
|
|
PruneRHS PrunePolicy
|
2017-09-11 15:45:10 +02:00
|
|
|
Debug JobDebugSettings
|
2017-12-26 19:36:27 +01:00
|
|
|
snapperTask *Task
|
2017-12-26 22:48:42 +01:00
|
|
|
mainTask *Task
|
|
|
|
handlerTask *Task
|
2017-12-26 19:36:27 +01:00
|
|
|
pruneRHSTask *Task
|
|
|
|
pruneLHSTask *Task
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
func parseLocalJob(c JobParsingContext, name string, i map[string]interface{}) (j *LocalJob, err error) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
var asMap struct {
|
2017-09-11 15:45:10 +02:00
|
|
|
Mapping map[string]string
|
|
|
|
SnapshotPrefix string `mapstructure:"snapshot_prefix"`
|
|
|
|
Interval string
|
|
|
|
InitialReplPolicy string `mapstructure:"initial_repl_policy"`
|
|
|
|
PruneLHS map[string]interface{} `mapstructure:"prune_lhs"`
|
|
|
|
PruneRHS map[string]interface{} `mapstructure:"prune_rhs"`
|
|
|
|
Debug map[string]interface{}
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = mapstructure.Decode(i, &asMap); err != nil {
|
|
|
|
err = errors.Wrap(err, "mapstructure error")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &LocalJob{Name: name}
|
|
|
|
|
|
|
|
if j.Mapping, err = parseDatasetMapFilter(asMap.Mapping, false); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-16 20:24:46 +02:00
|
|
|
if j.SnapshotPrefix, err = parseSnapshotPrefix(asMap.SnapshotPrefix); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:12:50 +02:00
|
|
|
if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
err = errors.Wrap(err, "cannot parse interval")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-22 10:11:14 +02:00
|
|
|
if j.InitialReplPolicy, err = parseInitialReplPolicy(asMap.InitialReplPolicy, replication.DEFAULT_INITIAL_REPL_POLICY); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:48:31 +01:00
|
|
|
if j.PruneLHS, err = parsePrunePolicy(asMap.PruneLHS, true); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
err = errors.Wrap(err, "cannot parse 'prune_lhs'")
|
|
|
|
return
|
|
|
|
}
|
2018-02-17 20:48:31 +01:00
|
|
|
if j.PruneRHS, err = parsePrunePolicy(asMap.PruneRHS, false); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
err = errors.Wrap(err, "cannot parse 'prune_rhs'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:45:10 +02:00
|
|
|
if err = mapstructure.Decode(asMap.Debug, &j.Debug); err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'debug'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *LocalJob) JobName() string {
|
|
|
|
return j.Name
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:22:55 +02:00
|
|
|
func (j *LocalJob) JobType() JobType { return JobTypeLocal }
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func (j *LocalJob) JobStart(ctx context.Context) {
|
|
|
|
|
2017-12-26 22:48:42 +01:00
|
|
|
rootLog := ctx.Value(contextKeyLog).(Logger)
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2018-04-05 22:18:22 +02:00
|
|
|
j.snapperTask = NewTask("snapshot", j, rootLog)
|
|
|
|
j.mainTask = NewTask("main", j, rootLog)
|
|
|
|
j.handlerTask = NewTask("handler", j, rootLog)
|
|
|
|
j.pruneRHSTask = NewTask("prune_rhs", j, rootLog)
|
|
|
|
j.pruneLHSTask = NewTask("prune_lhs", j, rootLog)
|
2017-12-26 19:36:27 +01:00
|
|
|
|
2017-09-16 20:24:46 +02:00
|
|
|
// Allow access to any dataset since we control what mapping
|
|
|
|
// is passed to the pull routine.
|
|
|
|
// All local datasets will be passed to its Map() function,
|
|
|
|
// but only those for which a mapping exists will actually be pulled.
|
|
|
|
// We can pay this small performance penalty for now.
|
2018-06-20 20:20:37 +02:00
|
|
|
wildcardMapFilter := NewDatasetMapFilter(1, false)
|
|
|
|
wildcardMapFilter.Add("<", "<")
|
2018-08-22 00:52:46 +02:00
|
|
|
sender := &endpoint.Sender{wildcardMapFilter, NewPrefixFilter(j.SnapshotPrefix)}
|
2017-09-16 20:24:46 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
receiver, err := endpoint.NewReceiver(j.Mapping, NewPrefixFilter(j.SnapshotPrefix))
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
rootLog.WithError(err).Error("unexpected error setting up local handler")
|
|
|
|
}
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
snapper := IntervalAutosnap{
|
2017-12-26 19:36:27 +01:00
|
|
|
task: j.snapperTask,
|
2017-09-16 21:12:26 +02:00
|
|
|
DatasetFilter: j.Mapping.AsFilter(),
|
|
|
|
Prefix: j.SnapshotPrefix,
|
|
|
|
SnapshotInterval: j.Interval,
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:36:27 +01:00
|
|
|
plhs, err := j.Pruner(j.pruneLHSTask, PrunePolicySideLeft, false)
|
2017-09-16 21:12:26 +02:00
|
|
|
if err != nil {
|
2017-12-26 22:48:42 +01:00
|
|
|
rootLog.WithError(err).Error("error creating lhs pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
2017-12-26 19:36:27 +01:00
|
|
|
prhs, err := j.Pruner(j.pruneRHSTask, PrunePolicySideRight, false)
|
2017-09-13 23:27:18 +02:00
|
|
|
if err != nil {
|
2017-12-26 22:48:42 +01:00
|
|
|
rootLog.WithError(err).Error("error creating rhs pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
didSnaps := make(chan struct{})
|
2017-12-26 22:48:42 +01:00
|
|
|
go snapper.Run(ctx, didSnaps)
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Log().WithError(ctx.Err()).Info("context")
|
2017-09-16 21:12:26 +02:00
|
|
|
break outer
|
|
|
|
case <-didSnaps:
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Log().Debug("finished taking snapshots")
|
|
|
|
j.mainTask.Log().Info("starting replication procedure")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Log().Debug("replicating from lhs to rhs")
|
|
|
|
j.mainTask.Enter("replicate")
|
2018-06-20 20:20:37 +02:00
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
rep := replication.NewReplication()
|
2018-08-22 00:10:09 +02:00
|
|
|
rep.Drive(ctx, sender, receiver)
|
2018-06-20 20:20:37 +02:00
|
|
|
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Finish()
|
|
|
|
|
|
|
|
// use a ctx as soon as Pull gains ctx support
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
default:
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Log().Info("pruning lhs")
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2017-12-26 22:48:42 +01:00
|
|
|
plhs.Run(ctx)
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Log().Info("pruning rhs")
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2017-12-26 22:48:42 +01:00
|
|
|
prhs.Run(ctx)
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:34:41 +01:00
|
|
|
func (j *LocalJob) JobStatus(ctxt context.Context) (*JobStatus, error) {
|
2017-12-26 19:36:27 +01:00
|
|
|
return &JobStatus{Tasks: []*TaskStatus{
|
|
|
|
j.snapperTask.Status(),
|
|
|
|
j.pruneLHSTask.Status(),
|
|
|
|
j.pruneRHSTask.Status(),
|
2017-12-26 22:48:42 +01:00
|
|
|
j.mainTask.Status(),
|
2017-12-26 19:36:27 +01:00
|
|
|
}}, nil
|
2017-12-24 15:34:41 +01:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:36:27 +01:00
|
|
|
func (j *LocalJob) Pruner(task *Task, side PrunePolicySide, dryRun bool) (p Pruner, err error) {
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
var dsfilter zfs.DatasetFilter
|
|
|
|
var pp PrunePolicy
|
|
|
|
switch side {
|
|
|
|
case PrunePolicySideLeft:
|
|
|
|
pp = j.PruneLHS
|
|
|
|
dsfilter = j.Mapping.AsFilter()
|
|
|
|
case PrunePolicySideRight:
|
|
|
|
pp = j.PruneRHS
|
|
|
|
dsfilter, err = j.Mapping.InvertedFilter()
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot invert mapping for prune_rhs")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("must be either left or right side")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p = Pruner{
|
2017-12-26 19:36:27 +01:00
|
|
|
task,
|
2017-09-16 21:12:26 +02:00
|
|
|
time.Now(),
|
|
|
|
dryRun,
|
|
|
|
dsfilter,
|
|
|
|
j.SnapshotPrefix,
|
|
|
|
pp,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|