peer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "errors"
  17. "net/http"
  18. "sync"
  19. "time"
  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. appRespBatchMs = 50
  26. propBatchMs = 10
  27. ConnReadTimeout = 5 * time.Second
  28. ConnWriteTimeout = 5 * time.Second
  29. )
  30. type peer struct {
  31. sync.Mutex
  32. id types.ID
  33. cid types.ID
  34. tr http.RoundTripper
  35. // the url this sender post to
  36. u string
  37. r Raft
  38. fs *stats.FollowerStats
  39. batcher *Batcher
  40. propBatcher *ProposalBatcher
  41. pipeline *pipeline
  42. stream *stream
  43. paused bool
  44. stopped bool
  45. }
  46. func NewPeer(tr http.RoundTripper, u string, id types.ID, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error) *peer {
  47. return &peer{
  48. id: id,
  49. cid: cid,
  50. tr: tr,
  51. u: u,
  52. r: r,
  53. fs: fs,
  54. pipeline: newPipeline(tr, u, id, cid, fs, errorc),
  55. stream: &stream{},
  56. batcher: NewBatcher(100, appRespBatchMs*time.Millisecond),
  57. propBatcher: NewProposalBatcher(100, propBatchMs*time.Millisecond),
  58. }
  59. }
  60. func (p *peer) Update(u string) {
  61. p.Lock()
  62. defer p.Unlock()
  63. if p.stopped {
  64. // TODO: not panic here?
  65. panic("peer: update a stopped peer")
  66. }
  67. p.u = u
  68. p.pipeline.update(u)
  69. }
  70. // Send sends the data to the remote node. It is always non-blocking.
  71. // It may be fail to send data if it returns nil error.
  72. // TODO (xiangli): reasonable retry logic
  73. func (p *peer) Send(m raftpb.Message) error {
  74. p.Lock()
  75. defer p.Unlock()
  76. if p.stopped {
  77. return errors.New("peer: stopped")
  78. }
  79. if p.paused {
  80. return nil
  81. }
  82. // move all the stream related stuff into stream
  83. p.stream.invalidate(m.Term)
  84. if shouldInitStream(m) && !p.stream.isOpen() {
  85. u := p.u
  86. // todo: steam open should not block.
  87. p.stream.open(types.ID(m.From), p.id, p.cid, m.Term, p.tr, u, p.r)
  88. p.batcher.Reset(time.Now())
  89. }
  90. var err error
  91. switch {
  92. case isProposal(m):
  93. p.propBatcher.Batch(m)
  94. case canBatch(m) && p.stream.isOpen():
  95. if !p.batcher.ShouldBatch(time.Now()) {
  96. err = p.pipeline.send(m)
  97. }
  98. case canUseStream(m):
  99. if ok := p.stream.write(m); !ok {
  100. err = p.pipeline.send(m)
  101. }
  102. default:
  103. err = p.pipeline.send(m)
  104. }
  105. // send out batched MsgProp if needed
  106. // TODO: it is triggered by all outcoming send now, and it needs
  107. // more clear solution. Either use separate goroutine to trigger it
  108. // or use streaming.
  109. if !p.propBatcher.IsEmpty() {
  110. t := time.Now()
  111. if !p.propBatcher.ShouldBatch(t) {
  112. p.pipeline.send(p.propBatcher.Message)
  113. p.propBatcher.Reset(t)
  114. }
  115. }
  116. return err
  117. }
  118. // Stop performs any necessary finalization and terminates the peer
  119. // elegantly.
  120. func (p *peer) Stop() {
  121. p.Lock()
  122. defer p.Unlock()
  123. p.pipeline.stop()
  124. p.stream.stop()
  125. p.stopped = true
  126. }
  127. // attachStream attaches a streamSever to the peer.
  128. func (p *peer) attachStream(sw *streamWriter) error {
  129. p.Lock()
  130. defer p.Unlock()
  131. if p.stopped {
  132. return errors.New("peer: stopped")
  133. }
  134. sw.fs = p.fs
  135. return p.stream.attach(sw)
  136. }
  137. // Pause pauses the peer. The peer will simply drops all incoming
  138. // messages without retruning an error.
  139. func (p *peer) Pause() {
  140. p.Lock()
  141. defer p.Unlock()
  142. p.paused = true
  143. }
  144. // Resume resumes a paused peer.
  145. func (p *peer) Resume() {
  146. p.Lock()
  147. defer p.Unlock()
  148. p.paused = false
  149. }
  150. func isProposal(m raftpb.Message) bool { return m.Type == raftpb.MsgProp }