Browse Source

add machinesHttpHandler

Xiang Li 12 years ago
parent
commit
76ef446baf
4 changed files with 40 additions and 9 deletions
  1. 35 6
      client_handlers.go
  2. 2 2
      command.go
  3. 1 0
      error.go
  4. 2 1
      etcd.go

+ 35 - 6
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

+ 2 - 2
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
 }

+ 1 - 0
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 {

+ 2 - 1
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 {