/
processor.go
1312 lines (1135 loc) · 45.7 KB
/
processor.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2020 (c) Cognizant Digital Business, Evolutionary AI. All rights reserved. Issued under the Apache 2.0 License.
package main
// This file contains the implementation of a main processing loop
// for handling pubsub messages and dispatching then after extracting data
// from firebase
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime/debug"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/valyala/fastjson"
"golang.org/x/crypto/ssh"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/dgryski/go-farm"
runnerReports "github.com/leaf-ai/studio-go-runner/internal/gen/dev.cognizant_dev.ai/genproto/studio-go-runner/reports/v1"
"github.com/leaf-ai/go-service/pkg/network"
"github.com/leaf-ai/studio-go-runner/internal/defense"
"github.com/leaf-ai/studio-go-runner/internal/request"
pkgResources "github.com/leaf-ai/studio-go-runner/internal/resources"
"github.com/leaf-ai/studio-go-runner/internal/runner"
"github.com/leaf-ai/studio-go-runner/internal/task"
"github.com/dustin/go-humanize"
"github.com/karlmutch/go-shortid"
"github.com/go-stack/stack"
"github.com/jjeffery/kv" // MIT License
)
type processor struct {
Group string `json:"group"` // A caller specific grouping for work that can share sensitive resources
RootDir string `json:"root_dir"`
ExprDir string `json:"expr_dir"`
ExprSubDir string `json:"expr_sub_dir"`
ExprEnvs map[string]string `json:"expr_envs"`
Request *request.Request `json:"request"` // merge these two fields, to avoid split data in a DB and some in JSON
QueueCreds string `json:"credentials_file"`
Artifacts *runner.ArtifactCache
Executor Executor
ready chan bool // Used by the processor to indicate it has released resources or state has changed
AccessionID string // A unique identifier for this task
ResponseQ chan *runnerReports.Report // A response queue the runner can employ to send progress updates on
}
type tempSafe struct {
dir string
sync.Mutex
}
var (
// Used to store machine resource prfile
machineResources = &pkgResources.Resources{}
// tempRoot is used to store information about the root directory uses by the
// runner
tempRoot = tempSafe{}
// A shared cache for all projects exists that is used by processors
artifactCache = runner.NewArtifactCache()
// Used to initialize a logger sink into which the artifact caching code in the runner
// can send error messages for the application to determine what action is taken with
// caching kv.that might be short lived
cacheReport sync.Once
// Guards against multiple threads of processing claiming a single directory
guardExprDir sync.Mutex
)
const (
// ExecUnknown is an unused guard value
ExecUnknown = iota
// ExecPythonVEnv indicates we are using the python virtualenv packaging
ExecPythonVEnv
// ExecSingularity inidcates we are using the Singularity container packaging and runtime
ExecSingularity
fmtAddLog = `[{"op": "add", "path": "/studioml/log/-", "value": {"ts": "%s", "msg":"%s"}}]`
)
func init() {
res, err := pkgResources.NewResources(*tempOpt)
if err != nil {
logger.Fatal("could not initialize disk space tracking", "err", err.Error())
}
machineResources = res
// A cache exists on linux for cuda lets remove it as it
// can cause issues
errGo := os.RemoveAll("$HOME/.nv")
if errGo != nil {
logger.Fatal("could not clear the $HOME/.nv cache", "err", errGo.Error())
}
}
func cacheReporter(ctx context.Context) {
for {
select {
case err := <-artifactCache.ErrorC:
logger.Info("artifact cache error", "error", err, "stack", stack.Trace().TrimRuntime())
case <-ctx.Done():
return
}
}
}
// Executor is an interface that defines a job handling worker implementation. Each variant of a worker
// conforms to a standard processor interface
//
type Executor interface {
// Make is used to allow a script to be generated for the specific run strategy being used
Make(alloc *pkgResources.Allocated, e interface{}) (err kv.Error)
// Run will execute the worker task used by the experiment
Run(ctx context.Context, refresh map[string]request.Artifact) (err kv.Error)
// Close can be used to tidy up after an experiment has completed
Close() (err kv.Error)
}
// Singleton style initialization to instantiate and overridding directory
// for the entire server working area
//
func makeCWD() (temp string, err kv.Error) {
tempRoot.Lock()
defer tempRoot.Unlock()
if tempRoot.dir == "" {
id, errGo := shortid.Generate()
if errGo != nil {
return "", kv.Wrap(errGo, "temp file id generation failed").With("stack", stack.Trace().TrimRuntime())
}
if tempRoot.dir, errGo = ioutil.TempDir(*tempOpt, "gorun_"+id); errGo != nil {
return "", kv.Wrap(errGo, "temp file create failed").With("stack", stack.Trace().TrimRuntime())
}
}
return tempRoot.dir, nil
}
// newProcessor will parse the inbound message and then validate that there are
// sufficient resources to run an experiment and then create a new working directory.
//
func newProcessor(ctx context.Context, qt *task.QueueTask, accessionID string) (proc *processor, hardError bool, err kv.Error) {
// When a processor is initialized make sure that the logger is enabled first time through
//
cacheReport.Do(func() {
go cacheReporter(ctx)
})
temp, err := makeCWD()
if err != nil {
return nil, false, err
}
// Processors share the same root directory and use acccession numbers on the experiment key
// to avoid collisions
//
proc = &processor{
RootDir: temp,
Group: qt.Subscription,
QueueCreds: qt.Credentials[:],
ready: make(chan bool),
AccessionID: accessionID,
ResponseQ: qt.ResponseQ,
}
// Extract processor information from the message received on the wire, includes decryption etc
if hardError, err = proc.unpackMsg(qt); hardError == true || err != nil {
return proc, hardError, err
}
// Recheck the alloc using the encrypted resource description
if _, err = proc.allocate(false); err != nil {
return proc, false, err
}
if _, err = proc.mkUniqDir(); err != nil {
return proc, false, err
}
// Determine the type of execution that is needed for this job by
// inspecting the artifacts specified
//
mode := ExecUnknown
for group := range proc.Request.Experiment.Artifacts {
if len(group) == 0 {
continue
}
switch group {
case "workspace":
if mode == ExecUnknown {
mode = ExecPythonVEnv
}
case "_singularity":
mode = ExecSingularity
}
}
switch mode {
case ExecPythonVEnv:
if proc.Executor, err = runner.NewVirtualEnv(proc.Request, proc.ExprDir, proc.AccessionID, proc.ResponseQ); err != nil {
return nil, true, err
}
case ExecSingularity:
if proc.Executor, err = runner.NewSingularity(proc.Request, proc.ExprDir); err != nil {
return nil, true, err
}
default:
return nil, true, kv.NewError("unable to determine execution class from artifacts").With("stack", stack.Trace().TrimRuntime()).
With("mode", mode, "project", proc.Request.Config.Database.ProjectId).With("experiment", proc.Request.Experiment.Key)
}
return proc, false, nil
}
// unpackMsg will use the message payload inside the queueTask (qt) and transform it into a payload
// inside the processor, handling any validation and decryption needed
//
func (proc *processor) unpackMsg(qt *task.QueueTask) (hardError bool, err kv.Error) {
// Check to see if we have an encrypted or signed request
if isEnvelope, _ := defense.IsEnvelope(qt.Msg); isEnvelope {
if qt.Wrapper == nil {
return false, kv.NewError("encrypted msg support not enabled").With("stack", stack.Trace().TrimRuntime())
}
// First load in the clear text portion of the message and test its resource request
// against available resources before decryption
envelope, err := defense.UnmarshalEnvelope(qt.Msg)
if err != nil {
return true, err
}
if _, err = allocResource(&envelope.Message.Resource, "", false); err != nil {
return false, err
}
if len(envelope.Message.Signature) == 0 {
return false, kv.NewError("encrypted payload has no signature").With("stack", stack.Trace().TrimRuntime())
}
if len(envelope.Message.Fingerprint) == 0 {
return false, kv.NewError("payload signature has no fingerprint").With("stack", stack.Trace().TrimRuntime())
}
// Now check the signature by getting the queue name and then looking for the applicable
// public key inside the signature store
pubKey, fp, err := GetRqstSigs().SelectSSH(qt.ShortQName)
if err != nil {
return false, err
}
if fp != envelope.Message.Fingerprint {
logger.Info("payload signature has an unmatched fingerprint", "fingerprint", fp, "message.Fingerprint", envelope.Message.Fingerprint)
}
sigBin, errGo := base64.StdEncoding.DecodeString(envelope.Message.Signature)
if errGo != nil {
return false, kv.Wrap(errGo).With("signature", envelope.Message.Signature).With("stack", stack.Trace().TrimRuntime())
}
err = nil
func() {
defer func() {
if r := recover(); r != nil {
err = kv.Wrap(r.(error)).With("stack", stack.Trace().TrimRuntime())
}
}()
// First try for the RFC format using the parser
sig, errSig := defense.ParseSSHSignature(sigBin)
if errSig != nil {
// We could have 64 byte blob so just try to use that
if len(sigBin) == 64 {
sig = &ssh.Signature{
Format: "ssh-ed25519",
Blob: sigBin,
}
} else {
err = errSig
return
}
}
if err == nil {
if errGo := pubKey.Verify([]byte(envelope.Message.Payload), sig); errGo != nil {
err = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
}()
if err != nil {
return false, err
}
// Decrypt, using the wrapper, the master request structure and assign it to our task
if proc.Request, err = qt.Wrapper.Request(envelope); err != nil {
return true, err
}
} else {
if !*acceptClearTextOpt {
return true, kv.NewError("unencrypted messages not enabled").With("stack", stack.Trace().TrimRuntime())
}
// restore the msg into the processing data structure from the JSON queue payload
if proc.Request, err = request.UnmarshalRequest(qt.Msg); err != nil {
return true, err
}
}
return hardError, nil
}
// Close will release all resources and clean up the work directory that
// was used by the studioml work
//
func (p *processor) Close() (err error) {
if *debugOpt || 0 == len(p.ExprDir) {
return nil
}
return os.RemoveAll(p.ExprDir)
}
// fetchAll is used to retrieve from the storage system employed by studioml any and all available
// artifacts and to unpack them into the experiment directory. fetchAll is called by the deployAndRun
// receiver.
// This function will try to constrain artifacts fetched to the disk size specified by the requested
// disk space that was defined in the experiments resource request. This is used tp defang
// zip bombs to some extent and tries to prevent disk space exhaustion.
//
func (p *processor) fetchAll(ctx context.Context) (err kv.Error) {
diskBytes, errGo := humanize.ParseBytes(p.Request.Experiment.Resource.Hdd)
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
diskBudget := int64(diskBytes)
for group, artifact := range p.Request.Experiment.Artifacts {
// Artifacts that have no qualified location will be ignored
if 0 == len(artifact.Qualified) {
continue
}
// This artifact is downloaded during the runtime pass not beforehand
if group == "_singularity" {
continue
}
// Extract all available artifacts into subdirectories of the main experiment directory.
//
// The current convention is that the archives include the directory name under which
// the files are unpacked in their table of contents
//
size, warns, err := artifactCache.Fetch(ctx, artifact.Clone(), p.Request.Config.Database.ProjectId, group, diskBudget, p.ExprEnvs, p.ExprDir)
diskBudget -= size
if diskBudget < 0 {
err = kv.NewError("disk budget exhausted")
}
if err != nil {
msg := "artifact fetch failed"
msgDetail := []interface{}{
"group", group,
"project", p.Request.Config.Database.ProjectId,
"Experiment", p.Request.Experiment.Key,
"stack", stack.Trace().TrimRuntime(),
"err", err,
}
if artifact.Mutable {
logger.Debug(msg, msgDetail)
} else {
logger.Warn(msg, msgDetail)
}
msgDetail[len(msgDetail)-2] = "warning"
for _, warn := range warns {
msgDetail[len(msgDetail)-1] = warn
if artifact.Mutable {
logger.Debug(msg, msgDetail)
} else {
logger.Warn(msg, msgDetail)
}
}
// Mutable artifacts can be create only items that dont yet exist on the storage platform
if !artifact.Mutable {
return err.With(msgDetail...)
}
}
}
return nil
}
func jsonEscape(unescaped string) (escaped string, errGo error) {
b, errGo := json.Marshal(unescaped)
if errGo != nil {
return escaped, errGo
}
escaped = string(b)
return escaped[1 : len(escaped)-1], nil
}
// copyToMetaData is used to copy a file to the meta data area using the file naming semantics
// of the metadata layout
func (p *processor) copyToMetaData(src string, jsonDest string) (err kv.Error) {
logger.Debug("copying start", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
defer logger.Debug("copying done", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
fStat, errGo := os.Stat(src)
if errGo != nil {
return kv.Wrap(errGo).With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
if !fStat.Mode().IsRegular() {
return kv.NewError("not a regular file").With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
source, errGo := os.Open(filepath.Clean(src))
if errGo != nil {
return kv.Wrap(errGo).With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
defer source.Close()
// If there is no need to scan the file look for json data to scrape from it
// simply copy the file and return
if len(jsonDest) == 0 {
return kv.NewError("the json destination is missing").With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
// If we need to scrape the file then we should scan it line by line
jsonDestination, errGo := os.OpenFile(jsonDest, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0600)
if errGo != nil {
return kv.Wrap(errGo).With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
defer func() {
jsonDestination.Close()
// Uploading a zero length json file is pointless as we do have a record of
// the presence of an experiment left by the metadata file
if fileInfo, errGo := os.Stat(jsonDest); errGo == nil {
if fileInfo.Size() == 0 {
_ = os.Remove(jsonDest)
logger.Debug("removing empty scrape file", "scrape_file", jsonDest)
}
}
}()
// Store any discovered json fragments for generating experiment documents as a single collection
jsonDirectives := []string{}
autoCapture := *captureOutputMD
// Checkmarx code checking note. Checkmarx is for Web applications and is not a good fit general purpose server code.
// It is also worth mentioning that if you are reading this message that Checkmarx does not understand Go package structure
// and does not appear to use the Go AST to validate code so is not able to perform path and escape analysis which
// means that more than 95% of warning are for unvisited code.
//
// The following will raise a 'Denial Of Service Resource Exhaustion' message but this is bogus.
// The scanner in go is space limited intentially to prevent resource exhaustion.
logger.Debug("scrape start", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
s := bufio.NewScanner(source)
s.Split(bufio.ScanLines)
for s.Scan() {
line := strings.TrimSpace(s.Text())
if len(line) <= 2 {
continue
}
// See if this line is a sensible json fragment
if !((line[0] == '{' && line[len(line)-1] == '}') ||
(line[0] == '[' && line[len(line)-1] == ']')) {
// If we dont have a fragment we check to see if in the line should be formatted as
// json and inserted using a command line switch
if !autoCapture {
continue
}
line, errGo = jsonEscape(line)
if errGo != nil {
logger.Trace("output json filter failed", "error", errGo, "line", line, "stack", stack.Trace().TrimRuntime())
continue
}
line = fmt.Sprintf(fmtAddLog, time.Now().UTC().Format("2006-01-02T15:04:05.999999999-0700"), line)
}
// After each line is scanned the json fragment is merged into a collection of all detected patches and merges that
// have been output by the experiment
if errGo = fastjson.Validate(line); errGo != nil {
if logger.IsTrace() {
logger.Trace("output json filter failed", "error", errGo, "line", line, "stack", stack.Trace().TrimRuntime())
}
continue
}
jsonDirectives = append(jsonDirectives, line)
if logger.IsTrace() {
logger.Trace("json filter added", "line", line, "stack", stack.Trace().TrimRuntime())
}
}
logger.Debug("scrape stop", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
if len(jsonDirectives) == 0 {
logger.Debug("no json directives found", "stack", stack.Trace().TrimRuntime())
return nil
}
// Zero copy prepend
jsonDirectives = append(jsonDirectives, " ")
copy(jsonDirectives[1:], jsonDirectives[0:])
jsonDirectives[0] = `{"studioml": {"log": [{"ts": "0", "msg":"Init"},{"ts":"1", "msg":""}]}}`
logger.Debug("JSONEditor start", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
defer logger.Debug("JSONEditor end", "source", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
result, err := runner.JSONEditor("", jsonDirectives)
if err != nil {
return err
}
if _, errGo = fmt.Fprintln(jsonDestination, result); errGo != nil {
return kv.Wrap(errGo).With("src", src, "jsonDest", jsonDest, "stack", stack.Trace().TrimRuntime())
}
return nil
}
// updateMetaData is used to update files and artifacts related to the experiment
// that reside in the meta data area
//
func (p *processor) updateMetaData(group string, artifact request.Artifact, accessionID string) (err kv.Error) {
metaDir := filepath.Join(p.ExprDir, "_metadata")
if _, errGo := os.Stat(metaDir); os.IsNotExist(errGo) {
os.MkdirAll(metaDir, 0700)
}
switch group {
case "output":
src := filepath.Join(p.ExprDir, "output", "output")
jsonDest := filepath.Join(metaDir, "scrape-host-"+accessionID+".json")
return p.copyToMetaData(src, jsonDest)
default:
return kv.NewError("group unrecognized").With("group", group, "stack", stack.Trace().TrimRuntime())
}
}
// returnOne is used to upload a single artifact to the data store specified by the experimenter
//
func (p *processor) returnOne(ctx context.Context, group string, artifact request.Artifact, accessionID string) (uploaded bool, warns []kv.Error, err kv.Error) {
// Meta data is specialized
if len(accessionID) != 0 {
switch group {
case "output":
if err = p.updateMetaData(group, artifact, accessionID); err != nil {
logger.Warn("output artifact could not be used for metadata", "project_id", p.Request.Config.Database.ProjectId,
"ep.Request.Experiment.Keyxperiment_id", p.Request.Experiment.Key, "error", err.Error())
}
}
}
if _, errGo := os.Stat(filepath.Join(p.ExprDir, group)); os.IsNotExist(errGo) {
logger.Debug("upload skipped", "project_id", p.Request.Config.Database.ProjectId,
"experiment_id", p.Request.Experiment.Key, "file", filepath.Join(p.ExprDir, group), "error", errGo.Error())
return false, warns, nil
}
logger.Debug("uploading artifact", "project_id", p.Request.Config.Database.ProjectId,
"experiment_id", p.Request.Experiment.Key, "file", filepath.Join(p.ExprDir, group))
defer logger.Debug("upload artifact done", "project_id", p.Request.Config.Database.ProjectId,
"experiment_id", p.Request.Experiment.Key, "file", filepath.Join(p.ExprDir, group))
return artifactCache.Restore(ctx, &artifact, p.Request.Config.Database.ProjectId, group, p.ExprEnvs, p.ExprDir)
}
// returnAll creates tar archives of the experiments artifacts and then puts them
// back to the studioml shared storage
//
func (p *processor) returnAll(ctx context.Context, accessionID string) {
returned := make([]string, 0, len(p.Request.Experiment.Artifacts))
// Accessioning can modify the system artifacts and so the order we traverse
// is important, we want the _metadata artifact after the _output
// artifact which can be done using a descending sort which places underscores
// before lowercase letters
//
keys := make([]string, 0, len(p.Request.Experiment.Artifacts))
for group := range p.Request.Experiment.Artifacts {
keys = append(keys, group)
}
sort.Strings(keys)
for _, group := range keys {
if artifact, isPresent := p.Request.Experiment.Artifacts[group]; isPresent {
if artifact.Mutable {
uploaded, warns, err := p.returnOne(ctx, group, artifact, accessionID)
if err != nil {
logger.Debug("return error", "project_id", p.Request.Config.Database.ProjectId, "group", group, "error", err.Error())
} else {
if uploaded {
returned = append(returned, group)
}
}
for _, warn := range warns {
logger.Debug("return warning", "project_id", p.Request.Config.Database.ProjectId, "group", group, "warning", warn.Error())
}
}
}
}
if len(returned) != 0 {
logger.Info("project returned", "project_id", p.Request.Config.Database.ProjectId, "result", strings.Join(returned, ", "))
}
}
// allocate is used to reserve the resources on the local host needed to handle the entire job as
// a highwater mark.
//
// The returned alloc structure should be used with the deallocate function otherwise resource
// leaks will occur.
//
func (p *processor) allocate(liveRun bool) (alloc *pkgResources.Allocated, err kv.Error) {
return allocResource(&p.Request.Experiment.Resource, p.Request.Experiment.Key, liveRun)
}
// deallocate first releases resources and then triggers a ready channel to notify any listener that the
func (p *processor) deallocate(alloc *pkgResources.Allocated, id string) {
deallocResource(alloc, id)
// Only wait a second to alert others that the resources have been released
// before simply carrying on without doing the notify
//
select {
case <-time.After(time.Second):
case p.ready <- true:
}
}
// Process is the main function where experiment processing occurs.
//
// This function is invoked by the cmd/runner/handle.go:HandleMsg function and blocks.
//
func (p *processor) Process(ctx context.Context) (ack bool, err kv.Error) {
// Call the allocation function to get access to resources and get back
// the allocation we received
alloc, err := p.allocate(true)
if err != nil {
return false, kv.Wrap(err, "allocation failed").With("stack", stack.Trace().TrimRuntime())
}
// Setup a function to release resources that have been allocated and
// use a panic handler to catch issues related to, or unrelated to the runner
//
defer func() {
if r := recover(); r != nil {
logger.Warn("panic", "panic", fmt.Sprintf("%#+v", r), "stack", string(debug.Stack()))
if err != nil {
// Modify the return values to include details about the panic, but be sure not to
// obscure earlier failures
err = kv.NewError("panic").With("panic", fmt.Sprintf("%#+v", r)).With("stack", stack.Trace().TrimRuntime())
}
}
p.deallocate(alloc, p.Request.Experiment.Key)
}()
// The ResponseQ is a means of sending informative messages to a listener
// acting as an experiment orchestration agent while experiments are running
if p.ResponseQ != nil {
select {
case p.ResponseQ <- &runnerReports.Report{
Time: timestamppb.Now(),
ExecutorId: &wrappers.StringValue{
Value: network.GetHostName(),
},
UniqueId: &wrappers.StringValue{
Value: p.AccessionID,
},
ExperimentId: &wrappers.StringValue{
Value: p.Request.Experiment.Key,
},
Payload: &runnerReports.Report_Logging{
Logging: &runnerReports.LogEntry{
Time: timestamppb.Now(),
Severity: runnerReports.LogSeverity_Info,
Message: &wrappers.StringValue{
Value: "start",
},
Fields: map[string]string{},
},
},
}:
default:
logger.Warn("unresponsive response queue channel")
}
}
// The allocation details are passed in to the runner to allow the
// resource reservations to become known to the running applications.
// This call will block until the task stops processing.
if _, err = p.deployAndRun(ctx, alloc, p.AccessionID); err != nil {
if p.ResponseQ != nil {
select {
case p.ResponseQ <- &runnerReports.Report{
Time: timestamppb.Now(),
ExecutorId: &wrappers.StringValue{
Value: network.GetHostName(),
},
UniqueId: &wrappers.StringValue{
Value: p.AccessionID,
},
ExperimentId: &wrappers.StringValue{
Value: p.Request.Experiment.Key,
},
Payload: &runnerReports.Report_Logging{
Logging: &runnerReports.LogEntry{
Time: timestamppb.Now(),
Severity: runnerReports.LogSeverity_Info,
Message: &wrappers.StringValue{
Value: "stop",
},
Fields: map[string]string{
"error": err.Error(),
"success": "False",
},
},
},
}:
default:
logger.Warn("unresponsive response queue channel")
}
}
return false, err
}
if p.ResponseQ != nil {
select {
case p.ResponseQ <- &runnerReports.Report{
Time: timestamppb.Now(),
ExecutorId: &wrappers.StringValue{
Value: network.GetHostName(),
},
UniqueId: &wrappers.StringValue{
Value: p.AccessionID,
},
ExperimentId: &wrappers.StringValue{
Value: p.Request.Experiment.Key,
},
Payload: &runnerReports.Report_Logging{
Logging: &runnerReports.LogEntry{
Time: timestamppb.Now(),
Severity: runnerReports.LogSeverity_Info,
Message: &wrappers.StringValue{
Value: "stop",
},
Fields: map[string]string{
"success": "True",
},
},
},
}:
default:
logger.Warn("unresponsive response queue channel")
}
}
return true, nil
}
// getHash produces a very simple and short hash for use in generating directory names from
// the experiment IDs assign by users to shorten the names and defang them
//
func getHash(text string) string {
// hasher := md5.New()
// hasher.Write([]byte(text))
// return hex.EncodeToString(hasher.Sum(nil))
//
// The stadtx hash could improve on this, see https://github.com/dgryski/go-stadtx. However
// it appears the impl was never set in stone and the author has disappeared from github
//
return fmt.Sprintf("%x", farm.Hash64([]byte(text)))
}
// mkUniqDir will create a working directory for an experiment
// using the file system calls appropriately so as to make sure
// no other instance of the same experiment is using it. It is
// only being used by the caller and for which no race conditions
// during creation would have occurred.
//
// A new UUID could have been used to do this but that makes
// diagnosis of failed experiements very difficult so we keep a meaningful
// name for the new directory and use an index on the end of the experiment
// id so that during diagnosis we know exactly which attempts came first.
//
// There are lots of easier methods to create unique directories of course,
// but most involve using long unique identifies.
//
// This function will fill in the name being used into the structure being
// used for the method scope on success.
//
// The name of a created working directory will be returned that can be used
// for the dynamic portions of processing such as for creating
// the python virtual environment and also script files used by the runner. This
// isolates experimenter supplied files from the runners working files and
// can be prevent uploading artifacts needlessly.
//
func (p *processor) mkUniqDir() (dir string, err kv.Error) {
_ = os.MkdirAll(filepath.Join(p.RootDir, "experiments"), 0700)
// Shorten any excessively massively long names supplied by users
expDir := getHash(p.Request.Experiment.Key)
inst := 0
direct := ""
guardExprDir.Lock()
defer guardExprDir.Unlock()
// Loop until we fail to find a directory with the prefix
for {
direct = filepath.Join(p.RootDir, "experiments", expDir+"."+strconv.Itoa(inst))
// Create the next directory in sequence with another directory containing our signature
errGo := os.Mkdir(direct, 0700)
switch {
case errGo == nil:
p.ExprDir = direct
p.ExprSubDir = expDir + "." + strconv.Itoa(inst)
return "", nil
case os.IsExist(errGo):
inst++
continue
}
err = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
logger.Warn("failure creating working dir", "directory", direct, "error", err)
return "", err
}
}
// extractValidEnv is used to convert the environment variables of the current process
// into a map removing any names that dont translate to valid user environment variables,
// such as names that start with underscores etc
//
func extractValidEnv() (envs map[string]string) {
envs = map[string]string{}
for _, v := range os.Environ() {
// After the first equal keep everything else together
pair := strings.SplitN(v, "=", 2)
// Extract the first unicode rune and test that it is a valid character for an env name
envName := []rune(pair[0])
if len(pair) == 2 && (unicode.IsLetter(envName[0]) || unicode.IsDigit(envName[0])) {
pair[1] = strings.Replace(pair[1], "\"", "\\\"", -1)
envs[pair[0]] = pair[1]
} else {
// The underscore is always present and represents the CWD so dont print messages about it
if envName[0] != '_' {
logger.Debug(fmt.Sprintf("env var %s (%c) (%d) dropped due to conformance", pair[0], envName[0], len(pair)))
}
}
}
return envs
}
// applyEnv is used to apply the contents of the env block specified by the studioml client into the
// runners environment table.
//
// this function is also used to examine the contents of the processor request environment variables and
// to resolve locally any environment variables that are present indicated by the %...% pairs.
// If the enclosed value is not an environment variable within the context of the runner then the
// text will be left untouched.
//
// This behavior is specific to the go runner at this time.
//
func (p *processor) applyEnv(alloc *pkgResources.Allocated) {
p.ExprEnvs = extractValidEnv()
// Expand %...% pairs by iterating the env table for the process and explicitly replacing on each line
re := regexp.MustCompile(`(?U)(?:\%(.*)*\%)+`)
// Checkmarx code checking note. Checkmarx is for Web applications and is not a good fit general purpose server code.
// It is also worth mentioning that if you are reading this message that Checkmarx does not understand Go package structure
// and does not appear to use the Go AST to validate code so is not able to perform path and escape analysis which
// means that more than 95% of warning are for unvisited code.
//
// The following will raise a 'Denial Of Service Resource Exhaustion' message but this is bogus.
// The data used to generate the p.Request values comes from a managed json message that is validated.
// Environment variables need to be applied here to assist in unpacking S3 files etc
for k, v := range p.Request.Config.Env {
for _, match := range re.FindAllString(v, -1) {
if envV := os.Getenv(match[1 : len(match)-1]); len(envV) != 0 {
v = strings.Replace(v, match, envV, -1)
}
}
// Update the processor env table with the resolved value
p.Request.Config.Env[k] = v
p.ExprEnvs[k] = v
}
// create the map into which customer environment variables will be added to
// the experiment script
//
p.ExprEnvs["AWS_SDK_LOAD_CONFIG"] = "1"
// Although we copy the env values to the runners env table through they done get
// automatically included into the script this is done via the Make being given
// a set of env variables as an array that will be written into the script using the receiever
// contents.
//
for _, gpu := range alloc.GPU {
for env, gpuVar := range gpu.Env {
if len(gpuVar) != 0 {
if expVar, isPresent := p.ExprEnvs[env]; isPresent {
p.ExprEnvs[env] = expVar + "," + gpuVar
} else {
p.ExprEnvs[env] = gpuVar
}
}
}
}
}
func (p *processor) calcTimeLimit() (maxDuration time.Duration) {
// Determine when the life time of the experiment is over and then check it before starting
// the experiment. when running this function also checks to ensure the lifetime has not expired
//
maxDuration = time.Duration(96 * time.Hour)
if len(p.Request.Config.Lifetime) != 0 {
limit, errGo := time.ParseDuration(p.Request.Config.Lifetime)
if errGo != nil {
logger.Warn("maximum life time ignored", "error", errGo,
"project_id", p.Request.Config.Database.ProjectId, "experiment_id", p.Request.Experiment.Key,
"stack", stack.Trace().TrimRuntime())
} else {
if p.Request.Experiment.TimeAdded > 10.0 {
limit = time.Until(time.Unix(int64(p.Request.Experiment.TimeAdded), 0).Add(limit))
if limit <= 0 {
logger.Warn("maximum life time reached",
"project_id", p.Request.Config.Database.ProjectId, "experiment_id", p.Request.Experiment.Key,
"stack", stack.Trace().TrimRuntime())
return 0
}
if limit < maxDuration {
maxDuration = limit
}
}
}
}
// Determine the maximum run duration for any single attempt to run the experiment
if len(p.Request.Experiment.MaxDuration) != 0 {
limit, errGo := time.ParseDuration(p.Request.Experiment.MaxDuration)
if errGo != nil {
logger.Warn("maximum duration ignored", "error", errGo,
"project_id", p.Request.Config.Database.ProjectId, "experiment_id", p.Request.Experiment.Key,
"stack", stack.Trace().TrimRuntime())
}
if limit < maxDuration {
maxDuration = limit
}
}
return maxDuration
}
func (p *processor) checkpointStart(ctx context.Context, accessionID string, refresh map[string]request.Artifact, saveTimeout time.Duration) (doneC chan struct{}) {
doneC = make(chan struct{}, 1)
// On a regular basis we will flush the log and compress it for uploading to
// AWS or Google Cloud Storage etc, use the interval specified in the meta data for the job
//
saveDuration := time.Duration(600 * time.Minute)