Support failover in cluster (experimental)

This commit is contained in:
finley
2025-04-19 22:11:58 +08:00
parent 14ec8277ca
commit f4a2c92fc1
20 changed files with 739 additions and 136 deletions

42
cluster/raft/utils.go Normal file
View File

@@ -0,0 +1,42 @@
package raft
import (
"fmt"
"strconv"
"github.com/hashicorp/raft"
)
func (node *Node) Self() string {
return node.Cfg.ID()
}
func (node *Node) State() raft.RaftState {
return node.inner.State()
}
func (node *Node) CommittedIndex() (uint64, error) {
stats := node.inner.Stats()
committedIndex0 := stats["commit_index"]
return strconv.ParseUint(committedIndex0, 10, 64)
}
func (node *Node) GetLeaderRedisAddress() string {
// redis advertise address used as leader id
_, id := node.inner.LeaderWithID()
return string(id)
}
func (node *Node) GetNodes() ([]raft.Server, error) {
configFuture := node.inner.GetConfiguration()
if err := configFuture.Error(); err != nil {
return nil, fmt.Errorf("failed to get raft configuration: %v", err)
}
return configFuture.Configuration().Servers, nil
}
func (node *Node) GetSlaves(id string) *MasterSlave {
node.FSM.mu.RLock()
defer node.FSM.mu.RUnlock()
return node.FSM.MasterSlaves[id]
}