2013-12-10 05:34:05 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-05 16:25:23 +08:00
|
|
|
"io/ioutil"
|
2013-12-10 05:34:05 +08:00
|
|
|
"net/http"
|
2014-02-05 16:25:23 +08:00
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2015-05-04 03:37:49 +08:00
|
|
|
"github.com/chrislusf/raft"
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2013-12-10 05:34:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handles incoming RAFT joins.
|
|
|
|
func (s *RaftServer) joinHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
glog.V(0).Infoln("Processing incoming join. Current Leader", s.raftServer.Leader(), "Self", s.raftServer.Name(), "Peers", s.raftServer.Peers())
|
|
|
|
command := &raft.DefaultJoinCommand{}
|
|
|
|
|
2014-02-05 16:25:23 +08:00
|
|
|
commandText, _ := ioutil.ReadAll(req.Body)
|
|
|
|
glog.V(0).Info("Command:", string(commandText))
|
|
|
|
if err := json.NewDecoder(strings.NewReader(string(commandText))).Decode(&command); err != nil {
|
2014-04-17 14:43:27 +08:00
|
|
|
glog.V(0).Infoln("Error decoding json message:", err, string(commandText))
|
2013-12-10 05:34:05 +08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infoln("join command from Name", command.Name, "Connection", command.ConnectionString)
|
|
|
|
|
|
|
|
if _, err := s.raftServer.Do(command); err != nil {
|
|
|
|
switch err {
|
|
|
|
case raft.NotLeaderError:
|
|
|
|
s.redirectToLeader(w, req)
|
|
|
|
default:
|
|
|
|
glog.V(0).Infoln("Error processing join:", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RaftServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
|
|
|
|
s.router.HandleFunc(pattern, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RaftServer) redirectToLeader(w http.ResponseWriter, req *http.Request) {
|
2014-04-12 07:23:58 +08:00
|
|
|
if leader, e := s.topo.Leader(); e == nil {
|
2014-02-05 16:25:23 +08:00
|
|
|
//http.StatusMovedPermanently does not cause http POST following redirection
|
2014-04-12 07:23:58 +08:00
|
|
|
glog.V(0).Infoln("Redirecting to", http.StatusMovedPermanently, "http://"+leader+req.URL.Path)
|
|
|
|
http.Redirect(w, req, "http://"+leader+req.URL.Path, http.StatusMovedPermanently)
|
2013-12-10 05:34:05 +08:00
|
|
|
} else {
|
|
|
|
glog.V(0).Infoln("Error: Leader Unknown")
|
|
|
|
http.Error(w, "Leader unknown", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 17:10:07 +08:00
|
|
|
|
|
|
|
func (s *RaftServer) statusHandler(w http.ResponseWriter, r *http.Request) {
|
2014-04-16 00:30:08 +08:00
|
|
|
ret := operation.ClusterStatusResult{
|
|
|
|
IsLeader: s.topo.IsLeader(),
|
|
|
|
Peers: s.Peers(),
|
|
|
|
}
|
2014-04-12 07:23:58 +08:00
|
|
|
if leader, e := s.topo.Leader(); e == nil {
|
2014-04-16 00:30:08 +08:00
|
|
|
ret.Leader = leader
|
2014-04-12 07:23:58 +08:00
|
|
|
}
|
2015-01-08 16:19:32 +08:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, ret)
|
2014-02-04 17:10:07 +08:00
|
|
|
}
|