Skip to content

Commit

Permalink
runtime: added counter to failpoint.terms field
Browse files Browse the repository at this point in the history
Each time sleep is executed in actSleep counter is incremented. Added counter endpoint that gives number of sleeps executed on the current terms: GET pkg/failpoint/count
When DELETE /pkg/failpoint is executed, failpoint.terms is nilled, hence counter is reset.

Requested in reference to:
tests/linearizability: added sleep fail point injection #14796

Signed-off-by: Ramil Mirhasanov <ramil600@yahoo.com>
  • Loading branch information
ramil600 committed Nov 21, 2022
1 parent 51e235e commit d4701a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
lines[i] = fps[i] + "=" + s
}
w.Write([]byte(strings.Join(lines, "\n") + "\n"))
} else if strings.HasSuffix(key, "/count") {
fp := key[:len(key)-6]
count, err := StatusCount(fp)
if err != nil {
http.Error(w, "failed to GET: "+err.Error(), http.StatusNotFound)
}
w.Write([]byte(count))
} else {
status, err := Status(key)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ func Status(failpath string) (string, error) {
return t.desc, nil
}

// StatusCount gives the number of sleeps executed on the current terms
func StatusCount(failpath string) (string, error) {
failpointsMu.RLock()
fp := failpoints[failpath]
failpointsMu.RUnlock()
if fp == nil {
return "", ErrNoExist
}
fp.mu.RLock()
t := fp.t
fp.mu.RUnlock()
if t == nil {
return "", ErrDisabled
}
count := fp.t.counter
return fmt.Sprint(count), nil

}

func List() []string {
failpointsMu.RLock()
ret := make([]string, 0, len(failpoints))
Expand Down
5 changes: 5 additions & 0 deletions runtime/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type terms struct {

// mu protects the state of the terms chain
mu sync.Mutex
// counts amount of sleep executions
counter int
}

// term is an executable unit of the failpoint terms chain
Expand Down Expand Up @@ -310,6 +312,9 @@ func actSleep(t *term) interface{} {
return nil
}
time.Sleep(dur)
t.parent.mu.Lock()
t.parent.counter++
t.parent.mu.Unlock()
return nil
}

Expand Down

0 comments on commit d4701a0

Please sign in to comment.