-
Notifications
You must be signed in to change notification settings - Fork 197
/
healthService.go
159 lines (132 loc) · 4.86 KB
/
healthService.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 health
import (
"context"
"fmt"
"os"
"path"
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/config"
)
var log = logger.GetOrCreate("health")
type healthService struct {
config config.HealthServiceConfig
folder string
cancelFunction func()
records *records
diagnosableComponents []diagnosable
diagnosableComponentsMutex sync.RWMutex
clock clock
memory memory
onMonitorContinuouslyBeginIteration func()
onMonitorContinuouslyEndIteration func()
}
// NewHealthService creates a new HealthService object
func NewHealthService(config config.HealthServiceConfig, workingDir string) *healthService {
log.Debug("NewHealthService", "config", config)
folder := path.Join(workingDir, config.FolderPath)
recordsObj := newRecords(config.NumMemoryUsageRecordsToKeep)
return &healthService{
config: config,
folder: folder,
cancelFunction: func() {},
records: recordsObj,
diagnosableComponents: make([]diagnosable, 0),
clock: &realClock{},
memory: &realMemory{},
onMonitorContinuouslyBeginIteration: func() {},
onMonitorContinuouslyEndIteration: func() {},
}
}
// RegisterComponent registers a diagnosable component
func (h *healthService) RegisterComponent(component interface{}) {
err := h.doRegisterComponent(component)
if err != nil {
log.Error("healthService.RegisterComponent()", "err", err, "component", fmt.Sprintf("%T", component))
}
}
func (h *healthService) doRegisterComponent(component interface{}) error {
asDiagnosable, ok := component.(diagnosable)
if !ok {
return errNotDiagnosableComponent
}
if check.IfNil(asDiagnosable) {
return errNilComponent
}
h.diagnosableComponentsMutex.Lock()
h.diagnosableComponents = append(h.diagnosableComponents, asDiagnosable)
h.diagnosableComponentsMutex.Unlock()
return nil
}
// Start starts the health service
func (h *healthService) Start() {
log.Debug("healthService.Start()")
h.prepareFolder()
ctx := h.setupCancellation()
go h.monitorContinuously(ctx)
}
func (h *healthService) prepareFolder() {
err := os.MkdirAll(h.folder, os.ModePerm)
if err != nil {
log.Error("healthService.prepareFolder", "err", err)
}
}
func (h *healthService) setupCancellation() context.Context {
ctx, cancelFunc := context.WithCancel(context.Background())
h.cancelFunction = cancelFunc
return ctx
}
func (h *healthService) monitorContinuously(ctx context.Context) {
intervalVerifyMemoryInSeconds := time.Duration(h.config.IntervalVerifyMemoryInSeconds) * time.Second
intervalDiagnoseComponentsInSeconds := time.Duration(h.config.IntervalDiagnoseComponentsInSeconds) * time.Second
intervalDiagnoseComponentsDeeplyInSeconds := time.Duration(h.config.IntervalDiagnoseComponentsDeeplyInSeconds) * time.Second
chanMonitorMemory := h.clock.after(intervalVerifyMemoryInSeconds)
chanDiagnoseComponents := h.clock.after(intervalDiagnoseComponentsInSeconds)
chanDiagnoseComponentsDeeply := h.clock.after(intervalDiagnoseComponentsDeeplyInSeconds)
for {
h.onMonitorContinuouslyBeginIteration()
select {
case <-chanMonitorMemory:
h.monitorMemory()
chanMonitorMemory = h.clock.after(intervalVerifyMemoryInSeconds)
case <-chanDiagnoseComponents:
h.diagnoseComponents(false)
chanDiagnoseComponents = h.clock.after(intervalDiagnoseComponentsInSeconds)
case <-chanDiagnoseComponentsDeeply:
h.diagnoseComponents(true)
chanDiagnoseComponentsDeeply = h.clock.after(intervalDiagnoseComponentsDeeplyInSeconds)
case <-ctx.Done():
log.Debug("healthService.monitorContinuously() ended")
return
}
h.onMonitorContinuouslyEndIteration()
}
}
func (h *healthService) monitorMemory() {
stats := h.memory.getStats()
log.Trace("healthService.monitorMemory()", "heapInUse", core.ConvertBytes(stats.HeapInuse))
if int(stats.HeapInuse) > h.config.MemoryUsageToCreateProfiles {
recordObj := newMemoryUsageRecord(stats, h.clock.now(), h.folder)
h.records.addRecord(recordObj)
}
}
func (h *healthService) diagnoseComponents(deep bool) {
log.Trace("healthService.diagnoseComponents()", "deep", deep)
h.diagnosableComponentsMutex.RLock()
defer h.diagnosableComponentsMutex.RUnlock()
for _, component := range h.diagnosableComponents {
component.Diagnose(deep)
}
}
// Close stops the service
func (h *healthService) Close() error {
h.cancelFunction()
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (h *healthService) IsInterfaceNil() bool {
return h == nil
}