-
Notifications
You must be signed in to change notification settings - Fork 172
/
utils.go
370 lines (337 loc) · 9.65 KB
/
utils.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
// Package ais provides core functionality for the AIStore object storage.
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package ais
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/k8s"
"github.com/NVIDIA/aistore/cmn/nlog"
"github.com/NVIDIA/aistore/core/meta"
)
const (
accessNetPublic = netAccess(1 << iota)
accessNetIntraControl
accessNetIntraData
accessNetPublicControl = accessNetPublic | accessNetIntraControl
accessNetPublicData = accessNetPublic | accessNetIntraData
accessControlData = accessNetIntraControl | accessNetIntraData
accessNetAll = accessNetPublic | accessNetIntraData | accessNetIntraControl
)
// Network access of handlers (Public, IntraControl, & IntraData)
type (
netAccess int
// HTTP Range (aka Read Range)
htrange struct {
Start, Length int64
}
// Local unicast IP info
localIPv4Info struct {
ipv4 string
mtu int
}
)
func (na netAccess) isSet(flag netAccess) bool { return na&flag == flag }
//
// IPV4
//
// returns a list of local unicast (IPv4, MTU)
func getLocalIPv4s(config *cmn.Config) (addrlist []*localIPv4Info, err error) {
addrlist = make([]*localIPv4Info, 0, 4)
addrs, e := net.InterfaceAddrs()
if e != nil {
err = fmt.Errorf("failed to get host unicast IPs: %w", e)
return
}
iflist, e := net.Interfaces()
if e != nil {
err = fmt.Errorf("failed to get network interfaces: %w", e)
return
}
for _, addr := range addrs {
curr := &localIPv4Info{}
if ipnet, ok := addr.(*net.IPNet); ok {
// production or K8s: skip loopbacks
if ipnet.IP.IsLoopback() && (!config.TestingEnv() || k8s.IsK8s()) {
continue
}
if ipnet.IP.To4() == nil {
continue
}
curr.ipv4 = ipnet.IP.String()
}
for _, intf := range iflist {
ifAddrs, e := intf.Addrs()
// skip invalid interfaces
if e != nil {
continue
}
for _, ifAddr := range ifAddrs {
if ipnet, ok := ifAddr.(*net.IPNet); ok && ipnet.IP.To4() != nil && ipnet.IP.String() == curr.ipv4 {
curr.mtu = intf.MTU
addrlist = append(addrlist, curr)
break
}
}
if curr.mtu != 0 {
break
}
}
}
if len(addrlist) == 0 {
return addrlist, errors.New("the host does not have any IPv4 addresses")
}
return addrlist, nil
}
// given configured list of hostnames, return the first one matching local unicast IPv4
func _selectHost(locIPs []*localIPv4Info, hostnames []string) (string, error) {
sb := &strings.Builder{}
sb.WriteByte('[')
for i, lip := range locIPs {
sb.WriteString(lip.ipv4)
sb.WriteString("(MTU=")
sb.WriteString(strconv.Itoa(lip.mtu))
sb.WriteByte(')')
if i < len(locIPs)-1 {
sb.WriteByte(' ')
}
}
sb.WriteByte(']')
nlog.Infoln("local IPv4:", sb.String())
nlog.Infoln("configured:", hostnames)
for i, host := range hostnames {
host = strings.TrimSpace(host)
var ipv4 string
if net.ParseIP(host) != nil { // parses as IP
ipv4 = host
} else {
ip, err := _resolve(host)
if err != nil {
nlog.Errorln("failed to resolve hostname(?)", host, "err:", err, "[idx:", i, len(hostnames))
continue
}
ipv4 = ip.String()
nlog.Infoln("resolved hostname", host, "to IP addr", ipv4)
}
for _, addr := range locIPs {
if addr.ipv4 == ipv4 {
nlog.Infoln("selected: hostname", host, "IP", ipv4)
return host, nil
}
}
}
err := fmt.Errorf("failed to select hostname from: (%s, %v)", sb.String(), hostnames)
nlog.Errorln(err)
return "", err
}
// _localIP takes a list of local IPv4s and returns the best fit for a daemon to listen on it
func _localIP(addrList []*localIPv4Info) (ip net.IP, err error) {
if len(addrList) == 0 {
return nil, errors.New("no addresses to choose from")
}
if len(addrList) == 1 {
nlog.Infof("Found only one IPv4: %s, MTU %d", addrList[0].ipv4, addrList[0].mtu)
if addrList[0].mtu <= 1500 {
nlog.Warningf("IPv4 %s MTU size is small: %d\n", addrList[0].ipv4, addrList[0].mtu)
}
if ip = net.ParseIP(addrList[0].ipv4); ip == nil {
return nil, fmt.Errorf("failed to parse IP address: %s", addrList[0].ipv4)
}
return ip, nil
}
if cmn.Rom.FastV(4, cos.SmoduleAIS) {
nlog.Infof("%d IPv4s:", len(addrList))
for _, addr := range addrList {
nlog.Infof(" %#v\n", *addr)
}
}
if ip = net.ParseIP(addrList[0].ipv4); ip == nil {
return nil, fmt.Errorf("failed to parse IP address: %s", addrList[0].ipv4)
}
return ip, nil
}
func multihome(configuredIPv4s string) (pub string, extra []string) {
if i := strings.IndexByte(configuredIPv4s, cmn.HostnameListSepa[0]); i <= 0 {
cos.ExitAssertLog(i < 0, "invalid format:", configuredIPv4s)
return configuredIPv4s, nil
}
// trim + validation
lst := strings.Split(configuredIPv4s, cmn.HostnameListSepa)
pub, extra = strings.TrimSpace(lst[0]), lst[1:]
for i := range extra {
extra[i] = strings.TrimSpace(extra[i])
cos.ExitAssertLog(extra[i] != "", "invalid format (empty value):", configuredIPv4s)
cos.ExitAssertLog(extra[i] != pub, "duplicated addr or hostname:", configuredIPv4s)
for j := 0; j < i; j++ {
cos.ExitAssertLog(extra[i] != extra[j], "duplicated addr or hostname:", configuredIPv4s)
}
}
nlog.Infof("multihome: %s and %v", pub, extra)
return pub, extra
}
// choose one of the local IPv4s if local config doesn't contain (explicitly) specified
func initNetInfo(ni *meta.NetInfo, addrList []*localIPv4Info, proto, configuredIPv4s, port string) (err error) {
var (
ip net.IP
host string
)
if configuredIPv4s == "" {
if ip, err = _localIP(addrList); err == nil {
ni.Init(proto, ip.String(), port)
}
return
}
lst := strings.Split(configuredIPv4s, cmn.HostnameListSepa)
if host, err = _selectHost(addrList, lst); err == nil {
ni.Init(proto, host, port)
}
return
}
func _resolve(hostName string) (net.IP, error) {
ips, err := net.LookupIP(hostName)
if err != nil {
return nil, err
}
for _, ip := range ips {
if ip.To4() != nil {
return ip, nil
}
}
return nil, fmt.Errorf("failed to find non-empty IPv4 in list %v (hostName=%q)", ips, hostName)
}
func parseOrResolve(hostname string) (err error) {
if net.ParseIP(hostname) != nil {
// is a parse-able IP addr
return
}
_, err = _resolve(hostname)
return
}
/////////////
// htrange //
/////////////
// (compare w/ cmn.MakeRangeHdr)
func (r htrange) contentRange(size int64) string {
return fmt.Sprintf("%s%d-%d/%d", cos.HdrContentRangeValPrefix, r.Start, r.Start+r.Length-1, size)
}
// ParseMultiRange parses a Range Header string as per RFC 7233.
// ErrNoOverlap is returned if none of the ranges overlap with the [0, size) content.
func parseMultiRange(s string, size int64) (ranges []htrange, err error) {
var noOverlap bool
if !strings.HasPrefix(s, cos.HdrRangeValPrefix) {
return nil, fmt.Errorf("read range %q is invalid (prefix)", s)
}
allRanges := strings.Split(s[len(cos.HdrRangeValPrefix):], ",")
for _, ra := range allRanges {
ra = strings.TrimSpace(ra)
if ra == "" {
continue
}
i := strings.Index(ra, "-")
if i < 0 {
return nil, fmt.Errorf("read range %q is invalid (-)", s)
}
var (
r htrange
start = strings.TrimSpace(ra[:i])
end = strings.TrimSpace(ra[i+1:])
)
if start == "" {
// If no start is specified, end specifies the range start relative
// to the end of the file, and we are dealing with <suffix-length>
// which has to be a non-negative integer as per RFC 7233 Section 2.1 "Byte-Ranges".
if end == "" || end[0] == '-' {
return nil, fmt.Errorf("read range %q is invalid as per RFC 7233 Section 2.1", ra)
}
i, err := strconv.ParseInt(end, 10, 64)
if i < 0 || err != nil {
return nil, fmt.Errorf("read range %q is invalid (see RFC 7233 Section 2.1)", ra)
}
if i > size {
i = size
}
r.Start = size - i
r.Length = size - r.Start
} else {
i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i < 0 {
return nil, fmt.Errorf("read range %q is invalid (start)", ra)
}
if i >= size {
// If the range begins after the size of the content it does not overlap.
noOverlap = true
continue
}
r.Start = i
if end == "" {
// If no end is specified, range extends to the end of the file.
r.Length = size - r.Start
} else {
i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.Start > i {
return nil, fmt.Errorf("read range %q is invalid (end)", ra)
}
if i >= size {
i = size - 1
}
r.Length = i - r.Start + 1
}
}
ranges = append(ranges, r)
}
if noOverlap && len(ranges) == 0 {
return nil, cmn.NewErrRangeNotSatisfiable(nil, allRanges, size)
}
return ranges, nil
}
//
// misc. helpers
//
func deploymentType() string {
switch {
case k8s.IsK8s():
return apc.DeploymentK8s
case cmn.GCO.Get().TestingEnv():
return apc.DeploymentDev
default:
return runtime.GOOS
}
}
// for AIS metadata filenames (constants), see `cmn/fname` package
func cleanupConfigDir(name string, keepInitialConfig bool) {
if !keepInitialConfig {
// remove plain-text (initial) config
cos.RemoveFile(daemon.cli.globalConfigPath)
cos.RemoveFile(daemon.cli.localConfigPath)
}
config := cmn.GCO.Get()
filepath.Walk(config.ConfigDir, func(path string, finfo os.FileInfo, _ error) error {
if strings.HasPrefix(finfo.Name(), ".ais.") {
if err := cos.RemoveFile(path); err != nil {
nlog.Errorf("%s: failed to cleanup %q, err: %v", name, path, err)
}
}
return nil
})
}
//
// common APPEND(file(s)) pre-parser
//
const appendHandleSepa = "|"
func preParse(packedHdl string) (items []string, err error) {
items = strings.SplitN(packedHdl, appendHandleSepa, 4)
if len(items) != 4 {
err = fmt.Errorf("invalid APPEND handle: %q", packedHdl)
}
return
}