/
resource_proto_helper.go
57 lines (49 loc) · 1.33 KB
/
resource_proto_helper.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
package pb
import (
"fmt"
)
func (l *Location) URL() string {
return fmt.Sprintf("%s:%d", l.Server, l.Port)
}
// the distance is a relative value, similar to network lantency
func (a *Location) Distance(b *Location) float64 {
if a.DataCenter != b.DataCenter {
return 1000
}
if a.Rack != b.Rack {
return 100
}
if a.Server != b.Server {
return 10
}
return 1
}
func (a ComputeResource) Minus(b ComputeResource) ComputeResource {
return ComputeResource{
CpuCount: a.GetCpuCount() - b.GetCpuCount(),
CpuLevel: a.GetCpuLevel(),
MemoryMb: a.GetMemoryMb() - b.GetMemoryMb(),
GpuCount: a.GetGpuCount() - b.GetGpuCount(),
GpuLevel: a.GetGpuLevel(),
DiskMb: a.GetDiskMb() - b.GetDiskMb(),
}
}
func (a ComputeResource) Plus(b ComputeResource) ComputeResource {
return ComputeResource{
CpuCount: a.GetCpuCount() + b.GetCpuCount(),
CpuLevel: b.GetCpuLevel(),
MemoryMb: a.GetMemoryMb() + b.GetMemoryMb(),
GpuCount: a.GetGpuCount() + b.GetGpuCount(),
GpuLevel: b.GetGpuLevel(),
DiskMb: a.GetDiskMb() + b.GetDiskMb(),
}
}
func (a ComputeResource) GreaterThanZero() bool {
return a.CpuCount > 0 && a.MemoryMb > 0
}
func (a ComputeResource) IsZero() bool {
return a.CpuCount == 0 && a.MemoryMb == 0
}
func (a ComputeResource) Covers(b ComputeResource) bool {
return a.CpuCount >= b.CpuCount && a.MemoryMb >= b.MemoryMb
}