cluster

package
v0.0.0-...-9a2c5cd Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultUpdateInterval = time.Second * 5
	DefaultUpdateTimeout  = time.Second
)

Default values for Cluster config

Variables

This section is empty.

Functions

func PostgreSQL

func PostgreSQL(ctx context.Context, db dbsql.Node) (bool, error)

PostgreSQL checks whether PostgreSQL server is primary or not.

Types

type AliveNodes

type AliveNodes struct {
	Alive     []dbsql.Node
	Primaries []dbsql.Node
	Standbys  []dbsql.Node
}

AliveNodes of Store

type Checker

type Checker func(ctx context.Context, db dbsql.Node) (bool, error)

Checker is a signature for functions that check if a specific node is alive and is primary. Returns true for primary and false if not. If error is returned, the node is considered dead. Check function can be used to perform a Query returning single boolean value that signals if node is primary or not.

type Cluster

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

Cluster consists of number of 'nodes' of a single SQL database. Background goroutine periodically checks nodes and updates their status.

func New

func New(log *wlog.Logger, nodes []dbsql.Node, opts ...Option) (*Cluster, error)

New constructs Cluster object representing a single 'Cluster' of SQL database. Close function must be called when a Cluster isn't necessary anymore.

func (*Cluster) Alive

func (cl *Cluster) Alive() dbsql.Node

Alive returns node that is considered alive

func (*Cluster) Close

func (cl *Cluster) Close() error

Close databases and stop node updates.

func (*Cluster) Err

func (cl *Cluster) Err() error

Err returns the combined error including most recent errors for all nodes. This error is CollectedErrors or nil.

func (*Cluster) HealthCheck

func (cl *Cluster) HealthCheck(ctx context.Context) []health.CheckResult

func (*Cluster) Node

func (cl *Cluster) Node(criteria NodeStateCriteria) dbsql.Node

Node returns Cluster node with specified status.

func (*Cluster) Nodes

func (cl *Cluster) Nodes() []dbsql.Node

Nodes returns list of all nodes

func (*Cluster) Primary

func (cl *Cluster) Primary() dbsql.Node

Primary returns first available node that is considered alive and is primary (able to execute write operations)

func (*Cluster) PrimaryPreferred

func (cl *Cluster) PrimaryPreferred() dbsql.Node

PrimaryPreferred returns primary node if possible, standby otherwise

func (*Cluster) Shutdown

func (cl *Cluster) Shutdown(p *shutdown.Process) error

func (*Cluster) Standby

func (cl *Cluster) Standby() dbsql.Node

Standby returns node that is considered alive and is standby (unable to execute write operations)

func (*Cluster) StandbyPreferred

func (cl *Cluster) StandbyPreferred() dbsql.Node

StandbyPreferred returns standby node if possible, primary otherwise

func (*Cluster) WaitForAlive

func (cl *Cluster) WaitForAlive(ctx context.Context) (dbsql.Node, error)

WaitForAlive node to appear or until context is canceled

func (*Cluster) WaitForNode

func (cl *Cluster) WaitForNode(ctx context.Context, criteria NodeStateCriteria) (dbsql.Node, error)

WaitForNode with specified status to appear or until context is canceled

func (*Cluster) WaitForPrimary

func (cl *Cluster) WaitForPrimary(ctx context.Context) (dbsql.Node, error)

WaitForPrimary node to appear or until context is canceled

func (*Cluster) WaitForPrimaryPreferred

func (cl *Cluster) WaitForPrimaryPreferred(ctx context.Context) (dbsql.Node, error)

WaitForPrimaryPreferred node to appear or until context is canceled

func (*Cluster) WaitForStandby

func (cl *Cluster) WaitForStandby(ctx context.Context) (dbsql.Node, error)

WaitForStandby node to appear or until context is canceled

func (*Cluster) WaitForStandbyPreferred

func (cl *Cluster) WaitForStandbyPreferred(ctx context.Context) (dbsql.Node, error)

WaitForStandbyPreferred node to appear or until context is canceled

type CollectedErrors

type CollectedErrors struct {
	Errors []NodeError
}

CollectedErrors are errors collected when checking node statuses

func (*CollectedErrors) Error

func (e *CollectedErrors) Error() string

type ForecastStore

type ForecastStore Store

ForecastStore is a type alias for plumbing it through Wire.

type NodeError

type NodeError struct {
	Addr       string
	Err        error
	OccurredAt time.Time
}

NodeError is an error that background goroutine got while check given node

func (*NodeError) Error

func (e *NodeError) Error() string

type NodeStateCriteria

type NodeStateCriteria int

NodeStateCriteria for choosing a node

const (

	// Alive for choosing any alive node
	Alive NodeStateCriteria = iota + 1

	// Primary for choosing primary node
	Primary

	// Standby for choosing standby node
	Standby

	// PreferPrimary for choosing primary or any alive node
	PreferPrimary

	// PreferStandby for choosing standby or any alive node
	PreferStandby
)

type Option

type Option func(*Cluster)

Option is a functional option type for Cluster constructor.

func WithNodePicker

func WithNodePicker(picker Picker) Option

WithNodePicker sets algorithm for node selection (e.g., random, round-robin etc.).

func WithTracer

func WithTracer(tracer Tracer) Option

WithTracer sets tracer for actions happening in the background.

func WithUpdate

func WithUpdate() Option

WithUpdate decides whether to update node states. Useful for tests with mocked sql.DB.

func WithUpdateInterval

func WithUpdateInterval(d time.Duration) Option

WithUpdateInterval sets interval between Cluster node updates.

func WithUpdateTimeout

func WithUpdateTimeout(d time.Duration) Option

WithUpdateTimeout sets ping timeout for update of each node in Cluster.

type Picker

type Picker func(nodes []dbsql.Node) dbsql.Node

Picker is a signature for functions that determine how to pick single node from set of nodes. Nodes passed to the picker function are sorted according to latency (from lowest to greatest).

func PickNodeClosest

func PickNodeClosest() Picker

PickNodeClosest returns node with the least latency

func PickNodeRandom

func PickNodeRandom() Picker

PickNodeRandom returns random node from nodes set

func PickNodeRoundRobin

func PickNodeRoundRobin() Picker

PickNodeRoundRobin returns next node based on Round Robin algorithm

type Store

type Store interface {
	Close() error
	Err() error

	Nodes() []dbsql.Node
	Alive() dbsql.Node
	Primary() dbsql.Node
	Standby() dbsql.Node
	StandbyPreferred() dbsql.Node
	Node(criteria NodeStateCriteria) dbsql.Node

	WaitForPrimary(ctx context.Context) (dbsql.Node, error)
	WaitForStandby(ctx context.Context) (dbsql.Node, error)
	WaitForPrimaryPreferred(ctx context.Context) (dbsql.Node, error)
	WaitForStandbyPreferred(ctx context.Context) (dbsql.Node, error)
	WaitForAlive(ctx context.Context) (dbsql.Node, error)
}

Store represents a store that manages a Cluster of nodes. It provides methods for retrieving information about the nodes in the Cluster, as well as closing the store and checking for any errors.

type Tracer

type Tracer struct {

	// UpdateNodes is called when before updating nodes status.
	UpdateNodes func()

	// UpdatedNodes is called after all nodes are updated. The nodes is a list of currently alive nodes.
	UpdatedNodes func(nodes AliveNodes)

	// NodeDead is called when it is determined that specified node is dead.
	NodeDead func(node dbsql.Node, err error)

	// NodeAlive is called when it is determined that specified node is alive.
	NodeAlive func(node dbsql.Node)

	// NotifiedWaiters is called when all callers of 'WaitFor*' functions have been notified.
	NotifiedWaiters func()
}

Tracer is a set of hooks to run at various stages of background nodes status update. Any particular hook may be nil. Functions may be called concurrently from different goroutines.

func DefaultTracer

func DefaultTracer(log *wlog.Logger) Tracer

Jump to

Keyboard shortcuts

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