forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
383 lines (322 loc) · 10.5 KB
/
sender.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package writer
import (
"container/list"
"fmt"
"sync"
"time"
"github.com/ninnemana/datadog-agent/pkg/trace/watchdog"
"github.com/ninnemana/datadog-agent/pkg/trace/writer/backoff"
writerconfig "github.com/ninnemana/datadog-agent/pkg/trace/writer/config"
"github.com/ninnemana/datadog-agent/pkg/util/log"
)
// payload represents a data payload to be sent to some endpoint
type payload struct {
creationDate time.Time
bytes []byte
headers map[string]string
}
// newPayload constructs a new payload object with the provided data and with CreationDate initialized to the current
// time.
func newPayload(bytes []byte, headers map[string]string) *payload {
return &payload{
creationDate: time.Now(),
bytes: bytes,
headers: headers,
}
}
// eventType is a type of event sent down the monitor channel.
type eventType int
const (
eventTypeSuccess eventType = iota
eventTypeFailure
eventTypeRetry
)
var eventTypeStrings = map[eventType]string{
eventTypeSuccess: "success",
eventTypeFailure: "failure",
eventTypeRetry: "retry",
}
func (e eventType) String() string { return eventTypeStrings[e] }
type monitorEvent struct {
typ eventType
payload *payload
stats sendStats
err error
retryDelay time.Duration
retryNum int
}
// sendStats represents basic stats related to the sending of a payload.
type sendStats struct {
sendTime time.Duration
host string
}
// payloadSender represents an object capable of asynchronously sending payloads to some endpoint.
type payloadSender interface {
Start()
Run()
Stop()
Send(payload *payload)
Monitor() <-chan monitorEvent
setEndpoint(endpoint)
}
// queuableSender is a specific implementation of a payloadSender that will queue new payloads on error and
// retry sending them according to some configurable BackoffTimer.
type queuableSender struct {
conf writerconfig.QueuablePayloadSenderConf
mu sync.RWMutex // guards below group
queuedPayloads *list.List
currentQueuedSize int64
backoffTimer backoff.Timer
syncBarrier <-chan interface{} // used only in tests
wg sync.WaitGroup // tracks active sends
in chan *payload
monitorCh chan monitorEvent
endpoint endpoint
exit chan struct{}
}
// newSender constructs a new QueuablePayloadSender with custom configuration to send payloads to
// the provided endpoint.
func newSender(e endpoint, conf writerconfig.QueuablePayloadSenderConf) *queuableSender {
return &queuableSender{
conf: conf,
queuedPayloads: list.New(),
backoffTimer: backoff.NewCustomExponentialTimer(conf.ExponentialBackoff),
in: make(chan *payload, conf.InChannelSize),
monitorCh: make(chan monitorEvent),
endpoint: e,
exit: make(chan struct{}),
}
}
// Send sends a single isolated payload through this sender.
func (s *queuableSender) Send(payload *payload) {
s.in <- payload
}
// Stop asks this sender to stop and waits until it correctly stops.
func (s *queuableSender) Stop() {
close(s.in)
<-s.exit
s.wg.Wait()
close(s.monitorCh)
}
func (s *queuableSender) setEndpoint(e endpoint) {
s.endpoint = e
}
// Monitor allows an external entity to monitor events of this sender by receiving Sender*Event structs.
func (s *queuableSender) Monitor() <-chan monitorEvent {
return s.monitorCh
}
// send will send the provided payload without any checks.
func (s *queuableSender) doSend(payload *payload) (sendStats, error) {
if payload == nil {
return sendStats{}, nil
}
startFlush := time.Now()
err := s.endpoint.write(payload)
sendStats := sendStats{
sendTime: time.Since(startFlush),
host: s.endpoint.baseURL(),
}
return sendStats, err
}
// Start asynchronously starts this QueueablePayloadSender.
func (s *queuableSender) Start() {
go func() {
defer watchdog.LogOnPanic()
s.Run()
}()
}
// Run executes the queuableSender main logic synchronously.
func (s *queuableSender) Run() {
defer close(s.exit)
sema := make(chan struct{}, s.conf.MaxConnections)
for {
select {
case payload, more := <-s.in:
if !more {
log.Info("Exiting payload sender, try flushing whatever is left")
s.flushQueue()
return
}
if payload == nil {
continue
}
s.mu.RLock()
queuing := s.queuedPayloads.Len() > 0
s.mu.RUnlock()
if queuing {
if err := s.enqueue(payload); err != nil {
log.Debugf("Error while queueing payload: %v", err)
s.notifyError(payload, err)
}
continue
}
sema <- struct{}{}
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := s.trySend(payload); err != nil {
log.Debugf("Error while sending or queueing payload. err=%v", err)
s.notifyError(payload, err)
}
<-sema
}()
case <-s.backoffTimer.ReceiveTick():
s.flushQueue()
case <-s.syncBarrier:
// TODO: Is there a way of avoiding this? I want Promises in Go :(((
// This serves as a barrier (assuming syncBarrier is an unbuffered channel). Used for testing
continue
}
}
}
// trySend sends the provided payload or queues it if this sender is currently queueing payloads.
func (s *queuableSender) trySend(payload *payload) error {
stats, err := s.doSend(payload)
if err != nil {
if _, ok := err.(*retriableError); ok {
// start a retry queue
s.mu.Lock()
defer s.mu.Unlock()
retryNum, delay := s.backoffTimer.ScheduleRetry(err)
log.Debugf("Got retriable error. Starting a queue. delay=%s, err=%v", delay, err)
s.notifyRetry(payload, err, delay, retryNum)
return s.enqueueLocked(payload)
}
return err
}
log.Tracef("Successfully sent direct payload: %v", payload)
s.notifySuccess(payload, stats)
return nil
}
func (s *queuableSender) enqueue(p *payload) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.enqueueLocked(p)
}
func (s *queuableSender) enqueueLocked(payload *payload) error {
// Start by discarding payloads that are too old, freeing up memory
s.discardOldPayloads()
for s.conf.MaxQueuedPayloads > 0 && s.queuedPayloads.Len() >= s.conf.MaxQueuedPayloads {
log.Debugf("Dropping existing payload because max queued payloads reached: %d", s.conf.MaxQueuedPayloads)
if _, err := s.dropOldestPayload("max queued payloads reached"); err != nil {
panic(fmt.Errorf("unable to respect max queued payloads value of %d", s.conf.MaxQueuedPayloads))
}
}
newPayloadSize := int64(len(payload.bytes))
if s.conf.MaxQueuedBytes > 0 && newPayloadSize > s.conf.MaxQueuedBytes {
log.Debugf("Payload bigger than max size: size=%d, max size=%d", newPayloadSize, s.conf.MaxQueuedBytes)
return fmt.Errorf("unable to queue payload bigger than max size: payload size=%d, max size=%d",
newPayloadSize, s.conf.MaxQueuedBytes)
}
for s.conf.MaxQueuedBytes > 0 && s.currentQueuedSize+newPayloadSize > s.conf.MaxQueuedBytes {
if _, err := s.dropOldestPayload("max queued bytes reached"); err != nil {
// Should never happen because we know we can fit it in
panic(fmt.Errorf("unable to find space for queueing payload of size %d: %v", newPayloadSize, err))
}
}
log.Tracef("Queuing new payload: %v", payload)
s.queuedPayloads.PushBack(payload)
s.currentQueuedSize += newPayloadSize
return nil
}
func (s *queuableSender) flushQueue() error {
s.mu.Lock()
defer s.mu.Unlock()
log.Debugf("Attempting to flush queue with %d payloads", s.queuedPayloads.Len())
// Start by discarding payloads that are too old
s.discardOldPayloads()
// For the remaining ones, try to send them one by one
var next *list.Element
for e := s.queuedPayloads.Front(); e != nil; e = next {
payload := e.Value.(*payload)
var err error
var stats sendStats
if stats, err = s.doSend(payload); err != nil {
if _, ok := err.(*retriableError); ok {
// If send failed due to a retriable error, retry flush later
retryNum, delay := s.backoffTimer.ScheduleRetry(err)
log.Debugf("Got retriable error. Retrying flush later: retry=%d, delay=%s, err=%v",
retryNum, delay, err)
s.notifyRetry(payload, err, delay, retryNum)
// Don't try to send following. We'll flush all later.
return err
}
// If send failed due to non-retriable error, notify error and drop it
log.Debugf("Dropping payload due to non-retriable error: err=%v, payload=%v", err, payload)
s.notifyError(payload, err)
next = s.removeQueuedPayload(e)
// Try sending next ones
continue
}
// If successful, remove payload from queue
log.Tracef("Successfully sent a queued payload: %v", payload)
s.notifySuccess(payload, stats)
next = s.removeQueuedPayload(e)
}
s.backoffTimer.Reset()
return nil
}
func (s *queuableSender) removeQueuedPayload(e *list.Element) *list.Element {
next := e.Next()
payload := e.Value.(*payload)
s.currentQueuedSize -= int64(len(payload.bytes))
s.queuedPayloads.Remove(e)
return next
}
// Discard those payloads that are older than max age.
func (s *queuableSender) discardOldPayloads() {
// If MaxAge <= 0 then age limitation is disabled so do nothing
if s.conf.MaxAge <= 0 {
return
}
var next *list.Element
for e := s.queuedPayloads.Front(); e != nil; e = next {
payload := e.Value.(*payload)
age := time.Since(payload.creationDate)
// Payloads are kept in order so as soon as we find one that isn't, we can break out
if age < s.conf.MaxAge {
break
}
err := fmt.Errorf("payload is older than max age: age=%v, max age=%v", age, s.conf.MaxAge)
log.Tracef("Discarding payload: err=%v, payload=%v", err, payload)
s.notifyError(payload, err)
next = s.removeQueuedPayload(e)
}
}
// Payloads are kept in order so dropping the one at the front guarantees we're dropping the oldest
func (s *queuableSender) dropOldestPayload(reason string) (*payload, error) {
if s.queuedPayloads.Len() == 0 {
return nil, fmt.Errorf("no queued payloads")
}
err := fmt.Errorf("payload dropped: %s", reason)
droppedPayload := s.queuedPayloads.Front().Value.(*payload)
s.removeQueuedPayload(s.queuedPayloads.Front())
s.notifyError(droppedPayload, err)
return droppedPayload, nil
}
func (s *queuableSender) notifySuccess(payload *payload, sendStats sendStats) {
s.sendEvent(&monitorEvent{
typ: eventTypeSuccess,
payload: payload,
stats: sendStats,
})
}
func (s *queuableSender) notifyError(payload *payload, err error) {
s.sendEvent(&monitorEvent{
typ: eventTypeFailure,
payload: payload,
err: err,
})
}
func (s *queuableSender) notifyRetry(payload *payload, err error, delay time.Duration, retryNum int) {
s.sendEvent(&monitorEvent{
typ: eventTypeRetry,
payload: payload,
err: err,
retryDelay: delay,
retryNum: retryNum,
})
}
func (s *queuableSender) sendEvent(event *monitorEvent) {
s.monitorCh <- *event
}