pipeline.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/coreos/etcd/etcdserver/stats"
  25. "github.com/coreos/etcd/pkg/pbutil"
  26. "github.com/coreos/etcd/pkg/types"
  27. "github.com/coreos/etcd/raft"
  28. "github.com/coreos/etcd/raft/raftpb"
  29. "github.com/coreos/etcd/version"
  30. )
  31. const (
  32. connPerPipeline = 4
  33. // pipelineBufSize is the size of pipeline buffer, which helps hold the
  34. // temporary network latency.
  35. // The size ensures that pipeline does not drop messages when the network
  36. // is out of work for less than 1 second in good path.
  37. pipelineBufSize = 64
  38. )
  39. var errStopped = errors.New("stopped")
  40. type canceler interface {
  41. CancelRequest(*http.Request)
  42. }
  43. type pipeline struct {
  44. from, to types.ID
  45. cid types.ID
  46. tr http.RoundTripper
  47. picker *urlPicker
  48. status *peerStatus
  49. fs *stats.FollowerStats
  50. r Raft
  51. errorc chan error
  52. msgc chan raftpb.Message
  53. // wait for the handling routines
  54. wg sync.WaitGroup
  55. stopc chan struct{}
  56. }
  57. func newPipeline(tr http.RoundTripper, picker *urlPicker, from, to, cid types.ID, status *peerStatus, fs *stats.FollowerStats, r Raft, errorc chan error) *pipeline {
  58. p := &pipeline{
  59. from: from,
  60. to: to,
  61. cid: cid,
  62. tr: tr,
  63. picker: picker,
  64. status: status,
  65. fs: fs,
  66. r: r,
  67. errorc: errorc,
  68. stopc: make(chan struct{}),
  69. msgc: make(chan raftpb.Message, pipelineBufSize),
  70. }
  71. p.wg.Add(connPerPipeline)
  72. for i := 0; i < connPerPipeline; i++ {
  73. go p.handle()
  74. }
  75. return p
  76. }
  77. func (p *pipeline) stop() {
  78. close(p.msgc)
  79. close(p.stopc)
  80. p.wg.Wait()
  81. }
  82. func (p *pipeline) handle() {
  83. defer p.wg.Done()
  84. for m := range p.msgc {
  85. start := time.Now()
  86. err := p.post(pbutil.MustMarshal(&m))
  87. if err == errStopped {
  88. return
  89. }
  90. end := time.Now()
  91. if err != nil {
  92. reportSentFailure(pipelineMsg, m)
  93. p.status.deactivate(failureType{source: pipelineMsg, action: "write"}, err.Error())
  94. if m.Type == raftpb.MsgApp && p.fs != nil {
  95. p.fs.Fail()
  96. }
  97. p.r.ReportUnreachable(m.To)
  98. if isMsgSnap(m) {
  99. p.r.ReportSnapshot(m.To, raft.SnapshotFailure)
  100. }
  101. } else {
  102. p.status.activate()
  103. if m.Type == raftpb.MsgApp && p.fs != nil {
  104. p.fs.Succ(end.Sub(start))
  105. }
  106. if isMsgSnap(m) {
  107. p.r.ReportSnapshot(m.To, raft.SnapshotFinish)
  108. }
  109. reportSentDuration(pipelineMsg, m, time.Since(start))
  110. }
  111. }
  112. }
  113. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  114. // error on any failure.
  115. func (p *pipeline) post(data []byte) (err error) {
  116. u := p.picker.pick()
  117. uu := u
  118. uu.Path = RaftPrefix
  119. req, err := http.NewRequest("POST", uu.String(), bytes.NewBuffer(data))
  120. if err != nil {
  121. p.picker.unreachable(u)
  122. return err
  123. }
  124. req.Header.Set("Content-Type", "application/protobuf")
  125. req.Header.Set("X-Server-From", p.from.String())
  126. req.Header.Set("X-Server-Version", version.Version)
  127. req.Header.Set("X-Min-Cluster-Version", version.MinClusterVersion)
  128. req.Header.Set("X-Etcd-Cluster-ID", p.cid.String())
  129. var stopped bool
  130. defer func() {
  131. if stopped {
  132. // rewrite to errStopped so the caller goroutine can stop itself
  133. err = errStopped
  134. }
  135. }()
  136. done := make(chan struct{}, 1)
  137. go func() {
  138. select {
  139. case <-done:
  140. case <-p.stopc:
  141. waitSchedule()
  142. stopped = true
  143. if cancel, ok := p.tr.(canceler); ok {
  144. cancel.CancelRequest(req)
  145. }
  146. }
  147. }()
  148. resp, err := p.tr.RoundTrip(req)
  149. done <- struct{}{}
  150. if err != nil {
  151. p.picker.unreachable(u)
  152. return err
  153. }
  154. b, err := ioutil.ReadAll(resp.Body)
  155. if err != nil {
  156. p.picker.unreachable(u)
  157. return err
  158. }
  159. resp.Body.Close()
  160. switch resp.StatusCode {
  161. case http.StatusPreconditionFailed:
  162. switch strings.TrimSuffix(string(b), "\n") {
  163. case errIncompatibleVersion.Error():
  164. plog.Errorf("request sent was ignored by peer %s (server version incompatible)", p.to)
  165. return errIncompatibleVersion
  166. case errClusterIDMismatch.Error():
  167. plog.Errorf("request sent was ignored (cluster ID mismatch: remote[%s]=%s, local=%s)",
  168. p.to, resp.Header.Get("X-Etcd-Cluster-ID"), p.cid)
  169. return errClusterIDMismatch
  170. default:
  171. return fmt.Errorf("unhandled error %q when precondition failed", string(b))
  172. }
  173. case http.StatusForbidden:
  174. err := fmt.Errorf("the member has been permanently removed from the cluster")
  175. select {
  176. case p.errorc <- err:
  177. default:
  178. }
  179. return nil
  180. case http.StatusNoContent:
  181. return nil
  182. default:
  183. return fmt.Errorf("unexpected http status %s while posting to %q", http.StatusText(resp.StatusCode), req.URL.String())
  184. }
  185. }
  186. // waitSchedule waits other goroutines to be scheduled for a while
  187. func waitSchedule() { time.Sleep(time.Millisecond) }