X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/10dc1ca759592b7281265ac1378bda126c979208..570af13f6dfd7f725abf49335b7abdb46aae5048:/services/crunch-dispatch-local/crunch-dispatch-local.go diff --git a/services/crunch-dispatch-local/crunch-dispatch-local.go b/services/crunch-dispatch-local/crunch-dispatch-local.go index fc10393626..4a45f10545 100644 --- a/services/crunch-dispatch-local/crunch-dispatch-local.go +++ b/services/crunch-dispatch-local/crunch-dispatch-local.go @@ -17,10 +17,11 @@ import ( "syscall" "time" - "git.curoverse.com/arvados.git/sdk/go/arvados" - "git.curoverse.com/arvados.git/sdk/go/arvadosclient" - "git.curoverse.com/arvados.git/sdk/go/dispatch" - "github.com/Sirupsen/logrus" + "git.arvados.org/arvados.git/lib/config" + "git.arvados.org/arvados.git/sdk/go/arvados" + "git.arvados.org/arvados.git/sdk/go/arvadosclient" + "git.arvados.org/arvados.git/sdk/go/dispatch" + "github.com/sirupsen/logrus" ) var version = "dev" @@ -74,6 +75,13 @@ func doMain() error { return nil } + loader := config.NewLoader(nil, logger) + cfg, err := loader.Load() + cluster, err := cfg.GetCluster("") + if err != nil { + return fmt.Errorf("config error: %s", err) + } + logger.Printf("crunch-dispatch-local %s started", version) runningCmds = make(map[string]*exec.Cmd) @@ -85,14 +93,15 @@ func doMain() error { } arv.Retries = 25 + ctx, cancel := context.WithCancel(context.Background()) + dispatcher := dispatch.Dispatcher{ Logger: logger, Arv: arv, - RunContainer: run, + RunContainer: (&LocalRun{startFunc, make(chan bool, 8), ctx, cluster}).run, PollPeriod: time.Duration(*pollInterval) * time.Second, } - ctx, cancel := context.WithCancel(context.Background()) err = dispatcher.Run(ctx) if err != nil { return err @@ -123,7 +132,12 @@ func startFunc(container arvados.Container, cmd *exec.Cmd) error { return cmd.Start() } -var startCmd = startFunc +type LocalRun struct { + startCmd func(container arvados.Container, cmd *exec.Cmd) error + concurrencyLimit chan bool + ctx context.Context + cluster *arvados.Cluster +} // Run a container. // @@ -133,16 +147,38 @@ var startCmd = startFunc // // If the container is in any other state, or is not Complete/Cancelled after // crunch-run terminates, mark the container as Cancelled. -func run(dispatcher *dispatch.Dispatcher, +func (lr *LocalRun) run(dispatcher *dispatch.Dispatcher, container arvados.Container, status <-chan arvados.Container) { uuid := container.UUID if container.State == dispatch.Locked { + + select { + case lr.concurrencyLimit <- true: + break + case <-lr.ctx.Done(): + return + } + + defer func() { <-lr.concurrencyLimit }() + + select { + case c := <-status: + // Check for state updates after possibly + // waiting to be ready-to-run + if c.Priority == 0 { + goto Finish + } + default: + break + } + waitGroup.Add(1) + defer waitGroup.Done() - cmd := exec.Command(*crunchRunCommand, uuid) + cmd := exec.Command(*crunchRunCommand, "--runtime-engine="+lr.cluster.Containers.RuntimeEngine, uuid) cmd.Stdin = nil cmd.Stderr = os.Stderr cmd.Stdout = os.Stderr @@ -153,7 +189,7 @@ func run(dispatcher *dispatch.Dispatcher, // succeed in starting crunch-run. runningCmdsMutex.Lock() - if err := startCmd(container, cmd); err != nil { + if err := lr.startCmd(container, cmd); err != nil { runningCmdsMutex.Unlock() dispatcher.Logger.Warnf("error starting %q for %s: %s", *crunchRunCommand, uuid, err) dispatcher.UpdateState(uuid, dispatch.Cancelled) @@ -194,9 +230,10 @@ func run(dispatcher *dispatch.Dispatcher, delete(runningCmds, uuid) runningCmdsMutex.Unlock() } - waitGroup.Done() } +Finish: + // If the container is not finalized, then change it to "Cancelled". err := dispatcher.Arv.Get("containers", uuid, nil, &container) if err != nil {