sender.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "bytes"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "sync"
  20. "time"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. )
  26. const (
  27. connPerSender = 4
  28. // senderBufSize is the size of sender buffer, which helps hold the
  29. // temporary network latency.
  30. // The size ensures that sender does not drop messages when the network
  31. // is out of work for less than 1 second in good path.
  32. senderBufSize = 64
  33. appRespBatchMs = 50
  34. ConnReadTimeout = 5 * time.Second
  35. ConnWriteTimeout = 5 * time.Second
  36. )
  37. type Sender interface {
  38. // StartStreaming enables streaming in the sender using the given writer,
  39. // which provides a fast and efficient way to send appendEntry messages.
  40. StartStreaming(w WriteFlusher, to types.ID, term uint64) (done <-chan struct{}, err error)
  41. Update(u string)
  42. // Send sends the data to the remote node. It is always non-blocking.
  43. // It may be fail to send data if it returns nil error.
  44. Send(m raftpb.Message) error
  45. // Stop performs any necessary finalization and terminates the Sender
  46. // elegantly.
  47. Stop()
  48. // Pause pauses the sender. The sender will simply drops all incoming
  49. // messages without retruning an error.
  50. Pause()
  51. // Resume resumes a paused sender.
  52. Resume()
  53. }
  54. func NewSender(tr http.RoundTripper, u string, cid types.ID, p Processor, fs *stats.FollowerStats, shouldstop chan struct{}) *sender {
  55. s := &sender{
  56. tr: tr,
  57. u: u,
  58. cid: cid,
  59. p: p,
  60. fs: fs,
  61. shouldstop: shouldstop,
  62. batcher: NewBatcher(100, appRespBatchMs*time.Millisecond),
  63. q: make(chan []byte, senderBufSize),
  64. }
  65. s.wg.Add(connPerSender)
  66. for i := 0; i < connPerSender; i++ {
  67. go s.handle()
  68. }
  69. return s
  70. }
  71. type sender struct {
  72. tr http.RoundTripper
  73. u string
  74. cid types.ID
  75. p Processor
  76. fs *stats.FollowerStats
  77. shouldstop chan struct{}
  78. strmCln *streamClient
  79. batcher *Batcher
  80. strmSrv *streamServer
  81. strmSrvMu sync.Mutex
  82. q chan []byte
  83. paused bool
  84. mu sync.RWMutex
  85. wg sync.WaitGroup
  86. }
  87. func (s *sender) StartStreaming(w WriteFlusher, to types.ID, term uint64) (<-chan struct{}, error) {
  88. s.strmSrvMu.Lock()
  89. defer s.strmSrvMu.Unlock()
  90. if s.strmSrv != nil {
  91. // ignore lower-term streaming request
  92. if term < s.strmSrv.term {
  93. return nil, fmt.Errorf("out of data streaming request: term %d, request term %d", term, s.strmSrv.term)
  94. }
  95. // stop the existing one
  96. s.strmSrv.stop()
  97. }
  98. s.strmSrv = startStreamServer(w, to, term, s.fs)
  99. return s.strmSrv.stopNotify(), nil
  100. }
  101. func (s *sender) Update(u string) {
  102. s.mu.Lock()
  103. defer s.mu.Unlock()
  104. s.u = u
  105. }
  106. // TODO (xiangli): reasonable retry logic
  107. func (s *sender) Send(m raftpb.Message) error {
  108. s.mu.RLock()
  109. pause := s.paused
  110. s.mu.RUnlock()
  111. if pause {
  112. return nil
  113. }
  114. s.maybeStopStream(m.Term)
  115. if shouldInitStream(m) && !s.hasStreamClient() {
  116. s.initStream(types.ID(m.From), types.ID(m.To), m.Term)
  117. s.batcher.Reset(time.Now())
  118. }
  119. if canBatch(m) && s.hasStreamClient() {
  120. if s.batcher.ShouldBatch(time.Now()) {
  121. return nil
  122. }
  123. }
  124. if canUseStream(m) {
  125. if ok := s.tryStream(m); ok {
  126. return nil
  127. }
  128. }
  129. // TODO: don't block. we should be able to have 1000s
  130. // of messages out at a time.
  131. data := pbutil.MustMarshal(&m)
  132. select {
  133. case s.q <- data:
  134. return nil
  135. default:
  136. log.Printf("sender: reach the maximal serving to %s", s.u)
  137. return fmt.Errorf("reach maximal serving")
  138. }
  139. }
  140. func (s *sender) Stop() {
  141. close(s.q)
  142. s.wg.Wait()
  143. s.strmSrvMu.Lock()
  144. if s.strmSrv != nil {
  145. s.strmSrv.stop()
  146. }
  147. s.strmSrvMu.Unlock()
  148. if s.strmCln != nil {
  149. s.strmCln.stop()
  150. }
  151. }
  152. func (s *sender) Pause() {
  153. s.mu.Lock()
  154. defer s.mu.Unlock()
  155. s.paused = true
  156. }
  157. func (s *sender) Resume() {
  158. s.mu.Lock()
  159. defer s.mu.Unlock()
  160. s.paused = false
  161. }
  162. func (s *sender) maybeStopStream(term uint64) {
  163. if s.strmCln != nil && term > s.strmCln.term {
  164. s.strmCln.stop()
  165. s.strmCln = nil
  166. }
  167. s.strmSrvMu.Lock()
  168. defer s.strmSrvMu.Unlock()
  169. if s.strmSrv != nil && term > s.strmSrv.term {
  170. s.strmSrv.stop()
  171. s.strmSrv = nil
  172. }
  173. }
  174. func (s *sender) hasStreamClient() bool {
  175. return s.strmCln != nil && !s.strmCln.isStopped()
  176. }
  177. func (s *sender) initStream(from, to types.ID, term uint64) {
  178. strmCln := newStreamClient(from, to, term, s.p)
  179. s.mu.Lock()
  180. u := s.u
  181. s.mu.Unlock()
  182. if err := strmCln.start(s.tr, u, s.cid); err != nil {
  183. log.Printf("rafthttp: start stream client error: %v", err)
  184. return
  185. }
  186. s.strmCln = strmCln
  187. }
  188. func (s *sender) tryStream(m raftpb.Message) bool {
  189. s.strmSrvMu.Lock()
  190. defer s.strmSrvMu.Unlock()
  191. if s.strmSrv == nil || m.Term != s.strmSrv.term {
  192. return false
  193. }
  194. if err := s.strmSrv.send(m.Entries); err != nil {
  195. log.Printf("rafthttp: send stream message error: %v", err)
  196. s.strmSrv.stop()
  197. s.strmSrv = nil
  198. return false
  199. }
  200. return true
  201. }
  202. func (s *sender) handle() {
  203. defer s.wg.Done()
  204. for d := range s.q {
  205. start := time.Now()
  206. err := s.post(d)
  207. end := time.Now()
  208. if err != nil {
  209. s.fs.Fail()
  210. log.Printf("sender: %v", err)
  211. continue
  212. }
  213. s.fs.Succ(end.Sub(start))
  214. }
  215. }
  216. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  217. // error on any failure.
  218. func (s *sender) post(data []byte) error {
  219. s.mu.RLock()
  220. req, err := http.NewRequest("POST", s.u, bytes.NewBuffer(data))
  221. s.mu.RUnlock()
  222. if err != nil {
  223. return fmt.Errorf("new request to %s error: %v", s.u, err)
  224. }
  225. req.Header.Set("Content-Type", "application/protobuf")
  226. req.Header.Set("X-Etcd-Cluster-ID", s.cid.String())
  227. resp, err := s.tr.RoundTrip(req)
  228. if err != nil {
  229. return fmt.Errorf("error posting to %q: %v", req.URL.String(), err)
  230. }
  231. resp.Body.Close()
  232. switch resp.StatusCode {
  233. case http.StatusPreconditionFailed:
  234. select {
  235. case s.shouldstop <- struct{}{}:
  236. default:
  237. }
  238. log.Printf("etcdserver: conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), s.cid)
  239. return nil
  240. case http.StatusForbidden:
  241. select {
  242. case s.shouldstop <- struct{}{}:
  243. default:
  244. }
  245. log.Println("etcdserver: this member has been permanently removed from the cluster")
  246. 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")
  247. return nil
  248. case http.StatusNoContent:
  249. return nil
  250. default:
  251. return fmt.Errorf("unhandled status %s", http.StatusText(resp.StatusCode))
  252. }
  253. }