-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
339 lines (296 loc) · 9.66 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/airnandez/chasqui/fileserver"
)
const (
defaultClientCA = "ca.pem"
defaultClientCert = ""
defaultClientKey = ""
)
type clientConfig struct {
// Command line options
help bool
addr string
ca string
cert string
key string
}
func clientCmd() command {
fset := flag.NewFlagSet("chasqui client", flag.ExitOnError)
config := clientConfig{}
fset.BoolVar(&config.help, "help", false, "")
fset.StringVar(&config.addr, "addr", defaultClientAddr, "")
fset.StringVar(&config.ca, "ca", defaultClientCA, "")
fset.StringVar(&config.cert, "cert", defaultClientCert, "")
fset.StringVar(&config.key, "key", defaultClientKey, "")
run := func(args []string) error {
fset.Usage = func() { clientUsage(args[0], os.Stderr) }
fset.Parse(args[1:])
return clientRun(args[0], config)
}
return command{fset: fset, run: run}
}
func clientRun(cmdName string, config clientConfig) error {
if config.help {
clientUsage(cmdName, os.Stderr)
return nil
}
errlog = setErrlog("client")
debug(1, "running client with:")
debug(1, " addr='%s'\n", config.addr)
debug(1, " ca='%s'\n", config.ca)
debug(1, " cert='%s'\n", config.cert)
debug(1, " key='%s'\n", config.key)
// Process requests
return clientHandleRequests(config)
}
func clientHandleRequests(config clientConfig) error {
http.HandleFunc("/load", func(w http.ResponseWriter, r *http.Request) {
clientLoadRequestHandler(config, w, r)
})
http.HandleFunc("/stop", clientStopRequestHandler)
return http.ListenAndServe(config.addr, nil) // TODO: should be HTTPS
}
func clientLoadRequestHandler(config clientConfig, w http.ResponseWriter, r *http.Request) {
// Ensure method is POST
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
// Ensure HTTP request body is not empty
if r.Body == nil {
http.Error(w, "empty request body", http.StatusBadRequest)
return
}
// Decode the request contained in the body
var payload LoadRequest
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
debug(1, "received request %v", payload)
// Verify the incoming request is valid
if err := clientVerifyLoadRequest(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Execute the requested operation
resp, err := clientProcessLoadRequest(config, &payload)
if err != nil {
debug(1, "error processing load request: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Serialize and send the response
debug(1, "sending response %v", resp)
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
io.Copy(w, &buf)
}
func clientVerifyLoadRequest(req *LoadRequest) error {
if len(req.ServerAddrs) == 0 {
return fmt.Errorf("server addresses not included in load request")
}
if req.Duration < 0 {
return fmt.Errorf("invalid duration %s", req.Duration)
}
return nil
}
func clientProcessLoadRequest(config clientConfig, req *LoadRequest) (*LoadResponse, error) {
// Create channels to send requests to the workers and receive
// responses from them. We start as many workers as specified
// in the request. If nothing was specified in the request,
// create twice as many workers as there are CPU cores in this
// computer.
numWorkers := req.Concurrency
if numWorkers <= 0 {
numWorkers = 2 * runtime.NumCPU()
}
numWorkers = minInt(numWorkers, 1000*runtime.NumCPU())
requests := make(chan *DownloadReq, numWorkers)
responses := make(chan *DownloadResp, numWorkers)
// Prepare the fileserver clients for serving this load request
fsclients := make([]*fileserver.Client, len(req.ServerAddrs))
for i := range req.ServerAddrs {
c, err := fileserver.NewClient(req.UseHttp1, config.cert, config.key, config.ca)
if err != nil {
return nil, fmt.Errorf("could not initialize fileserver client [%s]", err)
}
fsclients[i] = c
}
// Start collecting responses from workers
summary := make(chan *LoadResponse)
go clientCollectResponses(numWorkers, responses, summary)
// Start the workers
debug(1, "starting %d workers", numWorkers)
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go clientWorker(i, &wg, requests)
}
// Start emitting requests
go clientEmitRequests(config, req, fsclients, requests, responses)
// Wait for workers to finish their execution
wg.Wait()
debug(1, "all workers finished execution")
close(responses)
// Close connections to servers
for i := range fsclients {
fsclients[i].CloseIdleConnections()
}
// Receive summary of worker responses
finalResp := <-summary
close(summary)
return finalResp, nil
}
// clientEmitRequests emits file download requests against the file servers. The emitted requests are
// executed by workers
func clientEmitRequests(config clientConfig, req *LoadRequest, fsclients []*fileserver.Client, requests chan *DownloadReq, responses chan *DownloadResp) {
timeout := time.After(req.Duration)
numServers := len(req.ServerAddrs)
seqNumber := uint64(0)
notAfter := time.Now().Add(req.Duration)
loop:
for {
seqNumber += 1
s := rand.Intn(numServers)
newreq := &DownloadReq{
seqNumber: seqNumber,
server: req.ServerAddrs[s],
fsclient: fsclients[s],
fileID: fmt.Sprintf("file-%d", seqNumber),
size: uint64(req.MeanSize) + uint64(rand.NormFloat64()*float64(req.StdSize)),
notAfter: notAfter,
replyTo: responses,
}
select {
case <-timeout:
// Stop generating requests
break loop
case requests <- newreq:
}
}
// Inform the workers no more requests will be emitted
close(requests)
debug(1, "stopped emitting download requests")
}
func clientCollectResponses(numWorkers int, responses chan *DownloadResp, summary chan *LoadResponse) {
totalSize := float64(0) // MB
fileCount, errCount := uint64(0), uint64(0)
start := time.Now()
for resp := range responses {
if resp.err != nil {
errCount += 1
debug(1, "error from worker: seqNumber=%d %s\n", resp.seqNumber, resp.err)
continue
}
fileCount += 1
totalSize += float64(resp.size) / float64(MB)
}
summary <- &LoadResponse{
Start: start,
End: time.Now(),
Concurrency: numWorkers,
NumFiles: fileCount,
DataSize: totalSize,
Rate: float64(totalSize) / time.Since(start).Seconds(),
ErrCount: errCount,
}
}
type LoadRequest struct {
// Network addresses of the servers involved in this test
ServerAddrs []string
// Duration of this test
Duration time.Duration
// Number of concurrent download operations
Concurrency int
// Use HTTP1 for download operations. By default use HTTP2
UseHttp1 bool
// Mean and std of the file size to request to the servers (bytes)
MeanSize uint64
StdSize uint64
}
type LoadResponse struct {
// Start and end times
Start time.Time
End time.Time
// Number of concurrent download operations
Concurrency int
// Number of files downloaded in this test
NumFiles uint64
// Volume of data downloaded data in this test (in MB)
DataSize float64
// Download rate for this test: MB/sec
Rate float64
// Number of errors observed in this test
ErrCount uint64
}
func clientStopRequestHandler(w http.ResponseWriter, r *http.Request) {
// Ensure method is POST
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
// TODO: stop execution: this is not an elegant way of stopping
os.Exit(0)
}
// masterUsage prints the usage information about the 'master' subcommand
func clientUsage(cmd string, f *os.File) {
const clientTempl = `
USAGE:
{{.Tab1}}{{.AppName}} {{.SubCmd}} [-addr=<network address>] [-ca=<file>] [-cert=<file>]
{{.Tab1}}{{.AppNameFiller}} {{.SubCmdFiller}} [-key=<file>]
{{.Tab1}}{{.AppName}} {{.SubCmd}} -help
DESCRIPTION:
{{.Tab1}}'{{.AppName}} {{.SubCmd}}' starts a process to download data from the file servers.
{{.Tab1}}A client waits for instructions from the driver process and emits download
{{.Tab1}}requests against the file servers. It sends back to the driver process a
{{.Tab1}}report on the execution of each request.
OPTIONS:
{{.Tab1}}-addr=<network address>
{{.Tab2}}network address this client process listens to for receiving
{{.Tab2}}instructions from the driver process. The form of the address is
{{.Tab2}}'host:port'.
{{.Tab2}}Default: {{.DefaultClientAddr}}
{{.Tab1}}-ca=<file>
{{.Tab2}}path of the PEM-formatted file which contains the certificates of the
{{.Tab2}}certification authorities this client process trusts. The client process
{{.Tab2}}requires the file server to present a certificate and verifies that
{{.Tab2}}certificate is issued by one of the trusted certification authorities
{{.Tab2}}included in this file.
{{.Tab2}}Default: "{{.DefaultClientCA}}"
{{.Tab1}}-cert=<file>
{{.Tab2}}path of the PEM-formatted file which contains the certificate this
{{.Tab2}}client process uses for identifying itself to the file server.
{{.Tab2}}Default: "{{.DefaultClientCert}}"
{{.Tab1}}-key=<file>
{{.Tab2}}path of the PEM-formatted file which contains the private key of
{{.Tab2}}the certificate specified with the '-cert' option.
{{.Tab2}}Default: "{{.DefaultClientKey}}"
{{.Tab1}}-help
{{.Tab2}}print this help
`
tmplFields["SubCmd"] = cmd
tmplFields["SubCmdFiller"] = strings.Repeat(" ", len(cmd))
tmplFields["DefaultClientAddr"] = defaultClientAddr
tmplFields["DefaultClientCA"] = defaultClientCA
tmplFields["DefaultClientCert"] = defaultClientCert
tmplFields["DefaultClientKey"] = defaultClientKey
render(clientTempl, tmplFields, f)
}