This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_validator.go
320 lines (270 loc) · 8.76 KB
/
node_validator.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
// Copyright 2013-2019 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 (
"bytes"
"fmt"
"net"
"strconv"
"strings"
"time"
. "github.com/aerospike/aerospike-client-go/logger"
. "github.com/aerospike/aerospike-client-go/types"
)
type nodesToAddT map[string]*Node
func (nta nodesToAddT) addNodeIfNotExists(ndv *nodeValidator, cluster *Cluster) bool {
_, exists := nta[ndv.name]
if !exists {
// found a new node
node := cluster.createNode(ndv)
nta[ndv.name] = node
}
return exists
}
// Validates a Database server node
type nodeValidator struct {
name string
aliases []*Host
primaryHost *Host
detectLoadBalancer bool
sessionToken []byte
SessionExpiration time.Time
supportsFloat, supportsBatchIndex, supportsReplicasAll, supportsReplicas, supportsGeo, supportsPeers, supportsLUTNow, supportsTruncateNamespace bool
}
func (ndv *nodeValidator) seedNodes(cluster *Cluster, host *Host, nodesToAdd nodesToAddT) error {
if err := ndv.setAliases(host); err != nil {
return err
}
found := false
var resultErr error
for _, alias := range ndv.aliases {
if resultErr = ndv.validateAlias(cluster, alias); resultErr != nil {
Logger.Debug("Alias %s failed: %s", alias, resultErr)
continue
}
found = true
nodesToAdd.addNodeIfNotExists(ndv, cluster)
}
if !found {
return resultErr
}
return nil
}
func (ndv *nodeValidator) validateNode(cluster *Cluster, host *Host) error {
if clusterNodes := cluster.GetNodes(); cluster.clientPolicy.IgnoreOtherSubnetAliases && len(clusterNodes) > 0 {
masterHostname := clusterNodes[0].host.Name
ip, ipnet, err := net.ParseCIDR(masterHostname + "/24")
if err != nil {
Logger.Error(err.Error())
return NewAerospikeError(NO_AVAILABLE_CONNECTIONS_TO_NODE, "Failed parsing hostname...")
}
stop := ip.Mask(ipnet.Mask)
stop[3] += 255
if bytes.Compare(net.ParseIP(host.Name).To4(), ip.Mask(ipnet.Mask).To4()) >= 0 && bytes.Compare(net.ParseIP(host.Name).To4(), stop.To4()) < 0 {
} else {
return NewAerospikeError(NO_AVAILABLE_CONNECTIONS_TO_NODE, "Ignored hostname from other subnet...")
}
}
if err := ndv.setAliases(host); err != nil {
return err
}
var resultErr error
for _, alias := range ndv.aliases {
if err := ndv.validateAlias(cluster, alias); err != nil {
resultErr = err
Logger.Debug("Aliases %s failed: %s", alias, err)
continue
}
return nil
}
return resultErr
}
func (ndv *nodeValidator) setAliases(host *Host) error {
ndv.detectLoadBalancer = true
// IP addresses do not need a lookup
ip := net.ParseIP(host.Name)
if ip != nil {
// avoid detecting load balancer on localhost
ndv.detectLoadBalancer = !ip.IsLoopback()
aliases := make([]*Host, 1)
aliases[0] = NewHost(host.Name, host.Port)
aliases[0].TLSName = host.TLSName
ndv.aliases = aliases
} else {
addresses, err := net.LookupHost(host.Name)
if err != nil {
Logger.Error("Host lookup failed with error: %s", err.Error())
return err
}
aliases := make([]*Host, len(addresses))
for idx, addr := range addresses {
aliases[idx] = NewHost(addr, host.Port)
aliases[idx].TLSName = host.TLSName
// avoid detecting load balancer on localhost
if ip := net.ParseIP(host.Name); ip != nil && ip.IsLoopback() {
ndv.detectLoadBalancer = false
}
}
ndv.aliases = aliases
}
Logger.Debug("Node Validator has %d nodes and they are: %v", len(ndv.aliases), ndv.aliases)
return nil
}
func (ndv *nodeValidator) validateAlias(cluster *Cluster, alias *Host) error {
clientPolicy := cluster.clientPolicy
clientPolicy.Timeout /= time.Duration(2)
conn, err := NewSecureConnection(&clientPolicy, alias)
if err != nil {
return err
}
defer conn.Close()
if clientPolicy.RequiresAuthentication() {
// need to authenticate
acmd := NewLoginCommand(conn.dataBuffer)
err = acmd.login(&clientPolicy, conn, cluster.Password())
if err != nil {
return err
}
ndv.sessionToken = acmd.SessionToken
ndv.SessionExpiration = acmd.SessionExpiration
}
// check to make sure we have actually connected
info, err := RequestInfo(conn, "build")
if err != nil {
return err
}
if _, exists := info["ERROR:80:not authenticated"]; exists {
return NewAerospikeError(NOT_AUTHENTICATED)
}
hasClusterName := len(clientPolicy.ClusterName) > 0
infoKeys := []string{"node", "partition-generation", "features"}
if hasClusterName {
infoKeys = append(infoKeys, "cluster-name")
}
addressCommand := clientPolicy.serviceString()
if ndv.detectLoadBalancer {
infoKeys = append(infoKeys, addressCommand)
}
infoMap, err := RequestInfo(conn, infoKeys...)
if err != nil {
return err
}
nodeName, exists := infoMap["node"]
if !exists {
return NewAerospikeError(INVALID_NODE_ERROR, "Invalid node alias:"+alias.String())
}
genStr, exists := infoMap["partition-generation"]
if !exists {
return NewAerospikeError(INVALID_NODE_ERROR, "Invalid partition-generation for node:"+alias.String())
}
gen, err := strconv.Atoi(genStr)
if err != nil {
return NewAerospikeError(PARSE_ERROR, fmt.Sprintf("Invalid partition-generation for Node %s (%s), value: %s", nodeName, alias.String(), genStr))
}
if gen == -1 {
return NewAerospikeError(INVALID_NODE_ERROR, fmt.Sprintf("Node %s (%s) is not yet fully initialized", nodeName, alias.String()))
}
if hasClusterName {
id := infoMap["cluster-name"]
if len(id) == 0 || id != clientPolicy.ClusterName {
return NewAerospikeError(CLUSTER_NAME_MISMATCH_ERROR, fmt.Sprintf("Node %s (%s) expected cluster name `%s` but received `%s`", nodeName, alias.String(), clientPolicy.ClusterName, id))
}
}
// set features
if features, exists := infoMap["features"]; exists {
ndv.setFeatures(features)
}
// check if the host is a load-balancer
if peersStr, exists := infoMap[addressCommand]; exists {
var hostAddress []*Host
peerParser := peerListParser{buf: []byte("[" + peersStr + "]")}
if hostAddress, err = peerParser.readHosts(alias.TLSName); err != nil {
Logger.Error("Failed to parse `%s` results... err: %s", alias.String(), err.Error())
}
if len(hostAddress) > 0 {
isLoadBalancer := true
LOAD_BALANCER:
for _, h := range hostAddress {
for _, a := range ndv.aliases {
if h.equals(a) {
// one of the aliases were the same as an advertised service
// no need to replace the seed host with the alias
isLoadBalancer = false
break LOAD_BALANCER
}
}
}
if isLoadBalancer && ndv.detectLoadBalancer {
aliasFound := false
// take the seed out of the aliases if it is load balancer
Logger.Info("Host `%s` seems to be a load balancer. It is going to be replace by `%v`", alias.String(), hostAddress[0])
// try to connect to the aliases, and coose the first one that connects
for _, h := range hostAddress {
hconn, err := NewSecureConnection(&clientPolicy, h)
if err != nil {
continue
}
defer hconn.Close()
if clientPolicy.RequiresAuthentication() {
// need to authenticate
acmd := NewLoginCommand(hconn.dataBuffer)
err = acmd.login(&clientPolicy, hconn, cluster.Password())
if err != nil {
continue
}
ndv.sessionToken = acmd.SessionToken
ndv.SessionExpiration = acmd.SessionExpiration
}
alias = h
ndv.aliases = hostAddress
aliasFound = true
// found one, no need to try the rest
break
}
// Failed to find a valid address to connect. IP Address is probably internal on the cloud
// because the server access-address is not configured. Log warning and continue
// with original seed.
if !aliasFound {
Logger.Info("Inaccessible address `%s` as cluster seed. access-address is probably not configured on server.", alias.String())
}
}
}
}
ndv.name = nodeName
ndv.primaryHost = alias
return nil
}
func (ndv *nodeValidator) setFeatures(features string) {
featureList := strings.Split(features, ";")
for i := range featureList {
switch featureList[i] {
case "float":
ndv.supportsFloat = true
case "batch-index":
ndv.supportsBatchIndex = true
case "replicas":
ndv.supportsReplicas = true
case "replicas-all":
ndv.supportsReplicasAll = true
case "geo":
ndv.supportsGeo = true
case "peers":
ndv.supportsPeers = true
case "lut-now":
ndv.supportsLUTNow = true
case "truncate-namespace":
ndv.supportsTruncateNamespace = true
}
}
}