/
session_checker.go
165 lines (152 loc) · 4.69 KB
/
session_checker.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
160
161
162
163
164
165
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package healthcheck
import (
"runtime/debug"
"sync/atomic"
"time"
"sofastack.io/sofa-mosn/pkg/log"
"sofastack.io/sofa-mosn/pkg/types"
"sofastack.io/sofa-mosn/pkg/utils"
)
// sessionChecker is a wrapper of types.HealthCheckSession for health check
type sessionChecker struct {
Session types.HealthCheckSession
Host types.Host
HealthChecker *healthChecker
//
resp chan checkResponse
timeout chan bool
checkID uint64
stop chan struct{}
checkTimer *utils.Timer
checkTimeout *utils.Timer
unHealthCount uint32
healthCount uint32
}
type checkResponse struct {
ID uint64
Healthy bool
}
func newChecker(s types.HealthCheckSession, h types.Host, hc *healthChecker) *sessionChecker {
c := &sessionChecker{
Session: s,
Host: h,
HealthChecker: hc,
resp: make(chan checkResponse),
timeout: make(chan bool),
stop: make(chan struct{}),
}
return c
}
var firstInterval = time.Second
func (c *sessionChecker) Start() {
defer func() {
if r := recover(); r != nil {
log.DefaultLogger.Errorf("[upstream] [health check] [session checker] panic %v\n%s", r, string(debug.Stack()))
}
// stop all the timer when start is finished
c.checkTimer.Stop()
c.checkTimeout.Stop()
}()
c.checkTimer = utils.NewTimer(firstInterval, c.OnCheck)
for {
select {
case <-c.stop:
return
default:
// prepare a check
currentID := atomic.AddUint64(&c.checkID, 1)
select {
case <-c.stop:
return
case resp := <-c.resp:
// if the ID is not equal, means we receive a timeout for this ID, ignore the response
if resp.ID == currentID {
c.checkTimeout.Stop()
if resp.Healthy {
c.HandleSuccess()
} else {
c.HandleFailure(types.FailureActive)
}
// next health checker
c.checkTimer = utils.NewTimer(c.HealthChecker.getCheckInterval(), c.OnCheck)
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("[upstream] [health check] [session checker] receive a response id: %d", resp.ID)
}
} else {
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("[upstream] [health check] [session checker] receive a expired id response, response id: %d, currentID: %d", resp.ID, currentID)
}
}
case <-c.timeout:
c.checkTimer.Stop()
c.Session.OnTimeout() // session timeout callbacks
c.HandleFailure(types.FailureNetwork)
// next health checker
c.checkTimer = utils.NewTimer(c.HealthChecker.getCheckInterval(), c.OnCheck)
if log.DefaultLogger.GetLogLevel() >= log.DEBUG {
log.DefaultLogger.Debugf("[upstream] [health check] [session checker] receive a timeout response at id: %d", currentID)
}
}
}
}
}
func (c *sessionChecker) Stop() {
close(c.stop)
}
func (c *sessionChecker) HandleSuccess() {
c.unHealthCount = 0
changed := false
if c.Host.ContainHealthFlag(types.FAILED_ACTIVE_HC) {
c.healthCount++
// check the threshold
if c.healthCount == c.HealthChecker.healthyThreshold {
changed = true
c.Host.ClearHealthFlag(types.FAILED_ACTIVE_HC)
}
}
c.HealthChecker.incHealthy(c.Host, changed)
}
func (c *sessionChecker) HandleFailure(reason types.FailureType) {
c.healthCount = 0
changed := false
if !c.Host.ContainHealthFlag(types.FAILED_ACTIVE_HC) {
c.unHealthCount++
// check the threshold
if c.unHealthCount == c.HealthChecker.unhealthyThreshold {
changed = true
c.Host.SetHealthFlag(types.FAILED_ACTIVE_HC)
}
}
c.HealthChecker.decHealthy(c.Host, reason, changed)
}
func (c *sessionChecker) OnCheck() {
// record current id
id := atomic.LoadUint64(&c.checkID)
c.HealthChecker.stats.attempt.Inc(1)
// start a timeout before check health
c.checkTimeout.Stop()
c.checkTimeout = utils.NewTimer(c.HealthChecker.timeout, c.OnTimeout)
c.resp <- checkResponse{
ID: id,
Healthy: c.Session.CheckHealth(),
}
}
func (c *sessionChecker) OnTimeout() {
c.timeout <- true
}