Skip to content

Commit

Permalink
etcdserver: add more detailed traces on linearized reading (#12335)
Browse files Browse the repository at this point in the history
To improve debuggability of `agreement among raft nodes before
linearized reading`, we added some tracing inside
`linearizableReadLoop`.

This will allow us to know the timing of `s.r.ReadIndex` vs
`s.applyWait.Wait(rs.Index)`.
  • Loading branch information
PierreZ committed Sep 27, 2020
1 parent 73e5714 commit cc2b4cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions etcdserver/v3_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/base64"
"encoding/binary"
"strconv"
"time"

"go.etcd.io/etcd/v3/auth"
Expand Down Expand Up @@ -718,6 +719,9 @@ func (s *EtcdServer) linearizableReadLoop() {
return
}

// as a single loop is can unlock multiple reads, it is not very useful
// to propagate the trace from Txn or Range.
trace := traceutil.New("linearizableReadLoop", s.getLogger())
nextnr := newNotifier()

s.readMu.Lock()
Expand Down Expand Up @@ -778,16 +782,26 @@ func (s *EtcdServer) linearizableReadLoop() {
if !done {
continue
}
trace.Step("read index received")

if ai := s.getAppliedIndex(); ai < rs.Index {
index := rs.Index
trace.AddField(traceutil.Field{Key: "readStateIndex", Value: index})

ai := s.getAppliedIndex()
trace.AddField(traceutil.Field{Key: "appliedIndex", Value: strconv.FormatUint(ai, 10)})

if ai < index {
select {
case <-s.applyWait.Wait(rs.Index):
case <-s.applyWait.Wait(index):
case <-s.stopping:
return
}
}
// unblock all l-reads requested at indices before rs.Index
nr.notify(nil)
trace.Step("applied index is now lower than readState.Index")

trace.LogAllStepsIfLong(traceThreshold)
}
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/traceutil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func (t *Trace) LogIfLong(threshold time.Duration) {
}
}

// LogAllStepsIfLong dumps all logs if the duration is longer than threshold
func (t *Trace) LogAllStepsIfLong(threshold time.Duration) {
if time.Since(t.startTime) > threshold {
t.LogWithStepThreshold(0)
}
}

// LogWithStepThreshold only dumps step whose duration is longer than step threshold
func (t *Trace) LogWithStepThreshold(threshold time.Duration) {
msg, fs := t.logInfo(threshold)
Expand Down

0 comments on commit cc2b4cd

Please sign in to comment.