http.go 5.4 KB

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