etcd/etcd_handlers.go

356 lines
7.8 KiB
Go
Raw Normal View History

package main
import (
2013-08-07 10:28:59 +04:00
"fmt"
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
//-------------------------------------------------------------------
2013-08-10 10:03:49 +04:00
// Handlers to handle etcd-store related request via etcd url
//-------------------------------------------------------------------
// 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-08-07 03:41:00 +04:00
switch req.Method {
case "GET":
2013-07-01 22:16:30 +04:00
GetHttpHandler(&w, req)
2013-08-07 03:41:00 +04:00
case "POST":
2013-07-01 22:16:30 +04:00
SetHttpHandler(&w, req)
2013-08-07 03:41:00 +04:00
case "DELETE":
2013-07-01 22:16:30 +04:00
DeleteHttpHandler(&w, req)
2013-08-07 03:41:00 +04:00
default:
2013-07-01 22:16:30 +04:00
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
2013-07-16 03:04:41 +04:00
// Set/Delete 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-31 21:10:00 +04:00
if store.CheckKeyword(key) {
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(400, "Set"))
return
}
debugf("[recv] POST %v/v1/keys/%s", raftServer.Name(), key)
2013-07-15 03:19:30 +04:00
value := req.FormValue("value")
2013-07-15 03:19:30 +04:00
if len(value) == 0 {
2013-07-12 19:41:28 +04:00
(*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-15 03:19:30 +04:00
prevValue := req.FormValue("prevValue")
2013-07-01 22:16:30 +04:00
2013-07-15 03:19:30 +04:00
strDuration := req.FormValue("ttl")
2013-07-15 03:19:30 +04:00
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-31 21:10:00 +04:00
return
}
2013-07-15 03:19:30 +04:00
if len(prevValue) != 0 {
2013-08-07 03:47:57 +04:00
command := &TestAndSetCommand{
2013-08-07 10:28:59 +04:00
Key: key,
Value: value,
PrevValue: prevValue,
2013-08-07 03:47:57 +04:00
ExpireTime: expireTime,
}
2013-07-15 03:19:30 +04:00
dispatch(command, w, req, true)
2013-07-08 22:00:10 +04:00
2013-07-15 03:19:30 +04:00
} else {
2013-08-07 03:47:57 +04:00
command := &SetCommand{
2013-08-07 10:28:59 +04:00
Key: key,
Value: value,
2013-08-07 03:47:57 +04:00
ExpireTime: expireTime,
}
2013-07-15 03:19:30 +04:00
dispatch(command, w, req, true)
}
2013-07-10 04:31:24 +04:00
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
debugf("[recv] DELETE %v/v1/keys/%s", raftServer.Name(), key)
2013-06-09 22:12:32 +04:00
2013-08-07 03:47:57 +04:00
command := &DeleteCommand{
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-08-11 04:00:25 +04:00
func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd 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 {
2013-08-01 07:35:14 +04:00
(*w).WriteHeader(http.StatusNotFound)
(*w).Write(newJsonError(100, err.Error()))
2013-07-12 19:41:28 +04:00
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
}
if err.Error() == errors[103] {
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(103, ""))
return
}
2013-07-12 19:41:28 +04:00
(*w).WriteHeader(http.StatusInternalServerError)
(*w).Write(newJsonError(300, err.Error()))
2013-07-12 19:41:28 +04:00
return
} else {
2013-07-12 19:41:28 +04:00
if body == nil {
2013-08-01 07:35:14 +04:00
(*w).WriteHeader(http.StatusNotFound)
2013-08-04 09:56:39 +04:00
(*w).Write(newJsonError(300, "Empty result from raft"))
2013-07-12 19:41:28 +04:00
} else {
2013-07-14 06:13:07 +04:00
body, ok := body.([]byte)
2013-07-21 03:29:27 +04:00
// this should not happen
2013-07-14 06:13:07 +04:00
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
2013-08-11 04:00:25 +04:00
if etcd {
etcdAddr, _ := nameToEtcdURL(raftServer.Leader())
url = etcdAddr + path
2013-07-11 07:00:05 +04:00
} else {
2013-08-11 04:00:25 +04:00
raftAddr, _ := nameToRaftURL(raftServer.Leader())
url = raftAddr + path
2013-07-11 07:00:05 +04:00
}
2013-06-28 22:29:11 +04:00
debugf("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?
//--------------------------------------
2013-08-10 08:06:16 +04:00
// Handler to return the current leader's raft address
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)
2013-08-10 08:06:16 +04:00
raftURL, _ := nameToRaftURL(leader)
w.Write([]byte(raftURL))
2013-07-14 06:13:07 +04:00
} 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
2013-08-10 10:03:49 +04:00
machines, _ := getEtcdURL(raftServer.Name())
2013-07-14 06:13:07 +04:00
2013-07-16 03:04:41 +04:00
// Add all peers to the list and separate by comma
2013-07-14 06:13:07 +04:00
// We do not use json here since we accept machines list
2013-07-16 03:04:41 +04:00
// in the command line separate by comma.
2013-07-14 06:13:07 +04:00
for peerName, _ := range peers {
2013-08-10 10:03:49 +04:00
if addr, ok := getEtcdURL(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))
}
2013-08-02 23:31:35 +04:00
// Handler to return the current version of etcd
func VersionHttpHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("etcd %s", releaseVersion)))
2013-08-02 23:31:35 +04:00
}
// Handler to return the basic stats of etcd
func StatsHttpHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(etcdStore.Stats())
}
// 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
debugf("[recv] GET http://%v/v1/keys/%s", raftServer.Name(), key)
2013-08-07 03:47:57 +04:00
command := &GetCommand{
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 {
2013-08-01 07:35:14 +04:00
(*w).WriteHeader(http.StatusNotFound)
(*w).Write(newJsonError(100, err.Error()))
2013-07-12 19:41:28 +04:00
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, ""))
2013-07-31 21:10:00 +04:00
} 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
}
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/"):]
2013-08-07 03:47:57 +04:00
command := &WatchCommand{
Key: key,
}
2013-06-13 22:01:06 +04:00
2013-07-01 03:30:41 +04:00
if req.Method == "GET" {
debugf("[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
debugf("[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 {
w.WriteHeader(http.StatusInternalServerError)
w.Write(newJsonError(500, key))
} else {
w.WriteHeader(http.StatusOK)
body, ok := body.([]byte)
if !ok {
panic("wrong type")
}
w.Write(body)
}
2013-06-13 22:01:06 +04:00
}
// TestHandler
func TestHttpHandler(w http.ResponseWriter, req *http.Request) {
testType := req.URL.Path[len("/test/"):]
if testType == "speed" {
directSet()
w.WriteHeader(http.StatusOK)
w.Write([]byte("speed test success"))
return
}
w.WriteHeader(http.StatusBadRequest)
}
// 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
}