etcd/rafthttp/peer.go

259 lines
8.0 KiB
Go
Raw Normal View History

// 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.
package rafthttp
import (
2016-02-18 08:50:54 +03:00
"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"
"github.com/coreos/etcd/raft/raftpb"
2015-12-08 18:52:54 +03:00
"github.com/coreos/etcd/snap"
)
const (
2016-02-01 08:42:39 +03:00
// ConnReadTimeout and ConnWriteTimeout are the i/o timeout set on each connection rafthttp pkg creates.
2015-03-03 03:32:04 +03:00
// A 5 seconds timeout is good enough for recycling bad connections. Or we have to wait for
// tcp keepalive failing to detect a bad connection, which is at minutes level.
2016-02-01 08:42:39 +03:00
// For long term streaming connections, rafthttp pkg sends application level linkHeartbeatMessage
2015-03-03 03:32:04 +03:00
// to keep the connection alive.
2015-03-03 20:49:01 +03:00
// For short term pipeline connections, the connection MUST be killed to avoid it being
2015-03-03 03:32:04 +03:00
// put back to http pkg connection pool.
ConnReadTimeout = 5 * time.Second
ConnWriteTimeout = 5 * time.Second
recvBufSize = 4096
// maxPendingProposals holds the proposals during one leader election process.
// Generally one leader election takes at most 1 sec. It should have
// 0-2 election conflicts, and each one takes 0.5 sec.
// We assume the number of concurrent proposers is smaller than 4096.
// One client blocks on its proposal for at least 1 sec, so 4096 is enough
// to hold all proposals.
maxPendingProposals = 4096
streamAppV2 = "streamMsgAppV2"
streamMsg = "streamMsg"
pipelineMsg = "pipeline"
sendSnap = "sendMsgSnap"
)
2015-02-23 11:15:45 +03:00
type Peer interface {
2015-10-18 09:30:30 +03:00
// send sends the message to the remote peer. The function is non-blocking
2015-02-23 11:15:45 +03:00
// and has no promise that the message will be received by the remote.
// When it fails to send message out, it will report the status to underlying
// raft.
2015-10-18 09:30:30 +03:00
send(m raftpb.Message)
2015-12-08 18:52:54 +03:00
2016-02-01 08:42:39 +03:00
// sendSnap sends the merged snapshot message to the remote peer. Its behavior
2015-12-08 18:52:54 +03:00
// is similar to send.
sendSnap(m snap.Message)
2015-10-18 09:30:30 +03:00
// update updates the urls of remote peer.
update(urls types.URLs)
2016-01-06 11:17:02 +03:00
// attachOutgoingConn attaches the outgoing connection to the peer for
2015-02-23 11:15:45 +03:00
// stream usage. After the call, the ownership of the outgoing
// connection hands over to the peer. The peer will close the connection
// when it is no longer used.
attachOutgoingConn(conn *outgoingConn)
// activeSince returns the time that the connection with the
// peer becomes active.
activeSince() time.Time
2015-10-18 09:30:30 +03:00
// stop performs any necessary finalization and terminates the peer
2015-02-23 11:15:45 +03:00
// elegantly.
2015-10-18 09:30:30 +03:00
stop()
2015-02-23 11:15:45 +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-03-03 03:32:04 +03:00
// id of the remote raft peer node
id types.ID
r Raft
v3demo bool
status *peerStatus
picker *urlPicker
msgAppV2Writer *streamWriter
writer *streamWriter
pipeline *pipeline
snapSender *snapshotSender // snapshot sender to send v3 snapshot messages
msgAppV2Reader *streamReader
2016-02-18 08:50:54 +03:00
msgAppReader *streamReader
2016-02-18 08:50:54 +03:00
sendc chan raftpb.Message
recvc chan raftpb.Message
propc chan raftpb.Message
2015-03-03 03:32:04 +03:00
2016-02-18 08:50:54 +03:00
mu sync.Mutex
paused bool
2016-02-18 08:50:54 +03:00
cancel context.CancelFunc // cancel pending works in go routine created by peer.
stopc chan struct{}
}
func startPeer(transport *Transport, urls types.URLs, local, to, cid types.ID, r Raft, fs *stats.FollowerStats, errorc chan error, v3demo bool) *peer {
status := newPeerStatus(to)
picker := newURLPicker(urls)
2015-02-06 20:12:32 +03:00
p := &peer{
id: to,
r: r,
v3demo: v3demo,
status: status,
picker: picker,
msgAppV2Writer: startStreamWriter(to, status, fs, r),
writer: startStreamWriter(to, status, fs, r),
pipeline: newPipeline(transport, picker, local, to, cid, status, fs, r, errorc),
snapSender: newSnapshotSender(transport, picker, local, to, cid, status, r, errorc),
sendc: make(chan raftpb.Message),
recvc: make(chan raftpb.Message, recvBufSize),
propc: make(chan raftpb.Message, maxPendingProposals),
stopc: make(chan struct{}),
2014-12-31 00:45:11 +03:00
}
ctx, cancel := context.WithCancel(context.Background())
2016-02-18 08:50:54 +03:00
p.cancel = cancel
go func() {
for {
select {
case mm := <-p.propc:
if err := r.Process(ctx, mm); err != nil {
2015-06-03 01:33:58 +03:00
plog.Warningf("failed to process raft message (%v)", err)
}
case mm := <-p.recvc:
2016-02-18 08:50:54 +03:00
if err := r.Process(ctx, mm); err != nil {
2015-06-03 01:33:58 +03:00
plog.Warningf("failed to process raft message (%v)", err)
}
case <-p.stopc:
return
2015-02-06 20:12:32 +03:00
}
}
}()
2016-02-18 08:50:54 +03:00
p.msgAppV2Reader = startStreamReader(transport, picker, streamTypeMsgAppV2, local, to, cid, status, p.recvc, p.propc, errorc)
p.msgAppReader = startStreamReader(transport, picker, streamTypeMessage, local, to, cid, status, p.recvc, p.propc, errorc)
return p
}
2015-10-18 09:30:30 +03:00
func (p *peer) send(m raftpb.Message) {
2016-02-18 08:50:54 +03:00
p.mu.Lock()
paused := p.paused
p.mu.Unlock()
if paused {
return
}
writec, name := p.pick(m)
2015-02-06 20:12:32 +03:00
select {
2016-02-18 08:50:54 +03:00
case writec <- m:
default:
p.r.ReportUnreachable(m.To)
if isMsgSnap(m) {
p.r.ReportSnapshot(m.To, raft.SnapshotFailure)
}
if p.status.isActive() {
plog.MergeWarningf("dropped internal raft message to %s since %s's sending buffer is full (bad/overloaded network)", p.id, name)
}
plog.Debugf("dropped %s to %s since %s's sending buffer is full", m.Type, p.id, name)
}
2015-02-06 20:12:32 +03:00
}
2015-12-08 18:52:54 +03:00
func (p *peer) sendSnap(m snap.Message) {
go p.snapSender.send(m)
}
2015-10-18 09:30:30 +03:00
func (p *peer) update(urls types.URLs) {
2016-02-18 08:50:54 +03:00
p.picker.update(urls)
2015-02-06 20:12:32 +03:00
}
func (p *peer) attachOutgoingConn(conn *outgoingConn) {
var ok bool
switch conn.t {
case streamTypeMsgAppV2:
ok = p.msgAppV2Writer.attach(conn)
case streamTypeMessage:
ok = p.writer.attach(conn)
default:
2015-06-03 01:33:58 +03:00
plog.Panicf("unhandled stream type %s", conn.t)
}
if !ok {
conn.Close()
2015-02-06 20:12:32 +03:00
}
}
func (p *peer) activeSince() time.Time { return p.status.activeSince }
2015-02-06 20:12:32 +03:00
// Pause pauses the peer. The peer will simply drops all incoming
2016-01-06 11:17:02 +03:00
// messages without returning an error.
2015-02-06 20:12:32 +03:00
func (p *peer) Pause() {
2016-02-18 08:50:54 +03:00
p.mu.Lock()
defer p.mu.Unlock()
p.paused = true
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() {
2016-02-18 08:50:54 +03:00
p.mu.Lock()
defer p.mu.Unlock()
p.paused = false
2015-02-06 20:12:32 +03:00
}
2015-10-18 09:30:30 +03:00
func (p *peer) stop() {
close(p.stopc)
2016-02-18 08:50:54 +03:00
p.cancel()
p.msgAppV2Writer.stop()
p.writer.stop()
p.pipeline.stop()
p.snapSender.stop()
p.msgAppV2Reader.stop()
p.msgAppReader.stop()
2015-02-06 20:12:32 +03:00
}
// pick picks a chan for sending the given message. The picked chan and the picked chan
// string name are returned.
func (p *peer) pick(m raftpb.Message) (writec chan<- raftpb.Message, picked string) {
var ok bool
// 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.
if isMsgSnap(m) {
return p.pipeline.msgc, pipelineMsg
} else if writec, ok = p.msgAppV2Writer.writec(); ok && isMsgApp(m) {
return writec, streamAppV2
} else if writec, ok = p.writer.writec(); ok {
return writec, streamMsg
}
return p.pipeline.msgc, pipelineMsg
}
func isMsgApp(m raftpb.Message) bool { return m.Type == raftpb.MsgApp }
func isMsgSnap(m raftpb.Message) bool { return m.Type == raftpb.MsgSnap }