http.go 6.3 KB

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