raft

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: CC0-1.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultRaftSetTimeout    = 500 * time.Millisecond
	DefaultRaftDeleteTimeout = 500 * time.Millisecond
)
View Source
const (
	RecordExists int = iota
	RecordDoesNotExist
	RecordCannotVerifyExistence
)
View Source
const (
	BadgerOpSet    = "SET"
	BadgerOpDelete = "DEL"
)
View Source
const ModelSeparator = "|"

ModelSeparator is used to namespace a model table name and avoid bugs due to model names with the same prefix, e.g. `foo` and `foobar`.

Variables

View Source
var (
	ErrKeyNotFound = errors.New("not found")
)

Functions

func NewBadgerFSM

func NewBadgerFSM(store *BadgerStore, logger hclog.Logger) raft.FSM

NewBadgerFSM instantiates a new badger FSM

Types

type ApplyResponse

type ApplyResponse struct {
	Error error
	Data  interface{}
}

ApplyResponse is the response return by an FSM apply.

type Backend

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

Backend runs a Raft/BadgerDB cluster.

func NewBackend

func NewBackend(cfg *Config) *Backend

NewBackend instantiates a new Raft/BadgerDB backend.

func (*Backend) Bootstrap

func (b *Backend) Bootstrap() error

Bootstrap bootstraps the raft cluster.

func (*Backend) Get

func (b *Backend) Get(key string) ([]byte, error)

Get returns the data stored at the given key.

func (*Backend) GetLeader

func (b *Backend) GetLeader() (*Peer, error)

GetLeader attempts to return the raft cluster leader. If there is no current cluster leader, or if the leader is unknown, an error is returned.

func (*Backend) GetWithPrefix

func (b *Backend) GetWithPrefix(prefix string) ([][]byte, error)

GetWithPrefix returns all the results stored with a given prefix.

func (*Backend) IsFollower

func (b *Backend) IsFollower() bool

IsFollower returns true if the backend is currently the cluster follower.

func (*Backend) IsLeader

func (b *Backend) IsLeader() bool

IsLeader returns true if the backend is currently the cluster leader.

func (*Backend) LastContact

func (b *Backend) LastContact() (time.Time, error)

LastContact returns the time of last contact by a leader. This only makes sense if the backend is currently a follower.

func (*Backend) RaftState

func (b *Backend) RaftState() string

RaftState returns the current raft state.

func (*Backend) Start

func (b *Backend) Start() error

Start initialises and starts a Raft/BadgerDB raft.

func (*Backend) Stats

func (b *Backend) Stats() map[string]string

Stats returns the raft stats.

func (*Backend) Stop

func (b *Backend) Stop() error

Stop stops the raft backend.

func (*Backend) WaitUntilReady

func (b *Backend) WaitUntilReady(maxRetries int, sleepTime time.Duration) error

WaitUntilReady checks if we can get a cluster leader. If we can, we assume a successful cluster has been formed. If we cannot, we keep trying for the given max retries, sleeping with the given duration between retries.

type BadgerStore

type BadgerStore struct {
	// DB is the underlying handle to the db.
	DB *badger.DB
	// contains filtered or unexported fields
}

BadgerStore provides access to BadgerDB for Raft to store and retrieve log entries. It also provides key/value storage, and can be used as a LogStore and StableStore.

func NewBadgerStore

func NewBadgerStore(options badger.Options) (*BadgerStore, error)

NewBadgerStore uses the supplied options to open the BadgerDB and prepares it for its use as a raft backend.

func (*BadgerStore) Close

func (b *BadgerStore) Close() error

Close is used to gracefully close the DB connection.

func (*BadgerStore) DeleteRange

func (b *BadgerStore) DeleteRange(min, max uint64) error

DeleteRange is used to delete logs within a given range inclusively.

func (*BadgerStore) FirstIndex

func (b *BadgerStore) FirstIndex() (uint64, error)

func (*BadgerStore) Get

func (b *BadgerStore) Get(k []byte) ([]byte, error)

Get is used to retrieve a value from the k/v store by key

func (*BadgerStore) GetLog

func (b *BadgerStore) GetLog(index uint64, log *raft.Log) error

GetLog reads a log at the given index and if found attempts to store it at the given log.

func (*BadgerStore) GetUint64

func (b *BadgerStore) GetUint64(key []byte) (uint64, error)

GetUint64 is like Get, but handles uint64 values

func (*BadgerStore) LastIndex

func (b *BadgerStore) LastIndex() (uint64, error)

LastIndex returns the last known index from the Raft log.

func (*BadgerStore) Set

func (b *BadgerStore) Set(k, v []byte) error

Set is used to set a key/value set outside the raft log

func (*BadgerStore) SetUint64

func (b *BadgerStore) SetUint64(key []byte, val uint64) error

SetUint64 is like Set, but handles uint64 values

func (*BadgerStore) StoreLog

func (b *BadgerStore) StoreLog(log *raft.Log) error

StoreLog stores a single Raft log.

func (*BadgerStore) StoreLogs

func (b *BadgerStore) StoreLogs(logs []*raft.Log) error

StoreLogs stores many Raft logs.

func (*BadgerStore) Sync

func (b *BadgerStore) Sync() error

Sync performs a fsync on the database file handle. This is not necessary under normal operation unless NoSync is enabled, in which this forces the database file to sync against the disk.

type Client

type Client[M Modeler] struct {
	// contains filtered or unexported fields
}

Client is the generic client that can perform CRUD operations on a Raft/BadgerDB raft backend. Write operations must be run on the current Raft leader, else the client will return an error.

func NewClient

func NewClient[M Modeler](b *Backend) *Client[M]

NewClient instantiates a new client. Note that the generic data structure must implement the Modeler interface.

func (*Client[M]) Create

func (c *Client[M]) Create(key string, data any) error

Create stores the data at the given key. If there's existing data stored at the key it returns an error.

func (*Client[M]) CreateOrUpdate

func (c *Client[M]) CreateOrUpdate(key string, m Modeler) error

func (*Client[M]) Delete

func (c *Client[M]) Delete(key string) error

Delete deletes the data at the given key.

func (*Client[M]) Exists

func (c *Client[M]) Exists(key string) int

Exists attempts to check if the data at the given key exists or not.

func (*Client[M]) GetAll

func (c *Client[M]) GetAll() ([]*M, error)

GetAll returns all the records found for a given model.

func (*Client[M]) Read

func (c *Client[M]) Read(key string) (*M, error)

Read reads the data at the given key.

func (*Client[M]) Update

func (c *Client[M]) Update(key string, data any) error

Update updates the data found at the given key. If there is no existing data stored at the key it returns an error.

type Config

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

Config is the data structure used to pass the configuration to a raft backend.

func NewConfig

func NewConfig(opts ...Option) (*Config, error)

func (*Config) Validate

func (c *Config) Validate() error

type Modeler

type Modeler interface {
	// Model should return a unique string for the model. Think of this as a table name in a relational database.
	Model() string
}

Modeler is a generic database model for storing and retrieving data in BadgerDB/Raft.

type User struct {
 Email string `json:"email"`
 Age   int	  `json:"age"`
}

func(u User) Model() string {
	return "users"
}

type Option

type Option func(*Config)

func WithDataDir

func WithDataDir(dataDir string) Option

func WithLogOutput

func WithLogOutput(logOutput io.Writer) Option

WithLogOutput sets the log output in a config.

func WithLogger

func WithLogger(logger hclog.Logger) Option

func WithMaxRetainedSnapshots

func WithMaxRetainedSnapshots(maxRetainedSnapshots int) Option

WithMaxRetainedSnapshots sets the max retained snapshots in a config.

func WithPeerID

func WithPeerID(peerID string) Option

WithPeerID sets the peer ID in the config.

func WithPeers

func WithPeers(peers []string) Option

WithPeers sets the peers in a config.

func WithTransportMaxPool

func WithTransportMaxPool(transportMaxPool int) Option

WithTransportMaxPool sets the transport max pool in a config.

func WithTransportTimeout

func WithTransportTimeout(transportTimeout time.Duration) Option

WithTransportTimeout sets the transport timeout in a config.

type Payload

type Payload struct {
	// Badger type of operation - can be `SET` or `DEL`
	OP    string
	Key   string
	Value interface{}
}

Payload is the data structure that holds the data needed to write to Badger.

type Peer

type Peer struct {
	Address  string
	RaftPort int
	HTTPPort int
}

Peer is the data structure that holds the configuration of a raft peer.

func NewPeer

func NewPeer(peer string) (*Peer, error)

NewPeer parses a string in the format address:raftPort:httpPort and returns a Peer

func (*Peer) HTTPAddress

func (p *Peer) HTTPAddress() string

HTTPAddress returns the raft HTTP address

func (*Peer) RaftServerAddress

func (p *Peer) RaftServerAddress() raft.ServerAddress

RaftServerAddress returns the raft transport layer address.

type Snapshot

type Snapshot struct{}

Snapshot is a data structure used to satisfy the raft.FSMSnapshot interface. Currently, it is not implemented.

func (Snapshot) Persist

func (s Snapshot) Persist(_ raft.SnapshotSink) error

Persist is not implemented.

func (Snapshot) Release

func (s Snapshot) Release()

Release is not implemented.

type TestCluster

type TestCluster struct {
	BackendLeader    *Backend
	BackendFollowers []*Backend
	PeerLeader       *Peer
	PeerFollowers    []*Peer
	ConfigLeader     *config.Config
	ConfigFollowers  []*config.Config
}

func NewTestCluster

func NewTestCluster(t *testing.T, sleepAfterStart time.Duration, c1 *config.Config, c2 *config.Config, c3 *config.Config) (*TestCluster, func() error, error)

NewTestCluster starts a new Raft cluster to be used for testing purposes. The second value returned is a cancel function that can be wrapped in a `defer` statement to clean the test.

Jump to

Keyboard shortcuts

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