|
|
@@ -15,109 +15,112 @@
|
|
|
package rafthttp
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
- "sync"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
|
"github.com/coreos/etcd/etcdserver/stats"
|
|
|
"github.com/coreos/etcd/pkg/types"
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- appRespBatchMs = 50
|
|
|
- propBatchMs = 10
|
|
|
-
|
|
|
DialTimeout = time.Second
|
|
|
ConnReadTimeout = 5 * time.Second
|
|
|
ConnWriteTimeout = 5 * time.Second
|
|
|
+
|
|
|
+ recvBufSize = 4096
|
|
|
)
|
|
|
|
|
|
+// peer is the representative of a remote raft node. Local raft node sends
|
|
|
+// messages to the remote through peer.
|
|
|
+// Each peer has two underlying mechanisms to send out a message: stream and
|
|
|
+// pipeline.
|
|
|
+// A stream is a receiver initialized long-polling connection, which
|
|
|
+// is always open to transfer messages. Besides general stream, peer also has
|
|
|
+// a optimized stream for sending msgApp since msgApp accounts for large part
|
|
|
+// of all messages. Only raft leader uses the optimized stream to send msgApp
|
|
|
+// to the remote follower node.
|
|
|
+// A pipeline is a series of http clients that send http requests to the remote.
|
|
|
+// It is only used when the stream has not been established.
|
|
|
type peer struct {
|
|
|
- sync.Mutex
|
|
|
-
|
|
|
- id types.ID
|
|
|
- cid types.ID
|
|
|
-
|
|
|
- tr http.RoundTripper
|
|
|
- // the url this sender post to
|
|
|
- u string
|
|
|
- r Raft
|
|
|
- fs *stats.FollowerStats
|
|
|
+ id types.ID
|
|
|
|
|
|
- batcher *Batcher
|
|
|
- propBatcher *ProposalBatcher
|
|
|
-
|
|
|
- pipeline *pipeline
|
|
|
- stream *stream
|
|
|
+ msgAppWriter *streamWriter
|
|
|
+ writer *streamWriter
|
|
|
+ pipeline *pipeline
|
|
|
|
|
|
sendc chan raftpb.Message
|
|
|
- updatec chan string
|
|
|
- attachc chan *streamWriter
|
|
|
+ recvc chan raftpb.Message
|
|
|
+ newURLc chan string
|
|
|
+ // for testing
|
|
|
pausec chan struct{}
|
|
|
resumec chan struct{}
|
|
|
- stopc chan struct{}
|
|
|
- done chan struct{}
|
|
|
+
|
|
|
+ stopc chan struct{}
|
|
|
+ done chan struct{}
|
|
|
}
|
|
|
|
|
|
-func NewPeer(tr http.RoundTripper, u string, id types.ID, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error) *peer {
|
|
|
+func startPeer(tr http.RoundTripper, u string, local, to, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error) *peer {
|
|
|
p := &peer{
|
|
|
- id: id,
|
|
|
- cid: cid,
|
|
|
- tr: tr,
|
|
|
- u: u,
|
|
|
- r: r,
|
|
|
- fs: fs,
|
|
|
- pipeline: newPipeline(tr, u, id, cid, fs, errorc),
|
|
|
- stream: &stream{},
|
|
|
- batcher: NewBatcher(100, appRespBatchMs*time.Millisecond),
|
|
|
- propBatcher: NewProposalBatcher(100, propBatchMs*time.Millisecond),
|
|
|
-
|
|
|
- sendc: make(chan raftpb.Message),
|
|
|
- updatec: make(chan string),
|
|
|
- attachc: make(chan *streamWriter),
|
|
|
- pausec: make(chan struct{}),
|
|
|
- resumec: make(chan struct{}),
|
|
|
- stopc: make(chan struct{}),
|
|
|
- done: make(chan struct{}),
|
|
|
+ id: to,
|
|
|
+ msgAppWriter: startStreamWriter(fs),
|
|
|
+ writer: startStreamWriter(fs),
|
|
|
+ pipeline: newPipeline(tr, u, to, cid, fs, errorc),
|
|
|
+ sendc: make(chan raftpb.Message),
|
|
|
+ recvc: make(chan raftpb.Message, recvBufSize),
|
|
|
+ newURLc: make(chan string),
|
|
|
+ pausec: make(chan struct{}),
|
|
|
+ resumec: make(chan struct{}),
|
|
|
+ stopc: make(chan struct{}),
|
|
|
+ done: make(chan struct{}),
|
|
|
}
|
|
|
- go p.run()
|
|
|
- return p
|
|
|
-}
|
|
|
-
|
|
|
-func (p *peer) run() {
|
|
|
- var paused bool
|
|
|
- // non-blocking main loop
|
|
|
- for {
|
|
|
- select {
|
|
|
- case m := <-p.sendc:
|
|
|
- if paused {
|
|
|
- continue
|
|
|
+ go func() {
|
|
|
+ var paused bool
|
|
|
+ msgAppReader := startStreamReader(tr, u, streamTypeMsgApp, local, to, cid, p.recvc)
|
|
|
+ reader := startStreamReader(tr, u, streamTypeMessage, local, to, cid, p.recvc)
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case m := <-p.sendc:
|
|
|
+ if paused {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ writec, name, size := p.pick(m)
|
|
|
+ select {
|
|
|
+ case writec <- m:
|
|
|
+ default:
|
|
|
+ log.Printf("peer: dropping %s to %s since %s with %d-size buffer is blocked",
|
|
|
+ m.Type, p.id, name, size)
|
|
|
+ }
|
|
|
+ case mm := <-p.recvc:
|
|
|
+ if mm.Type == raftpb.MsgApp {
|
|
|
+ msgAppReader.updateMsgAppTerm(mm.Term)
|
|
|
+ }
|
|
|
+ if err := r.Process(context.TODO(), mm); err != nil {
|
|
|
+ log.Printf("peer: process raft message error: %v", err)
|
|
|
+ }
|
|
|
+ case u := <-p.newURLc:
|
|
|
+ msgAppReader.update(u)
|
|
|
+ reader.update(u)
|
|
|
+ p.pipeline.update(u)
|
|
|
+ case <-p.pausec:
|
|
|
+ paused = true
|
|
|
+ case <-p.resumec:
|
|
|
+ paused = false
|
|
|
+ case <-p.stopc:
|
|
|
+ p.msgAppWriter.stop()
|
|
|
+ p.writer.stop()
|
|
|
+ p.pipeline.stop()
|
|
|
+ msgAppReader.stop()
|
|
|
+ reader.stop()
|
|
|
+ close(p.done)
|
|
|
+ return
|
|
|
}
|
|
|
- p.send(m)
|
|
|
- case u := <-p.updatec:
|
|
|
- p.u = u
|
|
|
- p.pipeline.update(u)
|
|
|
- case sw := <-p.attachc:
|
|
|
- sw.fs = p.fs
|
|
|
- if err := p.stream.attach(sw); err != nil {
|
|
|
- sw.stop()
|
|
|
- continue
|
|
|
- }
|
|
|
- go sw.handle()
|
|
|
- case <-p.pausec:
|
|
|
- paused = true
|
|
|
- case <-p.resumec:
|
|
|
- paused = false
|
|
|
- case <-p.stopc:
|
|
|
- p.pipeline.stop()
|
|
|
- p.stream.stop()
|
|
|
- close(p.done)
|
|
|
- return
|
|
|
}
|
|
|
- }
|
|
|
+ }()
|
|
|
+
|
|
|
+ return p
|
|
|
}
|
|
|
|
|
|
func (p *peer) Send(m raftpb.Message) {
|
|
|
@@ -130,20 +133,24 @@ func (p *peer) Send(m raftpb.Message) {
|
|
|
|
|
|
func (p *peer) Update(u string) {
|
|
|
select {
|
|
|
- case p.updatec <- u:
|
|
|
+ case p.newURLc <- u:
|
|
|
case <-p.done:
|
|
|
log.Panicf("peer: unexpected stopped")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// attachStream attaches a streamWriter to the peer.
|
|
|
-// If attach succeeds, peer will take charge of the given streamWriter.
|
|
|
-func (p *peer) attachStream(sw *streamWriter) error {
|
|
|
- select {
|
|
|
- case p.attachc <- sw:
|
|
|
- return nil
|
|
|
- case <-p.done:
|
|
|
- return fmt.Errorf("peer: stopped")
|
|
|
+func (p *peer) attachOutgoingConn(conn *outgoingConn) {
|
|
|
+ var ok bool
|
|
|
+ switch conn.t {
|
|
|
+ case streamTypeMsgApp:
|
|
|
+ ok = p.msgAppWriter.attach(conn)
|
|
|
+ case streamTypeMessage:
|
|
|
+ ok = p.writer.attach(conn)
|
|
|
+ default:
|
|
|
+ log.Panicf("rafthttp: unhandled stream type %s", conn.t)
|
|
|
+ }
|
|
|
+ if !ok {
|
|
|
+ conn.Close()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -167,54 +174,21 @@ func (p *peer) Resume() {
|
|
|
// Stop performs any necessary finalization and terminates the peer
|
|
|
// elegantly.
|
|
|
func (p *peer) Stop() {
|
|
|
- select {
|
|
|
- case p.stopc <- struct{}{}:
|
|
|
- case <-p.done:
|
|
|
- return
|
|
|
- }
|
|
|
+ close(p.stopc)
|
|
|
<-p.done
|
|
|
}
|
|
|
|
|
|
-// send sends the data to the remote node. It is always non-blocking.
|
|
|
-// It may be fail to send data if it returns nil error.
|
|
|
-// TODO (xiangli): reasonable retry logic
|
|
|
-func (p *peer) send(m raftpb.Message) error {
|
|
|
- // move all the stream related stuff into stream
|
|
|
- p.stream.invalidate(m.Term)
|
|
|
- if shouldInitStream(m) && !p.stream.isOpen() {
|
|
|
- u := p.u
|
|
|
- // todo: steam open should not block.
|
|
|
- p.stream.open(types.ID(m.From), p.id, p.cid, m.Term, p.tr, u, p.r)
|
|
|
- p.batcher.Reset(time.Now())
|
|
|
- }
|
|
|
-
|
|
|
- var err error
|
|
|
+func (p *peer) pick(m raftpb.Message) (writec chan raftpb.Message, name string, size int) {
|
|
|
switch {
|
|
|
- case isProposal(m):
|
|
|
- p.propBatcher.Batch(m)
|
|
|
- case canBatch(m) && p.stream.isOpen():
|
|
|
- if !p.batcher.ShouldBatch(time.Now()) {
|
|
|
- err = p.pipeline.send(m)
|
|
|
- }
|
|
|
- case canUseStream(m):
|
|
|
- if ok := p.stream.write(m); !ok {
|
|
|
- err = p.pipeline.send(m)
|
|
|
- }
|
|
|
+ case p.msgAppWriter.isWorking() && canUseMsgAppStream(m):
|
|
|
+ writec = p.msgAppWriter.msgc
|
|
|
+ name, size = "msgapp stream", streamBufSize
|
|
|
+ case p.writer.isWorking():
|
|
|
+ writec = p.writer.msgc
|
|
|
+ name, size = "general stream", streamBufSize
|
|
|
default:
|
|
|
- err = p.pipeline.send(m)
|
|
|
+ writec = p.pipeline.msgc
|
|
|
+ name, size = "pipeline", pipelineBufSize
|
|
|
}
|
|
|
- // send out batched MsgProp if needed
|
|
|
- // TODO: it is triggered by all outcoming send now, and it needs
|
|
|
- // more clear solution. Either use separate goroutine to trigger it
|
|
|
- // or use streaming.
|
|
|
- if !p.propBatcher.IsEmpty() {
|
|
|
- t := time.Now()
|
|
|
- if !p.propBatcher.ShouldBatch(t) {
|
|
|
- p.pipeline.send(p.propBatcher.Message)
|
|
|
- p.propBatcher.Reset(t)
|
|
|
- }
|
|
|
- }
|
|
|
- return err
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
-func isProposal(m raftpb.Message) bool { return m.Type == raftpb.MsgProp }
|