/
etcd_stress.go
221 lines (194 loc) · 5.38 KB
/
etcd_stress.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
package main
import (
"crypto/tls"
"errors"
"flag"
"fmt"
"net/http"
"net/url"
"regexp"
"runtime"
"strings"
"github.com/coreos/go-etcd/etcd"
"github.com/jenspinney/etcdstress/generator"
)
var (
etcdFlags *ETCDFlags
dataCountRequested int
numPopulateWorkers int
expectedDataCount int
expectedDataTolerance float64
etcdClient *etcd.Client
)
const (
DEBUG = "debug"
INFO = "info"
ERROR = "error"
FATAL = "fatal"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.IntVar(&dataCountRequested, "dataCountRequested", 0, "number of entries to create")
flag.IntVar(&numPopulateWorkers, "numPopulateWorkers", 2, "number of workers to use when populating etcd")
etcdFlags = AddETCDFlags(flag.CommandLine)
flag.Parse()
fmt.Println("initializing etcd client")
etcdOptions, err := etcdFlags.Validate()
if err != nil {
panic("failed to load etcd flags")
}
etcdClient = initializeEtcdClient(etcdOptions)
purge("/data")
fmt.Printf("%#v\n\n", flag.Args())
if dataCountRequested > 0 {
fmt.Println("Here about to generate")
dataGenerator := generator.NewEtcdFiller(numPopulateWorkers, etcdClient)
expectedDataCount, err = dataGenerator.Generate(dataCountRequested)
if err != nil {
panic("failed to generate data")
}
fmt.Printf("requested %d entries, received %d\n", dataCountRequested, expectedDataCount)
expectedDataTolerance = float64(expectedDataCount) * generator.ERROR_TOLERANCE
}
}
type ETCDFlags struct {
etcdCertFile string
etcdKeyFile string
etcdCaFile string
clusterUrls string
clientSessionCacheSize int
maxIdleConnsPerHost int
}
func AddETCDFlags(flagSet *flag.FlagSet) *ETCDFlags {
flags := &ETCDFlags{}
flagSet.StringVar(
&flags.clusterUrls,
"etcdCluster",
"http://127.0.0.1:4001",
"comma-separated list of etcd URLs (scheme://ip:port)",
)
flagSet.StringVar(
&flags.etcdCertFile,
"etcdCertFile",
"",
"Location of the client certificate for mutual auth",
)
flagSet.StringVar(
&flags.etcdKeyFile,
"etcdKeyFile",
"",
"Location of the client key for mutual auth",
)
flagSet.StringVar(
&flags.etcdCaFile,
"etcdCaFile",
"",
"Location of the CA certificate for mutual auth",
)
flagSet.IntVar(
&flags.clientSessionCacheSize,
"etcdSessionCacheSize",
0,
"Capacity of the ClientSessionCache option on the TLS configuration. If zero, golang's default will be used",
)
flagSet.IntVar(
&flags.maxIdleConnsPerHost,
"etcdMaxIdleConnsPerHost",
0,
"Controls the maximum number of idle (keep-alive) connctions per host. If zero, golang's default will be used",
)
return flags
}
func (flags *ETCDFlags) Validate() (*ETCDOptions, error) {
scheme := ""
clusterUrls := strings.Split(flags.clusterUrls, ",")
for i, uString := range clusterUrls {
uString = strings.TrimSpace(uString)
clusterUrls[i] = uString
u, err := url.Parse(uString)
if err != nil {
return nil, fmt.Errorf("Invalid cluster URL: '%s', error: [%s]", uString, err.Error())
}
if scheme == "" {
if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.New("Invalid scheme: " + uString)
}
scheme = u.Scheme
} else if scheme != u.Scheme {
return nil, fmt.Errorf("Multiple url schemes provided: %s", flags.clusterUrls)
}
}
isSSL := false
if scheme == "https" {
isSSL = true
if flags.etcdCertFile == "" {
return nil, errors.New("Cert file must be provided for https connections")
}
if flags.etcdKeyFile == "" {
return nil, errors.New("Key file must be provided for https connections")
}
}
return &ETCDOptions{
CertFile: flags.etcdCertFile,
KeyFile: flags.etcdKeyFile,
CAFile: flags.etcdCaFile,
ClusterUrls: clusterUrls,
IsSSL: isSSL,
ClientSessionCacheSize: flags.clientSessionCacheSize,
MaxIdleConnsPerHost: flags.maxIdleConnsPerHost,
}, nil
}
type ETCDOptions struct {
CertFile string
KeyFile string
CAFile string
ClusterUrls []string
IsSSL bool
ClientSessionCacheSize int
MaxIdleConnsPerHost int
}
func initializeEtcdClient(etcdOptions *ETCDOptions) *etcd.Client {
var etcdClient *etcd.Client
var tr *http.Transport
if etcdOptions.IsSSL {
if etcdOptions.CertFile == "" || etcdOptions.KeyFile == "" {
panic(errors.New("Require both cert and key path"))
}
var err error
etcdClient, err = etcd.NewTLSClient(etcdOptions.ClusterUrls, etcdOptions.CertFile, etcdOptions.KeyFile, etcdOptions.CAFile)
if err != nil {
panic(err)
}
tlsCert, err := tls.LoadX509KeyPair(etcdOptions.CertFile, etcdOptions.KeyFile)
if err != nil {
panic(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{tlsCert},
InsecureSkipVerify: true,
ClientSessionCache: tls.NewLRUClientSessionCache(etcdOptions.ClientSessionCacheSize),
}
tr = &http.Transport{
TLSClientConfig: tlsConfig,
Dial: etcdClient.DefaultDial,
MaxIdleConnsPerHost: etcdOptions.MaxIdleConnsPerHost,
}
etcdClient.SetTransport(tr)
} else {
etcdClient = etcd.NewClient(etcdOptions.ClusterUrls)
}
etcdClient.SetConsistency(etcd.STRONG_CONSISTENCY)
return etcdClient
}
func purge(key string) {
_, err := etcdClient.Delete(key, true)
if err != nil {
matches, matchErr := regexp.Match(".*Key not found.*", []byte(err.Error()))
if matchErr != nil {
panic(matchErr)
}
if !matches {
fmt.Println("No data to purge")
}
}
}