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"
|
|
|
|
"github.com/zrepl/zrepl/util"
|
2017-09-10 16:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type PullJob struct {
|
2017-09-13 23:27:18 +02:00
|
|
|
Name string
|
|
|
|
Connect RWCConnecter
|
|
|
|
Interval time.Duration
|
|
|
|
Mapping *DatasetMapFilter
|
|
|
|
// constructed from mapping during parsing
|
|
|
|
pruneFilter *DatasetMapFilter
|
|
|
|
SnapshotPrefix string
|
2017-09-10 16:13:05 +02:00
|
|
|
InitialReplPolicy InitialReplPolicy
|
|
|
|
Prune PrunePolicy
|
2017-09-11 15:45:10 +02:00
|
|
|
Debug JobDebugSettings
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parsePullJob(name string, i map[string]interface{}) (j *PullJob, err error) {
|
|
|
|
|
|
|
|
var asMap struct {
|
|
|
|
Connect map[string]interface{}
|
2017-09-13 23:27:18 +02:00
|
|
|
Interval string
|
2017-09-10 16:13:05 +02:00
|
|
|
Mapping map[string]string
|
|
|
|
InitialReplPolicy string `mapstructure:"initial_repl_policy"`
|
|
|
|
Prune map[string]interface{}
|
|
|
|
SnapshotPrefix string `mapstructure:"snapshot_prefix"`
|
2017-09-11 15:45:10 +02:00
|
|
|
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 = &PullJob{Name: name}
|
|
|
|
|
|
|
|
j.Connect, err = parseSSHStdinserverConnecter(asMap.Connect)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'connect'")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'interval'")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
j.Mapping, err = parseDatasetMapFilter(asMap.Mapping, false)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'mapping'")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
if j.pruneFilter, err = j.Mapping.InvertedFilter(); err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot automatically invert 'mapping' for prune job")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
j.InitialReplPolicy, err = parseInitialReplPolicy(asMap.InitialReplPolicy, DEFAULT_INITIAL_REPL_POLICY)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse 'initial_repl_policy'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
if j.SnapshotPrefix, err = parseSnapshotPrefix(asMap.SnapshotPrefix); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if j.Prune, err = parsePrunePolicy(asMap.Prune); err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot parse prune policy")
|
|
|
|
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 *PullJob) JobName() string {
|
|
|
|
return j.Name
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func (j *PullJob) JobStart(ctx context.Context) {
|
2017-09-11 15:45:10 +02:00
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
|
|
|
defer log.Printf("exiting")
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
ticker := time.NewTicker(j.Interval)
|
|
|
|
|
|
|
|
start:
|
|
|
|
|
|
|
|
log.Printf("connecting")
|
2017-09-11 15:45:10 +02:00
|
|
|
rwc, err := j.Connect.Connect()
|
2017-09-10 16:13:05 +02:00
|
|
|
if err != nil {
|
2017-09-13 23:27:18 +02:00
|
|
|
log.Printf("error connecting: %s", err)
|
|
|
|
return
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
2017-09-11 15:45:10 +02:00
|
|
|
|
|
|
|
rwc, err = util.NewReadWriteCloserLogger(rwc, j.Debug.Conn.ReadDump, j.Debug.Conn.WriteDump)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := rpc.NewClient(rwc)
|
|
|
|
if j.Debug.RPC.Log {
|
|
|
|
client.SetLogger(log, true)
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
log.Printf("starting pull")
|
|
|
|
|
|
|
|
pullLog := util.NewPrefixLogger(log, "pull")
|
|
|
|
err = doPull(PullContext{client, pullLog, j.Mapping, j.InitialReplPolicy})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error doing pull: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeRPCWithTimeout(log, client, time.Second*10, "")
|
|
|
|
|
|
|
|
log.Printf("starting prune")
|
|
|
|
prunectx := context.WithValue(ctx, contextKeyLog, util.NewPrefixLogger(log, "prune"))
|
2017-09-16 21:12:26 +02:00
|
|
|
pruner, err := j.Pruner(PrunePolicySideDefault, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error creating pruner: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
pruner.Run(prunectx)
|
|
|
|
log.Printf("finish prune")
|
|
|
|
|
|
|
|
log.Printf("wait for next interval")
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("context: %s", ctx.Err())
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
goto start
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
func (j *PullJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) {
|
|
|
|
p = Pruner{
|
|
|
|
time.Now(),
|
|
|
|
dryRun,
|
|
|
|
j.pruneFilter,
|
|
|
|
j.SnapshotPrefix,
|
|
|
|
j.Prune,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func (j *PullJob) doRun(ctx context.Context) {
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|