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-11 15:45:10 +02:00
|
|
|
"github.com/zrepl/zrepl/rpc"
|
2017-09-16 21:12:26 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"sync"
|
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
|
|
|
|
InitialReplPolicy InitialReplPolicy
|
|
|
|
PruneLHS PrunePolicy
|
|
|
|
PruneRHS PrunePolicy
|
2017-09-11 15:45:10 +02:00
|
|
|
Debug JobDebugSettings
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if j.InitialReplPolicy, err = parseInitialReplPolicy(asMap.InitialReplPolicy, DEFAULT_INITIAL_REPL_POLICY); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if j.PruneLHS, err = parsePrunePolicy(asMap.PruneLHS); err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'prune_lhs'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if j.PruneRHS, err = parsePrunePolicy(asMap.PruneRHS); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func (j *LocalJob) JobStart(ctx context.Context) {
|
|
|
|
|
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
2017-09-23 17:52:29 +02:00
|
|
|
defer log.Info("exiting")
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
local := rpc.NewLocalRPC()
|
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.
|
2017-11-12 23:02:23 +01:00
|
|
|
handler := NewHandler(log.WithField(logTaskField, "handler"), localPullACL{}, NewPrefixFilter(j.SnapshotPrefix))
|
2017-09-16 20:24:46 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
registerEndpoints(local, handler)
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
snapper := IntervalAutosnap{
|
|
|
|
DatasetFilter: j.Mapping.AsFilter(),
|
|
|
|
Prefix: j.SnapshotPrefix,
|
|
|
|
SnapshotInterval: j.Interval,
|
|
|
|
}
|
|
|
|
|
|
|
|
plhs, err := j.Pruner(PrunePolicySideLeft, false)
|
|
|
|
if err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error creating lhs pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
prhs, err := j.Pruner(PrunePolicySideRight, false)
|
2017-09-13 23:27:18 +02:00
|
|
|
if err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error creating rhs pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
makeCtx := func(parent context.Context, taskName string) (ctx context.Context) {
|
2017-09-23 11:24:36 +02:00
|
|
|
return context.WithValue(parent, contextKeyLog, log.WithField(logTaskField, taskName))
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
var snapCtx, plCtx, prCtx, pullCtx context.Context
|
|
|
|
snapCtx = makeCtx(ctx, "autosnap")
|
|
|
|
plCtx = makeCtx(ctx, "prune_lhs")
|
|
|
|
prCtx = makeCtx(ctx, "prune_rhs")
|
|
|
|
pullCtx = makeCtx(ctx, "repl")
|
|
|
|
|
|
|
|
didSnaps := make(chan struct{})
|
|
|
|
go snapper.Run(snapCtx, didSnaps)
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
case <-didSnaps:
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Debug("finished taking snapshots")
|
|
|
|
log.Info("starting replication procedure")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
log := pullCtx.Value(contextKeyLog).(Logger)
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Debug("replicating from lhs to rhs")
|
2017-12-24 15:05:07 +01:00
|
|
|
puller := Puller{local, log, j.Mapping, j.InitialReplPolicy}
|
|
|
|
if err := puller.doPull(); err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error replicating lhs to rhs")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
// use a ctx as soon as doPull gains ctx support
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("pruning lhs")
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
plhs.Run(plCtx)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("pruning rhs")
|
2017-09-16 21:12:26 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
prhs.Run(prCtx)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-09-16 21:12:26 +02:00
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(ctx.Err()).Info("context")
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *LocalJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) {
|
|
|
|
|
|
|
|
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{
|
|
|
|
time.Now(),
|
|
|
|
dryRun,
|
|
|
|
dsfilter,
|
|
|
|
j.SnapshotPrefix,
|
|
|
|
pp,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|