mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge branch 'fileSystem' of https://github.com/xiangli-cmu/etcd into fileSystem
This commit is contained in:
commit
7588f2da47
@ -105,7 +105,7 @@ func CreateHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
return dispatch(command, w, req, true)
|
return dispatchEtcdCommand(command, w, req)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
return dispatch(command, w, req, true)
|
return dispatchEtcdCommand(command, w, req)
|
||||||
|
|
||||||
} else { // update with test
|
} else { // update with test
|
||||||
var prevIndex uint64
|
var prevIndex uint64
|
||||||
@ -161,7 +161,7 @@ func UpdateHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
PrevIndex: prevIndex,
|
PrevIndex: prevIndex,
|
||||||
}
|
}
|
||||||
|
|
||||||
return dispatch(command, w, req, true)
|
return dispatchEtcdCommand(command, w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -179,37 +179,12 @@ func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
command.Recursive = true
|
command.Recursive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return dispatch(command, w, req, true)
|
return dispatchEtcdCommand(command, w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch the command to leader
|
// Dispatch the command to leader
|
||||||
func dispatch(c Command, w http.ResponseWriter, req *http.Request, etcd bool) error {
|
func dispatchEtcdCommand(c Command, w http.ResponseWriter, req *http.Request) error {
|
||||||
if r.State() == raft.Leader {
|
return dispatch(c, w, req, nameToEtcdURL)
|
||||||
if body, err := r.Do(c); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
if body == nil {
|
|
||||||
return etcdErr.NewError(300, "Empty result from raft")
|
|
||||||
} else {
|
|
||||||
body, _ := body.([]byte)
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(body)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
leader := r.Leader()
|
|
||||||
// current no leader
|
|
||||||
if leader == "" {
|
|
||||||
return etcdErr.NewError(300, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
redirect(leader, etcd, w, req)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return etcdErr.NewError(300, "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
@ -294,7 +269,8 @@ func GetHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
if req.FormValue("consistent") == "true" {
|
if req.FormValue("consistent") == "true" {
|
||||||
if r.State() != raft.Leader {
|
if r.State() != raft.Leader {
|
||||||
leader := r.Leader()
|
leader := r.Leader()
|
||||||
redirect(leader, true, w, req)
|
url, _ := nameToEtcdURL(leader)
|
||||||
|
redirect(url, w, req)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
|||||||
|
|
||||||
if err := decodeJsonRequest(req, command); err == nil {
|
if err := decodeJsonRequest(req, command); err == nil {
|
||||||
debugf("Receive Join Request from %s", command.Name)
|
debugf("Receive Join Request from %s", command.Name)
|
||||||
return dispatch(command, w, req, false)
|
return dispatchRaftCommand(command, w, req)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return nil
|
return nil
|
||||||
@ -125,8 +125,7 @@ func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
debugf("[recv] Remove Request [%s]", command.Name)
|
debugf("[recv] Remove Request [%s]", command.Name)
|
||||||
|
|
||||||
dispatch(command, w, req, false)
|
dispatchRaftCommand(command, w, req)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to the name request
|
// Response to the name request
|
||||||
@ -142,3 +141,7 @@ func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(r.version))
|
w.Write([]byte(r.version))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dispatchRaftCommand(c Command, w http.ResponseWriter, req *http.Request) error {
|
||||||
|
return dispatch(c, w, req, nameToRaftURL)
|
||||||
|
}
|
||||||
|
41
util.go
41
util.go
@ -65,17 +65,40 @@ func startWebInterface() {
|
|||||||
// HTTP Utilities
|
// HTTP Utilities
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
|
|
||||||
func redirect(node string, etcd bool, w http.ResponseWriter, req *http.Request) {
|
func dispatch(c Command, w http.ResponseWriter, req *http.Request, toURL func(name string) (string, bool)) error {
|
||||||
var url string
|
if r.State() == raft.Leader {
|
||||||
|
if body, err := r.Do(c); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if body == nil {
|
||||||
|
return etcdErr.NewError(300, "Empty result from raft")
|
||||||
|
} else {
|
||||||
|
body, _ := body.([]byte)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(body)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
leader := r.Leader()
|
||||||
|
// current no leader
|
||||||
|
if leader == "" {
|
||||||
|
return etcdErr.NewError(300, "")
|
||||||
|
}
|
||||||
|
url, _ := toURL(leader)
|
||||||
|
|
||||||
|
redirect(url, w, req)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return etcdErr.NewError(300, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
|
||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
|
|
||||||
if etcd {
|
url := hostname + path
|
||||||
etcdAddr, _ := nameToEtcdURL(node)
|
|
||||||
url = etcdAddr + path
|
|
||||||
} else {
|
|
||||||
raftAddr, _ := nameToRaftURL(node)
|
|
||||||
url = raftAddr + path
|
|
||||||
}
|
|
||||||
|
|
||||||
debugf("Redirect to %s", url)
|
debugf("Redirect to %s", url)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user