2018-08-27 22:21:45 +02:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/problame/go-streamrpc"
|
2018-09-08 07:03:41 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-08-27 22:21:45 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"github.com/zrepl/zrepl/daemon/logging"
|
|
|
|
"github.com/zrepl/zrepl/daemon/serve"
|
|
|
|
"github.com/zrepl/zrepl/endpoint"
|
2018-09-05 01:41:54 +02:00
|
|
|
"path"
|
2018-09-06 04:49:26 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Sink struct {
|
|
|
|
name string
|
|
|
|
l serve.ListenerFactory
|
2018-08-31 21:51:44 +02:00
|
|
|
rpcConf *streamrpc.ConnConfig
|
2018-09-06 04:49:26 +02:00
|
|
|
rootDataset *zfs.DatasetPath
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func SinkFromConfig(g *config.Global, in *config.SinkJob) (s *Sink, err error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
s = &Sink{name: in.Name}
|
2018-09-04 23:44:45 +02:00
|
|
|
if s.l, s.rpcConf, err = serve.FromConfig(g, in.Serve); err != nil {
|
2018-08-27 22:21:45 +02:00
|
|
|
return nil, errors.Wrap(err, "cannot build server")
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
s.rootDataset, err = zfs.NewDatasetPath(in.RootDataset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("root dataset is not a valid zfs filesystem path")
|
|
|
|
}
|
|
|
|
if s.rootDataset.Length() <= 0 {
|
|
|
|
return nil, errors.New("root dataset must not be empty") // duplicates error check of receiver
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Sink) Name() string { return j.name }
|
|
|
|
|
2018-09-23 21:08:03 +02:00
|
|
|
type SinkStatus struct {}
|
|
|
|
|
|
|
|
func (*Sink) Status() *Status {
|
|
|
|
return &Status{Type: TypeSink} // FIXME SinkStatus
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 07:03:41 +02:00
|
|
|
func (*Sink) RegisterMetrics(registerer prometheus.Registerer) {}
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
func (j *Sink) Run(ctx context.Context) {
|
|
|
|
|
|
|
|
log := GetLogger(ctx)
|
|
|
|
defer log.Info("job exiting")
|
|
|
|
|
|
|
|
l, err := j.l.Listen()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("cannot listen")
|
|
|
|
return
|
|
|
|
}
|
2018-09-05 01:41:54 +02:00
|
|
|
defer l.Close()
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
log.WithField("addr", l.Addr()).Debug("accepting connections")
|
|
|
|
|
|
|
|
var connId int
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
2018-09-05 01:41:54 +02:00
|
|
|
case res := <-accept(ctx, l):
|
2018-08-27 22:21:45 +02:00
|
|
|
if res.err != nil {
|
2018-09-05 01:41:54 +02:00
|
|
|
log.WithError(res.err).Info("accept error")
|
|
|
|
continue
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
connId++
|
|
|
|
connLog := log.
|
|
|
|
WithField("connID", connId)
|
2018-09-18 22:44:00 +02:00
|
|
|
go j.handleConnection(WithLogger(ctx, connLog), res.conn)
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
func (j *Sink) handleConnection(ctx context.Context, conn serve.AuthenticatedConn) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
log := GetLogger(ctx)
|
2018-09-05 01:41:54 +02:00
|
|
|
log.
|
|
|
|
WithField("addr", conn.RemoteAddr()).
|
|
|
|
WithField("client_identity", conn.ClientIdentity()).
|
|
|
|
Info("handling connection")
|
2018-08-27 22:21:45 +02:00
|
|
|
defer log.Info("finished handling connection")
|
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
clientRootStr := path.Join(j.rootDataset.ToString(), conn.ClientIdentity())
|
|
|
|
clientRoot, err := zfs.NewDatasetPath(clientRootStr)
|
|
|
|
if err != nil {
|
2018-09-05 01:41:54 +02:00
|
|
|
log.WithError(err).
|
|
|
|
WithField("client_identity", conn.ClientIdentity()).
|
|
|
|
Error("cannot build client filesystem map (client identity must be a valid ZFS FS name")
|
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
log.WithField("client_root", clientRoot).Debug("client root")
|
2018-09-05 01:41:54 +02:00
|
|
|
|
2018-08-31 16:26:11 +02:00
|
|
|
ctx = logging.WithSubsystemLoggers(ctx, log)
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
local, err := endpoint.NewReceiver(clientRoot)
|
2018-08-27 22:21:45 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("unexpected error: cannot convert mapping to filter")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := endpoint.NewHandler(local)
|
2018-08-31 21:51:44 +02:00
|
|
|
if err := streamrpc.ServeConn(ctx, conn, j.rpcConf, handler.Handle); err != nil {
|
2018-08-27 22:21:45 +02:00
|
|
|
log.WithError(err).Error("error serving client")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type acceptResult struct {
|
2018-09-05 01:41:54 +02:00
|
|
|
conn serve.AuthenticatedConn
|
2018-08-27 22:21:45 +02:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
func accept(ctx context.Context, listener serve.AuthenticatedListener) <-chan acceptResult {
|
2018-08-27 22:21:45 +02:00
|
|
|
c := make(chan acceptResult, 1)
|
|
|
|
go func() {
|
2018-09-05 01:41:54 +02:00
|
|
|
conn, err := listener.Accept(ctx)
|
2018-08-27 22:21:45 +02:00
|
|
|
c <- acceptResult{conn, err}
|
|
|
|
}()
|
|
|
|
return c
|
|
|
|
}
|