transport.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // error channel used to report detected critical error, 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. for _, r := range t.remotes {
  172. r.stop()
  173. }
  174. for _, p := range t.peers {
  175. p.stop()
  176. }
  177. t.prober.RemoveAll()
  178. if tr, ok := t.streamRt.(*http.Transport); ok {
  179. tr.CloseIdleConnections()
  180. }
  181. if tr, ok := t.pipelineRt.(*http.Transport); ok {
  182. tr.CloseIdleConnections()
  183. }
  184. }
  185. func (t *Transport) AddRemote(id types.ID, us []string) {
  186. t.mu.Lock()
  187. defer t.mu.Unlock()
  188. if _, ok := t.peers[id]; ok {
  189. return
  190. }
  191. if _, ok := t.remotes[id]; ok {
  192. return
  193. }
  194. urls, err := types.NewURLs(us)
  195. if err != nil {
  196. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  197. }
  198. t.remotes[id] = startRemote(t, urls, t.ID, id, t.ClusterID, t.Raft, t.ErrorC)
  199. }
  200. func (t *Transport) AddPeer(id types.ID, us []string) {
  201. t.mu.Lock()
  202. defer t.mu.Unlock()
  203. if _, ok := t.peers[id]; ok {
  204. return
  205. }
  206. urls, err := types.NewURLs(us)
  207. if err != nil {
  208. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  209. }
  210. fs := t.LeaderStats.Follower(id.String())
  211. t.peers[id] = startPeer(t, urls, t.ID, id, t.ClusterID, t.Raft, fs, t.ErrorC, t.V3demo)
  212. addPeerToProber(t.prober, id.String(), us)
  213. }
  214. func (t *Transport) RemovePeer(id types.ID) {
  215. t.mu.Lock()
  216. defer t.mu.Unlock()
  217. t.removePeer(id)
  218. }
  219. func (t *Transport) RemoveAllPeers() {
  220. t.mu.Lock()
  221. defer t.mu.Unlock()
  222. for id := range t.peers {
  223. t.removePeer(id)
  224. }
  225. }
  226. // the caller of this function must have the peers mutex.
  227. func (t *Transport) removePeer(id types.ID) {
  228. if peer, ok := t.peers[id]; ok {
  229. peer.stop()
  230. } else {
  231. plog.Panicf("unexpected removal of unknown peer '%d'", id)
  232. }
  233. delete(t.peers, id)
  234. delete(t.LeaderStats.Followers, id.String())
  235. t.prober.Remove(id.String())
  236. }
  237. func (t *Transport) UpdatePeer(id types.ID, us []string) {
  238. t.mu.Lock()
  239. defer t.mu.Unlock()
  240. // TODO: return error or just panic?
  241. if _, ok := t.peers[id]; !ok {
  242. return
  243. }
  244. urls, err := types.NewURLs(us)
  245. if err != nil {
  246. plog.Panicf("newURLs %+v should never fail: %+v", us, err)
  247. }
  248. t.peers[id].update(urls)
  249. t.prober.Remove(id.String())
  250. addPeerToProber(t.prober, id.String(), us)
  251. }
  252. func (t *Transport) ActiveSince(id types.ID) time.Time {
  253. t.mu.Lock()
  254. defer t.mu.Unlock()
  255. if p, ok := t.peers[id]; ok {
  256. return p.activeSince()
  257. }
  258. return time.Time{}
  259. }
  260. func (t *Transport) SendSnapshot(m snap.Message) {
  261. p := t.peers[types.ID(m.To)]
  262. if p == nil {
  263. m.CloseWithError(errMemberNotFound)
  264. return
  265. }
  266. p.sendSnap(m)
  267. }
  268. type Pausable interface {
  269. Pause()
  270. Resume()
  271. }
  272. // for testing
  273. func (t *Transport) Pause() {
  274. for _, p := range t.peers {
  275. p.(Pausable).Pause()
  276. }
  277. }
  278. func (t *Transport) Resume() {
  279. for _, p := range t.peers {
  280. p.(Pausable).Resume()
  281. }
  282. }
  283. type nopTransporter struct{}
  284. func NewNopTransporter() Transporter {
  285. return &nopTransporter{}
  286. }
  287. func (s *nopTransporter) Start() error { return nil }
  288. func (s *nopTransporter) Handler() http.Handler { return nil }
  289. func (s *nopTransporter) Send(m []raftpb.Message) {}
  290. func (s *nopTransporter) SendSnapshot(m snap.Message) {}
  291. func (s *nopTransporter) AddRemote(id types.ID, us []string) {}
  292. func (s *nopTransporter) AddPeer(id types.ID, us []string) {}
  293. func (s *nopTransporter) RemovePeer(id types.ID) {}
  294. func (s *nopTransporter) RemoveAllPeers() {}
  295. func (s *nopTransporter) UpdatePeer(id types.ID, us []string) {}
  296. func (s *nopTransporter) ActiveSince(id types.ID) time.Time { return time.Time{} }
  297. func (s *nopTransporter) Stop() {}
  298. func (s *nopTransporter) Pause() {}
  299. func (s *nopTransporter) Resume() {}
  300. type snapTransporter struct {
  301. nopTransporter
  302. snapDoneC chan snap.Message
  303. snapDir string
  304. }
  305. func NewSnapTransporter(snapDir string) (Transporter, <-chan snap.Message) {
  306. ch := make(chan snap.Message, 1)
  307. tr := &snapTransporter{snapDoneC: ch, snapDir: snapDir}
  308. return tr, ch
  309. }
  310. func (s *snapTransporter) SendSnapshot(m snap.Message) {
  311. ss := snap.New(s.snapDir)
  312. ss.SaveDBFrom(m.ReadCloser, m.Snapshot.Metadata.Index+1)
  313. m.CloseWithError(nil)
  314. s.snapDoneC <- m
  315. }