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"
|
|
|
|
"io"
|
|
|
|
"math/bits"
|
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Sender interface {
|
|
|
|
Send(ctx context.Context, r *pdu.SendReq) (*pdu.SendRes, io.ReadCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Receiver interface {
|
|
|
|
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
|
|
|
|
Status string
|
|
|
|
Problem string
|
|
|
|
}
|
|
|
|
|
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-08-22 00:10:09 +02:00
|
|
|
RetryWait
|
|
|
|
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-16 21:05:21 +02:00
|
|
|
idx := bits.TrailingZeros(uint(s))
|
|
|
|
if idx == bits.UintSize {
|
|
|
|
panic(s)
|
|
|
|
}
|
2018-08-22 00:10:09 +02:00
|
|
|
m := []state{
|
|
|
|
stateReady,
|
|
|
|
stateRetryWait,
|
2018-08-16 21:05:21 +02:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
return m[idx]
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type Replication struct {
|
2018-08-16 21:05:21 +02:00
|
|
|
// lock protects all fields in this struct, but not the data behind pointers
|
|
|
|
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
|
|
|
|
retryWaitUntil time.Time
|
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-08-22 00:10:09 +02:00
|
|
|
type ReplicationBuilder struct {
|
|
|
|
r *Replication
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func BuildReplication(fs string) *ReplicationBuilder {
|
|
|
|
return &ReplicationBuilder{&Replication{fs: fs}}
|
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{
|
|
|
|
state: StepReady,
|
|
|
|
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-25 21:30:25 +02:00
|
|
|
StepReady StepState = 1 << iota
|
2018-08-16 21:05:21 +02:00
|
|
|
StepRetry
|
|
|
|
StepPermanentError
|
|
|
|
StepCompleted
|
|
|
|
)
|
|
|
|
|
|
|
|
type FilesystemVersion interface {
|
|
|
|
SnapshotTime() time.Time
|
|
|
|
RelName() string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (f *Replication) TakeStep(ctx context.Context, 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-08-21 22:15:00 +02:00
|
|
|
s = s(ctx, sender, receiver, u)
|
2018-08-16 21:05:21 +02:00
|
|
|
delta := time.Now().Sub(preTime)
|
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")
|
|
|
|
|
|
|
|
return post, nextStepDate
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type updater func(func(fsr *Replication)) State
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
type state func(ctx context.Context, sender Sender, receiver Receiver, u updater) state
|
2018-08-16 21:05:21 +02:00
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func stateReady(ctx context.Context, 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-08-21 22:15:00 +02:00
|
|
|
stepState := current.do(ctx, 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
|
|
|
}
|
|
|
|
case StepRetry:
|
|
|
|
f.retryWaitUntil = time.Now().Add(10 * time.Second) // FIXME make configurable
|
2018-08-22 00:10:09 +02:00
|
|
|
f.state = RetryWait
|
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-08-22 00:10:09 +02:00
|
|
|
func stateRetryWait(ctx context.Context, sender Sender, receiver Receiver, u updater) state {
|
2018-08-16 21:05:21 +02:00
|
|
|
var sleepUntil time.Time
|
2018-08-22 00:10:09 +02:00
|
|
|
u(func(f *Replication) {
|
2018-08-16 21:05:21 +02:00
|
|
|
sleepUntil = f.retryWaitUntil
|
|
|
|
})
|
|
|
|
t := time.NewTimer(sleepUntil.Sub(time.Now()))
|
|
|
|
defer t.Stop()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-08-22 00:10:09 +02:00
|
|
|
return u(func(f *Replication) {
|
|
|
|
f.state = PermanentError
|
2018-08-16 21:05:21 +02:00
|
|
|
f.err = ctx.Err()
|
|
|
|
}).fsrsf()
|
|
|
|
case <-t.C:
|
|
|
|
}
|
2018-08-22 00:10:09 +02:00
|
|
|
return u(func(f *Replication) {
|
|
|
|
f.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()
|
|
|
|
}
|
|
|
|
return &rep
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (s *ReplicationStep) do(ctx context.Context, 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
|
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
fallthrough
|
|
|
|
case io.ErrUnexpectedEOF:
|
|
|
|
fallthrough
|
|
|
|
case io.ErrClosedPipe:
|
|
|
|
s.state = StepRetry
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
if _, ok := err.(net.Error); ok {
|
|
|
|
s.state = StepRetry
|
|
|
|
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
|
|
|
|
s.state = StepCompleted
|
|
|
|
return s.state
|
|
|
|
}
|
|
|
|
|
|
|
|
var sr *pdu.SendReq
|
|
|
|
if s.from == nil {
|
|
|
|
sr = &pdu.SendReq{
|
|
|
|
Filesystem: fs,
|
|
|
|
From: s.to.RelName(), // FIXME fix protocol to use To, like zfs does internally
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sr = &pdu.SendReq{
|
|
|
|
Filesystem: fs,
|
|
|
|
From: s.from.RelName(),
|
|
|
|
To: s.to.RelName(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("request", sr).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)
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := &pdu.ReceiveReq{
|
|
|
|
Filesystem: fs,
|
|
|
|
ClearResumeToken: !sres.UsedResumeToken,
|
|
|
|
}
|
|
|
|
log.WithField("request", rr).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 {
|
|
|
|
log.WithError(err).Error("receive request failed (might also be error on sender)")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
log.Info("receive finished")
|
|
|
|
return updateStateCompleted()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-22 00:10:09 +02:00
|
|
|
return fmt.Sprintf("%s(%s => %s)", s.parent.fs, s.from, s.to.RelName())
|
2018-08-16 21:05:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:09 +02:00
|
|
|
func (step *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
|
|
|
|
if step.from != nil {
|
|
|
|
from = step.from.RelName()
|
|
|
|
}
|
|
|
|
rep := StepReport{
|
|
|
|
From: from,
|
|
|
|
To: step.to.RelName(),
|
|
|
|
Status: step.state.String(),
|
|
|
|
}
|
|
|
|
return &rep
|
|
|
|
}
|