forked from NVIDIA/aistore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
272 lines (241 loc) · 7.53 KB
/
init.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
// Package tools provides common tools and utilities for all unit and integration tests
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved.
*/
package tools
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/artashesbalabekyan/aistore/api"
"github.com/artashesbalabekyan/aistore/api/authn"
"github.com/artashesbalabekyan/aistore/api/env"
"github.com/artashesbalabekyan/aistore/cluster/meta"
"github.com/artashesbalabekyan/aistore/cmn"
"github.com/artashesbalabekyan/aistore/cmn/cos"
"github.com/artashesbalabekyan/aistore/tools/docker"
"github.com/artashesbalabekyan/aistore/tools/tlog"
)
const (
defaultProxyURL = "http://localhost:8080" // the url for the cluster's proxy (local)
dockerEnvFile = "/tmp/docker_ais/deploy.env" // filepath of Docker deployment config
)
const (
registerTimeout = time.Minute * 2
bucketTimeout = time.Minute
)
type (
// command used to restore a node
RestoreCmd struct {
Node *meta.Snode
Cmd string
Args []string
PID int
}
ClusterType string
)
// Cluster type used for test
const (
ClusterTypeLocal ClusterType = "local"
ClusterTypeDocker ClusterType = "docker"
ClusterTypeK8s ClusterType = "k8s"
)
var (
proxyURLReadOnly string // user-defined primary proxy URL - it is read-only variable and tests mustn't change it
pmapReadOnly meta.NodeMap // initial proxy map - it is read-only variable
testClusterType ClusterType // AIS cluster type - it is read-only variable
currSmap *meta.Smap
restoreNodesOnce sync.Once // Ensures that the initialization happens only once.
restoreNodes map[string]RestoreCmd // initial proxy and target nodes => command to restore them
transportArgs = cmn.TransportArgs{
Timeout: 600 * time.Second,
UseHTTPProxyEnv: true,
// Allow a lot of idle connections so they can be reused when making huge
// number of requests (eg. in `TestETLBigBucket`).
MaxIdleConns: 2000,
IdleConnsPerHost: 200,
}
HTTPClient *http.Client
RemoteCluster struct {
UUID string
Alias string
URL string
}
LoggedUserToken string
gctx *Ctx
)
func init() {
envURL := os.Getenv(env.AIS.Endpoint)
// Since tests do not have access to cluster configuration, the tests
// detect client type by the primary proxy URL passed by a user.
// Certificate check is always disabled.
transportArgs.UseHTTPS = cos.IsHTTPS(envURL)
transportArgs.SkipVerify = cos.IsParseBool(os.Getenv(env.AIS.SkipVerifyCrt))
HTTPClient = cmn.NewClient(transportArgs)
gctx = &Ctx{
Client: HTTPClient,
Log: tlog.Logf,
}
}
// InitLocalCluster initializes AIS cluster that must be either:
// 1. deployed locally using `make deploy` command and accessible @ localhost:8080, or
// 2. deployed in local docker environment, or
// 3. provided via `AIS_ENDPOINT` environment variable
//
// In addition, try to query remote AIS cluster that may or may not be locally deployed as well.
func InitLocalCluster() {
var (
// Gets the fields from the .env file from which the docker was deployed
envVars = parseEnvVariables(dockerEnvFile)
// Host IP and port of primary cluster
primaryHostIP, port = envVars["PRIMARY_HOST_IP"], envVars["PORT"]
clusterType = ClusterTypeLocal
proxyURL = defaultProxyURL
)
if docker.IsRunning() {
clusterType = ClusterTypeDocker
proxyURL = "http://" + primaryHostIP + ":" + port
}
// This is needed for testing on Kubernetes if we want to run 'make test-XXX'
// Many of the other packages do not accept the 'url' flag
if cliAISURL := os.Getenv(env.AIS.Endpoint); cliAISURL != "" {
if !strings.HasPrefix(cliAISURL, "http") {
cliAISURL = "http://" + cliAISURL
}
proxyURL = cliAISURL
}
err := InitCluster(proxyURL, clusterType)
if err == nil {
initRemAis() // remote AIS that optionally may be run locally as well and used for testing
return
}
fmt.Printf("Error: %s\n\n", strings.TrimSuffix(err.Error(), "\n"))
if strings.Contains(err.Error(), "token") {
fmt.Printf("Hint: make sure to provide access token via %s environment or the default config location\n",
env.AuthN.TokenFile)
} else if strings.Contains(err.Error(), "unreachable") {
fmt.Printf("Hint: make sure that cluster is running and/or specify its endpoint via %s environment\n",
env.AIS.Endpoint)
} else {
fmt.Printf("Hint: check api/env/*.go environment and, in particular, %s, %s, and more\n",
env.AIS.Endpoint, env.AIS.PrimaryID)
if len(envVars) > 0 {
fmt.Println("Docker Environment:")
for k, v := range envVars {
fmt.Printf("\t%s:\t%s\n", k, v)
}
}
}
os.Exit(1)
}
// InitCluster initializes the environment necessary for testing against an AIS cluster.
// NOTE:
//
// the function is also used for testing by NVIDIA/ais-k8s Operator
func InitCluster(proxyURL string, clusterType ClusterType) (err error) {
LoggedUserToken = authn.LoadToken("")
proxyURLReadOnly = proxyURL
testClusterType = clusterType
if err = initProxyURL(); err != nil {
return
}
initPmap()
return
}
func initProxyURL() (err error) {
// Discover if a proxy is ready to accept requests.
err = cmn.NetworkCallWithRetry(&cmn.RetryArgs{
Call: func() (int, error) { return 0, GetProxyReadiness(proxyURLReadOnly) },
SoftErr: 5,
HardErr: 5,
Sleep: 5 * time.Second,
Action: "reach AIS at " + proxyURLReadOnly,
IsClient: true,
})
if err != nil {
return errors.New("AIS is unreachable at " + proxyURLReadOnly)
}
if testClusterType == ClusterTypeK8s {
// For kubernetes cluster, we use LoadBalancer service to expose the proxies.
// `proxyURLReadOnly` will point to LoadBalancer service, and we need not get primary URL.
return
}
// Primary proxy can change if proxy tests are run and
// no new cluster is re-deployed before each test.
// Finds who is the current primary proxy.
primary, err := GetPrimaryProxy(proxyURLReadOnly)
if err != nil {
err = fmt.Errorf("failed to get primary proxy info from %s; err %v", proxyURLReadOnly, err)
return err
}
proxyURLReadOnly = primary.URL(cmn.NetPublic)
return
}
func initPmap() {
bp := BaseAPIParams(proxyURLReadOnly)
smap, err := waitForStartup(bp)
cos.AssertNoErr(err)
pmapReadOnly = smap.Pmap
}
func initRemAis() {
all, err := api.GetRemoteAIS(BaseAPIParams(proxyURLReadOnly))
if err != nil {
if !errors.Is(err, io.EOF) {
fmt.Fprintf(os.Stderr, "failed to query remote ais cluster: %v\n", err)
}
return
}
cos.AssertMsg(len(all.A) < 2, "multi-remote clustering is not implemented yet")
if len(all.A) == 1 {
remais := all.A[0]
RemoteCluster.UUID = remais.UUID
RemoteCluster.Alias = remais.Alias
RemoteCluster.URL = remais.URL
}
}
func initNodeCmd() {
bp := BaseAPIParams(proxyURLReadOnly)
smap, err := waitForStartup(bp)
cos.AssertNoErr(err)
restoreNodes = make(map[string]RestoreCmd, smap.CountProxies()+smap.CountTargets())
for _, node := range smap.Pmap {
if node.ID() == MockDaemonID {
continue
}
restoreNodes[node.ID()] = GetRestoreCmd(node)
}
for _, node := range smap.Tmap {
if node.ID() == MockDaemonID {
continue
}
restoreNodes[node.ID()] = GetRestoreCmd(node)
}
}
// reads .env file and parses its contents
func parseEnvVariables(fpath string, delimiter ...string) map[string]string {
m := map[string]string{}
dlim := "="
data, err := os.ReadFile(fpath)
if err != nil {
return nil
}
if len(delimiter) > 0 {
dlim = delimiter[0]
}
paramList := strings.Split(string(data), "\n")
for _, dat := range paramList {
datum := strings.Split(dat, dlim)
// key=val
if len(datum) == 2 {
key := strings.TrimSpace(datum[0])
value := strings.TrimSpace(datum[1])
m[key] = value
}
}
return m
}