raft

package
v0.0.0-...-e82ecba Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultRaftStateLogName = "raft-state-log"
	DefaultSnapshotLogName  = "raft-snapshot-log"
)
View Source
const (
	InstallSnapshotToStore = "installSnapShot"
	AddMemeberToRaft       = "addMemberToRaft"
)

Variables

Functions

func RegisterRaftServer

func RegisterRaftServer(s *grpc.Server, srv RaftServer)

Types

type AppendEntriesReply

type AppendEntriesReply struct {
	Term      int32     `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	Success   bool      `protobuf:"varint,2,opt,name=Success,proto3" json:"Success,omitempty"`
	NextIndex int32     `protobuf:"varint,3,opt,name=NextIndex,proto3" json:"NextIndex,omitempty"`
	Base      *RaftBase `protobuf:"bytes,4,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*AppendEntriesReply) Descriptor deprecated

func (*AppendEntriesReply) Descriptor() ([]byte, []int)

Deprecated: Use AppendEntriesReply.ProtoReflect.Descriptor instead.

func (*AppendEntriesReply) GetBase

func (x *AppendEntriesReply) GetBase() *RaftBase

func (*AppendEntriesReply) GetNextIndex

func (x *AppendEntriesReply) GetNextIndex() int32

func (*AppendEntriesReply) GetSuccess

func (x *AppendEntriesReply) GetSuccess() bool

func (*AppendEntriesReply) GetTerm

func (x *AppendEntriesReply) GetTerm() int32

func (*AppendEntriesReply) ProtoMessage

func (*AppendEntriesReply) ProtoMessage()

func (*AppendEntriesReply) ProtoReflect

func (x *AppendEntriesReply) ProtoReflect() protoreflect.Message

func (*AppendEntriesReply) Reset

func (x *AppendEntriesReply) Reset()

func (*AppendEntriesReply) String

func (x *AppendEntriesReply) String() string

type AppendEntriesRequest

type AppendEntriesRequest struct {
	Term         int32       `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	LeaderName   string      `protobuf:"bytes,2,opt,name=LeaderName,proto3" json:"LeaderName,omitempty"`
	PrevLogIndex int32       `protobuf:"varint,3,opt,name=PrevLogIndex,proto3" json:"PrevLogIndex,omitempty"`
	PrevLogTerm  int32       `protobuf:"varint,4,opt,name=PrevLogTerm,proto3" json:"PrevLogTerm,omitempty"`
	Entries      []*LogEntry `protobuf:"bytes,5,rep,name=Entries,proto3" json:"Entries,omitempty"`
	LeaderCommit int32       `protobuf:"varint,6,opt,name=LeaderCommit,proto3" json:"LeaderCommit,omitempty"`
	Base         *RaftBase   `protobuf:"bytes,7,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*AppendEntriesRequest) Descriptor deprecated

func (*AppendEntriesRequest) Descriptor() ([]byte, []int)

Deprecated: Use AppendEntriesRequest.ProtoReflect.Descriptor instead.

func (*AppendEntriesRequest) GetBase

func (x *AppendEntriesRequest) GetBase() *RaftBase

func (*AppendEntriesRequest) GetEntries

func (x *AppendEntriesRequest) GetEntries() []*LogEntry

func (*AppendEntriesRequest) GetLeaderCommit

func (x *AppendEntriesRequest) GetLeaderCommit() int32

func (*AppendEntriesRequest) GetLeaderName

func (x *AppendEntriesRequest) GetLeaderName() string

func (*AppendEntriesRequest) GetPrevLogIndex

func (x *AppendEntriesRequest) GetPrevLogIndex() int32

func (*AppendEntriesRequest) GetPrevLogTerm

func (x *AppendEntriesRequest) GetPrevLogTerm() int32

func (*AppendEntriesRequest) GetTerm

func (x *AppendEntriesRequest) GetTerm() int32

func (*AppendEntriesRequest) ProtoMessage

func (*AppendEntriesRequest) ProtoMessage()

func (*AppendEntriesRequest) ProtoReflect

func (x *AppendEntriesRequest) ProtoReflect() protoreflect.Message

func (*AppendEntriesRequest) Reset

func (x *AppendEntriesRequest) Reset()

func (*AppendEntriesRequest) String

func (x *AppendEntriesRequest) String() string

type ApplyMsg

type ApplyMsg struct {
	CommandValid bool
	CommandIndex int32
	Command      []byte
}

type InstallSnapshotReply

type InstallSnapshotReply struct {
	Term int32     `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	Base *RaftBase `protobuf:"bytes,2,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*InstallSnapshotReply) Descriptor deprecated

func (*InstallSnapshotReply) Descriptor() ([]byte, []int)

Deprecated: Use InstallSnapshotReply.ProtoReflect.Descriptor instead.

func (*InstallSnapshotReply) GetBase

func (x *InstallSnapshotReply) GetBase() *RaftBase

func (*InstallSnapshotReply) GetTerm

func (x *InstallSnapshotReply) GetTerm() int32

func (*InstallSnapshotReply) ProtoMessage

func (*InstallSnapshotReply) ProtoMessage()

func (*InstallSnapshotReply) ProtoReflect

func (x *InstallSnapshotReply) ProtoReflect() protoreflect.Message

func (*InstallSnapshotReply) Reset

func (x *InstallSnapshotReply) Reset()

func (*InstallSnapshotReply) String

func (x *InstallSnapshotReply) String() string

type InstallSnapshotRequest

type InstallSnapshotRequest struct {
	Term              int32     `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	LeaderName        string    `protobuf:"bytes,2,opt,name=LeaderName,proto3" json:"LeaderName,omitempty"`
	LastIncludedIndex int32     `protobuf:"varint,3,opt,name=LastIncludedIndex,proto3" json:"LastIncludedIndex,omitempty"`
	LastIncludedTerm  int32     `protobuf:"varint,4,opt,name=LastIncludedTerm,proto3" json:"LastIncludedTerm,omitempty"`
	Data              [][]byte  `protobuf:"bytes,5,rep,name=Data,proto3" json:"Data,omitempty"`
	Base              *RaftBase `protobuf:"bytes,6,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*InstallSnapshotRequest) Descriptor deprecated

func (*InstallSnapshotRequest) Descriptor() ([]byte, []int)

Deprecated: Use InstallSnapshotRequest.ProtoReflect.Descriptor instead.

func (*InstallSnapshotRequest) GetBase

func (x *InstallSnapshotRequest) GetBase() *RaftBase

func (*InstallSnapshotRequest) GetData

func (x *InstallSnapshotRequest) GetData() [][]byte

func (*InstallSnapshotRequest) GetLastIncludedIndex

func (x *InstallSnapshotRequest) GetLastIncludedIndex() int32

func (*InstallSnapshotRequest) GetLastIncludedTerm

func (x *InstallSnapshotRequest) GetLastIncludedTerm() int32

func (*InstallSnapshotRequest) GetLeaderName

func (x *InstallSnapshotRequest) GetLeaderName() string

func (*InstallSnapshotRequest) GetTerm

func (x *InstallSnapshotRequest) GetTerm() int32

func (*InstallSnapshotRequest) ProtoMessage

func (*InstallSnapshotRequest) ProtoMessage()

func (*InstallSnapshotRequest) ProtoReflect

func (x *InstallSnapshotRequest) ProtoReflect() protoreflect.Message

func (*InstallSnapshotRequest) Reset

func (x *InstallSnapshotRequest) Reset()

func (*InstallSnapshotRequest) String

func (x *InstallSnapshotRequest) String() string

type LogEntry

type LogEntry struct {
	Term    int32  `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	Index   int32  `protobuf:"varint,2,opt,name=Index,proto3" json:"Index,omitempty"`
	Command []byte `protobuf:"bytes,3,opt,name=Command,proto3" json:"Command,omitempty"`
	// contains filtered or unexported fields
}

func (*LogEntry) Descriptor deprecated

func (*LogEntry) Descriptor() ([]byte, []int)

Deprecated: Use LogEntry.ProtoReflect.Descriptor instead.

func (*LogEntry) GetCommand

func (x *LogEntry) GetCommand() []byte

func (*LogEntry) GetIndex

func (x *LogEntry) GetIndex() int32

func (*LogEntry) GetTerm

func (x *LogEntry) GetTerm() int32

func (*LogEntry) ProtoMessage

func (*LogEntry) ProtoMessage()

func (*LogEntry) ProtoReflect

func (x *LogEntry) ProtoReflect() protoreflect.Message

func (*LogEntry) Reset

func (x *LogEntry) Reset()

func (*LogEntry) String

func (x *LogEntry) String() string

type Option

type Option func(*Options)

func WithAddress

func WithAddress(address string) Option

func WithApplyInterval

func WithApplyInterval(t time.Duration) Option

func WithCodecType

func WithCodecType(t string) Option

func WithElectionTimeout

func WithElectionTimeout(t time.Duration) Option

func WithHeartBeatTimeout

func WithHeartBeatTimeout(t time.Duration) Option

func WithMaxlogEntryLength

func WithMaxlogEntryLength(length int) Option

func WithNativeName

func WithNativeName(name string) Option

func WithRPCTimeout

func WithRPCTimeout(t time.Duration) Option

func WithRaftClient

func WithRaftClient(name, addr string) Option

func WithRaftPeers

func WithRaftPeers(m map[string]string) Option

func WithSnapshotPath

func WithSnapshotPath(path string) Option

type Options

type Options struct {
	NativeName        string
	Address           string        `yaml:"address"`
	CodecType         string        `yaml:"codec_type"`
	ApplyMsgLength    int           `yaml:"apply_msg_length"`
	SnapshotPath      string        `yaml:"snapshot_path"`
	MaxLogEntryLength int           `yaml:"max_log_entry_length"`
	ElectionTimeout   time.Duration `yaml:"election_timeout"`
	HeartBeatTimeout  time.Duration `yaml:"heartbeat_timeout"`
	ApplyInterval     time.Duration `yaml:"apply_interval"`
	RPCTimeout        time.Duration `yaml:"rpc_timeout"`
	RaftPeers         map[string]string
	InfoCh            chan *RaftInfo
}

func DefaultOptions

func DefaultOptions() *Options

type Persister

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

func MakePersister

func MakePersister(name, path string) *Persister

func (*Persister) RaftStateSize

func (ps *Persister) RaftStateSize() int

func (*Persister) ReadRaftState

func (ps *Persister) ReadRaftState() []byte

func (*Persister) ReadSnapshot

func (ps *Persister) ReadSnapshot() [][]byte

func (*Persister) ReadSnapshotByLastLength

func (ps *Persister) ReadSnapshotByLastLength(lastLength int32) [][]byte

func (*Persister) SaveRaftState

func (ps *Persister) SaveRaftState(state []byte)

func (*Persister) SaveSnapshot

func (ps *Persister) SaveSnapshot(snapshot [][]byte)

func (*Persister) SaveStateAndSnapshot

func (ps *Persister) SaveStateAndSnapshot(state []byte, snapshot [][]byte)

func (*Persister) SnapshotSize

func (ps *Persister) SnapshotSize() int

type Raft

type Raft struct {
	UnimplementedRaftServer
	// contains filtered or unexported fields
}

func NewRaft

func NewRaft(persister *Persister, applyCh chan ApplyMsg, opts *Options) *Raft

func (*Raft) AppendEntries

func (rf *Raft) AppendEntries(ctx context.Context, req *AppendEntriesRequest) (reply *AppendEntriesReply, err error)

func (*Raft) ApplyChan

func (rf *Raft) ApplyChan() <-chan ApplyMsg

func (*Raft) Info

func (rf *Raft) Info() *RaftInfo

func (*Raft) InstallSnapshot

func (rf *Raft) InstallSnapshot(ctx context.Context, req *InstallSnapshotRequest) (reply *InstallSnapshotReply, err error)

func (*Raft) Kill

func (rf *Raft) Kill()

func (*Raft) Put

func (rf *Raft) Put(command []byte) (int32, int32, bool)

func (*Raft) ReadSnapshotToLogEntryByLastLength

func (rf *Raft) ReadSnapshotToLogEntryByLastLength(lastLength int32) []*LogEntry

func (*Raft) Start

func (rf *Raft) Start(peers map[string]RaftClient)

func (*Raft) Vote

func (rf *Raft) Vote(ctx context.Context, req *VoteRequest) (reply *VoteReply, err error)

type RaftBase

type RaftBase struct {
	To   string `protobuf:"bytes,1,opt,name=To,proto3" json:"To,omitempty"`
	From string `protobuf:"bytes,2,opt,name=From,proto3" json:"From,omitempty"`
	// contains filtered or unexported fields
}

func (*RaftBase) Descriptor deprecated

func (*RaftBase) Descriptor() ([]byte, []int)

Deprecated: Use RaftBase.ProtoReflect.Descriptor instead.

func (*RaftBase) GetFrom

func (x *RaftBase) GetFrom() string

func (*RaftBase) GetTo

func (x *RaftBase) GetTo() string

func (*RaftBase) ProtoMessage

func (*RaftBase) ProtoMessage()

func (*RaftBase) ProtoReflect

func (x *RaftBase) ProtoReflect() protoreflect.Message

func (*RaftBase) Reset

func (x *RaftBase) Reset()

func (*RaftBase) String

func (x *RaftBase) String() string

type RaftClient

type RaftClient interface {
	InstallSnapshot(ctx context.Context, in *InstallSnapshotRequest, opts ...grpc.CallOption) (*InstallSnapshotReply, error)
	AppendEntries(ctx context.Context, in *AppendEntriesRequest, opts ...grpc.CallOption) (*AppendEntriesReply, error)
	Vote(ctx context.Context, in *VoteRequest, opts ...grpc.CallOption) (*VoteReply, error)
}

RaftClient is the client API for Raft service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewRaftClient

func NewRaftClient(cc grpc.ClientConnInterface) RaftClient

type RaftInfo

type RaftInfo struct {
	Name   string
	Leader string
	Role   Role
	Term   int32
}

type RaftServer

RaftServer is the server API for Raft service.

type RaftState

type RaftState struct {
	Term              int32
	VoteFor           string
	CommitIndex       int32
	LastSnapshotIndex int32
	LastSnapshotTerm  int32
	LogEntries        []*LogEntry
}

type Role

type Role string
const (
	Follower  Role = "Follwer"
	Candidate Role = "Candidate"
	Leader    Role = "Leader"
)

type Server

type Server interface {
	Put([]byte) bool
	CommitChannel() <-chan []byte
	Run()
	Kill()
}

func New

func New(o *Options) Server

type UnimplementedRaftServer

type UnimplementedRaftServer struct {
}

UnimplementedRaftServer can be embedded to have forward compatible implementations.

func (*UnimplementedRaftServer) AppendEntries

func (*UnimplementedRaftServer) InstallSnapshot

func (*UnimplementedRaftServer) Vote

type VoteReply

type VoteReply struct {
	Term        int32     `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	VoteGranted bool      `protobuf:"varint,2,opt,name=VoteGranted,proto3" json:"VoteGranted,omitempty"`
	Base        *RaftBase `protobuf:"bytes,3,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*VoteReply) Descriptor deprecated

func (*VoteReply) Descriptor() ([]byte, []int)

Deprecated: Use VoteReply.ProtoReflect.Descriptor instead.

func (*VoteReply) GetBase

func (x *VoteReply) GetBase() *RaftBase

func (*VoteReply) GetTerm

func (x *VoteReply) GetTerm() int32

func (*VoteReply) GetVoteGranted

func (x *VoteReply) GetVoteGranted() bool

func (*VoteReply) ProtoMessage

func (*VoteReply) ProtoMessage()

func (*VoteReply) ProtoReflect

func (x *VoteReply) ProtoReflect() protoreflect.Message

func (*VoteReply) Reset

func (x *VoteReply) Reset()

func (*VoteReply) String

func (x *VoteReply) String() string

type VoteRequest

type VoteRequest struct {
	Term          int32     `protobuf:"varint,1,opt,name=Term,proto3" json:"Term,omitempty"`
	CandidateName string    `protobuf:"bytes,2,opt,name=CandidateName,proto3" json:"CandidateName,omitempty"`
	LastLogIndex  int32     `protobuf:"varint,3,opt,name=LastLogIndex,proto3" json:"LastLogIndex,omitempty"`
	LastLogTerm   int32     `protobuf:"varint,4,opt,name=LastLogTerm,proto3" json:"LastLogTerm,omitempty"`
	Base          *RaftBase `protobuf:"bytes,5,opt,name=Base,proto3" json:"Base,omitempty"`
	// contains filtered or unexported fields
}

func (*VoteRequest) Descriptor deprecated

func (*VoteRequest) Descriptor() ([]byte, []int)

Deprecated: Use VoteRequest.ProtoReflect.Descriptor instead.

func (*VoteRequest) GetBase

func (x *VoteRequest) GetBase() *RaftBase

func (*VoteRequest) GetCandidateName

func (x *VoteRequest) GetCandidateName() string

func (*VoteRequest) GetLastLogIndex

func (x *VoteRequest) GetLastLogIndex() int32

func (*VoteRequest) GetLastLogTerm

func (x *VoteRequest) GetLastLogTerm() int32

func (*VoteRequest) GetTerm

func (x *VoteRequest) GetTerm() int32

func (*VoteRequest) ProtoMessage

func (*VoteRequest) ProtoMessage()

func (*VoteRequest) ProtoReflect

func (x *VoteRequest) ProtoReflect() protoreflect.Message

func (*VoteRequest) Reset

func (x *VoteRequest) Reset()

func (*VoteRequest) String

func (x *VoteRequest) String() string

Jump to

Keyboard shortcuts

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