forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_executor.go
100 lines (83 loc) · 1.79 KB
/
query_executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gocql
import (
"time"
)
type ExecutableQuery interface {
execute(conn *Conn) *Iter
attempt(keyspace string, end, start time.Time, iter *Iter, host *HostInfo)
retryPolicy() RetryPolicy
GetRoutingKey() ([]byte, error)
Keyspace() string
Cancel()
RetryableQuery
}
type queryExecutor struct {
pool *policyConnPool
policy HostSelectionPolicy
}
func (q *queryExecutor) attemptQuery(qry ExecutableQuery, conn *Conn) *Iter {
start := time.Now()
iter := qry.execute(conn)
end := time.Now()
qry.attempt(q.pool.keyspace, end, start, iter, conn.host)
return iter
}
func (q *queryExecutor) executeQuery(qry ExecutableQuery) (*Iter, error) {
rt := qry.retryPolicy()
hostIter := q.policy.Pick(qry)
var iter *Iter
for hostResponse := hostIter(); hostResponse != nil; hostResponse = hostIter() {
host := hostResponse.Info()
if host == nil || !host.IsUp() {
continue
}
pool, ok := q.pool.getPool(host)
if !ok {
continue
}
conn := pool.Pick()
if conn == nil {
continue
}
iter = q.attemptQuery(qry, conn)
// Update host
hostResponse.Mark(iter.err)
if rt == nil {
iter.host = host
break
}
switch rt.GetRetryType(iter.err) {
case Retry:
for rt.Attempt(qry) {
iter = q.attemptQuery(qry, conn)
hostResponse.Mark(iter.err)
if iter.err == nil {
iter.host = host
return iter, nil
}
if rt.GetRetryType(iter.err) != Retry {
break
}
}
case Rethrow:
return nil, iter.err
case Ignore:
return iter, nil
case RetryNextHost:
default:
}
// Exit for loop if the query was successful
if iter.err == nil {
iter.host = host
return iter, nil
}
if !rt.Attempt(qry) {
// What do here? Should we just return an error here?
break
}
}
if iter == nil {
return nil, ErrNoConnections
}
return iter, nil
}