http.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rafthttp
  14. import (
  15. "io/ioutil"
  16. "log"
  17. "net/http"
  18. "path"
  19. "strconv"
  20. "strings"
  21. "github.com/coreos/etcd/pkg/ioutils"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. )
  26. const (
  27. ConnReadLimitByte = 64 * 1024
  28. )
  29. var (
  30. RaftPrefix = "/raft"
  31. RaftStreamPrefix = path.Join(RaftPrefix, "stream")
  32. )
  33. type Processor interface {
  34. Process(ctx context.Context, m raftpb.Message) error
  35. }
  36. type SenderFinder interface {
  37. // Sender returns the sender of the given id.
  38. Sender(id types.ID) Sender
  39. }
  40. func NewHandler(p Processor, cid types.ID) http.Handler {
  41. return &handler{
  42. p: p,
  43. cid: cid,
  44. }
  45. }
  46. // NewStreamHandler returns a handler which initiates streamer when receiving
  47. // stream request from follower.
  48. func NewStreamHandler(finder SenderFinder, id, cid types.ID) http.Handler {
  49. return &streamHandler{
  50. finder: finder,
  51. id: id,
  52. cid: cid,
  53. }
  54. }
  55. type handler struct {
  56. p Processor
  57. cid types.ID
  58. }
  59. func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  60. if r.Method != "POST" {
  61. w.Header().Set("Allow", "POST")
  62. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  63. return
  64. }
  65. wcid := h.cid.String()
  66. w.Header().Set("X-Etcd-Cluster-ID", wcid)
  67. gcid := r.Header.Get("X-Etcd-Cluster-ID")
  68. if gcid != wcid {
  69. log.Printf("rafthttp: request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  70. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  71. return
  72. }
  73. // Limit the data size that could be read from the request body, which ensures that read from
  74. // connection will not time out accidentally due to possible block in underlying implementation.
  75. limitedr := ioutils.NewLimitedBufferReader(r.Body, ConnReadLimitByte)
  76. b, err := ioutil.ReadAll(limitedr)
  77. if err != nil {
  78. log.Println("rafthttp: error reading raft message:", err)
  79. http.Error(w, "error reading raft message", http.StatusBadRequest)
  80. return
  81. }
  82. var m raftpb.Message
  83. if err := m.Unmarshal(b); err != nil {
  84. log.Println("rafthttp: error unmarshaling raft message:", err)
  85. http.Error(w, "error unmarshaling raft message", http.StatusBadRequest)
  86. return
  87. }
  88. if err := h.p.Process(context.TODO(), m); err != nil {
  89. switch v := err.(type) {
  90. case writerToResponse:
  91. v.WriteTo(w)
  92. default:
  93. log.Printf("rafthttp: error processing raft message: %v", err)
  94. http.Error(w, "error processing raft message", http.StatusInternalServerError)
  95. }
  96. return
  97. }
  98. w.WriteHeader(http.StatusNoContent)
  99. }
  100. type streamHandler struct {
  101. finder SenderFinder
  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. fromStr := strings.TrimPrefix(r.URL.Path, RaftStreamPrefix+"/")
  112. from, err := types.IDFromString(fromStr)
  113. if err != nil {
  114. log.Printf("rafthttp: path %s cannot be parsed", fromStr)
  115. http.Error(w, "invalid path", http.StatusNotFound)
  116. return
  117. }
  118. s := h.finder.Sender(from)
  119. if s == nil {
  120. log.Printf("rafthttp: fail to find sender %s", from)
  121. http.Error(w, "error sender not found", http.StatusNotFound)
  122. return
  123. }
  124. wcid := h.cid.String()
  125. if gcid := r.Header.Get("X-Etcd-Cluster-ID"); gcid != wcid {
  126. log.Printf("rafthttp: streaming request ignored due to cluster ID mismatch got %s want %s", gcid, wcid)
  127. http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed)
  128. return
  129. }
  130. wto := h.id.String()
  131. if gto := r.Header.Get("X-Raft-To"); gto != wto {
  132. log.Printf("rafthttp: streaming request ignored due to ID mismatch got %s want %s", gto, wto)
  133. http.Error(w, "to field mismatch", http.StatusPreconditionFailed)
  134. return
  135. }
  136. termStr := r.Header.Get("X-Raft-Term")
  137. term, err := strconv.ParseUint(termStr, 10, 64)
  138. if err != nil {
  139. log.Printf("rafthttp: streaming request ignored due to parse term %s error: %v", termStr, err)
  140. http.Error(w, "invalid term field", http.StatusBadRequest)
  141. return
  142. }
  143. w.WriteHeader(http.StatusOK)
  144. w.(http.Flusher).Flush()
  145. done, err := s.StartStreaming(w.(WriteFlusher), from, term)
  146. if err != nil {
  147. log.Printf("rafthttp: streaming request ignored due to start streaming error: %v", err)
  148. // TODO: consider http status and info here
  149. http.Error(w, "error enable streaming", http.StatusInternalServerError)
  150. return
  151. }
  152. <-done
  153. }
  154. type writerToResponse interface {
  155. WriteTo(w http.ResponseWriter)
  156. }