-
Notifications
You must be signed in to change notification settings - Fork 1
/
subsystem.go
159 lines (138 loc) · 3.92 KB
/
subsystem.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package memory
import (
"fmt"
"github.com/converged-computing/jsongraph-go/jsongraph/metadata"
"github.com/converged-computing/rainbow/pkg/types"
)
// NewSubsystem generates a new subsystem graph
func NewSubsystem(name string) *Subsystem {
vertices := map[int]*types.Vertex{}
lookup := map[string]int{}
metrics := Metrics{ResourceCounts: map[string]int64{}, Name: name}
s := Subsystem{
Vertices: vertices,
Lookup: lookup,
Metrics: metrics,
Name: name,
}
// Create a top level vertex for all clusters that will be added
// Question: should the root be above the subsystems?
s.AddNode("", name, 1, "", metadata.Metadata{}, false)
return &s
}
// AddNode (a physical node) as a vertex, return the vertex id
func (s *Subsystem) AddNode(
lookupName, typ string,
size int32,
unit string,
meta metadata.Metadata,
countResource bool,
) int {
// Add resource to the metrics, indexed by the level (clusterName)
// We don't count the root "root" of a subsystem, typically
if countResource {
s.Metrics.CountResource(typ)
}
id := s.counter
newEdges := map[int]*types.Edge{}
newSubsystems := map[string]map[int]*types.Edge{}
// Add the subsystem node
s.Vertices[id] = &types.Vertex{
Identifier: id,
Edges: newEdges,
Size: size,
Type: typ,
Unit: unit,
Metadata: meta,
Subsystems: newSubsystems,
}
s.counter += 1
// If name is not null, we want to remember this node for later
if lookupName != "" {
s.Lookup[lookupName] = id
}
return id
}
// GetNode returns the vertex if of a node, if it exists in the lookup
func (s *Subsystem) GetNode(name string) (int, bool) {
id, ok := s.Lookup[name]
if ok {
return id, true
}
return id, false
}
// Add an edge to the graph with a source and dest identifier
// This assumes they belong in the same subsystem (src subsystem == dest subsystem)
// Optionally add a weight. We aren't using this (but I think might)
func (s *Subsystem) AddInternalEdge(
src, dest, weight int,
relation,
subsystem string,
) error {
// We shoudn't be added identifiers that don't exist...
srcVertex, ok := s.Vertices[src]
if !ok {
return fmt.Errorf("vertex with identifier %d does not exist", src)
}
destVertex, ok := s.Vertices[dest]
if !ok {
return fmt.Errorf("vertex with identifier %d does not exist", dest)
}
// add edge src --> dest
// Right now subsystem references the source
newEdge := types.Edge{
Weight: weight,
Vertex: destVertex,
Relation: relation,
Subsystem: subsystem,
}
srcVertex.Edges[dest] = &newEdge
s.Vertices[src] = srcVertex
return nil
}
// Add an subsystem edge, meaning adding the edge AND a link to the dominant subsystem
// This would be called by the dominant to add an edge to itself
func (s *Subsystem) AddSubsystemEdge(
src int,
dest *types.Vertex,
weight int,
relation string,
subsystem string,
) error {
// The source vertex is owned by this subsystem
// But we don't check dest, it's part of another subsystem
srcVertex, ok := s.Vertices[src]
if !ok {
return fmt.Errorf("vertex with identifier %d does not exist", src)
}
// add edge src --> dest
// Right now subsystem references the source
newEdge := types.Edge{
Weight: weight,
Vertex: dest,
Relation: relation,
Subsystem: subsystem,
}
// Add the reference of the new edge here, note to
// subsystem edges. This makes the search easier so we don't
// iterate thrugh subsystem AND dominant subsystem nodes.
subsysEdges, ok := srcVertex.Subsystems[subsystem]
if !ok {
subsysEdges = map[int]*types.Edge{}
srcVertex.Subsystems[subsystem] = subsysEdges
}
srcVertex.Subsystems[subsystem][dest.Identifier] = &newEdge
s.Vertices[src] = srcVertex
return nil
}
// GetConnections to a vertex
func (s *Subsystem) GetConnections(src int) []int {
conns := []int{}
for _, edge := range s.Vertices[src].Edges {
conns = append(conns, edge.Vertex.Identifier)
}
return conns
}
func (s *Subsystem) CountVertices() int {
return len(s.Vertices)
}