peer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/raft/raftpb"
  23. )
  24. const (
  25. raftPrefix = "/raft"
  26. peerMembersPrefix = "/members"
  27. )
  28. // NewPeerHandler generates an http.Handler to handle etcd peer (raft) requests.
  29. func NewPeerHandler(server *etcdserver.EtcdServer) http.Handler {
  30. rh := &raftHandler{
  31. stats: server,
  32. server: server,
  33. clusterInfo: server.Cluster,
  34. }
  35. mh := &peerMembersHandler{
  36. clusterInfo: server.Cluster,
  37. }
  38. mux := http.NewServeMux()
  39. mux.HandleFunc("/", http.NotFound)
  40. mux.Handle(raftPrefix, rh)
  41. mux.Handle(peerMembersPrefix, mh)
  42. return mux
  43. }
  44. type raftHandler struct {
  45. stats etcdserver.Stats
  46. server etcdserver.Server
  47. clusterInfo etcdserver.ClusterInfo
  48. }
  49. func (h *raftHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  50. if !allowMethod(w, r.Method, "POST") {
  51. return
  52. }
  53. wcid := strconv.FormatUint(h.clusterInfo.ID(), 16)
  54. w.Header().Set("X-Etcd-Cluster-ID", wcid)
  55. gcid := r.Header.Get("X-Etcd-Cluster-ID")
  56. if gcid != wcid {
  57. log.Printf("etcdhttp: request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  58. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  59. return
  60. }
  61. b, err := ioutil.ReadAll(r.Body)
  62. if err != nil {
  63. log.Println("etcdhttp: error reading raft message:", err)
  64. http.Error(w, "error reading raft message", http.StatusBadRequest)
  65. return
  66. }
  67. var m raftpb.Message
  68. if err := m.Unmarshal(b); err != nil {
  69. log.Println("etcdhttp: error unmarshaling raft message:", err)
  70. http.Error(w, "error unmarshaling raft message", http.StatusBadRequest)
  71. return
  72. }
  73. if err := h.server.Process(context.TODO(), m); err != nil {
  74. log.Println("etcdhttp: error processing raft message:", err)
  75. switch err {
  76. case etcdserver.ErrRemoved:
  77. http.Error(w, "cannot process message from removed node", http.StatusForbidden)
  78. default:
  79. writeError(w, err)
  80. }
  81. return
  82. }
  83. if m.Type == raftpb.MsgApp {
  84. h.stats.UpdateRecvApp(m.From, r.ContentLength)
  85. }
  86. w.WriteHeader(http.StatusNoContent)
  87. }
  88. type peerMembersHandler struct {
  89. clusterInfo etcdserver.ClusterInfo
  90. }
  91. func (h *peerMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  92. if !allowMethod(w, r.Method, "GET") {
  93. return
  94. }
  95. cid := strconv.FormatUint(h.clusterInfo.ID(), 16)
  96. w.Header().Set("X-Etcd-Cluster-ID", cid)
  97. if r.URL.Path != peerMembersPrefix {
  98. http.Error(w, "bad path", http.StatusBadRequest)
  99. return
  100. }
  101. ms := h.clusterInfo.Members()
  102. w.Header().Set("Content-Type", "application/json")
  103. if err := json.NewEncoder(w).Encode(ms); err != nil {
  104. log.Printf("etcdhttp: %v", err)
  105. }
  106. }