sendhub.go 3.4 KB

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