forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
111 lines (93 loc) · 2.85 KB
/
exec.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
101
102
103
104
105
106
107
108
109
110
111
package gocql
import (
"fmt"
)
// SingleHostQueryExecutor allows to quickly execute diagnostic queries while
// connected to only a single node.
// The executor opens only a single connection to a node and does not use
// connection pools.
// Consistency level used is ONE.
// Retry policy is applied, attempts are visible in query metrics but query
// observer is not notified.
type SingleHostQueryExecutor struct {
session *Session
control *controlConn
}
// Exec executes the query without returning any rows.
func (e SingleHostQueryExecutor) Exec(stmt string, values ...interface{}) error {
return e.control.query(stmt, values...).Close()
}
// Iter executes the query and returns an iterator capable of iterating
// over all results.
func (e SingleHostQueryExecutor) Iter(stmt string, values ...interface{}) *Iter {
return e.control.query(stmt, values...)
}
func (e SingleHostQueryExecutor) Close() {
if e.control != nil {
e.control.close()
}
if e.session != nil {
e.session.Close()
}
}
// NewSingleHostQueryExecutor creates a SingleHostQueryExecutor by connecting
// to one of the hosts specified in the ClusterConfig.
// If ProtoVersion is not specified version 4 is used.
// Caller is responsible for closing the executor after use.
func NewSingleHostQueryExecutor(cfg *ClusterConfig) (e SingleHostQueryExecutor, err error) {
// Check that hosts in the ClusterConfig is not empty
if len(cfg.Hosts) < 1 {
err = ErrNoHosts
return
}
c := *cfg
// If protocol version not set assume 4 and skip discovery
if c.ProtoVersion == 0 {
e.session.logger.Print("gocql: setting protocol version 4")
c.ProtoVersion = 4
}
// Close in case of error
defer func() {
if err != nil {
e.Close()
}
}()
// Create uninitialised session
c.disableInit = true
if e.session, err = NewSession(c); err != nil {
err = fmt.Errorf("new session: %w", err)
return
}
var hosts []*HostInfo
if hosts, err = addrsToHosts(c.Hosts, c.Port, c.Logger); err != nil {
err = fmt.Errorf("addrs to hosts: %w", err)
return
}
// Create control connection to one of the hosts
e.control = createControlConn(e.session)
// shuffle endpoints so not all drivers will connect to the same initial
// node.
hosts = shuffleHosts(hosts)
conncfg := *e.control.session.connCfg
conncfg.disableCoalesce = true
var conn *Conn
for _, host := range hosts {
conn, err = e.control.session.dial(e.control.session.ctx, host, &conncfg, e.control)
if err != nil {
e.control.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
continue
}
err = e.control.setupConn(conn)
if err == nil {
break
}
e.control.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
conn.Close()
conn = nil
}
if conn == nil {
err = fmt.Errorf("setup: %w", err)
return
}
return
}