-
Notifications
You must be signed in to change notification settings - Fork 21
/
workunit.go
502 lines (425 loc) · 15.6 KB
/
workunit.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
package core
import (
//"bytes"
//"encoding/json"
"encoding/base64"
"fmt"
"io/ioutil"
"reflect"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/cwl"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/golib/httpclient"
//"github.com/davecgh/go-spew/spew"
//"gopkg.in/mgo.v2/bson"
"os"
//"path"
//"reflect"
//"regexp"
"regexp/syntax"
"strings"
"time"
)
const (
WORK_STAT_INIT = "init" // initial state
WORK_STAT_QUEUED = "queued" // after requeue ; after failures below max ; on WorkQueue.Add()
WORK_STAT_RESERVED = "reserved" // short lived state between queued and checkout. when a worker checks the workunit out, the state is reserved.
WORK_STAT_CHECKOUT = "checkout" // normal work checkout ; client registers that already has a workunit (e.g. after reboot of server)
WORK_STAT_SUSPEND = "suspend" // on MAX_FAILURE ; on SuspendJob
WORK_STAT_FAILED_PERMANENT = "failed-permanent" // app had exit code 42
WORK_STAT_DONE = "done" // client only: done
WORK_STAT_ERROR = "fail" // client only: workunit computation or IO error (variable was renamed to ERROR but not the string fail, to maintain backwards compability)
WORK_STAT_PREPARED = "prepared" // client only: after argument parsing
WORK_STAT_COMPUTED = "computed" // client only: after computation is done, before upload
WORK_STAT_DISCARDED = "discarded" // client only: job / task suspended or server UUID changes
WORK_STAT_PROXYQUEUED = "proxyqueued" // proxy only
)
type Workunit struct {
Workunit_Unique_Identifier `bson:",inline" json:",inline" mapstructure:",squash"`
Id string `bson:"id,omitempty" json:"id,omitempty" mapstructure:"id,omitempty"` // global identifier: jobid_taskid_rank (for backwards coompatibility only)
WuId string `bson:"wuid,omitempty" json:"wuid,omitempty" mapstructure:"wuid,omitempty"` // deprecated !
Info *Info `bson:"info,omitempty" json:"info,omitempty" mapstructure:"info,omitempty"`
Inputs []*IO `bson:"inputs,omitempty" json:"inputs,omitempty" mapstructure:"inputs,omitempty"`
Outputs []*IO `bson:"outputs,omitempty" json:"outputs,omitempty" mapstructure:"outputs,omitempty"`
Predata []*IO `bson:"predata,omitempty" json:"predata,omitempty" mapstructure:"predata,omitempty"`
Cmd *Command `bson:"cmd,omitempty" json:"cmd,omitempty" mapstructure:"cmd,omitempty"`
TotalWork int `bson:"totalwork,omitempty" json:"totalwork,omitempty" mapstructure:"totalwork,omitempty"`
Partition *PartInfo `bson:"part,omitempty" json:"part,omitempty" mapstructure:"part,omitempty"`
State string `bson:"state,omitempty" json:"state,omitempty" mapstructure:"state,omitempty"`
Failed int `bson:"failed,omitempty" json:"failed,omitempty" mapstructure:"failed,omitempty"`
CheckoutTime time.Time `bson:"checkout_time,omitempty" json:"checkout_time,omitempty" mapstructure:"checkout_time,omitempty"`
Client string `bson:"client,omitempty" json:"client,omitempty" mapstructure:"client,omitempty"`
ComputeTime int `bson:"computetime,omitempty" json:"computetime,omitempty" mapstructure:"computetime,omitempty"`
ExitStatus int `bson:"exitstatus,omitempty" json:"exitstatus,omitempty" mapstructure:"exitstatus,omitempty"` // Linux Exit Status Code (0 is success)
Notes []string `bson:"notes,omitempty" json:"notes,omitempty" mapstructure:"notes,omitempty"`
UserAttr map[string]interface{} `bson:"userattr,omitempty" json:"userattr,omitempty" mapstructure:"userattr,omitempty"`
ShockHost string `bson:"shockhost,omitempty" json:"shockhost,omitempty" mapstructure:"shockhost,omitempty"` // specifies default Shock host for outputs
CWL_workunit *CWL_workunit `bson:"cwl,omitempty" json:"cwl,omitempty" mapstructure:"cwl,omitempty"`
WorkPath string // this is the working directory. If empty, it will be computed.
WorkPerf *WorkPerf
Context *cwl.WorkflowContext `bson:"-" json:"-" mapstructure:"-"`
}
func NewWorkunit(qm *ServerMgr, task *Task, rank int, job *Job) (workunit *Workunit, err error) {
task_id := task.Task_Unique_Identifier
workunit = &Workunit{
Workunit_Unique_Identifier: New_Workunit_Unique_Identifier(task_id, rank),
Id: "defined below",
Cmd: task.Cmd,
//App: task.App,
Info: task.Info,
Inputs: task.Inputs,
Outputs: task.Outputs,
Predata: task.Predata,
TotalWork: task.TotalWork, //keep this info in workunit for load balancing
Partition: task.Partition,
State: WORK_STAT_INIT,
Failed: 0,
UserAttr: task.UserAttr,
ExitStatus: -1,
//AppVariables: task.AppVariables // not needed yet
}
var work_str string
work_str, err = workunit.String()
if err != nil {
err = fmt.Errorf("(NewWorkunit) workunit.String() returned: %s", err.Error())
return
}
workunit.Id = work_str
workunit.WuId = work_str
if task.WorkflowStep != nil {
workflow_step := task.WorkflowStep
workunit.CWL_workunit = &CWL_workunit{}
workunit.ShockHost = job.ShockHost
// ****** get CommandLineTool (or whatever can be executed)
p := workflow_step.Run
if p == nil {
err = fmt.Errorf("(NewWorkunit) process is nil !!?")
return
}
//var process_name string
var clt *cwl.CommandLineTool
//var a_workflow *cwl.Workflow
var process interface{}
process, _, err = cwl.GetProcess(p, job.WorkflowContext) // TODO add new schemata
if err != nil {
err = fmt.Errorf("(NewWorkunit) GetProcess returned: %s", err.Error())
return
}
if process == nil {
err = fmt.Errorf("(NewWorkunit) process == nil")
return
}
//var requirements *[]cwl.Requirement
switch process.(type) {
case *cwl.CommandLineTool:
clt, _ = process.(*cwl.CommandLineTool)
if clt.CwlVersion == "" {
clt.CwlVersion = job.WorkflowContext.CwlVersion
}
if clt.CwlVersion == "" {
err = fmt.Errorf("(NewWorkunit) CommandLineTool misses CwlVersion")
return
}
if clt.Namespaces == nil {
clt.Namespaces = job.WorkflowContext.Namespaces // not sure why the CommandLineTool does not have the namespaces... :-(
}
//requirements = clt.Requirements
case *cwl.ExpressionTool:
var et *cwl.ExpressionTool
et, _ = process.(*cwl.ExpressionTool)
if et.CwlVersion == "" {
et.CwlVersion = job.WorkflowContext.CwlVersion
}
if et.CwlVersion == "" {
err = fmt.Errorf("(NewWorkunit) ExpressionTool misses CwlVersion")
return
}
if et.Namespaces == nil {
et.Namespaces = job.WorkflowContext.Namespaces
}
default:
err = fmt.Errorf("(NewWorkunit) Tool %s not supported", reflect.TypeOf(process))
return
}
var shock_requirement *cwl.ShockRequirement
shock_requirement = job.CWL_ShockRequirement
if shock_requirement == nil {
err = fmt.Errorf("(NewWorkunit) shock_requirement == nil")
return
}
//shock_requirement, err = cwl.GetShockRequirement(requirements)
//if err != nil {
//fmt.Println("process:")
//spew.Dump(process)
// err = fmt.Errorf("(NewWorkunit) ShockRequirement not found , err: %s", err.Error())
// return
//}
if shock_requirement.Shock_api_url == "" {
err = fmt.Errorf("(NewWorkunit) Shock_api_url in ShockRequirement is empty")
return
}
workunit.ShockHost = shock_requirement.Shock_api_url
workunit.CWL_workunit.Tool = process
//}
//if use_workflow {
// wfl.CwlVersion = job.CwlVersion
//}
context := job.WorkflowContext
// ****** get inputs
job_input_map := context.Job_input_map
if job_input_map == nil {
err = fmt.Errorf("(NewWorkunit) job.CWL_collection.Job_input_map is empty")
return
}
//job_input_map := *job.CWL_collection.Job_input_map
//job_input := *job.CWL_collection.Job_input
var workflow_instance *WorkflowInstance
var ok bool
workflow_instance, ok, err = job.GetWorkflowInstance(task.Parent, true)
if err != nil {
err = fmt.Errorf("(NewWorkunit) GetWorkflowInstance returned %s", err.Error())
return
}
if !ok {
err = fmt.Errorf("(NewWorkunit) WorkflowInstance not found: %s", task.Parent)
return
}
workflow_input_map := workflow_instance.Inputs.GetMap()
var workunit_input_map map[string]cwl.CWLType
workunit_input_map, err = qm.GetStepInputObjects(job, task_id, workflow_input_map, workflow_step, context)
if err != nil {
err = fmt.Errorf("(NewWorkunit) GetStepInputObjects returned: %s", err.Error())
return
}
//fmt.Println("workunit_input_map after second round:\n")
//spew.Dump(workunit_input_map)
job_input := cwl.Job_document{}
for elem_id, elem := range workunit_input_map {
named_type := cwl.NewNamedCWLType(elem_id, elem)
job_input = append(job_input, named_type)
}
workunit.CWL_workunit.Job_input = &job_input
//spew.Dump(job_input)
workunit.CWL_workunit.OutputsExpected = &workflow_step.Out
err = workunit.Evaluate(workunit_input_map, context)
if err != nil {
err = fmt.Errorf("(NewWorkunit) workunit.Evaluate returned: %s", err.Error())
return
}
//spew.Dump(workflow_step.Out)
//panic("done")
}
//panic("done")
//spew.Dump(workunit.Cmd)
//panic("done")
return
}
func (w *Workunit) Evaluate(inputs interface{}, context *cwl.WorkflowContext) (err error) {
if w.CWL_workunit != nil {
process := w.CWL_workunit.Tool
switch process.(type) {
case *cwl.CommandLineTool:
clt := process.(*cwl.CommandLineTool)
err = clt.Evaluate(inputs, context)
if err != nil {
err = fmt.Errorf("(Workunit/Evaluate) CommandLineTool.Evaluate returned: %s", err.Error())
return
}
case *cwl.ExpressionTool:
et := process.(*cwl.ExpressionTool)
err = et.Evaluate(inputs, context)
if err != nil {
err = fmt.Errorf("(Workunit/Evaluate) ExpressionTool.Evaluate returned: %s", err.Error())
return
}
case *cwl.Workflow:
wf := process.(*cwl.Workflow)
err = wf.Evaluate(inputs, context)
if err != nil {
err = fmt.Errorf("(Workunit/Evaluate) Workflow.Evaluate returned: %s", err.Error())
return
}
default:
err = fmt.Errorf("(Workunit/Evaluate) Process type not supported %s", reflect.TypeOf(process))
return
}
}
return
}
func (w *Workunit) GetId() (id Workunit_Unique_Identifier) {
id = w.Workunit_Unique_Identifier
return
}
func (work *Workunit) Mkdir() (err error) {
// delete workdir just in case it exists; will not work if awe-worker is not in docker container AND tasks are in container
work_path, err := work.Path()
if err != nil {
err = fmt.Errorf("(Workunit/Mkdir) work.Path() returned: %s", err.Error)
return
}
err = os.RemoveAll(work_path)
if err != nil {
err = fmt.Errorf("(Workunit/Mkdir) os.RemoveAll returned: %s", err.Error)
return
}
err = os.MkdirAll(work_path, 0777)
if err != nil {
err = fmt.Errorf("(Workunit/Mkdir) os.MkdirAll returned: %s", err.Error)
return
}
return
}
func (work *Workunit) RemoveDir() (err error) {
work_path, err := work.Path()
if err != nil {
return
}
err = os.RemoveAll(work_path)
if err != nil {
return
}
return
}
func (work *Workunit) SetState(new_state string, reason string) (err error) {
if new_state == WORK_STAT_SUSPEND && reason == "" {
err = fmt.Errorf("To suspend you need to provide a reason")
return
}
work.State = new_state
if new_state != WORK_STAT_CHECKOUT {
work.Client = ""
}
if reason != "" {
if len(work.Notes) == 0 {
work.Notes = append(work.Notes, reason)
}
}
return
}
func (work *Workunit) Path() (path string, err error) {
if work.WorkPath == "" {
id := work.Workunit_Unique_Identifier.JobId
if id == "" {
err = fmt.Errorf("(Workunit/Path) JobId is missing")
return
}
task_name := work.Workunit_Unique_Identifier.Parent
if task_name != "" {
task_name += "-"
}
task_name += work.Workunit_Unique_Identifier.TaskName
// convert name to make it filesystem compatible
task_name = strings.Map(
func(r rune) rune {
if syntax.IsWordChar(r) || r == '-' { // word char: [0-9A-Za-z] and '-'
return r
}
return '_'
},
task_name)
work.WorkPath = fmt.Sprintf("%s/%s/%s/%s/%s_%s_%d", conf.WORK_PATH, id[0:2], id[2:4], id[4:6], id, task_name, work.Workunit_Unique_Identifier.Rank)
}
path = work.WorkPath
return
}
func (work *Workunit) CDworkpath() (err error) {
work_path, err := work.Path()
if err != nil {
return
}
return os.Chdir(work_path)
}
func (work *Workunit) GetNotes() string {
seen := map[string]bool{}
uniq := []string{}
for _, n := range work.Notes {
if _, ok := seen[n]; !ok {
uniq = append(uniq, n)
seen[n] = true
}
}
return strings.Join(uniq, "###")
}
//calculate the range of data part
//algorithm: try to evenly distribute indexed parts to workunits
//e.g. totalWork=4, totalParts=10, then each workunits have parts 3,3,2,2
func (work *Workunit) Part() (part string) {
if work.Rank == 0 {
return ""
}
partsize := work.Partition.TotalIndex / work.TotalWork //floor
remainder := work.Partition.TotalIndex % work.TotalWork
var start, end int
if work.Rank <= remainder {
start = (partsize+1)*(work.Rank-1) + 1
end = start + partsize
} else {
start = (partsize+1)*remainder + partsize*(work.Rank-remainder-1) + 1
end = start + partsize - 1
}
if start == end {
part = fmt.Sprintf("%d", start)
} else {
part = fmt.Sprintf("%d-%d", start, end)
}
return
}
func (work *Workunit) GetIdBase64() (work_id_b64 string, err error) {
var work_str string
work_str, err = work.String()
if err != nil {
err = fmt.Errorf("(NotifyWorkunitProcessedWithLogs) workid.String() returned: %s", err.Error())
return
}
work_id_b64 = "base64:" + base64.StdEncoding.EncodeToString([]byte(work_str))
return
}
func (work *Workunit) FetchDataToken() (token string, err error) {
var work_id_b64 string
work_id_b64, err = work.GetIdBase64()
if err != nil {
err = fmt.Errorf("(FetchDataToken) work.GetIdBase64 returned: %s", err.Error())
return
}
targeturl := fmt.Sprintf("%s/work/%s?datatoken&client=%s", conf.SERVER_URL, work_id_b64, Self.Id)
logger.Debug(1, "(FetchDataToken) targeturl: %s", targeturl)
var headers httpclient.Header
logger.Debug(3, "(FetchDataToken) len(conf.CLIENT_GROUP_TOKEN): %d ", len(conf.CLIENT_GROUP_TOKEN))
if conf.CLIENT_GROUP_TOKEN != "" {
headers = httpclient.Header{
"Authorization": []string{"CG_TOKEN " + conf.CLIENT_GROUP_TOKEN},
}
}
res, err := httpclient.Get(targeturl, headers, nil)
if err != nil {
err = fmt.Errorf("(FetchDataToken) httpclient.Get returned: %s", err.Error())
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
var bodyBytes []byte
bodyBytes, err = ioutil.ReadAll(res.Body)
if err != nil {
err = fmt.Errorf("(FetchDataToken) res.Status was %d, but could not read body: %s", res.StatusCode, err.Error())
return
}
bodyString := string(bodyBytes)
err = fmt.Errorf("(FetchDataToken) res.Status was %d, body contained: %s", res.StatusCode, bodyString)
return
}
if res.Header == nil {
logger.Debug(3, "(FetchDataToken) res.Header empty")
return
}
header_array, ok := res.Header["Datatoken"]
if !ok {
logger.Debug(3, "(FetchDataToken) Datatoken header not found")
return
}
if len(header_array) == 0 {
logger.Debug(3, "(FetchDataToken) len(header_array) == 0")
return
}
token = header_array[0]
return
}