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/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. errorc chan error
  43. q chan *raftpb.Message
  44. // wait for the handling routines
  45. wg sync.WaitGroup
  46. sync.Mutex
  47. // if the last send was successful, the pipeline is active.
  48. // Or it is inactive
  49. active bool
  50. errored error
  51. }
  52. func newPipeline(tr http.RoundTripper, u string, id, cid types.ID, fs *stats.FollowerStats, errorc chan error) *pipeline {
  53. p := &pipeline{
  54. id: id,
  55. cid: cid,
  56. tr: tr,
  57. u: u,
  58. fs: fs,
  59. errorc: errorc,
  60. q: make(chan *raftpb.Message, pipelineBufSize),
  61. active: true,
  62. }
  63. p.wg.Add(connPerPipeline)
  64. for i := 0; i < connPerPipeline; i++ {
  65. go p.handle()
  66. }
  67. return p
  68. }
  69. func (p *pipeline) update(u string) { p.u = u }
  70. func (p *pipeline) send(m raftpb.Message) error {
  71. // TODO: don't block. we should be able to have 1000s
  72. // of messages out at a time.
  73. select {
  74. case p.q <- &m:
  75. return nil
  76. default:
  77. log.Printf("pipeline: dropping %s because maximal number %d of pipeline buffer entries to %s has been reached",
  78. m.Type, pipelineBufSize, p.u)
  79. return fmt.Errorf("reach maximal serving")
  80. }
  81. }
  82. func (p *pipeline) stop() {
  83. close(p.q)
  84. p.wg.Wait()
  85. }
  86. func (p *pipeline) handle() {
  87. defer p.wg.Done()
  88. for m := range p.q {
  89. start := time.Now()
  90. err := p.pipeline(pbutil.MustMarshal(m))
  91. end := time.Now()
  92. p.Lock()
  93. if err != nil {
  94. if p.errored == nil || p.errored.Error() != err.Error() {
  95. log.Printf("pipeline: error posting to %s: %v", p.id, err)
  96. p.errored = err
  97. }
  98. if p.active {
  99. log.Printf("pipeline: the connection with %s became inactive", p.id)
  100. p.active = false
  101. }
  102. if m.Type == raftpb.MsgApp {
  103. p.fs.Fail()
  104. }
  105. } else {
  106. if !p.active {
  107. log.Printf("pipeline: the connection with %s became active", p.id)
  108. p.active = true
  109. p.errored = nil
  110. }
  111. if m.Type == raftpb.MsgApp {
  112. p.fs.Succ(end.Sub(start))
  113. }
  114. }
  115. p.Unlock()
  116. }
  117. }
  118. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  119. // error on any failure.
  120. func (p *pipeline) pipeline(data []byte) error {
  121. p.Lock()
  122. req, err := http.NewRequest("POST", p.u, bytes.NewBuffer(data))
  123. p.Unlock()
  124. if err != nil {
  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. return err
  132. }
  133. resp.Body.Close()
  134. switch resp.StatusCode {
  135. case http.StatusPreconditionFailed:
  136. err := fmt.Errorf("conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), p.cid)
  137. select {
  138. case p.errorc <- err:
  139. default:
  140. }
  141. return nil
  142. case http.StatusForbidden:
  143. err := fmt.Errorf("the member has been permanently removed from the cluster")
  144. select {
  145. case p.errorc <- err:
  146. default:
  147. }
  148. return nil
  149. case http.StatusNoContent:
  150. return nil
  151. default:
  152. return fmt.Errorf("unexpected http status %s while posting to %q", http.StatusText(resp.StatusCode), req.URL.String())
  153. }
  154. }