2013-12-10 05:34:05 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/goraft/raft"
|
|
|
|
"github.com/gorilla/mux"
|
2014-02-05 16:25:23 +08:00
|
|
|
"io/ioutil"
|
2013-12-10 05:34:05 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RaftServer struct {
|
|
|
|
peers []string // initial peers to join with
|
|
|
|
raftServer raft.Server
|
|
|
|
dataDir string
|
|
|
|
httpAddr string
|
|
|
|
version string
|
|
|
|
router *mux.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRaftServer(r *mux.Router, version string, peers []string, httpAddr string, dataDir string) *RaftServer {
|
|
|
|
s := &RaftServer{
|
|
|
|
version: version,
|
|
|
|
peers: peers,
|
|
|
|
httpAddr: httpAddr,
|
|
|
|
dataDir: dataDir,
|
|
|
|
router: r,
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:11:28 +08:00
|
|
|
if glog.V(4) {
|
|
|
|
raft.SetLogLevel(2)
|
|
|
|
}
|
2013-12-10 05:34:05 +08:00
|
|
|
|
|
|
|
var err error
|
2014-02-04 17:10:07 +08:00
|
|
|
transporter := raft.NewHTTPTransporter("/cluster")
|
2013-12-10 05:34:05 +08:00
|
|
|
s.raftServer, err = raft.NewServer(s.httpAddr, s.dataDir, transporter, nil, nil, "")
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infoln(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
transporter.Install(s.raftServer, s)
|
2014-01-29 03:15:24 +08:00
|
|
|
s.raftServer.SetHeartbeatInterval(1 * time.Second)
|
2013-12-10 05:34:05 +08:00
|
|
|
s.raftServer.SetElectionTimeout(1500 * time.Millisecond)
|
|
|
|
s.raftServer.Start()
|
|
|
|
|
2014-02-04 17:10:07 +08:00
|
|
|
s.router.HandleFunc("/cluster/join", s.joinHandler).Methods("POST")
|
2014-02-04 17:11:28 +08:00
|
|
|
s.router.HandleFunc("/cluster/status", s.statusHandler).Methods("GET")
|
2013-12-10 05:34:05 +08:00
|
|
|
|
|
|
|
// Join to leader if specified.
|
|
|
|
if len(s.peers) > 0 {
|
|
|
|
glog.V(0).Infoln("Joining cluster:", strings.Join(s.peers, ","))
|
|
|
|
|
|
|
|
if !s.raftServer.IsLogEmpty() {
|
|
|
|
glog.V(0).Infoln("Cannot join with an existing log")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Join(s.peers); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infoln("Joined cluster")
|
|
|
|
|
|
|
|
// Initialize the server by joining itself.
|
|
|
|
} else if s.raftServer.IsLogEmpty() {
|
|
|
|
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 {
|
|
|
|
glog.V(0).Infoln("Recovered from log")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2014-02-10 15:37:29 +08:00
|
|
|
func (s *RaftServer) Name() string {
|
|
|
|
return s.raftServer.Name()
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:25:23 +08:00
|
|
|
func (s *RaftServer) IsLeader() bool {
|
|
|
|
return s.Leader() == s.raftServer.Name()
|
|
|
|
}
|
|
|
|
|
2013-12-10 05:34:05 +08:00
|
|
|
func (s *RaftServer) Leader() string {
|
|
|
|
l := s.raftServer.Leader()
|
|
|
|
|
|
|
|
if l == "" {
|
|
|
|
// We are a single node cluster, we are the leader
|
|
|
|
return s.raftServer.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
members = append(members, strings.TrimPrefix(p.ConnectionString, "http://"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join joins an existing cluster.
|
|
|
|
func (s *RaftServer) Join(peers []string) error {
|
|
|
|
command := &raft.DefaultJoinCommand{
|
|
|
|
Name: s.raftServer.Name(),
|
|
|
|
ConnectionString: "http://" + s.httpAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
json.NewEncoder(&b).Encode(command)
|
|
|
|
|
|
|
|
for _, m := range peers {
|
2014-02-05 16:25:23 +08:00
|
|
|
target := fmt.Sprintf("http://%s/cluster/join", strings.TrimSpace(m))
|
|
|
|
glog.V(0).Infoln("Attempting to connect to:", target)
|
2013-12-10 05:34:05 +08:00
|
|
|
|
2014-02-05 16:25:23 +08:00
|
|
|
err := postFollowingOneRedirect(target, "application/json", &b)
|
2013-12-10 05:34:05 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2014-02-05 16:25:23 +08:00
|
|
|
glog.V(0).Infoln("Post returned error: ", err)
|
2013-12-10 05:34:05 +08:00
|
|
|
if _, ok := err.(*url.Error); ok {
|
|
|
|
// If we receive a network error try the next member
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Could not connect to any cluster peers")
|
|
|
|
}
|
2014-02-05 16:25:23 +08:00
|
|
|
|
|
|
|
// a workaround because http POST following redirection misses request body
|
|
|
|
func postFollowingOneRedirect(target string, contentType string, b *bytes.Buffer) error {
|
|
|
|
backupReader := bytes.NewReader(b.Bytes())
|
|
|
|
resp, err := http.Post(target, contentType, b)
|
|
|
|
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 == "" {
|
|
|
|
return errors.New(fmt.Sprintf("%d response missing Location header", resp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|