sender.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // TODO: considering how to switch between all available peer urls
  82. u := fmt.Sprintf("%s%s", m.PickPeerURL(), raftPrefix)
  83. c := &http.Client{Transport: h.tr}
  84. fs := h.ls.Follower(m.ID.String())
  85. s := newSender(u, h.cl.ID(), c, fs)
  86. h.senders[m.ID] = s
  87. }
  88. func (h *sendHub) Remove(id types.ID) {
  89. h.senders[id].stop()
  90. delete(h.senders, id)
  91. }
  92. type sender struct {
  93. u string
  94. cid types.ID
  95. c *http.Client
  96. fs *stats.FollowerStats
  97. q chan []byte
  98. }
  99. func newSender(u string, cid types.ID, c *http.Client, fs *stats.FollowerStats) *sender {
  100. s := &sender{
  101. u: u,
  102. cid: cid,
  103. c: c,
  104. fs: fs,
  105. q: make(chan []byte),
  106. }
  107. for i := 0; i < connPerSender; i++ {
  108. go s.handle()
  109. }
  110. return s
  111. }
  112. func (s *sender) send(data []byte) {
  113. select {
  114. case s.q <- data:
  115. default:
  116. log.Printf("sender: reach the maximal serving to %s", s.u)
  117. }
  118. }
  119. func (s *sender) stop() {
  120. close(s.q)
  121. }
  122. func (s *sender) handle() {
  123. for d := range s.q {
  124. start := time.Now()
  125. err := s.post(d)
  126. end := time.Now()
  127. if err != nil {
  128. s.fs.Fail()
  129. log.Printf("sender: %v", err)
  130. continue
  131. }
  132. s.fs.Succ(end.Sub(start))
  133. }
  134. }
  135. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  136. // error on any failure.
  137. func (s *sender) post(data []byte) error {
  138. req, err := http.NewRequest("POST", s.u, bytes.NewBuffer(data))
  139. if err != nil {
  140. return fmt.Errorf("new request to %s error: %v", s.u, err)
  141. }
  142. req.Header.Set("Content-Type", "application/protobuf")
  143. req.Header.Set("X-Etcd-Cluster-ID", s.cid.String())
  144. resp, err := s.c.Do(req)
  145. if err != nil {
  146. return fmt.Errorf("do request %+v error: %v", req, err)
  147. }
  148. resp.Body.Close()
  149. switch resp.StatusCode {
  150. case http.StatusPreconditionFailed:
  151. // TODO: shutdown the etcdserver gracefully?
  152. log.Fatalf("etcd: conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), s.cid)
  153. return nil
  154. case http.StatusForbidden:
  155. // TODO: stop the server
  156. log.Println("etcd: this member has been permanently removed from the cluster")
  157. 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")
  158. return nil
  159. case http.StatusNoContent:
  160. return nil
  161. default:
  162. return fmt.Errorf("unhandled status %s", http.StatusText(resp.StatusCode))
  163. }
  164. }