-
Notifications
You must be signed in to change notification settings - Fork 32
/
predicates.go
110 lines (97 loc) · 3.02 KB
/
predicates.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
package scheduler
import (
"fmt"
"github.com/ohsu-comp-bio/funnel/tes"
)
// Predicate is a function that checks whether a task fits a node.
type Predicate func(*tes.Task, *Node) error
// ResourcesFit determines whether a task fits a node's resources.
func ResourcesFit(t *tes.Task, n *Node) error {
req := t.GetResources()
switch {
case n.GetPreemptible() && !req.GetPreemptible():
return fmt.Errorf("Fail preemptible")
case n.GetAvailable().GetCpus() <= 0:
return fmt.Errorf("Fail zero cpus available")
case n.GetAvailable().GetRamGb() <= 0.0:
return fmt.Errorf("Fail zero ram available")
case n.GetAvailable().GetDiskGb() <= 0.0:
return fmt.Errorf("Fail zero disk available")
case n.GetAvailable().GetCpus() < uint32(req.GetCpuCores()):
return fmt.Errorf(
"Fail cpus, requested %d, available %d",
req.GetCpuCores(),
n.GetAvailable().GetCpus(),
)
case n.GetAvailable().GetRamGb() < req.GetRamGb():
return fmt.Errorf(
"Fail ram, requested %f, available %f",
req.GetRamGb(),
n.GetAvailable().GetRamGb(),
)
case n.GetAvailable().GetDiskGb() < req.GetDiskGb():
return fmt.Errorf(
"Fail disk, requested %f, available %f",
req.GetDiskGb(),
n.GetAvailable().GetDiskGb(),
)
}
return nil
}
// ZonesFit determines whether a task's zones fit a node.
func ZonesFit(t *tes.Task, n *Node) error {
if n.Zone == "" {
// Node doesn't have a set zone, so don't bother checking.
return nil
}
if len(t.GetResources().GetZones()) == 0 {
// Request doesn't specify any zones, so don't bother checking.
return nil
}
for _, z := range t.GetResources().GetZones() {
if z == n.Zone {
return nil
}
}
return fmt.Errorf("Failed zones")
}
// NotDead returns true if the node state is not Dead or Gone.
func NotDead(j *tes.Task, n *Node) error {
if n.State != NodeState_DEAD && n.State != NodeState_GONE {
return nil
}
return fmt.Errorf("Fail not dead")
}
// Alive returns true if the node state is not Dead or Gone.
func Alive(j *tes.Task, n *Node) error {
if n.State != NodeState_ALIVE {
return fmt.Errorf("Fail not alive")
}
return nil
}
// NodeHasTag returns a predicate function which returns true
// if the node has the given tag (key in Metadata field).
func NodeHasTag(tag string) Predicate {
return func(j *tes.Task, n *Node) error {
if _, ok := n.Metadata[tag]; !ok {
return fmt.Errorf("fail node has tag: %s", tag)
}
return nil
}
}
// TODO should have a predicate which understands authorization
// - storage
// - other auth resources?
// - does storage need to be scheduler specific?
// - how can we detect that a task cannot ever be scheduled? can we?
// for example, if it requests access to storage that isn't available?
// maybe set a max. time allowed to be unscheduled before notification
// Match checks whether a task fits a node using the given Predicate list.
func Match(node *Node, task *tes.Task, predicates []Predicate) bool {
for _, pred := range predicates {
if err := pred(task, node); err != nil {
return false
}
}
return true
}