zrepl/cmd/config_job_source.go

173 lines
3.7 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/problame/go-streamrpc"
"github.com/zrepl/zrepl/config"
"github.com/zrepl/zrepl/endpoint"
2018-08-25 21:30:25 +02:00
"net"
)
type SourceJob struct {
Name string
Serve ListenerFactory
Filesystems *DatasetMapFilter
SnapshotPrefix string
Interval time.Duration
Prune PrunePolicy
}
2018-08-27 15:19:56 +02:00
func parseSourceJob(c config.Global, in config.SourceJob) (j *SourceJob, err error) {
j = &SourceJob{
Name: in.Name,
Interval: in.Snapshotting.Interval,
}
2018-08-27 15:19:56 +02:00
if j.Serve, err = parseAuthenticatedChannelListenerFactory(c, in.Replication.Serve); err != nil {
return
}
2018-08-27 15:19:56 +02:00
if j.Filesystems, err = parseDatasetMapFilter(in.Replication.Filesystems, true); err != nil {
return
}
2018-08-27 15:19:56 +02:00
if j.SnapshotPrefix, err = parseSnapshotPrefix(in.Snapshotting.SnapshotPrefix); err != nil {
return
}
2018-08-27 15:19:56 +02:00
if j.Prune, err = parsePrunePolicy(in.Pruning, true); err != nil {
2017-09-13 23:46:34 +02:00
err = errors.Wrap(err, "cannot parse 'prune'")
return
}
2018-08-27 15:19:56 +02:00
if in.Debug.Conn.ReadDump != "" || in.Debug.Conn.WriteDump != "" {
logServe := logListenerFactory{
ListenerFactory: j.Serve,
2018-08-27 15:19:56 +02:00
ReadDump: in.Debug.Conn.ReadDump,
WriteDump: in.Debug.Conn.WriteDump,
}
j.Serve = logServe
}
return
}
func (j *SourceJob) JobName() string {
return j.Name
}
func (j *SourceJob) JobType() JobType { return JobTypeSource }
func (j *SourceJob) JobStart(ctx context.Context) {
log := getLogger(ctx)
defer log.Info("exiting")
a := IntervalAutosnap{j.Filesystems, j.SnapshotPrefix, j.Interval}
p, err := j.Pruner(PrunePolicySideDefault, false)
if err != nil {
log.WithError(err).Error("error creating pruner")
return
}
didSnaps := make(chan struct{})
go j.serve(ctx) // logSubsysField set by handleConnection
go a.Run(WithLogger(ctx, log.WithField(logSubsysField, "snap")), didSnaps)
outer:
for {
select {
case <-ctx.Done():
break outer
case <-didSnaps:
p.Run(WithLogger(ctx, log.WithField(logSubsysField, "prune")))
}
}
log.WithError(ctx.Err()).Info("context")
}
func (j *SourceJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) {
p = Pruner{
time.Now(),
dryRun,
j.Filesystems,
j.SnapshotPrefix,
j.Prune,
}
return
}
func (j *SourceJob) serve(ctx context.Context) {
log := getLogger(ctx)
2018-08-25 12:58:17 +02:00
listener, err := j.Serve.Listen()
if err != nil {
getLogger(ctx).WithError(err).Error("error listening")
return
}
type connChanMsg struct {
conn net.Conn
err error
}
connChan := make(chan connChanMsg, 1)
2017-09-11 13:50:35 +02:00
// Serve connections until interrupted or error
2018-08-25 21:30:25 +02:00
outer:
for {
2017-09-11 13:50:35 +02:00
go func() {
rwc, err := listener.Accept()
connChan <- connChanMsg{rwc, err}
2017-09-11 13:50:35 +02:00
}()
2017-09-11 13:50:35 +02:00
select {
case rwcMsg := <-connChan:
2017-09-11 13:50:35 +02:00
if rwcMsg.err != nil {
log.WithError(rwcMsg.err).Error("error accepting connection")
continue
2017-09-11 13:50:35 +02:00
}
j.handleConnection(ctx, rwcMsg.conn)
2017-09-11 13:50:35 +02:00
case <-ctx.Done():
log.WithError(ctx.Err()).Info("context")
2017-09-11 13:50:35 +02:00
break outer
}
}
log.Info("closing listener")
2017-09-11 13:50:35 +02:00
err = listener.Close()
if err != nil {
log.WithError(err).Error("error closing listener")
2017-09-11 13:50:35 +02:00
}
return
}
func (j *SourceJob) handleConnection(ctx context.Context, conn net.Conn) {
log := getLogger(ctx)
log.Info("handling client connection")
2018-08-22 00:52:46 +02:00
senderEP := endpoint.NewSender(j.Filesystems, NewPrefixFilter(j.SnapshotPrefix))
ctx = endpoint.WithLogger(ctx, log.WithField(logSubsysField, "serve"))
ctx = streamrpc.ContextWithLogger(ctx, streamrpcLogAdaptor{log.WithField(logSubsysField, "rpc")})
2018-08-22 00:52:46 +02:00
handler := endpoint.NewHandler(senderEP)
2018-08-10 17:06:00 +02:00
if err := streamrpc.ServeConn(ctx, conn, STREAMRPC_CONFIG, handler.Handle); err != nil {
log.WithError(err).Error("error serving connection")
2018-07-15 17:36:53 +02:00
} else {
log.Info("client closed connection")
}
}