From 8309c235d97c6a14b3ac2458918afb36b67e89f3 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 14 Jul 2013 16:19:30 -0700 Subject: [PATCH 1/2] combine set and testandset command --- client_handlers.go | 68 ++++++++++++---------------------------------- etcd.go | 1 - 2 files changed, 18 insertions(+), 51 deletions(-) diff --git a/client_handlers.go b/client_handlers.go index 6418307ea..4b630f085 100644 --- a/client_handlers.go +++ b/client_handlers.go @@ -37,23 +37,20 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) { debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key) - command := &SetCommand{} - command.Key = key + value := req.FormValue("value") - command.Value = req.FormValue("value") - - if len(command.Value) == 0 { + if len(value) == 0 { (*w).WriteHeader(http.StatusBadRequest) (*w).Write(newJsonError(200, "Set")) return } + prevValue := req.FormValue("prevValue") + strDuration := req.FormValue("ttl") - var err error - - command.ExpireTime, err = durationToExpireTime(strDuration) + expireTime, err := durationToExpireTime(strDuration) if err != nil { @@ -62,51 +59,22 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) { (*w).Write(newJsonError(202, "Set")) } - dispatch(command, w, req, true) + if len(prevValue) != 0 { + command := &TestAndSetCommand{} + command.Key = key + command.Value = value + command.PrevValue = prevValue + command.ExpireTime = expireTime + dispatch(command, w, req, true) -} - -// TestAndSet handler -func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) { - key := req.URL.Path[len("/v1/testAndSet/"):] - - debug("[recv] POST http://%v/v1/testAndSet/%s", raftServer.Name(), key) - - command := &TestAndSetCommand{} - command.Key = key - - command.PrevValue = req.FormValue("prevValue") - command.Value = req.FormValue("value") - - if len(command.Value) == 0 { - w.WriteHeader(http.StatusBadRequest) - - w.Write(newJsonError(200, "TestAndSet")) - - return + } else { + command := &SetCommand{} + command.Key = key + command.Value = value + command.ExpireTime = expireTime + dispatch(command, w, req, true) } - if len(command.PrevValue) == 0 { - w.WriteHeader(http.StatusBadRequest) - - w.Write(newJsonError(201, "TestAndSet")) - return - } - - strDuration := req.FormValue("ttl") - - var err error - - command.ExpireTime, err = durationToExpireTime(strDuration) - - if err != nil { - w.WriteHeader(http.StatusBadRequest) - - w.Write(newJsonError(202, "TestAndSet")) - } - - dispatch(command, &w, req, true) - } // Delete Handler diff --git a/etcd.go b/etcd.go index 3e3ae4a51..6d0c97973 100644 --- a/etcd.go +++ b/etcd.go @@ -372,7 +372,6 @@ func startClientTransport(port int, st int) { // external commands http.HandleFunc("/"+version+"/keys/", Multiplexer) http.HandleFunc("/"+version+"/watch/", WatchHttpHandler) - http.HandleFunc("/"+version+"/testAndSet/", TestAndSetHttpHandler) http.HandleFunc("/leader", LeaderHttpHandler) http.HandleFunc("/machines", MachinesHttpHandler) From 0999cc1115c1b15541cf0450bd39666eee4369a0 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 14 Jul 2013 16:22:07 -0700 Subject: [PATCH 2/2] Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d888ffecd..e45bf45ef 100644 --- a/README.md +++ b/README.md @@ -166,15 +166,16 @@ curl http://127.0.0.1:4001/v1/keys/testAndSet -d value=one ``` Let's try an invaild `TestAndSet` command. +We can give another parameter prevValue to set command to make it a TestAndSet command. ```sh -curl http://127.0.0.1:4001/v1/testAndSet/testAndSet -d prevValue=two -d value=three +curl http://127.0.0.1:4001/v1/keys/testAndSet -d prevValue=two -d value=three ``` This will try to test if the previous of the key is two, it is change it to three. -```html -Test one==two fails +```json +{"errorCode":101,"message":"The given PrevValue is not equal to the value of the key","cause":"TestAndSet: one!=two"} ``` which means `testAndSet` failed. @@ -182,7 +183,7 @@ which means `testAndSet` failed. Let us try a vaild one. ```sh -curl http://127.0.0.1:4001/v1/testAndSet/testAndSet -d prevValue=one -d value=two +curl http://127.0.0.1:4001/v1/keys/testAndSet -d prevValue=one -d value=two ``` The response should be