Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rafthttp: configurable stream reader retry timeout #8003

Merged
merged 4 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rafthttp/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/coreos/etcd/raft/raftpb"
"github.com/coreos/etcd/snap"
"golang.org/x/net/context"
"golang.org/x/time/rate"
)

const (
Expand Down Expand Up @@ -198,6 +199,13 @@ func startPeer(transport *Transport, urls types.URLs, peerID types.ID, fs *stats
recvc: p.recvc,
propc: p.propc,
}

if transport.DialRetryTimeout != 0 {
limit := rate.Every(transport.DialRetryTimeout)
p.msgAppV2Reader.rl = rate.NewLimiter(limit, 1)
p.msgAppReader.rl = rate.NewLimiter(limit, 1)
}

p.msgAppV2Reader.start()
p.msgAppReader.start()

Expand Down
31 changes: 22 additions & 9 deletions rafthttp/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"sync"
"time"

"golang.org/x/time/rate"

"github.com/coreos/etcd/etcdserver/stats"
"github.com/coreos/etcd/pkg/httputil"
"github.com/coreos/etcd/pkg/transport"
Expand Down Expand Up @@ -278,6 +280,8 @@ type streamReader struct {
recvc chan<- raftpb.Message
propc chan<- raftpb.Message

rl *rate.Limiter // alters the frequency of dial retrial attempts

errorc chan<- error

mu sync.Mutex
Expand All @@ -289,14 +293,21 @@ type streamReader struct {
done chan struct{}
}

func (r *streamReader) start() {
r.stopc = make(chan struct{})
r.done = make(chan struct{})
if r.errorc == nil {
r.errorc = r.tr.ErrorC
func (cr *streamReader) start() {
cr.stopc = make(chan struct{})
cr.done = make(chan struct{})
if cr.errorc == nil {
cr.errorc = cr.tr.ErrorC
}

if cr.rl == nil {
// If client didn't provide rate limiter, use the default which will
// wait 100ms to create a new stream, so it doesn't bring too much
// overhead when retry.
cr.rl = rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
}

go r.run()
go cr.run()
}

func (cr *streamReader) run() {
Expand All @@ -323,13 +334,15 @@ func (cr *streamReader) run() {
}
}
select {
// Wait 100ms to create a new stream, so it doesn't bring too much
// overhead when retry.
case <-time.After(100 * time.Millisecond):
case <-cr.stopc:
plog.Infof("stopped streaming with peer %s (%s reader)", cr.peerID, t)
close(cr.done)
return
default:
// wait for a while before new dial attempt
if err := cr.rl.Wait(context.TODO()); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of this forced timeout, would it be possible to replace stopc with

type streamReader struct {
...
    runCtx context.Context
    runCancel context.CancelFunc
...
}

and replace the select with:

err := cr.rl.Wait(cr.runCtx)
if cr.runCtx.Err() != nil {
    plog.Infof("...")
    close(cr.done)
    return
}
if err != nil {
    plog.Errorf(...)
}

and replace any <-stopc with <-runCtx.Done()
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@heyitsanthony that's right, I've replaced streamReader.stopc with streamReader.ctx and streamReader.cancel.

plog.Errorf("streaming with peer %s (%s reader) rate limiter error: %v", cr.peerID, t, err)
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions rafthttp/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"golang.org/x/time/rate"

"github.com/coreos/etcd/etcdserver/stats"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/pkg/types"
Expand Down Expand Up @@ -113,6 +115,7 @@ func TestStreamReaderDialRequest(t *testing.T) {
peerID: types.ID(2),
tr: &Transport{streamRt: tr, ClusterID: types.ID(1), ID: types.ID(1)},
picker: mustNewURLPicker(t, []string{"http://localhost:2380"}),
rl: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
}
sr.dial(tt)

Expand Down Expand Up @@ -167,6 +170,7 @@ func TestStreamReaderDialResult(t *testing.T) {
tr: &Transport{streamRt: tr, ClusterID: types.ID(1)},
picker: mustNewURLPicker(t, []string{"http://localhost:2380"}),
errorc: make(chan error, 1),
rl: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
}

_, err := sr.dial(streamTypeMessage)
Expand All @@ -192,6 +196,7 @@ func TestStreamReaderStopOnDial(t *testing.T) {
errorc: make(chan error, 1),
typ: streamTypeMessage,
status: newPeerStatus(types.ID(2)),
rl: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
}
tr.onResp = func() {
// stop() waits for the run() goroutine to exit, but that exit
Expand Down Expand Up @@ -246,6 +251,7 @@ func TestStreamReaderDialDetectUnsupport(t *testing.T) {
peerID: types.ID(2),
tr: &Transport{streamRt: tr, ClusterID: types.ID(1)},
picker: mustNewURLPicker(t, []string{"http://localhost:2380"}),
rl: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
}

_, err := sr.dial(typ)
Expand Down
6 changes: 4 additions & 2 deletions rafthttp/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ type Transporter interface {
// User needs to call Start before calling other functions, and call
// Stop when the Transport is no longer used.
type Transport struct {
DialTimeout time.Duration // maximum duration before timing out dial of the request
TLSInfo transport.TLSInfo // TLS information used when creating connection
DialTimeout time.Duration // maximum duration before timing out dial of the request
DialRetryTimeout time.Duration // alters the frequency of streamReader dial retrial attempts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to DialRetryLimiter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiang90 do you mean to have a single instance of DialRetryLimiter *rate.Limiter in rafthttp.Transport? In this way DialRetryLimiter will be shared among all streamReader instances, so it will limit the dial frequency for every peer.

If this is a desired behavior, we definitely can put DialRetryLimiter into the Trasnport, because rate.Limiter seems to be goroutine-safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. ok. i see. i agree probably better to limit per stream (we need to document this better though). my main motivation is to change timeout to something like rate. retry timeout is usually used to describe the timeout of the entire retry (when you give up on retries). we, here, actually retry forever and there is a backoff between each retry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point... May be variable of rate.Limit type would be a better option? Something like RetryFrequency rate.Limit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitalyisaev2 right. can you give it a try?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiang90 Yes, sure


TLSInfo transport.TLSInfo // TLS information used when creating connection

ID types.ID // local member ID
URLs types.URLs // local peer URLs
Expand Down