sender.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/types"
  23. )
  24. const (
  25. connPerSender = 4
  26. senderBufSize = connPerSender * 4
  27. )
  28. type Sender interface {
  29. Update(u string)
  30. // Send sends the data to the remote node. It is always non-blocking.
  31. // It may be fail to send data if it returns nil error.
  32. Send(data []byte) error
  33. // Stop performs any necessary finalization and terminates the Sender
  34. // elegantly.
  35. Stop()
  36. }
  37. func NewSender(tr http.RoundTripper, u string, cid types.ID, fs *stats.FollowerStats, shouldstop chan struct{}) *sender {
  38. s := &sender{
  39. tr: tr,
  40. u: u,
  41. cid: cid,
  42. fs: fs,
  43. q: make(chan []byte, senderBufSize),
  44. shouldstop: shouldstop,
  45. }
  46. s.wg.Add(connPerSender)
  47. for i := 0; i < connPerSender; i++ {
  48. go s.handle()
  49. }
  50. return s
  51. }
  52. type sender struct {
  53. tr http.RoundTripper
  54. u string
  55. cid types.ID
  56. fs *stats.FollowerStats
  57. q chan []byte
  58. mu sync.RWMutex
  59. wg sync.WaitGroup
  60. shouldstop chan struct{}
  61. }
  62. func (s *sender) Update(u string) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. s.u = u
  66. }
  67. // TODO (xiangli): reasonable retry logic
  68. func (s *sender) Send(data []byte) error {
  69. select {
  70. case s.q <- data:
  71. return nil
  72. default:
  73. log.Printf("sender: reach the maximal serving to %s", s.u)
  74. return fmt.Errorf("reach maximal serving")
  75. }
  76. }
  77. func (s *sender) Stop() {
  78. close(s.q)
  79. s.wg.Wait()
  80. }
  81. func (s *sender) handle() {
  82. defer s.wg.Done()
  83. for d := range s.q {
  84. start := time.Now()
  85. err := s.post(d)
  86. end := time.Now()
  87. if err != nil {
  88. s.fs.Fail()
  89. log.Printf("sender: %v", err)
  90. continue
  91. }
  92. s.fs.Succ(end.Sub(start))
  93. }
  94. }
  95. // post POSTs a data payload to a url. Returns nil if the POST succeeds,
  96. // error on any failure.
  97. func (s *sender) post(data []byte) error {
  98. s.mu.RLock()
  99. req, err := http.NewRequest("POST", s.u, bytes.NewBuffer(data))
  100. s.mu.RUnlock()
  101. if err != nil {
  102. return fmt.Errorf("new request to %s error: %v", s.u, err)
  103. }
  104. req.Header.Set("Content-Type", "application/protobuf")
  105. req.Header.Set("X-Etcd-Cluster-ID", s.cid.String())
  106. resp, err := s.tr.RoundTrip(req)
  107. if err != nil {
  108. return fmt.Errorf("error posting to %q: %v", req.URL.String(), err)
  109. }
  110. resp.Body.Close()
  111. switch resp.StatusCode {
  112. case http.StatusPreconditionFailed:
  113. select {
  114. case s.shouldstop <- struct{}{}:
  115. default:
  116. }
  117. log.Printf("etcdserver: conflicting cluster ID with the target cluster (%s != %s)", resp.Header.Get("X-Etcd-Cluster-ID"), s.cid)
  118. return nil
  119. case http.StatusForbidden:
  120. select {
  121. case s.shouldstop <- struct{}{}:
  122. default:
  123. }
  124. log.Println("etcdserver: this member has been permanently removed from the cluster")
  125. 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")
  126. return nil
  127. case http.StatusNoContent:
  128. return nil
  129. default:
  130. return fmt.Errorf("unhandled status %s", http.StatusText(resp.StatusCode))
  131. }
  132. }