sendhub.go 3.0 KB

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