core

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidEntry = Entry{
	Term: InvalidTerm,
	Idx:  InvalidIndex,
	Cmd:  "",
}
View Source
var NullMsg = Msg{Tp: Null}

Functions

This section is empty.

Types

type Address

type Address string
const All Address = "All"
const Composed Address = "Composed"
const InvalidId Address = "InvalidId"

type AppendEntriesReq

type AppendEntriesReq struct {
	Term         Term
	LeaderId     Address
	PrevLogIndex Index
	PrevLogTerm  Term
	Entries      []Entry
	LeaderCommit Index
}

func (*AppendEntriesReq) GetTerm

func (th *AppendEntriesReq) GetTerm() Term

type AppendEntriesResp

type AppendEntriesResp struct {
	Term    Term
	Success bool
}

func (*AppendEntriesResp) GetTerm

func (th *AppendEntriesResp) GetTerm() Term

type Candidate

type Candidate struct {
	RaftBase
	// contains filtered or unexported fields
}

func NewCandidate

func NewCandidate(f *Follower, leaderTransfer bool) *Candidate

func (*Candidate) TakeAction

func (c *Candidate) TakeAction(msg Msg) Msg

type Cluster

type Cluster struct {
	Me      Address
	Members []Address
}

type CmdReq

type CmdReq struct {
	Cmd Command
}

type CmdResp

type CmdResp struct {
	Result  interface{}
	Success bool
}

type Command

type Command interface{}

type Config

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

func InitConfig

func InitConfig(cls Cluster, eleTimeoutMin int64, eleTimeoutMax int64) Config

type ConfigChangeCmd

type ConfigChangeCmd struct {
	Members     []Address
	PrevMembers []Address
}

ConfigChangeCmd save the current and previous config snapshot

The main reason of not use rpc like {Op: Add/Remove, Address: "some-addr"}

is that:

ConfigChangeCmd act as an Entry to replicate self all across the cluster,

we can hardly to maintain this special Entry will be applied only once (unlike other Entries, ConfigChangeCmd must apply before commit). Obviously stateless cmd is more robust than stateful. (just like declarative vs. imperative)

type Entry

type Entry struct {
	Term Term
	Idx  Index
	Cmd  Command
}

type Follower

type Follower struct{ RaftBase }

func NewFollower

func NewFollower(cfg Config, sm StateMachine) *Follower

func (*Follower) TakeAction

func (f *Follower) TakeAction(msg Msg) Msg

type Index

type Index int64
const InvalidIndex Index = 0

InvalidIndex is the init value of any indexes. First valid index is 1.

type Leader

type Leader struct {
	RaftBase
	// contains filtered or unexported fields
}

func NewLeader

func NewLeader(c *Candidate) *Leader

func (*Leader) TakeAction

func (l *Leader) TakeAction(msg Msg) Msg

type Msg

type Msg struct {
	Tp      MsgType
	From    Address
	To      Address
	Payload interface{}
}

type MsgType

type MsgType int
const (
	Tick MsgType = iota
	MoveState
	Rpc
	Cmd
	Null
)

type RaftBase

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

func (*RaftBase) GetAllEntries

func (r *RaftBase) GetAllEntries() []Entry

func (*RaftBase) GetCluster

func (r *RaftBase) GetCluster() Cluster

func (*RaftBase) Resp

func (r *RaftBase) Resp(to Address, payload interface{}) Msg

type RaftObject

type RaftObject interface {
	TakeAction(msg Msg) Msg
	GetAllEntries() []Entry
	GetCluster() Cluster
}

type RequestVoteReq

type RequestVoteReq struct {
	Term           Term
	CandidateId    Address
	LastLogIndex   Index
	LastLogTerm    Term
	LeaderTransfer bool
}

func (*RequestVoteReq) GetTerm

func (th *RequestVoteReq) GetTerm() Term

type RequestVoteResp

type RequestVoteResp struct {
	Term        Term
	VoteGranted bool
}

func (*RequestVoteResp) GetTerm

func (th *RequestVoteResp) GetTerm() Term

type StateMachine

type StateMachine interface {
	Exec(cmd Command) interface{}
}

type Term

type Term int64
const InvalidTerm Term = 0

InvalidTerm is the init value of term, which will become 1 after first follower turn to candidate.

type TermHolder

type TermHolder interface {
	GetTerm() Term
}

type TimeoutNowReq

type TimeoutNowReq struct {
	Term Term
}

func (*TimeoutNowReq) GetTerm

func (th *TimeoutNowReq) GetTerm() Term

Jump to

Keyboard shortcuts

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