http.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "io/ioutil"
  17. "log"
  18. "net/http"
  19. "path"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. pioutil "github.com/coreos/etcd/pkg/ioutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/version"
  25. )
  26. const (
  27. ConnReadLimitByte = 64 * 1024
  28. )
  29. var (
  30. RaftPrefix = "/raft"
  31. RaftStreamPrefix = path.Join(RaftPrefix, "stream")
  32. )
  33. func NewHandler(r Raft, cid types.ID) http.Handler {
  34. return &handler{
  35. r: r,
  36. cid: cid,
  37. }
  38. }
  39. type peerGetter interface {
  40. Get(id types.ID) Peer
  41. }
  42. func newStreamHandler(peerGetter peerGetter, r Raft, id, cid types.ID) http.Handler {
  43. return &streamHandler{
  44. peerGetter: peerGetter,
  45. r: r,
  46. id: id,
  47. cid: cid,
  48. }
  49. }
  50. type writerToResponse interface {
  51. WriteTo(w http.ResponseWriter)
  52. }
  53. type handler struct {
  54. r Raft
  55. cid types.ID
  56. }
  57. func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  58. if r.Method != "POST" {
  59. w.Header().Set("Allow", "POST")
  60. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  61. return
  62. }
  63. wcid := h.cid.String()
  64. w.Header().Set("X-Etcd-Cluster-ID", wcid)
  65. gcid := r.Header.Get("X-Etcd-Cluster-ID")
  66. if gcid != wcid {
  67. log.Printf("rafthttp: request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  68. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  69. return
  70. }
  71. // Limit the data size that could be read from the request body, which ensures that read from
  72. // connection will not time out accidentally due to possible block in underlying implementation.
  73. limitedr := pioutil.NewLimitedBufferReader(r.Body, ConnReadLimitByte)
  74. b, err := ioutil.ReadAll(limitedr)
  75. if err != nil {
  76. log.Println("rafthttp: error reading raft message:", err)
  77. http.Error(w, "error reading raft message", http.StatusBadRequest)
  78. return
  79. }
  80. var m raftpb.Message
  81. if err := m.Unmarshal(b); err != nil {
  82. log.Println("rafthttp: error unmarshaling raft message:", err)
  83. http.Error(w, "error unmarshaling raft message", http.StatusBadRequest)
  84. return
  85. }
  86. if err := h.r.Process(context.TODO(), m); err != nil {
  87. switch v := err.(type) {
  88. case writerToResponse:
  89. v.WriteTo(w)
  90. default:
  91. log.Printf("rafthttp: error processing raft message: %v", err)
  92. http.Error(w, "error processing raft message", http.StatusInternalServerError)
  93. }
  94. return
  95. }
  96. // Write StatusNoContet header after the message has been processed by
  97. // raft, which faciliates the client to report MsgSnap status.
  98. w.WriteHeader(http.StatusNoContent)
  99. }
  100. type streamHandler struct {
  101. peerGetter peerGetter
  102. r Raft
  103. id types.ID
  104. cid types.ID
  105. }
  106. func (h *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  107. if r.Method != "GET" {
  108. w.Header().Set("Allow", "GET")
  109. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  110. return
  111. }
  112. w.Header().Add("X-Server-Version", version.Version)
  113. var t streamType
  114. switch path.Dir(r.URL.Path) {
  115. // backward compatibility
  116. case RaftStreamPrefix:
  117. t = streamTypeMsgApp
  118. case path.Join(RaftStreamPrefix, string(streamTypeMsgApp)):
  119. t = streamTypeMsgAppV2
  120. case path.Join(RaftStreamPrefix, string(streamTypeMessage)):
  121. t = streamTypeMessage
  122. default:
  123. log.Printf("rafthttp: ignored unexpected streaming request path %s", r.URL.Path)
  124. http.Error(w, "invalid path", http.StatusNotFound)
  125. return
  126. }
  127. fromStr := path.Base(r.URL.Path)
  128. from, err := types.IDFromString(fromStr)
  129. if err != nil {
  130. log.Printf("rafthttp: failed to parse from %s into ID", fromStr)
  131. http.Error(w, "invalid from", http.StatusNotFound)
  132. return
  133. }
  134. if h.r.IsIDRemoved(uint64(from)) {
  135. log.Printf("rafthttp: reject the stream from peer %s since it was removed", from)
  136. http.Error(w, "removed member", http.StatusGone)
  137. return
  138. }
  139. p := h.peerGetter.Get(from)
  140. if p == nil {
  141. log.Printf("rafthttp: fail to find sender %s", from)
  142. http.Error(w, "error sender not found", http.StatusNotFound)
  143. return
  144. }
  145. wcid := h.cid.String()
  146. if gcid := r.Header.Get("X-Etcd-Cluster-ID"); gcid != wcid {
  147. log.Printf("rafthttp: streaming request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  148. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  149. return
  150. }
  151. wto := h.id.String()
  152. if gto := r.Header.Get("X-Raft-To"); gto != wto {
  153. log.Printf("rafthttp: streaming request ignored due to ID mismatch got %s want %s", gto, wto)
  154. http.Error(w, "to field mismatch", http.StatusPreconditionFailed)
  155. return
  156. }
  157. w.WriteHeader(http.StatusOK)
  158. w.(http.Flusher).Flush()
  159. c := newCloseNotifier()
  160. conn := &outgoingConn{
  161. t: t,
  162. termStr: r.Header.Get("X-Raft-Term"),
  163. Writer: w,
  164. Flusher: w.(http.Flusher),
  165. Closer: c,
  166. }
  167. p.attachOutgoingConn(conn)
  168. <-c.closeNotify()
  169. }
  170. type closeNotifier struct {
  171. done chan struct{}
  172. }
  173. func newCloseNotifier() *closeNotifier {
  174. return &closeNotifier{
  175. done: make(chan struct{}),
  176. }
  177. }
  178. func (n *closeNotifier) Close() error {
  179. close(n.done)
  180. return nil
  181. }
  182. func (n *closeNotifier) closeNotify() <-chan struct{} { return n.done }