Make minor fixes to query tracker

This commit is contained in:
Alan Bernstein 2020-10-26 16:55:30 -05:00
parent 2bc2a32263
commit c5a7a259aa
2 changed files with 8 additions and 7 deletions

View file

@ -23,14 +23,14 @@ import (
type ActiveQueryStatus struct {
Query string `json:"query"`
Node string `json:"node"`
Index string `json:index`
Index string `json:"index"`
Age time.Duration `json:"age"`
}
type PastQueryStatus struct {
Query string `json:"query"`
Node string `json:"nodeID"`
Index string `json:index`
Index string `json:"index"`
Age time.Duration `json:"age"`
Runtime time.Duration `json:"runtime"`
}
@ -86,9 +86,10 @@ func (b *ringBuffer) add(q pastQuery) {
b.mu.Lock()
defer b.mu.Unlock()
b.queries[(b.start+b.count)%cap(b.queries)] = q
if b.count == cap(b.queries) {
b.start = (b.start + 1) % cap(b.queries)
// len(b.queries) is used here as the *capacity* of the ringBuffer
b.queries[(b.start+b.count)%len(b.queries)] = q
if b.count == len(b.queries) {
b.start = (b.start + 1) % len(b.queries)
} else {
b.count++
}

View file

@ -56,14 +56,14 @@ func TestRingBuffer(t *testing.T) {
}
func TestQueryTracker(t *testing.T) {
tracker := newQueryTracker()
tracker := newQueryTracker(5)
defer tracker.Stop()
if queries := tracker.ActiveQueries(); len(queries) > 0 {
t.Fatalf("expected no active queries; found %v", queries)
}
qs := tracker.Start("test query", "node0")
qs := tracker.Start("test query", "node0", "i")
var queries []ActiveQueryStatus
for len(queries) < 1 {