Categorygithub.com/invxp/raft
modulepackage
0.0.5
Repository: https://github.com/invxp/raft.git
Documentation: pkg.go.dev

# README

Raft

Raft 共识算法基于Go实现, 能力列表:

  1. Leader Election √
  2. Log Replication √
  3. Log Recover √
  4. Log Compaction ❌
  5. Membership Management √

Raft介绍:

实现参考:

如何使用

可以直接阅读源码来学习具体的实现, 如果实在懒得看, 可以按照下面做:

package main

func TestRaft(t *testing.T) {
    //具体可参考TestMainFunctions
    //创建日志回调记录
    commit := make(chan raft.CommitEntry)
    
    //开始监听所有服务
    server := raft.NewServer(":0", commit, nil)
    server.Server()
    
    exit := make(chan interface{})
    go func() {
        for {
            select {
            case c := <-commit:
                //如果有数据提交会通知
                log.Println(c)
            case <-exit:
                return
            }
        }
    }()
    
    //提交一条日志(如果只有一个节点会提交失败)
    fmt.Println(server.Commit("TEST-LOG"))
    
    time.Sleep(time.Second)
    
    //获取所有日志
    fmt.Println(server.Logs(0))
    
    //获取所有节点IP
    fmt.Println(server.Nodes())
    
    //获取节点当前状态
    fmt.Println(server.Status())
    
    //退出
    server.Shutdown()
    close(exit)
}

测试用例可以这样做:

$ go test -v -race -run @XXXXX(具体方法名)
PASS / FAILED

或测试全部用例:

$ go test -v -race

TODO

  1. Log Compaction(日志快照与压缩)
  2. 缩容场景处理

# Packages

No description provided by the author

# Functions

NewBench 新建测试用例(初始化).
No description provided by the author
NewServer 新建一个raft服务 address为TCP监听的地址,一般给个端口就可以如:8888 commitChan如果有数据新增,则会通知到这里,业务可以用于监听 config配置信息,需要确保electionMin比Max要小,并且heartbeat也要比electionMin小这三个参数才可生效 可根据实际情况调整时间,默认min(150ms), max(300ms), heartbeat(20ms) nodeIDs除自己以外的节点列表(新的节点把老的节点的地址都填上即可相互连接).

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

BenchTest 测试用例.
CommitEntry 数据提交的通道,每次数据有变动时CommitChannel会触发结果.
Config 配置.
LogEntry 日志记录.
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

FSM 状态机.