-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathhealth.go
More file actions
264 lines (236 loc) · 7.67 KB
/
Copy pathhealth.go
File metadata and controls
264 lines (236 loc) · 7.67 KB
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package awss3
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/elastic/beats/v7/libbeat/management/status"
"github.com/elastic/elastic-agent-libs/logp"
)
// defaultRecvFailThreshold is the number of consecutive ReceiveMessage
// failures required before the input reports Degraded for receive errors.
const defaultRecvFailThreshold = 3
// defaultProcFailThreshold is the number of consecutive S3 object processing
// failures required before the input reports Degraded for processing errors.
const defaultProcFailThreshold = 3
// sqsHealth aggregates health signals from the SQS reader, S3 processors,
// and message disposition callbacks into a single coherent status for Fleet.
//
// It replaces the pattern of scattered UpdateStatus calls that race on one
// reporter. Processing code reports events (Set*/Clear*); the aggregator
// decides what state to publish based on active conditions.
//
// sqsHealth implements status.StatusReporter so it can be passed to
// readSQSMessages (which expects that interface) without signature changes.
type sqsHealth struct {
mu sync.Mutex
reporter status.StatusReporter
log *logp.Logger
conditions map[condition]healthCondition
consecutiveRecvFails int
recvFailThreshold int
consecutiveProcFails int
procFailThreshold int
currentStatus status.Status
currentMsg string
closed bool
}
type condition string
const (
condReceive condition = "receive"
condWorker condition = "worker"
condProcess condition = "process"
condDelete condition = "delete"
condFinalize condition = "finalize"
condPoison condition = "poison"
)
type healthCondition struct {
msg string
at time.Time
}
func newSQSHealth(reporter status.StatusReporter, log *logp.Logger) *sqsHealth {
return &sqsHealth{
reporter: reporter,
log: log,
conditions: make(map[condition]healthCondition),
recvFailThreshold: defaultRecvFailThreshold,
procFailThreshold: defaultProcFailThreshold,
}
}
// UpdateStatus satisfies status.StatusReporter. It is called by
// readSQSMessages (which reports receive-level Degraded/Running) and by
// lifecycle code (Starting, Configuring, Failed, Stopped).
//
// Lifecycle states pass through directly and reset runtime conditions.
// Running from readSQSMessages clears the receive condition.
// Degraded from readSQSMessages tracks consecutive receive failures.
func (h *sqsHealth) UpdateStatus(s status.Status, msg string) {
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
switch s {
case status.Starting, status.Configuring, status.Stopping, status.Stopped, status.Failed:
clear(h.conditions)
h.consecutiveRecvFails = 0
h.consecutiveProcFails = 0
h.publish(s, msg)
if s == status.Stopped || s == status.Failed {
h.closed = true
}
case status.Running:
h.consecutiveRecvFails = 0
delete(h.conditions, condReceive)
h.update()
case status.Degraded:
h.consecutiveRecvFails++
if h.consecutiveRecvFails >= h.recvFailThreshold {
h.conditions[condReceive] = healthCondition{msg: msg, at: time.Now()}
}
h.update()
}
}
// SetWorkerError records a worker setup failure (for example pipeline
// connection error). This is a persistent condition cleared only by a
// successful lifecycle transition.
func (h *sqsHealth) SetWorkerError(err error) {
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.conditions[condWorker] = healthCondition{
msg: fmt.Sprintf("The input could not start an SQS message processor: %s", err),
at: time.Now(),
}
h.update()
}
// SetProcessingError records an S3 processing failure. Errors caused by
// context cancellation (shutdown) are suppressed. Individual failures do
// not degrade, but sustained consecutive failures (above threshold)
// indicate a persistent problem like missing S3 permissions.
func (h *sqsHealth) SetProcessingError(err error) {
if isShutdownErr(err) {
return
}
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.consecutiveProcFails++
if h.consecutiveProcFails >= h.procFailThreshold {
h.conditions[condProcess] = healthCondition{
msg: fmt.Sprintf("The input cannot process S3 objects (%d consecutive failures): %s", h.consecutiveProcFails, err),
at: time.Now(),
}
h.update()
}
}
// SetDeleteFailed records a failure to delete an SQS message after
// successful processing. This means the message will be reprocessed,
// causing duplicates.
func (h *sqsHealth) SetDeleteFailed(err error) {
if isShutdownErr(err) {
return
}
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.conditions[condDelete] = healthCondition{
msg: fmt.Sprintf("The input processed an SQS message but could not delete it from the queue. AWS may deliver the message again, which can result in duplicate events: %s", err),
at: time.Now(),
}
h.update()
}
// SetFinalizeFailed records a failure to finalize S3 objects after
// successful processing and deletion.
func (h *sqsHealth) SetFinalizeFailed(err error) {
if isShutdownErr(err) {
return
}
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.conditions[condFinalize] = healthCondition{
msg: fmt.Sprintf("The input could not copy an S3 object to the backup bucket, or could not delete the source object after backup. Check the backup bucket configuration and permissions: %s", err),
at: time.Now(),
}
h.update()
}
// RecordPoisonPill records that a message was deleted as a poison pill
// (non-retryable error after max receives). This is a data-loss signal.
func (h *sqsHealth) RecordPoisonPill(err error) {
if isShutdownErr(err) {
return
}
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.conditions[condPoison] = healthCondition{
msg: fmt.Sprintf("The input stopped retrying an SQS message after repeated processing failures and deleted it from the queue. Data from that notification may be missing: %s", err),
at: time.Now(),
}
h.update()
}
// ClearDisposition clears delete, finalize, poison-pill, and processing
// conditions after a successful end-to-end message completion (delete +
// finalize). Resets the consecutive processing failure counter.
func (h *sqsHealth) ClearDisposition() {
h.mu.Lock()
defer h.mu.Unlock()
if h.closed {
return
}
h.consecutiveProcFails = 0
delete(h.conditions, condProcess)
delete(h.conditions, condDelete)
delete(h.conditions, condFinalize)
delete(h.conditions, condPoison)
h.update()
}
// update derives the aggregate status from active conditions and
// publishes it if changed. Must be called with h.mu held.
func (h *sqsHealth) update() {
if len(h.conditions) == 0 {
h.publish(status.Running, "Input is running")
return
}
msg := h.pickMessage()
h.publish(status.Degraded, msg)
}
// pickMessage returns the message from the highest-priority active condition.
// Priority order: worker, receive, process, delete, finalize, poison.
func (h *sqsHealth) pickMessage() string {
for _, key := range []condition{condWorker, condReceive, condProcess, condDelete, condFinalize, condPoison} {
if c, ok := h.conditions[key]; ok {
return c.msg
}
}
return "Input is degraded"
}
// publish sends the status to the underlying reporter if it differs from
// the last published state. This replaces StatusReporterHelper's dedup.
func (h *sqsHealth) publish(s status.Status, msg string) {
if s == h.currentStatus && msg == h.currentMsg {
return
}
h.currentStatus = s
h.currentMsg = msg
h.log.Debugw("health status", "status", s, "msg", msg)
h.reporter.UpdateStatus(s, msg)
}
func isShutdownErr(err error) bool {
return errors.Is(err, context.Canceled)
}