peer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdhttp
  14. import (
  15. "encoding/json"
  16. "io/ioutil"
  17. "log"
  18. "net/http"
  19. "strconv"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  21. "github.com/coreos/etcd/etcdserver"
  22. "github.com/coreos/etcd/pkg/strutil"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. )
  25. const (
  26. raftPrefix = "/raft"
  27. peerMembersPrefix = "/members"
  28. )
  29. // NewPeerHandler generates an http.Handler to handle etcd peer (raft) requests.
  30. func NewPeerHandler(server *etcdserver.EtcdServer) http.Handler {
  31. rh := &raftHandler{
  32. stats: server,
  33. server: server,
  34. clusterInfo: server.Cluster,
  35. }
  36. mh := &peerMembersHandler{
  37. clusterInfo: server.Cluster,
  38. }
  39. mux := http.NewServeMux()
  40. mux.HandleFunc("/", http.NotFound)
  41. mux.Handle(raftPrefix, rh)
  42. mux.Handle(peerMembersPrefix, mh)
  43. return mux
  44. }
  45. type raftHandler struct {
  46. stats etcdserver.Stats
  47. server etcdserver.Server
  48. clusterInfo etcdserver.ClusterInfo
  49. }
  50. func (h *raftHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  51. if !allowMethod(w, r.Method, "POST") {
  52. return
  53. }
  54. wcid := strconv.FormatUint(h.clusterInfo.ID(), 16)
  55. w.Header().Set("X-Etcd-Cluster-ID", wcid)
  56. gcid := r.Header.Get("X-Etcd-Cluster-ID")
  57. if gcid != wcid {
  58. log.Printf("etcdhttp: request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  59. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  60. return
  61. }
  62. b, err := ioutil.ReadAll(r.Body)
  63. if err != nil {
  64. log.Println("etcdhttp: error reading raft message:", err)
  65. http.Error(w, "error reading raft message", http.StatusBadRequest)
  66. return
  67. }
  68. var m raftpb.Message
  69. if err := m.Unmarshal(b); err != nil {
  70. log.Println("etcdhttp: error unmarshaling raft message:", err)
  71. http.Error(w, "error unmarshaling raft message", http.StatusBadRequest)
  72. return
  73. }
  74. if err := h.server.Process(context.TODO(), m); err != nil {
  75. switch err {
  76. case etcdserver.ErrRemoved:
  77. log.Printf("etcdhttp: reject message from removed member %s", strutil.IDAsHex(m.From))
  78. http.Error(w, "cannot process message from removed member", http.StatusForbidden)
  79. default:
  80. writeError(w, err)
  81. }
  82. return
  83. }
  84. if m.Type == raftpb.MsgApp {
  85. h.stats.UpdateRecvApp(m.From, r.ContentLength)
  86. }
  87. w.WriteHeader(http.StatusNoContent)
  88. }
  89. type peerMembersHandler struct {
  90. clusterInfo etcdserver.ClusterInfo
  91. }
  92. func (h *peerMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  93. if !allowMethod(w, r.Method, "GET") {
  94. return
  95. }
  96. cid := strconv.FormatUint(h.clusterInfo.ID(), 16)
  97. w.Header().Set("X-Etcd-Cluster-ID", cid)
  98. if r.URL.Path != peerMembersPrefix {
  99. http.Error(w, "bad path", http.StatusBadRequest)
  100. return
  101. }
  102. ms := h.clusterInfo.Members()
  103. w.Header().Set("Content-Type", "application/json")
  104. if err := json.NewEncoder(w).Encode(ms); err != nil {
  105. log.Printf("etcdhttp: %v", err)
  106. }
  107. }