pipeline.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "fmt"
  18. "log"
  19. "net/http"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/etcdserver/stats"
  23. "github.com/coreos/etcd/pkg/pbutil"
  24. "github.com/coreos/etcd/pkg/types"
  25. "github.com/coreos/etcd/raft"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. )
  28. const (
  29. connPerPipeline = 4
  30. // pipelineBufSize is the size of pipeline buffer, which helps hold the
  31. // temporary network latency.
  32. // The size ensures that pipeline does not drop messages when the network
  33. // is out of work for less than 1 second in good path.
  34. pipelineBufSize = 64
  35. )
  36. type pipeline struct {
  37. id types.ID
  38. cid types.ID
  39. tr http.RoundTripper
  40. picker *urlPicker
  41. fs *stats.FollowerStats
  42. r Raft
  43. errorc chan error
  44. msgc chan raftpb.Message
  45. // wait for the handling routines
  46. wg sync.WaitGroup
  47. sync.Mutex
  48. // if the last send was successful, the pipeline is active.
  49. // Or it is inactive
  50. active bool
  51. errored error
  52. }
  53. func newPipeline(tr http.RoundTripper, picker *urlPicker, id, cid types.ID, fs *stats.FollowerStats, r Raft, errorc chan error) *pipeline {
  54. p := &pipeline{
  55. id: id,
  56. cid: cid,
  57. tr: tr,
  58. picker: picker,
  59. fs: fs,
  60. r: r,
  61. errorc: errorc,
  62. msgc: make(chan raftpb.Message, pipelineBufSize),
  63. active: true,
  64. }
  65. p.wg.Add(connPerPipeline)
  66. for i := 0; i < connPerPipeline; i++ {
  67. go p.handle()
  68. }
  69. return p
  70. }
  71. func (p *pipeline) stop() {
  72. close(p.msgc)
  73. p.wg.Wait()
  74. }
  75. func (p *pipeline) handle() {
  76. defer p.wg.Done()
  77. for m := range p.msgc {
  78. start := time.Now()
  79. err := p.post(pbutil.MustMarshal(&m))
  80. end := time.Now()
  81. p.Lock()
  82. if err != nil {
  83. reportSentFailure(pipelineMsg, m)
  84. if p.errored == nil || p.errored.Error() != err.Error() {
  85. log.Printf("pipeline: error posting to %s: %v", p.id, err)
  86. p.errored = err
  87. }
  88. if p.active {
  89. log.Printf("pipeline: the connection with %s became inactive", p.id)
  90. p.active = false
  91. }
  92. if m.Type == raftpb.MsgApp && p.fs != nil {
  93. p.fs.Fail()
  94. }
  95. p.r.ReportUnreachable(m.To)
  96. if isMsgSnap(m) {
  97. p.r.ReportSnapshot(m.To, raft.SnapshotFailure)
  98. }
  99. } else {
  100. if !p.active {
  101. log.Printf("pipeline: the connection with %s became active", p.id)
  102. p.active = true
  103. p.errored = nil
  104. }
  105. if m.Type == raftpb.MsgApp && p.fs != nil {
  106. p.fs.Succ(end.Sub(start))
  107. }
  108. if isMsgSnap(m) {
  109. p.r.ReportSnapshot(m.To, raft.SnapshotFinish)
  110. }
  111. reportSentDuration(pipelineMsg, m, time.Since(start))
  112. }
  113. p.Unlock()
  114. }
  115. }
  116. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  117. // error on any failure.
  118. func (p *pipeline) post(data []byte) error {
  119. u := p.picker.pick()
  120. uu := u
  121. uu.Path = RaftPrefix
  122. req, err := http.NewRequest("POST", uu.String(), bytes.NewBuffer(data))
  123. if err != nil {
  124. p.picker.unreachable(u)
  125. return err
  126. }
  127. req.Header.Set("Content-Type", "application/protobuf")
  128. req.Header.Set("X-Etcd-Cluster-ID", p.cid.String())
  129. resp, err := p.tr.RoundTrip(req)
  130. if err != nil {
  131. p.picker.unreachable(u)
  132. return err
  133. }
  134. resp.Body.Close()
  135. switch resp.StatusCode {
  136. case http.StatusPreconditionFailed:
  137. err := fmt.Errorf("conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), p.cid)
  138. select {
  139. case p.errorc <- err:
  140. default:
  141. }
  142. return nil
  143. case http.StatusForbidden:
  144. err := fmt.Errorf("the member has been permanently removed from the cluster")
  145. select {
  146. case p.errorc <- err:
  147. default:
  148. }
  149. return nil
  150. case http.StatusNoContent:
  151. return nil
  152. default:
  153. return fmt.Errorf("unexpected http status %s while posting to %q", http.StatusText(resp.StatusCode), req.URL.String())
  154. }
  155. }