transport.go 9.5 KB

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