-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
248 lines (224 loc) · 11.4 KB
/
main.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
package main
import (
"fmt"
"net"
"os"
"os/signal"
"slices"
"strings"
"syscall"
"time"
service "github.com/arangodb-helper/testagent/service"
arangodb "github.com/arangodb-helper/testagent/service/cluster/arangodb"
"github.com/arangodb-helper/testagent/service/test"
complex "github.com/arangodb-helper/testagent/tests/complex"
"github.com/arangodb-helper/testagent/tests/simple"
logging "github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Configuration data with defaults:
const (
projectName = "testAgent"
defaultOperationTimeout = time.Minute * 6
defaultRetryTimeout = time.Minute * 8
defaultStepTimeout = time.Second * 15
)
var (
projectVersion = "dev"
projectBuild = "dev"
cmdMain = cobra.Command{
Use: projectName,
Short: "Test long running operations on ArangoDB clusters while introducing chaos",
Run: cmdMainRun,
}
log = logging.MustGetLogger(projectName)
appFlags struct {
port int
service.ServiceConfig
arangodb.ArangodbConfig
simple.SimpleConfig
complex.ComplextTestConfig
complex.DocColConfig
complex.GraphTestConf
logLevel string
}
maskAny = errors.WithStack
)
func init() {
f := cmdMain.Flags()
defaultDockerEndpoints := []string{"unix:///var/run/docker.sock"}
// Full test list:
// defaultTestList := []string{"simple", "DocColTest", "OneShardTest", "CommunityGraphTest", "SmartGraphTest", "EnterpriseGraphTest"}
// Use only "simple" test by default for backwards compatibility
defaultTestList := []string{"simple"}
f.IntVar(&appFlags.AgencySize, "agency-size", 3, "Number of agents in the cluster")
f.IntVar(&appFlags.port, "port", 4200, "First port of range of ports used by the testAgent")
f.StringVar(&appFlags.logLevel, "log-level", "debug", "Minimum log level (debug|info|warning|error)")
f.IntVar(&appFlags.ServiceConfig.ChaosConfig.ChaosLevel, "chaos-level", 4, "Chaos level. Default: 4.")
f.StringVar(&appFlags.ArangodbImage, "arangodb-image", getEnvVar("ARANGODB_IMAGE", "arangodb/arangodb-starter"), "name of the Docker image containing arangodb (the cluster starter)")
f.StringVar(&appFlags.ArangoImage, "arango-image", getEnvVar("ARANGO_IMAGE", ""), "name of the Docker image containing arangod (the database)")
f.StringVar(&appFlags.NetworkBlockerImage, "network-blocker-image", getEnvVar("NETWORK_BLOCKER_IMAGE", ""), "name of the Docker image containing network-blocker")
f.StringSliceVar(&appFlags.DockerEndpoints, "docker-endpoint", defaultDockerEndpoints, "Endpoints used to reach the docker daemons")
f.StringVar(&appFlags.DockerHostIP, "docker-host-ip", "", "IP of the docker host")
f.BoolVar(&appFlags.DockerNetHost, "docker-net-host", false, "If set, run all containers with `--net=host`")
f.BoolVar(&appFlags.ForceOneShard, "force-one-shard", false, "If set, force one shard arangodb cluster")
f.BoolVar(&appFlags.ReplicationVersion2, "replication-version-2", false, "If set, use replication version 2")
f.BoolVar(&appFlags.FailedWriteConcern403, "return-403-on-failed-write-concern", false, "If set, option `--cluster.failed-write-concern-status-code` will not be set for DB servers, bringing it to the default value of 403. Otherwise this parameter will be set to 503.")
f.StringVar(&appFlags.DockerInterface, "docker-interface", "docker0", "Network interface used to connect docker containers to")
f.StringVar(&appFlags.ReportDir, "report-dir", getEnvVar("REPORT_DIR", "."), "Directory in which failure reports will be created")
f.BoolVar(&appFlags.CollectMetrics, "collect-metrics", false, "If set, metrics will be collected and saved into files.")
f.StringVar(&appFlags.MetricsDir, "metrics-dir", getEnvVar("METRICS_DIR", "."), "Directory in which metrics will be stored")
f.BoolVar(&appFlags.Privileged, "privileged", false, "If set, run all containers with `--privileged`")
f.IntVar(&appFlags.ChaosConfig.MaxMachines, "max-machines", 10, "Upper limit to the number of machines in a cluster")
f.StringSliceVar(&appFlags.EnableTests, "enable-test", defaultTestList, "Enable particular test. Default: run all tests. Available tests: simple, DocColTest, OneShardTest, CommunityGraphTest, SmartGraphTest, EnterpriseGraphTest")
f.IntVar(&appFlags.SimpleConfig.MaxDocuments, "simple-max-documents", 20000, "Upper limit to the number of documents created in simple test")
f.IntVar(&appFlags.SimpleConfig.MaxCollections, "simple-max-collections", 10, "Upper limit to the number of collections created in simple test")
f.DurationVar(&appFlags.SimpleConfig.OperationTimeout, "simple-operation-timeout", defaultOperationTimeout, "Timeout per database operation")
f.DurationVar(&appFlags.SimpleConfig.RetryTimeout, "simple-retry-timeout", defaultRetryTimeout, "How long are tests retried before giving up")
f.Int64Var(&appFlags.GraphTestConf.MaxVertices, "graph-max-vertices", 150000, "Upper limit to the number of vertices (graph tests)")
f.IntVar(&appFlags.GraphTestConf.VertexSize, "graph-vertex-size", 4096, "The size of the payload field in bytes in all vertices (graph tests)")
f.IntVar(&appFlags.GraphTestConf.EdgeSize, "graph-edge-size", 8192, "The size of the payload field in bytes in all edges (graph tests)")
f.IntVar(&appFlags.GraphTestConf.TraversalOperationsPerCycle, "graph-traversal-ops", 50, "How many traversal operations to perform in one test cycle (graph tests)")
f.IntVar(&appFlags.GraphTestConf.BatchSize, "graph-batch-size", 250, "Batch size for creating vertices/edges (graph tests)")
f.IntVar(&appFlags.DocColConfig.MaxDocuments, "doc-max-documents", 50000, "Upper limit to the number of documents created in document collection tests")
f.IntVar(&appFlags.DocColConfig.BatchSize, "doc-batch-size", 250, "Batch size for creating documents in document tests")
f.IntVar(&appFlags.DocColConfig.DocumentSize, "doc-document-size", 20480, "The size of payload field in bytes in regular documents in document collection tests")
f.IntVar(&appFlags.DocColConfig.MaxUpdates, "doc-max-updates", 3, "Number of update operations to be performed on each document")
f.IntVar(&appFlags.ComplextTestConfig.NumberOfShards, "complex-shards", 10, "Number of shards (\"complex\" test suite)")
f.IntVar(&appFlags.ComplextTestConfig.ReplicationFactor, "complex-replicationFactor", 2, "Replication factor (\"complex\" test suite)")
f.DurationVar(&appFlags.ComplextTestConfig.OperationTimeout, "complex-operation-timeout", defaultOperationTimeout, "Timeout per database operation (\"complex\" test suite)")
f.DurationVar(&appFlags.ComplextTestConfig.RetryTimeout, "complex-retry-timeout", defaultRetryTimeout, "How long are tests retried before giving up (\"complex\" test suite)")
f.DurationVar(&appFlags.ComplextTestConfig.StepTimeout, "complex-step-timeout", defaultStepTimeout, "Pause between test actions (\"complex\" test suite)")
}
// handleSignal listens for termination signals and stops this process onup termination.
func handleSignal(sigChannel chan os.Signal, stopChan chan struct{}) {
signalCount := 0
for s := range sigChannel {
signalCount++
fmt.Println("Received signal:", s)
if signalCount > 1 {
os.Exit(1)
}
stopChan <- struct{}{}
}
}
func main() {
cmdMain.Execute()
}
func cmdMainRun(cmd *cobra.Command, args []string) {
logging.SetFormatter(logging.MustStringFormatter(`%{time:15:04:05.000} %{shortfunc} %{message}`))
log.Infof("Starting %s version %s, build %s", projectName, projectVersion, projectBuild)
level, err := logging.LogLevel(appFlags.logLevel)
if err != nil {
Exitf("Invalid log-level '%s': %#v", appFlags.logLevel, err)
}
logging.SetLevel(level, projectName)
appFlags.ArangodbConfig.Verbose = appFlags.logLevel == "debug"
// Get host IP
if appFlags.ArangodbConfig.DockerHostIP == "" {
if !appFlags.DockerNetHost && os.Getenv("RUNNING_IN_DOCKER") != "" {
log.Fatal("When running in docker you must specify a --docker-host-ip")
}
ip, err := findLocalIP()
if err != nil {
log.Fatalf("Cannot detect local IP: %v", err)
}
log.Infof("Detected local IP %s", ip)
appFlags.ArangodbConfig.DockerHostIP = ip
}
if appFlags.DockerNetHost {
// Network chaos is not supported with host networking
appFlags.ChaosConfig.DisableNetworkChaos = true
}
// Setup ports
appFlags.ServerPort = appFlags.port
appFlags.ArangodbConfig.MasterPort = appFlags.port + 1
// Interrupt signal:
sigChannel := make(chan os.Signal)
stopChan := make(chan struct{}, 10)
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
go handleSignal(sigChannel, stopChan)
// Create cluster builder
log.Debug("creating arangodb cluster builder")
if appFlags.CollectMetrics {
if _, err := os.Stat(appFlags.MetricsDir); os.IsNotExist(err) {
log.Info("Metrics directory does not exist. Creating: %s", appFlags.MetricsDir)
if err := os.Mkdir(appFlags.MetricsDir, 0755); err != nil {
Exitf("Can't create metrics directory: %#v", err)
}
}
}
cb, err := arangodb.NewArangodbClusterBuilder(log, appFlags.MetricsDir, appFlags.CollectMetrics, appFlags.ArangodbConfig)
if err != nil {
log.Fatalf("Failed to create cluster builder: %#v", err)
}
// Create tests
tests := []test.TestScript{}
if slices.Contains(appFlags.EnableTests, "simple") {
tests = append(tests, simple.NewSimpleTest(log, appFlags.ReportDir, appFlags.SimpleConfig))
}
if slices.Contains(appFlags.EnableTests, "DocColTest") {
tests = append(tests, complex.NewRegularDocColTest(log, appFlags.ReportDir, appFlags.ComplextTestConfig, appFlags.DocColConfig))
}
if slices.Contains(appFlags.EnableTests, "OneShardTest") {
tests = append(tests, complex.NewOneShardTest(log, appFlags.ReportDir, appFlags.ComplextTestConfig, appFlags.DocColConfig))
}
if slices.Contains(appFlags.EnableTests, "CommunityGraphTest") {
tests = append(tests, complex.NewComGraphTest(log, appFlags.ReportDir, appFlags.ComplextTestConfig, appFlags.GraphTestConf))
}
if slices.Contains(appFlags.EnableTests, "SmartGraphTest") {
tests = append(tests, complex.NewSmartGraphTest(log, appFlags.ReportDir, appFlags.ComplextTestConfig, appFlags.GraphTestConf))
}
if slices.Contains(appFlags.EnableTests, "EnterpriseGraphTest") {
tests = append(tests, complex.NewEnterpriseGraphTest(log, appFlags.ReportDir, appFlags.ComplextTestConfig, appFlags.GraphTestConf))
}
// Create service
log.Debug("creating service")
appFlags.ServiceConfig.ProjectVersion = projectVersion
appFlags.ServiceConfig.ProjectBuild = projectBuild
service, err := service.NewService(appFlags.ServiceConfig, service.ServiceDependencies{
Logger: log,
ClusterBuilder: cb,
Tests: tests,
})
if err != nil {
log.Fatalf("Failed to create service: %#v", err)
}
// Run the service
if err := service.Run(stopChan, true); err != nil {
log.Fatalf("Run failed: %#v", err)
}
log.Info("Test completed")
}
// getEnvVar returns the value of the environment variable with given key of the given default
// value of no such variable exist or is empty.
func getEnvVar(key, defaultValue string) string {
value := os.Getenv(key)
if value != "" {
return value
}
return defaultValue
}
func Exitf(format string, args ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
fmt.Printf(format, args...)
os.Exit(1)
}
func findLocalIP() (string, error) {
ifas, err := net.InterfaceAddrs()
if err != nil {
return "", maskAny(err)
}
for _, ia := range ifas {
// check the address type and if it is not a loopback the display it
if ipnet, ok := ia.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", maskAny(fmt.Errorf("No suitable address found"))
}