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
139 lines (122 loc) · 3.73 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
// 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 (
"net"
"regexp"
"strconv"
"time"
. "github.com/aerospike/aerospike-client-go/logger"
. "github.com/aerospike/aerospike-client-go/types"
)
// Validates a Database server node
type nodeValidator struct {
name string
aliases []*Host
address string
useNewInfo bool //= true
cluster *Cluster
}
// Generates a node validator
func newNodeValidator(cluster *Cluster, host *Host, timeout time.Duration) (*nodeValidator, error) {
newNodeValidator := &nodeValidator{
useNewInfo: true,
cluster: cluster,
}
if err := newNodeValidator.setAliases(host); err != nil {
return nil, err
}
if err := newNodeValidator.setAddress(timeout); err != nil {
return nil, err
}
return newNodeValidator, nil
}
func (ndv *nodeValidator) setAliases(host *Host) error {
// IP addresses do not need a lookup
ip := net.ParseIP(host.Name)
if ip != nil {
aliases := make([]*Host, 1)
aliases[0] = NewHost(host.Name, host.Port)
ndv.aliases = aliases
} else {
addresses, err := net.LookupHost(host.Name)
if err != nil {
Logger.Error("HostLookup failed with error: ", err)
return err
}
aliases := make([]*Host, len(addresses))
for idx, addr := range addresses {
aliases[idx] = NewHost(addr, host.Port)
}
ndv.aliases = aliases
}
Logger.Debug("Node Validator has %d nodes.", len(ndv.aliases))
return nil
}
func (ndv *nodeValidator) setAddress(timeout time.Duration) error {
for _, alias := range ndv.aliases {
address := net.JoinHostPort(alias.Name, strconv.Itoa(alias.Port))
conn, err := NewConnection(address, time.Second)
if err != nil {
return err
}
defer conn.Close()
// need to authenticate
if ndv.cluster.user != "" {
command := newAdminCommand()
if err := command.authenticate(conn, ndv.cluster.user, ndv.cluster.password); err != nil {
// Socket not authenticated. Do not put back into pool.
conn.Close()
return err
}
}
if err := conn.SetTimeout(timeout); err != nil {
return err
}
infoMap, err := RequestInfo(conn, "node", "build")
if err != nil {
return err
}
if nodeName, exists := infoMap["node"]; exists {
ndv.name = nodeName
ndv.address = address
// Check new info protocol support for >= 2.6.6 build
if buildVersion, exists := infoMap["build"]; exists {
v1, v2, v3, err := parseVersionString(buildVersion)
if err != nil {
Logger.Error(err.Error())
return err
}
ndv.useNewInfo = v1 > 2 || (v1 == 2 && (v2 > 6 || (v2 == 6 && v3 >= 6)))
}
}
}
return nil
}
// parses a version string
var r = regexp.MustCompile(`(\d+)\.(\d+)\.(\d+).*`)
func parseVersionString(version string) (int, int, int, error) {
vNumber := r.FindStringSubmatch(version)
if len(vNumber) < 4 {
return -1, -1, -1, NewAerospikeError(PARSE_ERROR, "Invalid build version string in Info: "+version)
}
v1, err1 := strconv.Atoi(vNumber[1])
v2, err2 := strconv.Atoi(vNumber[2])
v3, err3 := strconv.Atoi(vNumber[3])
if err1 == nil && err2 == nil && err3 == nil {
return v1, v2, v3, nil
}
Logger.Error("Invalid build version string in Info: " + version)
return -1, -1, -1, NewAerospikeError(PARSE_ERROR, "Invalid build version string in Info: "+version)
}