Documentation ¶
Index ¶
- Constants
- Variables
- func NewBadgerFSM(store *BadgerStore, logger hclog.Logger) raft.FSM
- type ApplyResponse
- type Backend
- func (b *Backend) Bootstrap() error
- func (b *Backend) Get(key string) ([]byte, error)
- func (b *Backend) GetLeader() (*Peer, error)
- func (b *Backend) GetWithPrefix(prefix string) ([][]byte, error)
- func (b *Backend) IsFollower() bool
- func (b *Backend) IsLeader() bool
- func (b *Backend) LastContact() (time.Time, error)
- func (b *Backend) RaftState() string
- func (b *Backend) Start() error
- func (b *Backend) Stats() map[string]string
- func (b *Backend) Stop() error
- func (b *Backend) WaitUntilReady(maxRetries int, sleepTime time.Duration) error
- type BadgerStore
- func (b *BadgerStore) Close() error
- func (b *BadgerStore) DeleteRange(min, max uint64) error
- func (b *BadgerStore) FirstIndex() (uint64, error)
- func (b *BadgerStore) Get(k []byte) ([]byte, error)
- func (b *BadgerStore) GetLog(index uint64, log *raft.Log) error
- func (b *BadgerStore) GetUint64(key []byte) (uint64, error)
- func (b *BadgerStore) LastIndex() (uint64, error)
- func (b *BadgerStore) Set(k, v []byte) error
- func (b *BadgerStore) SetUint64(key []byte, val uint64) error
- func (b *BadgerStore) StoreLog(log *raft.Log) error
- func (b *BadgerStore) StoreLogs(logs []*raft.Log) error
- func (b *BadgerStore) Sync() error
- type Client
- func (c *Client[M]) Create(key string, data any) error
- func (c *Client[M]) CreateOrUpdate(key string, m Modeler) error
- func (c *Client[M]) Delete(key string) error
- func (c *Client[M]) Exists(key string) int
- func (c *Client[M]) GetAll() ([]*M, error)
- func (c *Client[M]) Read(key string) (*M, error)
- func (c *Client[M]) Update(key string, data any) error
- type Config
- type Modeler
- type Option
- func WithDataDir(dataDir string) Option
- func WithLogOutput(logOutput io.Writer) Option
- func WithLogger(logger hclog.Logger) Option
- func WithMaxRetainedSnapshots(maxRetainedSnapshots int) Option
- func WithPeerID(peerID string) Option
- func WithPeers(peers []string) Option
- func WithTransportMaxPool(transportMaxPool int) Option
- func WithTransportTimeout(transportTimeout time.Duration) Option
- type Payload
- type Peer
- type Snapshot
- type TestCluster
Constants ¶
const ( DefaultRaftSetTimeout = 500 * time.Millisecond DefaultRaftDeleteTimeout = 500 * time.Millisecond )
const ( RecordExists int = iota RecordDoesNotExist RecordCannotVerifyExistence )
const ( BadgerOpSet = "SET" BadgerOpDelete = "DEL" )
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 ¶
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 ¶
NewBackend instantiates a new Raft/BadgerDB backend.
func (*Backend) GetLeader ¶
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 ¶
GetWithPrefix returns all the results stored with a given prefix.
func (*Backend) IsFollower ¶
IsFollower returns true if the backend is currently the cluster follower.
func (*Backend) LastContact ¶
LastContact returns the time of last contact by a leader. This only makes sense if the backend is currently a follower.
func (*Backend) WaitUntilReady ¶
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 ¶
NewClient instantiates a new client. Note that the generic data structure must implement the Modeler interface.
func (*Client[M]) Create ¶
Create stores the data at the given key. If there's 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.
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 WithLogOutput ¶
WithLogOutput sets the log output in a config.
func WithLogger ¶
func WithLogger(logger hclog.Logger) Option
func WithMaxRetainedSnapshots ¶
WithMaxRetainedSnapshots sets the max retained snapshots in a config.
func WithTransportMaxPool ¶
WithTransportMaxPool sets the transport max pool in a config.
func WithTransportTimeout ¶
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 ¶
Peer is the data structure that holds the configuration of a raft peer.
func (*Peer) HTTPAddress ¶
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.
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.