Skip to content

Commit 7a33650

Browse files
authored
Merge pull request #609 from mcshooter/fixWindowsCPUIssue
Prevent uptimeFunc from being called everytime CheckHealth is called
2 parents a276a05 + 26f070b commit 7a33650

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pkg/healthchecker/health_checker.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,27 @@ func NewHealthChecker(hco *options.HealthCheckerOptions) (types.HealthChecker, e
6565
// CheckHealth checks for the health of the component and tries to repair if enabled.
6666
// Returns true if healthy, false otherwise.
6767
func (hc *healthChecker) CheckHealth() (bool, error) {
68-
var logStartTime string
6968
healthy, err := hc.healthCheckFunc()
7069
if err != nil {
7170
return healthy, err
7271
}
73-
uptime, err := hc.uptimeFunc()
74-
if err != nil {
75-
glog.Warningf("Failed to get the uptime: %+v", err)
76-
return true, err
77-
}
78-
if hc.loopBackTime > 0 && uptime > hc.loopBackTime {
79-
logStartTime = time.Now().Add(-hc.loopBackTime).Format(types.LogParsingTimeLayout)
80-
} else {
81-
logStartTime = time.Now().Add(-uptime).Format(types.LogParsingTimeLayout)
82-
}
83-
logPatternHealthy, err := logPatternHealthCheck(hc.service, logStartTime, hc.logPatternsToCheck)
72+
logPatternHealthy, err := logPatternHealthCheck(hc.service, hc.loopBackTime, hc.logPatternsToCheck)
8473
if err != nil {
8574
return logPatternHealthy, err
8675
}
8776
if healthy && logPatternHealthy {
8877
return true, nil
8978
}
79+
9080
// The service is unhealthy.
9181
// Attempt repair based on flag.
9282
if hc.enableRepair {
9383
// repair if the service has been up for the cool down period.
84+
uptime, err := hc.uptimeFunc()
85+
if err != nil {
86+
glog.Infof("error in getting uptime for %v: %v\n", hc.component, err)
87+
return false, nil
88+
}
9489
glog.Infof("%v is unhealthy, component uptime: %v\n", hc.component, uptime)
9590
if uptime > hc.coolDownTime {
9691
glog.Infof("%v cooldown period of %v exceeded, repairing", hc.component, hc.coolDownTime)
@@ -102,10 +97,22 @@ func (hc *healthChecker) CheckHealth() (bool, error) {
10297

10398
// logPatternHealthCheck checks for the provided logPattern occurrences in the service logs.
10499
// Returns true if the pattern is empty or does not exist logThresholdCount times since start of service, false otherwise.
105-
func logPatternHealthCheck(service, logStartTime string, logPatternsToCheck map[string]int) (bool, error) {
100+
func logPatternHealthCheck(service string, loopBackTime time.Duration, logPatternsToCheck map[string]int) (bool, error) {
106101
if len(logPatternsToCheck) == 0 {
107102
return true, nil
108103
}
104+
uptimeFunc := getUptimeFunc(service)
105+
glog.Infof("Getting uptime for service: %v\n", service)
106+
uptime, err := uptimeFunc()
107+
if err != nil {
108+
glog.Warningf("Failed to get the uptime: %+v", err)
109+
return true, err
110+
}
111+
112+
logStartTime := time.Now().Add(-uptime).Format(types.LogParsingTimeLayout)
113+
if loopBackTime > 0 && uptime > loopBackTime {
114+
logStartTime = time.Now().Add(-loopBackTime).Format(types.LogParsingTimeLayout)
115+
}
109116
for pattern, count := range logPatternsToCheck {
110117
healthy, err := checkForPattern(service, logStartTime, pattern, count)
111118
if err != nil || !healthy {
@@ -127,7 +134,6 @@ func healthCheckEndpointOKFunc(endpoint string, timeout time.Duration) func() (b
127134
}
128135
}
129136

130-
131137
// getHealthCheckFunc returns the health check function based on the component.
132138
func getHealthCheckFunc(hco *options.HealthCheckerOptions) func() (bool, error) {
133139
switch hco.Component {

0 commit comments

Comments
 (0)