2018-12-11 22:01:50 +01:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-11 13:46:36 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-10 21:58:28 +02:00
|
|
|
"io"
|
2018-12-11 22:01:50 +01:00
|
|
|
"net"
|
2019-03-11 13:46:36 +01:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2018-12-11 22:01:50 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2020-08-31 16:04:00 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon/logging/trace"
|
|
|
|
|
2019-03-11 13:46:36 +01:00
|
|
|
"github.com/google/uuid"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2019-02-22 11:40:27 +01:00
|
|
|
"github.com/zrepl/zrepl/replication/logic"
|
|
|
|
"github.com/zrepl/zrepl/replication/logic/pdu"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/rpc/dataconn"
|
|
|
|
"github.com/zrepl/zrepl/rpc/grpcclientidentity/grpchelper"
|
|
|
|
"github.com/zrepl/zrepl/rpc/versionhandshake"
|
|
|
|
"github.com/zrepl/zrepl/transport"
|
|
|
|
"github.com/zrepl/zrepl/util/envconst"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client implements the active side of a replication setup.
|
|
|
|
// It satisfies the Endpoint, Sender and Receiver interface defined by package replication.
|
|
|
|
type Client struct {
|
|
|
|
dataClient *dataconn.Client
|
|
|
|
controlClient pdu.ReplicationClient // this the grpc client instance, see constructor
|
|
|
|
controlConn *grpc.ClientConn
|
|
|
|
loggers Loggers
|
2019-03-15 16:16:56 +01:00
|
|
|
closed chan struct{}
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 11:40:27 +01:00
|
|
|
var _ logic.Endpoint = &Client{}
|
|
|
|
var _ logic.Sender = &Client{}
|
|
|
|
var _ logic.Receiver = &Client{}
|
2018-12-11 22:01:50 +01:00
|
|
|
|
|
|
|
type DialContextFunc = func(ctx context.Context, network string, addr string) (net.Conn, error)
|
|
|
|
|
|
|
|
// config must be validated, NewClient will panic if it is not valid
|
|
|
|
func NewClient(cn transport.Connecter, loggers Loggers) *Client {
|
|
|
|
|
|
|
|
cn = versionhandshake.Connecter(cn, envconst.Duration("ZREPL_RPC_CLIENT_VERSIONHANDSHAKE_TIMEOUT", 10*time.Second))
|
|
|
|
|
|
|
|
muxedConnecter := mux(cn)
|
|
|
|
|
|
|
|
c := &Client{
|
|
|
|
loggers: loggers,
|
2019-03-15 16:16:56 +01:00
|
|
|
closed: make(chan struct{}),
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
grpcConn := grpchelper.ClientConn(muxedConnecter.control, loggers.Control)
|
|
|
|
|
|
|
|
go func() {
|
2019-03-15 16:16:56 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
<-c.closed
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
defer cancel()
|
|
|
|
for ctx.Err() == nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
state := grpcConn.GetState()
|
|
|
|
loggers.General.WithField("grpc_state", state.String()).Debug("grpc state change")
|
2019-03-15 16:16:56 +01:00
|
|
|
grpcConn.WaitForStateChange(ctx, state)
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
c.controlClient = pdu.NewReplicationClient(grpcConn)
|
|
|
|
c.controlConn = grpcConn
|
|
|
|
|
|
|
|
c.dataClient = dataconn.NewClient(muxedConnecter.data, loggers.Data)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() {
|
2019-03-15 16:16:56 +01:00
|
|
|
close(c.closed)
|
2018-12-11 22:01:50 +01:00
|
|
|
if err := c.controlConn.Close(); err != nil {
|
2019-03-15 16:16:56 +01:00
|
|
|
c.loggers.General.WithError(err).Error("cannot close control connection")
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
// TODO c.dataClient should have Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// callers must ensure that the returned io.ReadCloser is closed
|
|
|
|
// TODO expose dataClient interface to the outside world
|
2020-04-10 21:58:28 +02:00
|
|
|
func (c *Client) Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, io.ReadCloser, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.Send")
|
|
|
|
defer endSpan()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
// TODO the returned sendStream may return a read error created by the remote side
|
2020-04-10 21:58:28 +02:00
|
|
|
res, stream, err := c.dataClient.ReqSend(ctx, r)
|
2018-12-11 22:01:50 +01:00
|
|
|
if err != nil {
|
2019-03-28 21:53:14 +01:00
|
|
|
return nil, nil, err
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
2020-04-10 21:58:28 +02:00
|
|
|
if stream == nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
return res, nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:58:28 +02:00
|
|
|
return res, stream, nil
|
2018-12-11 22:01:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-10 21:58:28 +02:00
|
|
|
func (c *Client) Receive(ctx context.Context, req *pdu.ReceiveReq, stream io.ReadCloser) (*pdu.ReceiveRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.Receive")
|
|
|
|
defer endSpan()
|
|
|
|
|
2020-04-10 21:58:28 +02:00
|
|
|
return c.dataClient.ReqRecv(ctx, req, stream)
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
|
|
|
|
2021-08-16 10:11:37 +02:00
|
|
|
func (c *Client) SendDry(ctx context.Context, in *pdu.SendReq) (*pdu.SendRes, error) {
|
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.SendDry")
|
|
|
|
defer endSpan()
|
|
|
|
|
|
|
|
return c.controlClient.SendDry(ctx, in)
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (c *Client) ListFilesystems(ctx context.Context, in *pdu.ListFilesystemReq) (*pdu.ListFilesystemRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.ListFilesystems")
|
|
|
|
defer endSpan()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return c.controlClient.ListFilesystems(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ListFilesystemVersions(ctx context.Context, in *pdu.ListFilesystemVersionsReq) (*pdu.ListFilesystemVersionsRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.ListFilesystemVersions")
|
|
|
|
defer endSpan()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return c.controlClient.ListFilesystemVersions(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DestroySnapshots(ctx context.Context, in *pdu.DestroySnapshotsReq) (*pdu.DestroySnapshotsRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.DestroySnapshots")
|
|
|
|
defer endSpan()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return c.controlClient.DestroySnapshots(ctx, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ReplicationCursor(ctx context.Context, in *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.ReplicationCursor")
|
|
|
|
defer endSpan()
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return c.controlClient.ReplicationCursor(ctx, in)
|
|
|
|
}
|
|
|
|
|
new features: {resumable,encrypted,hold-protected} send-recv, last-received-hold
- **Resumable Send & Recv Support**
No knobs required, automatically used where supported.
- **Hold-Protected Send & Recv**
Automatic ZFS holds to ensure that we can always resume a replication step.
- **Encrypted Send & Recv Support** for OpenZFS native encryption.
Configurable at the job level, i.e., for all filesystems a job is responsible for.
- **Receive-side hold on last received dataset**
The counterpart to the replication cursor bookmark on the send-side.
Ensures that incremental replication will always be possible between a sender and receiver.
Design Doc
----------
`replication/design.md` doc describes how we use ZFS holds and bookmarks to ensure that a single replication step is always resumable.
The replication algorithm described in the design doc introduces the notion of job IDs (please read the details on this design doc).
We reuse the job names for job IDs and use `JobID` type to ensure that a job name can be embedded into hold tags, bookmark names, etc.
This might BREAK CONFIG on upgrade.
Protocol Version Bump
---------------------
This commit makes backwards-incompatible changes to the replication/pdu protobufs.
Thus, bump the version number used in the protocol handshake.
Replication Cursor Format Change
--------------------------------
The new replication cursor bookmark format is: `#zrepl_CURSOR_G_${this.GUID}_J_${jobid}`
Including the GUID enables transaction-safe moving-forward of the cursor.
Including the job id enables that multiple sending jobs can send the same filesystem without interfering.
The `zrepl migrate replication-cursor:v1-v2` subcommand can be used to safely destroy old-format cursors once zrepl has created new-format cursors.
Changes in This Commit
----------------------
- package zfs
- infrastructure for holds
- infrastructure for resume token decoding
- implement a variant of OpenZFS's `entity_namecheck` and use it for validation in new code
- ZFSSendArgs to specify a ZFS send operation
- validation code protects against malicious resume tokens by checking that the token encodes the same send parameters that the send-side would use if no resume token were available (i.e. same filesystem, `fromguid`, `toguid`)
- RecvOptions support for `recv -s` flag
- convert a bunch of ZFS operations to be idempotent
- achieved through more differentiated error message scraping / additional pre-/post-checks
- package replication/pdu
- add field for encryption to send request messages
- add fields for resume handling to send & recv request messages
- receive requests now contain `FilesystemVersion To` in addition to the filesystem into which the stream should be `recv`d into
- can use `zfs recv $root_fs/$client_id/path/to/dataset@${To.Name}`, which enables additional validation after recv (i.e. whether `To.Guid` matched what we received in the stream)
- used to set `last-received-hold`
- package replication/logic
- introduce `PlannerPolicy` struct, currently only used to configure whether encrypted sends should be requested from the sender
- integrate encryption and resume token support into `Step` struct
- package endpoint
- move the concepts that endpoint builds on top of ZFS to a single file `endpoint/endpoint_zfs.go`
- step-holds + step-bookmarks
- last-received-hold
- new replication cursor + old replication cursor compat code
- adjust `endpoint/endpoint.go` handlers for
- encryption
- resumability
- new replication cursor
- last-received-hold
- client subcommand `zrepl holds list`: list all holds and hold-like bookmarks that zrepl thinks belong to it
- client subcommand `zrepl migrate replication-cursor:v1-v2`
2019-09-11 17:19:17 +02:00
|
|
|
func (c *Client) SendCompleted(ctx context.Context, in *pdu.SendCompletedReq) (*pdu.SendCompletedRes, error) {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.SendCompleted")
|
|
|
|
defer endSpan()
|
|
|
|
|
new features: {resumable,encrypted,hold-protected} send-recv, last-received-hold
- **Resumable Send & Recv Support**
No knobs required, automatically used where supported.
- **Hold-Protected Send & Recv**
Automatic ZFS holds to ensure that we can always resume a replication step.
- **Encrypted Send & Recv Support** for OpenZFS native encryption.
Configurable at the job level, i.e., for all filesystems a job is responsible for.
- **Receive-side hold on last received dataset**
The counterpart to the replication cursor bookmark on the send-side.
Ensures that incremental replication will always be possible between a sender and receiver.
Design Doc
----------
`replication/design.md` doc describes how we use ZFS holds and bookmarks to ensure that a single replication step is always resumable.
The replication algorithm described in the design doc introduces the notion of job IDs (please read the details on this design doc).
We reuse the job names for job IDs and use `JobID` type to ensure that a job name can be embedded into hold tags, bookmark names, etc.
This might BREAK CONFIG on upgrade.
Protocol Version Bump
---------------------
This commit makes backwards-incompatible changes to the replication/pdu protobufs.
Thus, bump the version number used in the protocol handshake.
Replication Cursor Format Change
--------------------------------
The new replication cursor bookmark format is: `#zrepl_CURSOR_G_${this.GUID}_J_${jobid}`
Including the GUID enables transaction-safe moving-forward of the cursor.
Including the job id enables that multiple sending jobs can send the same filesystem without interfering.
The `zrepl migrate replication-cursor:v1-v2` subcommand can be used to safely destroy old-format cursors once zrepl has created new-format cursors.
Changes in This Commit
----------------------
- package zfs
- infrastructure for holds
- infrastructure for resume token decoding
- implement a variant of OpenZFS's `entity_namecheck` and use it for validation in new code
- ZFSSendArgs to specify a ZFS send operation
- validation code protects against malicious resume tokens by checking that the token encodes the same send parameters that the send-side would use if no resume token were available (i.e. same filesystem, `fromguid`, `toguid`)
- RecvOptions support for `recv -s` flag
- convert a bunch of ZFS operations to be idempotent
- achieved through more differentiated error message scraping / additional pre-/post-checks
- package replication/pdu
- add field for encryption to send request messages
- add fields for resume handling to send & recv request messages
- receive requests now contain `FilesystemVersion To` in addition to the filesystem into which the stream should be `recv`d into
- can use `zfs recv $root_fs/$client_id/path/to/dataset@${To.Name}`, which enables additional validation after recv (i.e. whether `To.Guid` matched what we received in the stream)
- used to set `last-received-hold`
- package replication/logic
- introduce `PlannerPolicy` struct, currently only used to configure whether encrypted sends should be requested from the sender
- integrate encryption and resume token support into `Step` struct
- package endpoint
- move the concepts that endpoint builds on top of ZFS to a single file `endpoint/endpoint_zfs.go`
- step-holds + step-bookmarks
- last-received-hold
- new replication cursor + old replication cursor compat code
- adjust `endpoint/endpoint.go` handlers for
- encryption
- resumability
- new replication cursor
- last-received-hold
- client subcommand `zrepl holds list`: list all holds and hold-like bookmarks that zrepl thinks belong to it
- client subcommand `zrepl migrate replication-cursor:v1-v2`
2019-09-11 17:19:17 +02:00
|
|
|
return c.controlClient.SendCompleted(ctx, in)
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:46:36 +01:00
|
|
|
func (c *Client) WaitForConnectivity(ctx context.Context) error {
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx, endSpan := trace.WithSpan(ctx, "rpc.client.WaitForConnectivity")
|
|
|
|
defer endSpan()
|
|
|
|
|
2019-03-11 13:46:36 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
msg := uuid.New().String()
|
|
|
|
req := pdu.PingReq{Message: msg}
|
|
|
|
var ctrlOk, dataOk int32
|
|
|
|
loggers := GetLoggersOrPanic(ctx)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
checkRes := func(res *pdu.PingRes, err error, logger Logger, okVar *int32) {
|
|
|
|
if err == nil && res.GetEcho() != req.GetMessage() {
|
|
|
|
err = errors.New("pilot message not echoed correctly")
|
|
|
|
}
|
|
|
|
if err == context.Canceled {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("ping failed")
|
|
|
|
atomic.StoreInt32(okVar, 0)
|
|
|
|
cancel()
|
|
|
|
} else {
|
|
|
|
atomic.StoreInt32(okVar, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-24 23:31:45 +01:00
|
|
|
ctrl, ctrlErr := c.controlClient.Ping(ctx, &req, grpc.WaitForReady(true))
|
2019-03-11 13:46:36 +01:00
|
|
|
checkRes(ctrl, ctrlErr, loggers.Control, &ctrlOk)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
data, dataErr := c.dataClient.ReqPing(ctx, &req)
|
2021-01-24 23:31:45 +01:00
|
|
|
// dataClient uses transport.Connecter, which doesn't expose WaitForReady(true)
|
2019-03-11 13:46:36 +01:00
|
|
|
// => we need to mask dial timeouts
|
2022-10-23 18:18:07 +02:00
|
|
|
if err, ok := dataErr.(interface{ Timeout() bool }); ok && err.Timeout() {
|
|
|
|
// Rate-limit pings here in case Timeout() == true is a mis-classification
|
2019-03-15 15:59:47 +01:00
|
|
|
// or returns immediately (this is a tight loop in that case)
|
|
|
|
// TODO keep this in lockstep with controlClient
|
|
|
|
// => don't use FailFast for control, but check that both control and data worked
|
|
|
|
time.Sleep(envconst.Duration("ZREPL_RPC_DATACONN_PING_SLEEP", 1*time.Second))
|
2019-03-11 13:46:36 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-03-22 19:41:12 +01:00
|
|
|
// it's not a dial timeout,
|
2019-03-11 13:46:36 +01:00
|
|
|
checkRes(data, dataErr, loggers.Data, &dataOk)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
var what string
|
|
|
|
if ctrlOk == 1 && dataOk == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ctrlOk == 0 {
|
|
|
|
what += "control"
|
|
|
|
}
|
|
|
|
if dataOk == 0 {
|
|
|
|
if len(what) > 0 {
|
|
|
|
what += " and data"
|
|
|
|
} else {
|
|
|
|
what += "data"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s rpc failed to respond to ping rpcs", what)
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (c *Client) ResetConnectBackoff() {
|
|
|
|
c.controlConn.ResetConnectBackoff()
|
|
|
|
}
|