raftstorage

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package raftstorage implements a Raft-backed storage provider.

Index

Constants

View Source
const (
	// DefaultListenPort is the default raft listen port
	DefaultListenPort = 9000
	// DefaultListenAddress is the default raft listen address
	DefaultListenAddress = "[::]:9000"
)
View Source
const (
	Follower  = raft.Follower
	Candidate = raft.Candidate
	Leader    = raft.Leader
	Shutdown  = raft.Shutdown
)

Raft states.

View Source
const (
	Voter    = raft.Voter
	Nonvoter = raft.Nonvoter
)

Raft suffrage states.

Variables

View Source
var DefaultDataDir = func() string {
	if runtime.GOOS == "windows" {
		return "C:\\ProgramData\\webmesh\\store"
	}
	return "/var/lib/webmesh/store"
}()

DefaultDataDir is the default data directory.

Functions

This section is empty.

Types

type Consensus

type Consensus struct {
	*Provider
}

RaftConsensus is the Raft consensus implementation.

func (*Consensus) AddObserver

func (r *Consensus) AddObserver(ctx context.Context, peer *v1.StoragePeer) error

AddObserver adds an observer to the consensus group.

func (*Consensus) AddVoter

func (r *Consensus) AddVoter(ctx context.Context, peer *v1.StoragePeer) error

AddVoter adds a voter to the consensus group.

func (*Consensus) DemoteVoter

func (r *Consensus) DemoteVoter(ctx context.Context, peer *v1.StoragePeer) error

DemoteVoter demotes a voter to an observer.

func (*Consensus) GetLeader

func (r *Consensus) GetLeader(ctx context.Context) (*v1.StoragePeer, error)

GetLeader returns the leader of the cluster.

func (*Consensus) IsLeader

func (r *Consensus) IsLeader() bool

IsLeader returns true if the Raft node is the leader.

func (*Consensus) IsMember

func (r *Consensus) IsMember() bool

IsMember returns true if the Raft node is a member of the cluster.

func (*Consensus) RemovePeer

func (r *Consensus) RemovePeer(ctx context.Context, peer *v1.StoragePeer, wait bool) error

RemovePeer removes a peer from the consensus group.

type LeaderObservation

type LeaderObservation = raft.LeaderObservation

LeaderObservation is an alias for raft.LeaderObservation.

type MonotonicLogStore

type MonotonicLogStore struct {
	raft.LogStore
}

MonotonicLogStore is a LogStore that is monotonic.

func (*MonotonicLogStore) IsMonotonic

func (m *MonotonicLogStore) IsMonotonic() bool

IsMonotonic returns true if the log store is monotonic.

type Observation

type Observation = raft.Observation

Observation is an alias for raft.Observation.

type ObservationCallback

type ObservationCallback func(ctx context.Context, obs Observation)

ObservationCallback is a callback that can be registered for when an observation is received.

type Options

type Options struct {
	// NodeID is the node ID.
	NodeID string
	// Transport is the Raft transport to use for communicating with
	// other Raft nodes.
	Transport transport.RaftTransport
	// DataDir is the directory to store data in.
	DataDir string
	// ClearDataDir is if the data directory should be cleared on startup.
	ClearDataDir bool
	// InMemory is if the store should be in memory. This should only be used for testing and ephemeral nodes.
	InMemory bool
	// ConnectionPoolCount is the number of connections to pool. If 0, no connection pooling is used.
	ConnectionPoolCount int
	// ConnectionTimeout is the timeout for connections.
	ConnectionTimeout time.Duration
	// HeartbeatTimeout is the timeout for heartbeats.
	HeartbeatTimeout time.Duration
	// ElectionTimeout is the timeout for elections.
	ElectionTimeout time.Duration
	// ApplyTimeout is the timeout for applying.
	ApplyTimeout time.Duration
	// CommitTimeout is the timeout for committing.
	CommitTimeout time.Duration
	// MaxAppendEntries is the maximum number of append entries.
	MaxAppendEntries int
	// LeaderLeaseTimeout is the timeout for leader leases.
	LeaderLeaseTimeout time.Duration
	// SnapshotInterval is the interval to take snapshots.
	SnapshotInterval time.Duration
	// SnapshotThreshold is the threshold to take snapshots.
	SnapshotThreshold uint64
	// SnapshotRetention is the number of snapshots to retain.
	SnapshotRetention uint64
	// ObserverChanBuffer is the buffer size for the observer channel.
	ObserverChanBuffer int
	// LogLevel is the log level for the raft backend.
	LogLevel string
}

Options are the raft options.

func NewOptions

func NewOptions(nodeID string) Options

NewOptions returns new raft options with sensible defaults.

func (*Options) RaftConfig

func (o *Options) RaftConfig(ctx context.Context, nodeID string) *raft.Config

RaftConfig builds a raft config.

type PeerObservation

type PeerObservation = raft.PeerObservation

PeerObservation is an alias for raft.PeerObservation.

type Provider

type Provider struct {
	Options
	// contains filtered or unexported fields
}

RaftStorage is a storage provider that uses Raft for consensus. BadgerDB is used for the underlying storage.

func NewProvider

func NewProvider(opts Options) *Provider

NewProvider returns a new RaftStorageProvider.

func (*Provider) Apply

func (r *Provider) Apply(ctx context.Context, log *v1.RaftLogEntry) (*v1.RaftApplyResponse, error)

Apply applies a raft log entry.

func (*Provider) Bootstrap

func (r *Provider) Bootstrap(ctx context.Context) error

Bootstrap bootstraps the raft storage provider.

func (*Provider) Close

func (r *Provider) Close() error

Close closes the mesh storage and shuts down the raft instance.

func (*Provider) Configuration

func (r *Provider) Configuration() (raft.Configuration, error)

Configuration returns the current raft configuration.

func (*Provider) Consensus

func (r *Provider) Consensus() storage.Consensus

Consensus returns the underlying Consensus instance.

func (*Provider) IsObserver

func (r *Provider) IsObserver() bool

IsObserver returns true if the Raft node is an observer.

func (*Provider) IsVoter

func (r *Provider) IsVoter() bool

IsVoter returns true if the Raft node is a voter.

func (*Provider) ListenPort

func (r *Provider) ListenPort() uint16

ListenPort returns the TCP port that the storage provider is listening on.

func (*Provider) MeshStorage

func (r *Provider) MeshStorage() storage.MeshStorage

MeshStorage returns the underlying MeshStorage instance.

func (*Provider) OnObservation

func (r *Provider) OnObservation(cb ObservationCallback)

OnObservation registers a callback for when an observation is received.

func (*Provider) Start

func (r *Provider) Start(ctx context.Context) error

Start starts the raft storage provider.

func (*Provider) Status

func (r *Provider) Status() *v1.StorageStatus

Status returns the status of the storage provider.

type RaftStorage

type RaftStorage struct {
	storage.MeshStorage
	// contains filtered or unexported fields
}

RaftStorage wraps the storage.Storage interface to force write operations through the Raft log.

func (*RaftStorage) Delete

func (rs *RaftStorage) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*RaftStorage) PutValue

func (rs *RaftStorage) PutValue(ctx context.Context, key, value string, ttl time.Duration) error

Put sets the value of a key.

func (*RaftStorage) Restore

func (rs *RaftStorage) Restore(ctx context.Context, r io.Reader) error

Restore restores a snapshot of the storage.

func (*RaftStorage) Snapshot

func (rs *RaftStorage) Snapshot(ctx context.Context) (io.Reader, error)

Snapshot returns a snapshot of the storage.

type SnapshotMeta

type SnapshotMeta = raft.SnapshotMeta

SnapshotMeta is an alias for raft.SnapshotMeta.

Directories

Path Synopsis
Package fsm implements the Raft FSM.
Package fsm implements the Raft FSM.

Jump to

Keyboard shortcuts

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