-
Notifications
You must be signed in to change notification settings - Fork 9
/
transfer.go
541 lines (507 loc) · 13.8 KB
/
transfer.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package testruns
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/mit-dci/opencbdc-tctl/common"
"github.com/mit-dci/opencbdc-tctl/coordinator/sources"
"github.com/mit-dci/opencbdc-tctl/logging"
"github.com/mit-dci/opencbdc-tctl/wire"
)
// copyFiles describes which files to copy from the given system role upon
// completion of the test
var copyFiles = map[common.SystemRole][]string{
common.SystemRoleArchiver: {
"tp_samples.txt",
"block_log.txt%%OPT",
},
common.SystemRoleRaftAtomizer: {
"tp_samples.txt%%OPT",
"block_log.txt%%OPT",
"discarded_log.txt%%OPT",
"complete_tx_log.txt%%OPT",
"state_machine_log.txt%%OPT",
"raft_store_log.txt%%OPT",
"tx_notify_log.txt%%OPT",
},
common.SystemRoleAtomizerCliWatchtower: {
"latency_samples_%IDX%.txt%%OPT",
"tx_samples_%IDX%.txt%%OPT",
},
common.SystemRoleSentinel: {},
common.SystemRoleShard: {
"tp_samples.txt%%OPT",
"block_log.txt%%OPT",
},
common.SystemRoleCoordinator: {},
common.SystemRoleWatchtower: {
"tp_samples.txt%%OPT",
"block_log.txt%%OPT",
},
common.SystemRoleShardTwoPhase: {},
common.SystemRoleSentinelTwoPhase: {},
common.SystemRoleTwoPhaseGen: {
"tx_samples_%IDX%.txt",
},
}
// CopyOutputs will use the `copyFiles` map to instruct the agents to upload all
// indicated files from its file system to S3 so that the coordinator can
// download them later
func (t *TestRunManager) CopyOutputs(
tr *common.TestRun,
envs map[int32][]byte,
ignoreErrors bool,
) error {
path := filepath.Join(
common.DataDir(),
fmt.Sprintf("testruns/%s/outputs", tr.ID),
)
t.UpdateStatus(
tr,
common.TestRunStatusRunning,
"Uploading testrun output files from agents to S3 (0%)",
)
allDownloads := make([]common.S3Download, 0)
allDownloadsLock := sync.Mutex{}
f := func(role *common.TestRunRole) error {
if len(copyFiles[common.SystemRole(role.Role)]) > 0 {
for _, f := range t.SubstituteParameters(copyFiles[common.SystemRole(role.Role)], role, tr) {
// Optionally ignore failures for files that may or may not
// exist on the target agent
ignoreFile := false
if strings.HasSuffix(f, "%%OPT") {
f = f[:len(f)-5]
ignoreFile = true
}
// Calculate the target path in the outputs bucket based on the
// test run ID, system role, index and filename
targetPath := fmt.Sprintf(
"testruns/%s/outputs/%s-%d-%s",
tr.ID,
string(role.Role),
role.Index,
f,
)
// Instruct the agent to upload the file to S3
msg, err := t.am.QueryAgentWithTimeout(
role.AgentID,
&wire.UploadFileToS3RequestMsg{
EnvironmentID: envs[role.AgentID],
SourcePath: f,
TargetRegion: os.Getenv("AWS_REGION"),
TargetBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
TargetPath: targetPath,
},
3*time.Minute,
)
err = t.processS3UploadResponse(role.AgentID, msg, err)
if err != nil {
// Watchtower CLI temporarily ignored due to new tx_samples
// usage (optional)
if ignoreErrors || ignoreFile {
logging.Warnf(
"Ignoring error while copying file %s from agent %d: %v",
f,
role.AgentID,
err,
)
continue
}
return err
}
// Append this uploaded file to the array of downloads
allDownloadsLock.Lock()
allDownloads = append(allDownloads, common.S3Download{
TargetPath: filepath.Join(
path,
fmt.Sprintf(
"%s-%d-%s",
string(role.Role),
role.Index,
f,
),
),
SourceRegion: os.Getenv("AWS_REGION"),
SourceBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
SourcePath: targetPath,
Retries: 10,
})
allDownloadsLock.Unlock()
}
}
return nil
}
err := t.RunForAllAgents(
f,
tr,
"Uploading testrun output files from agents to S3",
time.Minute*10,
)
if err != nil {
return err
}
// Append all downloads to the downloads array for the test run. This is
// done to defer the downloading until after the roles are shut down, not
// keeping them online longer than necessary. Once everything is in S3 we
// can download that after the roles have been shut down.
tr.PendingResultDownloads = append(
tr.PendingResultDownloads,
allDownloads...)
return nil
}
// GetPerformanceProfiles instructs the agents to upload the performance data
// gathered while running the command(s) to S3
func (t *TestRunManager) GetPerformanceProfiles(
tr *common.TestRun,
cmds []runningCommand,
envs map[int32][]byte,
) error {
errs := make([]error, 0)
// The path where we store all performance data in S3 derive from the test
// run ID
path := filepath.Join(
common.DataDir(),
fmt.Sprintf("testruns/%s/performanceprofiles", tr.ID),
)
err := os.MkdirAll(path, 0755)
if err != nil && !errors.Is(err, os.ErrExist) {
return err
}
t.WriteLog(
tr,
"Instructing agents to upload their performance profiles to S3",
)
total := len(cmds)
done := int32(0)
wg := sync.WaitGroup{}
wg.Add(total)
allDownloads := make([]common.S3Download, 0)
allDownloadsLock := sync.Mutex{}
for _, c := range cmds {
go func(cmd runningCommand, complete *int32) {
// The regular performance counters that are always gathered
files := []string{fmt.Sprintf("perf_%x.txt", cmd.commandID)}
if tr.RunPerf {
// These files are only created when we run the command in
// `perf`
files = append(
files,
fmt.Sprintf("perf_%x.data", cmd.commandID),
)
files = append(
files,
fmt.Sprintf("perf_%x.script", cmd.commandID),
)
files = append(
files,
fmt.Sprintf("perf_%x.data.tar.bz2", cmd.commandID),
)
}
for _, f := range files {
// Instruct the agent to upload the file
msg, err := t.am.QueryAgentWithTimeout(
cmd.agentID,
&wire.UploadFileToS3RequestMsg{
EnvironmentID: envs[cmd.agentID],
SourcePath: f,
TargetRegion: os.Getenv("AWS_REGION"),
TargetBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
TargetPath: fmt.Sprintf(
"testruns/%s/performanceprofiles/%s",
tr.ID,
f,
),
},
30*time.Second,
)
// If anything went wrong, append it to the errors array and
// stop trying further uploads
err = t.processS3UploadResponse(cmd.agentID, msg, err)
if err != nil {
errs = append(errs, err)
break
}
// Append the uploaded files to the list of things to download
// once we're done
allDownloadsLock.Lock()
allDownloads = append(allDownloads, common.S3Download{
TargetPath: filepath.Join(path, f),
SourceRegion: os.Getenv("AWS_REGION"),
SourceBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
SourcePath: fmt.Sprintf(
"testruns/%s/performanceprofiles/%s",
tr.ID,
f,
),
Retries: 10,
})
allDownloadsLock.Unlock()
}
t.UpdateStatus(
tr,
common.TestRunStatusRunning,
fmt.Sprintf(
"Copying performance profiles (%.1f%%)",
float64(atomic.AddInt32(complete, 1))/float64(total)*50,
),
)
wg.Done()
}(c, &done)
}
wg.Wait()
if len(errs) > 0 {
for _, e := range errs {
logging.Errorf("%v", e)
}
logging.Warnf(
"%d errors occurred while copying performance data",
len(errs),
)
t.WriteLog(
tr,
"%d errors occurred while copying performance data",
len(errs),
)
}
// Append all downloads to the downloads array for the test run.
tr.PendingResultDownloads = append(
tr.PendingResultDownloads,
allDownloads...)
return nil
}
// GetPerformanceProfiles instructs the agents to upload the performance data
// gathered while running the command(s) to S3
func (t *TestRunManager) GetLogFiles(
tr *common.TestRun,
cmds []runningCommand,
envs map[int32][]byte,
) error {
// TODO: refactor with GetPerformanceProfiles and CopyOutputs
errs := make([]error, 0)
// The path where we store all performance data in S3 derive from the test
// run ID
path := filepath.Join(
common.DataDir(),
fmt.Sprintf("testruns/%s/logs", tr.ID),
)
err := os.MkdirAll(path, 0755)
if err != nil && !errors.Is(err, os.ErrExist) {
return err
}
t.WriteLog(
tr,
"Instructing agents to upload their log files to S3",
)
total := len(cmds)
done := int32(0)
wg := sync.WaitGroup{}
wg.Add(total)
allDownloads := make([]common.S3Download, 0)
allDownloadsLock := sync.Mutex{}
for _, c := range cmds {
go func(cmd runningCommand, complete *int32) {
// The regular performance counters that are always gathered
files := []string{
fmt.Sprintf("command_%x_stdout.txt", cmd.commandID),
fmt.Sprintf("command_%x_stderr.txt", cmd.commandID),
}
for _, f := range files {
// Instruct the agent to upload the file
msg, err := t.am.QueryAgentWithTimeout(
cmd.agentID,
&wire.UploadFileToS3RequestMsg{
EnvironmentID: envs[cmd.agentID],
SourcePath: f,
TargetRegion: os.Getenv("AWS_REGION"),
TargetBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
TargetPath: fmt.Sprintf(
"testruns/%s/logs/%s",
tr.ID,
f,
),
},
120*time.Second,
)
// If anything went wrong, append it to the errors array and
// stop trying further uploads
err = t.processS3UploadResponse(cmd.agentID, msg, err)
if err != nil {
errs = append(errs, err)
break
}
// Append the uploaded files to the list of things to download
// once we're done
allDownloadsLock.Lock()
allDownloads = append(allDownloads, common.S3Download{
TargetPath: filepath.Join(path, f),
SourceRegion: os.Getenv("AWS_REGION"),
SourceBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
SourcePath: fmt.Sprintf(
"testruns/%s/logs/%s",
tr.ID,
f,
),
Retries: 10,
})
allDownloadsLock.Unlock()
}
t.UpdateStatus(
tr,
common.TestRunStatusRunning,
fmt.Sprintf(
"Copying log files (%.1f%%)",
float64(atomic.AddInt32(complete, 1))/float64(total)*50,
),
)
wg.Done()
}(c, &done)
}
wg.Wait()
if len(errs) > 0 {
for _, e := range errs {
logging.Errorf("%v", e)
}
logging.Warnf(
"%d errors occurred while copying log files",
len(errs),
)
t.WriteLog(
tr,
"%d errors occurred while copying log files",
len(errs),
)
}
// Append all downloads to the downloads array for the test run.
tr.PendingResultDownloads = append(
tr.PendingResultDownloads,
allDownloads...)
return nil
}
// RedownloadTestOutputsFromS3 will enumerate all files in the S3 bucket for
// the given testrun and download them all. This can be triggered from the user
// interface. This exists because sometimes files get either corrupted or fail
// downloading and redoing the downloads can help fix that.
func (t *TestRunManager) RedownloadTestOutputsFromS3(tr *common.TestRun) error {
downloads := make([]common.S3Download, 0)
prefix := fmt.Sprintf("testruns/%s/", tr.ID)
objects, err := t.awsm.ListObjectsInS3(
os.Getenv("AWS_REGION"),
os.Getenv("OUTPUTS_S3_BUCKET"),
prefix,
)
if err != nil {
return err
}
for _, o := range objects {
downloads = append(downloads, common.S3Download{
Retries: 10,
SourceRegion: os.Getenv("AWS_REGION"),
SourceBucket: os.Getenv("OUTPUTS_S3_BUCKET"),
SourcePath: o,
TargetPath: filepath.Join(common.DataDir(), o),
})
}
logging.Infof(
"Re-downloading %d outputs from S3 for testrun %s",
len(downloads),
tr.ID,
)
return t.awsm.DownloadMultipleFromS3(downloads)
}
// UploadBinaries upload binaries for this testrun to S3
func (t *TestRunManager) UploadBinaries(
tr *common.TestRun,
seeder bool,
) (string, error) {
hash := tr.CommitHash
debug := tr.RunPerf || tr.Debug
if seeder {
hash = tr.SeederHash
debug = false
}
sourcePath, err := sources.BinariesArchivePath(
hash,
debug,
)
if err != nil {
return "", err
}
binariesInS3 := fmt.Sprintf("binaries/%s.tar.gz", hash)
if debug {
// We need a separate archive for debug binaries since they perform
// much worse. We can't run debugging or perf on a binary set with
// optimizations because the stacktraces won't make much sense.
binariesInS3 = fmt.Sprintf("binaries/%s-debug.tar.gz", hash)
}
err = t.awsm.UploadToS3IfNotExists(common.S3Upload{
SourcePath: sourcePath,
TargetRegion: os.Getenv("AWS_REGION"),
TargetBucket: os.Getenv("BINARIES_S3_BUCKET"),
TargetPath: binariesInS3,
})
if err != nil {
return "", err
}
return binariesInS3, nil
}
// UploadConfig uploads the contents of the configuration file for the system
// to S3 for future reference
func (t *TestRunManager) UploadConfig(cfg []byte, tr *common.TestRun) error {
file, err := ioutil.TempFile("", "")
if err != nil {
return err
}
defer os.Remove(file.Name())
n, err := file.Write(cfg)
if n != len(cfg) || err != nil {
return fmt.Errorf(
"error writing file. wrote %d of %d, err: %v",
n,
len(cfg),
err,
)
}
return t.awsm.UploadToS3(common.S3Upload{
SourcePath: file.Name(),
TargetRegion: os.Getenv("AWS_REGION"),
TargetBucket: os.Getenv("BINARIES_S3_BUCKET"),
TargetPath: fmt.Sprintf("testruns/%s/outputs/config.cfg", tr.ID),
})
}
// processS3UploadResponse will look at the message and error returned by the
// call to QueryAgentWithTimeout with an UploadFileToS3RequestMsg. It will
// return the error from the function (if any), or check for a mismatching
// message type or false value in the Success member
// TODO: This could probably be generalized to an msg,err,expected type call
func (t *TestRunManager) processS3UploadResponse(
agentID int32,
msg wire.Msg,
err error,
) error {
if err == nil {
// Check if the return type is the expected type
if _, ok := msg.(*wire.UploadFileToS3ResponseMsg); !ok {
err = fmt.Errorf(
"expected UploadFileToS3ResponseMsg, got %T",
msg,
)
}
}
if err == nil {
// Check if the return message has true in its Success member
if resp, ok := msg.(*wire.UploadFileToS3ResponseMsg); ok &&
!resp.Success {
err = fmt.Errorf(
"agent %d returned success=false on S3 upload",
agentID,
)
}
}
return err
}