2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-11-16 21:21:05 +03:00
|
|
|
|
|
|
|
package rafthttp
|
|
|
|
|
|
|
|
import (
|
2015-02-06 20:12:32 +03:00
|
|
|
"log"
|
2014-11-16 21:21:05 +03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
2014-11-16 21:21:05 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/stats"
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-11-18 03:40:52 +03:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
2014-11-16 21:21:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-02-13 09:23:10 +03:00
|
|
|
DialTimeout = time.Second
|
2014-11-24 10:37:54 +03:00
|
|
|
ConnReadTimeout = 5 * time.Second
|
|
|
|
ConnWriteTimeout = 5 * time.Second
|
2015-02-15 19:44:34 +03:00
|
|
|
|
|
|
|
recvBufSize = 4096
|
2014-11-16 21:21:05 +03:00
|
|
|
)
|
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
// 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.
|
2014-12-29 04:44:26 +03:00
|
|
|
type peer struct {
|
2015-02-15 19:44:34 +03:00
|
|
|
id types.ID
|
2014-11-19 00:53:14 +03:00
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
msgAppWriter *streamWriter
|
|
|
|
writer *streamWriter
|
|
|
|
pipeline *pipeline
|
2014-12-15 07:00:32 +03:00
|
|
|
|
2015-02-06 20:12:32 +03:00
|
|
|
sendc chan raftpb.Message
|
2015-02-15 19:44:34 +03:00
|
|
|
recvc chan raftpb.Message
|
|
|
|
newURLc chan string
|
|
|
|
// for testing
|
2015-02-06 20:12:32 +03:00
|
|
|
pausec chan struct{}
|
|
|
|
resumec chan struct{}
|
2015-02-15 19:44:34 +03:00
|
|
|
|
|
|
|
stopc chan struct{}
|
|
|
|
done chan struct{}
|
2014-11-19 00:53:14 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
func startPeer(tr http.RoundTripper, u string, local, to, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error) *peer {
|
2015-02-06 20:12:32 +03:00
|
|
|
p := &peer{
|
2015-02-15 19:44:34 +03:00
|
|
|
id: to,
|
2015-03-01 18:02:24 +03:00
|
|
|
msgAppWriter: startStreamWriter(fs, r),
|
|
|
|
writer: startStreamWriter(fs, r),
|
|
|
|
pipeline: newPipeline(tr, u, to, cid, fs, r, errorc),
|
2015-02-15 19:44:34 +03:00
|
|
|
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{}),
|
2014-12-31 00:45:11 +03:00
|
|
|
}
|
2015-02-15 19:44:34 +03:00
|
|
|
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
|
2015-02-06 20:12:32 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-15 19:44:34 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
return p
|
2014-11-16 21:21:05 +03:00
|
|
|
}
|
|
|
|
|
2015-02-06 20:12:32 +03:00
|
|
|
func (p *peer) Send(m raftpb.Message) {
|
|
|
|
select {
|
|
|
|
case p.sendc <- m:
|
|
|
|
case <-p.done:
|
|
|
|
log.Panicf("peer: unexpected stopped")
|
2015-01-04 07:02:43 +03:00
|
|
|
}
|
2015-02-06 20:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peer) Update(u string) {
|
|
|
|
select {
|
2015-02-15 19:44:34 +03:00
|
|
|
case p.newURLc <- u:
|
2015-02-06 20:12:32 +03:00
|
|
|
case <-p.done:
|
|
|
|
log.Panicf("peer: unexpected stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
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()
|
2015-02-06 20:12:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause pauses the peer. The peer will simply drops all incoming
|
|
|
|
// messages without retruning an error.
|
|
|
|
func (p *peer) Pause() {
|
|
|
|
select {
|
|
|
|
case p.pausec <- struct{}{}:
|
|
|
|
case <-p.done:
|
2014-12-05 01:20:58 +03:00
|
|
|
}
|
2015-02-06 20:12:32 +03:00
|
|
|
}
|
2014-12-05 01:20:58 +03:00
|
|
|
|
2015-02-06 20:12:32 +03:00
|
|
|
// Resume resumes a paused peer.
|
|
|
|
func (p *peer) Resume() {
|
|
|
|
select {
|
|
|
|
case p.resumec <- struct{}{}:
|
|
|
|
case <-p.done:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop performs any necessary finalization and terminates the peer
|
|
|
|
// elegantly.
|
|
|
|
func (p *peer) Stop() {
|
2015-02-15 19:44:34 +03:00
|
|
|
close(p.stopc)
|
2015-02-06 20:12:32 +03:00
|
|
|
<-p.done
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:44:34 +03:00
|
|
|
func (p *peer) pick(m raftpb.Message) (writec chan raftpb.Message, name string, size int) {
|
2014-12-10 09:52:00 +03:00
|
|
|
switch {
|
2015-03-01 18:15:19 +03:00
|
|
|
// Considering MsgSnap may have a big size, e.g., 1G, and will block
|
|
|
|
// stream for a long time, only use one of the N pipelines to send MsgSnap.
|
|
|
|
case isMsgSnap(m):
|
|
|
|
writec = p.pipeline.msgc
|
|
|
|
name, size = "pipeline", pipelineBufSize
|
2015-02-15 19:44:34 +03:00
|
|
|
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
|
2014-12-10 09:52:00 +03:00
|
|
|
default:
|
2015-02-15 19:44:34 +03:00
|
|
|
writec = p.pipeline.msgc
|
|
|
|
name, size = "pipeline", pipelineBufSize
|
2014-11-19 00:53:14 +03:00
|
|
|
}
|
2015-02-15 19:44:34 +03:00
|
|
|
return
|
2014-12-10 09:52:00 +03:00
|
|
|
}
|
2015-03-01 18:15:19 +03:00
|
|
|
|
|
|
|
func isMsgSnap(m raftpb.Message) bool { return m.Type == raftpb.MsgSnap }
|