diff --git a/client_handlers.go b/client_handlers.go index 8b8e0b1ce..d5f4e9984 100644 --- a/client_handlers.go +++ b/client_handlers.go @@ -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 diff --git a/command.go b/command.go index 821bd1a27..d05156102 100644 --- a/command.go +++ b/command.go @@ -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 } diff --git a/error.go b/error.go index 9c17c244c..2830ef064 100644 --- a/error.go +++ b/error.go @@ -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 { diff --git a/etcd.go b/etcd.go index 306219aff..5278741be 100644 --- a/etcd.go +++ b/etcd.go @@ -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 {