-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathnode.go
400 lines (336 loc) · 10.6 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2013-2015 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"strconv"
"strings"
"sync"
"time"
. "github.com/aerospike/aerospike-client-go/logger"
. "github.com/aerospike/aerospike-client-go/types"
. "github.com/aerospike/aerospike-client-go/types/atomic"
)
const (
_PARTITIONS = 4096
_FULL_HEALTH = 100
)
// Node represents an Aerospike Database Server Node
type Node struct {
cluster *Cluster
name string
host *Host
aliases []*Host
address string
connections *AtomicQueue //ArrayBlockingQueue<*Connection>
connectionCount *AtomicInt
health *AtomicInt //AtomicInteger
partitionGeneration *AtomicInt
refreshCount *AtomicInt
referenceCount *AtomicInt
responded *AtomicBool
useNewInfo bool
active *AtomicBool
mutex sync.RWMutex
}
// NewNode initializes a server node with connection parameters.
func newNode(cluster *Cluster, nv *nodeValidator) *Node {
return &Node{
cluster: cluster,
name: nv.name,
aliases: nv.aliases,
address: nv.address,
useNewInfo: nv.useNewInfo,
// Assign host to first IP alias because the server identifies nodes
// by IP address (not hostname).
host: nv.aliases[0],
connections: NewAtomicQueue(cluster.clientPolicy.ConnectionQueueSize),
connectionCount: NewAtomicInt(0),
health: NewAtomicInt(_FULL_HEALTH),
partitionGeneration: NewAtomicInt(-1),
referenceCount: NewAtomicInt(0),
refreshCount: NewAtomicInt(0),
responded: NewAtomicBool(false),
active: NewAtomicBool(true),
}
}
// Refresh requests current status from server node, and updates node with the result.
func (nd *Node) Refresh() ([]*Host, error) {
var friends []*Host
nd.refreshCount.IncrementAndGet()
conn, err := nd.GetConnection(1 * time.Second)
if err != nil {
return nil, err
}
infoMap, err := RequestInfo(conn, "node", "partition-generation", "services")
if err != nil {
nd.InvalidateConnection(conn)
nd.DecreaseHealth()
return nil, err
}
if err := nd.verifyNodeName(infoMap); err != nil {
nd.PutConnection(conn)
return nil, err
}
nd.RestoreHealth()
nd.responded.Set(true)
if friends, err = nd.addFriends(infoMap); err != nil {
nd.PutConnection(conn)
return nil, err
}
if err := nd.updatePartitions(conn, infoMap); err != nil {
nd.InvalidateConnection(conn)
return nil, err
}
// Suppose there is a request peak, and we create lots of connections to
// handle those requests. We want to keep those connections around while the
// peak is taking place. When the peak is over, we hopefully will want to
// close and those connections, and remove pressure from our side (the
// client) and servers. Because the connection pool is a FIFO queue, if we
// refresh the connection here, we may end up preventing lots of unused
// connections from going idle, and therefore preventing them from being
// closed. We should keep the number of connections to servers as low as
// possible when there is no need for them. For that reason, we just put the
// connection back into the pool without refreshing it.
nd.PutConnection(conn)
return friends, nil
}
func (nd *Node) verifyNodeName(infoMap map[string]string) error {
infoName, exists := infoMap["node"]
if !exists || len(infoName) == 0 {
nd.DecreaseHealth()
return NewAerospikeError(INVALID_NODE_ERROR, "Node name is empty")
}
if !(nd.name == infoName) {
// Set node to inactive immediately.
nd.active.Set(false)
return NewAerospikeError(INVALID_NODE_ERROR, "Node name has changed. Old="+nd.name+" New="+infoName)
}
return nil
}
func (nd *Node) addFriends(infoMap map[string]string) ([]*Host, error) {
friendString, exists := infoMap["services"]
var friends []*Host
if !exists || len(friendString) == 0 {
return friends, nil
}
friendNames := strings.Split(friendString, ";")
for _, friend := range friendNames {
friendInfo := strings.Split(friend, ":")
host := friendInfo[0]
port, _ := strconv.Atoi(friendInfo[1])
alias := NewHost(host, port)
node := nd.cluster.findAlias(alias)
if node != nil {
node.referenceCount.IncrementAndGet()
} else {
if !nd.findAlias(friends, alias) {
if friends == nil {
friends = make([]*Host, 0, 16)
}
friends = append(friends, alias)
}
}
}
return friends, nil
}
func (nd *Node) findAlias(friends []*Host, alias *Host) bool {
for _, host := range friends {
if *host == *alias {
return true
}
}
return false
}
func (nd *Node) updatePartitions(conn *Connection, infoMap map[string]string) error {
genString, exists := infoMap["partition-generation"]
if !exists || len(genString) == 0 {
return NewAerospikeError(PARSE_ERROR, "partition-generation is empty")
}
generation, _ := strconv.Atoi(genString)
if nd.partitionGeneration.Get() != generation {
Logger.Info("Node %s partition generation %d changed", nd.GetName(), generation)
if err := nd.cluster.updatePartitions(conn, nd); err != nil {
return err
}
nd.partitionGeneration.Set(generation)
}
return nil
}
// GetConnection gets a connection to the node.
// If no pooled connection is available, a new connection will be created.
func (nd *Node) GetConnection(timeout time.Duration) (conn *Connection, err error) {
tBegin := time.Now()
pollTries := 0
L:
for timeout == 0 || time.Now().Sub(tBegin) <= timeout {
if t := nd.connections.Poll(); t != nil {
conn = t.(*Connection)
if conn.IsConnected() && !conn.isIdle() {
if err := conn.SetTimeout(timeout); err == nil {
return conn, nil
}
}
nd.InvalidateConnection(conn)
}
// if connection count is limited and enough connections are already created, don't create a new one
if nd.cluster.clientPolicy.LimitConnectionsToQueueSize && nd.connectionCount.Get() >= nd.cluster.clientPolicy.ConnectionQueueSize {
// will avoid an infinite loop
if timeout != 0 || pollTries < 10 {
// 10 reteies, each waits for 100us for a total of 1 milliseconds
time.Sleep(time.Microsecond * 100)
pollTries++
continue
}
break L
}
if conn, err = NewConnection(nd.address, nd.cluster.clientPolicy.Timeout); err != nil {
return nil, err
}
// need to authenticate
if err = conn.Authenticate(nd.cluster.user, nd.cluster.Password()); err != nil {
// Socket not authenticated. Do not put back into pool.
conn.Close()
return nil, err
}
if err = conn.SetTimeout(timeout); err != nil {
// Socket not authenticated. Do not put back into pool.
conn.Close()
return nil, err
}
conn.setIdleTimeout(nd.cluster.clientPolicy.IdleTimeout)
conn.refresh()
nd.connectionCount.IncrementAndGet()
return conn, nil
}
return nil, NewAerospikeError(NO_AVAILABLE_CONNECTIONS_TO_NODE)
}
// PutConnection puts back a connection to the pool.
// If connection pool is full, the connection will be
// closed and discarded.
func (nd *Node) PutConnection(conn *Connection) {
conn.refresh()
if !nd.active.Get() || !nd.connections.Offer(conn) {
nd.InvalidateConnection(conn)
}
}
// InvalidateConnection closes and discards a connection from the pool.
func (nd *Node) InvalidateConnection(conn *Connection) {
nd.connectionCount.DecrementAndGet()
conn.Close()
}
// RestoreHealth marks the node as healthy.
func (nd *Node) RestoreHealth() {
// There can be cases where health is full, but active is false.
// Once a node has been marked inactive, it stays inactive.
nd.health.Set(_FULL_HEALTH)
}
// DecreaseHealth decreases node Health as a result of bad connection or communication.
func (nd *Node) DecreaseHealth() {
nd.health.DecrementAndGet()
}
// IsUnhealthy checks if the node is unhealthy.
func (nd *Node) IsUnhealthy() bool {
return nd.health.Get() <= 0
}
// GetHost retrieves host for the node.
func (nd *Node) GetHost() *Host {
return nd.host
}
// IsActive Checks if the node is active.
func (nd *Node) IsActive() bool {
return nd.active.Get()
}
// GetName returns node name.
func (nd *Node) GetName() string {
return nd.name
}
// GetAliases returnss node aliases.
func (nd *Node) GetAliases() []*Host {
nd.mutex.RLock()
aliases := nd.aliases
nd.mutex.RUnlock()
return aliases
}
// Sets node aliases
func (nd *Node) setAliases(aliases []*Host) {
nd.mutex.Lock()
nd.aliases = aliases
nd.mutex.Unlock()
}
// AddAlias adds an alias for the node
func (nd *Node) AddAlias(aliasToAdd *Host) {
// Aliases are only referenced in the cluster tend goroutine,
// so synchronization is not necessary.
aliases := nd.GetAliases()
if aliases == nil {
aliases = []*Host{}
}
aliases = append(aliases, aliasToAdd)
nd.setAliases(aliases)
}
// Close marks node as inactice and closes all of its pooled connections.
func (nd *Node) Close() {
nd.active.Set(false)
nd.closeConnections()
}
// String implements stringer interface
func (nd *Node) String() string {
return nd.name + " " + nd.host.String()
}
func (nd *Node) closeConnections() {
for conn := nd.connections.Poll(); conn != nil; conn = nd.connections.Poll() {
conn.(*Connection).Close()
}
}
// Equals compares equality of two nodes based on their names.
func (nd *Node) Equals(other *Node) bool {
return nd.name == other.name
}
// MigrationInProgress determines if the node is participating in a data migration
func (nd *Node) MigrationInProgress() (bool, error) {
values, err := RequestNodeStats(nd)
if err != nil {
return false, err
}
// if the migration_progress_send exists and is not `0`, then migration is in progress
if migration, exists := values["migrate_progress_send"]; exists && migration != "0" {
return true, nil
}
// migration not in progress
return false, nil
}
// WaitUntillMigrationIsFinished will block until migration operations are finished.
func (nd *Node) WaitUntillMigrationIsFinished(timeout time.Duration) (err error) {
if timeout <= 0 {
timeout = _NO_TIMEOUT
}
done := make(chan error)
go func() {
// this function is guaranteed to return after timeout
// no go routines will be leaked
for {
if res, err := nd.MigrationInProgress(); err != nil || !res {
done <- err
return
}
}
}()
dealine := time.After(timeout)
select {
case <-dealine:
return NewAerospikeError(TIMEOUT)
case err = <-done:
return err
}
}