-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -243,7 +245,9 @@ func (cw *streamWriter) closeUnlocked() bool { | |
if !cw.working { | ||
return false | ||
} | ||
cw.closer.Close() | ||
if err := cw.closer.Close(); err != nil { | ||
plog.Errorf("Peer %s (writer) connection close error: %v", cw.peerID, err) | ||
} | ||
if len(cw.msgc) > 0 { | ||
cw.r.ReportUnreachable(uint64(cw.peerID)) | ||
} | ||
|
@@ -278,25 +282,28 @@ 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 | ||
paused bool | ||
cancel func() | ||
closer io.Closer | ||
|
||
stopc chan struct{} | ||
done chan struct{} | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
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.done = make(chan struct{}) | ||
if cr.errorc == nil { | ||
cr.errorc = cr.tr.ErrorC | ||
} | ||
|
||
go r.run() | ||
if cr.ctx == nil { | ||
cr.ctx, cr.cancel = context.WithCancel(context.Background()) | ||
} | ||
go cr.run() | ||
} | ||
|
||
func (cr *streamReader) run() { | ||
|
@@ -311,7 +318,7 @@ func (cr *streamReader) run() { | |
} else { | ||
cr.status.activate() | ||
plog.Infof("established a TCP streaming connection with peer %s (%s reader)", cr.peerID, cr.typ) | ||
err := cr.decodeLoop(rc, t) | ||
err = cr.decodeLoop(rc, t) | ||
plog.Warningf("lost the TCP streaming connection with peer %s (%s reader)", cr.peerID, cr.typ) | ||
switch { | ||
// all data is read out | ||
|
@@ -322,15 +329,16 @@ func (cr *streamReader) run() { | |
cr.status.deactivate(failureType{source: t.String(), action: "read"}, err.Error()) | ||
} | ||
} | ||
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: | ||
// Wait for a while before new dial attempt | ||
err = cr.rl.Wait(cr.ctx) | ||
if cr.ctx.Err() != nil { | ||
plog.Infof("stopped streaming with peer %s (%s reader)", cr.peerID, t) | ||
close(cr.done) | ||
return | ||
} | ||
if err != nil { | ||
plog.Errorf("streaming with peer %s (%s reader) rate limiter error: %v", cr.peerID, t, err) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -346,7 +354,7 @@ func (cr *streamReader) decodeLoop(rc io.ReadCloser, t streamType) error { | |
plog.Panicf("unhandled stream type %s", t) | ||
} | ||
select { | ||
case <-cr.stopc: | ||
case <-cr.ctx.Done(): | ||
cr.mu.Unlock() | ||
if err := rc.Close(); err != nil { | ||
return err | ||
|
@@ -401,11 +409,8 @@ func (cr *streamReader) decodeLoop(rc io.ReadCloser, t streamType) error { | |
} | ||
|
||
func (cr *streamReader) stop() { | ||
close(cr.stopc) | ||
cr.mu.Lock() | ||
if cr.cancel != nil { | ||
cr.cancel() | ||
} | ||
cr.cancel() | ||
cr.close() | ||
cr.mu.Unlock() | ||
<-cr.done | ||
|
@@ -429,13 +434,11 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) { | |
|
||
setPeerURLsHeader(req, cr.tr.URLs) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
req = req.WithContext(ctx) | ||
req = req.WithContext(cr.ctx) | ||
|
||
cr.mu.Lock() | ||
cr.cancel = cancel | ||
select { | ||
case <-cr.stopc: | ||
case <-cr.ctx.Done(): | ||
cr.mu.Unlock() | ||
return nil, fmt.Errorf("stream reader is stopped") | ||
default: | ||
|
@@ -497,7 +500,9 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) { | |
|
||
func (cr *streamReader) close() { | ||
if cr.closer != nil { | ||
cr.closer.Close() | ||
if err := cr.closer.Close(); err != nil { | ||
plog.Errorf("Peer %s (reader) connection close error: %v", cr.peerID, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/Peer/peer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @heyitsanthony fixed |
||
} | ||
} | ||
cr.closer = nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Peer/peer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed