rafthttp: stop printing log when attaching stream with the same term

There is no need to print log when attaching stream with the same
term because the stream is installed back immediately.

This happens a lot when etcd 2.1 connects to etcd 2.0, so we make
the change.
This commit is contained in:
Yicheng Qin 2015-05-14 21:52:59 -07:00
parent f59bddd74b
commit 5074235254

View File

@ -76,9 +76,12 @@ func (s *stream) attach(sw *streamWriter) error {
// ignore lower-term streaming request // ignore lower-term streaming request
if sw.term < s.w.term { if sw.term < s.w.term {
return fmt.Errorf("cannot attach out of data stream server [%d / %d]", sw.term, s.w.term) return fmt.Errorf("cannot attach out of data stream server [%d / %d]", sw.term, s.w.term)
} } else if sw.term == s.w.term {
s.w.stopWithoutLog()
} else {
s.w.stop() s.w.stop()
} }
}
s.w = sw s.w = sw
return nil return nil
} }
@ -156,6 +159,7 @@ type streamWriter struct {
fs *stats.FollowerStats fs *stats.FollowerStats
q chan []raftpb.Entry q chan []raftpb.Entry
done chan struct{} done chan struct{}
printLog bool
} }
// newStreamWriter starts and returns a new unstarted stream writer. // newStreamWriter starts and returns a new unstarted stream writer.
@ -166,6 +170,7 @@ func newStreamWriter(to types.ID, term uint64) *streamWriter {
term: term, term: term,
q: make(chan []raftpb.Entry, streamBufSize), q: make(chan []raftpb.Entry, streamBufSize),
done: make(chan struct{}), done: make(chan struct{}),
printLog: true,
} }
return s return s
} }
@ -188,7 +193,9 @@ func (s *streamWriter) send(ents []raftpb.Entry) error {
func (s *streamWriter) handle(w WriteFlusher) { func (s *streamWriter) handle(w WriteFlusher) {
defer func() { defer func() {
close(s.done) close(s.done)
if s.printLog {
log.Printf("rafthttp: server streaming to %s at term %d has been stopped", s.to, s.term) log.Printf("rafthttp: server streaming to %s at term %d has been stopped", s.to, s.term)
}
}() }()
ew := newEntryWriter(w, s.to) ew := newEntryWriter(w, s.to)
@ -215,6 +222,11 @@ func (s *streamWriter) stop() {
<-s.done <-s.done
} }
func (s *streamWriter) stopWithoutLog() {
s.printLog = false
s.stop()
}
func (s *streamWriter) stopNotify() <-chan struct{} { return s.done } func (s *streamWriter) stopNotify() <-chan struct{} { return s.done }
// TODO: move the raft interface out of the reader. // TODO: move the raft interface out of the reader.