-
Notifications
You must be signed in to change notification settings - Fork 21
/
workunit.go
168 lines (150 loc) · 4.79 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
package core
import (
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
"os"
"time"
)
const (
WORK_STAT_QUEUED = "queued"
WORK_STAT_CHECKOUT = "checkout"
WORK_STAT_SUSPEND = "suspend"
WORK_STAT_DONE = "done"
WORK_STAT_FAIL = "fail"
WORK_STAT_PREPARED = "prepared"
WORK_STAT_COMPUTED = "computed"
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"`
App *App `bson:"app" json:"app"`
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"`
Notes string `bson:"notes" json:"notes"`
UserAttr map[string]string `bson:"userattr" json:"userattr"`
}
// 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_QUEUED,
Failed: 0,
UserAttr: task.UserAttr,
//AppVariables: task.AppVariables // not needed yet
}
}
func (work *Workunit) Mkdir() (err error) {
// delete workdir just in case it exists; will not work if awe-client 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) Path() string {
id := work.Id
return fmt.Sprintf("%s/%s/%s/%s/%s", conf.WORK_PATH, id[0:2], id[2:4], id[4:6], id)
}
func (work *Workunit) CDworkpath() (err error) {
return os.Chdir(work.Path())
}
func (work *Workunit) IndexType() (indextype string) {
return work.Partition.Index
}
//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
}