etcd/rafthttp/pipeline.go

159 lines
3.9 KiB
Go
Raw Normal View History

2016-05-13 06:49:28 +03:00
// Copyright 2015 The etcd Authors
2015-02-05 20:40:46 +03:00
//
// 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 (
"bytes"
"errors"
"io/ioutil"
2015-02-05 20:40:46 +03:00
"sync"
"time"
"github.com/coreos/etcd/etcdserver/stats"
"github.com/coreos/etcd/pkg/httputil"
2015-02-05 20:40:46 +03:00
"github.com/coreos/etcd/pkg/pbutil"
"github.com/coreos/etcd/pkg/types"
2015-03-01 19:17:14 +03:00
"github.com/coreos/etcd/raft"
2015-02-05 20:40:46 +03:00
"github.com/coreos/etcd/raft/raftpb"
)
const (
connPerPipeline = 4
// pipelineBufSize is the size of pipeline buffer, which helps hold the
// temporary network latency.
// The size ensures that pipeline does not drop messages when the network
// is out of work for less than 1 second in good path.
pipelineBufSize = 64
)
var errStopped = errors.New("stopped")
2015-02-05 20:40:46 +03:00
type pipeline struct {
2016-06-02 08:12:47 +03:00
peerID types.ID
2015-02-05 20:40:46 +03:00
tr *Transport
2015-02-27 18:54:06 +03:00
picker *urlPicker
status *peerStatus
raft Raft
2015-02-05 20:40:46 +03:00
errorc chan error
// deprecate when we depercate v2 API
followerStats *stats.FollowerStats
2015-02-05 20:40:46 +03:00
msgc chan raftpb.Message
2015-02-05 20:40:46 +03:00
// wait for the handling routines
wg sync.WaitGroup
stopc chan struct{}
2015-02-05 20:40:46 +03:00
}
func (p *pipeline) start() {
p.stopc = make(chan struct{})
p.msgc = make(chan raftpb.Message, pipelineBufSize)
2015-02-05 20:40:46 +03:00
p.wg.Add(connPerPipeline)
for i := 0; i < connPerPipeline; i++ {
go p.handle()
}
2016-06-02 08:12:47 +03:00
plog.Infof("started HTTP pipelining with peer %s", p.peerID)
2015-02-05 20:40:46 +03:00
}
func (p *pipeline) stop() {
close(p.stopc)
2015-02-05 20:40:46 +03:00
p.wg.Wait()
2016-06-02 08:12:47 +03:00
plog.Infof("stopped HTTP pipelining with peer %s", p.peerID)
2015-02-05 20:40:46 +03:00
}
func (p *pipeline) handle() {
defer p.wg.Done()
for {
select {
case m := <-p.msgc:
start := time.Now()
err := p.post(pbutil.MustMarshal(&m))
end := time.Now()
if err != nil {
p.status.deactivate(failureType{source: pipelineMsg, action: "write"}, err.Error())
if m.Type == raftpb.MsgApp && p.followerStats != nil {
p.followerStats.Fail()
}
p.raft.ReportUnreachable(m.To)
if isMsgSnap(m) {
p.raft.ReportSnapshot(m.To, raft.SnapshotFailure)
}
continue
}
2015-10-18 09:30:30 +03:00
p.status.activate()
if m.Type == raftpb.MsgApp && p.followerStats != nil {
p.followerStats.Succ(end.Sub(start))
2015-02-05 20:40:46 +03:00
}
2015-03-01 19:17:14 +03:00
if isMsgSnap(m) {
p.raft.ReportSnapshot(m.To, raft.SnapshotFinish)
2015-03-01 19:17:14 +03:00
}
2016-05-07 04:02:55 +03:00
sentBytes.WithLabelValues(types.ID(m.To).String()).Add(float64(m.Size()))
case <-p.stopc:
return
2015-02-05 20:40:46 +03:00
}
}
}
// post POSTs a data payload to a url. Returns nil if the POST succeeds,
// error on any failure.
func (p *pipeline) post(data []byte) (err error) {
2015-02-27 18:54:06 +03:00
u := p.picker.pick()
req := createPostRequest(u, RaftPrefix, bytes.NewBuffer(data), "application/protobuf", p.tr.URLs, p.tr.ID, p.tr.ClusterID)
done := make(chan struct{}, 1)
cancel := httputil.RequestCanceler(p.tr.pipelineRt, req)
go func() {
select {
case <-done:
case <-p.stopc:
waitSchedule()
cancel()
}
}()
resp, err := p.tr.pipelineRt.RoundTrip(req)
done <- struct{}{}
2015-02-05 20:40:46 +03:00
if err != nil {
2015-02-27 18:54:06 +03:00
p.picker.unreachable(u)
2015-02-05 20:40:46 +03:00
return err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
p.picker.unreachable(u)
return err
}
2015-02-05 20:40:46 +03:00
resp.Body.Close()
2016-06-02 08:12:47 +03:00
err = checkPostResponse(resp, b, req, p.peerID)
if err != nil {
p.picker.unreachable(u)
// errMemberRemoved is a critical error since a removed member should
// always be stopped. So we use reportCriticalError to report it to errorc.
if err == errMemberRemoved {
reportCriticalError(err, p.errorc)
}
return err
2015-02-05 20:40:46 +03:00
}
return nil
2015-02-05 20:40:46 +03:00
}
// waitSchedule waits other goroutines to be scheduled for a while
func waitSchedule() { time.Sleep(time.Millisecond) }