-
Notifications
You must be signed in to change notification settings - Fork 145
/
job_hdlr.go
1300 lines (1198 loc) · 33.3 KB
/
job_hdlr.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package cli provides easy-to-use commands to manage, monitor, and utilize AIS clusters.
// This file handles commands that control running jobs in the cluster.
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package cli
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/NVIDIA/aistore/api"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/ext/dload"
"github.com/NVIDIA/aistore/ext/dsort"
"github.com/NVIDIA/aistore/ext/etl"
"github.com/NVIDIA/aistore/xact"
jsoniter "github.com/json-iterator/go"
"github.com/urfave/cli"
)
const (
prefetchUsage = "prefetch one remote bucket, multiple remote buckets, or\n" +
indent1 + "selected objects in a given remote bucket or buckets, e.g.:\n" +
indent1 + "\t- 'prefetch gs://abc'\t- prefetch entire bucket (all gs://abc objects that are _not_ present in the cluster);\n" +
indent1 + "\t- 'prefetch gs:'\t- prefetch all visible/accessible GCP buckets;\n" +
indent1 + "\t- 'prefetch gs://abc --template images/'\t- prefetch all objects from the virtual subdirectory \"images\";\n" +
indent1 + "\t- 'prefetch gs://abc/images/'\t- same as above;\n" +
indent1 + "\t- 'prefetch gs://abc --template \"shard-{0000..9999}.tar.lz4\"'\t- prefetch the matching range (prefix + brace expansion);\n" +
indent1 + "\t- 'prefetch \"gs://abc/shard-{0000..9999}.tar.lz4\"'\t- same as above (notice double quotes)"
)
// top-level job command
var (
jobCmd = cli.Command{
Name: commandJob,
Usage: "monitor, query, start/stop and manage jobs and eXtended actions (xactions)",
Subcommands: jobSub,
}
// NOTE: `appendJobSub` (below) expects jobSub[0] to be the `jobStartSub`
jobSub = []cli.Command{
jobStartSub,
jobStopSub,
jobWaitSub,
jobRemoveSub,
makeAlias(showCmdJob, "", true, commandShow), // alias for `ais show`
}
)
// job start
var (
startCommonFlags = []cli.Flag{
waitFlag,
waitJobXactFinishedFlag,
nonverboseFlag,
}
startSpecialFlags = map[string][]cli.Flag{
cmdDownload: {
dloadTimeoutFlag,
descJobFlag,
limitConnectionsFlag,
objectsListFlag,
dloadProgressFlag,
progressFlag,
waitFlag,
waitJobXactFinishedFlag,
limitBytesPerHourFlag,
syncFlag,
unitsFlag,
},
cmdDsort: {
dsortSpecFlag,
verboseFlag,
},
commandPrefetch: append(
listRangeProgressWaitFlags,
dryRunFlag,
verbObjPrefixFlag, // to disambiguate bucket/prefix vs bucket/objName
latestVerFlag,
blobThresholdFlag,
),
cmdBlobDownload: {
refreshFlag,
progressFlag,
listFlag,
chunkSizeFlag,
numWorkersFlag,
waitFlag,
waitJobXactFinishedFlag,
latestVerFlag,
nonverboseFlag,
},
cmdLRU: {
lruBucketsFlag,
forceFlag,
nonverboseFlag,
},
}
jobStartResilver = cli.Command{
Name: commandResilver,
Usage: "resilver user data on a given target (or all targets in the cluster): fix data redundancy\n" +
indent4 + "\twith respect to bucket configuration, remove migrated objects and old/obsolete workfiles",
ArgsUsage: optionalTargetIDArgument,
Flags: startCommonFlags,
Action: startResilverHandler,
BashComplete: suggestTargets,
}
prefetchStartCmd = cli.Command{
Name: commandPrefetch,
Usage: prefetchUsage,
ArgsUsage: bucketObjectOrTemplateMultiArg,
Flags: startSpecialFlags[commandPrefetch],
Action: startPrefetchHandler,
BashComplete: bucketCompletions(bcmplop{multiple: true}),
}
blobDownloadCmd = cli.Command{
Name: cmdBlobDownload,
Usage: "run a job to download large object(s) from remote storage to aistore cluster, e.g.:\n" +
indent1 + "\t- 'blob-download s3://ab/largefile --chunk-size=2mb --progress'\t- download one blob at a given chunk size\n" +
indent1 + "\t- 'blob-download s3://ab --list \"f1, f2\" --num-workers=4 --progress'\t- use 4 concurrent readers to download each of the 2 blobs\n" +
indent1 + "When _not_ using '--progress' option, run 'ais show job' to monitor.",
ArgsUsage: objectArgument,
Flags: startSpecialFlags[cmdBlobDownload],
Action: blobDownloadHandler,
BashComplete: remoteBucketCompletions(bcmplop{multiple: true}),
}
jobStartSub = cli.Command{
Name: commandStart,
Usage: "run batch job",
Subcommands: []cli.Command{
prefetchStartCmd,
blobDownloadCmd,
{
Name: cmdDownload,
Usage: "download files and objects from remote sources",
ArgsUsage: startDownloadArgument,
Flags: startSpecialFlags[cmdDownload],
Action: startDownloadHandler,
},
dsortStartCmd,
{
Name: cmdLRU,
Usage: "run LRU eviction",
Flags: startSpecialFlags[cmdLRU],
Action: startLRUHandler,
BashComplete: bucketCompletions(bcmplop{}),
},
{
Name: commandETL,
Usage: "start ETL",
Subcommands: []cli.Command{
initCmdETL,
objCmdETL,
bckCmdETL,
},
},
{
Name: commandRebalance,
Usage: "rebalance ais cluster",
Flags: clusterCmdsFlags[commandStart],
Action: startClusterRebalanceHandler,
},
cleanupCmd,
jobStartResilver,
// NOTE: append all `startableXactions`
},
}
)
// ais stop
var (
stopCmdsFlags = []cli.Flag{
allRunningJobsFlag,
regexJobsFlag,
yesFlag,
}
jobStopSub = cli.Command{
Name: commandStop,
Usage: "terminate a single batch job or multiple jobs, e.g.:\n" +
indent1 + "\t- 'stop tco-cysbohAGL'\t- terminate a given job identified by its unique ID;\n" +
indent1 + "\t- 'stop copy-listrange'\t- terminate all multi-object copies;\n" +
indent1 + "\t- 'stop copy-objects'\t- same as above (using display name);\n" +
indent1 + "\t- 'stop --all'\t- terminate all running jobs\n" +
indent1 + strToSentence(tabHelpOpt),
ArgsUsage: jobAnyArg,
Flags: stopCmdsFlags,
Action: stopJobHandler,
BashComplete: runningJobCompletions,
}
)
// ais wait
var (
waitCmdsFlags = []cli.Flag{
refreshFlag,
progressFlag,
waitJobXactFinishedFlag,
}
jobWaitSub = cli.Command{
Name: commandWait,
Usage: "wait for a specific batch job to complete (" + tabHelpOpt + ")",
ArgsUsage: jobAnyArg,
Flags: waitCmdsFlags,
Action: waitJobHandler,
BashComplete: runningJobCompletions,
}
)
// ais job remove
var (
removeCmdsFlags = []cli.Flag{
allFinishedJobsFlag,
regexJobsFlag,
}
jobRemoveSub = cli.Command{
Name: commandRemove,
Usage: "cleanup finished jobs",
Subcommands: []cli.Command{
{
Name: cmdDownload,
Usage: "remove finished download job(s)",
ArgsUsage: optionalJobIDArgument,
Flags: removeCmdsFlags,
Action: removeDownloadHandler,
BashComplete: downloadIDFinishedCompletions,
},
{
Name: cmdDsort,
Usage: "remove finished dsort job(s)",
ArgsUsage: optionalJobIDArgument,
Flags: removeCmdsFlags,
Action: removeDsortHandler,
BashComplete: dsortIDFinishedCompletions,
},
},
}
)
func jobName(xname, xid string) string { return xname + "[" + xid + "]" }
func appendJobSub(jobcmd *cli.Command) {
debug.Assert(jobcmd.Subcommands[0].Name == commandStart)
// append to `jobStartSub`, add to `ais start` (== `ais job start`)
cmds := jobcmd.Subcommands[0].Subcommands
cmds = append(cmds, storageSvcCmds...)
cmds = append(cmds, startableXactions(cmds)...)
jobcmd.Subcommands[0].Subcommands = cmds
}
func startableXactions(explDefinedCmds cli.Commands) cli.Commands {
var (
cmds = make(cli.Commands, 0, 8)
startable = xact.ListDisplayNames(true /*onlyStartable*/)
)
outer:
for _, xname := range startable {
for i := range explDefinedCmds {
cmd := &explDefinedCmds[i]
kind, _ := xact.GetKindName(xname)
// CLI is allowed to make it even shorter..
if strings.HasPrefix(xname, cmd.Name) || strings.HasPrefix(kind, cmd.Name) {
// the following x-s, even through startable, have their own custom CLI handlers:
// - cleanup-store
// - ec-encode
// - lru
// - make-n-copies
// - prefetch-listrange
// - blob-download
// - rebalance
// - resilver
continue outer
}
}
// add generic start handler on the fly
cmd := cli.Command{
Name: xname,
Usage: "start " + xname,
Flags: startCommonFlags,
Action: startXactionHandler,
}
if xact.IsSameScope(xname, xact.ScopeB) { // with a single arg: bucket
cmd.ArgsUsage = bucketArgument
cmd.BashComplete = bucketCompletions(bcmplop{})
} else if xact.IsSameScope(xname, xact.ScopeGB) { // with a single optional arg: bucket
cmd.ArgsUsage = optionalBucketArgument
cmd.BashComplete = bucketCompletions(bcmplop{})
}
cmds = append(cmds, cmd)
}
// and in addition:
cmd := bucketCmdCopy
cmd.Name, _ = xact.GetKindName(apc.ActCopyBck)
cmds = append(cmds, cmd)
cmd = bucketCmdRename
cmd.Name, _ = xact.GetKindName(apc.ActMoveBck)
cmds = append(cmds, cmd)
return cmds
}
//
// job start
//
// via `startableXactions()`
func startXactionHandler(c *cli.Context) (err error) {
xname := c.Command.Name
return startXactionKind(c, xname)
}
func startXactionKind(c *cli.Context, xname string) (err error) {
var (
bck cmn.Bck
extra string
)
if c.NArg() == 0 && xact.IsSameScope(xname, xact.ScopeB) {
return missingArgumentsError(c, c.Command.ArgsUsage)
}
if c.NArg() > 0 && xact.IsSameScope(xname, xact.ScopeB, xact.ScopeGB) {
uri := c.Args().Get(0)
if xname == apc.ActBlobDl {
bck, extra /*objName*/, err = parseBckObjURI(c, uri, false)
} else {
bck, err = parseBckURI(c, uri, false)
}
if err != nil {
return err
}
}
xargs := xact.ArgsMsg{Kind: xname, Bck: bck}
return startXaction(c, &xargs, extra)
}
func startResilverHandler(c *cli.Context) error {
var tid string
if c.NArg() > 0 {
tsi, _, err := getNode(c, c.Args().Get(0))
if err != nil {
return err
}
tid = tsi.ID()
}
xargs := xact.ArgsMsg{Kind: apc.ActResilver, DaemonID: tid}
return startXaction(c, &xargs, "")
}
func startXaction(c *cli.Context, xargs *xact.ArgsMsg, extra string) error {
if !xargs.Bck.IsQuery() {
if _, err := headBucket(xargs.Bck, false /* don't add */); err != nil {
return err
}
} else if xact.IsSameScope(xargs.Kind, xact.ScopeB) {
return fmt.Errorf("%q requires bucket to run", xargs.Kind)
}
xid, err := api.StartXaction(apiBP, xargs, extra)
if err != nil {
return V(err)
}
if xid == "" {
warn := fmt.Sprintf("The operation returned an empty UUID (a no-op?). %s\n",
toShowMsg(c, "", "To investigate", false))
actionWarn(c, warn)
return nil
}
debug.Assert(xact.IsValidUUID(xid), xid)
if xargs.Kind == apc.ActRebalance {
actionDone(c, "Started global rebalance. To monitor the progress, run 'ais show rebalance'")
} else {
actionX(c, xargs, "")
}
if !flagIsSet(c, waitFlag) && !flagIsSet(c, waitJobXactFinishedFlag) {
return nil
}
return waitJob(c, xargs.Kind, xid, xargs.Bck)
}
func startDownloadHandler(c *cli.Context) error {
var (
description = parseStrFlag(c, descJobFlag)
timeout = parseStrFlag(c, dloadTimeoutFlag)
objectsListPath = parseStrFlag(c, objectsListFlag)
progressInterval = parseStrFlag(c, dloadProgressFlag)
id string
)
if c.NArg() == 0 {
return missingArgumentsError(c, c.Command.ArgsUsage)
}
if c.NArg() == 1 {
return missingArgumentsError(c, "destination")
}
if c.NArg() > 2 {
const q = "For range download, enclose source in quotation marks, e.g.: \"gs://imagenet/train-{00..99}.tgz\""
s := fmt.Sprintf("too many arguments - expected 2, got %d.\n%s", len(c.Args()), q)
return &errUsage{
context: c,
message: s,
helpData: c.Command,
helpTemplate: cli.CommandHelpTemplate,
}
}
src, dst := c.Args().Get(0), c.Args().Get(1)
source, err := parseSource(src)
if err != nil {
return err
}
bck, pathSuffix, err := parseDest(c, dst)
if err != nil {
return err
}
limitBPH, err := parseSizeFlag(c, limitBytesPerHourFlag)
if err != nil {
return err
}
if _, err := time.ParseDuration(progressInterval); err != nil {
return err
}
basePayload := dload.Base{
Bck: bck,
Timeout: timeout,
Description: description,
ProgressInterval: progressInterval,
Limits: dload.Limits{
Connections: parseIntFlag(c, limitConnectionsFlag),
BytesPerHour: int(limitBPH),
},
}
if basePayload.Bck.Props, err = api.HeadBucket(apiBP, basePayload.Bck, true /* don't add */); err != nil {
if !cmn.IsStatusNotFound(err) {
return err
}
warn := fmt.Sprintf("destination bucket %s doesn't exist. Bucket with default properties will be created.",
basePayload.Bck.Cname(""))
actionWarn(c, warn)
}
// Heuristics to determine the download type.
var dlType dload.Type
if objectsListPath != "" {
dlType = dload.TypeMulti
} else if strings.Contains(source.link, "{") && strings.Contains(source.link, "}") {
dlType = dload.TypeRange
} else if source.backend.bck.IsEmpty() {
dlType = dload.TypeSingle
} else {
cfg, err := getRandTargetConfig(c)
if err != nil {
return err
}
if _, ok := cfg.Backend.Providers[source.backend.bck.Provider]; ok { // backend is configured
dlType = dload.TypeBackend
p, err := api.HeadBucket(apiBP, basePayload.Bck, false /* don't add */)
if err != nil {
return V(err)
}
if !p.BackendBck.Equal(&source.backend.bck) {
warn := fmt.Sprintf("%s does not have Cloud bucket %s as its *backend* - proceeding to download anyway.",
basePayload.Bck, source.backend.bck)
actionWarn(c, warn)
dlType = dload.TypeSingle
}
} else if source.backend.prefix == "" {
return fmt.Errorf(
"cluster is not configured with %q provider: cannot download remote bucket",
source.backend.bck.Provider,
)
} else {
if source.link == "" {
return fmt.Errorf(
"cluster is not configured with %q provider: cannot download bucket's objects",
source.backend.bck.Provider,
)
}
// If `prefix` is not empty then possibly it is just a single object
// which we can download without cloud to be configured (web link).
dlType = dload.TypeSingle
}
}
switch dlType {
case dload.TypeSingle:
payload := dload.SingleBody{
Base: basePayload,
SingleObj: dload.SingleObj{
Link: source.link,
ObjName: pathSuffix, // in this case pathSuffix is a full name of the object
},
}
id, err = api.DownloadWithParam(apiBP, dlType, payload)
case dload.TypeMulti:
var objects []string
{
file, err := os.Open(objectsListPath)
if err != nil {
return err
}
if err := jsoniter.NewDecoder(file).Decode(&objects); err != nil {
return fmt.Errorf("file %q doesn't seem to contain JSON array of strings: %v", objectsListPath, err)
}
}
for i, object := range objects {
objects[i] = source.link + "/" + object
}
payload := dload.MultiBody{
Base: basePayload,
ObjectsPayload: objects,
}
id, err = api.DownloadWithParam(apiBP, dlType, payload)
case dload.TypeRange:
payload := dload.RangeBody{
Base: basePayload,
Subdir: pathSuffix, // in this case pathSuffix is a subdirectory in which the objects are to be saved
Template: source.link,
}
id, err = api.DownloadWithParam(apiBP, dlType, payload)
case dload.TypeBackend:
payload := dload.BackendBody{
Base: basePayload,
Sync: flagIsSet(c, syncFlag),
Prefix: source.backend.prefix,
}
id, err = api.DownloadWithParam(apiBP, dlType, payload)
default:
debug.Assert(false)
}
if err != nil {
return err
}
fmt.Fprintf(c.App.Writer, "Started download job %s\n", id)
if flagIsSet(c, progressFlag) {
return pbDownload(c, id)
}
if flagIsSet(c, waitFlag) || flagIsSet(c, waitJobXactFinishedFlag) {
return wtDownload(c, id)
}
return bgDownload(c, id)
}
func pbDownload(c *cli.Context, id string) (err error) {
refreshRate := _refreshRate(c)
downloadingResult, err := newDownloaderPB(apiBP, id, refreshRate).run()
if err != nil {
return err
}
fmt.Fprintln(c.App.Writer, downloadingResult)
return nil
}
func wtDownload(c *cli.Context, id string) error {
if err := waitDownload(c, id); err != nil {
return err
}
resp, err := api.DownloadStatus(apiBP, id, true /*only active*/)
if err != nil {
return V(err)
}
if resp.ErrorCnt != 0 {
msg := toShowMsg(c, id, "For details", true)
warn := fmt.Sprintf("%d of %d download jobs failed. %s", resp.ErrorCnt, resp.ScheduledCnt, msg)
actionWarn(c, warn)
} else {
actionDownloaded(c, resp.FinishedCnt)
}
return nil
}
func actionDownloaded(c *cli.Context, cnt int) {
var msg string
if cnt == 1 {
msg = "File successfully downloaded"
} else {
msg = fmt.Sprintf("All %d files successfully downloaded", cnt)
}
actionDone(c, msg)
}
func bgDownload(c *cli.Context, id string) (err error) {
var (
resp *dload.StatusResp
startedTimeout = max(dloadStartedTime, refreshRateMinDur*2)
)
// In a non-interactive mode, allow downloader to start before checking
for elapsed := time.Duration(0); elapsed < startedTimeout; elapsed += refreshRateMinDur {
time.Sleep(refreshRateMinDur)
resp, err = api.DownloadStatus(apiBP, id, true /*only active*/)
if err != nil {
return err
}
if resp.ErrorCnt != 0 || resp.FinishedCnt != 0 || resp.FinishedTime.UnixNano() != 0 {
break
}
}
if resp.ErrorCnt != 0 {
msg := toShowMsg(c, id, "For details", true)
warn := fmt.Sprintf("%d of %d download jobs failed. %s", resp.ErrorCnt, resp.ScheduledCnt, msg)
actionWarn(c, warn)
} else if resp.FinishedTime.UnixNano() != 0 {
actionDownloaded(c, resp.FinishedCnt)
} else {
msg := toMonitorMsg(c, id, flprn(progressFlag)+"'")
actionDone(c, msg)
}
return err
}
func waitDownload(c *cli.Context, id string) (err error) {
var (
elapsed, timeout time.Duration
refreshRate = _refreshRate(c)
qn = xact.Cname(cmdDownload, id)
aborted bool
)
if flagIsSet(c, waitJobXactFinishedFlag) {
timeout = parseDurationFlag(c, waitJobXactFinishedFlag)
}
for {
resp, err := api.DownloadStatus(apiBP, id, true)
if err != nil {
return V(err)
}
aborted = resp.Aborted
if aborted || resp.JobFinished() {
break
}
time.Sleep(refreshRate)
elapsed += refreshRate
if timeout != 0 && elapsed > timeout {
return fmt.Errorf("timed out waiting for %s", qn)
}
}
if aborted {
return fmt.Errorf("download job %s was aborted", id)
}
return nil
}
func startLRUHandler(c *cli.Context) (err error) {
if !flagIsSet(c, lruBucketsFlag) {
return startXactionHandler(c)
}
if flagIsSet(c, forceFlag) {
warn := fmt.Sprintf("LRU eviction with %s option will evict buckets _ignoring_ their respective `lru.enabled` properties.",
qflprn(forceFlag))
if ok := confirm(c, "Would you like to continue?", warn); !ok {
return
}
}
s := parseStrFlag(c, lruBucketsFlag)
bckArgs := splitCsv(s)
buckets := make([]cmn.Bck, len(bckArgs))
for idx, bckArg := range bckArgs {
bck, err := parseBckURI(c, bckArg, false)
if err != nil {
return err
}
buckets[idx] = bck
}
var (
id string
xargs = xact.ArgsMsg{Kind: apc.ActLRU, Buckets: buckets, Force: flagIsSet(c, forceFlag)}
)
if id, err = api.StartXaction(apiBP, &xargs, ""); err != nil {
return
}
actionX(c, &xact.ArgsMsg{Kind: apc.ActLRU, ID: id}, "")
return
}
//
// job stop
//
func stopJobHandler(c *cli.Context) error {
var shift int
if c.Args().Get(0) == commandJob {
shift = 1
}
name, xid, daemonID, bck, err := jobArgs(c, shift, true /*ignore daemonID*/)
if err != nil {
return err
}
if name == "" && xid == "" && !flagIsSet(c, allRunningJobsFlag) {
return missingArgumentsError(c, c.Command.ArgsUsage)
}
if daemonID != "" {
warn := fmt.Sprintf("node ID %q will be ignored (stopping job on a given node not supported)\n",
daemonID)
actionWarn(c, warn)
}
regex := parseStrFlag(c, regexJobsFlag)
if xid != "" && (flagIsSet(c, allRunningJobsFlag) || regex != "") {
warn := fmt.Sprintf("in presence of %s argument ('%s') flags %s and %s will be ignored",
jobIDArgument, xid, qflprn(allRunningJobsFlag), qflprn(regexJobsFlag))
actionWarn(c, warn)
} else if xid == "" && (flagIsSet(c, allRunningJobsFlag) || regex != "") {
switch name {
case cmdDownload, cmdDsort:
// regex supported
case commandRebalance:
warn := fmt.Sprintf("global rebalance is global (ignoring %s and %s flags)",
qflprn(allRunningJobsFlag), qflprn(regexJobsFlag))
actionWarn(c, warn)
default:
if regex != "" {
warn := fmt.Sprintf("ignoring flag %s - "+NIY, qflprn(regexJobsFlag))
actionWarn(c, warn)
}
}
}
var (
otherID string
xactID = xid
xname, xactKind string
)
if name == "" && xid != "" {
name, otherID = xid2Name(xid)
}
if name != "" {
xactKind, xname = xact.GetKindName(name)
if xactKind == "" {
return incorrectUsageMsg(c, "unrecognized or misplaced option '%s'", name)
}
}
// confirm unless
if xactID == "" && name != "" {
if name != commandRebalance && !flagIsSet(c, yesFlag) && !flagIsSet(c, allRunningJobsFlag) {
prompt := fmt.Sprintf("Stop all '%s' jobs?", name)
if ok := confirm(c, prompt); !ok {
return nil
}
}
}
// specialized stop
switch name {
case cmdDownload:
if xid == "" {
return stopDownloadRegex(c, regex)
}
return stopDownloadHandler(c, xid)
case cmdDsort:
if xid == "" {
return stopDsortRegex(c, regex)
}
return stopDsortHandler(c, xid)
case commandETL:
return stopETLs(c, otherID /*etl name*/)
case commandRebalance:
return stopClusterRebalanceHandler(c)
}
// generic xstop
if xactID == "" {
return stopXactionKindOrAll(c, xactKind, xname, bck)
}
// query
msg := formatXactMsg(xactID, xname, bck)
xargs := xact.ArgsMsg{ID: xactID, Kind: xactKind}
_, snap, err := getXactSnap(&xargs)
if err != nil {
return fmt.Errorf("cannot stop %s: %v", msg, err)
}
if snap == nil {
actionWarn(c, msg+" not found, nothing to do")
return nil
}
// reformat
if xactID == "" {
xactID = snap.ID
debug.Assert(xactKind == snap.Kind)
msg = formatXactMsg(xactID, xname, snap.Bck)
} else {
debug.Assert(xactID == snap.ID)
msg = formatXactMsg(xactID, xname, snap.Bck)
}
// abort?
var s = "already finished"
if snap.IsAborted() {
s = "aborted"
}
if snap.IsAborted() || snap.Finished() {
fmt.Fprintf(c.App.Writer, "%s is %s, nothing to do\n", msg, s)
return nil
}
// abort
args := xact.ArgsMsg{ID: xactID, Kind: xactKind, Bck: snap.Bck}
if err := api.AbortXaction(apiBP, &args); err != nil {
return V(err)
}
actionDone(c, fmt.Sprintf("Stopped %s\n", msg))
return nil
}
// NOTE: the '--all' case when both (xactKind == "" && xname == "") - is also handled here
// TODO: aistore supports `bck` for additional filtering (NIY)
func stopXactionKindOrAll(c *cli.Context, xactKind, xname string, bck cmn.Bck) error {
cnames, err := api.GetAllRunningXactions(apiBP, xactKind)
if err != nil {
return V(err)
}
if len(cnames) == 0 {
var what string
if xname != "" {
what = " '" + xname + "'"
}
actionDone(c, fmt.Sprintf("No running%s jobs, nothing to do", what))
return nil
}
for _, cname := range cnames {
xactKind, xactID, err := xact.ParseCname(cname)
if err != nil {
debug.AssertNoErr(err)
continue
}
args := xact.ArgsMsg{ID: xactID, Kind: xactKind, Bck: bck}
if err := api.AbortXaction(apiBP, &args); err != nil {
actionWarn(c, fmt.Sprintf("failed to stop %s: %v", cname, err))
} else {
actionDone(c, "Stopped "+cname)
}
}
return nil
}
func formatXactMsg(xactID, xactKind string, bck cmn.Bck) string {
var sb string
if !bck.IsQuery() {
sb = ", " + bck.Cname("")
}
switch {
case xactKind != "" && xactID != "":
return fmt.Sprintf("%s%s", xact.Cname(xactKind, xactID), sb)
case xactKind != "" && sb != "":
return fmt.Sprintf("%s%s", xactKind, sb)
case xactKind != "":
return xactKind
default:
return fmt.Sprintf("[%s]%s", xactID, sb)
}
}
func stopDownloadRegex(c *cli.Context, regex string) error {
dlList, err := api.DownloadGetList(apiBP, regex, true /*onlyActive*/)
if err != nil {
return V(err)
}
var cnt int
for _, dl := range dlList {
if err = api.AbortDownload(apiBP, dl.ID); err == nil {
actionDone(c, "Stopped download job "+dl.ID)
cnt++
} else {
actionWarn(c, fmt.Sprintf("failed to stop download job %q: %v", dl.ID, err))
}
}
if err != nil {
return err
}
if cnt == 0 {
actionDone(c, "No running download jobs, nothing to do")
}
return nil
}
func stopDownloadHandler(c *cli.Context, id string) (err error) {
if err = api.AbortDownload(apiBP, id); err != nil {
return
}
actionDone(c, fmt.Sprintf("Stopped download job %s\n", id))
return
}
func stopDsortRegex(c *cli.Context, regex string) error {
dsortLst, err := api.ListDsort(apiBP, regex, true /*onlyActive*/)
if err != nil {
return V(err)
}
var cnt int
for _, dsort := range dsortLst {
if err = api.AbortDsort(apiBP, dsort.ID); err == nil {
actionDone(c, "Stopped dsort job "+dsort.ID)
cnt++
} else {
actionWarn(c, fmt.Sprintf("failed to stop dsort job %q: %v", dsort.ID, err))
}
}
if err != nil {
return err
}
if cnt == 0 {
actionDone(c, "No running dsort jobs, nothing to do")
}
return nil
}
func stopDsortHandler(c *cli.Context, id string) (err error) {
if err = api.AbortDsort(apiBP, id); err != nil {
return
}
actionDone(c, fmt.Sprintf("Stopped dsort job %s\n", id))
return
}
//
// `job wait`
//
func waitJobHandler(c *cli.Context) error {
var shift int
if c.Args().Get(0) == commandJob {
shift = 1
}
name, xid, daemonID, bck, err := jobArgs(c, shift, true /*ignore daemonID*/)
if err != nil {
return err
}
if name == "" && xid == "" {
return missingArgumentsError(c, c.Command.ArgsUsage)
}
if daemonID != "" {
actionWarn(c, fmt.Sprintf("node ID %q will be ignored (waiting for a single target not supported)\n", daemonID))
}
if name == "" && xid != "" {
name, _ = xid2Name(xid) // TODO: add waitETL
}
return waitJob(c, name, xid, bck)
}
func waitJob(c *cli.Context, name, xid string, bck cmn.Bck) error {
// special wait
switch name {
case cmdDownload:
if xid == "" {
return missingArgumentsError(c, jobIDArgument)
}
return waitDownloadHandler(c, xid /*job ID*/)
case cmdDsort:
if xid == "" {
return missingArgumentsError(c, jobIDArgument)
}
return waitDsortHandler(c, xid /*job ID*/)
}
// TODO: niy
if flagIsSet(c, refreshFlag) {
warn := fmt.Sprintf("ignoring flag %s - not fully implemented yet", qflprn(refreshFlag))
actionWarn(c, warn)
} else if flagIsSet(c, progressFlag) {
warn := fmt.Sprintf("ignoring flag %s - not fully implemented yet", qflprn(progressFlag))
actionWarn(c, warn)
}
// x-wait
var (