2013-12-10 05:34:05 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-24 14:06:44 +08:00
|
|
|
"math/rand"
|
2014-10-27 16:09:45 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-05-25 01:45:28 +08:00
|
|
|
"sort"
|
2021-09-13 13:47:52 +08:00
|
|
|
"strings"
|
2013-12-10 05:34:05 +08:00
|
|
|
"time"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2020-03-04 16:39:47 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
|
|
|
|
2015-05-04 03:37:49 +08:00
|
|
|
"github.com/chrislusf/raft"
|
2020-03-04 16:39:47 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
2013-12-10 05:34:05 +08:00
|
|
|
)
|
|
|
|
|
2022-02-12 18:19:49 +08:00
|
|
|
type RaftServerOption struct {
|
|
|
|
GrpcDialOption grpc.DialOption
|
|
|
|
Peers []pb.ServerAddress
|
|
|
|
ServerAddr pb.ServerAddress
|
|
|
|
DataDir string
|
|
|
|
Topo *topology.Topology
|
|
|
|
RaftResumeState bool
|
|
|
|
HeartbeatInterval time.Duration
|
|
|
|
ElectionTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2013-12-10 05:34:05 +08:00
|
|
|
type RaftServer struct {
|
2021-09-13 13:47:52 +08:00
|
|
|
peers []pb.ServerAddress // initial peers to join with
|
2013-12-10 05:34:05 +08:00
|
|
|
raftServer raft.Server
|
|
|
|
dataDir string
|
2021-09-13 13:47:52 +08:00
|
|
|
serverAddr pb.ServerAddress
|
2014-03-16 14:03:49 +08:00
|
|
|
topo *topology.Topology
|
2019-02-19 14:38:14 +08:00
|
|
|
*raft.GrpcServer
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2020-10-03 02:01:20 +08:00
|
|
|
type StateMachine struct {
|
|
|
|
raft.StateMachine
|
|
|
|
topo *topology.Topology
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateMachine) Save() ([]byte, error) {
|
|
|
|
state := topology.MaxVolumeIdCommand{
|
|
|
|
MaxVolumeId: s.topo.GetMaxVolumeId(),
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("Save raft state %+v", state)
|
|
|
|
return json.Marshal(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateMachine) Recovery(data []byte) error {
|
|
|
|
state := topology.MaxVolumeIdCommand{}
|
|
|
|
err := json.Unmarshal(data, &state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("Recovery raft state %+v", state)
|
|
|
|
s.topo.UpAdjustMaxVolumeId(state.MaxVolumeId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-12 18:19:49 +08:00
|
|
|
func NewRaftServer(option *RaftServerOption) (*RaftServer, error) {
|
2013-12-10 05:34:05 +08:00
|
|
|
s := &RaftServer{
|
2022-02-12 18:19:49 +08:00
|
|
|
peers: option.Peers,
|
|
|
|
serverAddr: option.ServerAddr,
|
|
|
|
dataDir: option.DataDir,
|
|
|
|
topo: option.Topo,
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 17:11:28 +08:00
|
|
|
if glog.V(4) {
|
|
|
|
raft.SetLogLevel(2)
|
|
|
|
}
|
2013-12-10 05:34:05 +08:00
|
|
|
|
2014-03-16 14:03:49 +08:00
|
|
|
raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
|
|
|
|
|
2013-12-10 05:34:05 +08:00
|
|
|
var err error
|
2022-02-12 18:19:49 +08:00
|
|
|
transporter := raft.NewGrpcTransporter(option.GrpcDialOption)
|
|
|
|
glog.V(0).Infof("Starting RaftServer with %v", option.ServerAddr)
|
2014-03-17 04:49:49 +08:00
|
|
|
|
2022-03-25 16:40:19 +08:00
|
|
|
// always clear previous log to avoid server is promotable
|
|
|
|
os.RemoveAll(path.Join(s.dataDir, "log"))
|
2022-02-12 18:19:49 +08:00
|
|
|
if !option.RaftResumeState {
|
2020-10-03 02:01:20 +08:00
|
|
|
// always clear previous metadata
|
|
|
|
os.RemoveAll(path.Join(s.dataDir, "conf"))
|
|
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot"))
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path.Join(s.dataDir, "snapshot"), 0600); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-12 18:19:49 +08:00
|
|
|
stateMachine := StateMachine{topo: option.Topo}
|
|
|
|
s.raftServer, err = raft.NewServer(string(s.serverAddr), s.dataDir, transporter, stateMachine, option.Topo, "")
|
2013-12-10 05:34:05 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
2020-10-03 02:01:20 +08:00
|
|
|
return nil, err
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
2022-02-12 18:19:49 +08:00
|
|
|
heartbeatInterval := time.Duration(float64(option.HeartbeatInterval) * (rand.Float64()*0.25 + 1))
|
|
|
|
s.raftServer.SetHeartbeatInterval(heartbeatInterval)
|
|
|
|
s.raftServer.SetElectionTimeout(option.ElectionTimeout)
|
2020-10-03 02:01:20 +08:00
|
|
|
if err := s.raftServer.LoadSnapshot(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.raftServer.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-10 05:34:05 +08:00
|
|
|
|
2018-06-12 16:54:09 +08:00
|
|
|
for _, peer := range s.peers {
|
2021-09-13 13:47:52 +08:00
|
|
|
if err := s.raftServer.AddPeer(string(peer), peer.ToGrpcAddress()); err != nil {
|
2020-10-03 02:01:20 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-05 00:56:17 +08:00
|
|
|
}
|
2020-10-03 02:01:20 +08:00
|
|
|
|
2020-10-05 00:56:17 +08:00
|
|
|
// Remove deleted peers
|
2020-10-07 16:25:39 +08:00
|
|
|
for existsPeerName := range s.raftServer.Peers() {
|
2021-09-13 13:47:52 +08:00
|
|
|
exists := false
|
|
|
|
var existingPeer pb.ServerAddress
|
2020-10-07 16:25:39 +08:00
|
|
|
for _, peer := range s.peers {
|
2021-09-13 13:47:52 +08:00
|
|
|
if peer.ToGrpcAddress() == existsPeerName {
|
2020-10-07 16:25:39 +08:00
|
|
|
exists, existingPeer = true, peer
|
|
|
|
break
|
2020-10-05 00:56:17 +08:00
|
|
|
}
|
2020-10-07 16:25:39 +08:00
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
if err := s.raftServer.RemovePeer(existsPeerName); err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
glog.V(0).Infof("removing old peer %s", existingPeer)
|
2020-10-05 00:56:17 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-12 16:54:09 +08:00
|
|
|
}
|
2019-02-10 04:52:09 +08:00
|
|
|
|
2019-02-19 14:38:14 +08:00
|
|
|
s.GrpcServer = raft.NewGrpcServer(s.raftServer)
|
|
|
|
|
2022-02-12 18:19:49 +08:00
|
|
|
if s.raftServer.IsLogEmpty() && isTheFirstOne(option.ServerAddr, s.peers) {
|
2014-10-27 16:09:45 +08:00
|
|
|
// Initialize the server by joining itself.
|
2020-10-07 16:25:39 +08:00
|
|
|
// s.DoJoinCommand()
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 16:54:09 +08:00
|
|
|
glog.V(0).Infof("current cluster leader: %v", s.raftServer.Leader())
|
|
|
|
|
2020-10-03 02:01:20 +08:00
|
|
|
return s, nil
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
2014-02-05 16:25:23 +08:00
|
|
|
func (s *RaftServer) Peers() (members []string) {
|
2013-12-10 05:34:05 +08:00
|
|
|
peers := s.raftServer.Peers()
|
|
|
|
|
|
|
|
for _, p := range peers {
|
2019-02-19 14:38:14 +08:00
|
|
|
members = append(members, p.Name)
|
2013-12-10 05:34:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:47:52 +08:00
|
|
|
func isTheFirstOne(self pb.ServerAddress, peers []pb.ServerAddress) bool {
|
|
|
|
sort.Slice(peers, func(i, j int) bool {
|
|
|
|
return strings.Compare(string(peers[i]), string(peers[j])) < 0
|
|
|
|
})
|
2019-02-15 16:09:48 +08:00
|
|
|
if len(peers) <= 0 {
|
2019-02-10 04:52:09 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return self == peers[0]
|
|
|
|
}
|
2020-10-07 16:25:39 +08:00
|
|
|
|
|
|
|
func (s *RaftServer) DoJoinCommand() {
|
|
|
|
|
|
|
|
glog.V(0).Infoln("Initializing new cluster")
|
|
|
|
|
|
|
|
if _, err := s.raftServer.Do(&raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
2021-09-13 13:47:52 +08:00
|
|
|
ConnectionString: s.serverAddr.ToGrpcAddress(),
|
2020-10-07 16:25:39 +08:00
|
|
|
}); err != nil {
|
|
|
|
glog.Errorf("fail to send join command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|