-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
node.go
248 lines (210 loc) · 5.08 KB
/
node.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
package gorethink
import (
"sync"
"sync/atomic"
"time"
p "github.com/dancannon/gorethink/ql2"
)
const (
maxNodeHealth = 100
)
// Node represents a database server in the cluster
type Node struct {
ID string
Host Host
aliases []Host
cluster *Cluster
pool *Pool
refreshDoneChan chan struct{}
mu sync.RWMutex
closed bool
health int64
}
func newNode(id string, aliases []Host, cluster *Cluster, pool *Pool) *Node {
node := &Node{
ID: id,
Host: aliases[0],
aliases: aliases,
cluster: cluster,
pool: pool,
health: maxNodeHealth,
refreshDoneChan: make(chan struct{}),
}
// Start node refresh loop
refreshInterval := cluster.opts.NodeRefreshInterval
if refreshInterval <= 0 {
// Default to refresh every 30 seconds
refreshInterval = time.Second * 30
}
go func() {
refreshTicker := time.NewTicker(refreshInterval)
for {
select {
case <-refreshTicker.C:
node.Refresh()
case <-node.refreshDoneChan:
return
}
}
}()
return node
}
// Closed returns true if the node is closed
func (n *Node) Closed() bool {
n.mu.RLock()
defer n.mu.RUnlock()
return n.closed
}
// Close closes the session
func (n *Node) Close(optArgs ...CloseOpts) error {
n.mu.Lock()
defer n.mu.Unlock()
if n.closed {
return nil
}
if len(optArgs) >= 1 {
if optArgs[0].NoReplyWait {
n.NoReplyWait()
}
}
n.refreshDoneChan <- struct{}{}
if n.pool != nil {
n.pool.Close()
}
n.pool = nil
n.closed = true
return nil
}
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool.
func (n *Node) SetMaxIdleConns(idleConns int) {
n.pool.SetMaxIdleConns(idleConns)
}
// SetMaxOpenConns sets the maximum number of open connections to the database.
func (n *Node) SetMaxOpenConns(openConns int) {
n.pool.SetMaxOpenConns(openConns)
}
// NoReplyWait ensures that previous queries with the noreply flag have been
// processed by the server. Note that this guarantee only applies to queries
// run on the given connection
func (n *Node) NoReplyWait() error {
return n.pool.Exec(Query{
Type: p.Query_NOREPLY_WAIT,
})
}
// Query executes a ReQL query using this nodes connection pool.
func (n *Node) Query(q Query) (cursor *Cursor, err error) {
if n.Closed() {
return nil, ErrInvalidNode
}
cursor, err = n.pool.Query(q)
if err != nil {
n.DecrementHealth()
}
return cursor, err
}
// Exec executes a ReQL query using this nodes connection pool.
func (n *Node) Exec(q Query) (err error) {
if n.Closed() {
return ErrInvalidNode
}
err = n.pool.Exec(q)
if err != nil {
n.DecrementHealth()
}
return err
}
// Refresh attempts to connect to the node and check that it is still connected
// to the cluster.
//
// If an error occurred or the node is no longer connected then
// the nodes health is decrease, if there were no issues then the node is marked
// as being healthy.
func (n *Node) Refresh() {
if n.cluster.opts.DiscoverHosts {
// If host discovery is enabled then check the servers status
q, err := newQuery(
DB("rethinkdb").Table("server_status").Get(n.ID),
map[string]interface{}{},
n.cluster.opts,
)
if err != nil {
return
}
cursor, err := n.pool.Query(q)
if err != nil {
n.DecrementHealth()
return
}
defer cursor.Close()
// Cant find node status so assuming node has been disconnected
if cursor.IsNil() {
n.DecrementHealth()
return
}
// Below check is for RethinkDB 2.0
var status nodeStatus
err = cursor.One(&status)
if err != nil {
return
}
if status.Status != "" && status.Status != "connected" {
n.DecrementHealth()
return
}
} else {
// If host discovery is disabled just execute a simple ping query
q, err := newQuery(
Expr("OK"),
map[string]interface{}{},
n.cluster.opts,
)
if err != nil {
return
}
cursor, err := n.pool.Query(q)
if err != nil {
n.DecrementHealth()
return
}
defer cursor.Close()
var status string
err = cursor.One(&status)
if err != nil {
return
}
if status != "OK" {
n.DecrementHealth()
return
}
}
// If status check was successful reset health
n.ResetHealth()
}
// DecrementHealth decreases the nodes health by 1 (the nodes health starts at maxNodeHealth)
func (n *Node) DecrementHealth() {
atomic.AddInt64(&n.health, -1)
}
// ResetHealth sets the nodes health back to maxNodeHealth (fully healthy)
func (n *Node) ResetHealth() {
atomic.StoreInt64(&n.health, maxNodeHealth)
}
// IsHealthy checks the nodes health by ensuring that the health counter is above 0.
func (n *Node) IsHealthy() bool {
health := atomic.LoadInt64(&n.health)
return health > 0
}
type nodeStatus struct {
ID string `gorethink:"id"`
Name string `gorethink:"name"`
Status string `gorethink:"status"`
Network struct {
Hostname string `gorethink:"hostname"`
ClusterPort int64 `gorethink:"cluster_port"`
ReqlPort int64 `gorethink:"reql_port"`
CanonicalAddresses []struct {
Host string `gorethink:"host"`
Port int64 `gorethink:"port"`
} `gorethink:"canonical_addresses"`
} `gorethink:"network"`
}