sender.go 6.2 KB

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