sendhub.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return &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. }
  59. func (h *sendHub) Sender(id types.ID) rafthttp.Sender { return h.senders[id] }
  60. func (h *sendHub) Send(msgs []raftpb.Message) {
  61. for _, m := range msgs {
  62. to := types.ID(m.To)
  63. s, ok := h.senders[to]
  64. if !ok {
  65. if !h.cl.IsIDRemoved(to) {
  66. log.Printf("etcdserver: send message to unknown receiver %s", to)
  67. }
  68. continue
  69. }
  70. if m.Type == raftpb.MsgApp {
  71. h.ss.SendAppendReq(m.Size())
  72. }
  73. s.Send(m)
  74. }
  75. }
  76. func (h *sendHub) Stop() {
  77. for _, s := range h.senders {
  78. s.Stop()
  79. }
  80. if tr, ok := h.tr.(*http.Transport); ok {
  81. tr.CloseIdleConnections()
  82. }
  83. }
  84. func (h *sendHub) ShouldStopNotify() <-chan struct{} {
  85. return h.shouldstop
  86. }
  87. func (h *sendHub) Add(m *Member) {
  88. if _, ok := h.senders[m.ID]; ok {
  89. return
  90. }
  91. // TODO: considering how to switch between all available peer urls
  92. peerURL := m.PickPeerURL()
  93. u, err := url.Parse(peerURL)
  94. if err != nil {
  95. log.Panicf("unexpect peer url %s", peerURL)
  96. }
  97. u.Path = path.Join(u.Path, raftPrefix)
  98. fs := h.ls.Follower(m.ID.String())
  99. s := rafthttp.NewSender(h.tr, u.String(), m.ID, h.cl.ID(), h.p, fs, h.shouldstop)
  100. h.senders[m.ID] = s
  101. }
  102. func (h *sendHub) Remove(id types.ID) {
  103. h.senders[id].Stop()
  104. delete(h.senders, id)
  105. }
  106. func (h *sendHub) Update(m *Member) {
  107. // TODO: return error or just panic?
  108. if _, ok := h.senders[m.ID]; !ok {
  109. return
  110. }
  111. peerURL := m.PickPeerURL()
  112. u, err := url.Parse(peerURL)
  113. if err != nil {
  114. log.Panicf("unexpect peer url %s", peerURL)
  115. }
  116. u.Path = path.Join(u.Path, raftPrefix)
  117. h.senders[m.ID].Update(u.String())
  118. }
  119. // for testing
  120. func (h *sendHub) pause() {
  121. for _, s := range h.senders {
  122. s.Pause()
  123. }
  124. }
  125. func (h *sendHub) resume() {
  126. for _, s := range h.senders {
  127. s.Resume()
  128. }
  129. }