sendhub.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 etcdserver
  14. import (
  15. "log"
  16. "net/http"
  17. "net/url"
  18. "path"
  19. "sync"
  20. "github.com/coreos/etcd/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. "github.com/coreos/etcd/rafthttp"
  24. )
  25. const (
  26. raftPrefix = "/raft"
  27. )
  28. type SendHub interface {
  29. rafthttp.SenderFinder
  30. Send(m []raftpb.Message)
  31. Add(m *Member)
  32. Remove(id types.ID)
  33. Update(m *Member)
  34. Stop()
  35. ShouldStopNotify() <-chan struct{}
  36. }
  37. type sendHub struct {
  38. tr http.RoundTripper
  39. cl ClusterInfo
  40. p rafthttp.Processor
  41. ss *stats.ServerStats
  42. ls *stats.LeaderStats
  43. mu sync.RWMutex // protect the sender map
  44. senders map[types.ID]rafthttp.Sender
  45. shouldstop chan struct{}
  46. }
  47. // newSendHub creates the default send hub used to transport raft messages
  48. // to other members. The returned sendHub will update the given ServerStats and
  49. // LeaderStats appropriately.
  50. func newSendHub(t http.RoundTripper, cl ClusterInfo, p rafthttp.Processor, ss *stats.ServerStats, ls *stats.LeaderStats) *sendHub {
  51. return &sendHub{
  52. tr: t,
  53. cl: cl,
  54. p: p,
  55. ss: ss,
  56. ls: ls,
  57. senders: make(map[types.ID]rafthttp.Sender),
  58. shouldstop: make(chan struct{}, 1),
  59. }
  60. }
  61. func (h *sendHub) Sender(id types.ID) rafthttp.Sender {
  62. h.mu.RLock()
  63. defer h.mu.RUnlock()
  64. return h.senders[id]
  65. }
  66. func (h *sendHub) Send(msgs []raftpb.Message) {
  67. for _, m := range msgs {
  68. to := types.ID(m.To)
  69. s, ok := h.senders[to]
  70. if !ok {
  71. if !h.cl.IsIDRemoved(to) {
  72. log.Printf("etcdserver: send message to unknown receiver %s", to)
  73. }
  74. continue
  75. }
  76. if m.Type == raftpb.MsgApp {
  77. h.ss.SendAppendReq(m.Size())
  78. }
  79. s.Send(m)
  80. }
  81. }
  82. func (h *sendHub) Stop() {
  83. for _, s := range h.senders {
  84. s.Stop()
  85. }
  86. if tr, ok := h.tr.(*http.Transport); ok {
  87. tr.CloseIdleConnections()
  88. }
  89. }
  90. func (h *sendHub) ShouldStopNotify() <-chan struct{} {
  91. return h.shouldstop
  92. }
  93. func (h *sendHub) Add(m *Member) {
  94. h.mu.Lock()
  95. defer h.mu.Unlock()
  96. if _, ok := h.senders[m.ID]; ok {
  97. return
  98. }
  99. // TODO: considering how to switch between all available peer urls
  100. peerURL := m.PickPeerURL()
  101. u, err := url.Parse(peerURL)
  102. if err != nil {
  103. log.Panicf("unexpect peer url %s", peerURL)
  104. }
  105. u.Path = path.Join(u.Path, raftPrefix)
  106. fs := h.ls.Follower(m.ID.String())
  107. s := rafthttp.NewSender(h.tr, u.String(), h.cl.ID(), h.p, fs, h.shouldstop)
  108. h.senders[m.ID] = s
  109. }
  110. func (h *sendHub) Remove(id types.ID) {
  111. h.mu.Lock()
  112. defer h.mu.Unlock()
  113. h.senders[id].Stop()
  114. delete(h.senders, id)
  115. }
  116. func (h *sendHub) Update(m *Member) {
  117. h.mu.Lock()
  118. defer h.mu.Unlock()
  119. // TODO: return error or just panic?
  120. if _, ok := h.senders[m.ID]; !ok {
  121. return
  122. }
  123. peerURL := m.PickPeerURL()
  124. u, err := url.Parse(peerURL)
  125. if err != nil {
  126. log.Panicf("unexpect peer url %s", peerURL)
  127. }
  128. u.Path = path.Join(u.Path, raftPrefix)
  129. h.senders[m.ID].Update(u.String())
  130. }
  131. // for testing
  132. func (h *sendHub) pause() {
  133. for _, s := range h.senders {
  134. s.Pause()
  135. }
  136. }
  137. func (h *sendHub) resume() {
  138. for _, s := range h.senders {
  139. s.Resume()
  140. }
  141. }