-
Notifications
You must be signed in to change notification settings - Fork 13
/
add.go
385 lines (345 loc) · 12.8 KB
/
add.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
// Copyright © 2016 Genome Research Limited
// Author: Sendu Bala <sb10@sanger.ac.uk>.
//
// This file is part of wr.
//
// wr is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wr is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wr. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"bufio"
"github.com/VertebrateResequencing/wr/jobqueue"
"github.com/pivotal-golang/bytefmt"
"github.com/spf13/cobra"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// options for this cmd
var reqGroup string
var cmdTime string
var cmdMem string
var cmdCPUs int
var cmdOvr int
var cmdPri int
var cmdRet int
var cmdFile string
var cmdID string
var cmdDeps string
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Add commands to the queue",
Long: `Manually add commands you want run to the queue.
You can supply your commands by putting them in a text file (1 per line), or
by piping them in. In addition to the command itself, you can specify additional
optional tab-separated columns as follows:
cmd cwd requirements_group memory time cpus override priority retries id deps
If any of these will be the same for all your commands, you can instead specify
them as flags (which are treated as defaults in the case that they are
unspecified in the text file, but otherwise ignored).
Cwd is the directory to cd to before running the command. If none is specified,
the default will be your current directory right now. (If adding to a remote
cloud-deployed manager, then cwd will instead default to /tmp.)
Requirments_group is an arbitrary string that identifies the kind of commands
you are adding, such that future commands you add with this same
requirements_group are likely to have similar memory and time requirements. It
defaults to the basename of the first word in your command, which it assumes to
be the name of your executable.
By providing the memory and time hints, wr manager can do a better job of
spawning runners to handle these commands. The manager learns how much memory
and time commands in the same requirements_group actually used in the past, and
will use its own values unless you set an override. For this learning to work
well, you should have reason to believe that all the commands you add with the
same requirements_group will have similar memory and time requirements, and you
should pick the name in a consistent way such that you'll use it again in the
future.
For example, if you want to run an executable called "exop", and you know that
the memory and time requirements of exop vary with the size of its input file,
you might batch your commands so that all the input files in one batch have
sizes in a certain range, and then provide a requirements_group that describes
this, eg. "exop.1-2G" for inputs in the 1 to 2 GB range.
(Don't name your requirements_group after the expected requirements themselves,
such as "5GB.1hr", because then the manager can't learn about your commands - it
is only learning about how good your estimates are! The name of your executable
should almost always be part of the requirements_group name.)
Override defines if your memory and time should be used instead of the manager's
estimate.
0: do not override wr's learned values for memory and time (if any)
1: override if yours are higher
2: always override
Priority defines how urgent a particular command is; those with higher
priorities will start running before those with lower priorities.
Retries defines how many times a command will be retried automatically if it
fails. Automatic retries are helpful in the case of transient errors, or errors
due to running out of memory or time (when retried, they will be retried with
more memory/time reserved). Once this number of retries is reached, the command
will be "buried" until you take manual action to fix the problem and press the
retry button in the web interface.
Id is an arbitrary name you can give your commands so you can query their status
later. If you split your commands into multiple batches with different
requirements_groups, you can give all the different batches the same identifier,
so you can track them in one go.
Deps defines the dependencies of this command. Starting from column 11 you can
specify other commands that must complete before this command will start. The
specification of the other commands is done by having the command line in one
column, and it's cwd in the next, then repeating in subsequent columns for every
other dependency. So a command with 1 dependency would have 12 columns, and one
with 2 dependencies would have 14 columns and so on. Dependencies must have
either been specified earlier in the file, or must already have been added to
the queue.
NB: Your commands will run with the environment variables you had when you
added them, not the possibly different environment variables you could have in
the future when the commands actually get run.`,
Run: func(cmd *cobra.Command, args []string) {
// check the command line options
if cmdFile == "" {
die("--file is required")
}
if cmdID == "" {
cmdID = "manually_added"
}
var cmdMB int
var err error
if cmdMem == "" {
cmdMB = 0
} else {
mb, err := bytefmt.ToMegabytes(cmdMem)
if err != nil {
die("--memory was not specified correctly: %s", err)
}
cmdMB = int(mb)
}
var cmdDuration time.Duration
if cmdTime == "" {
cmdDuration = 0 * time.Second
} else {
cmdDuration, err = time.ParseDuration(cmdTime)
if err != nil {
die("--time was not specified correctly: %s", err)
}
}
if cmdCPUs < 1 {
cmdCPUs = 1
}
if cmdOvr < 0 || cmdOvr > 2 {
die("--override must be in the range 0..2")
}
if cmdPri < 0 || cmdPri > 255 {
die("--priority must be in the range 0..255")
}
if cmdRet < 0 || cmdRet > 255 {
die("--retries must be in the range 0..255")
}
timeout := time.Duration(timeoutint) * time.Second
var defaultDeps []*jobqueue.Dependency
if cmdDeps != "" {
cols := strings.Split(cmdDeps, "\\t")
if len(cols)%2 != 0 {
warn("--deps has cols (%s) of len %d which %2 == %d", cols, len(cols), len(cols)%2)
die("--deps must have an even number of tab-separated columns")
}
defaultDeps = colsToDeps(cols)
}
// open file or set up to read from STDIN
var reader io.Reader
if cmdFile == "-" {
reader = os.Stdin
} else {
reader, err = os.Open(cmdFile)
if err != nil {
die("could not open file '%s': %s", cmdFile, err)
}
defer reader.(*os.File).Close()
}
// we'll default to pwd if the manager is on the same host as us, /tmp
// otherwise
jq, err := jobqueue.Connect(addr, "cmds", timeout)
if err != nil {
die("%s", err)
}
sstats, err := jq.ServerStats()
if err != nil {
die("even though I was able to connect to the manager, it failed to tell me its location")
}
var pwd string
var pwdWarning int
if jobqueue.CurrentIP()+":"+config.ManagerPort == sstats.ServerInfo.Addr {
pwd, err = os.Getwd()
if err != nil {
die("%s", err)
}
} else {
pwd = "/tmp"
pwdWarning = 1
}
jq.Disconnect()
// for network efficiency, read in all commands and create a big slice
// of Jobs and Add() them in one go afterwards
var jobs []*jobqueue.Job
scanner := bufio.NewScanner(reader)
defaultedID := false
for scanner.Scan() {
cols := strings.Split(scanner.Text(), "\t")
colsn := len(cols)
if colsn < 1 || cols[0] == "" {
continue
}
var cmd, cwd, rg, id string
var mb, cpus, override, priority, retries int
var dur time.Duration
var deps *jobqueue.Dependencies
// cmd cwd requirements_group memory time cpus override priority retries id deps
cmd = cols[0]
if colsn < 2 || cols[1] == "" {
if cmdCwd != "" {
cwd = cmdCwd
} else {
if pwdWarning == 1 {
warn("command working directories defaulting to /tmp since the manager is running remotely")
pwdWarning = 0
}
cwd = pwd
}
} else {
cwd = cols[1]
}
if colsn < 3 || cols[2] == "" {
if reqGroup != "" {
rg = reqGroup
} else {
parts := strings.Split(cmd, " ")
rg = filepath.Base(parts[0])
}
} else {
rg = cols[2]
}
if colsn < 4 || cols[3] == "" {
mb = cmdMB
} else {
thismb, err := bytefmt.ToMegabytes(cols[3])
if err != nil {
die("a value in the memory column (%s) was not specified correctly: %s", cols[3], err)
}
mb = int(thismb)
}
if colsn < 5 || cols[4] == "" {
dur = cmdDuration
} else {
dur, err = time.ParseDuration(cols[4])
if err != nil {
die("a value in the time column (%s) was not specified correctly: %s", cols[4], err)
}
}
if colsn < 6 || cols[5] == "" {
cpus = cmdCPUs
} else {
cpus, err = strconv.Atoi(cols[5])
if err != nil {
die("a value in the cpus column (%s) was not specified correctly: %s", cols[5], err)
}
}
if colsn < 7 || cols[6] == "" {
override = cmdOvr
} else {
override, err = strconv.Atoi(cols[6])
if err != nil {
die("a value in the override column (%s) was not specified correctly: %s", cols[6], err)
}
if override < 0 || override > 2 {
die("override column must contain values in the range 0..2 (not %d)", override)
}
}
if colsn < 8 || cols[7] == "" {
priority = cmdPri
} else {
priority, err = strconv.Atoi(cols[7])
if err != nil {
die("a value in the priority column (%s) was not specified correctly: %s", cols[7], err)
}
if priority < 0 || priority > 255 {
die("priority column must contain values in the range 0..255 (not %d)", priority)
}
}
if colsn < 9 || cols[8] == "" {
retries = cmdRet
} else {
retries, err = strconv.Atoi(cols[8])
if err != nil {
die("a value in the retries column (%s) was not specified correctly: %s", cols[8], err)
}
if priority < 0 || priority > 255 {
die("retries column must contain values in the range 0..255 (not %d)", retries)
}
}
if colsn < 10 || cols[9] == "" {
id = cmdID
defaultedID = true
} else {
id = cols[9]
}
if colsn < 11 || cols[10] == "" {
deps = jobqueue.NewDependencies(defaultDeps...)
} else {
// all remaining columns specify deps
if colsn%2 != 0 {
die("there must be an even number of dependency columns")
}
deps = jobqueue.NewDependencies(colsToDeps(cols[10:])...)
}
jobs = append(jobs, jobqueue.NewJob(cmd, cwd, rg, mb, dur, cpus, uint8(override), uint8(priority), uint8(retries), id, deps))
}
// connect to the server
jq, err = jobqueue.Connect(addr, "cmds", timeout)
if err != nil {
die("%s", err)
}
defer jq.Disconnect()
// add the jobs to the queue
inserts, dups, err := jq.Add(jobs)
if err != nil {
die("%s", err)
}
if defaultedID {
info("Added %d new commands (%d were duplicates) to the queue using default identifier '%s'", inserts, dups, cmdID)
} else {
info("Added %d new commands (%d were duplicates) to the queue", inserts, dups)
}
},
}
func init() {
RootCmd.AddCommand(addCmd)
// flags specific to this sub-command
addCmd.Flags().StringVarP(&cmdFile, "file", "f", "-", "file containing your commands; - means read from STDIN")
addCmd.Flags().StringVarP(&cmdID, "id", "i", "manually_added", "identifier for your commands")
addCmd.Flags().StringVarP(&cmdCwd, "cwd", "c", "", "working dir")
addCmd.Flags().StringVarP(&reqGroup, "requirements_group", "g", "", "group name for commands with similar reqs")
addCmd.Flags().StringVarP(&cmdMem, "memory", "m", "1G", "peak mem est. [specify units such as M for Megabytes or G for Gigabytes]")
addCmd.Flags().StringVarP(&cmdTime, "time", "t", "1h", "max time est. [specify units such as m for minutes or h for hours]")
addCmd.Flags().IntVar(&cmdCPUs, "cpus", 1, "cpu cores needed")
addCmd.Flags().IntVarP(&cmdOvr, "override", "o", 0, "[0|1|2] should your mem/time estimates override?")
addCmd.Flags().IntVarP(&cmdPri, "priority", "p", 0, "[0-255] command priority")
addCmd.Flags().IntVarP(&cmdRet, "retries", "r", 3, "[0-255] number of automatic retries for failed commands")
addCmd.Flags().StringVarP(&cmdDeps, "deps", "d", "", "dependencies of your commands, in the form \"command1\\tcwd1\\tcommand2\\tcwd2...\"")
addCmd.Flags().IntVar(&timeoutint, "timeout", 30, "how long (seconds) to wait to get a reply from 'wr manager'")
}
// convert cmd,cwd columns in to Dependency
func colsToDeps(cols []string) (deps []*jobqueue.Dependency) {
for i := 0; i < len(cols); i += 2 {
deps = append(deps, jobqueue.NewDependency(cols[i], cols[i+1]))
}
return
}