-
Notifications
You must be signed in to change notification settings - Fork 149
/
truncating_buffer.go
168 lines (146 loc) · 4.61 KB
/
truncating_buffer.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
package truncatingbuffer
import (
"fmt"
"log"
"sync"
"time"
"github.com/cloudfoundry/dropsonde/emitter"
"github.com/cloudfoundry/dropsonde/metrics"
"github.com/cloudfoundry/sonde-go/events"
"github.com/gogo/protobuf/proto"
)
var lgrSource = proto.String("LGR")
type TruncatingBuffer struct {
inputChannel <-chan *events.Envelope
context BufferContext
outputChannel chan *events.Envelope
lock *sync.RWMutex
bufferSize uint64
sentMessageCount uint64
queuedInternalMessageCount uint64
droppedMessageCount uint64
stopChannel chan struct{}
}
func NewTruncatingBuffer(inputChannel <-chan *events.Envelope, bufferSize uint, context BufferContext, stopChannel chan struct{}) *TruncatingBuffer {
if bufferSize < 3 {
panic("bufferSize must be larger than 3 for overflow")
}
if context == nil {
panic("context should not be nil")
}
return &TruncatingBuffer{
inputChannel: inputChannel,
outputChannel: make(chan *events.Envelope, bufferSize),
lock: &sync.RWMutex{},
bufferSize: uint64(bufferSize),
sentMessageCount: 0,
queuedInternalMessageCount: 0,
droppedMessageCount: 0,
stopChannel: stopChannel,
context: context,
}
}
func (r *TruncatingBuffer) GetOutputChannel() <-chan *events.Envelope {
return r.outputChannel
}
func (r *TruncatingBuffer) closeOutputChannel() {
close(r.outputChannel)
}
func (r *TruncatingBuffer) eventAllowed(eventType events.Envelope_EventType) bool {
return r.context.EventAllowed(eventType)
}
func (r *TruncatingBuffer) Run() {
defer r.closeOutputChannel()
for {
select {
case <-r.stopChannel:
return
case msg, ok := <-r.inputChannel:
if !ok {
return
}
if r.eventAllowed(msg.GetEventType()) {
r.forwardMessage(msg)
}
}
}
}
func (r *TruncatingBuffer) forwardMessage(msg *events.Envelope) {
select {
case r.outputChannel <- msg:
r.sentMessageCount++
queuedInternalMessageWasSent := (r.sentMessageCount+r.queuedInternalMessageCount > r.bufferSize)
if r.queuedInternalMessageCount > 0 && queuedInternalMessageWasSent {
r.queuedInternalMessageCount--
}
default:
deltaDropped := (r.dropMessages() - r.queuedInternalMessageCount)
r.queuedInternalMessageCount = 0
r.lock.Lock()
r.droppedMessageCount += deltaDropped
appId := r.context.AppID(msg)
r.notifyMessagesDropped(r.outputChannel, deltaDropped, r.droppedMessageCount, appId)
totalDropped := r.droppedMessageCount
r.lock.Unlock()
r.outputChannel <- msg
r.sentMessageCount = 1
log.Printf("TruncatingBuffer: deltaDropped=%d totalDropped=%d appId=%s destination=%s",
deltaDropped,
totalDropped,
appId,
r.context.Destination(),
)
}
}
func (r *TruncatingBuffer) dropMessages() uint64 {
dropped := uint64(0)
for {
select {
case _, ok := <-r.outputChannel:
if !ok {
return dropped
}
dropped++
default:
return dropped
}
}
}
func (r *TruncatingBuffer) notifyMessagesDropped(outputChannel chan *events.Envelope, deltaDropped, totalDropped uint64, appId string) {
metrics.BatchAddCounter("TruncatingBuffer.totalDroppedMessages", deltaDropped)
if r.eventAllowed(events.Envelope_LogMessage) {
r.emitMessage(outputChannel, generateLogMessage(deltaDropped, totalDropped, appId, r.context.Origin(), r.context.Destination()))
}
if r.eventAllowed(events.Envelope_CounterEvent) {
r.emitMessage(outputChannel, generateCounterEvent(deltaDropped, totalDropped))
}
}
func (r *TruncatingBuffer) emitMessage(outputChannel chan *events.Envelope, event events.Event) {
env, err := emitter.Wrap(event, r.context.Origin())
if err != nil {
log.Printf("Error marshalling message: %v", err)
return
}
outputChannel <- env
r.queuedInternalMessageCount++
}
func generateLogMessage(deltaDropped, totalDropped uint64, appId, source, destination string) *events.LogMessage {
messageString := fmt.Sprintf("Log message output is too high. %d messages dropped (Total %d messages dropped) from %s to %s.", deltaDropped, totalDropped, source, destination)
messageType := events.LogMessage_ERR
currentTime := time.Now()
logMessage := &events.LogMessage{
Message: []byte(messageString),
AppId: &appId,
MessageType: &messageType,
SourceType: lgrSource,
Timestamp: proto.Int64(currentTime.UnixNano()),
}
return logMessage
}
func generateCounterEvent(delta, total uint64) *events.CounterEvent {
return &events.CounterEvent{
Name: proto.String("TruncatingBuffer.DroppedMessages"),
Delta: proto.Uint64(delta),
Total: proto.Uint64(total),
}
}