21139: Remove excess concurrency to reduce mutex/goroutine overhead.
[lightning.git] / flake.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package lightning
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11         "io"
12         "net/http"
13         _ "net/http/pprof"
14
15         "git.arvados.org/arvados.git/sdk/go/arvados"
16         log "github.com/sirupsen/logrus"
17 )
18
19 type flakecmd struct {
20         filter filter
21 }
22
23 func (cmd *flakecmd) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
24         var err error
25         defer func() {
26                 if err != nil {
27                         fmt.Fprintf(stderr, "%s\n", err)
28                 }
29         }()
30         flags := flag.NewFlagSet("", flag.ContinueOnError)
31         flags.SetOutput(stderr)
32         pprof := flags.String("pprof", "", "serve Go profile data at http://`[addr]:port`")
33         runlocal := flags.Bool("local", false, "run on local host (default: run in an arvados container)")
34         projectUUID := flags.String("project", "", "project `UUID` for output data")
35         priority := flags.Int("priority", 500, "container request priority")
36         inputDir := flags.String("input-dir", "./in", "input `directory`")
37         outputDir := flags.String("output-dir", "./out", "output `directory`")
38         cmd.filter.Flags(flags)
39         err = flags.Parse(args)
40         if err == flag.ErrHelp {
41                 err = nil
42                 return 0
43         } else if err != nil {
44                 return 2
45         } else if flags.NArg() > 0 {
46                 err = fmt.Errorf("errant command line arguments after parsed flags: %v", flags.Args())
47                 return 2
48         }
49
50         if *pprof != "" {
51                 go func() {
52                         log.Println(http.ListenAndServe(*pprof, nil))
53                 }()
54         }
55
56         if !*runlocal {
57                 runner := arvadosContainerRunner{
58                         Name:        "lightning flake",
59                         Client:      arvados.NewClientFromEnv(),
60                         ProjectUUID: *projectUUID,
61                         RAM:         700000000000,
62                         VCPUs:       96,
63                         Priority:    *priority,
64                         KeepCache:   2,
65                         APIAccess:   true,
66                 }
67                 err = runner.TranslatePaths(inputDir)
68                 if err != nil {
69                         return 1
70                 }
71                 runner.Args = []string{"flake", "-local=true",
72                         "-pprof", ":6060",
73                         "-input-dir", *inputDir,
74                         "-output-dir", "/mnt/output",
75                         "-max-variants", fmt.Sprintf("%d", cmd.filter.MaxVariants),
76                         "-min-coverage", fmt.Sprintf("%f", cmd.filter.MinCoverage),
77                         "-max-tag", fmt.Sprintf("%d", cmd.filter.MaxTag),
78                 }
79                 var output string
80                 output, err = runner.Run()
81                 if err != nil {
82                         return 1
83                 }
84                 fmt.Fprintln(stdout, output)
85                 return 0
86         }
87
88         tilelib := &tileLibrary{
89                 retainNoCalls:       true,
90                 retainTileSequences: true,
91                 compactGenomes:      map[string][]tileVariantID{},
92         }
93         err = tilelib.LoadDir(context.Background(), *inputDir)
94         if err != nil {
95                 return 1
96         }
97
98         log.Info("filtering")
99         cmd.filter.Apply(tilelib)
100         log.Info("tidying")
101         tilelib.Tidy()
102         err = tilelib.WriteDir(*outputDir)
103         if err != nil {
104                 return 1
105         }
106         return 0
107 }