peer.go 3.2 KB

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