commit
393ed439b1
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
"github.com/coreos/etcd/store"
|
"github.com/coreos/etcd/store"
|
||||||
"github.com/coreos/go-raft"
|
"github.com/coreos/go-raft"
|
||||||
"path"
|
"path"
|
||||||
|
@ -147,7 +148,8 @@ func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
|
||||||
// check machine number in the cluster
|
// check machine number in the cluster
|
||||||
num := machineNum()
|
num := machineNum()
|
||||||
if num == maxClusterSize {
|
if num == maxClusterSize {
|
||||||
return []byte("join fail"), fmt.Errorf(errors[103])
|
debug("Reject join request from ", c.Name)
|
||||||
|
return []byte("join fail"), etcdErr.NewError(103, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
addNameToURL(c.Name, c.RaftURL, c.EtcdURL)
|
addNameToURL(c.Name, c.RaftURL, c.EtcdURL)
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package main
|
package error
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errors map[int]string
|
var errors map[int]string
|
||||||
|
|
||||||
|
const ()
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
errors = make(map[int]string)
|
errors = make(map[int]string)
|
||||||
|
|
||||||
|
@ -33,17 +36,39 @@ func init() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonError struct {
|
type Error struct {
|
||||||
ErrorCode int `json:"errorCode"`
|
ErrorCode int `json:"errorCode"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Cause string `json:"cause,omitempty"`
|
Cause string `json:"cause,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newJsonError(errorCode int, cause string) []byte {
|
func NewError(errorCode int, cause string) Error {
|
||||||
b, _ := json.Marshal(jsonError{
|
return Error{
|
||||||
ErrorCode: errorCode,
|
ErrorCode: errorCode,
|
||||||
Message: errors[errorCode],
|
Message: errors[errorCode],
|
||||||
Cause: cause,
|
Cause: cause,
|
||||||
})
|
}
|
||||||
return b
|
}
|
||||||
|
|
||||||
|
func Message(code int) string {
|
||||||
|
return errors[code]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only for error interface
|
||||||
|
func (e Error) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) toJsonString() string {
|
||||||
|
b, _ := json.Marshal(e)
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Error) Write(w http.ResponseWriter) {
|
||||||
|
// 3xx is reft internal error
|
||||||
|
if e.ErrorCode/100 == 3 {
|
||||||
|
http.Error(w, e.toJsonString(), http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
http.Error(w, e.toJsonString(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
}
|
}
|
187
etcd_handlers.go
187
etcd_handlers.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
"github.com/coreos/etcd/store"
|
"github.com/coreos/etcd/store"
|
||||||
"github.com/coreos/go-raft"
|
"github.com/coreos/go-raft"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -16,29 +17,42 @@ import (
|
||||||
func NewEtcdMuxer() *http.ServeMux {
|
func NewEtcdMuxer() *http.ServeMux {
|
||||||
// external commands
|
// external commands
|
||||||
etcdMux := http.NewServeMux()
|
etcdMux := http.NewServeMux()
|
||||||
etcdMux.HandleFunc("/"+version+"/keys/", Multiplexer)
|
etcdMux.Handle("/"+version+"/keys/", errorHandler(Multiplexer))
|
||||||
etcdMux.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
|
etcdMux.Handle("/"+version+"/watch/", errorHandler(WatchHttpHandler))
|
||||||
etcdMux.HandleFunc("/leader", LeaderHttpHandler)
|
etcdMux.Handle("/"+version+"/leader", errorHandler(LeaderHttpHandler))
|
||||||
etcdMux.HandleFunc("/machines", MachinesHttpHandler)
|
etcdMux.Handle("/"+version+"/machines", errorHandler(MachinesHttpHandler))
|
||||||
etcdMux.HandleFunc("/version", VersionHttpHandler)
|
etcdMux.Handle("/"+version+"/stats", errorHandler(StatsHttpHandler))
|
||||||
etcdMux.HandleFunc("/stats", StatsHttpHandler)
|
etcdMux.Handle("/version", errorHandler(VersionHttpHandler))
|
||||||
etcdMux.HandleFunc("/test/", TestHttpHandler)
|
etcdMux.HandleFunc("/test/", TestHttpHandler)
|
||||||
return etcdMux
|
return etcdMux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errorHandler func(http.ResponseWriter, *http.Request) error
|
||||||
|
|
||||||
|
func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if e := fn(w, r); e != nil {
|
||||||
|
if etcdErr, ok := e.(etcdErr.Error); ok {
|
||||||
|
debug("Return error: ", etcdErr.Error())
|
||||||
|
etcdErr.Write(w)
|
||||||
|
} else {
|
||||||
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Multiplex GET/POST/DELETE request to corresponding handlers
|
// Multiplex GET/POST/DELETE request to corresponding handlers
|
||||||
func Multiplexer(w http.ResponseWriter, req *http.Request) {
|
func Multiplexer(w http.ResponseWriter, req *http.Request) error {
|
||||||
|
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
GetHttpHandler(&w, req)
|
return GetHttpHandler(w, req)
|
||||||
case "POST":
|
case "POST":
|
||||||
SetHttpHandler(&w, req)
|
return SetHttpHandler(w, req)
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
DeleteHttpHandler(&w, req)
|
return DeleteHttpHandler(w, req)
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,15 +62,11 @@ func Multiplexer(w http.ResponseWriter, req *http.Request) {
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
|
|
||||||
// Set Command Handler
|
// Set Command Handler
|
||||||
func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
func SetHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
key := req.URL.Path[len("/v1/keys/"):]
|
key := req.URL.Path[len("/v1/keys/"):]
|
||||||
|
|
||||||
if store.CheckKeyword(key) {
|
if store.CheckKeyword(key) {
|
||||||
|
return etcdErr.NewError(400, "Set")
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
|
||||||
|
|
||||||
(*w).Write(newJsonError(400, "Set"))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debugf("[recv] POST %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
debugf("[recv] POST %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||||
|
@ -64,10 +74,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
value := req.FormValue("value")
|
value := req.FormValue("value")
|
||||||
|
|
||||||
if len(value) == 0 {
|
if len(value) == 0 {
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
return etcdErr.NewError(200, "Set")
|
||||||
|
|
||||||
(*w).Write(newJsonError(200, "Set"))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prevValue := req.FormValue("prevValue")
|
prevValue := req.FormValue("prevValue")
|
||||||
|
@ -77,11 +84,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
expireTime, err := durationToExpireTime(strDuration)
|
expireTime, err := durationToExpireTime(strDuration)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return etcdErr.NewError(202, "Set")
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
|
||||||
|
|
||||||
(*w).Write(newJsonError(202, "Set"))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(prevValue) != 0 {
|
if len(prevValue) != 0 {
|
||||||
|
@ -92,7 +95,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(command, w, req, true)
|
return dispatch(command, w, req, true)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
command := &SetCommand{
|
command := &SetCommand{
|
||||||
|
@ -101,13 +104,12 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(command, w, req, true)
|
return dispatch(command, w, req, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete Handler
|
// Delete Handler
|
||||||
func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
key := req.URL.Path[len("/v1/keys/"):]
|
key := req.URL.Path[len("/v1/keys/"):]
|
||||||
|
|
||||||
debugf("[recv] DELETE %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
debugf("[recv] DELETE %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||||
|
@ -116,76 +118,40 @@ func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
Key: key,
|
Key: key,
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(command, w, req, true)
|
return dispatch(command, w, req, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch the command to leader
|
// Dispatch the command to leader
|
||||||
func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) {
|
func dispatch(c Command, w http.ResponseWriter, req *http.Request, etcd bool) error {
|
||||||
|
|
||||||
if r.State() == raft.Leader {
|
if r.State() == raft.Leader {
|
||||||
if body, err := r.Do(c); err != nil {
|
if body, err := r.Do(c); err != nil {
|
||||||
|
return err
|
||||||
if _, ok := err.(store.NotFoundError); ok {
|
|
||||||
(*w).WriteHeader(http.StatusNotFound)
|
|
||||||
(*w).Write(newJsonError(100, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := err.(store.TestFail); ok {
|
|
||||||
(*w).WriteHeader(http.StatusBadRequest)
|
|
||||||
(*w).Write(newJsonError(101, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
(*w).WriteHeader(http.StatusInternalServerError)
|
|
||||||
(*w).Write(newJsonError(300, err.Error()))
|
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if body == nil {
|
if body == nil {
|
||||||
(*w).WriteHeader(http.StatusNotFound)
|
return etcdErr.NewError(300, "Empty result from raft")
|
||||||
(*w).Write(newJsonError(300, "Empty result from raft"))
|
|
||||||
} else {
|
} else {
|
||||||
body, ok := body.([]byte)
|
body, _ := body.([]byte)
|
||||||
// this should not happen
|
w.WriteHeader(http.StatusOK)
|
||||||
if !ok {
|
w.Write(body)
|
||||||
panic("wrong type")
|
return nil
|
||||||
}
|
|
||||||
(*w).WriteHeader(http.StatusOK)
|
|
||||||
(*w).Write(body)
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
leader := r.Leader()
|
leader := r.Leader()
|
||||||
// current no leader
|
// current no leader
|
||||||
if leader == "" {
|
if leader == "" {
|
||||||
(*w).WriteHeader(http.StatusInternalServerError)
|
return etcdErr.NewError(300, "")
|
||||||
(*w).Write(newJsonError(300, ""))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tell the client where is the leader
|
// tell the client where is the leader
|
||||||
|
|
||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
|
|
||||||
var url string
|
var url string
|
||||||
|
|
||||||
if etcd {
|
if etcd {
|
||||||
etcdAddr, _ := nameToEtcdURL(leader)
|
etcdAddr, _ := nameToEtcdURL(leader)
|
||||||
if etcdAddr == "" {
|
|
||||||
panic(leader)
|
|
||||||
}
|
|
||||||
url = etcdAddr + path
|
url = etcdAddr + path
|
||||||
} else {
|
} else {
|
||||||
raftAddr, _ := nameToRaftURL(leader)
|
raftAddr, _ := nameToRaftURL(leader)
|
||||||
|
@ -194,12 +160,10 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) {
|
||||||
|
|
||||||
debugf("Redirect to %s", url)
|
debugf("Redirect to %s", url)
|
||||||
|
|
||||||
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
|
http.Redirect(w, req, url, http.StatusTemporaryRedirect)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
(*w).WriteHeader(http.StatusInternalServerError)
|
return etcdErr.NewError(300, "")
|
||||||
(*w).Write(newJsonError(300, ""))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
|
@ -210,44 +174,44 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) {
|
||||||
//--------------------------------------
|
//--------------------------------------
|
||||||
|
|
||||||
// Handler to return the current leader's raft address
|
// Handler to return the current leader's raft address
|
||||||
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
leader := r.Leader()
|
leader := r.Leader()
|
||||||
|
|
||||||
if leader != "" {
|
if leader != "" {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
raftURL, _ := nameToRaftURL(leader)
|
raftURL, _ := nameToRaftURL(leader)
|
||||||
w.Write([]byte(raftURL))
|
w.Write([]byte(raftURL))
|
||||||
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
return etcdErr.NewError(301, "")
|
||||||
// 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
|
// Handler to return all the known machines in the current cluster
|
||||||
func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
machines := getMachines()
|
machines := getMachines()
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(strings.Join(machines, ", ")))
|
w.Write([]byte(strings.Join(machines, ", ")))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler to return the current version of etcd
|
// Handler to return the current version of etcd
|
||||||
func VersionHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func VersionHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(fmt.Sprintf("etcd %s", releaseVersion)))
|
fmt.Fprintf(w, "etcd %s", releaseVersion)
|
||||||
w.Write([]byte(fmt.Sprintf("etcd API %s", version)))
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handler to return the basic stats of etcd
|
// Handler to return the basic stats of etcd
|
||||||
func StatsHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func StatsHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(etcdStore.Stats())
|
w.Write(etcdStore.Stats())
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Handler
|
// Get Handler
|
||||||
func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
func GetHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
key := req.URL.Path[len("/v1/keys/"):]
|
key := req.URL.Path[len("/v1/keys/"):]
|
||||||
|
|
||||||
debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
|
||||||
|
@ -257,31 +221,19 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if body, err := command.Apply(r.Server); err != nil {
|
if body, err := command.Apply(r.Server); err != nil {
|
||||||
|
return err
|
||||||
if _, ok := err.(store.NotFoundError); ok {
|
|
||||||
(*w).WriteHeader(http.StatusNotFound)
|
|
||||||
(*w).Write(newJsonError(100, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
(*w).WriteHeader(http.StatusInternalServerError)
|
|
||||||
(*w).Write(newJsonError(300, ""))
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
body, ok := body.([]byte)
|
body, _ := body.([]byte)
|
||||||
if !ok {
|
w.WriteHeader(http.StatusOK)
|
||||||
panic("wrong type")
|
w.Write(body)
|
||||||
}
|
|
||||||
|
|
||||||
(*w).WriteHeader(http.StatusOK)
|
|
||||||
(*w).Write(body)
|
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch handler
|
// Watch handler
|
||||||
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
key := req.URL.Path[len("/v1/watch/"):]
|
key := req.URL.Path[len("/v1/watch/"):]
|
||||||
|
|
||||||
command := &WatchCommand{
|
command := &WatchCommand{
|
||||||
|
@ -300,28 +252,23 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
|
sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
return etcdErr.NewError(203, "Watch From Index")
|
||||||
w.Write(newJsonError(203, "Watch From Index"))
|
|
||||||
}
|
}
|
||||||
command.SinceIndex = sinceIndex
|
command.SinceIndex = sinceIndex
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if body, err := command.Apply(r.Server); err != nil {
|
if body, err := command.Apply(r.Server); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
return etcdErr.NewError(500, key)
|
||||||
w.Write(newJsonError(500, key))
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
body, ok := body.([]byte)
|
body, _ := body.([]byte)
|
||||||
if !ok {
|
|
||||||
panic("wrong type")
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(body)
|
w.Write(body)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,16 +94,16 @@ func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response to the join request
|
// Response to the join request
|
||||||
func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
|
func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error {
|
||||||
|
|
||||||
command := &JoinCommand{}
|
command := &JoinCommand{}
|
||||||
|
|
||||||
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)
|
||||||
dispatch(command, &w, req, false)
|
return dispatch(command, w, req, false)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
107
raft_server.go
107
raft_server.go
|
@ -5,11 +5,11 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
|
"github.com/coreos/go-raft"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/go-raft"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type raftServer struct {
|
type raftServer struct {
|
||||||
|
@ -67,55 +67,10 @@ func (r *raftServer) ListenAndServe() {
|
||||||
|
|
||||||
// start as a leader in a new cluster
|
// start as a leader in a new cluster
|
||||||
if len(cluster) == 0 {
|
if len(cluster) == 0 {
|
||||||
|
startAsLeader()
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 20)
|
|
||||||
|
|
||||||
// leader need to join self as a peer
|
|
||||||
for {
|
|
||||||
_, err := r.Do(newJoinCommand())
|
|
||||||
if err == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
debugf("%s start as a leader", r.name)
|
|
||||||
|
|
||||||
// start as a follower in a existing cluster
|
|
||||||
} else {
|
} else {
|
||||||
|
startAsFollower()
|
||||||
time.Sleep(time.Millisecond * 20)
|
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
for i := 0; i < retryTimes; i++ {
|
|
||||||
|
|
||||||
success := false
|
|
||||||
for _, machine := range cluster {
|
|
||||||
if len(machine) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = joinCluster(r.Server, machine, r.tlsConf.Scheme)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() == errors[103] {
|
|
||||||
fatal(err)
|
|
||||||
}
|
|
||||||
debugf("cannot join to cluster via machine %s %s", machine, err)
|
|
||||||
} else {
|
|
||||||
success = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if success {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
warnf("cannot join to cluster via given machines, retry in %d seconds", RetryInterval)
|
|
||||||
time.Sleep(time.Second * RetryInterval)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
fatalf("Cannot join the cluster via given machines after %x retries", retryTimes)
|
|
||||||
}
|
|
||||||
debugf("%s success join to the cluster", r.name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -133,6 +88,47 @@ func (r *raftServer) ListenAndServe() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startAsLeader() {
|
||||||
|
// leader need to join self as a peer
|
||||||
|
for {
|
||||||
|
_, err := r.Do(newJoinCommand())
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debugf("%s start as a leader", r.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func startAsFollower() {
|
||||||
|
// start as a follower in a existing cluster
|
||||||
|
for i := 0; i < retryTimes; i++ {
|
||||||
|
|
||||||
|
for _, machine := range cluster {
|
||||||
|
|
||||||
|
if len(machine) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := joinCluster(r.Server, machine, r.tlsConf.Scheme)
|
||||||
|
if err == nil {
|
||||||
|
debugf("%s success join to the cluster via machine %s", r.name, machine)
|
||||||
|
return
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if _, ok := err.(etcdErr.Error); ok {
|
||||||
|
fatal(err)
|
||||||
|
}
|
||||||
|
debugf("cannot join to cluster via machine %s %s", machine, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
warnf("cannot join to cluster via given machines, retry in %d seconds", RetryInterval)
|
||||||
|
time.Sleep(time.Second * RetryInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
fatalf("Cannot join the cluster via given machines after %x retries", retryTimes)
|
||||||
|
}
|
||||||
|
|
||||||
// Start to listen and response raft command
|
// Start to listen and response raft command
|
||||||
func (r *raftServer) startTransport(scheme string, tlsConf tls.Config) {
|
func (r *raftServer) startTransport(scheme string, tlsConf tls.Config) {
|
||||||
u, _ := url.Parse(r.url)
|
u, _ := url.Parse(r.url)
|
||||||
|
@ -148,7 +144,7 @@ func (r *raftServer) startTransport(scheme string, tlsConf tls.Config) {
|
||||||
|
|
||||||
// internal commands
|
// internal commands
|
||||||
raftMux.HandleFunc("/name", NameHttpHandler)
|
raftMux.HandleFunc("/name", NameHttpHandler)
|
||||||
raftMux.HandleFunc("/join", JoinHttpHandler)
|
raftMux.Handle("/join", errorHandler(JoinHttpHandler))
|
||||||
raftMux.HandleFunc("/vote", VoteHttpHandler)
|
raftMux.HandleFunc("/vote", VoteHttpHandler)
|
||||||
raftMux.HandleFunc("/log", GetLogHttpHandler)
|
raftMux.HandleFunc("/log", GetLogHttpHandler)
|
||||||
raftMux.HandleFunc("/log/append", AppendEntriesHttpHandler)
|
raftMux.HandleFunc("/log/append", AppendEntriesHttpHandler)
|
||||||
|
@ -171,11 +167,7 @@ func joinCluster(s *raft.Server, raftURL string, scheme string) error {
|
||||||
json.NewEncoder(&b).Encode(newJoinCommand())
|
json.NewEncoder(&b).Encode(newJoinCommand())
|
||||||
|
|
||||||
// t must be ok
|
// t must be ok
|
||||||
t, ok := r.Transporter().(transporter)
|
t, _ := r.Transporter().(transporter)
|
||||||
|
|
||||||
if !ok {
|
|
||||||
panic("wrong type")
|
|
||||||
}
|
|
||||||
|
|
||||||
joinURL := url.URL{Host: raftURL, Scheme: scheme, Path: "/join"}
|
joinURL := url.URL{Host: raftURL, Scheme: scheme, Path: "/join"}
|
||||||
|
|
||||||
|
@ -203,7 +195,10 @@ func joinCluster(s *raft.Server, raftURL string, scheme string) error {
|
||||||
|
|
||||||
} else if resp.StatusCode == http.StatusBadRequest {
|
} else if resp.StatusCode == http.StatusBadRequest {
|
||||||
debug("Reach max number machines in the cluster")
|
debug("Reach max number machines in the cluster")
|
||||||
return fmt.Errorf(errors[103])
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
err := &etcdErr.Error{}
|
||||||
|
decoder.Decode(err)
|
||||||
|
return *err
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Unable to join")
|
return fmt.Errorf("Unable to join")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
package store
|
|
||||||
|
|
||||||
type NotFoundError string
|
|
||||||
|
|
||||||
func (e NotFoundError) Error() string {
|
|
||||||
return string(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
type NotFile string
|
|
||||||
|
|
||||||
func (e NotFile) Error() string {
|
|
||||||
return string(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
type TestFail string
|
|
||||||
|
|
||||||
func (e TestFail) Error() string {
|
|
||||||
return string(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Keyword string
|
|
||||||
|
|
||||||
func (e Keyword) Error() string {
|
|
||||||
return string(e)
|
|
||||||
}
|
|
|
@ -3,6 +3,7 @@ package store
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
etcdErr "github.com/coreos/etcd/error"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -239,8 +240,7 @@ func (s *Store) internalSet(key string, value string, expireTime time.Time, inde
|
||||||
ok := s.Tree.set(key, Node{value, expireTime, update})
|
ok := s.Tree.set(key, Node{value, expireTime, update})
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
err := NotFile(key)
|
return nil, etcdErr.NewError(102, "set: "+key)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if isExpire {
|
if isExpire {
|
||||||
|
@ -393,8 +393,7 @@ func (s *Store) RawGet(key string) ([]*Response, error) {
|
||||||
return resps, nil
|
return resps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := NotFoundError(key)
|
return nil, etcdErr.NewError(100, "get: "+key)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Delete(key string, index uint64) ([]byte, error) {
|
func (s *Store) Delete(key string, index uint64) ([]byte, error) {
|
||||||
|
@ -451,8 +450,7 @@ func (s *Store) internalDelete(key string, index uint64) ([]byte, error) {
|
||||||
return msg, err
|
return msg, err
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err := NotFoundError(key)
|
return nil, etcdErr.NewError(100, "delete: "+key)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,8 +465,7 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
|
||||||
resp := s.internalGet(key)
|
resp := s.internalGet(key)
|
||||||
|
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
err := NotFoundError(key)
|
return nil, etcdErr.NewError(100, "testandset: "+key)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Value == prevValue {
|
if resp.Value == prevValue {
|
||||||
|
@ -478,8 +475,8 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// If fails, return err
|
// If fails, return err
|
||||||
err := TestFail(fmt.Sprintf("TestAndSet: %s!=%s", resp.Value, prevValue))
|
return nil, etcdErr.NewError(101, fmt.Sprintf("TestAndSet: %s!=%s",
|
||||||
return nil, err
|
resp.Value, prevValue))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,7 +176,7 @@ func Monitor(size int, allowDeadNum int, leaderChan chan string, all chan bool,
|
||||||
|
|
||||||
func getLeader(addr string) (string, error) {
|
func getLeader(addr string) (string, error) {
|
||||||
|
|
||||||
resp, err := client.Get(addr + "/leader")
|
resp, err := client.Get(addr + "/v1/leader")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
@ -116,7 +116,7 @@ func (c *Client) SyncCluster() bool {
|
||||||
// sync cluster information by providing machine list
|
// sync cluster information by providing machine list
|
||||||
func (c *Client) internalSyncCluster(machines []string) bool {
|
func (c *Client) internalSyncCluster(machines []string) bool {
|
||||||
for _, machine := range machines {
|
for _, machine := range machines {
|
||||||
httpPath := c.createHttpPath(machine, "machines")
|
httpPath := c.createHttpPath(machine, "v1/machines")
|
||||||
resp, err := c.httpClient.Get(httpPath)
|
resp, err := c.httpClient.Get(httpPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// try another machine in the cluster
|
// try another machine in the cluster
|
||||||
|
|
|
@ -17,7 +17,6 @@ func main() {
|
||||||
c := etcd.NewClient()
|
c := etcd.NewClient()
|
||||||
c.Set("lock", "unlock", 0)
|
c.Set("lock", "unlock", 0)
|
||||||
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
go t(i, ch, etcd.NewClient())
|
go t(i, ch, etcd.NewClient())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/coreos/go-etcd/etcd"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/coreos/go-etcd/etcd"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,21 +11,21 @@ var count = 0
|
||||||
func main() {
|
func main() {
|
||||||
ch := make(chan bool, 10)
|
ch := make(chan bool, 10)
|
||||||
// set up a lock
|
// set up a lock
|
||||||
for i:=0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
go t(i, ch, etcd.NewClient())
|
go t(i, ch, etcd.NewClient())
|
||||||
}
|
}
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
for i:=0; i< 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
<-ch
|
<-ch
|
||||||
}
|
}
|
||||||
fmt.Println(time.Now().Sub(start), ": ", 100 * 50, "commands")
|
fmt.Println(time.Now().Sub(start), ": ", 100*50, "commands")
|
||||||
}
|
}
|
||||||
|
|
||||||
func t(num int, ch chan bool, c *etcd.Client) {
|
func t(num int, ch chan bool, c *etcd.Client) {
|
||||||
c.SyncCluster()
|
c.SyncCluster()
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
str := fmt.Sprintf("foo_%d",num * i)
|
str := fmt.Sprintf("foo_%d", num*i)
|
||||||
c.Set(str, "10", 0)
|
c.Set(str, "10", 0)
|
||||||
}
|
}
|
||||||
ch<-true
|
ch <- true
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,12 +29,12 @@ func Start(raftServer *raft.Server, webURL string) {
|
||||||
webMux := http.NewServeMux()
|
webMux := http.NewServeMux()
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Handler: webMux,
|
Handler: webMux,
|
||||||
Addr: u.Host,
|
Addr: u.Host,
|
||||||
}
|
}
|
||||||
|
|
||||||
mainPage = &MainPage{
|
mainPage = &MainPage{
|
||||||
Leader: raftServer.Leader(),
|
Leader: raftServer.Leader(),
|
||||||
Address: u.Host,
|
Address: u.Host,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue