http.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "net/http"
  19. "path"
  20. pioutil "github.com/coreos/etcd/pkg/ioutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. "github.com/coreos/etcd/version"
  24. "golang.org/x/net/context"
  25. )
  26. const (
  27. ConnReadLimitByte = 64 * 1024
  28. )
  29. var (
  30. RaftPrefix = "/raft"
  31. ProbingPrefix = path.Join(RaftPrefix, "probing")
  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. plog.Errorf("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. plog.Errorf("request received was ignored (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. plog.Errorf("failed to read raft message (%v)", 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. plog.Errorf("failed to unmarshal raft message (%v)", 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. plog.Warningf("failed to process 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. plog.Errorf("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. plog.Errorf("streaming request ignored (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. plog.Debugf("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. plog.Errorf("failed to parse from %s into ID (%v)", fromStr, err)
  151. http.Error(w, "invalid from", http.StatusNotFound)
  152. return
  153. }
  154. if h.r.IsIDRemoved(uint64(from)) {
  155. plog.Warningf("rejected 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. // This may happen in following cases:
  162. // 1. user starts a remote peer that belongs to a different cluster
  163. // with the same cluster ID.
  164. // 2. local etcd falls behind of the cluster, and cannot recognize
  165. // the members that joined after its current progress.
  166. plog.Errorf("failed to find member %s in cluster %s", from, wcid)
  167. http.Error(w, "error sender not found", http.StatusNotFound)
  168. return
  169. }
  170. wto := h.id.String()
  171. if gto := r.Header.Get("X-Raft-To"); gto != wto {
  172. plog.Errorf("streaming request ignored (ID mismatch got %s want %s)", gto, wto)
  173. http.Error(w, "to field mismatch", http.StatusPreconditionFailed)
  174. return
  175. }
  176. w.WriteHeader(http.StatusOK)
  177. w.(http.Flusher).Flush()
  178. c := newCloseNotifier()
  179. conn := &outgoingConn{
  180. t: t,
  181. termStr: r.Header.Get("X-Raft-Term"),
  182. Writer: w,
  183. Flusher: w.(http.Flusher),
  184. Closer: c,
  185. }
  186. p.attachOutgoingConn(conn)
  187. <-c.closeNotify()
  188. }
  189. type closeNotifier struct {
  190. done chan struct{}
  191. }
  192. func newCloseNotifier() *closeNotifier {
  193. return &closeNotifier{
  194. done: make(chan struct{}),
  195. }
  196. }
  197. func (n *closeNotifier) Close() error {
  198. close(n.done)
  199. return nil
  200. }
  201. func (n *closeNotifier) closeNotify() <-chan struct{} { return n.done }