etcd/raft_handlers.go

145 lines
3.9 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
2013-07-10 04:31:24 +04:00
"net/http"
2013-08-20 04:19:45 +04:00
"github.com/coreos/go-raft"
)
//-------------------------------------------------------------
// Handlers to handle raft related request via raft server port
//-------------------------------------------------------------
// Get all the current logs
func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-08-15 01:45:47 +04:00
debugf("[recv] GET %s/log", r.url)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
2013-08-15 03:27:52 +04:00
json.NewEncoder(w).Encode(r.LogEntries())
}
// Response to vote request
func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
rvreq := &raft.RequestVoteRequest{}
err := decodeJsonRequest(req, rvreq)
if err == nil {
2013-08-15 01:45:47 +04:00
debugf("[recv] POST %s/vote [%s]", r.url, rvreq.CandidateName)
2013-08-15 03:27:52 +04:00
if resp := r.RequestVote(rvreq); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
return
}
}
warnf("[vote] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
// Response to append entries request
func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
aereq := &raft.AppendEntriesRequest{}
err := decodeJsonRequest(req, aereq)
if err == nil {
2013-08-15 01:45:47 +04:00
debugf("[recv] POST %s/log/append [%d]", r.url, len(aereq.Entries))
2013-08-21 01:05:23 +04:00
2013-08-22 01:34:52 +04:00
r.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength))
2013-08-21 01:05:23 +04:00
2013-08-15 03:27:52 +04:00
if resp := r.AppendEntries(aereq); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
if !resp.Success {
debugf("[Append Entry] Step back")
}
return
}
}
warnf("[Append Entry] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
// Response to recover from snapshot request
func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
aereq := &raft.SnapshotRequest{}
err := decodeJsonRequest(req, aereq)
if err == nil {
2013-08-15 01:45:47 +04:00
debugf("[recv] POST %s/snapshot/ ", r.url)
2013-08-15 03:27:52 +04:00
if resp := r.RequestSnapshot(aereq); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
return
}
}
warnf("[Snapshot] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
// Response to recover from snapshot request
func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
aereq := &raft.SnapshotRecoveryRequest{}
err := decodeJsonRequest(req, aereq)
if err == nil {
2013-08-15 01:45:47 +04:00
debugf("[recv] POST %s/snapshotRecovery/ ", r.url)
2013-08-15 03:27:52 +04:00
if resp := r.SnapshotRecoveryRequest(aereq); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
return
}
}
warnf("[Snapshot] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
2013-08-10 10:03:49 +04:00
// Get the port that listening for etcd connecting of the server
func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-08-15 01:45:47 +04:00
debugf("[recv] Get %s/etcdURL/ ", r.url)
w.WriteHeader(http.StatusOK)
2013-08-10 10:03:49 +04:00
w.Write([]byte(argInfo.EtcdURL))
}
// Response to the join request
2013-08-18 07:41:15 +04:00
func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
command := &JoinCommand{}
if err := decodeJsonRequest(req, command); err == nil {
debugf("Receive Join Request from %s", command.Name)
2013-08-18 07:41:15 +04:00
return dispatch(command, w, req, false)
} else {
w.WriteHeader(http.StatusInternalServerError)
2013-08-18 07:41:15 +04:00
return nil
}
}
2013-08-19 23:10:11 +04:00
// Response to remove request
func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "DELETE" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
nodeName := req.URL.Path[len("/remove/"):]
command := &RemoveCommand{
Name: nodeName,
}
debugf("[recv] Remove Request [%s]", command.Name)
dispatch(command, w, req, false)
}
2013-08-10 08:06:16 +04:00
// Response to the name request
func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-08-15 01:45:47 +04:00
debugf("[recv] Get %s/name/ ", r.url)
2013-08-10 08:06:16 +04:00
w.WriteHeader(http.StatusOK)
2013-08-15 03:27:52 +04:00
w.Write([]byte(r.name))
}
// Response to the name request
func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
debugf("[recv] Get %s/version/ ", r.url)
w.WriteHeader(http.StatusOK)
w.Write([]byte(r.version))
}