-
Notifications
You must be signed in to change notification settings - Fork 21
/
workunit.go
219 lines (198 loc) · 6.65 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
package core
import (
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core/cwl"
"os"
"strings"
"time"
)
const (
WORK_STAT_INIT = "init" //initial state
WORK_STAT_QUEUED = "queued" // . also: after requeue ; after failures below max ; on WorkQueue.Add()
WORK_STAT_RESERVED = "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_DONE = "done" // client-side, done.
WORK_STAT_FAILED_PERMANENT = "failed-permanent"
WORK_STAT_ERROR = "fail" // client-side, workunit computation or IO error (variable was renamed to ERROR but not the string fail, to maintain backwards compability)
WORK_STAT_PREPARED = "prepared" // client-side, after argument parsing
WORK_STAT_COMPUTED = "computed" // client-side, after computation is done, before upload
WORK_STAT_DISCARDED = "discarded"
WORK_STAT_PROXYQUEUED = "proxyqueued"
)
type Workunit struct {
Id string `bson:"wuid" json:"wuid"`
Info *Info `bson:"info" json:"info"`
Inputs []*IO `bson:"inputs" json:"inputs"`
Outputs []*IO `bson:"outputs" json:"outputs"`
Predata []*IO `bson:"predata" json:"predata"`
Cmd *Command `bson:"cmd" json:"cmd"`
Rank int `bson:"rank" json:"rank"`
TotalWork int `bson:"totalwork" json:"totalwork"`
Partition *PartInfo `bson:"part" json:"part"`
State string `bson:"state" json:"state"`
Failed int `bson:"failed" json:"failed"`
CheckoutTime time.Time `bson:"checkout_time" json:"checkout_time"`
Client string `bson:"client" json:"client"`
ComputeTime int `bson:"computetime" json:"computetime"`
ExitStatus int `bson:"exitstatus" json:"exitstatus"` // Linux Exit Status Code (0 is success)
Notes []string `bson:"notes" json:"notes"`
UserAttr map[string]string `bson:"userattr" json:"userattr"`
WorkPath string // this is the working directory. If empty, it will be computed.
WorkPerf *WorkPerf
CWL *CWL_workunit
}
type CWL_workunit struct {
CWL_job *cwl.Job_document
CWL_tool *cwl.CommandLineTool
CWL_tool_filename string
CWL_job_filename string
}
type WorkLog struct {
Id string `bson:"wuid" json:"wuid"`
Rank int `bson:"rank" json:"rank"`
Logs map[string]string `bson:"logs" json:"logs"`
}
func NewWorkLog(tid string, rank int) (wlog *WorkLog) {
wid := fmt.Sprintf("%s_%d", tid, rank)
wlog = new(WorkLog)
wlog.Id = wid
wlog.Rank = rank
wlog.Logs = map[string]string{}
for _, log := range conf.WORKUNIT_LOGS {
wlog.Logs[log], _ = QMgr.GetReportMsg(wid, log)
}
return
}
// create workunit slice type to use for sorting
type WorkunitsSortby struct {
Order string
Direction string
Workunits []*Workunit
}
func (w WorkunitsSortby) Len() int {
return len(w.Workunits)
}
func (w WorkunitsSortby) Swap(i, j int) {
w.Workunits[i], w.Workunits[j] = w.Workunits[j], w.Workunits[i]
}
func (w WorkunitsSortby) Less(i, j int) bool {
// default is ascending
if w.Direction == "desc" {
i, j = j, i
}
switch w.Order {
// default is info.submittime
default:
return w.Workunits[i].Info.SubmitTime.Before(w.Workunits[j].Info.SubmitTime)
case "wuid":
return w.Workunits[i].Id < w.Workunits[j].Id
case "client":
return w.Workunits[i].Client < w.Workunits[j].Client
case "info.submittime":
return w.Workunits[i].Info.SubmitTime.Before(w.Workunits[j].Info.SubmitTime)
case "checkout_time":
return w.Workunits[i].CheckoutTime.Before(w.Workunits[j].CheckoutTime)
case "info.name":
return w.Workunits[i].Info.Name < w.Workunits[j].Info.Name
case "cmd.name":
return w.Workunits[i].Cmd.Name < w.Workunits[j].Cmd.Name
case "rank":
return w.Workunits[i].Rank < w.Workunits[j].Rank
case "totalwork":
return w.Workunits[i].TotalWork < w.Workunits[j].TotalWork
case "state":
return w.Workunits[i].State < w.Workunits[j].State
case "failed":
return w.Workunits[i].Failed < w.Workunits[j].Failed
case "info.priority":
return w.Workunits[i].Info.Priority < w.Workunits[j].Info.Priority
}
}
func NewWorkunit(task *Task, rank int) *Workunit {
return &Workunit{
Id: fmt.Sprintf("%s_%d", task.Id, rank),
Cmd: task.Cmd,
//App: task.App,
Info: task.Info,
Inputs: task.Inputs,
Outputs: task.Outputs,
Predata: task.Predata,
Rank: rank,
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
}
}
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
os.RemoveAll(work.Path())
err = os.MkdirAll(work.Path(), 0777)
if err != nil {
return
}
return
}
func (work *Workunit) RemoveDir() (err error) {
err = os.RemoveAll(work.Path())
if err != nil {
return
}
return
}
func (work *Workunit) SetState(new_state string) {
work.State = new_state
if new_state != WORK_STAT_CHECKOUT {
work.Client = ""
}
}
func (work *Workunit) Path() string {
if work.WorkPath == "" {
id := work.Id
work.WorkPath = fmt.Sprintf("%s/%s/%s/%s/%s", conf.WORK_PATH, id[0:2], id[2:4], id[4:6], id)
}
return work.WorkPath
}
func (work *Workunit) CDworkpath() (err error) {
return os.Chdir(work.Path())
}
func (work *Workunit) GetNotes() string {
var seen map[string]bool
var 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
}