etcd/client_handlers.go

336 lines
7.3 KiB
Go
Raw Normal View History

package main
import (
2013-07-12 19:41:28 +04:00
"github.com/coreos/etcd/store"
"net/http"
"strconv"
2013-06-21 02:59:23 +04:00
"time"
)
2013-06-13 22:01:06 +04:00
//-------------------------------------------------------------------
// Handlers to handle etcd-store related request via raft client port
//-------------------------------------------------------------------
// Multiplex GET/POST/DELETE request to corresponding handlers
2013-07-01 22:16:30 +04:00
func Multiplexer(w http.ResponseWriter, req *http.Request) {
2013-06-13 22:01:06 +04:00
2013-07-01 22:16:30 +04:00
if req.Method == "GET" {
GetHttpHandler(&w, req)
} else if req.Method == "POST" {
SetHttpHandler(&w, req)
} else if req.Method == "DELETE" {
DeleteHttpHandler(&w, req)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
2013-06-21 02:59:23 +04:00
return
}
2013-07-01 22:16:30 +04:00
}
//--------------------------------------
2013-07-10 04:31:24 +04:00
// State sensitive handlers
// Set/Delte will dispatch to leader
//--------------------------------------
2013-07-10 01:55:45 +04:00
// Set Command Handler
2013-07-01 22:16:30 +04:00
func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
command := &SetCommand{}
command.Key = key
2013-07-01 22:16:30 +04:00
command.Value = req.FormValue("value")
2013-07-12 19:41:28 +04:00
if len(command.Value) == 0 {
(*w).WriteHeader(http.StatusBadRequest)
2013-07-12 21:42:07 +04:00
2013-07-13 00:38:45 +04:00
(*w).Write(newJsonError(200, "Set"))
2013-07-12 19:41:28 +04:00
return
}
2013-07-01 22:16:30 +04:00
strDuration := req.FormValue("ttl")
var err error
command.ExpireTime, err = durationToExpireTime(strDuration)
if err != nil {
2013-07-12 21:42:07 +04:00
(*w).WriteHeader(http.StatusBadRequest)
2013-07-13 00:38:45 +04:00
(*w).Write(newJsonError(202, "Set"))
}
2013-07-11 07:00:05 +04:00
dispatch(command, w, req, true)
}
// TestAndSet handler
2013-07-08 22:00:10 +04:00
func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/testAndSet/"):]
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/v1/testAndSet/%s", raftServer.Name(), key)
2013-07-08 22:00:10 +04:00
command := &TestAndSetCommand{}
command.Key = key
command.PrevValue = req.FormValue("prevValue")
command.Value = req.FormValue("value")
2013-07-12 19:41:28 +04:00
if len(command.Value) == 0 {
2013-07-12 21:42:07 +04:00
w.WriteHeader(http.StatusBadRequest)
2013-07-13 00:35:43 +04:00
w.Write(newJsonError(200, "TestAndSet"))
2013-07-12 21:42:07 +04:00
2013-07-12 19:41:28 +04:00
return
}
if len(command.PrevValue) == 0 {
2013-07-12 21:42:07 +04:00
w.WriteHeader(http.StatusBadRequest)
2013-07-13 00:35:43 +04:00
w.Write(newJsonError(201, "TestAndSet"))
2013-07-12 19:41:28 +04:00
return
}
2013-07-08 22:00:10 +04:00
strDuration := req.FormValue("ttl")
var err error
2013-07-08 22:00:10 +04:00
command.ExpireTime, err = durationToExpireTime(strDuration)
2013-07-08 22:00:10 +04:00
if err != nil {
2013-07-12 21:42:07 +04:00
w.WriteHeader(http.StatusBadRequest)
2013-07-13 00:35:43 +04:00
w.Write(newJsonError(202, "TestAndSet"))
}
2013-07-10 04:31:24 +04:00
2013-07-11 07:00:05 +04:00
dispatch(command, &w, req, true)
2013-07-08 22:00:10 +04:00
}
// Delete Handler
2013-07-01 22:16:30 +04:00
func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-06-09 22:12:32 +04:00
2013-07-10 01:55:45 +04:00
debug("[recv] DELETE http://%v/v1/keys/%s", raftServer.Name(), key)
2013-06-09 22:12:32 +04:00
command := &DeleteCommand{}
command.Key = key
2013-06-09 22:12:32 +04:00
2013-07-11 07:00:05 +04:00
dispatch(command, w, req, true)
2013-06-09 22:12:32 +04:00
}
// Dispatch the command to leader
2013-07-11 07:00:05 +04:00
func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool) {
2013-07-10 01:55:45 +04:00
if raftServer.State() == "leader" {
if body, err := raftServer.Do(c); err != nil {
2013-07-12 19:41:28 +04:00
if _, ok := err.(store.NotFoundError); ok {
http.NotFound((*w), req)
return
}
2013-07-12 19:41:28 +04:00
if _, ok := err.(store.TestFail); ok {
(*w).WriteHeader(http.StatusBadRequest)
2013-07-13 00:35:43 +04:00
(*w).Write(newJsonError(101, err.Error()))
return
}
2013-07-13 03:36:21 +04:00
if _, ok := err.(store.NotFile); ok {
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(102, err.Error()))
return
}
2013-07-12 19:41:28 +04:00
(*w).WriteHeader(http.StatusInternalServerError)
2013-07-13 00:38:45 +04:00
(*w).Write(newJsonError(300, "No Leader"))
2013-07-12 19:41:28 +04:00
return
} else {
2013-07-12 19:41:28 +04:00
if body == nil {
http.NotFound((*w), req)
} else {
2013-07-14 06:13:07 +04:00
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
2013-07-12 19:41:28 +04:00
(*w).WriteHeader(http.StatusOK)
(*w).Write(body)
}
return
2013-06-13 22:01:06 +04:00
}
} else {
// current no leader
2013-07-10 01:55:45 +04:00
if raftServer.Leader() == "" {
(*w).WriteHeader(http.StatusInternalServerError)
2013-07-13 00:35:43 +04:00
(*w).Write(newJsonError(300, ""))
return
}
// tell the client where is the leader
2013-06-28 22:26:17 +04:00
path := req.URL.Path
2013-06-28 22:29:11 +04:00
var scheme string
2013-06-28 22:29:11 +04:00
if scheme = req.URL.Scheme; scheme == "" {
scheme = "http://"
}
2013-06-28 22:29:11 +04:00
2013-07-11 07:00:05 +04:00
var url string
if client {
clientAddr, _ := getClientAddr(raftServer.Leader())
url = scheme + clientAddr + path
2013-07-11 07:00:05 +04:00
} else {
url = scheme + raftServer.Leader() + path
}
2013-06-28 22:29:11 +04:00
debug("Redirect to %s", url)
2013-07-06 21:21:39 +04:00
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
return
}
(*w).WriteHeader(http.StatusInternalServerError)
2013-07-13 00:35:43 +04:00
(*w).Write(newJsonError(300, ""))
return
2013-06-21 02:59:23 +04:00
}
//--------------------------------------
2013-07-10 04:31:24 +04:00
// State non-sensitive handlers
// will not dispatch to leader
// TODO: add sensitive version for these
// command?
//--------------------------------------
// Handler to return the current leader name
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-07-14 06:13:07 +04:00
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, _ := getClientAddr(raftServer.Name())
2013-07-14 06:13:07 +04:00
// 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 {
if addr, ok := getClientAddr(peerName); ok {
machines = machines + "," + addr
}
2013-07-14 06:13:07 +04:00
}
w.WriteHeader(http.StatusOK)
2013-07-14 06:13:07 +04:00
w.Write([]byte(machines))
}
// Get Handler
2013-07-01 22:16:30 +04:00
func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
key := req.URL.Path[len("/v1/keys/"):]
2013-06-20 08:03:28 +04:00
2013-07-10 01:55:45 +04:00
debug("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
command := &GetCommand{}
command.Key = key
2013-06-13 22:01:06 +04:00
2013-07-10 01:55:45 +04:00
if body, err := command.Apply(raftServer); err != nil {
2013-07-12 19:41:28 +04:00
if _, ok := err.(store.NotFoundError); ok {
http.NotFound((*w), req)
return
}
2013-07-01 22:16:30 +04:00
(*w).WriteHeader(http.StatusInternalServerError)
2013-07-13 00:35:43 +04:00
(*w).Write(newJsonError(300, ""))
return
} else {
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
2013-07-12 19:41:28 +04:00
(*w).WriteHeader(http.StatusOK)
2013-07-01 22:16:30 +04:00
(*w).Write(body)
2013-07-12 19:41:28 +04:00
return
}
2013-06-13 22:01:06 +04:00
}
2013-06-13 22:01:06 +04:00
// Watch handler
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
2013-07-01 22:16:30 +04:00
key := req.URL.Path[len("/v1/watch/"):]
command := &WatchCommand{}
command.Key = key
2013-06-13 22:01:06 +04:00
2013-07-01 03:30:41 +04:00
if req.Method == "GET" {
2013-07-10 01:55:45 +04:00
debug("[recv] GET http://%v/watch/%s", raftServer.Name(), key)
2013-07-01 03:30:41 +04:00
command.SinceIndex = 0
} else if req.Method == "POST" {
// watch from a specific index
2013-07-10 01:55:45 +04:00
debug("[recv] POST http://%v/watch/%s", raftServer.Name(), key)
2013-07-07 02:54:21 +04:00
content := req.FormValue("index")
2013-07-01 03:30:41 +04:00
sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
2013-07-13 00:35:43 +04:00
w.Write(newJsonError(203, "Watch From Index"))
2013-07-01 03:30:41 +04:00
}
command.SinceIndex = sinceIndex
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
2013-07-10 01:55:45 +04:00
if body, err := command.Apply(raftServer); err != nil {
warn("Unable to do watch command: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.WriteHeader(http.StatusOK)
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
w.Write(body)
return
}
2013-06-13 22:01:06 +04:00
}
// Convert string duration to time format
2013-07-10 04:31:24 +04:00
func durationToExpireTime(strDuration string) (time.Time, error) {
if strDuration != "" {
duration, err := strconv.Atoi(strDuration)
if err != nil {
2013-07-10 04:31:24 +04:00
return time.Unix(0, 0), err
}
return time.Now().Add(time.Second * (time.Duration)(duration)), nil
} else {
return time.Unix(0, 0), nil
}
2013-07-10 04:31:24 +04:00
}