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 (
|
2018-07-08 23:31:46 +02:00
|
|
|
"context"
|
2018-08-30 11:51:47 +02:00
|
|
|
"fmt"
|
2018-12-11 22:01:50 +01:00
|
|
|
"path"
|
|
|
|
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2019-02-22 11:40:27 +01:00
|
|
|
"github.com/zrepl/zrepl/replication/logic/pdu"
|
2019-03-20 18:52:40 +01:00
|
|
|
"github.com/zrepl/zrepl/util/chainlock"
|
2019-03-28 21:22:22 +01:00
|
|
|
"github.com/zrepl/zrepl/util/envconst"
|
|
|
|
"github.com/zrepl/zrepl/util/semaphore"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
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-12-11 22:01:50 +01: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 {
|
2019-02-22 11:40:27 +01:00
|
|
|
return nil, fmt.Errorf("endpoint does not allow access to filesystem %s", fs)
|
2018-09-06 04:49:26 +02:00
|
|
|
}
|
|
|
|
return dp, nil
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Sender) ListFilesystems(ctx context.Context, r *pdu.ListFilesystemReq) (*pdu.ListFilesystemRes, error) {
|
|
|
|
fss, err := zfs.ZFSListMapping(ctx, s.FSFilter)
|
2018-06-20 20:20:37 +02:00
|
|
|
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
|
2019-03-13 18:33:20 +01:00
|
|
|
IsPlaceholder: false, // sender FSs are never placeholders
|
2017-05-20 17:08:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 18:33:20 +01:00
|
|
|
res := &pdu.ListFilesystemRes{Filesystems: rfss}
|
2018-12-11 22:01:50 +01:00
|
|
|
return res, nil
|
2017-12-26 21:37:48 +01:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Sender) ListFilesystemVersions(ctx context.Context, r *pdu.ListFilesystemVersionsReq) (*pdu.ListFilesystemVersionsRes, error) {
|
|
|
|
lp, err := s.filterCheckFS(r.GetFilesystem())
|
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-12-11 22:01:50 +01:00
|
|
|
res := &pdu.ListFilesystemVersionsRes{Versions: rfsvs}
|
|
|
|
return res, nil
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-28 21:22:22 +01:00
|
|
|
var maxConcurrentZFSSendSemaphore = semaphore.New(envconst.Int64("ZREPL_ENDPOINT_MAX_CONCURRENT_SEND", 10))
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Sender) Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, zfs.StreamCopier, error) {
|
|
|
|
_, err := s.filterCheckFS(r.Filesystem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-28 21:22:22 +01:00
|
|
|
getLogger(ctx).Debug("acquire concurrent send semaphore")
|
|
|
|
// TODO use try-acquire and fail with resource-exhaustion rpc status
|
|
|
|
// => would require handling on the client-side
|
|
|
|
// => this is a dataconn endpoint, doesn't have the status code semantics of gRPC
|
|
|
|
guard, err := maxConcurrentZFSSendSemaphore.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer guard.Release()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
si, err := zfs.ZFSSendDry(r.Filesystem, r.From, r.To, "")
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-03-28 21:22:22 +01:00
|
|
|
|
2018-12-11 22:01:50 +01: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
|
|
|
|
}
|
|
|
|
res := &pdu.SendRes{ExpectedSize: expSize}
|
2018-08-29 23:29:45 +02:00
|
|
|
|
2018-08-30 12:52:08 +02:00
|
|
|
if r.DryRun {
|
2018-12-11 22:01:50 +01:00
|
|
|
return res, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
streamCopier, err := zfs.ZFSSend(ctx, r.Filesystem, r.From, r.To, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
return res, streamCopier, 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)
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:46:36 +01:00
|
|
|
func (p *Sender) Ping(ctx context.Context, req *pdu.PingReq) (*pdu.PingRes, error) {
|
|
|
|
res := pdu.PingRes{
|
|
|
|
Echo: req.GetMessage(),
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Sender) PingDataconn(ctx context.Context, req *pdu.PingReq) (*pdu.PingRes, error) {
|
|
|
|
return p.Ping(ctx, req)
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:33:20 +01:00
|
|
|
func (p *Sender) WaitForConnectivity(ctx context.Context) error {
|
2019-03-11 13:46:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-11-16 12:03:38 +01:00
|
|
|
return &pdu.ReplicationCursorRes{Result: &pdu.ReplicationCursorRes_Notexist{Notexist: true}}, 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-12-11 22:01:50 +01:00
|
|
|
func (p *Sender) Receive(ctx context.Context, r *pdu.ReceiveReq, receive zfs.StreamCopier) (*pdu.ReceiveRes, error) {
|
|
|
|
return nil, fmt.Errorf("sender does not implement Receive()")
|
|
|
|
}
|
|
|
|
|
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-12-11 22:01:50 +01:00
|
|
|
rootWithoutClientComponent *zfs.DatasetPath
|
|
|
|
appendClientIdentity bool
|
2019-03-20 18:52:40 +01:00
|
|
|
|
|
|
|
recvParentCreationMtx *chainlock.L
|
2018-09-06 04:49:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func NewReceiver(rootDataset *zfs.DatasetPath, appendClientIdentity bool) *Receiver {
|
2018-09-06 04:49:26 +02:00
|
|
|
if rootDataset.Length() <= 0 {
|
2018-12-11 22:01:50 +01:00
|
|
|
panic(fmt.Sprintf("root dataset must not be an empty path: %v", rootDataset))
|
2018-09-06 04:49:26 +02:00
|
|
|
}
|
2019-03-20 18:52:40 +01:00
|
|
|
return &Receiver{
|
|
|
|
rootWithoutClientComponent: rootDataset.Copy(),
|
|
|
|
appendClientIdentity: appendClientIdentity,
|
|
|
|
recvParentCreationMtx: chainlock.New(),
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientIdentity(rootFS *zfs.DatasetPath, clientIdentity string) error {
|
|
|
|
_, err := clientRoot(rootFS, clientIdentity)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func clientRoot(rootFS *zfs.DatasetPath, clientIdentity string) (*zfs.DatasetPath, error) {
|
|
|
|
rootFSLen := rootFS.Length()
|
|
|
|
clientRootStr := path.Join(rootFS.ToString(), clientIdentity)
|
|
|
|
clientRoot, err := zfs.NewDatasetPath(clientRootStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rootFSLen+1 != clientRoot.Length() {
|
|
|
|
return nil, fmt.Errorf("client identity must be a single ZFS filesystem path component")
|
|
|
|
}
|
|
|
|
return clientRoot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Receiver) clientRootFromCtx(ctx context.Context) *zfs.DatasetPath {
|
|
|
|
if !s.appendClientIdentity {
|
|
|
|
return s.rootWithoutClientComponent.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
clientIdentity, ok := ctx.Value(ClientIdentityKey).(string)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("ClientIdentityKey context value must be set"))
|
|
|
|
}
|
|
|
|
|
|
|
|
clientRoot, err := clientRoot(s.rootWithoutClientComponent, clientIdentity)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ClientIdentityContextKey must have been validated before invoking Receiver: %s", err))
|
|
|
|
}
|
|
|
|
return clientRoot
|
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-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) ListFilesystems(ctx context.Context, req *pdu.ListFilesystemReq) (*pdu.ListFilesystemRes, error) {
|
|
|
|
root := s.clientRootFromCtx(ctx)
|
|
|
|
filtered, err := zfs.ZFSListMapping(ctx, subroot{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
|
|
|
}
|
2019-03-13 18:33:20 +01:00
|
|
|
// present filesystem without the root_fs prefix
|
2018-09-06 04:49:26 +02:00
|
|
|
fss := make([]*pdu.Filesystem, 0, len(filtered))
|
|
|
|
for _, a := range filtered {
|
2019-03-19 17:43:28 +01:00
|
|
|
l := getLogger(ctx).WithField("fs", a)
|
|
|
|
ph, err := zfs.ZFSGetFilesystemPlaceholderState(a)
|
2018-06-20 20:20:37 +02:00
|
|
|
if err != nil {
|
2019-03-19 17:43:28 +01:00
|
|
|
l.WithError(err).Error("error getting placeholder state")
|
|
|
|
return nil, errors.Wrapf(err, "cannot get placeholder state for fs %q", a)
|
|
|
|
}
|
|
|
|
l.WithField("placeholder_state", fmt.Sprintf("%#v", ph)).Debug("placeholder state")
|
|
|
|
if !ph.FSExists {
|
|
|
|
l.Error("inconsistent placeholder state: filesystem must exists")
|
|
|
|
err := errors.Errorf("inconsistent placeholder state: filesystem %q must exist in this context", a.ToString())
|
|
|
|
return nil, err
|
2018-09-06 04:49:26 +02:00
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
a.TrimPrefix(root)
|
2019-03-19 17:43:28 +01:00
|
|
|
fss = append(fss, &pdu.Filesystem{Path: a.ToString(), IsPlaceholder: ph.IsPlaceholder})
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
if len(fss) == 0 {
|
2019-03-13 18:33:20 +01:00
|
|
|
getLogger(ctx).Debug("no filesystems found")
|
|
|
|
return &pdu.ListFilesystemRes{}, nil
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
return &pdu.ListFilesystemRes{Filesystems: fss}, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-12-26 21:37:48 +01:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) ListFilesystemVersions(ctx context.Context, req *pdu.ListFilesystemVersionsReq) (*pdu.ListFilesystemVersionsRes, error) {
|
|
|
|
root := s.clientRootFromCtx(ctx)
|
|
|
|
lp, err := subroot{root}.MapToLocal(req.GetFilesystem())
|
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-12-11 22:01:50 +01:00
|
|
|
return &pdu.ListFilesystemVersionsRes{Versions: rfsvs}, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-11 13:46:36 +01:00
|
|
|
func (s *Receiver) Ping(ctx context.Context, req *pdu.PingReq) (*pdu.PingRes, error) {
|
|
|
|
res := pdu.PingRes{
|
|
|
|
Echo: req.GetMessage(),
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Receiver) PingDataconn(ctx context.Context, req *pdu.PingReq) (*pdu.PingRes, error) {
|
|
|
|
return s.Ping(ctx, req)
|
|
|
|
}
|
|
|
|
|
2019-03-13 18:33:20 +01:00
|
|
|
func (s *Receiver) WaitForConnectivity(ctx context.Context) error {
|
2019-03-11 13:46:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) ReplicationCursor(context.Context, *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error) {
|
|
|
|
return nil, fmt.Errorf("ReplicationCursor not implemented for Receiver")
|
|
|
|
}
|
2018-07-08 23:31:46 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) Send(ctx context.Context, req *pdu.SendReq) (*pdu.SendRes, zfs.StreamCopier, error) {
|
|
|
|
return nil, nil, fmt.Errorf("receiver does not implement Send()")
|
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-28 21:22:22 +01:00
|
|
|
var maxConcurrentZFSRecvSemaphore = semaphore.New(envconst.Int64("ZREPL_ENDPOINT_MAX_CONCURRENT_RECV", 10))
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) Receive(ctx context.Context, req *pdu.ReceiveReq, receive zfs.StreamCopier) (*pdu.ReceiveRes, error) {
|
2018-08-31 16:26:11 +02:00
|
|
|
getLogger(ctx).Debug("incoming Receive")
|
2018-12-11 22:01:50 +01:00
|
|
|
defer receive.Close()
|
|
|
|
|
|
|
|
root := s.clientRootFromCtx(ctx)
|
|
|
|
lp, err := subroot{root}.MapToLocal(req.Filesystem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-31 16:26:11 +02:00
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
// create placeholder parent filesystems as appropriate
|
2019-03-20 18:52:40 +01:00
|
|
|
//
|
|
|
|
// Manipulating the ZFS dataset hierarchy must happen exclusively.
|
|
|
|
// TODO: Use fine-grained locking to allow separate clients / requests to pass
|
|
|
|
// through the following section concurrently when operating on disjoint
|
|
|
|
// ZFS dataset hierarchy subtrees.
|
2018-06-20 20:20:37 +02:00
|
|
|
var visitErr error
|
2019-03-20 18:52:40 +01:00
|
|
|
func() {
|
|
|
|
getLogger(ctx).Debug("begin aquire recvParentCreationMtx")
|
|
|
|
defer s.recvParentCreationMtx.Lock().Unlock()
|
|
|
|
getLogger(ctx).Debug("end aquire recvParentCreationMtx")
|
|
|
|
defer getLogger(ctx).Debug("release recvParentCreationMtx")
|
|
|
|
|
|
|
|
f := zfs.NewDatasetPathForest()
|
|
|
|
f.Add(lp)
|
|
|
|
getLogger(ctx).Debug("begin tree-walk")
|
|
|
|
f.WalkTopDown(func(v zfs.DatasetPathVisit) (visitChildTree bool) {
|
|
|
|
if v.Path.Equal(lp) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ph, err := zfs.ZFSGetFilesystemPlaceholderState(v.Path)
|
|
|
|
getLogger(ctx).
|
|
|
|
WithField("fs", v.Path.ToString()).
|
|
|
|
WithField("placeholder_state", fmt.Sprintf("%#v", ph)).
|
|
|
|
WithField("err", fmt.Sprintf("%s", err)).
|
|
|
|
WithField("errType", fmt.Sprintf("%T", err)).
|
|
|
|
Debug("placeholder state for filesystem")
|
2019-03-19 17:43:28 +01:00
|
|
|
if err != nil {
|
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-07-08 13:13:16 +02:00
|
|
|
|
2019-03-20 18:52:40 +01:00
|
|
|
if !ph.FSExists {
|
2019-09-07 20:01:15 +02:00
|
|
|
if s.rootWithoutClientComponent.HasPrefix(v.Path) {
|
|
|
|
if v.Path.Length() == 1 {
|
|
|
|
visitErr = fmt.Errorf("pool %q not imported", v.Path.ToString())
|
|
|
|
} else {
|
|
|
|
visitErr = fmt.Errorf("root_fs %q does not exist", s.rootWithoutClientComponent.ToString())
|
|
|
|
}
|
|
|
|
getLogger(ctx).WithError(visitErr).Error("placeholders are only created automatically below root_fs")
|
|
|
|
return false
|
|
|
|
}
|
2019-03-20 18:52:40 +01:00
|
|
|
l := getLogger(ctx).WithField("placeholder_fs", v.Path)
|
|
|
|
l.Debug("create placeholder filesystem")
|
|
|
|
err := zfs.ZFSCreatePlaceholderFilesystem(v.Path)
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Error("cannot create placeholder filesystem")
|
|
|
|
visitErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
getLogger(ctx).WithField("filesystem", v.Path.ToString()).Debug("exists")
|
|
|
|
return true // leave this fs as is
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
getLogger(ctx).WithField("visitErr", visitErr).Debug("complete tree-walk")
|
2018-06-20 20:20:37 +02:00
|
|
|
if visitErr != nil {
|
2019-03-16 14:46:07 +01:00
|
|
|
return nil, visitErr
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-19 17:43:28 +01:00
|
|
|
// determine whether we need to rollback the filesystem / change its placeholder state
|
2019-03-13 18:33:20 +01:00
|
|
|
var clearPlaceholderProperty bool
|
|
|
|
var recvOpts zfs.RecvOptions
|
2019-03-19 17:43:28 +01:00
|
|
|
ph, err := zfs.ZFSGetFilesystemPlaceholderState(lp)
|
|
|
|
if err == nil && ph.FSExists && ph.IsPlaceholder {
|
|
|
|
recvOpts.RollbackAndForceRecv = true
|
|
|
|
clearPlaceholderProperty = true
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2019-03-13 18:33:20 +01:00
|
|
|
if clearPlaceholderProperty {
|
2019-03-19 17:43:28 +01:00
|
|
|
if err := zfs.ZFSSetPlaceholder(lp, false); err != nil {
|
2019-03-13 18:33:20 +01:00
|
|
|
return nil, fmt.Errorf("cannot clear placeholder property for forced receive: %s", err)
|
|
|
|
}
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-28 21:22:22 +01:00
|
|
|
getLogger(ctx).Debug("acquire concurrent recv semaphore")
|
|
|
|
// TODO use try-acquire and fail with resource-exhaustion rpc status
|
|
|
|
// => would require handling on the client-side
|
|
|
|
// => this is a dataconn endpoint, doesn't have the status code semantics of gRPC
|
|
|
|
guard, err := maxConcurrentZFSRecvSemaphore.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer guard.Release()
|
|
|
|
|
2019-03-13 18:33:20 +01:00
|
|
|
getLogger(ctx).WithField("opts", fmt.Sprintf("%#v", recvOpts)).Debug("start receive command")
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2019-03-13 18:33:20 +01:00
|
|
|
if err := zfs.ZFSRecv(ctx, lp.ToString(), receive, recvOpts); err != nil {
|
2018-08-31 16:26:11 +02:00
|
|
|
getLogger(ctx).
|
|
|
|
WithError(err).
|
2019-03-13 18:33:20 +01:00
|
|
|
WithField("opts", recvOpts).
|
2018-08-31 16:26:11 +02:00
|
|
|
Error("zfs receive failed")
|
2018-12-11 22:01:50 +01:00
|
|
|
return nil, err
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
return &pdu.ReceiveRes{}, nil
|
2018-06-20 20:20:37 +02:00
|
|
|
}
|
2017-05-20 17:08:18 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (s *Receiver) DestroySnapshots(ctx context.Context, req *pdu.DestroySnapshotsReq) (*pdu.DestroySnapshotsRes, error) {
|
|
|
|
root := s.clientRootFromCtx(ctx)
|
|
|
|
lp, err := subroot{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) {
|
2019-08-12 01:19:08 +02:00
|
|
|
reqs := make([]*zfs.DestroySnapOp, len(snaps))
|
|
|
|
ress := make([]*pdu.DestroySnapshotRes, len(snaps))
|
|
|
|
errs := make([]error, len(snaps))
|
2018-08-30 11:51:47 +02:00
|
|
|
for i, fsv := range snaps {
|
|
|
|
if fsv.Type != pdu.FilesystemVersion_Snapshot {
|
|
|
|
return nil, fmt.Errorf("version %q is not a snapshot", fsv.Name)
|
|
|
|
}
|
2019-08-12 01:19:08 +02:00
|
|
|
ress[i] = &pdu.DestroySnapshotRes{
|
|
|
|
Snapshot: fsv,
|
|
|
|
// Error set after batch operation
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
2019-08-12 01:19:08 +02:00
|
|
|
reqs[i] = &zfs.DestroySnapOp{
|
|
|
|
Filesystem: lp.ToString(),
|
|
|
|
Name: fsv.Name,
|
|
|
|
ErrOut: &errs[i],
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
2019-08-12 01:19:08 +02:00
|
|
|
}
|
|
|
|
zfs.ZFSDestroyFilesystemVersions(reqs)
|
|
|
|
for i := range reqs {
|
|
|
|
if errs[i] != nil {
|
|
|
|
if de, ok := errs[i].(*zfs.DestroySnapshotsError); ok && len(de.Reason) == 1 {
|
|
|
|
ress[i].Error = de.Reason[0]
|
|
|
|
} else {
|
|
|
|
ress[i].Error = errs[i].Error()
|
|
|
|
}
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 01:19:08 +02:00
|
|
|
return &pdu.DestroySnapshotsRes{
|
|
|
|
Results: ress,
|
|
|
|
}, nil
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|