Debian Codegraph @

api/v1/lib/resources/aggregate.go

raw symbols SCIP
1 package resources
2
3 import (
4 "github.com/mesos/mesos-go/api/v1/lib"
5 )
6
7 func (n Name) Sum(resources ...mesos.Resource) (*mesos.Resource, bool) {
8 v := Reduce(Sum(n.Filter), resources...)
9 if v != nil {
10 return v, true
11 }
12 return nil, false
13 }
14
15 func CPUs(resources ...mesos.Resource) (float64, bool) {
16 v, ok := NameCPUs.Sum(resources...)
17 return v.GetScalar().GetValue(), ok
18 }
19
20 func GPUs(resources ...mesos.Resource) (uint64, bool) {
21 v, ok := NameGPUs.Sum(resources...)
22 return uint64(v.GetScalar().GetValue()), ok
23 }
24
25 func Memory(resources ...mesos.Resource) (uint64, bool) {
26 v, ok := NameMem.Sum(resources...)
27 return uint64(v.GetScalar().GetValue()), ok
28 }
29
30 func Disk(resources ...mesos.Resource) (uint64, bool) {
31 v, ok := NameDisk.Sum(resources...)
32 return uint64(v.GetScalar().GetValue()), ok
33 }
34
35 func Ports(resources ...mesos.Resource) (mesos.Ranges, bool) {
36 v, ok := NamePorts.Sum(resources...)
37 return mesos.Ranges(v.GetRanges().GetRange()), ok
38 }
39
40 func TypesOf(resources ...mesos.Resource) map[Name]mesos.Value_Type {
41 m := map[Name]mesos.Value_Type{}
42 for i := range resources {
43 name := Name(resources[i].GetName())
44 m[name] = resources[i].GetType() // TODO(jdef) check for conflicting types?
45 }
46 return m
47 }
48
49 func NamesOf(resources ...mesos.Resource) (names []Name) {
50 m := map[Name]struct{}{}
51 for i := range resources {
52 n := Name(resources[i].GetName())
53 if _, ok := m[n]; !ok {
54 m[n] = struct{}{}
55 names = append(names, n)
56 }
57 }
58 return
59 }
60
61 func SumAndCompare(expected []mesos.Resource, resources ...mesos.Resource) bool {
62 // from: https://github.com/apache/mesos/blob/master/src/common/resources.cpp
63 // This is a sanity check to ensure the amount of each type of
64 // resource does not change.
65 type total struct {
66 v *mesos.Resource
67 t mesos.Value_Type
68 ok bool
69 }
70 calcTotals := func(r []mesos.Resource) (m map[Name]total) {
71 m = make(map[Name]total)
72 for n, t := range TypesOf(expected...) {
73 v, ok := n.Sum(expected...)
74 m[n] = total{v, t, ok}
75 }
76 return
77 }
78 var (
79 et = calcTotals(expected)
80 rt = calcTotals(resources)
81 )
82 for n, tot := range et {
83 r, ok := rt[n]
84 if !ok {
85 return false
86 }
87 if tot.t != r.t || tot.ok != r.ok {
88 return false
89 }
90 switch r.t {
91 case mesos.SCALAR:
92 if v1, v2 := tot.v.GetScalar().GetValue(), r.v.GetScalar().GetValue(); v1 != v2 {
93 return false
94 }
95 case mesos.RANGES:
96 v1, v2 := mesos.Ranges(tot.v.GetRanges().GetRange()), mesos.Ranges(r.v.GetRanges().GetRange())
97 if !v1.Equivalent(v2) { // TODO(jdef): assumes that v1 and v2 are in sort-order, is that guaranteed here?
98 return false
99 }
100 case mesos.SET:
101 if tot.v.GetSet().Compare(r.v.GetSet()) != 0 {
102 return false
103 }
104 default:
105 // noop; we don't know how to sum other types, so ignore...
106 }
107 delete(rt, n)
108 }
109 return len(rt) == 0
110 }
111
112 type (
113 // FlattenOpt is a functional option for resource flattening, via Flatten. Implementations are expected to
114 // type-narrow the given interface, matching against WithRole, WithReservation or both methods (see Role.Assign)
115 FlattenOpt func(interface{})
116
117 flattenConfig struct {
118 role string
119 reservation *mesos.Resource_ReservationInfo
120 }
121 )
122
123 func (fc *flattenConfig) WithRole(role string) { fc.role = role }
124 func (fc *flattenConfig) WithReservation(r *mesos.Resource_ReservationInfo) { fc.reservation = r }
125
126 func Flatten(resources []mesos.Resource, opts ...FlattenOpt) []mesos.Resource {
127 var flattened mesos.Resources
128 fc := &flattenConfig{}
129 for _, f := range opts {
130 f(fc)
131 }
132 if fc.role == "" {
133 fc.role = string(RoleDefault)
134 }
135 // we intentionally manipulate a copy 'r' of the item in resources
136 for _, r := range resources {
137 r.Role = &fc.role
138 r.Reservation = fc.reservation
139 flattened.Add1(r)
140 }
141 return flattened
142 }