2018-08-22 00:52:46 +02:00
|
|
|
// Package endpoint implements replication endpoints for use with package replication.
|
2018-08-22 00:43:58 +02:00
|
|
|
package endpoint
|
2017-05-20 17:08:18 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-22 14:13:58 +02:00
|
|
|
"bytes"
|
2018-07-08 23:31:46 +02:00
|
|
|
"context"
|
2018-08-30 11:51:47 +02:00
|
|
|
"fmt"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/problame/go-streamrpc"
|
2018-08-22 00:19:03 +02:00
|
|
|
"github.com/zrepl/zrepl/replication"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/replication/pdu"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"io"
|
2017-05-20 17:08:18 +02:00
|
|
|
)
|
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
// Sender implements replication.ReplicationEndpoint for a sending side
|
|
|
|
type Sender struct {
|
2018-08-25 21:30:25 +02:00
|
|
|
FSFilter zfs.DatasetFilter
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
func NewSender(fsf zfs.DatasetFilter) *Sender {
|
|
|
|
return &Sender{FSFilter: fsf}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sender) filterCheckFS(fs string) (*zfs.DatasetPath, error) {
|
|
|
|
dp, err := zfs.NewDatasetPath(fs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if dp.Length() == 0 {
|
|
|
|
return nil, errors.New("empty filesystem not allowed")
|
|
|
|
}
|
|
|
|
pass, err := s.FSFilter.Filter(dp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !pass {
|
|
|
|
return nil, replication.NewFilteredError(fs)
|
|
|
|
}
|
|
|
|
return dp, nil
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (p *Sender) ListFilesystems(ctx context.Context) ([]*pdu.Filesystem, error) {
|
2018-06-20 20:20:37 +02:00
|
|
|
fss, err := zfs.ZFSListMapping(p.FSFilter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
rfss := make([]*pdu.Filesystem, len(fss))
|
2018-06-20 20:20:37 +02:00
|
|
|
for i := range fss {
|
2018-08-16 21:05:21 +02:00
|
|
|
rfss[i] = &pdu.Filesystem{
|
2018-06-20 20:20:37 +02:00
|
|
|
Path: fss[i].ToString(),
|
|
|
|
// FIXME: not supporting ResumeToken yet
|
2017-05-20 17:08:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
return rfss, nil
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (p *Sender) ListFilesystemVersions(ctx context.Context, fs string) ([]*pdu.FilesystemVersion, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
lp, err := p.filterCheckFS(fs)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-30 14:56:16 +02:00
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
fsvs, err := zfs.ZFSListFilesystemVersions(lp, nil)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
rfsvs := make([]*pdu.FilesystemVersion, len(fsvs))
|
2018-06-20 20:20:37 +02:00
|
|
|
for i := range fsvs {
|
2018-08-30 11:51:47 +02:00
|
|
|
rfsvs[i] = pdu.FilesystemVersionFromZFS(&fsvs[i])
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
return rfsvs, nil
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (p *Sender) Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, io.ReadCloser, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
_, err := p.filterCheckFS(r.Filesystem)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-29 23:29:45 +02:00
|
|
|
|
2018-08-30 12:52:08 +02:00
|
|
|
if r.DryRun {
|
2018-10-18 15:45:58 +02:00
|
|
|
si, err := zfs.ZFSSendDry(r.Filesystem, r.From, r.To, "")
|
2018-08-30 12:52:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-10-18 15:45:58 +02:00
|
|
|
var expSize int64 = 0 // protocol says 0 means no estimate
|
|
|
|
if si.SizeEstimate != -1 { // but si returns -1 for no size estimate
|
|
|
|
expSize = si.SizeEstimate
|
|
|
|
}
|
|
|
|
return &pdu.SendRes{ExpectedSize: expSize}, nil, nil
|
2018-08-30 12:52:08 +02:00
|
|
|
} else {
|
2018-10-19 16:12:21 +02:00
|
|
|
stream, err := zfs.ZFSSend(ctx, r.Filesystem, r.From, r.To, "")
|
2018-08-30 12:52:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &pdu.SendRes{}, stream, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 21:37:48 +01:00
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
func (p *Sender) DestroySnapshots(ctx context.Context, req *pdu.DestroySnapshotsReq) (*pdu.DestroySnapshotsRes, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
dp, err := p.filterCheckFS(req.Filesystem)
|
2018-08-30 11:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return doDestroySnapshots(ctx, dp, req.Snapshots)
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:24:15 +02:00
|
|
|
func (p *Sender) ReplicationCursor(ctx context.Context, req *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
dp, err := p.filterCheckFS(req.Filesystem)
|
2018-08-30 11:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:24:15 +02:00
|
|
|
switch op := req.Op.(type) {
|
|
|
|
case *pdu.ReplicationCursorReq_Get:
|
|
|
|
cursor, err := zfs.ZFSGetReplicationCursor(dp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-04 22:29:51 +02:00
|
|
|
}
|
2018-09-06 03:24:15 +02:00
|
|
|
if cursor == nil {
|
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Error{Error: "cursor does not exist"}}, nil
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
2018-09-06 06:48:52 +02:00
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Guid{Guid: cursor.Guid}}, nil
|
2018-09-06 03:24:15 +02:00
|
|
|
case *pdu.ReplicationCursorReq_Set:
|
|
|
|
guid, err := zfs.ZFSSetReplicationCursor(dp, op.Set.Snapshot)
|
2018-08-30 11:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-06 03:24:15 +02:00
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Guid{Guid: guid}}, nil
|
2018-08-30 11:51:47 +02:00
|
|
|
default:
|
2018-09-06 03:24:15 +02:00
|
|
|
return nil, errors.Errorf("unknown op %T", op)
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
type FSFilter interface { // FIXME unused
|
2018-08-22 00:43:58 +02:00
|
|
|
Filter(path *zfs.DatasetPath) (pass bool, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: can we get away without error types here?
|
2018-09-06 04:49:26 +02:00
|
|
|
type FSMap interface { // FIXME unused
|
2018-08-22 00:43:58 +02:00
|
|
|
FSFilter
|
2018-08-25 21:30:25 +02:00
|
|
|
Map(path *zfs.DatasetPath) (*zfs.DatasetPath, error)
|
|
|
|
Invert() (FSMap, error)
|
|
|
|
AsFilter() FSFilter
|
2018-08-22 00:43:58 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
// Receiver implements replication.ReplicationEndpoint for a receiving side
|
|
|
|
type Receiver struct {
|
2018-09-06 04:49:26 +02:00
|
|
|
root *zfs.DatasetPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReceiver(rootDataset *zfs.DatasetPath) (*Receiver, error) {
|
|
|
|
if rootDataset.Length() <= 0 {
|
|
|
|
return nil, errors.New("root dataset must not be an empty path")
|
|
|
|
}
|
|
|
|
return &Receiver{root: rootDataset.Copy()}, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
type subroot struct {
|
|
|
|
localRoot *zfs.DatasetPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ zfs.DatasetFilter = subroot{}
|
|
|
|
|
|
|
|
// Filters local p
|
|
|
|
func (f subroot) Filter(p *zfs.DatasetPath) (pass bool, err error) {
|
|
|
|
return p.HasPrefix(f.localRoot) && !p.Equal(f.localRoot), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f subroot) MapToLocal(fs string) (*zfs.DatasetPath, error) {
|
|
|
|
p, err := zfs.NewDatasetPath(fs)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
if p.Length() == 0 {
|
|
|
|
return nil, errors.Errorf("cannot map empty filesystem")
|
|
|
|
}
|
|
|
|
c := f.localRoot.Copy()
|
|
|
|
c.Extend(p)
|
|
|
|
return c, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (e *Receiver) ListFilesystems(ctx context.Context) ([]*pdu.Filesystem, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
filtered, err := zfs.ZFSListMapping(subroot{e.root})
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
2018-09-06 04:49:26 +02:00
|
|
|
return nil, err
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
// present without prefix, and only those that are not placeholders
|
|
|
|
fss := make([]*pdu.Filesystem, 0, len(filtered))
|
|
|
|
for _, a := range filtered {
|
|
|
|
ph, err := zfs.ZFSIsPlaceholderFilesystem(a)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
2018-09-06 04:49:26 +02:00
|
|
|
getLogger(ctx).
|
|
|
|
WithError(err).
|
|
|
|
WithField("fs", a).
|
|
|
|
Error("inconsistent placeholder property")
|
|
|
|
return nil, errors.New("server error, see logs") // don't leak path
|
|
|
|
}
|
|
|
|
if ph {
|
|
|
|
continue
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-09-06 04:49:26 +02:00
|
|
|
a.TrimPrefix(e.root)
|
|
|
|
fss = append(fss, &pdu.Filesystem{Path: a.ToString()})
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
|
|
|
return fss, nil
|
|
|
|
}
|
2017-12-26 21:37:48 +01:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (e *Receiver) ListFilesystemVersions(ctx context.Context, fs string) ([]*pdu.FilesystemVersion, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
lp, err := subroot{e.root}.MapToLocal(fs)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
fsvs, err := zfs.ZFSListFilesystemVersions(lp, nil)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-26 21:37:48 +01:00
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
rfsvs := make([]*pdu.FilesystemVersion, len(fsvs))
|
2018-06-20 20:20:37 +02:00
|
|
|
for i := range fsvs {
|
2018-08-30 11:51:47 +02:00
|
|
|
rfsvs[i] = pdu.FilesystemVersionFromZFS(&fsvs[i])
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-07-30 14:56:16 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
return rfsvs, nil
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (e *Receiver) Receive(ctx context.Context, req *pdu.ReceiveReq, sendStream io.ReadCloser) error {
|
2018-07-08 23:31:46 +02:00
|
|
|
defer sendStream.Close()
|
|
|
|
|
2018-09-06 04:49:26 +02:00
|
|
|
lp, err := subroot{e.root}.MapToLocal(req.Filesystem)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-31 16:26:11 +02:00
|
|
|
getLogger(ctx).Debug("incoming Receive")
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
// create placeholder parent filesystems as appropriate
|
|
|
|
var visitErr error
|
|
|
|
f := zfs.NewDatasetPathForest()
|
|
|
|
f.Add(lp)
|
2018-08-10 17:06:00 +02:00
|
|
|
getLogger(ctx).Debug("begin tree-walk")
|
2018-06-20 20:20:37 +02:00
|
|
|
f.WalkTopDown(func(v zfs.DatasetPathVisit) (visitChildTree bool) {
|
|
|
|
if v.Path.Equal(lp) {
|
|
|
|
return false
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
_, err := zfs.ZFSGet(v.Path, []string{zfs.ZREPL_PLACEHOLDER_PROPERTY_NAME})
|
|
|
|
if err != nil {
|
|
|
|
// interpret this as an early exit of the zfs binary due to the fs not existing
|
|
|
|
if err := zfs.ZFSCreatePlaceholderFilesystem(v.Path); err != nil {
|
2018-08-10 17:06:00 +02:00
|
|
|
getLogger(ctx).
|
|
|
|
WithError(err).
|
|
|
|
WithField("placeholder_fs", v.Path).
|
|
|
|
Error("cannot create placeholder filesystem")
|
2018-06-20 20:20:37 +02:00
|
|
|
visitErr = err
|
2017-12-26 21:37:48 +01:00
|
|
|
return false
|
2017-05-20 17:08:18 +02:00
|
|
|
}
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-08-10 17:06:00 +02:00
|
|
|
getLogger(ctx).WithField("filesystem", v.Path.ToString()).Debug("exists")
|
2018-06-20 20:20:37 +02:00
|
|
|
return true // leave this fs as is
|
|
|
|
})
|
2018-08-10 17:06:00 +02:00
|
|
|
getLogger(ctx).WithField("visitErr", visitErr).Debug("complete tree-walk")
|
2017-07-08 13:13:16 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
if visitErr != nil {
|
|
|
|
return visitErr
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
needForceRecv := false
|
|
|
|
props, err := zfs.ZFSGet(lp, []string{zfs.ZREPL_PLACEHOLDER_PROPERTY_NAME})
|
|
|
|
if err == nil {
|
|
|
|
if isPlaceholder, _ := zfs.IsPlaceholder(lp, props.Get(zfs.ZREPL_PLACEHOLDER_PROPERTY_NAME)); isPlaceholder {
|
|
|
|
needForceRecv = true
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
args := make([]string, 0, 1)
|
|
|
|
if needForceRecv {
|
|
|
|
args = append(args, "-F")
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-10 17:06:00 +02:00
|
|
|
getLogger(ctx).Debug("start receive command")
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-10-19 16:12:21 +02:00
|
|
|
if err := zfs.ZFSRecv(ctx, lp.ToString(), sendStream, args...); err != nil {
|
2018-08-31 16:26:11 +02:00
|
|
|
getLogger(ctx).
|
|
|
|
WithError(err).
|
|
|
|
WithField("args", args).
|
|
|
|
Error("zfs receive failed")
|
|
|
|
sendStream.Close()
|
2018-06-20 20:20:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
func (e *Receiver) DestroySnapshots(ctx context.Context, req *pdu.DestroySnapshotsReq) (*pdu.DestroySnapshotsRes, error) {
|
2018-09-06 04:49:26 +02:00
|
|
|
lp, err := subroot{e.root}.MapToLocal(req.Filesystem)
|
2018-08-30 11:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return doDestroySnapshots(ctx, lp, req.Snapshots)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doDestroySnapshots(ctx context.Context, lp *zfs.DatasetPath, snaps []*pdu.FilesystemVersion) (*pdu.DestroySnapshotsRes, error) {
|
|
|
|
fsvs := make([]*zfs.FilesystemVersion, len(snaps))
|
|
|
|
for i, fsv := range snaps {
|
|
|
|
if fsv.Type != pdu.FilesystemVersion_Snapshot {
|
|
|
|
return nil, fmt.Errorf("version %q is not a snapshot", fsv.Name)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
fsvs[i], err = fsv.ZFSFilesystemVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := &pdu.DestroySnapshotsRes{
|
|
|
|
Results: make([]*pdu.DestroySnapshotRes, len(fsvs)),
|
|
|
|
}
|
|
|
|
for i, fsv := range fsvs {
|
|
|
|
err := zfs.ZFSDestroyFilesystemVersion(lp, fsv)
|
|
|
|
errMsg := ""
|
|
|
|
if err != nil {
|
|
|
|
errMsg = err.Error()
|
|
|
|
}
|
|
|
|
res.Results[i] = &pdu.DestroySnapshotRes{
|
|
|
|
Snapshot: pdu.FilesystemVersionFromZFS(fsv),
|
|
|
|
Error: errMsg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
|
|
// RPC STUBS
|
|
|
|
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
const (
|
2018-09-06 03:24:15 +02:00
|
|
|
RPCListFilesystems = "ListFilesystems"
|
|
|
|
RPCListFilesystemVersions = "ListFilesystemVersions"
|
|
|
|
RPCReceive = "Receive"
|
|
|
|
RPCSend = "Send"
|
|
|
|
RPCSDestroySnapshots = "DestroySnapshots"
|
|
|
|
RPCReplicationCursor = "ReplicationCursor"
|
2018-06-20 20:20:37 +02:00
|
|
|
)
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
// Remote implements an endpoint stub that uses streamrpc as a transport.
|
|
|
|
type Remote struct {
|
|
|
|
c *streamrpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemote(c *streamrpc.Client) Remote {
|
|
|
|
return Remote{c}
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (s Remote) ListFilesystems(ctx context.Context) ([]*pdu.Filesystem, error) {
|
2018-08-16 21:05:21 +02:00
|
|
|
req := pdu.ListFilesystemReq{}
|
2018-06-20 20:20:37 +02:00
|
|
|
b, err := proto.Marshal(&req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-08-22 00:52:46 +02:00
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCListFilesystems, bytes.NewBuffer(b), nil)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rs != nil {
|
2018-07-08 23:31:46 +02:00
|
|
|
rs.Close()
|
2018-06-20 20:20:37 +02:00
|
|
|
return nil, errors.New("response contains unexpected stream")
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
var res pdu.ListFilesystemRes
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res.Filesystems, nil
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (s Remote) ListFilesystemVersions(ctx context.Context, fs string) ([]*pdu.FilesystemVersion, error) {
|
2018-08-16 21:05:21 +02:00
|
|
|
req := pdu.ListFilesystemVersionsReq{
|
2018-06-20 20:20:37 +02:00
|
|
|
Filesystem: fs,
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(&req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-08-22 00:52:46 +02:00
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCListFilesystemVersions, bytes.NewBuffer(b), nil)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rs != nil {
|
2018-07-08 23:31:46 +02:00
|
|
|
rs.Close()
|
2018-06-20 20:20:37 +02:00
|
|
|
return nil, errors.New("response contains unexpected stream")
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
var res pdu.ListFilesystemVersionsRes
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res.Versions, nil
|
|
|
|
}
|
2017-07-08 13:13:16 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (s Remote) Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, io.ReadCloser, error) {
|
2018-06-20 20:20:37 +02:00
|
|
|
b, err := proto.Marshal(r)
|
2017-12-26 21:37:48 +01:00
|
|
|
if err != nil {
|
2018-06-20 20:20:37 +02:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-22 00:52:46 +02:00
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCSend, bytes.NewBuffer(b), nil)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-09-24 12:27:57 +02:00
|
|
|
if !r.DryRun && rs == nil {
|
2018-06-20 20:20:37 +02:00
|
|
|
return nil, nil, errors.New("response does not contain a stream")
|
|
|
|
}
|
2018-09-24 12:27:57 +02:00
|
|
|
if r.DryRun && rs != nil {
|
|
|
|
rs.Close()
|
|
|
|
return nil, nil, errors.New("response contains unexpected stream (was dry run)")
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
var res pdu.SendRes
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
2018-07-08 23:31:46 +02:00
|
|
|
rs.Close()
|
2018-08-25 21:30:25 +02:00
|
|
|
return nil, nil, err
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
return &res, rs, nil
|
|
|
|
}
|
2017-07-08 13:13:16 +02:00
|
|
|
|
2018-08-25 21:30:25 +02:00
|
|
|
func (s Remote) Receive(ctx context.Context, r *pdu.ReceiveReq, sendStream io.ReadCloser) error {
|
2018-07-08 23:31:46 +02:00
|
|
|
defer sendStream.Close()
|
2018-06-20 20:20:37 +02:00
|
|
|
b, err := proto.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-22 00:52:46 +02:00
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCReceive, bytes.NewBuffer(b), sendStream)
|
2018-08-31 16:26:11 +02:00
|
|
|
getLogger(ctx).WithField("err", err).Debug("Remote.Receive RequestReplyReturned")
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rs != nil {
|
2018-07-08 23:31:46 +02:00
|
|
|
rs.Close()
|
2018-06-20 20:20:37 +02:00
|
|
|
return errors.New("response contains unexpected stream")
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
var res pdu.ReceiveRes
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-08 13:13:16 +02:00
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
func (s Remote) DestroySnapshots(ctx context.Context, r *pdu.DestroySnapshotsReq) (*pdu.DestroySnapshotsRes, error) {
|
|
|
|
b, err := proto.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCSDestroySnapshots, bytes.NewBuffer(b), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rs != nil {
|
|
|
|
rs.Close()
|
|
|
|
return nil, errors.New("response contains unexpected stream")
|
|
|
|
}
|
|
|
|
var res pdu.DestroySnapshotsRes
|
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &res, nil
|
2018-09-24 12:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Remote) ReplicationCursor(ctx context.Context, req *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error) {
|
|
|
|
b, err := proto.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rb, rs, err := s.c.RequestReply(ctx, RPCReplicationCursor, bytes.NewBuffer(b), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rs != nil {
|
|
|
|
rs.Close()
|
|
|
|
return nil, errors.New("response contains unexpected stream")
|
|
|
|
}
|
|
|
|
var res pdu.ReplicationCursorRes
|
|
|
|
if err := proto.Unmarshal(rb.Bytes(), &res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &res, nil
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
// Handler implements the server-side streamrpc.HandlerFunc for a Remote endpoint stub.
|
|
|
|
type Handler struct {
|
2018-08-22 00:10:09 +02:00
|
|
|
ep replication.Endpoint
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-09-22 14:13:58 +02:00
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func NewHandler(ep replication.Endpoint) Handler {
|
|
|
|
return Handler{ep}
|
2018-08-22 00:43:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:52:46 +02:00
|
|
|
func (a *Handler) Handle(ctx context.Context, endpoint string, reqStructured *bytes.Buffer, reqStream io.ReadCloser) (resStructured *bytes.Buffer, resStream io.ReadCloser, err error) {
|
2018-07-08 23:31:46 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
switch endpoint {
|
|
|
|
case RPCListFilesystems:
|
2018-08-16 21:05:21 +02:00
|
|
|
var req pdu.ListFilesystemReq
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-07-08 23:31:46 +02:00
|
|
|
fsses, err := a.ep.ListFilesystems(ctx)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
res := &pdu.ListFilesystemRes{
|
2018-06-20 20:20:37 +02:00
|
|
|
Filesystems: fsses,
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil, nil
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
case RPCListFilesystemVersions:
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
var req pdu.ListFilesystemVersionsReq
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
2017-05-20 17:08:18 +02:00
|
|
|
}
|
2018-07-08 23:31:46 +02:00
|
|
|
fsvs, err := a.ep.ListFilesystemVersions(ctx, req.Filesystem)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
res := &pdu.ListFilesystemVersionsRes{
|
2018-06-20 20:20:37 +02:00
|
|
|
Versions: fsvs,
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil, nil
|
|
|
|
|
|
|
|
case RPCSend:
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
sender, ok := a.ep.(replication.Sender)
|
|
|
|
if !ok {
|
|
|
|
goto Err
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
var req pdu.SendReq
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-22 00:10:09 +02:00
|
|
|
res, sendStream, err := sender.Send(ctx, &req)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
return bytes.NewBuffer(b), sendStream, err
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
case RPCReceive:
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
receiver, ok := a.ep.(replication.Receiver)
|
|
|
|
if !ok {
|
|
|
|
goto Err
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
var req pdu.ReceiveReq
|
2018-06-20 20:20:37 +02:00
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-22 00:10:09 +02:00
|
|
|
err := receiver.Receive(ctx, &req, reqStream)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
b, err := proto.Marshal(&pdu.ReceiveRes{})
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil, err
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
case RPCSDestroySnapshots:
|
|
|
|
|
|
|
|
var req pdu.DestroySnapshotsReq
|
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := a.ep.DestroySnapshots(ctx, &req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil, nil
|
|
|
|
|
2018-09-06 03:24:15 +02:00
|
|
|
case RPCReplicationCursor:
|
2018-08-30 11:51:47 +02:00
|
|
|
|
|
|
|
sender, ok := a.ep.(replication.Sender)
|
|
|
|
if !ok {
|
|
|
|
goto Err
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:24:15 +02:00
|
|
|
var req pdu.ReplicationCursorReq
|
2018-08-30 11:51:47 +02:00
|
|
|
if err := proto.Unmarshal(reqStructured.Bytes(), &req); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-09-06 03:24:15 +02:00
|
|
|
res, err := sender.ReplicationCursor(ctx, &req)
|
2018-08-30 11:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
b, err := proto.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(b), nil, nil
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2018-08-25 21:30:25 +02:00
|
|
|
Err:
|
2018-08-22 00:10:09 +02:00
|
|
|
return nil, nil, errors.New("no handler for given endpoint")
|
2017-05-20 17:08:18 +02:00
|
|
|
}
|