transport.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "log"
  17. "net/http"
  18. "net/url"
  19. "path"
  20. "sync"
  21. "github.com/coreos/etcd/etcdserver/stats"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. )
  26. type Raft interface {
  27. Process(ctx context.Context, m raftpb.Message) error
  28. }
  29. type Transporter interface {
  30. Handler() http.Handler
  31. Send(m []raftpb.Message)
  32. AddPeer(id types.ID, urls []string)
  33. RemovePeer(id types.ID)
  34. RemoveAllPeers()
  35. UpdatePeer(id types.ID, urls []string)
  36. Stop()
  37. }
  38. type transport struct {
  39. roundTripper http.RoundTripper
  40. id types.ID
  41. clusterID types.ID
  42. raft Raft
  43. serverStats *stats.ServerStats
  44. leaderStats *stats.LeaderStats
  45. mu sync.RWMutex // protect the peer map
  46. peers map[types.ID]*peer // remote peers
  47. errorc chan error
  48. }
  49. func NewTransporter(rt http.RoundTripper, id, cid types.ID, r Raft, errorc chan error, ss *stats.ServerStats, ls *stats.LeaderStats) Transporter {
  50. return &transport{
  51. roundTripper: rt,
  52. id: id,
  53. clusterID: cid,
  54. raft: r,
  55. serverStats: ss,
  56. leaderStats: ls,
  57. peers: make(map[types.ID]*peer),
  58. errorc: errorc,
  59. }
  60. }
  61. func (t *transport) Handler() http.Handler {
  62. h := NewHandler(t.raft, t.clusterID)
  63. sh := NewStreamHandler(t, t.id, t.clusterID)
  64. mux := http.NewServeMux()
  65. mux.Handle(RaftPrefix, h)
  66. mux.Handle(RaftStreamPrefix+"/", sh)
  67. return mux
  68. }
  69. func (t *transport) Peer(id types.ID) *peer {
  70. t.mu.RLock()
  71. defer t.mu.RUnlock()
  72. return t.peers[id]
  73. }
  74. func (t *transport) Send(msgs []raftpb.Message) {
  75. for _, m := range msgs {
  76. // intentionally dropped message
  77. if m.To == 0 {
  78. continue
  79. }
  80. to := types.ID(m.To)
  81. p, ok := t.peers[to]
  82. if !ok {
  83. log.Printf("etcdserver: send message to unknown receiver %s", to)
  84. continue
  85. }
  86. if m.Type == raftpb.MsgApp {
  87. t.serverStats.SendAppendReq(m.Size())
  88. }
  89. p.Send(m)
  90. }
  91. }
  92. func (t *transport) Stop() {
  93. for _, p := range t.peers {
  94. p.Stop()
  95. }
  96. if tr, ok := t.roundTripper.(*http.Transport); ok {
  97. tr.CloseIdleConnections()
  98. }
  99. }
  100. func (t *transport) AddPeer(id types.ID, urls []string) {
  101. t.mu.Lock()
  102. defer t.mu.Unlock()
  103. if _, ok := t.peers[id]; ok {
  104. return
  105. }
  106. // TODO: considering how to switch between all available peer urls
  107. peerURL := urls[0]
  108. u, err := url.Parse(peerURL)
  109. if err != nil {
  110. log.Panicf("unexpect peer url %s", peerURL)
  111. }
  112. u.Path = path.Join(u.Path, RaftPrefix)
  113. fs := t.leaderStats.Follower(id.String())
  114. t.peers[id] = NewPeer(t.roundTripper, u.String(), id, t.clusterID, t.raft, fs, t.errorc)
  115. }
  116. func (t *transport) RemovePeer(id types.ID) {
  117. t.mu.Lock()
  118. defer t.mu.Unlock()
  119. t.removePeer(id)
  120. }
  121. func (t *transport) RemoveAllPeers() {
  122. t.mu.Lock()
  123. defer t.mu.Unlock()
  124. for id, _ := range t.peers {
  125. t.removePeer(id)
  126. }
  127. }
  128. // the caller of this function must have the peers mutex.
  129. func (t *transport) removePeer(id types.ID) {
  130. if peer, ok := t.peers[id]; ok {
  131. peer.Stop()
  132. } else {
  133. log.Panicf("rafthttp: unexpected removal of unknown peer '%d'", id)
  134. }
  135. delete(t.peers, id)
  136. delete(t.leaderStats.Followers, id.String())
  137. }
  138. func (t *transport) UpdatePeer(id types.ID, urls []string) {
  139. t.mu.Lock()
  140. defer t.mu.Unlock()
  141. // TODO: return error or just panic?
  142. if _, ok := t.peers[id]; !ok {
  143. return
  144. }
  145. peerURL := urls[0]
  146. u, err := url.Parse(peerURL)
  147. if err != nil {
  148. log.Panicf("unexpect peer url %s", peerURL)
  149. }
  150. u.Path = path.Join(u.Path, RaftPrefix)
  151. t.peers[id].Update(u.String())
  152. }
  153. type Pausable interface {
  154. Pause()
  155. Resume()
  156. }
  157. // for testing
  158. func (t *transport) Pause() {
  159. for _, p := range t.peers {
  160. p.Pause()
  161. }
  162. }
  163. func (t *transport) Resume() {
  164. for _, p := range t.peers {
  165. p.Resume()
  166. }
  167. }