pipeline.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/raftpb"
  26. )
  27. const (
  28. connPerPipeline = 4
  29. // pipelineBufSize is the size of pipeline buffer, which helps hold the
  30. // temporary network latency.
  31. // The size ensures that pipeline does not drop messages when the network
  32. // is out of work for less than 1 second in good path.
  33. pipelineBufSize = 64
  34. )
  35. type pipeline struct {
  36. id types.ID
  37. cid types.ID
  38. tr http.RoundTripper
  39. // the url this pipeline sends to
  40. u string
  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, u string, 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. u: u,
  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) update(u string) { p.u = u }
  72. func (p *pipeline) stop() {
  73. close(p.msgc)
  74. p.wg.Wait()
  75. }
  76. func (p *pipeline) handle() {
  77. defer p.wg.Done()
  78. for m := range p.msgc {
  79. start := time.Now()
  80. err := p.post(pbutil.MustMarshal(&m))
  81. end := time.Now()
  82. p.Lock()
  83. if err != nil {
  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 {
  93. p.fs.Fail()
  94. }
  95. p.r.ReportUnreachable(m.To)
  96. } else {
  97. if !p.active {
  98. log.Printf("pipeline: the connection with %s became active", p.id)
  99. p.active = true
  100. p.errored = nil
  101. }
  102. if m.Type == raftpb.MsgApp {
  103. p.fs.Succ(end.Sub(start))
  104. }
  105. }
  106. p.Unlock()
  107. }
  108. }
  109. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  110. // error on any failure.
  111. func (p *pipeline) post(data []byte) error {
  112. p.Lock()
  113. req, err := http.NewRequest("POST", p.u, bytes.NewBuffer(data))
  114. p.Unlock()
  115. if err != nil {
  116. return err
  117. }
  118. req.Header.Set("Content-Type", "application/protobuf")
  119. req.Header.Set("X-Etcd-Cluster-ID", p.cid.String())
  120. resp, err := p.tr.RoundTrip(req)
  121. if err != nil {
  122. return err
  123. }
  124. resp.Body.Close()
  125. switch resp.StatusCode {
  126. case http.StatusPreconditionFailed:
  127. err := fmt.Errorf("conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), p.cid)
  128. select {
  129. case p.errorc <- err:
  130. default:
  131. }
  132. return nil
  133. case http.StatusForbidden:
  134. err := fmt.Errorf("the member has been permanently removed from the cluster")
  135. select {
  136. case p.errorc <- err:
  137. default:
  138. }
  139. return nil
  140. case http.StatusNoContent:
  141. return nil
  142. default:
  143. return fmt.Errorf("unexpected http status %s while posting to %q", http.StatusText(resp.StatusCode), req.URL.String())
  144. }
  145. }