Browse Source

during the leader election, return error to client

Xiang Li 12 years ago
parent
commit
f6b3d8a2af
2 changed files with 15 additions and 3 deletions
  1. 8 2
      etcd.go
  2. 7 1
      handlers.go

+ 8 - 2
etcd.go

@@ -168,8 +168,14 @@ func main() {
 		fatal("%v", err)
 		fatal("%v", err)
 	}
 	}
 
 
-	server.LoadSnapshot()
-	debug("%s finished load snapshot", server.Name())
+	err = server.LoadSnapshot()
+
+	if err == nil {
+		debug("%s finished load snapshot", server.Name())
+	} else {
+		fmt.Println(err)
+		debug("%s bad snapshot", server.Name())
+	}
 	server.Initialize()
 	server.Initialize()
 	debug("%s finished init", server.Name())
 	debug("%s finished init", server.Name())
 	server.SetElectionTimeout(ELECTIONTIMTOUT)
 	server.SetElectionTimeout(ELECTIONTIMTOUT)

+ 7 - 1
handlers.go

@@ -163,6 +163,12 @@ func excute(c Command, w *http.ResponseWriter, req *http.Request) {
 			return
 			return
 		}
 		}
 	} else {
 	} else {
+		// current no leader
+		if server.Leader() == "" {
+			(*w).WriteHeader(http.StatusInternalServerError)
+			return
+		}
+
 		// tell the client where is the leader
 		// tell the client where is the leader
 		debug("Redirect to the leader %s", server.Leader())
 		debug("Redirect to the leader %s", server.Leader())
 
 
@@ -176,7 +182,7 @@ func excute(c Command, w *http.ResponseWriter, req *http.Request) {
 
 
 		url := scheme + leaderClient() + path
 		url := scheme + leaderClient() + path
 
 
-		debug("redirect to ", url)
+		debug("redirect to %s", url)
 		http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
 		http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
 		return
 		return
 	}
 	}