cluster

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cluster contains primitives for node identity, membership tracking and consistent hashing used by distributed backends.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidAddress = errors.New("invalid node address")

ErrInvalidAddress is returned when the node address is invalid.

Functions

This section is empty.

Types

type Membership

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

Membership tracks current cluster nodes (static MVP, future: gossip/swim).

func NewMembership

func NewMembership(ring *Ring) *Membership

NewMembership creates a new membership container bound to a ring.

func (*Membership) List

func (m *Membership) List() []*Node

List returns current nodes snapshot.

func (*Membership) Mark

func (m *Membership) Mark(id NodeID, state NodeState) bool

Mark updates node state + incarnation and refreshes LastSeen. Returns true if node exists.

func (*Membership) Remove

func (m *Membership) Remove(id NodeID) bool

Remove deletes a node from membership and rebuilds the ring. Returns true if removed.

func (*Membership) Ring

func (m *Membership) Ring() *Ring

Ring returns the underlying ring reference.

func (*Membership) Upsert

func (m *Membership) Upsert(n *Node)

Upsert adds or updates a node and rebuilds ring.

func (*Membership) Version added in v0.2.1

func (m *Membership) Version() uint64

Version returns current membership version.

type MembershipVersion added in v0.2.1

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

MembershipVersion tracks a monotonically increasing version for membership changes. Used to expose a cheap cluster epoch for clients/metrics.

func (*MembershipVersion) Get added in v0.2.1

func (mv *MembershipVersion) Get() uint64

Get returns current version.

func (*MembershipVersion) Next added in v0.2.1

func (mv *MembershipVersion) Next() uint64

Next increments and returns the next version.

type Node

type Node struct {
	ID          NodeID
	Address     string // host:port for intra-cluster RPC
	State       NodeState
	Incarnation uint64
	LastSeen    time.Time
}

Node holds identity & state.

func NewNode

func NewNode(id string, addr string) *Node

NewNode creates a node from address (host:port). If id empty, derive a short hex id using xxhash64.

func (*Node) Validate

func (n *Node) Validate() error

Validate basic fields.

type NodeID

type NodeID string

NodeID is a stable identifier for a node.

type NodeState

type NodeState int

NodeState represents membership state of a node.

const (
	NodeAlive NodeState = iota
	NodeSuspect
	NodeDead
)

Node state enumeration.

func (NodeState) String

func (s NodeState) String() string

type Ring

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

Ring implements a consistent hashing ring with virtual nodes.

func NewRing

func NewRing(opts ...RingOption) *Ring

NewRing creates a ring with defaults overridden by options. NewRing constructs a new Ring applying provided options.

func (*Ring) Build

func (r *Ring) Build(nodes []*Node)

Build rebuilds ring from nodes set (copy-on-write). Build rebuilds the ring using the supplied node list.

func (*Ring) Lookup

func (r *Ring) Lookup(key string) []NodeID

Lookup returns primary + (replication-1) replicas. Lookup returns the primary owner and (replication-1) replicas for a key.

func (*Ring) Replication

func (r *Ring) Replication() int

Replication returns replication factor.

func (*Ring) VNodeHashes

func (r *Ring) VNodeHashes() []string

VNodeHashes returns a copy of vnode hash values as hex strings (debug only).

func (*Ring) VirtualNodesPerNode

func (r *Ring) VirtualNodesPerNode() int

VirtualNodesPerNode returns configured virtual nodes per physical node.

type RingOption

type RingOption func(*Ring)

RingOption configures ring.

func WithReplication

func WithReplication(n int) RingOption

WithReplication sets the replication factor (number of owners per key).

func WithVirtualNodes

func WithVirtualNodes(n int) RingOption

WithVirtualNodes sets the number of virtual nodes per physical node.

Jump to

Keyboard shortcuts

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