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
- Variables
- func CheckSecret(header, secret string) bool
- func Commit(ctx context.Context, index string) error
- func CommitAll(ctx context.Context, b *Bulk) error
- func Current(name string) bool
- func Enabled() bool
- func Mode() string
- func Prepare(ctx context.Context, index string) error
- func Readable(name string) bool
- func Ready(index string) bool
- func RequestRecovery(index string) error
- func ServeSnapshot(ctx context.Context, req *SnapshotRequest, w func() io.Writer) error
- func Start(ctx context.Context) error
- func Stop()
- func WriteSnapshot(w io.Writer, info *SnapshotInfo, dir string) error
- type Ack
- type Batch
- type Bulk
- type Checkpoint
- type FileSum
- type HTTPTransport
- type Op
- type SnapshotInfo
- type SnapshotRequest
- type Transport
Constants ¶
const ( ModeDocument = "document" ModeSegment = "segment" ModeNone = "none" )
Replication modes of disk indexes.
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 ¶
var ErrNotModified = errors.New("replication: snapshot not modified")
ErrNotModified is returned by Snapshot when the replica is current.
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 ¶
CheckSecret reports whether a request carries the cluster secret.
func Commit ¶
Commit returns once every operation recorded for index is on all in-sync replicas (document, segment) or in the bucket (remote).
func CommitAll ¶
CommitAll commits every index with recorded operations, for writes that name their indexes in the body (`_bulk`). The indexes ship side by side: a bulk over many of them waits for the slowest, not for their sum.
A primary retired with an operation recorded after b began fails it: its partition moved and the new owner may lack that operation. WAL entries carry no request, so an operation of a concurrent request counts too; such a bulk gets a 409 and retries, it is never acknowledged for a write the new owner did not fence.
func Current ¶ added in v1.4.1
Current reports whether the copy this node replicates holds every write the owner acknowledged: it is in the in-sync set, which the owner leaves a failed replica out of before acknowledging, and it applied everything it received. A segment replica answers from its installed files, so operations it only kept leave it behind until its next checkpoint.
func Mode ¶
func Mode() string
Mode returns the replication mode of disk indexes, ModeNone when off.
func Prepare ¶
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 Readable ¶ added in v1.4.1
Readable reports whether this node may serve reads of an index it replicates: a cold index is read-only on object storage, so every node sees the same data; a disk index qualifies once its replicated copy is installed and while it is in the in-sync set. The owner drops a failed replica from the set before acknowledging the write it missed, so such a copy serves no read, not even as a fallback for an unreachable owner.
func Ready ¶
Ready reports whether this node holds a usable copy of index as a replica, so it may serve reads of it.
func RequestRecovery ¶
RequestRecovery starts a file copy of index from its owner in the background, unless one is running.
func ServeSnapshot ¶
ServeSnapshot writes a file copy of an owned disk index to w. It answers ErrNotModified when the replica already is at have.
func Start ¶
Start turns replication on when the cluster is enabled and the mode is not none. It must run after cluster.Start.
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.
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 Bulk ¶ added in v1.4.1
type Bulk struct {
// contains filtered or unexported fields
}
Bulk is a running write that names its indexes in the body (`_bulk`), from PrepareAll until Done. Primaries retired while it runs are kept for its CommitAll, however long the request takes. A nil Bulk is a no-op.
func PrepareAll ¶
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. The bulk it returns is handed to CommitAll; on error it is already done.
type Checkpoint ¶
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) 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.