Documentation
¶
Overview ¶
Package replication implements leader → follower replication in the style of Redis' SYNC protocol:
- The follower connects to the leader, sends "REPLCONF listening-port <port>" (so the leader can report it) and "SYNC".
- 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.
- 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 ¶
- func SetupRaft(e *command.Engine, m *Manager, o RaftOptions) (*raft.Node, error)
- type Manager
- func (m *Manager) Close()
- func (m *Manager) Info() [][2]string
- func (m *Manager) IsReplica() bool
- func (m *Manager) Offset() int64
- func (m *Manager) OnLeaderChange(selfID string, dataAddrs map[string]string) func(leaderID string)
- func (m *Manager) Propagate(args []string)
- func (m *Manager) ReplConf(c *command.Client, args []string) resp.Value
- func (m *Manager) ReplicaOf(addr string) error
- func (m *Manager) Role() resp.Value
- func (m *Manager) Sync(c *command.Client, args []string) resp.Value
- func (m *Manager) Wait(n int, timeout time.Duration) int
- type Options
- type Peer
- type RaftOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetupRaft ¶
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 SetupWith ¶
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 ¶
Info returns the replication section of INFO (the "role" line is added by the INFO command itself).
func (*Manager) IsReplica ¶
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 ¶
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 ¶
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 ¶
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 ¶
ReplConf handles REPLCONF from a replica before SYNC (listening-port, capa). ACKs arrive on the hijacked connection and never get here.
func (*Manager) ReplicaOf ¶
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) Sync ¶
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.
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.