store

package
v0.0.0-...-5642128 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// SetOperation is for handling set operations in raft_apply.
	SetOperation byte = iota

	// GetOperation is for handling get operations in raft_apply.
	GetOperation
)

Variables

View Source
var ErrJoiningSelf = errors.New("trying to join self")

ErrJoiningSelf represents the situation where a node tries to join itself.

Functions

func NewBigcache

func NewBigcache() (*bigcache.BigCache, error)

NewBigcache creates a big cache interface that implements the Cache interface. Bigcache is slower but can store entries that are larger. While fastcache is suitable for smaller entries and at the same time a bit faster.

func NewFastcache

func NewFastcache(maxbytes int) *fastcache.Cache

Types

type Cache

type Cache interface {
	Set(key []byte, value []byte) error
	Get(key []byte) ([]byte, error)
}

type Config

type Config struct {
	DataDir           string
	BindAddr          string
	Bootstrap         bool
	LocalID           raft.ServerID
	SnapshotThreshold uint64
	StrongConsistency bool

	// Timeouts
	HeartbeatTimeout   time.Duration
	ElectionTimeout    time.Duration
	CommitTimeout      time.Duration
	LeaderLeaseTimeout time.Duration

	Transport *Transport
}

Config represents all of the user configurable fields for the Raft node. Note that without setting these defaults are also fine. Only Transport and LocalID are important.

type Store

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

Store represents a Raft node. It also implements the FSM interface that raft provides. So we can directly modify the cache stored in this struct.

func New

func New(conf Config) (*Store, error)

New creates a store instance.

func (*Store) Apply

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

Apply handles the applyRequest made by the createApplyReq function. It returns a applyResult struct such that handler functions can properly handle the given error.

func (*Store) Close

func (s *Store) Close() error

Close down this raft node and flush out possible data in the logger.

func (*Store) Get

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

Get finds a value with a given key either from this node's cache, or the leader. If the value is retrieved from a non-leader node, we risk the chance of the value not existing, or being old. On the other hand, request the value from the leader adds a lot of overhead.

func (*Store) GetServers

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

GetServers returns the server currently present in the node.

func (*Store) Join

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

Join adds a node with 'id' and 'addr' into the raft cluster. The address is the raft bind address of the node.

func (*Store) JoinNonVoter

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

JoinNonVoter adds a node that cannot write into the cluster. These are useful for adding more reability to reads.

func (*Store) LeaderAddr

func (s *Store) LeaderAddr() string

LeaderAddr returns the raft bind address of the leader node.

func (*Store) Leave

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

Leave removes a node from the cluster with the given id. This function is called by registry to let the raft node know that a node has left the cluster and that raft should not try to contact it anymore.

func (*Store) Restore

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

Restore takes in the bytes generated by snapshot.Persist() and parses the cache state from that.

func (*Store) Set

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

Set applies a given key-value pair into the raft cluster. Since writing a key is a leader-only operation, we need to check for that as well.

func (*Store) Snapshot

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

Snapshot takes a snapshot of the current finite state machine and logs the time so we can see how long a snapshot process took.

func (*Store) Stepdown

func (s *Store) Stepdown(wait bool) error

Stepdown forces the cluster to change the leadership.

func (*Store) WaitForLeader

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

WaitForLeader waits until a leader is elected. If a leader hasn't been elected in the given timeout return an error.

type Transport

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

Transport handles communications between different raft nodes.

func NewTLSTransport

func NewTLSTransport(ln net.Listener, servertls *tls.Config, peertls *tls.Config) *Transport

NewTLSTransport creates a transport instance with the tls fields populated.

func NewTransport

func NewTransport(ln net.Listener) *Transport

NewTransport creates a new transport instance.

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