1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 var ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
20 var discountConfiguredRAMPercent = 5
22 // ConstraintsNotSatisfiableError includes a list of available instance types
23 // to be reported back to the user.
24 type ConstraintsNotSatisfiableError struct {
26 AvailableTypes []arvados.InstanceType
29 // ChooseInstanceType returns the cheapest available
30 // arvados.InstanceType big enough to run ctr.
31 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
32 if len(cc.InstanceTypes) == 0 {
33 err = ErrInstanceTypesNotConfigured
37 needScratch := int64(0)
38 for _, m := range ctr.Mounts {
40 needScratch += m.Capacity
44 needVCPUs := ctr.RuntimeConstraints.VCPUs
46 needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
47 needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
50 for _, it := range cc.InstanceTypes {
52 case ok && it.Price > best.Price:
53 case int64(it.Scratch) < needScratch:
54 case int64(it.RAM) < needRAM:
55 case it.VCPUs < needVCPUs:
56 case it.Preemptible != ctr.SchedulingParameters.Preemptible:
57 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs):
58 // Equal price, but worse specs
60 // Lower price || (same price && better specs)
66 availableTypes := make([]arvados.InstanceType, 0, len(cc.InstanceTypes))
67 for _, t := range cc.InstanceTypes {
68 availableTypes = append(availableTypes, t)
70 sort.Slice(availableTypes, func(a, b int) bool {
71 return availableTypes[a].Price < availableTypes[b].Price
73 err = ConstraintsNotSatisfiableError{
74 errors.New("constraints not satisfiable by any configured instance type"),
82 // SlurmNodeTypeFeatureKludge ensures SLURM accepts every instance
83 // type name as a valid feature name, even if no instances of that
84 // type have appeared yet.
86 // It takes advantage of some SLURM peculiarities:
88 // (1) A feature is valid after it has been offered by a node, even if
89 // it is no longer offered by any node. So, to make a feature name
90 // valid, we can add it to a dummy node ("compute0"), then remove it.
92 // (2) To test whether a set of feature names are valid without
93 // actually submitting a job, we can call srun --test-only with the
96 // SlurmNodeTypeFeatureKludge does a test-and-fix operation
97 // immediately, and then periodically, in case slurm restarts and
98 // forgets the list of valid features. It never returns (unless there
99 // are no node types configured, in which case it returns
100 // immediately), so it should generally be invoked with "go".
101 func SlurmNodeTypeFeatureKludge(cc *arvados.Cluster) {
102 if len(cc.InstanceTypes) == 0 {
105 var features []string
106 for _, it := range cc.InstanceTypes {
107 features = append(features, "instancetype="+it.Name)
110 slurmKludge(features)
111 time.Sleep(2 * time.Second)
115 const slurmDummyNode = "compute0"
117 func slurmKludge(features []string) {
118 allFeatures := strings.Join(features, ",")
120 cmd := exec.Command("sinfo", "--nodes="+slurmDummyNode, "--format=%f", "--noheader")
121 out, err := cmd.CombinedOutput()
123 log.Printf("running %q %q: %s (output was %q)", cmd.Path, cmd.Args, err, out)
126 if string(out) == allFeatures+"\n" {
127 // Already configured correctly, nothing to do.
131 log.Printf("configuring node %q with all node type features", slurmDummyNode)
132 cmd = exec.Command("scontrol", "update", "NodeName="+slurmDummyNode, "Features="+allFeatures)
133 log.Printf("running: %q %q", cmd.Path, cmd.Args)
134 out, err = cmd.CombinedOutput()
136 log.Printf("error: scontrol: %s (output was %q)", err, out)