http.go 9.6 KB

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