sender.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 etcdserver
  14. import (
  15. "bytes"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "net/url"
  20. "path"
  21. "sync"
  22. "time"
  23. "github.com/coreos/etcd/etcdserver/stats"
  24. "github.com/coreos/etcd/pkg/types"
  25. "github.com/coreos/etcd/raft/raftpb"
  26. )
  27. const (
  28. raftPrefix = "/raft"
  29. connPerSender = 4
  30. senderBufSize = connPerSender * 4
  31. )
  32. type sendHub struct {
  33. tr http.RoundTripper
  34. cl ClusterInfo
  35. ss *stats.ServerStats
  36. ls *stats.LeaderStats
  37. senders map[types.ID]*sender
  38. shouldstop chan struct{}
  39. }
  40. // newSendHub creates the default send hub used to transport raft messages
  41. // to other members. The returned sendHub will update the given ServerStats and
  42. // LeaderStats appropriately.
  43. func newSendHub(t http.RoundTripper, cl ClusterInfo, ss *stats.ServerStats, ls *stats.LeaderStats) *sendHub {
  44. h := &sendHub{
  45. tr: t,
  46. cl: cl,
  47. ss: ss,
  48. ls: ls,
  49. senders: make(map[types.ID]*sender),
  50. shouldstop: make(chan struct{}, 1),
  51. }
  52. for _, m := range cl.Members() {
  53. h.Add(m)
  54. }
  55. return h
  56. }
  57. func (h *sendHub) Send(msgs []raftpb.Message) {
  58. for _, m := range msgs {
  59. to := types.ID(m.To)
  60. s, ok := h.senders[to]
  61. if !ok {
  62. if !h.cl.IsIDRemoved(to) {
  63. log.Printf("etcdserver: send message to unknown receiver %s", to)
  64. }
  65. continue
  66. }
  67. // TODO: don't block. we should be able to have 1000s
  68. // of messages out at a time.
  69. data, err := m.Marshal()
  70. if err != nil {
  71. log.Println("sender: dropping message:", err)
  72. return // drop bad message
  73. }
  74. if m.Type == raftpb.MsgApp {
  75. h.ss.SendAppendReq(len(data))
  76. }
  77. // TODO (xiangli): reasonable retry logic
  78. s.send(data)
  79. }
  80. }
  81. func (h *sendHub) Stop() {
  82. for _, s := range h.senders {
  83. s.stop()
  84. }
  85. }
  86. func (h *sendHub) ShouldStopNotify() <-chan struct{} {
  87. return h.shouldstop
  88. }
  89. func (h *sendHub) Add(m *Member) {
  90. if _, ok := h.senders[m.ID]; ok {
  91. return
  92. }
  93. // TODO: considering how to switch between all available peer urls
  94. u := fmt.Sprintf("%s%s", m.PickPeerURL(), raftPrefix)
  95. fs := h.ls.Follower(m.ID.String())
  96. s := newSender(h.tr, u, h.cl.ID(), fs, h.shouldstop)
  97. h.senders[m.ID] = s
  98. }
  99. func (h *sendHub) Remove(id types.ID) {
  100. h.senders[id].stop()
  101. delete(h.senders, id)
  102. }
  103. func (h *sendHub) Update(m *Member) {
  104. // TODO: return error or just panic?
  105. if _, ok := h.senders[m.ID]; !ok {
  106. return
  107. }
  108. peerURL := m.PickPeerURL()
  109. u, err := url.Parse(peerURL)
  110. if err != nil {
  111. log.Panicf("unexpect peer url %s", peerURL)
  112. }
  113. u.Path = path.Join(u.Path, raftPrefix)
  114. s := h.senders[m.ID]
  115. s.mu.Lock()
  116. defer s.mu.Unlock()
  117. s.u = u.String()
  118. }
  119. type sender struct {
  120. tr http.RoundTripper
  121. u string
  122. cid types.ID
  123. fs *stats.FollowerStats
  124. q chan []byte
  125. mu sync.RWMutex
  126. wg sync.WaitGroup
  127. shouldstop chan struct{}
  128. }
  129. func newSender(tr http.RoundTripper, u string, cid types.ID, fs *stats.FollowerStats, shouldstop chan struct{}) *sender {
  130. s := &sender{
  131. tr: tr,
  132. u: u,
  133. cid: cid,
  134. fs: fs,
  135. q: make(chan []byte, senderBufSize),
  136. shouldstop: shouldstop,
  137. }
  138. s.wg.Add(connPerSender)
  139. for i := 0; i < connPerSender; i++ {
  140. go s.handle()
  141. }
  142. return s
  143. }
  144. func (s *sender) send(data []byte) error {
  145. select {
  146. case s.q <- data:
  147. return nil
  148. default:
  149. log.Printf("sender: reach the maximal serving to %s", s.u)
  150. return fmt.Errorf("reach maximal serving")
  151. }
  152. }
  153. func (s *sender) stop() {
  154. close(s.q)
  155. s.wg.Wait()
  156. }
  157. func (s *sender) handle() {
  158. defer s.wg.Done()
  159. for d := range s.q {
  160. start := time.Now()
  161. err := s.post(d)
  162. end := time.Now()
  163. if err != nil {
  164. s.fs.Fail()
  165. log.Printf("sender: %v", err)
  166. continue
  167. }
  168. s.fs.Succ(end.Sub(start))
  169. }
  170. }
  171. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  172. // error on any failure.
  173. func (s *sender) post(data []byte) error {
  174. s.mu.RLock()
  175. req, err := http.NewRequest("POST", s.u, bytes.NewBuffer(data))
  176. s.mu.RUnlock()
  177. if err != nil {
  178. return fmt.Errorf("new request to %s error: %v", s.u, err)
  179. }
  180. req.Header.Set("Content-Type", "application/protobuf")
  181. req.Header.Set("X-Etcd-Cluster-ID", s.cid.String())
  182. resp, err := s.tr.RoundTrip(req)
  183. if err != nil {
  184. return fmt.Errorf("error posting to %q: %v", req.URL.String(), err)
  185. }
  186. resp.Body.Close()
  187. switch resp.StatusCode {
  188. case http.StatusPreconditionFailed:
  189. select {
  190. case s.shouldstop <- struct{}{}:
  191. default:
  192. }
  193. log.Printf("etcdserver: conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), s.cid)
  194. return nil
  195. case http.StatusForbidden:
  196. select {
  197. case s.shouldstop <- struct{}{}:
  198. default:
  199. }
  200. log.Println("etcdserver: this member has been permanently removed from the cluster")
  201. log.Println("etcdserver: the data-dir used by this member must be removed so that this host can be re-added with a new member ID")
  202. return nil
  203. case http.StatusNoContent:
  204. return nil
  205. default:
  206. return fmt.Errorf("unhandled status %s", http.StatusText(resp.StatusCode))
  207. }
  208. }