commit
c9561cacf0
|
@ -166,15 +166,16 @@ curl http://127.0.0.1:4001/v1/keys/testAndSet -d value=one
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's try an invaild `TestAndSet` command.
|
Let's try an invaild `TestAndSet` command.
|
||||||
|
We can give another parameter prevValue to set command to make it a TestAndSet command.
|
||||||
|
|
||||||
```sh
|
```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.
|
This will try to test if the previous of the key is two, it is change it to three.
|
||||||
|
|
||||||
```html
|
```json
|
||||||
Test one==two fails
|
{"errorCode":101,"message":"The given PrevValue is not equal to the value of the key","cause":"TestAndSet: one!=two"}
|
||||||
```
|
```
|
||||||
|
|
||||||
which means `testAndSet` failed.
|
which means `testAndSet` failed.
|
||||||
|
@ -182,7 +183,7 @@ which means `testAndSet` failed.
|
||||||
Let us try a vaild one.
|
Let us try a vaild one.
|
||||||
|
|
||||||
```sh
|
```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
|
The response should be
|
||||||
|
|
|
@ -37,23 +37,20 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
|
debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
|
||||||
|
|
||||||
command := &SetCommand{}
|
value := req.FormValue("value")
|
||||||
command.Key = key
|
|
||||||
|
|
||||||
command.Value = req.FormValue("value")
|
if len(value) == 0 {
|
||||||
|
|
||||||
if len(command.Value) == 0 {
|
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
(*w).WriteHeader(http.StatusBadRequest)
|
||||||
|
|
||||||
(*w).Write(newJsonError(200, "Set"))
|
(*w).Write(newJsonError(200, "Set"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prevValue := req.FormValue("prevValue")
|
||||||
|
|
||||||
strDuration := req.FormValue("ttl")
|
strDuration := req.FormValue("ttl")
|
||||||
|
|
||||||
var err error
|
expireTime, err := durationToExpireTime(strDuration)
|
||||||
|
|
||||||
command.ExpireTime, err = durationToExpireTime(strDuration)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
|
@ -62,51 +59,22 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
(*w).Write(newJsonError(202, "Set"))
|
(*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)
|
||||||
|
|
||||||
}
|
} else {
|
||||||
|
command := &SetCommand{}
|
||||||
// TestAndSet handler
|
command.Key = key
|
||||||
func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
|
command.Value = value
|
||||||
key := req.URL.Path[len("/v1/testAndSet/"):]
|
command.ExpireTime = expireTime
|
||||||
|
dispatch(command, w, req, true)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Delete Handler
|
||||||
|
|
1
etcd.go
1
etcd.go
|
@ -372,7 +372,6 @@ func startClientTransport(port int, st int) {
|
||||||
// external commands
|
// external commands
|
||||||
http.HandleFunc("/"+version+"/keys/", Multiplexer)
|
http.HandleFunc("/"+version+"/keys/", Multiplexer)
|
||||||
http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
|
http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
|
||||||
http.HandleFunc("/"+version+"/testAndSet/", TestAndSetHttpHandler)
|
|
||||||
http.HandleFunc("/leader", LeaderHttpHandler)
|
http.HandleFunc("/leader", LeaderHttpHandler)
|
||||||
http.HandleFunc("/machines", MachinesHttpHandler)
|
http.HandleFunc("/machines", MachinesHttpHandler)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue