sendhub.go 2.9 KB

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