-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
148 lines (136 loc) · 3.6 KB
/
group.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
package healthz
import (
"sort"
"sync"
)
type componentState uint8
const (
componentStateUnknown componentState = iota
componentStateStandalone
componentStateGroup
)
type componentGroup struct {
Severity Severity
Health Health
Subcomponents map[string]*componentGroup
state componentState
mutex sync.RWMutex
}
func newComponentGroup(severity Severity, health Health) *componentGroup {
return &componentGroup{
Severity: severity,
Health: health,
Subcomponents: make(map[string]*componentGroup),
state: componentStateUnknown,
}
}
func (c *componentGroup) RegisterSubcomponent(name string, severity Severity) ComponentGroup {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.state == componentStateStandalone {
panic("You should avoid calling both SetGroupHealth and RegisterSubcomponent on same ComponentGroup")
}
c.state = componentStateGroup
c.Subcomponents[name] = newComponentGroup(severity, Unknown)
return c.Subcomponents[name]
}
func (c *componentGroup) UnregisterSubcomponent(name string) {
c.mutex.Lock()
delete(c.Subcomponents, name)
c.mutex.Unlock()
}
func (c *componentGroup) SetGroupHealth(health Health) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.state == componentStateGroup {
panic("You should avoid calling both SetGroupHealth and RegisterSubcomponent on same ComponentGroup")
}
c.state = componentStateStandalone
c.Health = health
}
func (c *componentGroup) OverallHealth() Health {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.state == componentStateStandalone {
return c.Health
}
if len(c.Subcomponents) == 0 {
return Unknown
}
res := Redundant
var majorIsInGroup, unspecifiedIsInGroup bool
for _, c := range c.Subcomponents {
if c.Severity >= Major {
majorIsInGroup = true
componentHealth := c.OverallHealth()
if res > componentHealth {
res = componentHealth
}
}
}
if !majorIsInGroup && res > Normal {
res = Normal
}
for _, c := range c.Subcomponents {
if c.Severity == Unspecified {
unspecifiedIsInGroup = true
componentHealth := c.OverallHealth()
if majorIsInGroup || componentHealth < Normal {
componentHealth++
}
if res > componentHealth {
res = componentHealth
}
}
}
if !majorIsInGroup && !unspecifiedIsInGroup {
res = Unknown
}
return res
}
func (c *componentGroup) GroupReport() *GroupReport {
c.mutex.RLock()
defer c.mutex.RUnlock()
rc := &GroupReport{
Severity: c.Severity,
Subcomponents: make([]*GroupReport, 0, len(c.Subcomponents)),
}
if c.state == componentStateStandalone || len(c.Subcomponents) == 0 {
rc.OverallHealth = c.Health
return rc
}
// TODO: how can we refactor this algorithm which also appears in OverallHealth()?
rc.OverallHealth = Redundant
var majorIsInGroup, unspecifiedIsInGroup bool
for name, c := range c.Subcomponents {
subcomponentRC := c.GroupReport()
subcomponentRC.Name = name
rc.Subcomponents = append(rc.Subcomponents, subcomponentRC)
if c.Severity >= Major {
majorIsInGroup = true
if rc.OverallHealth > subcomponentRC.OverallHealth {
rc.OverallHealth = subcomponentRC.OverallHealth
}
}
}
if !majorIsInGroup && rc.OverallHealth > Normal {
rc.OverallHealth = Normal
}
for _, subcomponentRC := range rc.Subcomponents {
if subcomponentRC.Severity == Unspecified {
unspecifiedIsInGroup = true
componentHealth := subcomponentRC.OverallHealth
if majorIsInGroup || componentHealth < Normal {
componentHealth++
}
if rc.OverallHealth > componentHealth {
rc.OverallHealth = componentHealth
}
}
}
if !majorIsInGroup && !unspecifiedIsInGroup {
rc.OverallHealth = Unknown
}
sort.Sort(rc)
return rc
}