mirror of
https://github.com/zrepl/zrepl.git
synced 2025-02-22 05:11:06 +01:00
remove EndpointPair abstraction
This commit is contained in:
parent
38532abf45
commit
2f205d205b
@ -43,49 +43,3 @@ func NewFilteredError(fs string) *FilteredError {
|
||||
}
|
||||
|
||||
func (f FilteredError) Error() string { return "endpoint does not allow access to filesystem " + f.fs }
|
||||
|
||||
type ReplicationMode int
|
||||
|
||||
const (
|
||||
ReplicationModePull ReplicationMode = iota
|
||||
ReplicationModePush
|
||||
)
|
||||
|
||||
type EndpointPair struct {
|
||||
a, b ReplicationEndpoint
|
||||
m ReplicationMode
|
||||
}
|
||||
|
||||
func NewEndpointPairPull(sender, receiver ReplicationEndpoint) EndpointPair {
|
||||
return EndpointPair{sender, receiver, ReplicationModePull}
|
||||
}
|
||||
|
||||
func NewEndpointPairPush(sender, receiver ReplicationEndpoint) EndpointPair {
|
||||
return EndpointPair{receiver, sender, ReplicationModePush}
|
||||
}
|
||||
|
||||
func (p EndpointPair) Sender() ReplicationEndpoint {
|
||||
switch p.m {
|
||||
case ReplicationModePull:
|
||||
return p.a
|
||||
case ReplicationModePush:
|
||||
return p.b
|
||||
}
|
||||
panic("should not be reached")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p EndpointPair) Receiver() ReplicationEndpoint {
|
||||
switch p.m {
|
||||
case ReplicationModePull:
|
||||
return p.b
|
||||
case ReplicationModePush:
|
||||
return p.a
|
||||
}
|
||||
panic("should not be reached")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p EndpointPair) Mode() ReplicationMode {
|
||||
return p.m
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ type FSReplicationStep struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *FSReplication) TakeStep(ctx context.Context, ep EndpointPair) (post FSReplicationState, nextStepDate time.Time) {
|
||||
func (f *FSReplication) TakeStep(ctx context.Context, sender, receiver ReplicationEndpoint) (post FSReplicationState, nextStepDate time.Time) {
|
||||
|
||||
var u fsrUpdater = func(fu func(*FSReplication)) FSReplicationState {
|
||||
f.lock.Lock()
|
||||
@ -149,7 +149,7 @@ func (f *FSReplication) TakeStep(ctx context.Context, ep EndpointPair) (post FSR
|
||||
|
||||
pre := u(nil)
|
||||
preTime := time.Now()
|
||||
s = s(ctx, ep, u)
|
||||
s = s(ctx, sender, receiver, u)
|
||||
delta := time.Now().Sub(preTime)
|
||||
post = u(func(f *FSReplication) {
|
||||
if len(f.pending) == 0 {
|
||||
@ -169,9 +169,9 @@ func (f *FSReplication) TakeStep(ctx context.Context, ep EndpointPair) (post FSR
|
||||
|
||||
type fsrUpdater func(func(fsr *FSReplication)) FSReplicationState
|
||||
|
||||
type fsrsf func(ctx context.Context, ep EndpointPair, u fsrUpdater) fsrsf
|
||||
type fsrsf func(ctx context.Context, sender, receiver ReplicationEndpoint, u fsrUpdater) fsrsf
|
||||
|
||||
func fsrsfReady(ctx context.Context, ep EndpointPair, u fsrUpdater) fsrsf {
|
||||
func fsrsfReady(ctx context.Context, sender, receiver ReplicationEndpoint, u fsrUpdater) fsrsf {
|
||||
|
||||
var current *FSReplicationStep
|
||||
s := u(func(f *FSReplication) {
|
||||
@ -185,7 +185,7 @@ func fsrsfReady(ctx context.Context, ep EndpointPair, u fsrUpdater) fsrsf {
|
||||
return s.fsrsf()
|
||||
}
|
||||
|
||||
stepState := current.do(ctx, ep)
|
||||
stepState := current.do(ctx, sender, receiver)
|
||||
|
||||
return u(func(f *FSReplication) {
|
||||
switch stepState {
|
||||
@ -209,7 +209,7 @@ func fsrsfReady(ctx context.Context, ep EndpointPair, u fsrUpdater) fsrsf {
|
||||
}).fsrsf()
|
||||
}
|
||||
|
||||
func fsrsfRetryWait(ctx context.Context, ep EndpointPair, u fsrUpdater) fsrsf {
|
||||
func fsrsfRetryWait(ctx context.Context, sender, receiver ReplicationEndpoint, u fsrUpdater) fsrsf {
|
||||
var sleepUntil time.Time
|
||||
u(func(f *FSReplication) {
|
||||
sleepUntil = f.retryWaitUntil
|
||||
@ -255,7 +255,7 @@ func (fsr *FSReplication) Report() *FilesystemReplicationReport {
|
||||
return &rep
|
||||
}
|
||||
|
||||
func (s *FSReplicationStep) do(ctx context.Context, ep EndpointPair) FSReplicationStepState {
|
||||
func (s *FSReplicationStep) do(ctx context.Context, sender, receiver ReplicationEndpoint) FSReplicationStepState {
|
||||
|
||||
fs := s.fsrep.fs
|
||||
|
||||
@ -308,7 +308,7 @@ func (s *FSReplicationStep) do(ctx context.Context, ep EndpointPair) FSReplicati
|
||||
}
|
||||
|
||||
log.WithField("request", sr).Debug("initiate send request")
|
||||
sres, sstream, err := ep.Sender().Send(ctx, sr)
|
||||
sres, sstream, err := sender.Send(ctx, sr)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("send request failed")
|
||||
return updateStateError(err)
|
||||
@ -323,7 +323,7 @@ func (s *FSReplicationStep) do(ctx context.Context, ep EndpointPair) FSReplicati
|
||||
ClearResumeToken: !sres.UsedResumeToken,
|
||||
}
|
||||
log.WithField("request", rr).Debug("initiate receive request")
|
||||
err = ep.Receiver().Receive(ctx, rr, sstream)
|
||||
err = receiver.Receive(ctx, rr, sstream)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("receive request failed (might also be error on sender)")
|
||||
sstream.Close()
|
||||
|
@ -80,9 +80,9 @@ func NewReplication() *Replication {
|
||||
}
|
||||
|
||||
type replicationUpdater func(func(*Replication)) (newState ReplicationState)
|
||||
type replicationStateFunc func(context.Context, EndpointPair, replicationUpdater) replicationStateFunc
|
||||
type replicationStateFunc func(ctx context.Context, sender, receiver ReplicationEndpoint, u replicationUpdater) replicationStateFunc
|
||||
|
||||
func (r *Replication) Drive(ctx context.Context, ep EndpointPair) {
|
||||
func (r *Replication) Drive(ctx context.Context, sender, receiver ReplicationEndpoint) {
|
||||
|
||||
var u replicationUpdater = func(f func(*Replication)) ReplicationState {
|
||||
r.lock.Lock()
|
||||
@ -98,7 +98,7 @@ func (r *Replication) Drive(ctx context.Context, ep EndpointPair) {
|
||||
for s != nil {
|
||||
preTime := time.Now()
|
||||
pre = u(nil)
|
||||
s = s(ctx, ep, u)
|
||||
s = s(ctx, sender, receiver, u)
|
||||
delta := time.Now().Sub(preTime)
|
||||
post = u(nil)
|
||||
GetLogger(ctx).
|
||||
@ -133,7 +133,7 @@ func resolveConflict(conflict error) (path []*pdu.FilesystemVersion, msg string)
|
||||
return nil, "no automated way to handle conflict type"
|
||||
}
|
||||
|
||||
func rsfPlanning(ctx context.Context, ep EndpointPair, u replicationUpdater) replicationStateFunc {
|
||||
func rsfPlanning(ctx context.Context, sender, receiver ReplicationEndpoint, u replicationUpdater) replicationStateFunc {
|
||||
|
||||
log := GetLogger(ctx)
|
||||
|
||||
@ -144,13 +144,13 @@ func rsfPlanning(ctx context.Context, ep EndpointPair, u replicationUpdater) rep
|
||||
}).rsf()
|
||||
}
|
||||
|
||||
sfss, err := ep.Sender().ListFilesystems(ctx)
|
||||
sfss, err := sender.ListFilesystems(ctx)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error listing sender filesystems")
|
||||
return handlePlanningError(err)
|
||||
}
|
||||
|
||||
rfss, err := ep.Receiver().ListFilesystems(ctx)
|
||||
rfss, err := receiver.ListFilesystems(ctx)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error listing receiver filesystems")
|
||||
return handlePlanningError(err)
|
||||
@ -164,7 +164,7 @@ func rsfPlanning(ctx context.Context, ep EndpointPair, u replicationUpdater) rep
|
||||
|
||||
log.Info("assessing filesystem")
|
||||
|
||||
sfsvs, err := ep.Sender().ListFilesystemVersions(ctx, fs.Path)
|
||||
sfsvs, err := sender.ListFilesystemVersions(ctx, fs.Path)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("cannot get remote filesystem versions")
|
||||
return handlePlanningError(err)
|
||||
@ -186,7 +186,7 @@ func rsfPlanning(ctx context.Context, ep EndpointPair, u replicationUpdater) rep
|
||||
|
||||
var rfsvs []*pdu.FilesystemVersion
|
||||
if receiverFSExists {
|
||||
rfsvs, err = ep.Receiver().ListFilesystemVersions(ctx, fs.Path)
|
||||
rfsvs, err = receiver.ListFilesystemVersions(ctx, fs.Path)
|
||||
if err != nil {
|
||||
if _, ok := err.(*FilteredError); ok {
|
||||
log.Info("receiver ignores filesystem")
|
||||
@ -236,7 +236,7 @@ func rsfPlanning(ctx context.Context, ep EndpointPair, u replicationUpdater) rep
|
||||
}).rsf()
|
||||
}
|
||||
|
||||
func rsfPlanningError(ctx context.Context, ep EndpointPair, u replicationUpdater) replicationStateFunc {
|
||||
func rsfPlanningError(ctx context.Context, sender, receiver ReplicationEndpoint, u replicationUpdater) replicationStateFunc {
|
||||
sleepTime := 10 * time.Second
|
||||
u(func(r *Replication) {
|
||||
r.sleepUntil = time.Now().Add(sleepTime)
|
||||
@ -256,7 +256,7 @@ func rsfPlanningError(ctx context.Context, ep EndpointPair, u replicationUpdater
|
||||
}
|
||||
}
|
||||
|
||||
func rsfWorking(ctx context.Context, ep EndpointPair, u replicationUpdater) replicationStateFunc {
|
||||
func rsfWorking(ctx context.Context, sender, receiver ReplicationEndpoint, u replicationUpdater) replicationStateFunc {
|
||||
|
||||
var active *ReplicationQueueItemHandle
|
||||
rsfNext := u(func(r *Replication) {
|
||||
@ -273,7 +273,7 @@ func rsfWorking(ctx context.Context, ep EndpointPair, u replicationUpdater) repl
|
||||
return rsfNext
|
||||
}
|
||||
|
||||
state, nextStepDate := active.GetFSReplication().TakeStep(ctx, ep)
|
||||
state, nextStepDate := active.GetFSReplication().TakeStep(ctx, sender, receiver)
|
||||
|
||||
return u(func(r *Replication) {
|
||||
active.Update(state, nextStepDate)
|
||||
@ -281,7 +281,7 @@ func rsfWorking(ctx context.Context, ep EndpointPair, u replicationUpdater) repl
|
||||
}).rsf()
|
||||
}
|
||||
|
||||
func rsfWorkingWait(ctx context.Context, ep EndpointPair, u replicationUpdater) replicationStateFunc {
|
||||
func rsfWorkingWait(ctx context.Context, sender, receiver ReplicationEndpoint, u replicationUpdater) replicationStateFunc {
|
||||
sleepTime := 10 * time.Second
|
||||
u(func(r *Replication) {
|
||||
r.sleepUntil = time.Now().Add(sleepTime)
|
||||
@ -327,14 +327,14 @@ func (r *Replication) Report() *Report {
|
||||
active = r.active.GetFSReplication()
|
||||
rep.Active = active.Report()
|
||||
}
|
||||
r.queue.Foreach(func (h *ReplicationQueueItemHandle){
|
||||
r.queue.Foreach(func(h *ReplicationQueueItemHandle) {
|
||||
fsr := h.GetFSReplication()
|
||||
if active != fsr {
|
||||
rep.Pending = append(rep.Pending, fsr.Report())
|
||||
}
|
||||
})
|
||||
for _, fsr := range r.completed {
|
||||
rep.Completed = append(rep.Completed, fsr.Report())
|
||||
rep.Completed = append(rep.Completed, fsr.Report())
|
||||
}
|
||||
|
||||
return &rep
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
type Report = mainfsm.Report
|
||||
|
||||
type Replication interface {
|
||||
Drive(ctx context.Context, ep common.EndpointPair)
|
||||
Drive(ctx context.Context, sender, receiver common.ReplicationEndpoint)
|
||||
Report() *Report
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user