replication

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 32 Imported by: 1 module, 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 keeps the data of every index on more than one node, following the three OpenSearch replication paths:

  • document: the owner forwards every WAL entry to its replicas and acknowledges a write once every in-sync replica appended it; replicas index the entries themselves (OpenSearch document replication)
  • segment: entries are forwarded the same way for durability but the replicas only keep them; they copy the owner's segments on the next checkpoint after keeping new entries and replay the kept entries when promoted (segment replication)
  • remote: indexes on object storage upload their entries to the bucket before a write is acknowledged; segments already live there, so a new owner replays the remote translog (remote-backed storage)

document and segment apply to disk indexes and are chosen with `zed_cluster_replication`; remote is used for every writable index on object storage.

Index

Constants

View Source
const (
	ModeDocument = "document"
	ModeSegment  = "segment"
	ModeNone     = "none"
)

Replication modes of disk indexes.

View Source
const (
	ReplicatePath = "/api/_internal/replication/replicate"
	RecoverPath   = "/api/_internal/replication/recover"
	SnapshotPath  = "/api/_internal/replication/snapshot"
	SecretHeader  = "X-Zed-Cluster-Secret"
)

Node to node endpoints, authenticated with SecretHeader.

Variables

View Source
var ErrNotModified = errors.New("replication: snapshot not modified")

ErrNotModified is returned by Snapshot when the replica is current.

View Source
var ErrOutOfSync = errors.New("replication: replica out of sync")

ErrOutOfSync is returned by a replica that cannot apply a batch: other term, missing operations, or a recovery in progress.

Functions

func CheckSecret

func CheckSecret(header, secret string) bool

CheckSecret reports whether a request carries the cluster secret.

func Commit

func Commit(ctx context.Context, index string) error

Commit returns once every operation recorded for index is on all in-sync replicas (document, segment) or in the bucket (remote).

func CommitAll

func CommitAll(ctx context.Context) error

CommitAll commits every index with recorded operations, for writes that name their indexes in the body (`_bulk`).

func Enabled

func Enabled() bool

Enabled reports whether replication runs on this node.

func Mode

func Mode() string

Mode returns the replication mode of disk indexes, ModeNone when off.

func Prepare

func Prepare(ctx context.Context, index string) error

Prepare must run before a write to index is admitted on its owner: a replica that just became owner first replays the operations it kept (segment) or the remote translog (remote).

func PrepareAll

func PrepareAll(ctx context.Context) error

PrepareAll prepares every writable index owned here, for writes that name their indexes in the body (`_bulk`): a promoted node must replay what it kept before such a write lands.

func Ready

func Ready(index string) bool

Ready reports whether this node holds a usable copy of index as a replica, so it may serve reads of it.

func RequestRecovery

func RequestRecovery(index string) error

RequestRecovery starts a file copy of index from its owner in the background, unless one is running.

func ServeSnapshot

func ServeSnapshot(ctx context.Context, req *SnapshotRequest, w func() io.Writer) error

ServeSnapshot writes a file copy of an owned disk index to w. It answers ErrNotModified when the replica already is at have.

func Start

func Start(ctx context.Context) error

Start turns replication on when the cluster is enabled and the mode is not none. It must run after cluster.Start.

func Stop

func Stop()

Stop removes the hooks and stops the background loop.

func WriteSnapshot

func WriteSnapshot(w io.Writer, info *SnapshotInfo, dir string) error

WriteSnapshot streams info and every file under dir but info.Reused as a tar archive.

Types

type Ack

type Ack struct {
	Term       string `json:"term"`
	Seq        uint64 `json:"seq"`
	Recovering bool   `json:"recovering,omitempty"`
}

Ack is a replica's answer: the term it follows and its last operation.

func Apply

func Apply(batch *Batch) (Ack, error)

Apply handles a batch shipped by the owner. ErrOutOfSync comes with the Ack telling the owner what is missing.

type Batch

type Batch struct {
	Index    string          `json:"index"`
	Term     string          `json:"term"`
	From     uint64          `json:"from"`
	Ops      []Op            `json:"ops"`
	Mappings json.RawMessage `json:"mappings,omitempty"`
}

Batch carries the operations From.. of one index to a replica.

type Checkpoint

type Checkpoint struct {
	Term string `json:"term"`
	Seq  uint64 `json:"seq"`
}

Checkpoint names a point of an owner's history.

type FileSum

type FileSum struct {
	Path string `json:"path"` // slash separated, relative to the index directory
	Size int64  `json:"size"`
	Sum  string `json:"sum"` // hex sha256
}

FileSum identifies a segment file of a local copy by content.

type HTTPTransport

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

HTTPTransport calls the internal endpoints of the other nodes.

func NewHTTPTransport

func NewHTTPTransport(secret string) *HTTPTransport

NewHTTPTransport builds a transport authenticating with secret.

func (*HTTPTransport) Recover

func (t *HTTPTransport) Recover(ctx context.Context, node, index string) error

Recover implements Transport.

func (*HTTPTransport) Replicate

func (t *HTTPTransport) Replicate(ctx context.Context, node string, batch *Batch) (Ack, error)

Replicate implements Transport.

func (*HTTPTransport) Snapshot

func (t *HTTPTransport) Snapshot(ctx context.Context, owner string, req *SnapshotRequest, dir string) (*SnapshotInfo, error)

Snapshot implements Transport.

type Op

type Op struct {
	Seq   uint64          `json:"seq"`
	Shard string          `json:"shard"`
	Entry json.RawMessage `json:"entry"`
}

Op is one WAL entry recorded on the owner.

type SnapshotInfo

type SnapshotInfo struct {
	Checkpoint
	Index  json.RawMessage `json:"index"`
	Reused []string        `json:"reused,omitempty"`
}

SnapshotInfo describes a file copy: the history point it contains at least, the index metadata matching the files and the files of the request left out of the archive, which the replica takes from its copy.

func ReadSnapshot

func ReadSnapshot(r io.Reader, dir string) (*SnapshotInfo, error)

ReadSnapshot unpacks a WriteSnapshot archive into dir.

type SnapshotRequest

type SnapshotRequest struct {
	Index string     `json:"index"`
	Have  Checkpoint `json:"have"`
	Files []FileSum  `json:"files,omitempty"`
}

SnapshotRequest asks the owner for a file copy of Index unless it is still at Have. Files are the segments the replica holds: the owner does not send the ones it has too.

type Transport

type Transport interface {
	// Replicate sends a batch; ErrOutOfSync comes with the replica's Ack.
	Replicate(ctx context.Context, node string, batch *Batch) (Ack, error)
	// Recover asks a replica to copy the index from its owner.
	Recover(ctx context.Context, node, index string) error
	// Snapshot copies the files of req.Index from owner into dir, unless
	// the owner is still at req.Have (ErrNotModified).
	Snapshot(ctx context.Context, owner string, req *SnapshotRequest, dir string) (*SnapshotInfo, error)
}

Transport is how replication reaches other nodes.

Jump to

Keyboard shortcuts

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