raft4go

package module
v0.0.0-...-80829de Latest Latest
Warning

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

Go to latest
Published: May 6, 2020 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) ([]byte, error)

Get 从集群获取数据

func Put

func Put(key string, value []byte) error

Put 集群新增数据

func RaftStart

func RaftStart(params *Params)

RaftStart 启动且只能启动一次Raft服务

node 自身节点信息

nodes 集群节点信息

timeCheck raft心跳定时检查超时时间

timeout raft心跳定时/超时ms

func RegisterRaftServer

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

Types

type Data

type Data struct {
	// 数据key
	Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	// 数据hash
	Hash string `protobuf:"bytes,2,opt,name=hash,proto3" json:"hash,omitempty"`
	// 当前配置版本 index 递增
	Version              int32    `protobuf:"varint,3,opt,name=version,proto3" json:"version,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

data 匹配数据对象

func (*Data) Descriptor

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

func (*Data) GetHash

func (m *Data) GetHash() string

func (*Data) GetKey

func (m *Data) GetKey() string

func (*Data) GetVersion

func (m *Data) GetVersion() int32

func (*Data) ProtoMessage

func (*Data) ProtoMessage()

func (*Data) Reset

func (m *Data) Reset()

func (*Data) String

func (m *Data) String() string

func (*Data) XXX_DiscardUnknown

func (m *Data) XXX_DiscardUnknown()

func (*Data) XXX_Marshal

func (m *Data) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Data) XXX_Merge

func (m *Data) XXX_Merge(src proto.Message)

func (*Data) XXX_Size

func (m *Data) XXX_Size() int

func (*Data) XXX_Unmarshal

func (m *Data) XXX_Unmarshal(b []byte) error

type Log

type Log struct {
	Dir         string // 日志文件目录
	FileMaxSize int    // 每个日志文件保存的最大尺寸 单位:M
	FileMaxAge  int    // 文件最多保存多少天
	Utc         bool   // CST & UTC 时间
	Level       string // 日志级别(debug/info/warn/error/panic/fatal)
	Production  bool   // 是否生产环境,在生产环境下控制台不会输出任何日志
}

Log 日志属性

type Node

type Node struct {
	// 节点ID
	Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	// 节点地址
	Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"`
	// 节点异常次数,异常次数到达或超过3次,则表示该节点异常
	UnusualTimes         int32    `protobuf:"varint,3,opt,name=unusualTimes,proto3" json:"unusualTimes,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

node 节点信息

func (*Node) Descriptor

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

func (*Node) GetId

func (m *Node) GetId() string

func (*Node) GetUnusualTimes

func (m *Node) GetUnusualTimes() int32

func (*Node) GetUrl

func (m *Node) GetUrl() string

func (*Node) ProtoMessage

func (*Node) ProtoMessage()

func (*Node) Reset

func (m *Node) Reset()

func (*Node) String

func (m *Node) String() string

func (*Node) XXX_DiscardUnknown

func (m *Node) XXX_DiscardUnknown()

func (*Node) XXX_Marshal

func (m *Node) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Node) XXX_Merge

func (m *Node) XXX_Merge(src proto.Message)

func (*Node) XXX_Size

func (m *Node) XXX_Size() int

func (*Node) XXX_Unmarshal

func (m *Node) XXX_Unmarshal(b []byte) error

type Nodes

type Nodes map[string]*Node

Nodes 当前Raft可见节点集合

func NodeList

func NodeList() Nodes

NodeList 查看当前集群中节点集合,包括自身

type Params

type Params struct {
	Node          *Node   // 自身节点信息
	Nodes         []*Node // 集群节点信息
	TimeHeartbeat int64   // raft心跳定时ms
	TimeCheckReq  int64   //  raft心跳定时检查超时时间ms
	TimeoutReq    int64   // raft心跳定时ms
	PortReq       string  // raft服务开放端口号,默认19877
	Log           *Log    // 日志
}

Params 启动参数

type Raft

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

Raft 接收客户端提交的同步内容,被封装在自定义的方法中

也返回客户端期望的同步结果及从其他节点同步过来的信息

type RaftClient

type RaftClient interface {
	// Heartbeat 发送心跳
	Heartbeat(ctx context.Context, in *ReqHeartBeat, opts ...grpc.CallOption) (*RespHeartBeat, error)
	// NodeList 请求集群节点集合
	NodeList(ctx context.Context, in *ReqNodeList, opts ...grpc.CallOption) (*RespNodeList, error)
	// Data 请求当前集群指定key数据
	Data(ctx context.Context, in *ReqData, opts ...grpc.CallOption) (*RespData, error)
	// DataList 请求集群数据集合
	DataList(ctx context.Context, in *ReqDataList, opts ...grpc.CallOption) (*RespDataList, error)
	// SyncData 同步数据
	SyncData(ctx context.Context, in *ReqSyncData, opts ...grpc.CallOption) (*RespSyncData, error)
	// Vote 发起选举,索要选票
	Vote(ctx context.Context, in *ReqVote, opts ...grpc.CallOption) (*RespVote, 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 RaftServer

type RaftServer interface {
	// Heartbeat 发送心跳
	Heartbeat(context.Context, *ReqHeartBeat) (*RespHeartBeat, error)
	// NodeList 请求集群节点集合
	NodeList(context.Context, *ReqNodeList) (*RespNodeList, error)
	// Data 请求当前集群指定key数据
	Data(context.Context, *ReqData) (*RespData, error)
	// DataList 请求集群数据集合
	DataList(context.Context, *ReqDataList) (*RespDataList, error)
	// SyncData 同步数据
	SyncData(context.Context, *ReqSyncData) (*RespSyncData, error)
	// Vote 发起选举,索要选票
	Vote(context.Context, *ReqVote) (*RespVote, error)
}

RaftServer is the server API for Raft service.

type ReqData

type ReqData struct {
	// 数据key
	Key                  string   `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ReqData 请求当前集群指定key数据

func (*ReqData) Descriptor

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

func (*ReqData) GetKey

func (m *ReqData) GetKey() string

func (*ReqData) ProtoMessage

func (*ReqData) ProtoMessage()

func (*ReqData) Reset

func (m *ReqData) Reset()

func (*ReqData) String

func (m *ReqData) String() string

func (*ReqData) XXX_DiscardUnknown

func (m *ReqData) XXX_DiscardUnknown()

func (*ReqData) XXX_Marshal

func (m *ReqData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqData) XXX_Merge

func (m *ReqData) XXX_Merge(src proto.Message)

func (*ReqData) XXX_Size

func (m *ReqData) XXX_Size() int

func (*ReqData) XXX_Unmarshal

func (m *ReqData) XXX_Unmarshal(b []byte) error

type ReqDataList

type ReqDataList struct {
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ReqDataList 请求当前集群数据集合

func (*ReqDataList) Descriptor

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

func (*ReqDataList) ProtoMessage

func (*ReqDataList) ProtoMessage()

func (*ReqDataList) Reset

func (m *ReqDataList) Reset()

func (*ReqDataList) String

func (m *ReqDataList) String() string

func (*ReqDataList) XXX_DiscardUnknown

func (m *ReqDataList) XXX_DiscardUnknown()

func (*ReqDataList) XXX_Marshal

func (m *ReqDataList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqDataList) XXX_Merge

func (m *ReqDataList) XXX_Merge(src proto.Message)

func (*ReqDataList) XXX_Size

func (m *ReqDataList) XXX_Size() int

func (*ReqDataList) XXX_Unmarshal

func (m *ReqDataList) XXX_Unmarshal(b []byte) error

type ReqHeartBeat

type ReqHeartBeat struct {
	// 自身作为Leader节点的任期
	Term int32 `protobuf:"varint,1,opt,name=term,proto3" json:"term,omitempty"`
	// 自身作为Leader节点的ID
	Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
	// 自身作为Leader节点的地址
	Url string `protobuf:"bytes,3,opt,name=url,proto3" json:"url,omitempty"`
	// 自身作为Leader节点所有数据集hash,用于比较彼此数据
	Hash                 string   `protobuf:"bytes,4,opt,name=hash,proto3" json:"hash,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ReqHeartBeat 用于Leader节点复制日志给其他节点,也作为心跳

prevLogIndex和prevLogTerm表示上一次发送的日志的索引和任期,用于保证收到的日志是连续的

func (*ReqHeartBeat) Descriptor

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

func (*ReqHeartBeat) GetHash

func (m *ReqHeartBeat) GetHash() string

func (*ReqHeartBeat) GetId

func (m *ReqHeartBeat) GetId() string

func (*ReqHeartBeat) GetTerm

func (m *ReqHeartBeat) GetTerm() int32

func (*ReqHeartBeat) GetUrl

func (m *ReqHeartBeat) GetUrl() string

func (*ReqHeartBeat) ProtoMessage

func (*ReqHeartBeat) ProtoMessage()

func (*ReqHeartBeat) Reset

func (m *ReqHeartBeat) Reset()

func (*ReqHeartBeat) String

func (m *ReqHeartBeat) String() string

func (*ReqHeartBeat) XXX_DiscardUnknown

func (m *ReqHeartBeat) XXX_DiscardUnknown()

func (*ReqHeartBeat) XXX_Marshal

func (m *ReqHeartBeat) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqHeartBeat) XXX_Merge

func (m *ReqHeartBeat) XXX_Merge(src proto.Message)

func (*ReqHeartBeat) XXX_Size

func (m *ReqHeartBeat) XXX_Size() int

func (*ReqHeartBeat) XXX_Unmarshal

func (m *ReqHeartBeat) XXX_Unmarshal(b []byte) error

type ReqNodeList

type ReqNodeList struct {
	Nodes                []*Node  `protobuf:"bytes,1,rep,name=nodes,proto3" json:"nodes,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ReqNodeList 请求当前集群节点集合

func (*ReqNodeList) Descriptor

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

func (*ReqNodeList) GetNodes

func (m *ReqNodeList) GetNodes() []*Node

func (*ReqNodeList) ProtoMessage

func (*ReqNodeList) ProtoMessage()

func (*ReqNodeList) Reset

func (m *ReqNodeList) Reset()

func (*ReqNodeList) String

func (m *ReqNodeList) String() string

func (*ReqNodeList) XXX_DiscardUnknown

func (m *ReqNodeList) XXX_DiscardUnknown()

func (*ReqNodeList) XXX_Marshal

func (m *ReqNodeList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqNodeList) XXX_Merge

func (m *ReqNodeList) XXX_Merge(src proto.Message)

func (*ReqNodeList) XXX_Size

func (m *ReqNodeList) XXX_Size() int

func (*ReqNodeList) XXX_Unmarshal

func (m *ReqNodeList) XXX_Unmarshal(b []byte) error

type ReqSyncData

type ReqSyncData struct {
	// Leader节点的任期
	Term int32 `protobuf:"varint,1,opt,name=term,proto3" json:"term,omitempty"`
	// Leader节点的ID
	LeaderId string `protobuf:"bytes,2,opt,name=leaderId,proto3" json:"leaderId,omitempty"`
	// Leader节点的地址
	LeaderUrl string `protobuf:"bytes,3,opt,name=leaderUrl,proto3" json:"leaderUrl,omitempty"`
	// 当前配置版本 index 递增
	Version int32 `protobuf:"varint,4,opt,name=version,proto3" json:"version,omitempty"`
	// 当前term同步配置信息的key
	Key string `protobuf:"bytes,5,opt,name=key,proto3" json:"key,omitempty"`
	// 当前term同步配置信息的值
	Value                []byte   `protobuf:"bytes,6,opt,name=value,proto3" json:"value,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

ReqSyncData 用于Leader节点同步数据给其他节点

func (*ReqSyncData) Descriptor

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

func (*ReqSyncData) GetKey

func (m *ReqSyncData) GetKey() string

func (*ReqSyncData) GetLeaderId

func (m *ReqSyncData) GetLeaderId() string

func (*ReqSyncData) GetLeaderUrl

func (m *ReqSyncData) GetLeaderUrl() string

func (*ReqSyncData) GetTerm

func (m *ReqSyncData) GetTerm() int32

func (*ReqSyncData) GetValue

func (m *ReqSyncData) GetValue() []byte

func (*ReqSyncData) GetVersion

func (m *ReqSyncData) GetVersion() int32

func (*ReqSyncData) ProtoMessage

func (*ReqSyncData) ProtoMessage()

func (*ReqSyncData) Reset

func (m *ReqSyncData) Reset()

func (*ReqSyncData) String

func (m *ReqSyncData) String() string

func (*ReqSyncData) XXX_DiscardUnknown

func (m *ReqSyncData) XXX_DiscardUnknown()

func (*ReqSyncData) XXX_Marshal

func (m *ReqSyncData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqSyncData) XXX_Merge

func (m *ReqSyncData) XXX_Merge(src proto.Message)

func (*ReqSyncData) XXX_Size

func (m *ReqSyncData) XXX_Size() int

func (*ReqSyncData) XXX_Unmarshal

func (m *ReqSyncData) XXX_Unmarshal(b []byte) error

type ReqVote

type ReqVote struct {
	// Candidate的ID
	Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
	// Candidate的URL
	Url string `protobuf:"bytes,3,opt,name=url,proto3" json:"url,omitempty"`
	// Candidate的任期
	Term int32 `protobuf:"varint,1,opt,name=term,proto3" json:"term,omitempty"`
	// 时间戳ns
	Timestamp            int64    `protobuf:"varint,7,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

reqVote 用于Candidate获取选票

func (*ReqVote) Descriptor

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

func (*ReqVote) GetId

func (m *ReqVote) GetId() string

func (*ReqVote) GetTerm

func (m *ReqVote) GetTerm() int32

func (*ReqVote) GetTimestamp

func (m *ReqVote) GetTimestamp() int64

func (*ReqVote) GetUrl

func (m *ReqVote) GetUrl() string

func (*ReqVote) ProtoMessage

func (*ReqVote) ProtoMessage()

func (*ReqVote) Reset

func (m *ReqVote) Reset()

func (*ReqVote) String

func (m *ReqVote) String() string

func (*ReqVote) XXX_DiscardUnknown

func (m *ReqVote) XXX_DiscardUnknown()

func (*ReqVote) XXX_Marshal

func (m *ReqVote) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ReqVote) XXX_Merge

func (m *ReqVote) XXX_Merge(src proto.Message)

func (*ReqVote) XXX_Size

func (m *ReqVote) XXX_Size() int

func (*ReqVote) XXX_Unmarshal

func (m *ReqVote) XXX_Unmarshal(b []byte) error

type RespData

type RespData struct {
	// 当前集群指定key数据信息的值
	Value                []byte   `protobuf:"bytes,6,opt,name=value,proto3" json:"value,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

RespData 应答当前集群指定key数据

func (*RespData) Descriptor

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

func (*RespData) GetValue

func (m *RespData) GetValue() []byte

func (*RespData) ProtoMessage

func (*RespData) ProtoMessage()

func (*RespData) Reset

func (m *RespData) Reset()

func (*RespData) String

func (m *RespData) String() string

func (*RespData) XXX_DiscardUnknown

func (m *RespData) XXX_DiscardUnknown()

func (*RespData) XXX_Marshal

func (m *RespData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespData) XXX_Merge

func (m *RespData) XXX_Merge(src proto.Message)

func (*RespData) XXX_Size

func (m *RespData) XXX_Size() int

func (*RespData) XXX_Unmarshal

func (m *RespData) XXX_Unmarshal(b []byte) error

type RespDataList

type RespDataList struct {
	DataArr              []*Data  `protobuf:"bytes,1,rep,name=dataArr,proto3" json:"dataArr,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

RespDataList 应答当前集群数据集合

func (*RespDataList) Descriptor

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

func (*RespDataList) GetDataArr

func (m *RespDataList) GetDataArr() []*Data

func (*RespDataList) ProtoMessage

func (*RespDataList) ProtoMessage()

func (*RespDataList) Reset

func (m *RespDataList) Reset()

func (*RespDataList) String

func (m *RespDataList) String() string

func (*RespDataList) XXX_DiscardUnknown

func (m *RespDataList) XXX_DiscardUnknown()

func (*RespDataList) XXX_Marshal

func (m *RespDataList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespDataList) XXX_Merge

func (m *RespDataList) XXX_Merge(src proto.Message)

func (*RespDataList) XXX_Size

func (m *RespDataList) XXX_Size() int

func (*RespDataList) XXX_Unmarshal

func (m *RespDataList) XXX_Unmarshal(b []byte) error

type RespHeartBeat

type RespHeartBeat struct {
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

RespHeartBeat 接收者实现逻辑

返回false,如果收到的任期比当前任期小

返回false,如果不包含之前的日志条目(没有匹配prevLogIndex和prevLogTerm)

如果存在index相同但是term不相同的日志,删除从该位置开始所有的日志

追加所有不存在的日志

如果leaderCommit>commitIndex,将commitIndex设置为commitIndex = min(leaderCommit, index of last new entry)

func (*RespHeartBeat) Descriptor

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

func (*RespHeartBeat) ProtoMessage

func (*RespHeartBeat) ProtoMessage()

func (*RespHeartBeat) Reset

func (m *RespHeartBeat) Reset()

func (*RespHeartBeat) String

func (m *RespHeartBeat) String() string

func (*RespHeartBeat) XXX_DiscardUnknown

func (m *RespHeartBeat) XXX_DiscardUnknown()

func (*RespHeartBeat) XXX_Marshal

func (m *RespHeartBeat) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespHeartBeat) XXX_Merge

func (m *RespHeartBeat) XXX_Merge(src proto.Message)

func (*RespHeartBeat) XXX_Size

func (m *RespHeartBeat) XXX_Size() int

func (*RespHeartBeat) XXX_Unmarshal

func (m *RespHeartBeat) XXX_Unmarshal(b []byte) error

type RespNodeList

type RespNodeList struct {
	Nodes                []*Node  `protobuf:"bytes,1,rep,name=nodes,proto3" json:"nodes,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

RespNodeList 应答当前集群节点集合

func (*RespNodeList) Descriptor

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

func (*RespNodeList) GetNodes

func (m *RespNodeList) GetNodes() []*Node

func (*RespNodeList) ProtoMessage

func (*RespNodeList) ProtoMessage()

func (*RespNodeList) Reset

func (m *RespNodeList) Reset()

func (*RespNodeList) String

func (m *RespNodeList) String() string

func (*RespNodeList) XXX_DiscardUnknown

func (m *RespNodeList) XXX_DiscardUnknown()

func (*RespNodeList) XXX_Marshal

func (m *RespNodeList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespNodeList) XXX_Merge

func (m *RespNodeList) XXX_Merge(src proto.Message)

func (*RespNodeList) XXX_Size

func (m *RespNodeList) XXX_Size() int

func (*RespNodeList) XXX_Unmarshal

func (m *RespNodeList) XXX_Unmarshal(b []byte) error

type RespSyncData

type RespSyncData struct {
	// 当前任期号,用于Leader节点更新自己的任期(应该说是如果这个返回值比Leader自身的任期大,那么Leader需要更新自己的任期)
	Term int32 `protobuf:"varint,1,opt,name=term,proto3" json:"term,omitempty"`
	// 如果Follower节点匹配prevLogIndex和prevLogTerm,返回true
	Success              bool     `protobuf:"varint,2,opt,name=success,proto3" json:"success,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

RespSyncData 接收者实现逻辑

返回false,如果收到的任期比当前任期小

返回false,如果不包含之前的日志条目(没有匹配prevLogIndex和prevLogTerm)

如果存在index相同但是term不相同的日志,删除从该位置开始所有的日志

追加所有不存在的日志

如果leaderCommit>commitIndex,将commitIndex设置为commitIndex = min(leaderCommit, index of last new entry)

func (*RespSyncData) Descriptor

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

func (*RespSyncData) GetSuccess

func (m *RespSyncData) GetSuccess() bool

func (*RespSyncData) GetTerm

func (m *RespSyncData) GetTerm() int32

func (*RespSyncData) ProtoMessage

func (*RespSyncData) ProtoMessage()

func (*RespSyncData) Reset

func (m *RespSyncData) Reset()

func (*RespSyncData) String

func (m *RespSyncData) String() string

func (*RespSyncData) XXX_DiscardUnknown

func (m *RespSyncData) XXX_DiscardUnknown()

func (*RespSyncData) XXX_Marshal

func (m *RespSyncData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespSyncData) XXX_Merge

func (m *RespSyncData) XXX_Merge(src proto.Message)

func (*RespSyncData) XXX_Size

func (m *RespSyncData) XXX_Size() int

func (*RespSyncData) XXX_Unmarshal

func (m *RespSyncData) XXX_Unmarshal(b []byte) error

type RespVote

type RespVote struct {
	// true表示给Candidate投票
	VoteGranted          bool     `protobuf:"varint,2,opt,name=voteGranted,proto3" json:"voteGranted,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

接收者的实现逻辑

返回false,如果收到的任期比当前任期小

如果本地状态中votedFor为null或者candidateId,且candidate的日志等于或多余(按照index判断)接收者的日志,则接收者投票给candidate,即返回true

func (*RespVote) Descriptor

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

func (*RespVote) GetVoteGranted

func (m *RespVote) GetVoteGranted() bool

func (*RespVote) ProtoMessage

func (*RespVote) ProtoMessage()

func (*RespVote) Reset

func (m *RespVote) Reset()

func (*RespVote) String

func (m *RespVote) String() string

func (*RespVote) XXX_DiscardUnknown

func (m *RespVote) XXX_DiscardUnknown()

func (*RespVote) XXX_Marshal

func (m *RespVote) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*RespVote) XXX_Merge

func (m *RespVote) XXX_Merge(src proto.Message)

func (*RespVote) XXX_Size

func (m *RespVote) XXX_Size() int

func (*RespVote) XXX_Unmarshal

func (m *RespVote) XXX_Unmarshal(b []byte) error

type RoleStatus

type RoleStatus int

RoleStatus 角色定义

const (
	// RoleStatusLeader leader
	RoleStatusLeader RoleStatus = iota
	// RoleStatusCandidate candidate
	RoleStatusCandidate
	// RoleStatusFollower follower
	RoleStatusFollower
)

func Status

func Status() RoleStatus

Status 获取角色状态,0-leader、1-candidate、2-follower

RoleStatusLeader、RoleStatusCandidate、RoleStatusFollower

type Server

type Server struct{}

Server gRPC服务

func (*Server) Data

func (s *Server) Data(_ context.Context, req *ReqData) (resp *RespData, err error)

Data 接收请求当前集群指定key数据

func (*Server) DataList

func (s *Server) DataList(_ context.Context, _ *ReqDataList) (resp *RespDataList, err error)

DataList 接收请求集群数据集合

func (*Server) Heartbeat

func (s *Server) Heartbeat(ctx context.Context, req *ReqHeartBeat) (resp *RespHeartBeat, err error)

Heartbeat 接收发送心跳

func (*Server) NodeList

func (s *Server) NodeList(_ context.Context, req *ReqNodeList) (resp *RespNodeList, err error)

NodeList 接收请求集群节点集合

func (*Server) SyncData

func (s *Server) SyncData(_ context.Context, req *ReqSyncData) (resp *RespSyncData, err error)

SyncData 接收同步数据

func (*Server) Vote

func (s *Server) Vote(_ context.Context, req *ReqVote) (resp *RespVote, err error)

Vote 接收发起选举,索要选票

type UnimplementedRaftServer

type UnimplementedRaftServer struct {
}

UnimplementedRaftServer can be embedded to have forward compatible implementations.

func (*UnimplementedRaftServer) Data

func (*UnimplementedRaftServer) DataList

func (*UnimplementedRaftServer) Heartbeat

func (*UnimplementedRaftServer) NodeList

func (*UnimplementedRaftServer) SyncData

func (*UnimplementedRaftServer) Vote

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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