replication

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 17 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 implements leader → follower replication in the style of Redis' SYNC protocol:

  1. The follower connects to the leader, sends "REPLCONF listening-port <port>" (so the leader can report it) and "SYNC".
  2. The leader runs SYNC with every shard locked (the command has FlagAllKeys), so in one atomic step it serialises a snapshot into memory and starts buffering every write propagated after it. It then hijacks the connection and sends "+FULLRESYNC <runid> <offset>\r\n$<len>\r\n<snapshot bytes>" followed by the write commands as RESP arrays, plus a PING every PingInterval so the follower can detect a dead link.
  3. The follower flushes its dataset, loads the snapshot, then executes the stream as an internal "master" client (which may write although the node is read-only), and reports its progress with "REPLCONF ACK <offset>" every AckInterval.

Offsets count bytes of the command stream, as in Redis: the leader's master_repl_offset grows by the RESP size of every command it sends, and a follower's offset is the leader's offset at FULLRESYNC plus the bytes it has applied since. WAIT compares acknowledged offsets with the leader's offset at the time WAIT was called.

There is no partial resync (PSYNC continuation) and no backlog: a follower that disconnects does a full resync when it reconnects, and a follower that can't keep up (its output buffer passes MaxReplicaBuffer) is dropped and resyncs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupRaft

func SetupRaft(e *command.Engine, m *Manager, o RaftOptions) (*raft.Node, error)

SetupRaft starts Raft leader election when cfg.RaftPort is set, and wires it into m (see OnLeaderChange). It returns nil, nil when Raft is disabled. The node ID is cfg.ClusterNodeID; cfg.RaftPeers lists every node (this one may be included; it is skipped). Until a leader is elected this node refuses writes. Term and vote are persisted in <dir>/raft-<id>.json. It also makes RAFT.STATUS available. Stop the returned node at shutdown.

Types

type Manager

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

Manager implements command.Replicator. It is both sides at once: it can serve replicas (leader side) and follow a leader (follower side), which also allows chains (A → B → C).

func Setup

func Setup(e *command.Engine) *Manager

Setup creates the manager with default options; see SetupWith.

func SetupWith

func SetupWith(e *command.Engine, o Options) *Manager

SetupWith creates the manager, sets e.Repl and registers it as a propagator. If cfg.ReplicaOf is set ("host port"), it starts following that leader.

func (*Manager) Close

func (m *Manager) Close()

Close stops following, disconnects replicas and stops background work.

func (*Manager) Info

func (m *Manager) Info() [][2]string

Info returns the replication section of INFO (the "role" line is added by the INFO command itself).

func (*Manager) IsReplica

func (m *Manager) IsReplica() bool

IsReplica reports whether writes from clients must be refused: the node follows a leader, or failover fenced it while no leader is known.

func (*Manager) Offset

func (m *Manager) Offset() int64

Offset is this node's replication position: the applied offset when following, otherwise master_repl_offset. Raft uses it to prefer the most up-to-date candidate.

func (*Manager) OnLeaderChange

func (m *Manager) OnLeaderChange(selfID string, dataAddrs map[string]string) func(leaderID string)

OnLeaderChange returns the callback that wires Raft leadership into replication:

  • this node elected: become a leader (stop following, accept writes);
  • another node elected: follow its data address;
  • no leader known: if we are not following anyone (we were the leader, or never followed), fence writes until a leader is elected, so a node cut off from the majority stops accepting writes.

func (*Manager) Propagate

func (m *Manager) Propagate(args []string)

Propagate implements command.Propagator: every write command is encoded once and queued for every replica. It never blocks: a replica whose queue is full is dropped (it will reconnect and resync).

func (*Manager) ReplConf

func (m *Manager) ReplConf(c *command.Client, args []string) resp.Value

ReplConf handles REPLCONF from a replica before SYNC (listening-port, capa). ACKs arrive on the hijacked connection and never get here.

func (*Manager) ReplicaOf

func (m *Manager) ReplicaOf(addr string) error

ReplicaOf makes this node follow the leader at addr ("host:port"), or become a leader when addr is "" (the dataset is kept). Following the leader we already follow is a no-op.

func (*Manager) Role

func (m *Manager) Role() resp.Value

Role returns the ROLE reply.

func (*Manager) Sync

func (m *Manager) Sync(c *command.Client, args []string) resp.Value

Sync handles SYNC/PSYNC. The dispatcher holds every shard lock (the command has FlagAllKeys), so encoding the snapshot and registering the replica happen atomically with respect to writes: every write is either in the snapshot or in the replica's queue, never both, never neither.

func (*Manager) Wait

func (m *Manager) Wait(n int, timeout time.Duration) int

Wait blocks until n replicas acknowledged every write made before the call, or timeout passes (0 = no timeout), and returns how many did.

type Options

type Options struct {
	PingInterval     time.Duration // leader → replicas keepalive PING (default 1s)
	AckInterval      time.Duration // follower → leader REPLCONF ACK period (default 1s)
	Timeout          time.Duration // a link silent this long is dead (default 60s)
	MaxReplicaBuffer int64         // bytes queued per replica before it is dropped (default 256MB)
	BacklogSize      int           // stream bytes kept for partial resync (default: config repl-backlog-size)
}

Options tunes the manager. Zero values get the defaults.

type Peer

type Peer struct {
	ID       string
	RaftAddr string
	DataAddr string
}

Peer is one entry of cfg.RaftPeers: "id raftHost:raftPort dataHost:dataPort".

func ParsePeer

func ParsePeer(line string) (Peer, error)

ParsePeer parses a raft-peer line.

type RaftOptions

type RaftOptions struct {
	ElectionTimeoutMin time.Duration
	ElectionTimeoutMax time.Duration
	HeartbeatInterval  time.Duration
}

RaftOptions tunes SetupRaft (zero values = raft package defaults).

Jump to

Keyboard shortcuts

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