mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-10-15 20:06:19 +08:00
a start for distributed master support, not working yet
This commit is contained in:
46
go/weed/weed_server/raft_server_handlers.go
Normal file
46
go/weed/weed_server/raft_server_handlers.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package weed_server
|
||||
|
||||
import (
|
||||
"code.google.com/p/weed-fs/go/glog"
|
||||
"encoding/json"
|
||||
"github.com/goraft/raft"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 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{}
|
||||
|
||||
if err := json.NewDecoder(req.Body).Decode(&command); err != nil {
|
||||
glog.V(0).Infoln("Error decoding json message:", err)
|
||||
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) {
|
||||
if s.Leader() != "" {
|
||||
glog.V(0).Infoln("Redirecting to", "http://"+s.Leader()+req.URL.Path)
|
||||
http.Redirect(w, req, "http://"+s.Leader()+req.URL.Path, http.StatusMovedPermanently)
|
||||
} else {
|
||||
glog.V(0).Infoln("Error: Leader Unknown")
|
||||
http.Error(w, "Leader unknown", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user