Skip to content

Commit

Permalink
Added stats to Query structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip Couto committed Sep 4, 2014
1 parent 7bd964e commit ae293f4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
17 changes: 17 additions & 0 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,3 +1169,20 @@ func TestVarint(t *testing.T) {
t.Errorf("Expected %v, was %v", nil, *resultBig)
}
}

//TestQueryStats confirms that the stats are returning valid data. Accuracy may be questionable.
func TestQueryStats(t *testing.T) {
session := createSession(t)
defer session.Close()
qry := session.Query("SELECT * FROM system.peers")
if err := qry.Exec(); err != nil {
t.Fatalf("query failed. %v", err)
} else {
if qry.Attempts() < 1 {
t.Fatal("expected at least 1 attempt, but got 0")
}
if qry.Latency() <= 0 {
t.Fatalf("expected latency to be greater than 0, but got %v instead.", qry.Latency())
}
}
}
14 changes: 9 additions & 5 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ func TestQueryRetry(t *testing.T) {
t.Fatal("no timeout")
}()
rt := RetryPolicy{NumRetries: 1}

if err := db.Query("kill").RetryPolicy(rt).Exec(); err == nil {
qry := db.Query("kill").RetryPolicy(rt)
if err := qry.Exec(); err == nil {
t.Fatal("expected error")
}
//Minus 1 from the nKillReq variable since there is the initial query attempt
if srv.nKillReq-1 != uint64(rt.NumRetries) {
t.Fatalf("failed to retry the query %v time(s). Query executed %v times", rt.NumRetries, srv.nKillReq-1)
requests := srv.nKillReq
if requests != uint64(qry.Attempts()) {
t.Fatalf("expected requests %v to match query attemps %v", requests, qry.Attempts())
}
//Minus 1 from the requests variable since there is the initial query attempt
if requests-1 != uint64(rt.NumRetries) {
t.Fatalf("failed to retry the query %v time(s). Query executed %v times", rt.NumRetries, requests-1)
}
}

Expand Down
43 changes: 32 additions & 11 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func (s *Session) executeQuery(qry *Query) *Iter {
}

var iter *Iter
for count := 0; count <= qry.rt.NumRetries; count++ {
qry.attempts = 0
qry.totalLatency = 0
for qry.attempts <= qry.rt.NumRetries {
conn := s.Pool.Pick(qry)

//Assign the error unavailable to the iterator
Expand All @@ -143,7 +145,11 @@ func (s *Session) executeQuery(qry *Query) *Iter {
break
}

t := time.Now()
iter = conn.executeQuery(qry)
qry.totalLatency += time.Now().Sub(t).Nanoseconds()
qry.attempts++

//Exit for loop if the query was successful
if iter.err == nil {
break
Expand Down Expand Up @@ -190,16 +196,31 @@ func (s *Session) ExecuteBatch(batch *Batch) error {

// Query represents a CQL statement that can be executed.
type Query struct {
stmt string
values []interface{}
cons Consistency
pageSize int
pageState []byte
prefetch float64
trace Tracer
session *Session
rt RetryPolicy
binding func(q *QueryInfo) ([]interface{}, error)
stmt string
values []interface{}
cons Consistency
pageSize int
pageState []byte
prefetch float64
trace Tracer
session *Session
rt RetryPolicy
binding func(q *QueryInfo) ([]interface{}, error)
attempts int
totalLatency int64
}

//Attempts returns the number of times the query was executed.
func (q *Query) Attempts() int {
return q.attempts
}

//Latency returns the average amount of nanoseconds per attempt of the query.
func (q *Query) Latency() int64 {
if q.attempts > 0 {
return q.totalLatency / int64(q.attempts)
}
return 0
}

// Consistency sets the consistency level for this query. If no consistency
Expand Down

0 comments on commit ae293f4

Please sign in to comment.