pipeline.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. msgc 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. msgc: 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) stop() {
  71. close(p.msgc)
  72. p.wg.Wait()
  73. }
  74. func (p *pipeline) handle() {
  75. defer p.wg.Done()
  76. for m := range p.msgc {
  77. start := time.Now()
  78. err := p.post(pbutil.MustMarshal(&m))
  79. end := time.Now()
  80. p.Lock()
  81. if err != nil {
  82. if p.errored == nil || p.errored.Error() != err.Error() {
  83. log.Printf("pipeline: error posting to %s: %v", p.id, err)
  84. p.errored = err
  85. }
  86. if p.active {
  87. log.Printf("pipeline: the connection with %s became inactive", p.id)
  88. p.active = false
  89. }
  90. if m.Type == raftpb.MsgApp {
  91. p.fs.Fail()
  92. }
  93. } else {
  94. if !p.active {
  95. log.Printf("pipeline: the connection with %s became active", p.id)
  96. p.active = true
  97. p.errored = nil
  98. }
  99. if m.Type == raftpb.MsgApp {
  100. p.fs.Succ(end.Sub(start))
  101. }
  102. }
  103. p.Unlock()
  104. }
  105. }
  106. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  107. // error on any failure.
  108. func (p *pipeline) post(data []byte) error {
  109. p.Lock()
  110. req, err := http.NewRequest("POST", p.u, bytes.NewBuffer(data))
  111. p.Unlock()
  112. if err != nil {
  113. return err
  114. }
  115. req.Header.Set("Content-Type", "application/protobuf")
  116. req.Header.Set("X-Etcd-Cluster-ID", p.cid.String())
  117. resp, err := p.tr.RoundTrip(req)
  118. if err != nil {
  119. return err
  120. }
  121. resp.Body.Close()
  122. switch resp.StatusCode {
  123. case http.StatusPreconditionFailed:
  124. err := fmt.Errorf("conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), p.cid)
  125. select {
  126. case p.errorc <- err:
  127. default:
  128. }
  129. return nil
  130. case http.StatusForbidden:
  131. err := fmt.Errorf("the member has been permanently removed from the cluster")
  132. select {
  133. case p.errorc <- err:
  134. default:
  135. }
  136. return nil
  137. case http.StatusNoContent:
  138. return nil
  139. default:
  140. return fmt.Errorf("unexpected http status %s while posting to %q", http.StatusText(resp.StatusCode), req.URL.String())
  141. }
  142. }