http.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. "fmt"
  18. "io/ioutil"
  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. ProbingPrefix = path.Join(RaftPrefix, "probing")
  33. RaftStreamPrefix = path.Join(RaftPrefix, "stream")
  34. RaftSnapshotPrefix = path.Join(RaftPrefix, "snapshot")
  35. errIncompatibleVersion = errors.New("incompatible version")
  36. errClusterIDMismatch = errors.New("cluster ID mismatch")
  37. )
  38. func NewHandler(r Raft, cid types.ID) http.Handler {
  39. return &handler{
  40. r: r,
  41. cid: cid,
  42. }
  43. }
  44. func newSnapshotHandler(r Raft, snapSaver SnapshotSaver, cid types.ID) http.Handler {
  45. return &snapshotHandler{
  46. r: r,
  47. snapSaver: snapSaver,
  48. cid: cid,
  49. }
  50. }
  51. type peerGetter interface {
  52. Get(id types.ID) Peer
  53. }
  54. func newStreamHandler(peerGetter peerGetter, r Raft, id, cid types.ID) http.Handler {
  55. return &streamHandler{
  56. peerGetter: peerGetter,
  57. r: r,
  58. id: id,
  59. cid: cid,
  60. }
  61. }
  62. type writerToResponse interface {
  63. WriteTo(w http.ResponseWriter)
  64. }
  65. type handler struct {
  66. r Raft
  67. cid types.ID
  68. }
  69. func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  70. if r.Method != "POST" {
  71. w.Header().Set("Allow", "POST")
  72. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  73. return
  74. }
  75. w.Header().Set("X-Etcd-Cluster-ID", h.cid.String())
  76. if err := checkClusterCompatibilityFromHeader(r.Header, h.cid); err != nil {
  77. http.Error(w, err.Error(), http.StatusPreconditionFailed)
  78. return
  79. }
  80. // Limit the data size that could be read from the request body, which ensures that read from
  81. // connection will not time out accidentally due to possible block in underlying implementation.
  82. limitedr := pioutil.NewLimitedBufferReader(r.Body, ConnReadLimitByte)
  83. b, err := ioutil.ReadAll(limitedr)
  84. if err != nil {
  85. plog.Errorf("failed to read raft message (%v)", err)
  86. http.Error(w, "error reading raft message", http.StatusBadRequest)
  87. return
  88. }
  89. var m raftpb.Message
  90. if err := m.Unmarshal(b); err != nil {
  91. plog.Errorf("failed to unmarshal raft message (%v)", err)
  92. http.Error(w, "error unmarshaling raft message", http.StatusBadRequest)
  93. return
  94. }
  95. if err := h.r.Process(context.TODO(), m); err != nil {
  96. switch v := err.(type) {
  97. case writerToResponse:
  98. v.WriteTo(w)
  99. default:
  100. plog.Warningf("failed to process raft message (%v)", err)
  101. http.Error(w, "error processing raft message", http.StatusInternalServerError)
  102. }
  103. return
  104. }
  105. // Write StatusNoContet header after the message has been processed by
  106. // raft, which facilitates the client to report MsgSnap status.
  107. w.WriteHeader(http.StatusNoContent)
  108. }
  109. type snapshotHandler struct {
  110. r Raft
  111. snapSaver SnapshotSaver
  112. cid types.ID
  113. }
  114. // ServeHTTP serves HTTP request to receive and process snapshot message.
  115. //
  116. // If request sender dies without closing underlying TCP connection,
  117. // the handler will keep waiting for the request body until TCP keepalive
  118. // finds out that the connection is broken after several minutes.
  119. // This is acceptable because
  120. // 1. snapshot messages sent through other TCP connections could still be
  121. // received and processed.
  122. // 2. this case should happen rarely, so no further optimization is done.
  123. func (h *snapshotHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  124. if r.Method != "POST" {
  125. w.Header().Set("Allow", "POST")
  126. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  127. return
  128. }
  129. w.Header().Set("X-Etcd-Cluster-ID", h.cid.String())
  130. if err := checkClusterCompatibilityFromHeader(r.Header, h.cid); err != nil {
  131. http.Error(w, err.Error(), http.StatusPreconditionFailed)
  132. return
  133. }
  134. dec := &messageDecoder{r: r.Body}
  135. m, err := dec.decode()
  136. if err != nil {
  137. msg := fmt.Sprintf("failed to decode raft message (%v)", err)
  138. plog.Errorf(msg)
  139. http.Error(w, msg, http.StatusBadRequest)
  140. return
  141. }
  142. if m.Type != raftpb.MsgSnap {
  143. plog.Errorf("unexpected raft message type %s on snapshot path", m.Type)
  144. http.Error(w, "wrong raft message type", http.StatusBadRequest)
  145. return
  146. }
  147. // save snapshot
  148. if err := h.snapSaver.SaveFrom(r.Body, m.Snapshot.Metadata.Index); err != nil {
  149. msg := fmt.Sprintf("failed to save KV snapshot (%v)", err)
  150. plog.Error(msg)
  151. http.Error(w, msg, http.StatusInternalServerError)
  152. return
  153. }
  154. plog.Infof("received and saved snapshot [index: %d, from: %s] successfully", m.Snapshot.Metadata.Index, types.ID(m.From))
  155. if err := h.r.Process(context.TODO(), m); err != nil {
  156. switch v := err.(type) {
  157. // Process may return writerToResponse error when doing some
  158. // additional checks before calling raft.Node.Step.
  159. case writerToResponse:
  160. v.WriteTo(w)
  161. default:
  162. msg := fmt.Sprintf("failed to process raft message (%v)", err)
  163. plog.Warningf(msg)
  164. http.Error(w, msg, http.StatusInternalServerError)
  165. }
  166. return
  167. }
  168. // Write StatusNoContet header after the message has been processed by
  169. // raft, which facilitates the client to report MsgSnap status.
  170. w.WriteHeader(http.StatusNoContent)
  171. }
  172. type streamHandler struct {
  173. peerGetter peerGetter
  174. r Raft
  175. id types.ID
  176. cid types.ID
  177. }
  178. func (h *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  179. if r.Method != "GET" {
  180. w.Header().Set("Allow", "GET")
  181. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  182. return
  183. }
  184. w.Header().Set("X-Server-Version", version.Version)
  185. w.Header().Set("X-Etcd-Cluster-ID", h.cid.String())
  186. if err := checkClusterCompatibilityFromHeader(r.Header, h.cid); err != nil {
  187. http.Error(w, err.Error(), http.StatusPreconditionFailed)
  188. return
  189. }
  190. var t streamType
  191. switch path.Dir(r.URL.Path) {
  192. // backward compatibility
  193. case RaftStreamPrefix:
  194. t = streamTypeMsgApp
  195. case path.Join(RaftStreamPrefix, string(streamTypeMsgApp)):
  196. t = streamTypeMsgAppV2
  197. case path.Join(RaftStreamPrefix, string(streamTypeMessage)):
  198. t = streamTypeMessage
  199. default:
  200. plog.Debugf("ignored unexpected streaming request path %s", r.URL.Path)
  201. http.Error(w, "invalid path", http.StatusNotFound)
  202. return
  203. }
  204. fromStr := path.Base(r.URL.Path)
  205. from, err := types.IDFromString(fromStr)
  206. if err != nil {
  207. plog.Errorf("failed to parse from %s into ID (%v)", fromStr, err)
  208. http.Error(w, "invalid from", http.StatusNotFound)
  209. return
  210. }
  211. if h.r.IsIDRemoved(uint64(from)) {
  212. plog.Warningf("rejected the stream from peer %s since it was removed", from)
  213. http.Error(w, "removed member", http.StatusGone)
  214. return
  215. }
  216. p := h.peerGetter.Get(from)
  217. if p == nil {
  218. // This may happen in following cases:
  219. // 1. user starts a remote peer that belongs to a different cluster
  220. // with the same cluster ID.
  221. // 2. local etcd falls behind of the cluster, and cannot recognize
  222. // the members that joined after its current progress.
  223. plog.Errorf("failed to find member %s in cluster %s", from, h.cid)
  224. http.Error(w, "error sender not found", http.StatusNotFound)
  225. return
  226. }
  227. wto := h.id.String()
  228. if gto := r.Header.Get("X-Raft-To"); gto != wto {
  229. plog.Errorf("streaming request ignored (ID mismatch got %s want %s)", gto, wto)
  230. http.Error(w, "to field mismatch", http.StatusPreconditionFailed)
  231. return
  232. }
  233. w.WriteHeader(http.StatusOK)
  234. w.(http.Flusher).Flush()
  235. c := newCloseNotifier()
  236. conn := &outgoingConn{
  237. t: t,
  238. termStr: r.Header.Get("X-Raft-Term"),
  239. Writer: w,
  240. Flusher: w.(http.Flusher),
  241. Closer: c,
  242. }
  243. p.attachOutgoingConn(conn)
  244. <-c.closeNotify()
  245. }
  246. // checkClusterCompatibilityFromHeader checks the cluster compatibility of
  247. // the local member from the given header.
  248. // It checks whether the version of local member is compatible with
  249. // the versions in the header, and whether the cluster ID of local member
  250. // matches the one in the header.
  251. func checkClusterCompatibilityFromHeader(header http.Header, cid types.ID) error {
  252. if err := checkVersionCompability(header.Get("X-Server-From"), serverVersion(header), minClusterVersion(header)); err != nil {
  253. plog.Errorf("request version incompatibility (%v)", err)
  254. return errIncompatibleVersion
  255. }
  256. if gcid := header.Get("X-Etcd-Cluster-ID"); gcid != cid.String() {
  257. plog.Errorf("request cluster ID mismatch (got %s want %s)", gcid, cid)
  258. return errClusterIDMismatch
  259. }
  260. return nil
  261. }
  262. type closeNotifier struct {
  263. done chan struct{}
  264. }
  265. func newCloseNotifier() *closeNotifier {
  266. return &closeNotifier{
  267. done: make(chan struct{}),
  268. }
  269. }
  270. func (n *closeNotifier) Close() error {
  271. close(n.done)
  272. return nil
  273. }
  274. func (n *closeNotifier) closeNotify() <-chan struct{} { return n.done }