2018-08-22 00:10:09 +02:00
|
|
|
// Package fsrep implements replication of a single file system with existing versions
|
|
|
|
// from a sender to a receiver.
|
|
|
|
package fsrep
|
2018-08-16 21:05:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-09-08 07:03:41 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-10-19 16:27:05 +02:00
|
|
|
"github.com/zrepl/zrepl/util/watchdog"
|
2018-08-16 21:05:21 +02:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/replication/pdu"
|
2018-08-29 22:06:24 +02:00
|
|
|
"github.com/zrepl/zrepl/util"
|
2018-08-16 21:05:21 +02:00
|
|
|
)
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
contextKeyLogger contextKey = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger = logger.Logger
|
|
|
|
|
|
|
|
func WithLogger(ctx context.Context, log Logger) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyLogger, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogger(ctx context.Context) Logger {
|
|
|
|
l, ok := ctx.Value(contextKeyLogger).(Logger)
|
|
|
|
if !ok {
|
|
|
|
l = logger.NewNullLogger()
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:12:43 +02:00
|
|
|
// A Sender is usually part of a github.com/zrepl/zrepl/replication.Endpoint.
|
2018-08-22 00:10:09 +02:00
|
|
|
type Sender interface {
|
2018-08-26 15:12:43 +02:00
|
|
|
// If a non-nil io.ReadCloser is returned, it is guaranteed to be closed before
|
|
|
|
// any next call to the parent github.com/zrepl/zrepl/replication.Endpoint.
|
2018-08-30 12:52:08 +02:00
|
|
|
// If the send request is for dry run the io.ReadCloser will be nil
|
2018-08-22 00:10:09 +02:00
|
|
|
Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, io.ReadCloser, error)
|
2018-09-06 03:24:15 +02:00
|
|
|
ReplicationCursor(ctx context.Context, req *pdu.ReplicationCursorReq) (*pdu.ReplicationCursorRes, error)
|
2018-08-22 00:10:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 15:12:43 +02:00
|
|
|
// A Sender is usually part of a github.com/zrepl/zrepl/replication.Endpoint.
|
2018-08-22 00:10:09 +02:00
|
|
|
type Receiver interface {
|
2018-08-26 15:12:43 +02:00
|
|
|
// Receive sends r and sendStream (the latter containing a ZFS send stream)
|
|
|
|
// to the parent github.com/zrepl/zrepl/replication.Endpoint.
|
|
|
|
// Implementors must guarantee that Close was called on sendStream before
|
|
|
|
// the call to Receive returns.
|
2018-08-22 00:10:09 +02:00
|
|
|
Receive(ctx context.Context, r *pdu.ReceiveReq, sendStream io.ReadCloser) error
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
type StepReport struct {
|
|
|
|
From, To string
|
2018-08-30 14:07:28 +02:00
|
|
|
Status StepState
|
2018-08-16 21:05:21 +02:00
|
|
|
Problem string
|
2018-08-29 22:06:24 +02:00
|
|
|
Bytes int64
|
2018-09-06 02:41:25 +02:00
|
|
|
ExpectedBytes int64 // 0 means no size estimate possible
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type Report struct {
|
2018-08-25 21:30:25 +02:00
|
|
|
Filesystem string
|
|
|
|
Status string
|
|
|
|
Problem string
|
|
|
|
Completed, Pending []*StepReport
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
//go:generate stringer -type=State
|
|
|
|
type State uint
|
2018-08-16 21:05:21 +02:00
|
|
|
|
|
|
|
const (
|
2018-08-25 21:30:25 +02:00
|
|
|
Ready State = 1 << iota
|
2018-10-19 15:53:58 +02:00
|
|
|
Retry
|
2018-08-22 00:10:09 +02:00
|
|
|
PermanentError
|
|
|
|
Completed
|
2018-08-16 21:05:21 +02:00
|
|
|
)
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (s State) fsrsf() state {
|
2018-08-30 11:51:47 +02:00
|
|
|
m := map[State]state{
|
|
|
|
Ready: stateReady,
|
2018-10-19 15:53:58 +02:00
|
|
|
Retry: stateRetry,
|
2018-08-30 11:51:47 +02:00
|
|
|
PermanentError: nil,
|
|
|
|
Completed: nil,
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
2018-08-30 11:51:47 +02:00
|
|
|
return m[s]
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
func (s State) IsErrorState() bool {
|
|
|
|
return s & (Retry|PermanentError) != 0
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type Replication struct {
|
2018-09-08 07:03:41 +02:00
|
|
|
promBytesReplicated prometheus.Counter
|
|
|
|
|
|
|
|
// lock protects all fields below it in this struct, but not the data behind pointers
|
2018-08-16 21:05:21 +02:00
|
|
|
lock sync.Mutex
|
2018-08-22 00:10:09 +02:00
|
|
|
state State
|
2018-08-16 21:05:21 +02:00
|
|
|
fs string
|
|
|
|
err error
|
2018-08-22 00:10:09 +02:00
|
|
|
completed, pending []*ReplicationStep
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (f *Replication) State() State {
|
2018-08-16 21:05:21 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
return f.state
|
|
|
|
}
|
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
func (f *Replication) Err() error {
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
if f.state & (Retry|PermanentError) != 0 {
|
|
|
|
return f.err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:52:08 +02:00
|
|
|
func (f *Replication) UpdateSizeEsitmate(ctx context.Context, sender Sender) error {
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
for _, e := range f.pending {
|
|
|
|
if err := e.updateSizeEstimate(ctx, sender); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type ReplicationBuilder struct {
|
|
|
|
r *Replication
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 07:03:41 +02:00
|
|
|
func BuildReplication(fs string, promBytesReplicated prometheus.Counter) *ReplicationBuilder {
|
|
|
|
return &ReplicationBuilder{&Replication{fs: fs, promBytesReplicated: promBytesReplicated}}
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (b *ReplicationBuilder) AddStep(from, to FilesystemVersion) *ReplicationBuilder {
|
|
|
|
step := &ReplicationStep{
|
2018-08-30 11:51:47 +02:00
|
|
|
state: StepReplicationReady,
|
2018-08-22 00:10:09 +02:00
|
|
|
parent: b.r,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
b.r.pending = append(b.r.pending, step)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (b *ReplicationBuilder) Done() (r *Replication) {
|
2018-08-16 21:05:21 +02:00
|
|
|
if len(b.r.pending) > 0 {
|
2018-08-22 00:10:09 +02:00
|
|
|
b.r.state = Ready
|
2018-08-16 21:05:21 +02:00
|
|
|
} else {
|
2018-08-22 00:10:09 +02:00
|
|
|
b.r.state = Completed
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
2018-08-25 21:30:25 +02:00
|
|
|
r = b.r
|
2018-08-16 21:05:21 +02:00
|
|
|
b.r = nil
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func NewReplicationWithPermanentError(fs string, err error) *Replication {
|
|
|
|
return &Replication{
|
|
|
|
state: PermanentError,
|
2018-08-16 21:05:21 +02:00
|
|
|
fs: fs,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
//go:generate stringer -type=StepState
|
|
|
|
type StepState uint
|
2018-08-16 21:05:21 +02:00
|
|
|
|
|
|
|
const (
|
2018-08-30 11:51:47 +02:00
|
|
|
StepReplicationReady StepState = 1 << iota
|
|
|
|
StepReplicationRetry
|
|
|
|
StepMarkReplicatedReady
|
|
|
|
StepMarkReplicatedRetry
|
2018-08-16 21:05:21 +02:00
|
|
|
StepPermanentError
|
|
|
|
StepCompleted
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilesystemVersion interface {
|
|
|
|
SnapshotTime() time.Time
|
2018-08-30 11:51:47 +02:00
|
|
|
GetName() string // name without @ or #
|
|
|
|
RelName() string // name with @ or #
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type ReplicationStep struct {
|
2018-08-16 21:05:21 +02:00
|
|
|
// only protects state, err
|
2018-08-22 00:10:09 +02:00
|
|
|
// from, to and parent are assumed to be immutable
|
2018-08-16 21:05:21 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
state StepState
|
2018-08-16 21:05:21 +02:00
|
|
|
from, to FilesystemVersion
|
2018-08-22 00:10:09 +02:00
|
|
|
parent *Replication
|
2018-08-16 21:05:21 +02:00
|
|
|
|
|
|
|
// both retry and permanent error
|
2018-08-29 23:29:45 +02:00
|
|
|
err error
|
|
|
|
|
|
|
|
byteCounter *util.ByteCounterReader
|
2018-09-06 02:41:25 +02:00
|
|
|
expectedSize int64 // 0 means no size estimate present / possible
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
func (f *Replication) TakeStep(ctx context.Context, ka *watchdog.KeepAlive, sender Sender, receiver Receiver) (post State, nextStepDate time.Time) {
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
var u updater = func(fu func(*Replication)) State {
|
2018-08-16 21:05:21 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
if fu != nil {
|
|
|
|
fu(f)
|
|
|
|
}
|
|
|
|
return f.state
|
|
|
|
}
|
2018-08-22 00:10:09 +02:00
|
|
|
var s state = u(nil).fsrsf()
|
2018-08-16 21:05:21 +02:00
|
|
|
|
|
|
|
pre := u(nil)
|
|
|
|
preTime := time.Now()
|
2018-10-19 16:27:05 +02:00
|
|
|
s = s(ctx, ka, sender, receiver, u)
|
2018-08-16 21:05:21 +02:00
|
|
|
delta := time.Now().Sub(preTime)
|
2018-09-08 07:03:41 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
post = u(func(f *Replication) {
|
2018-08-16 21:05:21 +02:00
|
|
|
if len(f.pending) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nextStepDate = f.pending[0].to.SnapshotTime()
|
|
|
|
})
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
getLogger(ctx).
|
2018-08-16 21:05:21 +02:00
|
|
|
WithField("fs", f.fs).
|
|
|
|
WithField("transition", fmt.Sprintf("%s => %s", pre, post)).
|
|
|
|
WithField("duration", delta).
|
|
|
|
Debug("fsr step taken")
|
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
return post, nextStepDate
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type updater func(func(fsr *Replication)) State
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
type state func(ctx context.Context, ka *watchdog.KeepAlive, sender Sender, receiver Receiver, u updater) state
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
func stateReady(ctx context.Context, ka *watchdog.KeepAlive, sender Sender, receiver Receiver, u updater) state {
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
var current *ReplicationStep
|
|
|
|
s := u(func(f *Replication) {
|
2018-08-16 21:05:21 +02:00
|
|
|
if len(f.pending) == 0 {
|
2018-08-22 00:10:09 +02:00
|
|
|
f.state = Completed
|
2018-08-16 21:05:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
current = f.pending[0]
|
|
|
|
})
|
2018-08-22 00:10:09 +02:00
|
|
|
if s != Ready {
|
2018-08-16 21:05:21 +02:00
|
|
|
return s.fsrsf()
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
stepState := current.doReplication(ctx, ka, sender, receiver)
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
return u(func(f *Replication) {
|
2018-08-16 21:05:21 +02:00
|
|
|
switch stepState {
|
|
|
|
case StepCompleted:
|
|
|
|
f.completed = append(f.completed, current)
|
|
|
|
f.pending = f.pending[1:]
|
|
|
|
if len(f.pending) > 0 {
|
2018-08-22 00:10:09 +02:00
|
|
|
f.state = Ready
|
2018-08-16 21:05:21 +02:00
|
|
|
} else {
|
2018-08-22 00:10:09 +02:00
|
|
|
f.state = Completed
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
2018-08-30 11:51:47 +02:00
|
|
|
case StepReplicationRetry:
|
|
|
|
fallthrough
|
|
|
|
case StepMarkReplicatedRetry:
|
2018-10-19 15:53:58 +02:00
|
|
|
f.state = Retry
|
2018-08-16 21:05:21 +02:00
|
|
|
case StepPermanentError:
|
2018-08-22 00:10:09 +02:00
|
|
|
f.state = PermanentError
|
2018-08-16 21:05:21 +02:00
|
|
|
f.err = errors.New("a replication step failed with a permanent error")
|
|
|
|
default:
|
|
|
|
panic(f)
|
|
|
|
}
|
|
|
|
}).fsrsf()
|
|
|
|
}
|
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
func stateRetry(ctx context.Context, ka *watchdog.KeepAlive, sender Sender, receiver Receiver, u updater) state {
|
|
|
|
return u(func(fsr *Replication) {
|
|
|
|
fsr.state = Ready
|
2018-08-16 21:05:21 +02:00
|
|
|
}).fsrsf()
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (fsr *Replication) Report() *Report {
|
2018-08-16 21:05:21 +02:00
|
|
|
fsr.lock.Lock()
|
|
|
|
defer fsr.lock.Unlock()
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
rep := Report{
|
2018-08-16 21:05:21 +02:00
|
|
|
Filesystem: fsr.fs,
|
|
|
|
Status: fsr.state.String(),
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
if fsr.state&PermanentError != 0 {
|
2018-08-16 21:05:21 +02:00
|
|
|
rep.Problem = fsr.err.Error()
|
|
|
|
return &rep
|
|
|
|
}
|
|
|
|
|
|
|
|
rep.Completed = make([]*StepReport, len(fsr.completed))
|
|
|
|
for i := range fsr.completed {
|
|
|
|
rep.Completed[i] = fsr.completed[i].Report()
|
|
|
|
}
|
|
|
|
rep.Pending = make([]*StepReport, len(fsr.pending))
|
|
|
|
for i := range fsr.pending {
|
|
|
|
rep.Pending[i] = fsr.pending[i].Report()
|
|
|
|
}
|
2018-10-12 12:45:37 +02:00
|
|
|
|
2018-10-19 15:53:58 +02:00
|
|
|
if fsr.state&Retry != 0 {
|
|
|
|
if len(rep.Pending) != 0 { // should always be true for Retry == true?
|
2018-10-12 12:45:37 +02:00
|
|
|
rep.Problem = rep.Pending[0].Problem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
return &rep
|
|
|
|
}
|
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
func shouldRetry(err error) bool {
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
fallthrough
|
|
|
|
case io.ErrUnexpectedEOF:
|
|
|
|
fallthrough
|
|
|
|
case io.ErrClosedPipe:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
func (s *ReplicationStep) doReplication(ctx context.Context, ka *watchdog.KeepAlive, sender Sender, receiver Receiver) StepState {
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
fs := s.parent.fs
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
log := getLogger(ctx).
|
2018-08-16 21:05:21 +02:00
|
|
|
WithField("filesystem", fs).
|
|
|
|
WithField("step", s.String())
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
updateStateError := func(err error) StepState {
|
2018-08-16 21:05:21 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
s.err = err
|
2018-08-30 11:51:47 +02:00
|
|
|
if shouldRetry(s.err) {
|
|
|
|
s.state = StepReplicationRetry
|
2018-08-16 21:05:21 +02:00
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
s.state = StepPermanentError
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
updateStateCompleted := func() StepState {
|
2018-08-16 21:05:21 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.err = nil
|
2018-08-30 11:51:47 +02:00
|
|
|
s.state = StepMarkReplicatedReady
|
2018-08-16 21:05:21 +02:00
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:52:08 +02:00
|
|
|
sr := s.buildSendRequest(false)
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-31 16:26:11 +02:00
|
|
|
log.Debug("initiate send request")
|
2018-08-21 22:15:00 +02:00
|
|
|
sres, sstream, err := sender.Send(ctx, sr)
|
2018-08-16 21:05:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("send request failed")
|
|
|
|
return updateStateError(err)
|
|
|
|
}
|
|
|
|
if sstream == nil {
|
|
|
|
err := errors.New("send request did not return a stream, broken endpoint implementation")
|
|
|
|
return updateStateError(err)
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:06:24 +02:00
|
|
|
s.byteCounter = util.NewByteCounterReader(sstream)
|
2018-10-19 16:27:05 +02:00
|
|
|
s.byteCounter.SetCallback(1*time.Second, func(i int64) {
|
|
|
|
ka.MadeProgress()
|
|
|
|
})
|
2018-09-08 07:03:41 +02:00
|
|
|
defer func() {
|
|
|
|
s.parent.promBytesReplicated.Add(float64(s.byteCounter.Bytes()))
|
|
|
|
}()
|
2018-08-29 22:06:24 +02:00
|
|
|
sstream = s.byteCounter
|
|
|
|
|
2018-08-16 21:05:21 +02:00
|
|
|
rr := &pdu.ReceiveReq{
|
|
|
|
Filesystem: fs,
|
|
|
|
ClearResumeToken: !sres.UsedResumeToken,
|
|
|
|
}
|
2018-08-31 16:26:11 +02:00
|
|
|
log.Debug("initiate receive request")
|
2018-08-21 22:15:00 +02:00
|
|
|
err = receiver.Receive(ctx, rr, sstream)
|
2018-08-16 21:05:21 +02:00
|
|
|
if err != nil {
|
2018-08-31 16:26:11 +02:00
|
|
|
log.
|
|
|
|
WithError(err).
|
|
|
|
WithField("errType", fmt.Sprintf("%T", err)).
|
|
|
|
Error("receive request failed (might also be error on sender)")
|
2018-08-16 21:05:21 +02:00
|
|
|
sstream.Close()
|
|
|
|
// This failure could be due to
|
|
|
|
// - an unexpected exit of ZFS on the sending side
|
|
|
|
// - an unexpected exit of ZFS on the receiving side
|
|
|
|
// - a connectivity issue
|
|
|
|
return updateStateError(err)
|
|
|
|
}
|
2018-08-31 16:26:11 +02:00
|
|
|
log.Debug("receive finished")
|
2018-10-19 16:27:05 +02:00
|
|
|
ka.MadeProgress()
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-30 11:51:47 +02:00
|
|
|
updateStateCompleted()
|
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
return s.doMarkReplicated(ctx, ka, sender)
|
2018-08-30 11:51:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:27:05 +02:00
|
|
|
func (s *ReplicationStep) doMarkReplicated(ctx context.Context, ka *watchdog.KeepAlive, sender Sender) StepState {
|
2018-08-30 11:51:47 +02:00
|
|
|
|
|
|
|
log := getLogger(ctx).
|
|
|
|
WithField("filesystem", s.parent.fs).
|
|
|
|
WithField("step", s.String())
|
|
|
|
|
|
|
|
updateStateError := func(err error) StepState {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
s.err = err
|
|
|
|
if shouldRetry(s.err) {
|
|
|
|
s.state = StepMarkReplicatedRetry
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
s.state = StepPermanentError
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStateCompleted := func() StepState {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.state = StepCompleted
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
2018-09-06 03:24:15 +02:00
|
|
|
log.Debug("advance replication cursor")
|
|
|
|
req := &pdu.ReplicationCursorReq{
|
2018-08-30 11:51:47 +02:00
|
|
|
Filesystem: s.parent.fs,
|
2018-09-06 03:24:15 +02:00
|
|
|
Op: &pdu.ReplicationCursorReq_Set{
|
|
|
|
Set: &pdu.ReplicationCursorReq_SetOp{
|
|
|
|
Snapshot: s.to.GetName(),
|
|
|
|
},
|
|
|
|
},
|
2018-08-30 11:51:47 +02:00
|
|
|
}
|
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 {
|
2018-09-06 03:24:15 +02:00
|
|
|
log.WithError(err).Error("error advancing replication cursor")
|
2018-08-30 11:51:47 +02:00
|
|
|
return updateStateError(err)
|
|
|
|
}
|
2018-09-06 03:24:15 +02:00
|
|
|
if res.GetError() != "" {
|
|
|
|
err := fmt.Errorf("cannot advance replication cursor: %s", res.GetError())
|
2018-08-30 11:51:47 +02:00
|
|
|
log.Error(err.Error())
|
|
|
|
return updateStateError(err)
|
|
|
|
}
|
2018-10-19 16:27:05 +02:00
|
|
|
ka.MadeProgress()
|
2018-08-30 11:51:47 +02:00
|
|
|
|
|
|
|
return updateStateCompleted()
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:52:08 +02:00
|
|
|
func (s *ReplicationStep) updateSizeEstimate(ctx context.Context, sender Sender) error {
|
|
|
|
|
|
|
|
fs := s.parent.fs
|
|
|
|
|
|
|
|
log := getLogger(ctx).
|
|
|
|
WithField("filesystem", fs).
|
|
|
|
WithField("step", s.String())
|
|
|
|
|
|
|
|
sr := s.buildSendRequest(true)
|
|
|
|
|
2018-08-31 16:26:11 +02:00
|
|
|
log.Debug("initiate dry run send request")
|
2018-08-30 12:52:08 +02:00
|
|
|
sres, _, err := sender.Send(ctx, sr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("dry run send request failed")
|
2018-09-06 02:41:25 +02:00
|
|
|
return err
|
2018-08-30 12:52:08 +02:00
|
|
|
}
|
|
|
|
s.expectedSize = sres.ExpectedSize
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ReplicationStep) buildSendRequest(dryRun bool) (sr *pdu.SendReq) {
|
|
|
|
fs := s.parent.fs
|
|
|
|
if s.from == nil {
|
|
|
|
sr = &pdu.SendReq{
|
|
|
|
Filesystem: fs,
|
2018-10-14 13:06:23 +02:00
|
|
|
To: s.to.RelName(),
|
2018-08-30 12:52:08 +02:00
|
|
|
DryRun: dryRun,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sr = &pdu.SendReq{
|
|
|
|
Filesystem: fs,
|
|
|
|
From: s.from.RelName(),
|
|
|
|
To: s.to.RelName(),
|
|
|
|
DryRun: dryRun,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sr
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (s *ReplicationStep) String() string {
|
2018-08-16 21:05:21 +02:00
|
|
|
if s.from == nil { // FIXME: ZFS semantics are that to is nil on non-incremental send
|
2018-08-22 00:10:09 +02:00
|
|
|
return fmt.Sprintf("%s%s (full)", s.parent.fs, s.to.RelName())
|
2018-08-16 21:05:21 +02:00
|
|
|
} else {
|
2018-08-31 16:26:11 +02:00
|
|
|
return fmt.Sprintf("%s(%s => %s)", s.parent.fs, s.from.RelName(), s.to.RelName())
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:06:24 +02:00
|
|
|
func (s *ReplicationStep) Report() *StepReport {
|
2018-08-16 21:05:21 +02:00
|
|
|
var from string // FIXME follow same convention as ZFS: to should be nil on full send
|
2018-08-29 22:06:24 +02:00
|
|
|
if s.from != nil {
|
|
|
|
from = s.from.RelName()
|
|
|
|
}
|
|
|
|
bytes := int64(0)
|
|
|
|
if s.byteCounter != nil {
|
|
|
|
bytes = s.byteCounter.Bytes()
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
2018-10-12 12:45:37 +02:00
|
|
|
problem := ""
|
|
|
|
if s.err != nil {
|
|
|
|
problem = s.err.Error()
|
|
|
|
}
|
2018-08-16 21:05:21 +02:00
|
|
|
rep := StepReport{
|
|
|
|
From: from,
|
2018-08-29 22:06:24 +02:00
|
|
|
To: s.to.RelName(),
|
2018-08-30 14:07:28 +02:00
|
|
|
Status: s.state,
|
2018-10-12 12:45:37 +02:00
|
|
|
Problem: problem,
|
2018-08-29 22:06:24 +02:00
|
|
|
Bytes: bytes,
|
2018-08-29 23:29:45 +02:00
|
|
|
ExpectedBytes: s.expectedSize,
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
return &rep
|
|
|
|
}
|