Merge pull request #1 from xiangli-cmu/addGetClusterMachines

add machinesHttpHandler
release-0.4
Xiang Li 2013-07-13 19:17:50 -07:00
commit e2f90020ae
4 changed files with 40 additions and 9 deletions

View File

@ -146,14 +146,13 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool)
return
} else {
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
if body == nil {
http.NotFound((*w), req)
} else {
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
(*w).WriteHeader(http.StatusOK)
(*w).Write(body)
}
@ -204,8 +203,38 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool)
// Handler to return the current leader name
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) {
leader := raftServer.Leader()
if leader != "" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(raftServer.Leader()))
} else {
// not likely, but it may happen
w.WriteHeader(http.StatusInternalServerError)
w.Write(newJsonError(301, ""))
}
}
// Handler to return all the known machines in the current cluster
func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) {
peers := raftServer.Peers()
// Add itself to the machine list first
// Since peer map does not contain the server itself
machines := raftServer.Name()
// Add all peers to the list and sepearte by comma
// We do not use json here since we accept machines list
// in the command line seperate by comma.
for peerName, _ := range peers {
machines = machines + "," + peerName
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(raftServer.Leader()))
w.Write([]byte(machines))
}
// Get Handler

View File

@ -116,6 +116,6 @@ func (c *JoinCommand) CommandName() string {
// Join a server to the cluster
func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
err := server.AddPeer(c.Name)
// no result will be returned
return nil, err
return []byte("join success"), err
}

View File

@ -20,6 +20,7 @@ func init() {
errors[203] = "The given index in POST form is not a number"
// raft related errors
errors[300] = "Raft Internal Error"
errors[301] = "Durning Leader Election"
}
type jsonError struct {

View File

@ -97,7 +97,7 @@ const (
// Timeout for internal raft http connection
// The original timeout for http is 45 seconds
// which is too long for our usage.
HTTPTIMEOUT = time.Second
HTTPTIMEOUT = 10 * time.Second
)
//------------------------------------------------------------------------------
@ -371,6 +371,7 @@ func startClientTransport(port int, st int) {
http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
http.HandleFunc("/"+version+"/testAndSet/", TestAndSetHttpHandler)
http.HandleFunc("/leader", LeaderHttpHandler)
http.HandleFunc("/machines", MachinesHttpHandler)
switch st {