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

36
cluster/core/cron.go Normal file
View File

@@ -0,0 +1,36 @@
package core
import (
"sync/atomic"
"time"
"github.com/hdt3213/godis/cluster/raft"
)
func (cluster *Cluster) clusterCron() {
if cluster.config.noCron {
return
}
ticker := time.NewTicker(time.Second)
var running int32
for {
select {
case <-ticker.C:
if cluster.raftNode.State() == raft.Leader {
if atomic.CompareAndSwapInt32(&running, 0, 1) {
// Disable parallelism
go func() {
cluster.doFailoverCheck()
cluster.doRebalance()
atomic.StoreInt32(&running, 0)
}()
}
} else {
cluster.sendHearbeat()
}
case <-cluster.closeChan:
ticker.Stop()
return
}
}
}