aboutsummaryrefslogtreecommitdiff
path: root/weed/server/raft_server_handlers.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2016-06-02 18:09:14 -0700
committerChris Lu <chris.lu@gmail.com>2016-06-02 18:09:14 -0700
commit5ce6bbf07672bf3f3c8d26cd2ce0e3e853a47c44 (patch)
tree2e4dd2ad0a618ab2b7cdebcdb9c503526c31e2e8 /weed/server/raft_server_handlers.go
parentcaeffa3998adc060fa66c4cd77af971ff2d26c57 (diff)
downloadseaweedfs-5ce6bbf07672bf3f3c8d26cd2ce0e3e853a47c44.tar.xz
seaweedfs-5ce6bbf07672bf3f3c8d26cd2ce0e3e853a47c44.zip
directory structure change to work with glide
glide has its own requirements. My previous workaround caused me some code checkin errors. Need to fix this.
Diffstat (limited to 'weed/server/raft_server_handlers.go')
-rw-r--r--weed/server/raft_server_handlers.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/weed/server/raft_server_handlers.go b/weed/server/raft_server_handlers.go
new file mode 100644
index 000000000..335ba668f
--- /dev/null
+++ b/weed/server/raft_server_handlers.go
@@ -0,0 +1,64 @@
+package weed_server
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "strings"
+
+ "github.com/chrislusf/raft"
+ "github.com/chrislusf/seaweedfs/weed/glog"
+ "github.com/chrislusf/seaweedfs/weed/operation"
+)
+
+// 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{}
+
+ commandText, _ := ioutil.ReadAll(req.Body)
+ glog.V(0).Info("Command:", string(commandText))
+ if err := json.NewDecoder(strings.NewReader(string(commandText))).Decode(&command); err != nil {
+ glog.V(0).Infoln("Error decoding json message:", err, string(commandText))
+ 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 leader, e := s.topo.Leader(); e == nil {
+ //http.StatusMovedPermanently does not cause http POST following redirection
+ glog.V(0).Infoln("Redirecting to", http.StatusMovedPermanently, "http://"+leader+req.URL.Path)
+ http.Redirect(w, req, "http://"+leader+req.URL.Path, http.StatusMovedPermanently)
+ } else {
+ glog.V(0).Infoln("Error: Leader Unknown")
+ http.Error(w, "Leader unknown", http.StatusInternalServerError)
+ }
+}
+
+func (s *RaftServer) statusHandler(w http.ResponseWriter, r *http.Request) {
+ ret := operation.ClusterStatusResult{
+ IsLeader: s.topo.IsLeader(),
+ Peers: s.Peers(),
+ }
+ if leader, e := s.topo.Leader(); e == nil {
+ ret.Leader = leader
+ }
+ writeJsonQuiet(w, r, http.StatusOK, ret)
+}