Skip to content

Commit

Permalink
Merge pull request #1 from xiangli-cmu/addGetClusterMachines
Browse files Browse the repository at this point in the history
add machinesHttpHandler
  • Loading branch information
xiang90 committed Jul 14, 2013
2 parents 6821747 + 76ef446 commit e2f9002
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
41 changes: 35 additions & 6 deletions client_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit e2f9002

Please sign in to comment.