Documentation
¶
Overview ¶
Package redisdocker runs real Redis or Valkey containers as a replication group or a sharded cluster and manages their ACLs, TLS and RDB snapshots.
Index ¶
- Constants
- Variables
- func DecodeBundle(blob []byte) ([][]byte, bool)
- func EncodeBundle(shards [][]byte) []byte
- func EvenSlots(n int) [][]SlotRange
- func ImageFor(engine, version string) (string, string)
- func IsRDB(blob []byte) bool
- func LoadEmbedded(mr *miniredis.Miniredis, blob []byte) error
- func SeedFromBlob(blob []byte) ([]byte, error)
- func SeedsFor(blob []byte, shards int) ([][]byte, error)
- func SetUserArgs(u ACLUser) ([]any, error)
- func ValidateSlotMap(shards [][]SlotRange) error
- type ACLPolicy
- type ACLUser
- type Admin
- type Config
- type Dialer
- type Endpoint
- type Engine
- type Group
- func (g *Group) ApplyACL(ctx context.Context, policy ACLPolicy) error
- func (g *Group) Close()
- func (g *Group) ClusterMode() bool
- func (g *Group) Endpoints() [][]Endpoint
- func (g *Group) Failover(ctx context.Context, shard int) error
- func (g *Group) Primary(shard int) (Endpoint, error)
- func (g *Group) SetReplicas(ctx context.Context, shard, replicas int) error
- func (g *Group) Snapshot(ctx context.Context) ([][]byte, error)
- func (g *Group) TLS() bool
- type Runtime
- type ShardSpec
- type SlotRange
- type Spec
- type Target
Constants ¶
const ( EngineRedis = "redis" EngineValkey = "valkey" )
Engine names.
const AdminUser = "gs-admin"
AdminUser is the internal account the emulator uses for control traffic; it is never exposed to API users.
const TotalSlots = 16384
TotalSlots is the size of the Redis cluster hash-slot space.
Variables ¶
var ( // ErrShardRange is returned for a shard index the group does not have. ErrShardRange = errors.New("shard index out of range") // ErrNoReplica is returned when a failover has no replica to promote. ErrNoReplica = errors.New("shard has no replica to promote") // ErrGroupClosed is returned for operations on a closed group. ErrGroupClosed = errors.New("redis group is closed") )
var ErrACL = errors.New("invalid ACL user")
ErrACL is returned for an unusable ACL definition.
var ErrBadSnapshot = errors.New("invalid redis snapshot data")
ErrBadSnapshot is returned when snapshot data cannot be read.
var ErrNoCA = errors.New("redis group needs a TLS certificate authority")
ErrNoCA is returned when TLS is requested but the engine has no certificate authority.
var ErrNotConverged = errors.New("redis group did not converge")
ErrNotConverged is returned when a group does not reach its target state before the start timeout.
var ErrShape = errors.New("snapshot shards do not match the cluster")
ErrShape is returned when snapshot data cannot be spread over the requested shards.
var ErrSlots = errors.New("invalid slot assignment")
ErrSlots is returned when a slot assignment is malformed or does not cover the hash-slot space exactly once.
var ErrSpec = errors.New("invalid redis group spec")
ErrSpec is returned when a launch request is inconsistent.
Functions ¶
func DecodeBundle ¶
DecodeBundle unpacks EncodeBundle output; ok is false when blob is not a bundle.
func EncodeBundle ¶
EncodeBundle packs one RDB per shard into a single blob.
func EvenSlots ¶
EvenSlots splits the hash-slot space across n shards; the last shard takes the remainder.
func ImageFor ¶
ImageFor maps an engine and API version to a container image. Versions below 7 run on 7.0, whose cluster hostname support the group relies on; odd minors map to the next published image.
func LoadEmbedded ¶
LoadEmbedded loads a snapshot blob produced by either engine mode into an embedded server.
func SeedFromBlob ¶
SeedFromBlob converts any snapshot payload this module produces (a raw RDB or an embedded keyspace dump) to an RDB.
func SeedsFor ¶
SeedsFor turns a snapshot blob (bundle, raw RDB or embedded dump) into per-shard RDB seeds for shards shards.
func SetUserArgs ¶
SetUserArgs translates u into an ACL SETUSER command. The user is reset first so repeated syncs converge, then gets its passwords and the rules of its access string ("on ~app:* +@all").
func ValidateSlotMap ¶
ValidateSlotMap checks that the shards together cover every hash slot exactly once.
Types ¶
type ACLPolicy ¶
type ACLPolicy struct {
// DefaultPassword protects the "default" account when no listed user is named "default".
DefaultPassword string
Users []ACLUser
// OpenDefault leaves the "default" account passwordless and unrestricted when no listed user is named "default".
OpenDefault bool
}
ACLPolicy is the full set of accounts a group serves.
type ACLUser ¶
type ACLUser struct {
Name string
AccessString string
Passwords []string
// NoPassword lets the user in without a password.
NoPassword bool
}
ACLUser is one Redis ACL account derived from an API user.
type Config ¶
type Config struct {
Runtime Runtime
Ports *portalloc.Allocator
// CA signs node certificates; TLS groups cannot launch without it.
CA *devtls.CA
// Dial opens a control connection; defaults to go-redis.
Dial Dialer
// FetchRDB reads a node's dataset; defaults to a SYNC handshake.
FetchRDB func(ctx context.Context, t Target) ([]byte, error)
// Host is the address clients are told to use; defaults to 127.0.0.1.
Host string
// Label names the port reservations, e.g. "memorydb".
Label string
// TempDir holds per-group mount directories; defaults to the OS temp dir.
TempDir string
StartTimeout time.Duration
PollInterval time.Duration
}
Config configures an Engine.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine launches groups of Redis containers.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a launched set of containers.
func (*Group) ApplyACL ¶
ApplyACL syncs policy onto every node; accounts dropped since the previous sync are deleted.
func (*Group) Close ¶
func (g *Group) Close()
Close removes the containers, frees their ports and deletes the mount directory. It is idempotent.
func (*Group) ClusterMode ¶
ClusterMode reports whether the group runs Redis Cluster.
func (*Group) Endpoints ¶
Endpoints lists each shard's nodes in launch order; Primary names the current primary.
func (*Group) Failover ¶
Failover promotes the shard's first replica to primary and makes the old primary follow it.
func (*Group) SetReplicas ¶
SetReplicas grows or shrinks one shard to the given replica count; extra replicas are the highest numbered.
type Runtime ¶
type Runtime interface {
CreateAndStart(ctx context.Context, spec container.Spec) (string, error)
StopAndRemove(ctx context.Context, containerID string) error
Exec(ctx context.Context, containerID string, cmd []string) (container.ExecResult, error)
}
Runtime is the slice of the container runtime a group needs.
type ShardSpec ¶
type ShardSpec struct {
// Seed is an RDB file the primary starts with; empty starts blank.
Seed []byte
// Slots are the hash slots served by the shard; ignored unless the group is in cluster mode.
Slots []SlotRange
// Replicas is the number of replicas of the shard's primary.
Replicas int
}
ShardSpec describes one shard: its slots, replica count and optional RDB to start from.
type SlotRange ¶
SlotRange is an inclusive range of hash slots.
func ParseSlots ¶
ParseSlots parses "0-5461" or a comma or space separated list such as "0-100,200-300,500".
type Spec ¶
type Spec struct {
// Engine is "redis" (default) or "valkey".
Engine string
// Version is the engine version ("7.1", "7.2.4"); only major.minor selects the image.
Version string
// TLSNames are extra certificate names beyond localhost and the advertised host.
TLSNames []string
Shards []ShardSpec
// ClusterMode runs Redis Cluster; without it the group is one primary and its replicas.
ClusterMode bool
// TLS serves TLS only (plaintext port disabled).
TLS bool
}
Spec describes a group to launch.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package redisdockertest provides an in-memory container runtime and control connection for exercising redisdocker groups without Docker.
|
Package redisdockertest provides an in-memory container runtime and control connection for exercising redisdocker groups without Docker. |