pipeline.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "io/ioutil"
  19. "net/http"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/etcdserver/stats"
  23. "github.com/coreos/etcd/pkg/httputil"
  24. "github.com/coreos/etcd/pkg/pbutil"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/raft"
  27. "github.com/coreos/etcd/raft/raftpb"
  28. )
  29. const (
  30. connPerPipeline = 4
  31. // pipelineBufSize is the size of pipeline buffer, which helps hold the
  32. // temporary network latency.
  33. // The size ensures that pipeline does not drop messages when the network
  34. // is out of work for less than 1 second in good path.
  35. pipelineBufSize = 64
  36. )
  37. var errStopped = errors.New("stopped")
  38. type pipeline struct {
  39. from, to types.ID
  40. cid types.ID
  41. tr http.RoundTripper
  42. picker *urlPicker
  43. status *peerStatus
  44. fs *stats.FollowerStats
  45. r Raft
  46. errorc chan error
  47. msgc chan raftpb.Message
  48. // wait for the handling routines
  49. wg sync.WaitGroup
  50. stopc chan struct{}
  51. }
  52. func newPipeline(tr http.RoundTripper, picker *urlPicker, from, to, cid types.ID, status *peerStatus, fs *stats.FollowerStats, r Raft, errorc chan error) *pipeline {
  53. p := &pipeline{
  54. from: from,
  55. to: to,
  56. cid: cid,
  57. tr: tr,
  58. picker: picker,
  59. status: status,
  60. fs: fs,
  61. r: r,
  62. errorc: errorc,
  63. stopc: make(chan struct{}),
  64. msgc: make(chan raftpb.Message, pipelineBufSize),
  65. }
  66. p.wg.Add(connPerPipeline)
  67. for i := 0; i < connPerPipeline; i++ {
  68. go p.handle()
  69. }
  70. return p
  71. }
  72. func (p *pipeline) stop() {
  73. close(p.msgc)
  74. close(p.stopc)
  75. p.wg.Wait()
  76. }
  77. func (p *pipeline) handle() {
  78. defer p.wg.Done()
  79. for m := range p.msgc {
  80. start := time.Now()
  81. err := p.post(pbutil.MustMarshal(&m))
  82. if err == errStopped {
  83. return
  84. }
  85. end := time.Now()
  86. if err != nil {
  87. p.status.deactivate(failureType{source: pipelineMsg, action: "write"}, err.Error())
  88. reportSentFailure(pipelineMsg, m)
  89. if m.Type == raftpb.MsgApp && p.fs != nil {
  90. p.fs.Fail()
  91. }
  92. p.r.ReportUnreachable(m.To)
  93. if isMsgSnap(m) {
  94. p.r.ReportSnapshot(m.To, raft.SnapshotFailure)
  95. }
  96. return
  97. }
  98. p.status.activate()
  99. if m.Type == raftpb.MsgApp && p.fs != nil {
  100. p.fs.Succ(end.Sub(start))
  101. }
  102. if isMsgSnap(m) {
  103. p.r.ReportSnapshot(m.To, raft.SnapshotFinish)
  104. }
  105. reportSentDuration(pipelineMsg, m, time.Since(start))
  106. }
  107. }
  108. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  109. // error on any failure.
  110. func (p *pipeline) post(data []byte) (err error) {
  111. u := p.picker.pick()
  112. req := createPostRequest(u, RaftPrefix, bytes.NewBuffer(data), "application/protobuf", p.from, p.cid)
  113. var stopped bool
  114. defer func() {
  115. if stopped {
  116. // rewrite to errStopped so the caller goroutine can stop itself
  117. err = errStopped
  118. }
  119. }()
  120. done := make(chan struct{}, 1)
  121. cancel := httputil.RequestCanceler(p.tr, req)
  122. go func() {
  123. select {
  124. case <-done:
  125. case <-p.stopc:
  126. waitSchedule()
  127. stopped = true
  128. cancel()
  129. }
  130. }()
  131. resp, err := p.tr.RoundTrip(req)
  132. done <- struct{}{}
  133. if err != nil {
  134. p.picker.unreachable(u)
  135. return err
  136. }
  137. b, err := ioutil.ReadAll(resp.Body)
  138. if err != nil {
  139. p.picker.unreachable(u)
  140. return err
  141. }
  142. resp.Body.Close()
  143. err = checkPostResponse(resp, b, req, p.to)
  144. // errMemberRemoved is a critical error since a removed member should
  145. // always be stopped. So we use reportCriticalError to report it to errorc.
  146. if err == errMemberRemoved {
  147. reportCriticalError(err, p.errorc)
  148. return nil
  149. }
  150. return err
  151. }
  152. // waitSchedule waits other goroutines to be scheduled for a while
  153. func waitSchedule() { time.Sleep(time.Millisecond) }