sender.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rafthttp
  14. import (
  15. "bytes"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "sync"
  20. "time"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. )
  26. const (
  27. connPerSender = 4
  28. senderBufSize = connPerSender * 4
  29. )
  30. type Sender interface {
  31. Update(u string)
  32. // Send sends the data to the remote node. It is always non-blocking.
  33. // It may be fail to send data if it returns nil error.
  34. Send(m raftpb.Message) error
  35. // Stop performs any necessary finalization and terminates the Sender
  36. // elegantly.
  37. Stop()
  38. }
  39. func NewSender(tr http.RoundTripper, u string, cid types.ID, fs *stats.FollowerStats, shouldstop chan struct{}) *sender {
  40. s := &sender{
  41. tr: tr,
  42. u: u,
  43. cid: cid,
  44. fs: fs,
  45. q: make(chan []byte, senderBufSize),
  46. shouldstop: shouldstop,
  47. }
  48. s.wg.Add(connPerSender)
  49. for i := 0; i < connPerSender; i++ {
  50. go s.handle()
  51. }
  52. return s
  53. }
  54. type sender struct {
  55. tr http.RoundTripper
  56. u string
  57. cid types.ID
  58. fs *stats.FollowerStats
  59. q chan []byte
  60. mu sync.RWMutex
  61. wg sync.WaitGroup
  62. shouldstop chan struct{}
  63. }
  64. func (s *sender) Update(u string) {
  65. s.mu.Lock()
  66. defer s.mu.Unlock()
  67. s.u = u
  68. }
  69. // TODO (xiangli): reasonable retry logic
  70. func (s *sender) 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. data := pbutil.MustMarshal(&m)
  74. select {
  75. case s.q <- data:
  76. return nil
  77. default:
  78. log.Printf("sender: reach the maximal serving to %s", s.u)
  79. return fmt.Errorf("reach maximal serving")
  80. }
  81. }
  82. func (s *sender) Stop() {
  83. close(s.q)
  84. s.wg.Wait()
  85. }
  86. func (s *sender) handle() {
  87. defer s.wg.Done()
  88. for d := range s.q {
  89. start := time.Now()
  90. err := s.post(d)
  91. end := time.Now()
  92. if err != nil {
  93. s.fs.Fail()
  94. log.Printf("sender: %v", err)
  95. continue
  96. }
  97. s.fs.Succ(end.Sub(start))
  98. }
  99. }
  100. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  101. // error on any failure.
  102. func (s *sender) post(data []byte) error {
  103. s.mu.RLock()
  104. req, err := http.NewRequest("POST", s.u, bytes.NewBuffer(data))
  105. s.mu.RUnlock()
  106. if err != nil {
  107. return fmt.Errorf("new request to %s error: %v", s.u, err)
  108. }
  109. req.Header.Set("Content-Type", "application/protobuf")
  110. req.Header.Set("X-Etcd-Cluster-ID", s.cid.String())
  111. resp, err := s.tr.RoundTrip(req)
  112. if err != nil {
  113. return fmt.Errorf("error posting to %q: %v", req.URL.String(), err)
  114. }
  115. resp.Body.Close()
  116. switch resp.StatusCode {
  117. case http.StatusPreconditionFailed:
  118. select {
  119. case s.shouldstop <- struct{}{}:
  120. default:
  121. }
  122. log.Printf("etcdserver: conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), s.cid)
  123. return nil
  124. case http.StatusForbidden:
  125. select {
  126. case s.shouldstop <- struct{}{}:
  127. default:
  128. }
  129. log.Println("etcdserver: this member has been permanently removed from the cluster")
  130. log.Println("etcdserver: the data-dir used by this member must be removed so that this host can be re-added with a new member ID")
  131. return nil
  132. case http.StatusNoContent:
  133. return nil
  134. default:
  135. return fmt.Errorf("unhandled status %s", http.StatusText(resp.StatusCode))
  136. }
  137. }