raft

package
v4.0.0-...-1d6e2d7 Latest Latest
Warning

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

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

Documentation

Overview

Package raft is a distributed consensus package that implements the Raft protocol.

This package is internally used by Dragonboat, applications are not expected to import this package.

Index

Constants

View Source
const (
	// NoLeader is the flag used to indcate that there is no leader or the leader
	// is unknown.
	NoLeader uint64 = 0
	// NoNode is the flag used to indicate that the node id field is not set.
	NoNode uint64 = 0
)

Variables

View Source
var ErrCompacted = errors.New("entry compacted")

ErrCompacted is the error returned to indicate that the requested entries are no longer in the LogDB due to compaction.

View Source
var ErrSnapshotOutOfDate = errors.New("snapshot out of date")

ErrSnapshotOutOfDate is the error returned to indicate that the concerned snapshot is considered as out of date.

View Source
var ErrUnavailable = errors.New("entry unavailable")

ErrUnavailable is the error returned to indicate that requested entries are not available in LogDB.

Functions

func IsLocalMessageType

func IsLocalMessageType(t pb.MessageType) bool

IsLocalMessageType returns a boolean value indicating whether the specified message type is a local message type.

func ReplicaID

func ReplicaID(replicaID uint64) string

ReplicaID returns a human friendly form of ReplicaID for logging purposes.

func ShardID

func ShardID(shardID uint64) string

ShardID returns a human friendly form of ShardID for logging purposes.

Types

type ILogDB

type ILogDB interface {
	// GetRange returns the range of the entries in LogDB.
	GetRange() (uint64, uint64)
	// SetRange updates the ILogDB to extend the entry range known to the ILogDB.
	SetRange(index uint64, length uint64)
	// NodeState returns the state of the node persistent in LogDB.
	NodeState() (pb.State, pb.Membership)
	// SetState sets the persistent state known to ILogDB.
	SetState(ps pb.State)
	// CreateSnapshot sets the snapshot known to ILogDB
	CreateSnapshot(ss pb.Snapshot) error
	// ApplySnapshot makes the snapshot known to ILogDB and also update the entry
	// range known to ILogDB.
	ApplySnapshot(ss pb.Snapshot) error
	// Term returns the entry term of the specified entry.
	Term(index uint64) (uint64, error)
	// Entries returns entries between [low, high) with total size of entries
	// limited to maxSize bytes.
	Entries(low uint64, high uint64, maxSize uint64) ([]pb.Entry, error)
	// Snapshot returns the metadata for the most recent snapshot known to the
	// LogDB.
	Snapshot() pb.Snapshot
	// Compact performs entry range compaction on ILogDB up to the entry
	// specified by index.
	Compact(index uint64) error
	// Append makes the given entries known to the ILogDB instance. This is
	// usually not how entries are persisted.
	Append(entries []pb.Entry) error
}

ILogDB is a read-only interface to the underlying persistent storage to allow the raft package to access raft state, entries, snapshots stored in the persistent storage. Entries stored in the persistent storage accessible via ILogDB is usually not required in normal cases.

type LogTestHelper

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

LogTestHelper is a helper type used for testing logEntry.

func NewLog

func NewLog(logdb ILogDB) *LogTestHelper

NewLog creates and returns a new LogTestHelper instance used for testing purpose.

func (*LogTestHelper) AllEntries

func (l *LogTestHelper) AllEntries() []pb.Entry

AllEntries ...

func (*LogTestHelper) Append

func (l *LogTestHelper) Append(ents []pb.Entry) error

Append ...

func (*LogTestHelper) AppliedTo

func (l *LogTestHelper) AppliedTo(index uint64)

AppliedTo ...

func (*LogTestHelper) CheckBound

func (l *LogTestHelper) CheckBound(low uint64, high uint64) error

CheckBound ...

func (*LogTestHelper) Entries

func (l *LogTestHelper) Entries(start uint64,
	maxsize uint64) ([]pb.Entry, error)

Entries ...

func (*LogTestHelper) EntriesToApply

func (l *LogTestHelper) EntriesToApply() ([]pb.Entry, error)

EntriesToApply ...

func (*LogTestHelper) EntriesToSave

func (l *LogTestHelper) EntriesToSave() []pb.Entry

EntriesToSave ...

func (*LogTestHelper) FirstIndex

func (l *LogTestHelper) FirstIndex() uint64

FirstIndex ...

func (*LogTestHelper) GetCommitted

func (l *LogTestHelper) GetCommitted() uint64

GetCommitted ...

func (*LogTestHelper) GetConflictIndex

func (l *LogTestHelper) GetConflictIndex(ents []pb.Entry) (uint64, error)

GetConflictIndex ...

func (*LogTestHelper) GetEntries

func (l *LogTestHelper) GetEntries(low uint64, high uint64,
	maxsize uint64) ([]pb.Entry, error)

GetEntries ...

func (*LogTestHelper) HasEntriesToApply

func (l *LogTestHelper) HasEntriesToApply() bool

HasEntriesToApply ...

func (*LogTestHelper) LastIndex

func (l *LogTestHelper) LastIndex() uint64

LastIndex ...

func (*LogTestHelper) MatchTerm

func (l *LogTestHelper) MatchTerm(index uint64, term uint64) (bool, error)

MatchTerm ...

func (*LogTestHelper) SetCommitted

func (l *LogTestHelper) SetCommitted(v uint64)

SetCommitted ...

func (*LogTestHelper) Term

func (l *LogTestHelper) Term(index uint64) (uint64, error)

Term ...

func (*LogTestHelper) TryAppend

func (l *LogTestHelper) TryAppend(index uint64, logTerm uint64,
	committed uint64, ents []pb.Entry) (uint64, bool, error)

TryAppend ...

func (*LogTestHelper) TryCommit

func (l *LogTestHelper) TryCommit(index uint64, term uint64) (bool, error)

TryCommit ...

func (*LogTestHelper) UnstableOffset

func (l *LogTestHelper) UnstableOffset() uint64

UnstableOffset ...

func (*LogTestHelper) UpToDate

func (l *LogTestHelper) UpToDate(index uint64, term uint64) (bool, error)

UpToDate ...

type Peer

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

Peer is the interface struct for interacting with the underlying Raft protocol implementation.

func Launch

func Launch(config config.Config,
	logdb ILogDB, events server.IRaftEventListener,
	addresses []PeerAddress, initial bool, newNode bool) Peer

Launch starts or restarts a Raft node.

func (*Peer) ApplyConfigChange

func (p *Peer) ApplyConfigChange(cc pb.ConfigChange) error

ApplyConfigChange applies a raft membership change to the local raft node.

func (*Peer) Commit

func (p *Peer) Commit(ud pb.Update)

Commit commits the Update state to mark it as processed.

func (*Peer) GetUpdate

func (p *Peer) GetUpdate(moreToApply bool,
	lastApplied uint64) (pb.Update, error)

GetUpdate returns the current state of the Peer.

func (*Peer) Handle

func (p *Peer) Handle(m pb.Message) error

Handle processes the given message.

func (*Peer) HasEntryToApply

func (p *Peer) HasEntryToApply() bool

HasEntryToApply returns a boolean flag indicating whether there are more entries ready to be applied.

func (*Peer) HasUpdate

func (p *Peer) HasUpdate(moreToApply bool) bool

HasUpdate returns a boolean value indicating whether there is any Update ready to be processed.

func (*Peer) NotifyRaftLastApplied

func (p *Peer) NotifyRaftLastApplied(lastApplied uint64)

NotifyRaftLastApplied passes on the lastApplied index confirmed by the RSM to the raft state machine.

func (*Peer) ProposeConfigChange

func (p *Peer) ProposeConfigChange(cc pb.ConfigChange, key uint64) error

ProposeConfigChange proposes a raft membership change.

func (*Peer) ProposeEntries

func (p *Peer) ProposeEntries(ents []pb.Entry) error

ProposeEntries proposes specified entries in a batched mode using a single MTPropose message.

func (*Peer) QueryRaftLog

func (p *Peer) QueryRaftLog(firstIndex uint64,
	lastIndex uint64, maxSize uint64) error

func (*Peer) QuiescedTick

func (p *Peer) QuiescedTick() error

QuiescedTick moves the logical clock forward by one tick in quiesced mode.

func (*Peer) RateLimited

func (p *Peer) RateLimited() bool

RateLimited returns a boolean flag indicating whether the Raft node is rate limited.

func (*Peer) ReadIndex

func (p *Peer) ReadIndex(ctx pb.SystemCtx) error

ReadIndex starts a ReadIndex operation. The ReadIndex protocol is defined in the section 6.4 of the Raft thesis.

func (*Peer) RejectConfigChange

func (p *Peer) RejectConfigChange() error

RejectConfigChange rejects the currently pending raft membership change.

func (*Peer) ReportSnapshotStatus

func (p *Peer) ReportSnapshotStatus(replicaID uint64, reject bool) error

ReportSnapshotStatus reports the status of the snapshot to the local raft node.

func (*Peer) ReportUnreachableNode

func (p *Peer) ReportUnreachableNode(replicaID uint64) error

ReportUnreachableNode marks the specified node as not reachable.

func (*Peer) RequestLeaderTransfer

func (p *Peer) RequestLeaderTransfer(target uint64) error

RequestLeaderTransfer makes a request to transfer the leadership to the specified target node.

func (*Peer) RestoreRemotes

func (p *Peer) RestoreRemotes(ss pb.Snapshot) error

RestoreRemotes applies the remotes info obtained from the specified snapshot.

func (*Peer) Tick

func (p *Peer) Tick() error

Tick moves the logical clock forward by one tick.

type PeerAddress

type PeerAddress struct {
	Address   string
	ReplicaID uint64
}

PeerAddress is the basic info for a peer in the Raft shard.

type State

type State uint64

State is the state of a raft node defined in the raft thesis.

func (State) String

func (st State) String() string

type Status

type Status struct {
	ReplicaID uint64
	ShardID   uint64
	Applied   uint64
	LeaderID  uint64
	NodeState State
	pb.State
}

Status is the struct that captures the status of a raft node.

func (*Status) IsFollower

func (s *Status) IsFollower() bool

IsFollower returns a boolean value indicating whether the node is a follower.

func (*Status) IsLeader

func (s *Status) IsLeader() bool

IsLeader returns a boolean value indicating whether the node is leader.

Jump to

Keyboard shortcuts

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