You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.5 KiB

  1. package weed_server
  2. // https://yusufs.medium.com/creating-distributed-kv-database-by-implementing-raft-consensus-using-golang-d0884eef2e28
  3. // https://github.com/Jille/raft-grpc-example/blob/cd5bcab0218f008e044fbeee4facdd01b06018ad/application.go#L18
  4. import (
  5. "fmt"
  6. transport "github.com/Jille/raft-grpc-transport"
  7. "github.com/hashicorp/raft"
  8. boltdb "github.com/hashicorp/raft-boltdb"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb"
  11. "google.golang.org/grpc"
  12. "math/rand"
  13. "os"
  14. "path"
  15. "path/filepath"
  16. "sort"
  17. "strings"
  18. "time"
  19. )
  20. const (
  21. ldbFile = "logs.dat"
  22. sdbFile = "stable.dat"
  23. updatePeersTimeout = 15 * time.Minute
  24. )
  25. func getPeerIdx(self pb.ServerAddress, mapPeers map[string]pb.ServerAddress) int {
  26. peers := make([]pb.ServerAddress, 0, len(mapPeers))
  27. for _, peer := range mapPeers {
  28. peers = append(peers, peer)
  29. }
  30. sort.Slice(peers, func(i, j int) bool {
  31. return strings.Compare(string(peers[i]), string(peers[j])) < 0
  32. })
  33. for i, peer := range peers {
  34. if string(peer) == string(self) {
  35. return i
  36. }
  37. }
  38. return -1
  39. }
  40. func (s *RaftServer) AddPeersConfiguration() (cfg raft.Configuration) {
  41. for _, peer := range s.peers {
  42. cfg.Servers = append(cfg.Servers, raft.Server{
  43. Suffrage: raft.Voter,
  44. ID: raft.ServerID(peer),
  45. Address: raft.ServerAddress(peer.ToGrpcAddress()),
  46. })
  47. }
  48. return cfg
  49. }
  50. func (s *RaftServer) UpdatePeers() {
  51. for {
  52. select {
  53. case isLeader := <-s.RaftHashicorp.LeaderCh():
  54. if isLeader {
  55. peerLeader := string(s.serverAddr)
  56. existsPeerName := make(map[string]bool)
  57. for _, server := range s.RaftHashicorp.GetConfiguration().Configuration().Servers {
  58. if string(server.ID) == peerLeader {
  59. continue
  60. }
  61. existsPeerName[string(server.ID)] = true
  62. }
  63. for _, peer := range s.peers {
  64. peerName := string(peer)
  65. if peerName == peerLeader || existsPeerName[peerName] {
  66. continue
  67. }
  68. glog.V(0).Infof("adding new peer: %s", peerName)
  69. s.RaftHashicorp.AddVoter(
  70. raft.ServerID(peerName), raft.ServerAddress(peer.ToGrpcAddress()), 0, 0)
  71. }
  72. for peer, _ := range existsPeerName {
  73. if _, found := s.peers[peer]; !found {
  74. glog.V(0).Infof("removing old peer: %s", peer)
  75. s.RaftHashicorp.RemoveServer(raft.ServerID(peer), 0, 0)
  76. }
  77. }
  78. if _, found := s.peers[peerLeader]; !found {
  79. glog.V(0).Infof("removing old leader peer: %s", peerLeader)
  80. s.RaftHashicorp.RemoveServer(raft.ServerID(peerLeader), 0, 0)
  81. }
  82. }
  83. return
  84. case <-time.After(updatePeersTimeout):
  85. return
  86. }
  87. }
  88. }
  89. func NewHashicorpRaftServer(option *RaftServerOption) (*RaftServer, error) {
  90. s := &RaftServer{
  91. peers: option.Peers,
  92. serverAddr: option.ServerAddr,
  93. dataDir: option.DataDir,
  94. topo: option.Topo,
  95. }
  96. c := raft.DefaultConfig()
  97. c.LocalID = raft.ServerID(s.serverAddr) // TODO maybee the IP:port address will change
  98. c.HeartbeatTimeout = time.Duration(float64(option.HeartbeatInterval) * (rand.Float64()*0.25 + 1))
  99. c.ElectionTimeout = option.ElectionTimeout
  100. if c.LeaderLeaseTimeout > c.HeartbeatTimeout {
  101. c.LeaderLeaseTimeout = c.HeartbeatTimeout
  102. }
  103. if glog.V(4) {
  104. c.LogLevel = "Debug"
  105. } else if glog.V(2) {
  106. c.LogLevel = "Info"
  107. } else if glog.V(1) {
  108. c.LogLevel = "Warn"
  109. } else if glog.V(0) {
  110. c.LogLevel = "Error"
  111. }
  112. if option.RaftBootstrap {
  113. os.RemoveAll(path.Join(s.dataDir, ldbFile))
  114. os.RemoveAll(path.Join(s.dataDir, sdbFile))
  115. os.RemoveAll(path.Join(s.dataDir, "snapshots"))
  116. }
  117. if err := os.MkdirAll(path.Join(s.dataDir, "snapshots"), os.ModePerm); err != nil {
  118. return nil, err
  119. }
  120. baseDir := s.dataDir
  121. ldb, err := boltdb.NewBoltStore(filepath.Join(baseDir, ldbFile))
  122. if err != nil {
  123. return nil, fmt.Errorf(`boltdb.NewBoltStore(%q): %v`, filepath.Join(baseDir, "logs.dat"), err)
  124. }
  125. sdb, err := boltdb.NewBoltStore(filepath.Join(baseDir, sdbFile))
  126. if err != nil {
  127. return nil, fmt.Errorf(`boltdb.NewBoltStore(%q): %v`, filepath.Join(baseDir, "stable.dat"), err)
  128. }
  129. fss, err := raft.NewFileSnapshotStore(baseDir, 3, os.Stderr)
  130. if err != nil {
  131. return nil, fmt.Errorf(`raft.NewFileSnapshotStore(%q, ...): %v`, baseDir, err)
  132. }
  133. s.TransportManager = transport.New(raft.ServerAddress(s.serverAddr), []grpc.DialOption{option.GrpcDialOption})
  134. stateMachine := StateMachine{topo: option.Topo}
  135. s.RaftHashicorp, err = raft.NewRaft(c, &stateMachine, ldb, sdb, fss, s.TransportManager.Transport())
  136. if err != nil {
  137. return nil, fmt.Errorf("raft.NewRaft: %v", err)
  138. }
  139. if option.RaftBootstrap || len(s.RaftHashicorp.GetConfiguration().Configuration().Servers) == 0 {
  140. cfg := s.AddPeersConfiguration()
  141. // Need to get lock, in case all servers do this at the same time.
  142. peerIdx := getPeerIdx(s.serverAddr, s.peers)
  143. timeSpeep := time.Duration(float64(c.LeaderLeaseTimeout) * (rand.Float64()*0.25 + 1) * float64(peerIdx))
  144. glog.V(0).Infof("Bootstrapping idx: %d sleep: %v new cluster: %+v", peerIdx, timeSpeep, cfg)
  145. time.Sleep(timeSpeep)
  146. f := s.RaftHashicorp.BootstrapCluster(cfg)
  147. if err := f.Error(); err != nil {
  148. return nil, fmt.Errorf("raft.Raft.BootstrapCluster: %v", err)
  149. }
  150. } else {
  151. go s.UpdatePeers()
  152. }
  153. ticker := time.NewTicker(c.HeartbeatTimeout * 10)
  154. if glog.V(4) {
  155. go func() {
  156. for {
  157. select {
  158. case <-ticker.C:
  159. cfuture := s.RaftHashicorp.GetConfiguration()
  160. if err = cfuture.Error(); err != nil {
  161. glog.Fatalf("error getting config: %s", err)
  162. }
  163. configuration := cfuture.Configuration()
  164. glog.V(4).Infof("Showing peers known by %s:\n%+v", s.RaftHashicorp.String(), configuration.Servers)
  165. }
  166. }
  167. }()
  168. }
  169. return s, nil
  170. }