-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
172 lines (145 loc) · 3.25 KB
/
util.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
package planb
import (
"bytes"
"errors"
"io/ioutil"
"net"
"os"
"strings"
"github.com/bsm/pool"
"github.com/bsm/redeo/client"
"github.com/bsm/redeo/resp"
"github.com/google/uuid"
"github.com/hashicorp/raft"
)
var errUnexpectedServerResponse = errors.New("unexpected response")
// "inspired" by https://github.com/hashicorp/consul
func normNodeID(conf *raft.Config, fname string) error {
nodeID := string(conf.LocalID)
// if we don't have a nodeID, try to read it from file
if nodeID == "" {
if _, err := os.Stat(fname); err == nil {
binID, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
nodeID = string(binID)
}
}
// if we have a node ID, normalise and validate it.
if nodeID != "" {
nodeID = strings.ToLower(strings.TrimSpace(nodeID))
if _, err := uuid.Parse(nodeID); err != nil {
return err
}
conf.LocalID = raft.ServerID(nodeID)
return nil
}
// otherwise, create a new one
nodeID = uuid.New().String()
if err := ioutil.WriteFile(fname, []byte(nodeID), 0600); err != nil {
return err
}
conf.LocalID = raft.ServerID(nodeID)
return nil
}
func retrieveServerConfig(addr string) (*raft.Server, error) {
pool, err := client.New(&pool.Options{InitialSize: 1}, func() (net.Conn, error) {
return net.Dial("tcp", addr)
})
if err != nil {
return nil, err
}
defer pool.Close()
cn, err := pool.Get()
if err != nil {
return nil, err
}
defer pool.Put(cn)
cn.WriteCmd("INFO")
if err := cn.Flush(); err != nil {
cn.MarkFailed()
return nil, err
}
typ, err := cn.PeekType()
if err := cn.Flush(); err != nil {
cn.MarkFailed()
return nil, err
}
switch typ {
case resp.TypeBulk:
default:
return nil, errUnexpectedServerResponse
}
raw, err := cn.ReadBulk(nil)
if err != nil {
cn.MarkFailed()
return nil, err
}
info := serverInfo(raw)
nodeID, err := info.NodeID()
if err != nil {
return nil, err
}
address, err := info.Address()
if err != nil {
return nil, err
}
return &raft.Server{
ID: nodeID,
Address: address,
}, nil
}
// --------------------------------------------------------------------
type serverInfo []byte
func (i serverInfo) NodeID() (raft.ServerID, error) {
nodeID, err := i.parse("node_id")
if err != nil {
return "", err
}
if _, err := uuid.Parse(nodeID); err != nil {
return "", err
}
return raft.ServerID(nodeID), nil
}
func (i serverInfo) Address() (raft.ServerAddress, error) {
address, err := i.parse("tcp_addr")
if err != nil {
return "", err
}
if _, _, err := net.SplitHostPort(address); err != nil {
return "", err
}
return raft.ServerAddress(address), nil
}
func (i serverInfo) parse(s string) (string, error) {
raw := []byte(i)
pivot := []byte("\n" + s + ":")
if pos := bytes.Index(raw, pivot); pos < 0 {
return "", errUnexpectedServerResponse
} else {
raw = raw[pos+len(pivot):]
}
if pos := bytes.Index(raw, []byte{'\n'}); pos < 0 {
return "", errUnexpectedServerResponse
} else {
raw = raw[:pos]
}
return string(raw), nil
}
// --------------------------------------------------------------------
const (
fnvOffset32 uint32 = 2166136261
fnvPrime32 uint32 = 16777619
)
func fnv32a(b []byte) uint32 {
if len(b) == 0 {
return 0
}
h := fnvOffset32
for _, c := range b {
h ^= uint32(c)
h *= fnvPrime32
}
return h
}