pipeline.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2015 The etcd Authors
  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. "context"
  18. "errors"
  19. "io/ioutil"
  20. "sync"
  21. "time"
  22. stats "go.etcd.io/etcd/etcdserver/api/v2stats"
  23. "go.etcd.io/etcd/pkg/pbutil"
  24. "go.etcd.io/etcd/pkg/types"
  25. "go.etcd.io/etcd/raft"
  26. "go.etcd.io/etcd/raft/raftpb"
  27. "go.uber.org/zap"
  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. peerID types.ID
  40. tr *Transport
  41. picker *urlPicker
  42. status *peerStatus
  43. raft Raft
  44. errorc chan error
  45. // deprecate when we depercate v2 API
  46. followerStats *stats.FollowerStats
  47. msgc chan raftpb.Message
  48. // wait for the handling routines
  49. wg sync.WaitGroup
  50. stopc chan struct{}
  51. }
  52. func (p *pipeline) start() {
  53. p.stopc = make(chan struct{})
  54. p.msgc = make(chan raftpb.Message, pipelineBufSize)
  55. p.wg.Add(connPerPipeline)
  56. for i := 0; i < connPerPipeline; i++ {
  57. go p.handle()
  58. }
  59. if p.tr != nil && p.tr.Logger != nil {
  60. p.tr.Logger.Info(
  61. "started HTTP pipelining with remote peer",
  62. zap.String("local-member-id", p.tr.ID.String()),
  63. zap.String("remote-peer-id", p.peerID.String()),
  64. )
  65. } else {
  66. plog.Infof("started HTTP pipelining with peer %s", p.peerID)
  67. }
  68. }
  69. func (p *pipeline) stop() {
  70. close(p.stopc)
  71. p.wg.Wait()
  72. if p.tr != nil && p.tr.Logger != nil {
  73. p.tr.Logger.Info(
  74. "stopped HTTP pipelining with remote peer",
  75. zap.String("local-member-id", p.tr.ID.String()),
  76. zap.String("remote-peer-id", p.peerID.String()),
  77. )
  78. } else {
  79. plog.Infof("stopped HTTP pipelining with peer %s", p.peerID)
  80. }
  81. }
  82. func (p *pipeline) handle() {
  83. defer p.wg.Done()
  84. for {
  85. select {
  86. case m := <-p.msgc:
  87. start := time.Now()
  88. err := p.post(pbutil.MustMarshal(&m))
  89. end := time.Now()
  90. if err != nil {
  91. p.status.deactivate(failureType{source: pipelineMsg, action: "write"}, err.Error())
  92. if m.Type == raftpb.MsgApp && p.followerStats != nil {
  93. p.followerStats.Fail()
  94. }
  95. p.raft.ReportUnreachable(m.To)
  96. if isMsgSnap(m) {
  97. p.raft.ReportSnapshot(m.To, raft.SnapshotFailure)
  98. }
  99. sentFailures.WithLabelValues(types.ID(m.To).String()).Inc()
  100. continue
  101. }
  102. p.status.activate()
  103. if m.Type == raftpb.MsgApp && p.followerStats != nil {
  104. p.followerStats.Succ(end.Sub(start))
  105. }
  106. if isMsgSnap(m) {
  107. p.raft.ReportSnapshot(m.To, raft.SnapshotFinish)
  108. }
  109. sentBytes.WithLabelValues(types.ID(m.To).String()).Add(float64(m.Size()))
  110. case <-p.stopc:
  111. return
  112. }
  113. }
  114. }
  115. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  116. // error on any failure.
  117. func (p *pipeline) post(data []byte) (err error) {
  118. u := p.picker.pick()
  119. req := createPostRequest(u, RaftPrefix, bytes.NewBuffer(data), "application/protobuf", p.tr.URLs, p.tr.ID, p.tr.ClusterID)
  120. done := make(chan struct{}, 1)
  121. ctx, cancel := context.WithCancel(context.Background())
  122. req = req.WithContext(ctx)
  123. go func() {
  124. select {
  125. case <-done:
  126. case <-p.stopc:
  127. waitSchedule()
  128. cancel()
  129. }
  130. }()
  131. resp, err := p.tr.pipelineRt.RoundTrip(req)
  132. done <- struct{}{}
  133. if err != nil {
  134. p.picker.unreachable(u)
  135. return err
  136. }
  137. defer resp.Body.Close()
  138. b, err := ioutil.ReadAll(resp.Body)
  139. if err != nil {
  140. p.picker.unreachable(u)
  141. return err
  142. }
  143. err = checkPostResponse(resp, b, req, p.peerID)
  144. if err != nil {
  145. p.picker.unreachable(u)
  146. // errMemberRemoved is a critical error since a removed member should
  147. // always be stopped. So we use reportCriticalError to report it to errorc.
  148. if err == errMemberRemoved {
  149. reportCriticalError(err, p.errorc)
  150. }
  151. return err
  152. }
  153. return nil
  154. }
  155. // waitSchedule waits other goroutines to be scheduled for a while
  156. func waitSchedule() { time.Sleep(time.Millisecond) }