forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.go
250 lines (229 loc) · 6.37 KB
/
cluster.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
)
// ClusterConfig is a struct to configure the default cluster implementation
// of gocoql. It has a varity of attributes that can be used to modify the
// behavior to fit the most common use cases. Applications that requre a
// different setup must implement their own cluster.
type ClusterConfig struct {
Hosts []string // addresses for the initial connections
CQLVersion string // CQL version (default: 3.0.0)
ProtoVersion int // version of the native protocol (default: 2)
Timeout time.Duration // connection timeout (default: 600ms)
DefaultPort int // default port (default: 9042)
Keyspace string // initial keyspace (optional)
NumConns int // number of connections per host (default: 2)
NumStreams int // number of streams per connection (default: 128)
DelayMin time.Duration // minimum reconnection delay (default: 1s)
DelayMax time.Duration // maximum reconnection delay (default: 10min)
StartupMin int // wait for StartupMin hosts (default: len(Hosts)/2+1)
StartupTimeout time.Duration // amount of to wait for a connection (default: 5s)
Consistency Consistency // default consistency level (default: Quorum)
Compressor Compressor // compression algorithm (default: nil)
Authenticator Authenticator // authenticator (default: nil)
RetryPolicy RetryPolicy // Default retry policy to use for queries(default:0)
}
// NewCluster generates a new config for the default cluster implementation.
func NewCluster(hosts ...string) *ClusterConfig {
cfg := &ClusterConfig{
Hosts: hosts,
CQLVersion: "3.0.0",
ProtoVersion: 2,
Timeout: 600 * time.Millisecond,
DefaultPort: 9042,
NumConns: 2,
NumStreams: 128,
DelayMin: 1 * time.Second,
DelayMax: 10 * time.Minute,
StartupMin: len(hosts)/2 + 1,
StartupTimeout: 5 * time.Second,
Consistency: Quorum,
}
return cfg
}
// CreateSession initializes the cluster based on this config and returns a
// session object that can be used to interact with the database.
func (cfg *ClusterConfig) CreateSession() (*Session, error) {
//Check that hosts in the ClusterConfig is not empty
if len(cfg.Hosts) < 1 {
return nil, ErrNoHosts
}
impl := &clusterImpl{
cfg: *cfg,
hostPool: NewRoundRobin(),
connPool: make(map[string]*RoundRobin),
conns: make(map[*Conn]struct{}),
quitWait: make(chan bool),
cStart: make(chan int, 1),
keyspace: cfg.Keyspace,
}
for i := 0; i < len(impl.cfg.Hosts); i++ {
addr := strings.TrimSpace(impl.cfg.Hosts[i])
if strings.Index(addr, ":") < 0 {
addr = fmt.Sprintf("%s:%d", addr, impl.cfg.DefaultPort)
}
for j := 0; j < impl.cfg.NumConns; j++ {
go impl.connect(addr)
}
}
//See if a connection is made within the StartupTimeout window.
select {
case <-impl.cStart:
s := NewSession(impl)
s.SetConsistency(cfg.Consistency)
return s, nil
case <-time.After(cfg.StartupTimeout):
impl.Close()
return nil, ErrNoConnections
}
}
type clusterImpl struct {
cfg ClusterConfig
hostPool *RoundRobin
connPool map[string]*RoundRobin
conns map[*Conn]struct{}
keyspace string
mu sync.Mutex
started bool
cStart chan int
quit bool
quitWait chan bool
quitOnce sync.Once
}
func (c *clusterImpl) connect(addr string) {
cfg := ConnConfig{
ProtoVersion: c.cfg.ProtoVersion,
CQLVersion: c.cfg.CQLVersion,
Timeout: c.cfg.Timeout,
NumStreams: c.cfg.NumStreams,
Compressor: c.cfg.Compressor,
Authenticator: c.cfg.Authenticator,
}
delay := c.cfg.DelayMin
for {
conn, err := Connect(addr, cfg, c)
if err != nil {
log.Printf("failed to connect to %q: %v", addr, err)
select {
case <-time.After(delay):
if delay *= 2; delay > c.cfg.DelayMax {
delay = c.cfg.DelayMax
}
continue
case <-c.quitWait:
return
}
}
c.addConn(conn, "")
return
}
}
func (c *clusterImpl) changeKeyspace(conn *Conn, keyspace string, connected bool) {
if err := conn.UseKeyspace(keyspace); err != nil {
conn.Close()
if connected {
c.removeConn(conn)
}
go c.connect(conn.Address())
}
if !connected {
c.addConn(conn, keyspace)
}
}
func (c *clusterImpl) addConn(conn *Conn, keyspace string) {
c.mu.Lock()
defer c.mu.Unlock()
if c.quit {
conn.Close()
return
}
if keyspace != c.keyspace && c.keyspace != "" {
// change the keyspace before adding the node to the pool
go c.changeKeyspace(conn, c.keyspace, false)
return
}
connPool := c.connPool[conn.Address()]
if connPool == nil {
connPool = NewRoundRobin()
c.connPool[conn.Address()] = connPool
c.hostPool.AddNode(connPool)
if !c.started && c.hostPool.Size() >= c.cfg.StartupMin {
c.started = true
c.cStart <- 1
}
}
connPool.AddNode(conn)
c.conns[conn] = struct{}{}
}
func (c *clusterImpl) removeConn(conn *Conn) {
c.mu.Lock()
defer c.mu.Unlock()
conn.Close()
connPool := c.connPool[conn.addr]
if connPool == nil {
return
}
connPool.RemoveNode(conn)
if connPool.Size() == 0 {
c.hostPool.RemoveNode(connPool)
delete(c.connPool, conn.addr)
}
delete(c.conns, conn)
}
func (c *clusterImpl) HandleError(conn *Conn, err error, closed bool) {
if !closed {
// ignore all non-fatal errors
return
}
c.removeConn(conn)
if !c.quit {
go c.connect(conn.Address()) // reconnect
}
}
func (c *clusterImpl) HandleKeyspace(conn *Conn, keyspace string) {
c.mu.Lock()
if c.keyspace == keyspace {
c.mu.Unlock()
return
}
c.keyspace = keyspace
conns := make([]*Conn, 0, len(c.conns))
for conn := range c.conns {
conns = append(conns, conn)
}
c.mu.Unlock()
// change the keyspace of all other connections too
for i := 0; i < len(conns); i++ {
if conns[i] == conn {
continue
}
c.changeKeyspace(conns[i], keyspace, true)
}
}
func (c *clusterImpl) Pick(qry *Query) *Conn {
return c.hostPool.Pick(qry)
}
func (c *clusterImpl) Close() {
c.quitOnce.Do(func() {
c.mu.Lock()
defer c.mu.Unlock()
c.quit = true
close(c.quitWait)
for conn := range c.conns {
conn.Close()
}
})
}
var (
ErrNoHosts = errors.New("no hosts provided")
ErrNoConnections = errors.New("no connections were made in startup time frame")
)