raft

package
v0.0.0-...-3aaa451 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MSGID_RaftVote           = 3001 //投票同步
	MSGID_RaftVote_recv      = 3002 //投票同步 返回
	MSGID_RaftVoteHeart      = 3003 //心跳同步
	MSGID_RaftVoteHeart_recv = 3004 //心跳同步 返回
)
View Source
const (
	Leader    = "leader"    //管理者
	Follower  = "follower"  //追随者
	Candidate = "candidate" //候选者
)
View Source
const (
	Task_class_raft_heart = "Task_class_raft_heart" //定时发送心跳信息
)

Variables

View Source
var (
	HeartInterval = 15                         //心跳间隔,超过时间没收到,则视为断开
	HeartTime     = make(map[string]time.Time) //心跳时间,key为team leader
	HeartMin      = 1500                       //心跳检查间隔最小值 ms
	HeartMax      = 3000                       //心跳检查最大值 ms
)
View Source
var (
	TimeInterval = 5 //心跳间隔,单位秒
)
View Source
var (
	Version = uint64(0x00)
)

Functions

func Add

func Add(key, value []byte)

增加数据

func AddHeartTask

func AddHeartTask(params string)

加入定时器

func BuildHash

func BuildHash(key []byte) *utils.Multihash

生成multihash

func Del

func Del(key []byte)

删除数据

func DelTeam

func DelTeam(teamid *utils.Multihash) error

func DoVote

func DoVote(teamid, nodeid *utils.Multihash)

处理投票

func GetRole

func GetRole(teamid, nodeid *utils.Multihash) string

获取当前角色

func Init

func Init()

func MulitiHeart

func MulitiHeart(team *RaftTeam)

发送心跳

func MulitiVote

func MulitiVote(rt *RaftTeam)

广播投票消息

func RaftVote

func RaftVote(c engine.Controller, msg engine.Packet)

func RaftVoteHeart

func RaftVoteHeart(c engine.Controller, msg engine.Packet)

func RaftVoteHeart_recv

func RaftVoteHeart_recv(c engine.Controller, msg engine.Packet)

心跳返回

func RaftVote_recv

func RaftVote_recv(c engine.Controller, msg engine.Packet)

func RandInt64

func RandInt64(min, max int) string

随机数

func Register

func Register()

func RegisterRaft

func RegisterRaft()

func SaveTeam

func SaveTeam(team *RaftTeam) error

保存team

func Update

func Update(key, value []byte)

修改数据

func UpdateHeartTime

func UpdateHeartTime(teamid *utils.Multihash) error

更新心跳时间

Types

type HeartData

type HeartData struct {
	Teamid *utils.Multihash
	Index  uint64 //当前team 投票计数
}

心跳数据

func ParseHeartData

func ParseHeartData(data []byte) (*HeartData, error)

解析字节为heartdata

func (*HeartData) Json

func (hd *HeartData) Json() []byte

type RaftData

type RaftData struct {
	Team sync.Map //根据KEY的HASH值生成TEAM TeamId:RaftTeam
}
var (
	RD *RaftData
)

func NewRaftData

func NewRaftData() *RaftData

func (*RaftData) CreateTeam

func (rd *RaftData) CreateTeam(teamid *utils.Multihash) *RaftTeam

创建team

func (*RaftData) DelTeam

func (rd *RaftData) DelTeam(teamid *utils.Multihash) error

获删除team

func (*RaftData) GetTeam

func (rd *RaftData) GetTeam(teamid *utils.Multihash) *RaftTeam

获取team

func (*RaftData) SaveTeam

func (rd *RaftData) SaveTeam(team *RaftTeam) error

保存team

type RaftRole

type RaftRole struct {
	Nodeid *utils.Multihash //节点id
	Role   string
}

角色

func (*RaftRole) SetRole

func (rr *RaftRole) SetRole(nodeid *utils.Multihash, role string)

设置角色

type RaftTeam

type RaftTeam struct {
	TeamId  *utils.Multihash   //key HASH
	Nodeids []*utils.Multihash //成员节点
	Role    *RaftRole          //角色 NodeId:RaftRole
	Vote    map[string]int     //投票结果
	Index   uint64             //投票计数,发起新一轮投票,则计算+1
}

team 每个team有唯一的leader

func CreateTeam

func CreateTeam(teamid *utils.Multihash) *RaftTeam

创建一个team 一个key对应一个team

func FirstTeam

func FirstTeam(teamid *utils.Multihash) *RaftTeam

首次加入网络

func GetTeam

func GetTeam(teamid *utils.Multihash) *RaftTeam

获取TEAM

func Parse

func Parse(data []byte) (*RaftTeam, error)

解析字节为raftdata

func (*RaftTeam) CreateVote

func (rt *RaftTeam) CreateVote(nodeid *utils.Multihash) error

发起投票

func (*RaftTeam) DoVote

func (rt *RaftTeam) DoVote(nodeid *utils.Multihash) *RaftTeam

投票

func (*RaftTeam) Json

func (rt *RaftTeam) Json() []byte

Jump to

Keyboard shortcuts

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