2013-12-09 21:34:05 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-02-05 08:25:23 +00:00
|
|
|
"io/ioutil"
|
2014-04-11 23:23:58 +00:00
|
|
|
"math/rand"
|
2013-12-09 21:34:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-10-27 08:09:45 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-05-24 17:45:28 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2013-12-09 21:34:05 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-05-03 19:37:49 +00:00
|
|
|
"github.com/chrislusf/raft"
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/topology"
|
2014-10-26 18:34:55 +00:00
|
|
|
"github.com/gorilla/mux"
|
2013-12-09 21:34:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RaftServer struct {
|
|
|
|
peers []string // initial peers to join with
|
|
|
|
raftServer raft.Server
|
|
|
|
dataDir string
|
|
|
|
httpAddr string
|
|
|
|
router *mux.Router
|
2014-03-16 06:03:49 +00:00
|
|
|
topo *topology.Topology
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 20:46:59 +00:00
|
|
|
func NewRaftServer(r *mux.Router, peers []string, httpAddr string, dataDir string, topo *topology.Topology, pulseSeconds int) *RaftServer {
|
2013-12-09 21:34:05 +00:00
|
|
|
s := &RaftServer{
|
|
|
|
peers: peers,
|
|
|
|
httpAddr: httpAddr,
|
|
|
|
dataDir: dataDir,
|
|
|
|
router: r,
|
2014-03-16 06:03:49 +00:00
|
|
|
topo: topo,
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 09:11:28 +00:00
|
|
|
if glog.V(4) {
|
|
|
|
raft.SetLogLevel(2)
|
|
|
|
}
|
2013-12-09 21:34:05 +00:00
|
|
|
|
2014-03-16 06:03:49 +00:00
|
|
|
raft.RegisterCommand(&topology.MaxVolumeIdCommand{})
|
|
|
|
|
2013-12-09 21:34:05 +00:00
|
|
|
var err error
|
2014-04-16 00:56:47 +00:00
|
|
|
transporter := raft.NewHTTPTransporter("/cluster", 0)
|
2014-03-16 20:49:49 +00:00
|
|
|
transporter.Transport.MaxIdleConnsPerHost = 1024
|
2014-10-20 03:03:00 +00:00
|
|
|
glog.V(1).Infof("Starting RaftServer with IP:%v:", httpAddr)
|
2014-03-16 20:49:49 +00:00
|
|
|
|
2016-05-24 17:45:28 +00:00
|
|
|
// Clear old cluster configurations if peers are changed
|
|
|
|
if oldPeers, changed := isPeersChanged(s.dataDir, httpAddr, s.peers); changed {
|
|
|
|
glog.V(0).Infof("Peers Change: %v => %v", oldPeers, s.peers)
|
2014-10-27 08:09:45 +00:00
|
|
|
os.RemoveAll(path.Join(s.dataDir, "conf"))
|
|
|
|
os.RemoveAll(path.Join(s.dataDir, "log"))
|
|
|
|
os.RemoveAll(path.Join(s.dataDir, "snapshot"))
|
|
|
|
}
|
|
|
|
|
2014-03-16 06:03:49 +00:00
|
|
|
s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, topo, "")
|
2013-12-09 21:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
transporter.Install(s.raftServer, s)
|
2017-01-18 17:54:43 +00:00
|
|
|
s.raftServer.SetHeartbeatInterval(500 * time.Millisecond)
|
|
|
|
s.raftServer.SetElectionTimeout(time.Duration(pulseSeconds) * 500 * time.Millisecond)
|
2013-12-09 21:34:05 +00:00
|
|
|
s.raftServer.Start()
|
|
|
|
|
2014-02-04 09:10:07 +00:00
|
|
|
s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
|
2014-02-04 09:11:28 +00:00
|
|
|
s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
|
2013-12-09 21:34:05 +00:00
|
|
|
|
|
|
|
if len(s.peers) > 0 {
|
2014-10-27 08:09:45 +00:00
|
|
|
// Join to leader if specified.
|
2016-05-24 17:45:28 +00:00
|
|
|
for {
|
|
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
|
|
|
|
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
|
|
|
|
firstJoinError := s.Join(s.peers)
|
|
|
|
if firstJoinError != nil {
|
|
|
|
glog.V(0).Infoln("No existing server found. Starting as leader in the new cluster.")
|
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
|
|
|
ConnectionString: "http://" + s.httpAddr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
2014-04-11 23:23:58 +00:00
|
|
|
}
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
} else if s.raftServer.IsLogEmpty() {
|
2014-10-27 08:09:45 +00:00
|
|
|
// Initialize the server by joining itself.
|
2013-12-09 21:34:05 +00:00
|
|
|
glog.V(0).Infoln("Initializing new cluster")
|
|
|
|
|
|
|
|
_, err := s.raftServer.Do(&raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
|
|
|
ConnectionString: "http://" + s.httpAddr,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2014-10-27 08:09:45 +00:00
|
|
|
glog.V(0).Infoln("Old conf,log,snapshot should have been removed.")
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-02-05 08:25:23 +00:00
|
|
|
func (s *RaftServer) Peers() (members []string) {
|
2013-12-09 21:34:05 +00:00
|
|
|
peers := s.raftServer.Peers()
|
|
|
|
|
|
|
|
for _, p := range peers {
|
|
|
|
members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-24 17:45:28 +00:00
|
|
|
func isPeersChanged(dir string, self string, peers []string) (oldPeers []string, changed bool) {
|
|
|
|
confPath := path.Join(dir, "conf")
|
|
|
|
// open conf file
|
|
|
|
b, err := ioutil.ReadFile(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return oldPeers, true
|
|
|
|
}
|
|
|
|
conf := &raft.Config{}
|
|
|
|
if err = json.Unmarshal(b, conf); err != nil {
|
|
|
|
return oldPeers, true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range conf.Peers {
|
|
|
|
oldPeers = append(oldPeers, strings.TrimPrefix(p.ConnectionString, "http://"))
|
|
|
|
}
|
|
|
|
oldPeers = append(oldPeers, self)
|
|
|
|
|
|
|
|
sort.Strings(peers)
|
|
|
|
sort.Strings(oldPeers)
|
|
|
|
|
2016-11-03 07:46:59 +00:00
|
|
|
return oldPeers, !reflect.DeepEqual(peers, oldPeers)
|
2016-05-24 17:45:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:34:05 +00:00
|
|
|
// Join joins an existing cluster.
|
|
|
|
func (s *RaftServer) Join(peers []string) error {
|
|
|
|
command := &raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
|
|
|
ConnectionString: "http://" + s.httpAddr,
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:43:27 +00:00
|
|
|
var err error
|
2013-12-09 21:34:05 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
json.NewEncoder(&b).Encode(command)
|
|
|
|
for _, m := range peers {
|
2014-04-17 06:43:27 +00:00
|
|
|
if m == s.httpAddr {
|
|
|
|
continue
|
|
|
|
}
|
2014-02-05 08:25:23 +00:00
|
|
|
target := fmt.Sprintf("http://%s/cluster/join", strings.TrimSpace(m))
|
|
|
|
glog.V(0).Infoln("Attempting to connect to:", target)
|
2013-12-09 21:34:05 +00:00
|
|
|
|
2016-11-08 03:00:46 +00:00
|
|
|
err = postFollowingOneRedirect(target, "application/json", b)
|
2013-12-09 21:34:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2014-03-12 13:23:07 +00:00
|
|
|
glog.V(0).Infoln("Post returned error: ", err.Error())
|
2013-12-09 21:34:05 +00:00
|
|
|
if _, ok := err.(*url.Error); ok {
|
|
|
|
// If we receive a network error try the next member
|
|
|
|
continue
|
|
|
|
}
|
2014-04-17 06:43:27 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
2013-12-09 21:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Could not connect to any cluster peers")
|
|
|
|
}
|
2014-02-05 08:25:23 +00:00
|
|
|
|
|
|
|
// a workaround because http POST following redirection misses request body
|
2016-11-08 03:00:46 +00:00
|
|
|
func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer) error {
|
2014-02-05 08:25:23 +00:00
|
|
|
backupReader := bytes.NewReader(b.Bytes())
|
2016-11-08 03:00:46 +00:00
|
|
|
resp, err := http.Post(target, contentType, &b)
|
2014-02-05 08:25:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
reply, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
statusCode := resp.StatusCode
|
|
|
|
|
|
|
|
if statusCode == http.StatusMovedPermanently {
|
|
|
|
var urlStr string
|
|
|
|
if urlStr = resp.Header.Get("Location"); urlStr == "" {
|
2015-03-10 07:20:31 +00:00
|
|
|
return fmt.Errorf("%d response missing Location header", resp.StatusCode)
|
2014-02-05 08:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infoln("Post redirected to ", urlStr)
|
|
|
|
resp2, err2 := http.Post(urlStr, contentType, backupReader)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
defer resp2.Body.Close()
|
|
|
|
reply, _ = ioutil.ReadAll(resp2.Body)
|
|
|
|
statusCode = resp2.StatusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infoln("Post returned status: ", statusCode, string(reply))
|
|
|
|
if statusCode != http.StatusOK {
|
|
|
|
return errors.New(string(reply))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|