2013-12-09 21:34:05 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-02-05 08:25:23 +00:00
|
|
|
"io/ioutil"
|
2013-12-09 21:34:05 +00:00
|
|
|
"net/http"
|
2014-02-05 08:25:23 +00:00
|
|
|
"strings"
|
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/operation"
|
2013-12-09 21:34:05 +00: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 08:25:23 +00: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 06:43:27 +00:00
|
|
|
glog.V(0).Infoln("Error decoding json message:", err, string(commandText))
|
2013-12-09 21:34:05 +00: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-11 23:23:58 +00:00
|
|
|
if leader, e := s.topo.Leader(); e == nil {
|
2014-02-05 08:25:23 +00:00
|
|
|
//http.StatusMovedPermanently does not cause http POST following redirection
|
2014-04-11 23:23:58 +00: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-09 21:34:05 +00:00
|
|
|
} else {
|
|
|
|
glog.V(0).Infoln("Error: Leader Unknown")
|
|
|
|
http.Error(w, "Leader unknown", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 09:10:07 +00:00
|
|
|
|
|
|
|
func (s *RaftServer) statusHandler(w http.ResponseWriter, r *http.Request) {
|
2014-04-15 16:30:08 +00:00
|
|
|
ret := operation.ClusterStatusResult{
|
|
|
|
IsLeader: s.topo.IsLeader(),
|
|
|
|
Peers: s.Peers(),
|
|
|
|
}
|
2014-04-11 23:23:58 +00:00
|
|
|
if leader, e := s.topo.Leader(); e == nil {
|
2014-04-15 16:30:08 +00:00
|
|
|
ret.Leader = leader
|
2014-04-11 23:23:58 +00:00
|
|
|
}
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, ret)
|
2014-02-04 09:10:07 +00:00
|
|
|
}
|