sender.go 4.4 KB

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