raft

package
v3.3.8 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 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 ClusterID

func ClusterID(clusterID uint64) string

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

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 NodeID

func NodeID(nodeID uint64) string

NodeID returns a human friendly form of NodeID 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 added in v3.2.0

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

LogTestHelper is a helper type used for testing logEntry.

func NewLog added in v3.2.0

func NewLog(logdb ILogDB) *LogTestHelper

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

func (*LogTestHelper) AllEntries added in v3.2.0

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

AllEntries ...

func (*LogTestHelper) Append added in v3.2.0

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

Append ...

func (*LogTestHelper) AppliedTo added in v3.2.0

func (l *LogTestHelper) AppliedTo(index uint64)

AppliedTo ...

func (*LogTestHelper) CheckBound added in v3.2.0

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

CheckBound ...

func (*LogTestHelper) Entries added in v3.2.0

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

Entries ...

func (*LogTestHelper) EntriesToApply added in v3.2.0

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

EntriesToApply ...

func (*LogTestHelper) EntriesToSave added in v3.2.0

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

EntriesToSave ...

func (*LogTestHelper) FirstIndex added in v3.2.0

func (l *LogTestHelper) FirstIndex() uint64

FirstIndex ...

func (*LogTestHelper) GetCommitted added in v3.2.0

func (l *LogTestHelper) GetCommitted() uint64

GetCommitted ...

func (*LogTestHelper) GetConflictIndex added in v3.2.0

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

GetConflictIndex ...

func (*LogTestHelper) GetEntries added in v3.2.0

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

GetEntries ...

func (*LogTestHelper) HasEntriesToApply added in v3.2.0

func (l *LogTestHelper) HasEntriesToApply() bool

HasEntriesToApply ...

func (*LogTestHelper) LastIndex added in v3.2.0

func (l *LogTestHelper) LastIndex() uint64

LastIndex ...

func (*LogTestHelper) MatchTerm added in v3.2.0

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

MatchTerm ...

func (*LogTestHelper) SetCommitted added in v3.2.0

func (l *LogTestHelper) SetCommitted(v uint64)

SetCommitted ...

func (*LogTestHelper) Term added in v3.2.0

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

Term ...

func (*LogTestHelper) TryAppend added in v3.2.0

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

TryAppend ...

func (*LogTestHelper) TryCommit added in v3.2.0

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

TryCommit ...

func (*LogTestHelper) UnstableOffset added in v3.2.0

func (l *LogTestHelper) UnstableOffset() uint64

UnstableOffset ...

func (*LogTestHelper) UpToDate added in v3.2.0

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

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)

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

GetUpdate returns the current state of the Peer.

func (*Peer) Handle

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

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(configChange pb.ConfigChange, key uint64)

ProposeConfigChange proposes a raft membership change.

func (*Peer) ProposeEntries

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

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

func (*Peer) QuiescedTick

func (p *Peer) QuiescedTick()

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)

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()

RejectConfigChange rejects the currently pending raft membership change.

func (*Peer) ReportSnapshotStatus

func (p *Peer) ReportSnapshotStatus(nodeID uint64, reject bool)

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

func (*Peer) ReportUnreachableNode

func (p *Peer) ReportUnreachableNode(nodeID uint64)

ReportUnreachableNode marks the specified node as not reachable.

func (*Peer) RequestLeaderTransfer

func (p *Peer) RequestLeaderTransfer(target uint64)

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

func (*Peer) RestoreRemotes

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

RestoreRemotes applies the remotes info obtained from the specified snapshot.

func (*Peer) Tick

func (p *Peer) Tick()

Tick moves the logical clock forward by one tick.

type PeerAddress

type PeerAddress struct {
	NodeID  uint64
	Address string
}

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

type State

type State uint64

State is the state of a raft node defined in the raft paper, possible states are leader, follower, candidate and observer. Observer is non-voting member node.

func (State) String

func (st State) String() string

type Status

type Status struct {
	NodeID    uint64
	ClusterID 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