store

package
v0.0.0-...-3a25d02 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeoutExpired is for when waiting for a leader node to be elected
	// we run out of the given timeout.
	ErrTimeoutExpired = errors.New("timeout expired")

	// ErrNotLeader is for when a voter/non-voter node tries to execute an action
	// that is leader-only.
	ErrNotLeader = errors.New("not leader")

	// ErrJoinSelf is for  when a node tries to join itself.
	ErrJoinSelf = errors.New("trying to join self")

	// ErrValuesAreNil is for when either a key or value that are given to
	// raft.Apply are nil, even though they should contain []byte data.
	ErrValuesAreNil = errors.New("values are nil")
)

Functions

func CheckRaftConfig

func CheckRaftConfig(conf raft.Configuration) error

CheckRaftConfig checks that a given raft config is valid. This is used for recovering a raft cluster.

func RecoverNode

func RecoverNode(dir string, logs raft.LogStore, stable raft.StableStore,
	snaps raft.SnapshotStore, tn raft.Transport, conf raft.Configuration,
) error

RecoverNode tries to recover a Raft cluster from a given recovery configuration file.

Types

type Config

type Config struct {
	Bootstrap          bool
	SnapshotThreshold  uint64
	StrongConsistency  bool
	BindAddr           string
	CommitTimeout      time.Duration
	LocalID            raft.ServerID
	HeartbeatTimeout   time.Duration
	ElectionTimeout    time.Duration
	LeaderLeaseTimeout time.Duration

	Transport *Transport
}

Config contains the user-configurable values for the store.

type RaftStore

type RaftStore interface {
	Get(key []byte) (val []byte, err error)
	Put(key []byte, val []byte) error
	Leave(id string) error
	Join(id, addr string) error
	GetServers() ([]*messages.Server, error)
	LeaderAddr() string
}

RaftStore shows what methods a given Raft cluster should implement.

type Store

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

func New

func New(dir string, conf *Config, logging bool) (*Store, error)

New creates a new instance of an store. It sets up the data directory, raft directory raft databases, internal database and raft connections. It also checks for possible recovery files to recover a failed cluster.

func (*Store) Apply

func (s *Store) Apply(l *raft.Log) interface{}

Apply a given raft command. All of the work is done by the applyHelper.

func (*Store) Close

func (s *Store) Close() error

Close shuts the Raft cluster and the internal database connection.

func (*Store) Delete

func (s *Store) Delete(key []byte) error

Delete deletes the key-value pair with the given key. It is an leader-only operation.

func (*Store) Get

func (s *Store) Get(key []byte) ([]byte, error)

Get finds a given key from the raft cluster. If StrongConsistency is enabled, the get request will be redirected to the leader node. Otherwise the value is read from the given node. Getting from a non-leader node means that the value might be old or non-existant.

func (*Store) GetConfig

func (s *Store) GetConfig() (raft.Configuration, error)

GetConfig handles the GetConfiguration future and returns the raft config.

func (*Store) GetServers

func (s *Store) GetServers() ([]*messages.Server, error)

GetServers returns all of the servers that belong to the raft cluster.

func (*Store) IsLeader

func (s *Store) IsLeader() bool

IsLeader returns a value indicating if a given store is the leader.

func (*Store) Join

func (s *Store) Join(id, addr string) error

Join handles a given node joining the whole raft cluster. The joining has to be done using the leader node.

func (*Store) LeaderAddr

func (s *Store) LeaderAddr() string

LeaderAddr returns the leader node address.

func (*Store) LeaderID

func (s *Store) LeaderID() (string, error)

LeaderID returns the node ID of the leader node.

func (*Store) Leave

func (s *Store) Leave(id string) error

Leave removes a node with ID from the raft cluster.

func (*Store) Put

func (s *Store) Put(key, value []byte) error

Put writes a key-value pair into the cluster. This is a leader-only operation.

func (*Store) Restore

func (f *Store) Restore(rc io.ReadCloser) error

Restore takes in the data created by snapshot.Persist() and creates the internal database based on that data.

func (*Store) Snapshot

func (f *Store) Snapshot() (raft.FSMSnapshot, error)

Snapshot creates a snapshot of the store.

func (*Store) WaitForLeader

func (s *Store) WaitForLeader(timeout time.Duration) (string, error)

WaitForLeader waits until a leader is elected.

type Transport

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

Transport handles communications between different raft nodes.

func NewTransport

func NewTransport(ln net.Listener) *Transport

func (*Transport) Accept

func (tn *Transport) Accept() (net.Conn, error)

Accept acceps a given dial and checks that the RaftRPC identifier is defined at the start; if not then just return an error.

func (*Transport) Addr

func (tn *Transport) Addr() net.Addr

Addr returns a net.Addr representing the address Transport is listening on.

func (*Transport) Close

func (tn *Transport) Close() error

Close closes the listener

func (*Transport) Dial

func (tn *Transport) Dial(addr raft.ServerAddress, timeout time.Duration) (net.Conn, error)

Dial creates a connection to a given address. This function appends the RaftRPC identifier (1) to the request's beginning such that raft requests can be properly identified.

Jump to

Keyboard shortcuts

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