replication

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package replication distributes heartbeats between voters outside of Raft (data class B, ADR 0006, ADR 0012 B2/B3/B6). Pushes are best effort; the anti-entropy pass against every peer is what guarantees completeness.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	NodeID  string
	Metrics store.Metrics
	// Peers returns the current voters except this node (Raft configuration).
	Peers func() []Peer
	// Dial opens the connection that a peer worker owns (one per peer).
	Dial func(address string) (*grpc.ClientConn, error)
	Log  *slog.Logger
	// Now returns the current time. Default time.Now.
	Now func() time.Time
	// AntiEntropyInterval is the period of the anti-entropy pass per peer. Default 30s.
	AntiEntropyInterval time.Duration
	// PeerRefresh is the period of the peer set refresh. Default 5s.
	PeerRefresh time.Duration
	// PageSize is the maximum number of heartbeats per FetchRange. Default 5000.
	PageSize int
	// PushBuffer is the number of batches queued per peer. Default 64.
	PushBuffer int
	// OnStored is called with the heartbeats of every anti-entropy page after
	// they were stored (ADR 0021). It must not block, and the slice is only valid
	// during the call. It may be nil.
	OnStored func([]store.Heartbeat)
	// contains filtered or unexported fields
}

Options configure a Replicator. NodeID, Metrics, Peers and Dial are required.

type Peer

type Peer struct {
	ID      string
	Address string
}

Peer is another voter of the cluster.

type PeerStatus

type PeerStatus struct {
	ID      string
	Address string
	// Connected reports whether the push stream is open.
	Connected bool
	// CaughtUpAt is the start of the last anti-entropy round that completed.
	// Zero if the peer was never caught up.
	CaughtUpAt time.Time
	// LastError is the most recent push or anti-entropy error; empty after success.
	LastError string
	// Dropped counts batches that push did not deliver (FA-REP-07).
	Dropped uint64
}

PeerStatus describes the replication state towards one peer (FA-REP-05).

func (PeerStatus) Lag

func (s PeerStatus) Lag(now time.Time) (lag time.Duration, ok bool)

Lag returns how long ago the peer was last fully caught up. ok is false if the peer was never caught up.

type Replicator

type Replicator struct {
	// contains filtered or unexported fields
}

Replicator pushes new heartbeats to all peers and repairs gaps by anti-entropy.

func New

func New(opts Options) *Replicator

New creates a Replicator. It panics if a required option is missing, since that is a wiring error.

func (*Replicator) Publish

func (r *Replicator) Publish(hbs []store.Heartbeat)

Publish offers freshly stored heartbeats to every peer. It never blocks: a peer whose buffer is full loses the batch and reconnects, and anti-entropy repairs the gap (FA-REP-04, FA-REP-07). The caller may reuse hbs afterwards.

func (*Replicator) ReplicatedUntil

func (r *Replicator) ReplicatedUntil() time.Time

ReplicatedUntil returns the time up to which all current peers were fully caught up: the minimum of their last successful anti-entropy start. A peer that was never caught up counts as the zero time. Without peers it returns now.

func (*Replicator) Run

func (r *Replicator) Run(ctx context.Context)

Run keeps one worker per peer until ctx ends and then stops all workers.

func (*Replicator) Status

func (r *Replicator) Status() []PeerStatus

Status returns the state of every peer, sorted by ID.

func (*Replicator) Trigger

func (r *Replicator) Trigger(nodeID string)

Trigger requests an anti-entropy pass with the peer soon. Requests coalesce; unknown peers are ignored.

type Server

type Server struct {
	pb.UnimplementedReplicationServiceServer

	// NodeID is the ID of this node; its own stream is authoritative locally.
	NodeID  string
	Metrics store.Metrics
	// Triggers is notified once per opened push stream with the caller's node ID,
	// because pushes may have been lost while the stream was down (ADR 0006,
	// ADR 0012 B6). It may be nil.
	Triggers interface{ Trigger(nodeID string) }
	// OnStored is called with the validated heartbeats of a push after they were
	// stored, so that the leader can evaluate them (ADR 0021). It must not
	// block, and the slice is only valid during the call. It may be nil.
	OnStored func([]store.Heartbeat)
	Log      *slog.Logger
	// contains filtered or unexported fields
}

Server implements pb.ReplicationServiceServer on the cluster port. Only voters may call it; the interceptor of clusternet enforces this.

func (*Server) FetchRange

func (s *Server) FetchRange(ctx context.Context, req *pb.FetchRangeRequest) (*pb.FetchRangeResponse, error)

FetchRange returns up to limit heartbeats of one stream with seq in (after_seq, until_seq]. A limit of 0 or above 5000 means 5000; until_seq 0 means no bound.

func (*Server) GetWatermarks

GetWatermarks reports the positions of all known streams. The own current stream is reported with its last stored seq, since this node is its producer and a seq it lost was never stored anywhere (ADR 0012 B2).

func (*Server) StreamHeartbeats

func (s *Server) StreamHeartbeats(stream pb.ReplicationService_StreamHeartbeatsServer) error

StreamHeartbeats stores the heartbeats pushed by a voter and answers every message with the persisted positions of the streams in that message. Invalid heartbeats are dropped; anti-entropy never brings them back, so they must not be able to poison a batch.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL