transport.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. "net/http"
  17. "sync"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/xiang90/probing"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. "github.com/coreos/etcd/etcdserver/stats"
  23. "github.com/coreos/etcd/pkg/logutil"
  24. "github.com/coreos/etcd/pkg/transport"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/raft"
  27. "github.com/coreos/etcd/raft/raftpb"
  28. "github.com/coreos/etcd/snap"
  29. )
  30. var plog = logutil.NewMergeLogger(capnslog.NewPackageLogger("github.com/coreos/etcd", "rafthttp"))
  31. type Raft interface {
  32. Process(ctx context.Context, m raftpb.Message) error
  33. IsIDRemoved(id uint64) bool
  34. ReportUnreachable(id uint64)
  35. ReportSnapshot(id uint64, status raft.SnapshotStatus)
  36. }
  37. type Transporter interface {
  38. // Start starts the given Transporter.
  39. // Start MUST be called before calling other functions in the interface.
  40. Start() error
  41. // Handler returns the HTTP handler of the transporter.
  42. // A transporter HTTP handler handles the HTTP requests
  43. // from remote peers.
  44. // The handler MUST be used to handle RaftPrefix(/raft)
  45. // endpoint.
  46. Handler() http.Handler
  47. // Send sends out the given messages to the remote peers.
  48. // Each message has a To field, which is an id that maps
  49. // to an existing peer in the transport.
  50. // If the id cannot be found in the transport, the message
  51. // will be ignored.
  52. Send(m []raftpb.Message)
  53. // SendSnapshot sends out the given snapshot message to a remote peer.
  54. // The behavior of SendSnapshot is similar to Send.
  55. SendSnapshot(m snap.Message)
  56. // AddRemote adds a remote with given peer urls into the transport.
  57. // A remote helps newly joined member to catch up the progress of cluster,
  58. // and will not be used after that.
  59. // It is the caller's responsibility to ensure the urls are all valid,
  60. // or it panics.
  61. AddRemote(id types.ID, urls []string)
  62. // AddPeer adds a peer with given peer urls into the transport.
  63. // It is the caller's responsibility to ensure the urls are all valid,
  64. // or it panics.
  65. // Peer urls are used to connect to the remote peer.
  66. AddPeer(id types.ID, urls []string)
  67. // RemovePeer removes the peer with given id.
  68. RemovePeer(id types.ID)
  69. // RemoveAllPeers removes all the existing peers in the transport.
  70. RemoveAllPeers()
  71. // UpdatePeer updates the peer urls of the peer with the given id.
  72. // It is the caller's responsibility to ensure the urls are all valid,
  73. // or it panics.
  74. UpdatePeer(id types.ID, urls []string)
  75. // ActiveSince returns the time that the connection with the peer
  76. // of the given id becomes active.
  77. // If the connection is active since peer was added, it returns the adding time.
  78. // If the connection is currently inactive, it returns zero time.
  79. ActiveSince(id types.ID) time.Time
  80. // Stop closes the connections and stops the transporter.
  81. Stop()
  82. }
  83. // Transport implements Transporter interface. It provides the functionality
  84. // to send raft messages to peers, and receive raft messages from peers.
  85. // User should call Handler method to get a handler to serve requests
  86. // received from peerURLs.
  87. // User needs to call Start before calling other functions, and call
  88. // Stop when the Transport is no longer used.
  89. type Transport struct {
  90. DialTimeout time.Duration // maximum duration before timing out dial of the request
  91. TLSInfo transport.TLSInfo // TLS information used when creating connection
  92. ID types.ID // local member ID
  93. URLs types.URLs // local peer URLs
  94. ClusterID types.ID // raft cluster ID for request validation
  95. Raft Raft // raft state machine, to which the Transport forwards received messages and reports status
  96. Snapshotter *snap.Snapshotter
  97. ServerStats *stats.ServerStats // used to record general transportation statistics
  98. // used to record transportation statistics with followers when
  99. // performing as leader in raft protocol
  100. LeaderStats *stats.LeaderStats
  101. // ErrorC is used to report detected critical errors, e.g.,
  102. // the member has been permanently removed from the cluster
  103. // When an error is received from ErrorC, user should stop raft state
  104. // machine and thus stop the Transport.
  105. ErrorC chan error
  106. V3demo bool
  107. streamRt http.RoundTripper // roundTripper used by streams
  108. pipelineRt http.RoundTripper // roundTripper used by pipelines
  109. mu sync.RWMutex // protect the remote and peer map
  110. remotes map[types.ID]*remote // remotes map that helps newly joined member to catch up
  111. peers map[types.ID]Peer // peers map
  112. prober probing.Prober
  113. }
  114. func (t *Transport) Start() error {
  115. var err error
  116. t.streamRt, err = newStreamRoundTripper(t.TLSInfo, t.DialTimeout)
  117. if err != nil {
  118. return err
  119. }
  120. t.pipelineRt, err = NewRoundTripper(t.TLSInfo, t.DialTimeout)
  121. if err != nil {
  122. return err
  123. }
  124. t.remotes = make(map[types.ID]*remote)
  125. t.peers = make(map[types.ID]Peer)
  126. t.prober = probing.NewProber(t.pipelineRt)
  127. return nil
  128. }
  129. func (t *Transport) Handler() http.Handler {
  130. pipelineHandler := newPipelineHandler(t, t.Raft, t.ClusterID)
  131. streamHandler := newStreamHandler(t, t, t.Raft, t.ID, t.ClusterID)
  132. snapHandler := newSnapshotHandler(t, t.Raft, t.Snapshotter, t.ClusterID)
  133. mux := http.NewServeMux()
  134. mux.Handle(RaftPrefix, pipelineHandler)
  135. mux.Handle(RaftStreamPrefix+"/", streamHandler)
  136. mux.Handle(RaftSnapshotPrefix, snapHandler)
  137. mux.Handle(ProbingPrefix, probing.NewHandler())
  138. return mux
  139. }
  140. func (t *Transport) Get(id types.ID) Peer {
  141. t.mu.RLock()
  142. defer t.mu.RUnlock()
  143. return t.peers[id]
  144. }
  145. func (t *Transport) Send(msgs []raftpb.Message) {
  146. for _, m := range msgs {
  147. if m.To == 0 {
  148. // ignore intentionally dropped message
  149. continue
  150. }
  151. to := types.ID(m.To)
  152. t.mu.RLock()
  153. p, ok := t.peers[to]
  154. t.mu.RUnlock()
  155. if ok {
  156. if m.Type == raftpb.MsgApp {
  157. t.ServerStats.SendAppendReq(m.Size())
  158. }
  159. p.send(m)
  160. continue
  161. }
  162. g, ok := t.remotes[to]
  163. if ok {
  164. g.send(m)
  165. continue
  166. }
  167. plog.Debugf("ignored message %s (sent to unknown peer %s)", m.Type, to)
  168. }
  169. }
  170. func (t *Transport) Stop() {
  171. t.mu.Lock()
  172. defer t.mu.Unlock()
  173. for _, r := range t.remotes {
  174. r.stop()
  175. }
  176. for _, p := range t.peers {
  177. p.stop()
  178. }
  179. t.prober.RemoveAll()
  180. if tr, ok := t.streamRt.(*http.Transport); ok {
  181. tr.CloseIdleConnections()
  182. }
  183. if tr, ok := t.pipelineRt.(*http.Transport); ok {
  184. tr.CloseIdleConnections()
  185. }
  186. }
  187. func (t *Transport) AddRemote(id types.ID, us []string) {
  188. t.mu.Lock()
  189. defer t.mu.Unlock()
  190. if _, ok := t.peers[id]; ok {
  191. return
  192. }
  193. if _, ok := t.remotes[id]; ok {
  194. return
  195. }
  196. urls, err := types.NewURLs(us)
  197. if err != nil {
  198. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  199. }
  200. t.remotes[id] = startRemote(t, urls, t.ID, id, t.ClusterID, t.Raft, t.ErrorC)
  201. }
  202. func (t *Transport) AddPeer(id types.ID, us []string) {
  203. t.mu.Lock()
  204. defer t.mu.Unlock()
  205. if _, ok := t.peers[id]; ok {
  206. return
  207. }
  208. urls, err := types.NewURLs(us)
  209. if err != nil {
  210. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  211. }
  212. fs := t.LeaderStats.Follower(id.String())
  213. t.peers[id] = startPeer(t, urls, t.ID, id, t.ClusterID, t.Raft, fs, t.ErrorC, t.V3demo)
  214. addPeerToProber(t.prober, id.String(), us)
  215. }
  216. func (t *Transport) RemovePeer(id types.ID) {
  217. t.mu.Lock()
  218. defer t.mu.Unlock()
  219. t.removePeer(id)
  220. }
  221. func (t *Transport) RemoveAllPeers() {
  222. t.mu.Lock()
  223. defer t.mu.Unlock()
  224. for id := range t.peers {
  225. t.removePeer(id)
  226. }
  227. }
  228. // the caller of this function must have the peers mutex.
  229. func (t *Transport) removePeer(id types.ID) {
  230. if peer, ok := t.peers[id]; ok {
  231. peer.stop()
  232. } else {
  233. plog.Panicf("unexpected removal of unknown peer '%d'", id)
  234. }
  235. delete(t.peers, id)
  236. delete(t.LeaderStats.Followers, id.String())
  237. t.prober.Remove(id.String())
  238. }
  239. func (t *Transport) UpdatePeer(id types.ID, us []string) {
  240. t.mu.Lock()
  241. defer t.mu.Unlock()
  242. // TODO: return error or just panic?
  243. if _, ok := t.peers[id]; !ok {
  244. return
  245. }
  246. urls, err := types.NewURLs(us)
  247. if err != nil {
  248. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  249. }
  250. t.peers[id].update(urls)
  251. t.prober.Remove(id.String())
  252. addPeerToProber(t.prober, id.String(), us)
  253. }
  254. func (t *Transport) ActiveSince(id types.ID) time.Time {
  255. t.mu.Lock()
  256. defer t.mu.Unlock()
  257. if p, ok := t.peers[id]; ok {
  258. return p.activeSince()
  259. }
  260. return time.Time{}
  261. }
  262. func (t *Transport) SendSnapshot(m snap.Message) {
  263. t.mu.Lock()
  264. defer t.mu.Unlock()
  265. p := t.peers[types.ID(m.To)]
  266. if p == nil {
  267. m.CloseWithError(errMemberNotFound)
  268. return
  269. }
  270. p.sendSnap(m)
  271. }
  272. // Pausable is a testing interface for pausing transport traffic.
  273. type Pausable interface {
  274. Pause()
  275. Resume()
  276. }
  277. func (t *Transport) Pause() {
  278. for _, p := range t.peers {
  279. p.(Pausable).Pause()
  280. }
  281. }
  282. func (t *Transport) Resume() {
  283. for _, p := range t.peers {
  284. p.(Pausable).Resume()
  285. }
  286. }
  287. type nopTransporter struct{}
  288. func NewNopTransporter() Transporter {
  289. return &nopTransporter{}
  290. }
  291. func (s *nopTransporter) Start() error { return nil }
  292. func (s *nopTransporter) Handler() http.Handler { return nil }
  293. func (s *nopTransporter) Send(m []raftpb.Message) {}
  294. func (s *nopTransporter) SendSnapshot(m snap.Message) {}
  295. func (s *nopTransporter) AddRemote(id types.ID, us []string) {}
  296. func (s *nopTransporter) AddPeer(id types.ID, us []string) {}
  297. func (s *nopTransporter) RemovePeer(id types.ID) {}
  298. func (s *nopTransporter) RemoveAllPeers() {}
  299. func (s *nopTransporter) UpdatePeer(id types.ID, us []string) {}
  300. func (s *nopTransporter) ActiveSince(id types.ID) time.Time { return time.Time{} }
  301. func (s *nopTransporter) Stop() {}
  302. func (s *nopTransporter) Pause() {}
  303. func (s *nopTransporter) Resume() {}
  304. type snapTransporter struct {
  305. nopTransporter
  306. snapDoneC chan snap.Message
  307. snapDir string
  308. }
  309. func NewSnapTransporter(snapDir string) (Transporter, <-chan snap.Message) {
  310. ch := make(chan snap.Message, 1)
  311. tr := &snapTransporter{snapDoneC: ch, snapDir: snapDir}
  312. return tr, ch
  313. }
  314. func (s *snapTransporter) SendSnapshot(m snap.Message) {
  315. ss := snap.New(s.snapDir)
  316. ss.SaveDBFrom(m.ReadCloser, m.Snapshot.Metadata.Index+1)
  317. m.CloseWithError(nil)
  318. s.snapDoneC <- m
  319. }