-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck.go
269 lines (240 loc) · 6.1 KB
/
healthcheck.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package xlb
import (
"container/heap"
"context"
"fmt"
"github.com/rs/zerolog"
"net"
"sync"
"sync/atomic"
"time"
)
type HealthSchedulerOptions struct {
MaxItems int
Logger zerolog.Logger
ReleaseChecks int
CheckIntervalMs int
MaxWatchers int
}
type HealthCheckScheduler struct {
Q taskQueue
mu sync.Mutex
ctx context.Context
nextId int64
isSleeping int32
taskAdded chan int
logger zerolog.Logger
releaseChecks int
checkInterval int
maxWatchers uint32
curWatchers uint32
}
type healthCheckItem struct {
exec func() error
failures int
success int
route *route
}
func NewHealthCheckScheduler(opt HealthSchedulerOptions) *HealthCheckScheduler {
maxItems := 16
releaseChecks := 1
checkIntervalMs := 5000
if opt.MaxItems > 0 {
maxItems = opt.MaxItems
}
if opt.ReleaseChecks > 0 {
releaseChecks = opt.ReleaseChecks
}
if opt.CheckIntervalMs > 0 {
checkIntervalMs = opt.CheckIntervalMs
}
return &HealthCheckScheduler{
Q: newTaskQueue(maxItems),
taskAdded: make(chan int, 2),
logger: opt.Logger,
releaseChecks: releaseChecks,
checkInterval: checkIntervalMs,
maxWatchers: uint32(opt.MaxWatchers),
}
}
func (ts *HealthCheckScheduler) AddUnhealthy(ctx context.Context, rte *route, timeout time.Duration) {
// Do not accept already unhealthy routes (possibly duplicates) if one cannot change their state
// mark unhealthy on CAS
if !rte.healthy.CompareAndSwap(true, false) {
return
}
// Add to the scheduler
ts.add(&healthCheckItem{func() error {
dest, err := net.DialTimeout("tcp", rte.address, timeout)
if err != nil {
ts.logger.Error().Msgf("HC@route unreachable %s", rte.address)
return err
}
err = dest.Close()
if err != nil {
return err
}
return nil
}, 0, 0, rte}, int64(ts.checkInterval))
nextWatcherNum := atomic.AddUint32(&ts.curWatchers, 1)
if nextWatcherNum <= ts.maxWatchers {
// Add routine per health issue, however do not exceed max-routine
go ts.watchRoutine(ctx)
}
}
func (ts *HealthCheckScheduler) watchRoutine(ctx context.Context) {
for {
// Await for the scheduler
item, err := ts.poll(ctx, 1)
if err != nil {
atomic.AddUint32(&ts.curWatchers, ^uint32(0))
// Context ended return
return
}
// Execute the plan for recovery
err = item.exec()
if err != nil {
item.failures++
item.success = 0
} else {
item.success++
item.failures = 0
}
// Don't check inactive routes, just exit one of the watchers
if !item.route.active.Load() {
return
}
// Check if recovery matching strategy then exit routine
if item.success >= ts.releaseChecks {
item.route.healthy.Store(true)
atomic.AddUint32(&ts.curWatchers, ^uint32(0))
return
}
// If not ready, reschedule
// TODO: Add fading mechanics to the consecutive checks due to if we find host healthy, we want to return it back asap
ts.add(item, int64(ts.checkInterval))
}
}
func (ts *HealthCheckScheduler) add(task *healthCheckItem, after int64) {
id := atomic.AddInt64(&ts.nextId, 1)
item := &taskItem{
Id: id,
Value: task,
Priority: time.Now().UTC().Add(time.Duration(after) * time.Millisecond).Unix(),
}
ts.logger.Debug().Msgf("HC@Offer expiration offered as %d time: %d", after, item.Priority)
ts.mu.Lock()
heap.Push(&ts.Q, item)
ts.mu.Unlock()
// Check if we actually pushed new element on the top meaning new element is
// ready for the dispatch
if item.Index == 0 {
if atomic.CompareAndSwapInt32(&ts.isSleeping, 1, 0) {
// Wake up whole pool of workers
ts.taskAdded <- 1
}
}
}
func (ts *HealthCheckScheduler) poll(ctx context.Context, pollerId int) (*healthCheckItem, error) {
iteratedOnTask := int64(0)
for {
isNow := time.Now().UTC().Unix()
ts.mu.Lock()
i := ts.Q.Peek()
iteratedOnTask++
if i == nil {
// Set Sleep condition here
atomic.StoreInt32(&ts.isSleeping, 1)
ts.mu.Unlock()
// Wait for semaphore to unlock
select {
case <-ts.taskAdded:
ts.logger.Debug().Msgf("HC@Poller <%d> Called out on task added", pollerId)
break
case <-ctx.Done():
return nil, fmt.Errorf("context ended for the Poll action")
}
continue
} else {
iteratedOnTask++
task := i.(*taskItem)
// If task is ready to pop
if task.Priority <= isNow {
i = heap.Pop(&ts.Q)
ts.mu.Unlock()
ts.logger.Debug().Msgf("HC@Poller <%d> poll dequeue id <%d> at <%d> system <%d> loadIter <%d>", pollerId, task.Id, task.Priority, isNow, iteratedOnTask)
return task.Value.(*healthCheckItem), nil
} else {
// If Task should await for the next moment
ts.mu.Unlock()
select {
// Wait for general condition unlock
case <-ts.taskAdded:
ts.logger.Debug().Msgf("HC@Poller <%d> Called out on task added", pollerId)
continue
// Delay next checkup
case <-time.After(time.Duration(task.Priority-isNow) * time.Millisecond * 1000):
ts.logger.Debug().Msgf("HC@Poller <%d> Called out on time duration block end", pollerId)
continue
case <-ctx.Done():
return nil, fmt.Errorf("context ended for the Poll action")
}
}
}
}
}
// Object to operate within queue
type taskItem struct {
Id int64
Value interface{}
Priority int64
Index int
}
// Min Queue
type taskQueue []*taskItem
func newTaskQueue(capacity int) taskQueue {
return make(taskQueue, 0, capacity)
}
func (pq taskQueue) Len() int {
return len(pq)
}
func (pq taskQueue) Less(i, j int) bool {
return pq[i].Priority < pq[j].Priority
}
func (pq taskQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].Index = i
pq[j].Index = j
}
func (pq *taskQueue) Push(x interface{}) {
n := len(*pq)
c := cap(*pq)
if n+1 > c {
npq := make(taskQueue, n, c*2)
copy(npq, *pq)
*pq = npq
}
*pq = (*pq)[0 : n+1]
item := x.(*taskItem)
item.Index = n
(*pq)[n] = item
}
func (pq *taskQueue) Pop() interface{} {
n := len(*pq)
c := cap(*pq)
if n < (c/2) && c > 25 {
npq := make(taskQueue, n, c/2)
copy(npq, *pq)
*pq = npq
}
item := (*pq)[n-1]
item.Index = -1
*pq = (*pq)[0 : n-1]
return item
}
func (pq taskQueue) Peek() interface{} {
if pq.Len() == 0 {
return nil
}
return pq[0]
}