-
Notifications
You must be signed in to change notification settings - Fork 291
/
structure.go
143 lines (114 loc) · 2.44 KB
/
structure.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package master
import (
"sync"
"time"
"github.com/chrislusf/gleam/pb"
)
type AgentInformation struct {
Location pb.Location
LastHeartBeat time.Time
Resource pb.ComputeResource
Allocated pb.ComputeResource
}
type Rack struct {
sync.RWMutex
Name string
Agents map[string]*AgentInformation
Resource pb.ComputeResource
Allocated pb.ComputeResource
}
type DataCenter struct {
sync.RWMutex
Name string
Racks map[string]*Rack
Resource pb.ComputeResource
Allocated pb.ComputeResource
}
type Topology struct {
Resource pb.ComputeResource
Allocated pb.ComputeResource
sync.RWMutex
DataCenters map[string]*DataCenter
}
func NewTopology() *Topology {
return &Topology{
DataCenters: make(map[string]*DataCenter),
}
}
func NewDataCenter(name string) *DataCenter {
return &DataCenter{
Name: name,
Racks: make(map[string]*Rack),
}
}
func NewRack(name string) *Rack {
return &Rack{
Name: name,
Agents: make(map[string]*AgentInformation),
}
}
func (tp *Topology) GetDataCenter(name string) (*DataCenter, bool) {
tp.RLock()
defer tp.RUnlock()
dc, ok := tp.DataCenters[name]
return dc, ok
}
func (dc *DataCenter) GetRack(name string) (*Rack, bool) {
dc.RLock()
defer dc.RUnlock()
rack, ok := dc.Racks[name]
return rack, ok
}
func (rack *Rack) GetAgent(name string) (*AgentInformation, bool) {
rack.RLock()
defer rack.RUnlock()
agentInformation, ok := rack.Agents[name]
return agentInformation, ok
}
func (tp *Topology) AddDataCenter(dc *DataCenter) {
tp.Lock()
defer tp.Unlock()
tp.DataCenters[dc.Name] = dc
}
func (tp *Topology) GetDataCenters() map[string]*DataCenter {
tp.RLock()
defer tp.RUnlock()
s := make(map[string]*DataCenter, len(tp.DataCenters))
for k, v := range tp.DataCenters {
s[k] = v
}
return s
}
func (dc *DataCenter) GetRacks() (ret []*Rack) {
dc.RLock()
defer dc.RUnlock()
for _, v := range dc.Racks {
r := v
ret = append(ret, r)
}
return
}
func (dc *DataCenter) AddRack(rack *Rack) {
dc.Lock()
defer dc.Unlock()
dc.Racks[rack.Name] = rack
}
func (rack *Rack) AddAgent(a *AgentInformation) {
rack.Lock()
defer rack.Unlock()
rack.Agents[a.Location.URL()] = a
}
func (rack *Rack) DropAgent(location *pb.Location) {
rack.Lock()
defer rack.Unlock()
delete(rack.Agents, location.URL())
}
func (rack *Rack) GetAgents() (ret []*AgentInformation) {
rack.RLock()
defer rack.RUnlock()
for _, v := range rack.Agents {
a := v
ret = append(ret, a)
}
return
}