peer.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/etcdserver/stats"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. )
  24. const (
  25. DialTimeout = time.Second
  26. ConnReadTimeout = 5 * time.Second
  27. ConnWriteTimeout = 5 * time.Second
  28. recvBufSize = 4096
  29. )
  30. // peer is the representative of a remote raft node. Local raft node sends
  31. // messages to the remote through peer.
  32. // Each peer has two underlying mechanisms to send out a message: stream and
  33. // pipeline.
  34. // A stream is a receiver initialized long-polling connection, which
  35. // is always open to transfer messages. Besides general stream, peer also has
  36. // a optimized stream for sending msgApp since msgApp accounts for large part
  37. // of all messages. Only raft leader uses the optimized stream to send msgApp
  38. // to the remote follower node.
  39. // A pipeline is a series of http clients that send http requests to the remote.
  40. // It is only used when the stream has not been established.
  41. type peer struct {
  42. id types.ID
  43. msgAppWriter *streamWriter
  44. writer *streamWriter
  45. pipeline *pipeline
  46. sendc chan raftpb.Message
  47. recvc chan raftpb.Message
  48. newURLc chan string
  49. // for testing
  50. pausec chan struct{}
  51. resumec chan struct{}
  52. stopc chan struct{}
  53. done chan struct{}
  54. }
  55. func startPeer(tr http.RoundTripper, u string, local, to, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error) *peer {
  56. p := &peer{
  57. id: to,
  58. msgAppWriter: startStreamWriter(fs),
  59. writer: startStreamWriter(fs),
  60. pipeline: newPipeline(tr, u, to, cid, fs, errorc),
  61. sendc: make(chan raftpb.Message),
  62. recvc: make(chan raftpb.Message, recvBufSize),
  63. newURLc: make(chan string),
  64. pausec: make(chan struct{}),
  65. resumec: make(chan struct{}),
  66. stopc: make(chan struct{}),
  67. done: make(chan struct{}),
  68. }
  69. go func() {
  70. var paused bool
  71. msgAppReader := startStreamReader(tr, u, streamTypeMsgApp, local, to, cid, p.recvc)
  72. reader := startStreamReader(tr, u, streamTypeMessage, local, to, cid, p.recvc)
  73. for {
  74. select {
  75. case m := <-p.sendc:
  76. if paused {
  77. continue
  78. }
  79. writec, name, size := p.pick(m)
  80. select {
  81. case writec <- m:
  82. default:
  83. log.Printf("peer: dropping %s to %s since %s with %d-size buffer is blocked",
  84. m.Type, p.id, name, size)
  85. }
  86. case mm := <-p.recvc:
  87. if mm.Type == raftpb.MsgApp {
  88. msgAppReader.updateMsgAppTerm(mm.Term)
  89. }
  90. if err := r.Process(context.TODO(), mm); err != nil {
  91. log.Printf("peer: process raft message error: %v", err)
  92. }
  93. case u := <-p.newURLc:
  94. msgAppReader.update(u)
  95. reader.update(u)
  96. p.pipeline.update(u)
  97. case <-p.pausec:
  98. paused = true
  99. case <-p.resumec:
  100. paused = false
  101. case <-p.stopc:
  102. p.msgAppWriter.stop()
  103. p.writer.stop()
  104. p.pipeline.stop()
  105. msgAppReader.stop()
  106. reader.stop()
  107. close(p.done)
  108. return
  109. }
  110. }
  111. }()
  112. return p
  113. }
  114. func (p *peer) Send(m raftpb.Message) {
  115. select {
  116. case p.sendc <- m:
  117. case <-p.done:
  118. log.Panicf("peer: unexpected stopped")
  119. }
  120. }
  121. func (p *peer) Update(u string) {
  122. select {
  123. case p.newURLc <- u:
  124. case <-p.done:
  125. log.Panicf("peer: unexpected stopped")
  126. }
  127. }
  128. func (p *peer) attachOutgoingConn(conn *outgoingConn) {
  129. var ok bool
  130. switch conn.t {
  131. case streamTypeMsgApp:
  132. ok = p.msgAppWriter.attach(conn)
  133. case streamTypeMessage:
  134. ok = p.writer.attach(conn)
  135. default:
  136. log.Panicf("rafthttp: unhandled stream type %s", conn.t)
  137. }
  138. if !ok {
  139. conn.Close()
  140. }
  141. }
  142. // Pause pauses the peer. The peer will simply drops all incoming
  143. // messages without retruning an error.
  144. func (p *peer) Pause() {
  145. select {
  146. case p.pausec <- struct{}{}:
  147. case <-p.done:
  148. }
  149. }
  150. // Resume resumes a paused peer.
  151. func (p *peer) Resume() {
  152. select {
  153. case p.resumec <- struct{}{}:
  154. case <-p.done:
  155. }
  156. }
  157. // Stop performs any necessary finalization and terminates the peer
  158. // elegantly.
  159. func (p *peer) Stop() {
  160. close(p.stopc)
  161. <-p.done
  162. }
  163. func (p *peer) pick(m raftpb.Message) (writec chan raftpb.Message, name string, size int) {
  164. switch {
  165. case p.msgAppWriter.isWorking() && canUseMsgAppStream(m):
  166. writec = p.msgAppWriter.msgc
  167. name, size = "msgapp stream", streamBufSize
  168. case p.writer.isWorking():
  169. writec = p.writer.msgc
  170. name, size = "general stream", streamBufSize
  171. default:
  172. writec = p.pipeline.msgc
  173. name, size = "pipeline", pipelineBufSize
  174. }
  175. return
  176. }