forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
354 lines (295 loc) · 8.59 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
// Copyright 2013-2014 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 // socket?
connections *AtomicQueue //ArrayBlockingQueue<*Connection>
health *AtomicInt //AtomicInteger
partitionGeneration int
refreshCount int
referenceCount int
responded bool
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.connectionQueueSize),
health: NewAtomicInt(_FULL_HEALTH),
partitionGeneration: -1,
referenceCount: 0,
responded: 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++
conn, err := nd.GetConnection(1 * time.Second)
if err != nil {
return nil, err
}
infoMap, err := RequestInfo(conn, "node", "partition-generation", "services")
if err != nil {
conn.Close()
nd.DecreaseHealth()
return nil, err
}
if err := nd.verifyNodeName(infoMap); err != nil {
return nil, err
}
nd.RestoreHealth()
nd.responded = true
if friends, err = nd.addFriends(infoMap); err != nil {
return nil, err
}
if err := nd.updatePartitions(conn, infoMap); err != nil {
return nil, err
}
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++
} 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 != 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 = 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) {
for t := nd.connections.Poll(); t != nil; t = nd.connections.Poll() {
conn = t.(*Connection)
if conn.IsConnected() {
if err := conn.SetTimeout(timeout); err == nil {
return conn, nil
}
}
conn.Close()
}
if conn, err = NewConnection(nd.address, nd.cluster.connectionTimeout); err != nil {
return nil, err
}
// need to authenticate
if nd.cluster.user != "" {
command := newAdminCommand()
if err := command.authenticate(conn, nd.cluster.user, nd.cluster.password); err != nil {
// Socket not authenticated. Do not put back into pool.
conn.Close()
return nil, err
}
}
if conn.SetTimeout(timeout) != nil {
return nil, err
}
return conn, nil
}
// 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) {
if !nd.active.Get() || !nd.connections.Offer(conn) {
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
}
}