-
Notifications
You must be signed in to change notification settings - Fork 21
/
workunit.go
340 lines (287 loc) · 11.1 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
package core
import (
//"bytes"
//"encoding/json"
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/cwl"
"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
}
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
process_name, err = cwl.GetProcessName(p)
if err != nil {
err = fmt.Errorf("(NewWorkunit) embedded workflow or toll not supported yet: %s", err.Error())
return
}
if process_name == "" {
err = fmt.Errorf("(NewWorkunit) No tool name found")
return
}
if job.CWL_collection == nil {
err = fmt.Errorf("(NewWorkunit) job.CWL_collection == nil ")
return
}
var clt *cwl.CommandLineTool
//use_commandLineTool := false
//var wfl *cwl.Workflow
//use_workflow := false
clt, err = job.CWL_collection.GetCommandLineTool(process_name)
if err != nil {
err = fmt.Errorf("(NewWorkunit) CommandLineTool %s not found", process_name)
return
//wfl, err = job.CWL_collection.GetWorkflow(process_name)
//if err != nil {
// err = fmt.Errorf("(NewWorkunit) Process %s is neither CommandLineTool, nor Workflow.")
// return
//} else {
// use_workflow = true
//}
}
//else {
// use_commandLineTool = true
//}
//if use_commandLineTool {
clt.CwlVersion = job.CwlVersion
if clt.CwlVersion == "" {
err = fmt.Errorf("(NewWorkunit) CommandLineTool misses CwlVersion")
return
}
workunit.CWL_workunit.CWL_tool = clt
//}
//if use_workflow {
// wfl.CwlVersion = job.CwlVersion
//}
// ****** get inputs
job_input_map := *job.CWL_collection.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
workflow_instance, err = job.GetWorkflowInstance(task.Parent, true)
if err != nil {
err = fmt.Errorf("(NewWorkunit) GetWorkflowInstance returned %s", err.Error())
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)
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
//spew.Dump(workflow_step.Out)
//panic("done")
}
//panic("done")
//spew.Dump(workunit.Cmd)
//panic("done")
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 {
return
}
os.RemoveAll(work_path)
err = os.MkdirAll(work_path, 0777)
if err != nil {
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
}