2017-09-10 16:13:05 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-09-13 23:27:18 +02:00
|
|
|
"context"
|
2017-10-05 21:19:42 +02:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
mapstructure "github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
2017-09-11 13:50:35 +02:00
|
|
|
"github.com/zrepl/zrepl/rpc"
|
2017-09-11 15:45:10 +02:00
|
|
|
"github.com/zrepl/zrepl/util"
|
2017-09-10 16:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SourceJob struct {
|
|
|
|
Name string
|
|
|
|
Serve AuthenticatedChannelListenerFactory
|
2017-10-05 13:39:05 +02:00
|
|
|
Filesystems *DatasetMapFilter
|
2017-09-13 23:27:18 +02:00
|
|
|
SnapshotPrefix string
|
2017-09-10 16:13:05 +02:00
|
|
|
Interval time.Duration
|
|
|
|
Prune 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 parseSourceJob(c JobParsingContext, name string, i map[string]interface{}) (j *SourceJob, err error) {
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
var asMap struct {
|
|
|
|
Serve map[string]interface{}
|
2017-10-05 13:39:05 +02:00
|
|
|
Filesystems map[string]string
|
2017-09-10 16:13:05 +02:00
|
|
|
SnapshotPrefix string `mapstructure:"snapshot_prefix"`
|
|
|
|
Interval string
|
|
|
|
Prune map[string]interface{}
|
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 = &SourceJob{Name: name}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
if j.Serve, err = parseAuthenticatedChannelListenerFactory(c, asMap.Serve); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-05 13:39:05 +02:00
|
|
|
if j.Filesystems, err = parseDatasetMapFilter(asMap.Filesystems, true); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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.Prune, err = parsePrunePolicy(asMap.Prune); err != nil {
|
2017-09-13 23:46:34 +02:00
|
|
|
err = errors.Wrap(err, "cannot parse 'prune'")
|
2017-09-10 16:13:05 +02:00
|
|
|
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 *SourceJob) JobName() string {
|
|
|
|
return j.Name
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func (j *SourceJob) JobStart(ctx context.Context) {
|
|
|
|
|
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
2017-09-23 17:52:29 +02:00
|
|
|
defer log.Info("exiting")
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-10-05 13:39:05 +02:00
|
|
|
a := IntervalAutosnap{DatasetFilter: j.Filesystems, Prefix: j.SnapshotPrefix, SnapshotInterval: j.Interval}
|
2017-09-16 21:12:26 +02:00
|
|
|
p, err := j.Pruner(PrunePolicySideDefault, false)
|
|
|
|
if err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error creating pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-23 11:24:36 +02:00
|
|
|
snapContext := context.WithValue(ctx, contextKeyLog, log.WithField(logTaskField, "autosnap"))
|
|
|
|
prunerContext := context.WithValue(ctx, contextKeyLog, log.WithField(logTaskField, "prune"))
|
|
|
|
serveContext := context.WithValue(ctx, contextKeyLog, log.WithField(logTaskField, "serve"))
|
2017-09-16 21:12:26 +02:00
|
|
|
didSnaps := make(chan struct{})
|
|
|
|
|
|
|
|
go j.serve(serveContext)
|
|
|
|
go a.Run(snapContext, didSnaps)
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
case <-didSnaps:
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("starting pruner")
|
2017-09-16 21:12:26 +02:00
|
|
|
p.Run(prunerContext)
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("pruner done")
|
2017-09-16 21:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(prunerContext.Err()).Info("context")
|
2017-09-16 21:12:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:34:41 +01:00
|
|
|
func (j *SourceJob) JobStatus(ctxt context.Context) (*JobStatus, error) {
|
|
|
|
return &JobStatus{}, nil
|
|
|
|
}
|
|
|
|
|
2017-09-16 21:12:26 +02:00
|
|
|
func (j *SourceJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) {
|
|
|
|
p = Pruner{
|
|
|
|
time.Now(),
|
|
|
|
dryRun,
|
2017-10-05 13:39:05 +02:00
|
|
|
j.Filesystems,
|
2017-09-16 21:12:26 +02:00
|
|
|
j.SnapshotPrefix,
|
|
|
|
j.Prune,
|
|
|
|
}
|
|
|
|
return
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *SourceJob) serve(ctx context.Context) {
|
|
|
|
|
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
listener, err := j.Serve.Listen()
|
|
|
|
if err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error listening")
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:19:42 +02:00
|
|
|
type rwcChanMsg struct {
|
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
rwcChan := make(chan rwcChanMsg)
|
2017-09-11 13:50:35 +02:00
|
|
|
|
|
|
|
// Serve connections until interrupted or error
|
|
|
|
outer:
|
2017-09-10 16:13:05 +02:00
|
|
|
for {
|
|
|
|
|
2017-09-11 13:50:35 +02:00
|
|
|
go func() {
|
|
|
|
rwc, err := listener.Accept()
|
|
|
|
if err != nil {
|
2017-10-05 21:19:42 +02:00
|
|
|
rwcChan <- rwcChanMsg{rwc, err}
|
2017-09-11 13:50:35 +02:00
|
|
|
close(rwcChan)
|
|
|
|
return
|
|
|
|
}
|
2017-10-05 21:19:42 +02:00
|
|
|
rwcChan <- rwcChanMsg{rwc, err}
|
2017-09-11 13:50:35 +02:00
|
|
|
}()
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-11 13:50:35 +02:00
|
|
|
select {
|
|
|
|
|
2017-10-05 21:19:42 +02:00
|
|
|
case rwcMsg := <-rwcChan:
|
2017-09-11 13:50:35 +02:00
|
|
|
|
2017-10-05 21:19:42 +02:00
|
|
|
if rwcMsg.err != nil {
|
|
|
|
log.WithError(err).Error("error accepting connection")
|
|
|
|
break outer
|
2017-09-11 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:19:42 +02:00
|
|
|
rwc, err := util.NewReadWriteCloserLogger(rwcMsg.rwc, j.Debug.Conn.ReadDump, j.Debug.Conn.WriteDump)
|
2017-09-11 15:45:10 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-09-11 13:50:35 +02:00
|
|
|
// construct connection handler
|
2017-11-12 23:02:23 +01:00
|
|
|
handler := NewHandler(log, j.Filesystems, NewPrefixFilter(j.SnapshotPrefix))
|
2017-09-11 13:50:35 +02:00
|
|
|
|
|
|
|
// handle connection
|
|
|
|
rpcServer := rpc.NewServer(rwc)
|
2017-09-11 15:45:10 +02:00
|
|
|
if j.Debug.RPC.Log {
|
2017-09-22 14:13:58 +02:00
|
|
|
rpclog := log.WithField("subsystem", "rpc")
|
2017-09-11 15:45:10 +02:00
|
|
|
rpcServer.SetLogger(rpclog, true)
|
|
|
|
}
|
2017-09-11 13:50:35 +02:00
|
|
|
registerEndpoints(rpcServer, handler)
|
|
|
|
if err = rpcServer.Serve(); err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error serving connection")
|
2017-09-11 13:50:35 +02:00
|
|
|
}
|
|
|
|
rwc.Close()
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
case <-ctx.Done():
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(ctx.Err()).Info("context")
|
2017-09-11 13:50:35 +02:00
|
|
|
break outer
|
2017-09-10 16:13:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("closing listener")
|
2017-09-11 13:50:35 +02:00
|
|
|
err = listener.Close()
|
|
|
|
if err != nil {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithError(err).Error("error closing listener")
|
2017-09-11 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
return
|
2017-09-11 13:50:35 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|